乘法器

先看一个2个4位的乘法器,得到的结果是8位二进制:

a:               1 1 1 1
b:               1 1 0 1
---------------------
                  1 1 1 1     //-> result = result + (a<<0),result[3:0]加的是a移位0之后的1 1 1 1,得到结果I,
               0 0 0 0    // 此位乘的结果是0,不作处理;
            1 1 1 1 0 0 //  -> result = result + (a<<2),result[3:0]加的是1 1 0 0,移除的两个1其实已经到了reault[5:4],在和上次得到的结果相加,得到结果II;
         1 1 10 0 0 // -> result = result + (a<<3),result[3:0]加的是1 0 0 0,移除的三个1其实已经到了reault[6:4],结果III;
--------------------
a*b 1 1 0 0 0 0 1 1    

I: result[7:0] = 8'b0 0 0 0_1 1 1 1;//a<<0

     result[7:0] = 8'b0 0 1 1_1 1 0 0;

+----------------------------------------

II:result[7:0]= 8'b0 1 0 0_1 0 1 1;//a<<2

    result[7:0] = 8'b0 1 1 1_1 0 0 0;

+-----------------------------------------

III:result[7:0]=8'b1 1 0 0_0 0 1 1;//a<<3

2个8位的乘法实验代码实现:

//result = a * b
module mux(rst_n,a,b,result);
input [7:0] a;
input [7:0] b;
input rst_n;
output [15:0] result;
 
integer i;
reg [15:0] result;
always @ (a or b or rst_n)
if(!rst_n)
    result = 16'd0;
else
begin
  result = 16'd0;
for(i=0; i<8; i=i+1)
    if(b[i])//先判断该位是否为0,若为0,不进行任何操作
        result = result + (a<<i);
end
endmodule

仿真测试文件:

`timescale 1ns/1ns
module mux_top;
reg rst_n;
reg [7:0] a;
reg [7:0] b;
reg [3:0] c;
reg [4:0] d;
wire [15:0] result;

initial begin
  c = 4'b1101;
  $display("c= %b",c);
  d = 5'b00000;
  $display("d=%b",d);
  d = c<<1;//看c移位之后,看d的结果
  $display("move after:");
  $display("c= %b",c);
  $display("d=%b",d);
end

initial begin
    rst_n = 0;
    #10;
    rst_n = 1;
end

initial begin
    for(a=1; a<255; a=a+2)
        #10;
end

initial begin
    for(b=0; b<255; b=b+1)
        #10;
end
        
mux m1(
        .rst_n(rst_n),
        .a(a),
        .b(b),
        .result(result)
        );
endmodule

仿真结果:

d=c<<1执行完后,d的结果是11010,也就C移出的一位到了d[4].

仿真波形:

原文地址:https://www.cnblogs.com/wen2376/p/3264979.html