基於Verilog的陣列乘法器設計

里程。。發表於2020-01-17

陣列乘法器設計

參照計算機組成與系統結構這本書84頁的一個框圖,相關解析可以參照這本書,下面提供自己根據框圖寫的程式碼,其實也很簡單,照圖施工的過程。
陣列乘法器框圖

//全加器模組程式碼
module full_adder(
    input a0,
	input b0,
	input c0,
	output s1,
	output c1
);
    
    assign c1 = a0 & b0 | c0 & (a0 ^ b0);
	assign s1 = c0 ^ a0 ^ b0;

endmodule 
//乘法器子模組程式碼
module multiple_part(X,Y,pro,c0,c1,s1);
   
  input X;
  input Y;
  input pro;
  input c0;
  output c1;
  output s1;
 
  wire xy;
  

  full_adder full_adder(
   .a0(xy),
	.b0(pro),
	.c0(c0),
	.s1(s1),
	.c1(c1)
);
 assign xy = X  &  Y;

endmodule 
//頂層模組
module mutiple_top(
   input wire[0:3] x,
	input wire [0:3] y,
	output wire  [7:0] P
);
  
  wire c3_4;
  wire c0_1;
  wire c1_2;
  wire c2_3;
  wire s1_7;
  wire s2_6;
  wire s3_5;
  wire c7_6;
  wire c6_5;
  wire s6_8;
  wire c5_4;
  wire s5_9;
  wire c8_9;
  wire s9_15;
  wire s4_10;
  wire c10_11;
  wire s10_14;
  wire c4_11;
  wire s11_13;
  wire c14_13;
  wire c13_12;
  wire c9_10;
  wire c11_12;
  wire c15_14;
  multiple_part  multiple_part0(
       .X(x[3]),
		 .Y(y[3]),
		 .pro(0),
		 .c0(0),
		 .c1(c0_1),
		 .s1(P[0])
);

  
   multiple_part  multiple_part1(
       .X(x[2]),
		 .Y(y[3]),
		 .pro(0),
		 .c0(c0_1),
		 .c1(c1_2),
		 .s1(s1_7)
);

     multiple_part  multiple_part2(
       .X(x[1]),
		 .Y(y[3]),
		 .pro(0),
		 .c0(c1_2),
		 .c1(c2_3),
		 .s1(s2_6)
);

     multiple_part  multiple_part3(
       .X(x[0]),
		 .Y(y[3]),
		 .pro(0),
		 .c0(c2_3),
		 .c1(c3_4),
		 .s1(s3_5)
);

  
    multiple_part  multiple_part4(
       .X(x[0]),
		 .Y(y[2]),
		 .pro(c3_4),
		 .c0(c5_4),
		 .c1(c4_11),
		 .s1(s4_10)
);


    multiple_part  multiple_part5(
       .X(x[1]),
		 .Y(y[2]),
		 .pro(s3_5),
		 .c0(c6_5),
		 .c1(c5_4),
		 .s1(s5_9)
);

     multiple_part  multiple_part6(
       .X(x[2]),
		 .Y(y[2]),
		 .pro(s2_6),
		 .c0(c7_6),
		 .c1(c6_5),
		 .s1(s6_8)
);

    multiple_part  multiple_part7(
       .X(x[3]),
		 .Y(y[2]),
		 .pro(s1_7),
		 .c0(0),
		 .c1(c7_6),
		 .s1(P[1])
);

     multiple_part  multiple_part8(
       .X(x[3]),
		 .Y(y[1]),
		 .pro(s6_8),
		 .c0(0),
		 .c1(c8_9),
		 .s1(P[2])
);

    multiple_part  multiple_part9(
       .X(x[2]),
		 .Y(y[1]),
		 .pro(s5_9),
		 .c0(c8_9),
		 .c1(c9_10),
		 .s1(s9_15)
);

    multiple_part  multiple_part10(
       .X(x[1]),
		 .Y(y[1]),
		 .pro(s4_10),
		 .c0(c9_10),
		 .c1(c10_11),
		 .s1(s10_14)
);
  
    multiple_part  multiple_part11(
       .X(x[0]),
		 .Y(y[1]),
		 .pro(c4_11),
		 .c0(c10_11),
		 .c1(c11_12),
		 .s1(s11_13)
);

    multiple_part  multiple_part12(
       .X(x[0]),
		 .Y(y[0]),
		 .pro(c11_12),
		 .c0(c13_12),
		 .c1(P[7]),
		 .s1(P[6])
);

    multiple_part  multiple_part13(
       .X(x[1]),
		 .Y(y[0]),
		 .pro(s11_13),
		 .c0(c14_13),
		 .c1(c13_12),
		 .s1(P[5])
);
  
    multiple_part  multiple_part14(
       .X(x[2]),
		 .Y(y[0]),
		 .pro(s10_14),
		 .c0(c15_14),
		 .c1(c14_13),
		 .s1(P[4])
);
  
    multiple_part  multiple_part15(
       .X(x[3]),
		 .Y(y[0]),
		 .pro(s9_15),
		 .c0(0),
		 .c1(c15_14),
		 .s1(P[3])
);
endmodule 
//測試模組程式碼
`timescale 1ns/1ns
module mutiple_top_tb;
 
  reg [3:0] x;
  reg [3:0] y;
  wire [7:0] P;
  integer i ,j;


 mutiple_top mutiple_top(
    .x(x),
	 .y(y),
	 .P(P)
);

initial begin
   x = 4'd0;
	y = 4'd0;
	
	#5;
	for (i = 0; i < 16; i = i + 1)begin
	    for (j = 0 ; j < 16; j = j + 1) begin
		     y = y + 1;
			  #5;
		 end
		 
		 x = x + 1;
		 #5;
	end	
	$stop;
end
endmodule 

``

模擬波形圖
模擬波形圖
由波形圖可以看出,x乘以y的結果是等於p的,可知模擬正確。

相關文章