module multiplier(input clk,rst,input [7:0] A,B,output [16:0] C);reg [3:0] cnt;reg [16:0] temp;always@(posedge clk or negedge rst)if(!rst)begincnt<=0;temp<=0;endelse if(cnt==8)cnt<=0;else if(B[cnt])begintemp<=temp+(A<<cnt);cnt<=cnt+1;endelsecnt<=cnt+1;assign C=(cnt==8)?temp:0;endmodule
8-digit multiplier, 17-digit product
1 `timescale 1ns/1ns 2 module multiplier_tb; 3 reg [7:0] A,B; 4 reg clk,rst; 5 wire [16:0] C; 6 7 multiplier U( 8 .A(A), 9 .B(B),10 .clk(clk),11 .rst(rst),12 .C(C)13 );14 15 initial16 begin17 rst=0;18 clk=0;19 A=8‘hfc;20 B=8‘hcf;21 #5 rst=1;22 end23 always24 #10 clk=~clk;25 26 endmodule
Test Platform
In the figure, the product is output for 8th pulses. Why? It can be seen from the program that temp and CNT are assigned a value in a process, that is, after the arrival of the 8th CLK, temp and CNT are assigned a value at the same time. One is the product of the result, one is CNT = 8; and because of the immediacy of assign, the product has been obtained in the 8th clks.
Considering the bitwise right of the multiplier, elementary school knows that the multiplication formula should be misaligned. Why don't you know it now !!
Simple unsigned multiplier (how come there are always omissions, God! Level up is required !)