安德鲁

[笔记].算法 - 加法器.[Verilog]

0
阅读(2262)

出自Quartus II自带模板。

1 有符号数加法器


01 modulesigned_adder
02 #(parameterWIDTH=16)
03 (
04 inputsigned [WIDTH-1:0] dataa,
05 inputsigned [WIDTH-1:0] datab,
06 inputcin,
07 output[WIDTH:0] result
08 );
09
10 assignresult = dataa + datab + cin;
11
12 endmodule

2 无符号数加法器


01 moduleunsigned_adder
02 #(parameterWIDTH=16)
03 (
04 input[WIDTH-1:0] dataa,
05 input[WIDTH-1:0] datab,
06 inputcin,
07 output[WIDTH:0] result
08 );
09
10 assignresult = dataa + datab + cin;
11
12 endmodule

3 有符号数加法/减法器


01 modulesigned_adder_subtractor
02 #(parameterWIDTH=16)
03 (
04 inputsigned [WIDTH-1:0] dataa,
05 inputsigned [WIDTH-1:0] datab,
06 inputadd_sub,// if this is 1, add; else subtract
07 inputclk,
08 outputreg[WIDTH:0] result
09 );
10
11 always@ (posedgeclk)
12 begin
13 if(add_sub)
14 result <= dataa + datab;
15 else
16 result <= dataa - datab;
17 end
18
19 endmodule

无符号数加法/减法器


01 moduleunsigned_adder_subtractor
02 #(parameterWIDTH=16)
03 (
04 input[WIDTH-1:0] dataa,
05 input[WIDTH-1:0] datab,
06 inputadd_sub,// if this is 1, add; else subtract
07 inputclk,
08 outputreg[WIDTH:0] result
09 );
10
11 always@ (posedgeclk)
12 begin
13 if(add_sub)
14 result <= dataa + datab;
15 else
16 result <= dataa - datab;
17 end
18
19 endmodule

二进制流水线加法树


01 modulepipelined_binary_adder_tree
02 #(parameterWIDTH=16)
03 (
04 input[WIDTH-1:0] A, B, C, D, E,
05 inputclk,
06 output[WIDTH-1:0] out
07 );
08
09 wire[WIDTH-1:0] sum1, sum2, sum3, sum4;
10 reg[WIDTH-1:0] sumreg1, sumreg2, sumreg3, sumreg4;
11
12 always@ (posedgeclk)
13 begin
14 sumreg1 <= sum1;
15 sumreg2 <= sum2;
16 sumreg3 <= sum3;
17 sumreg4 <= sum4;
18 end
19
20 // 2-bit additions
21 assignsum1 = A + B;
22 assignsum2 = C + D;
23 assignsum3 = sumreg1 + sumreg2;
24 assignsum4 = sumreg3 + E;
25 assignout = sumreg4;
26
27 endmodule
Baidu
map