[笔记].算法 - 加法器.[Verilog]
0赞
发表于 8/23/2010 4:46:05 PM
阅读(2452)
出自Quartus II自带模板。
1 有符号数加法器
01 |
module signed_adder |
02 |
#( parameter WIDTH=16) |
03 |
( |
04 |
input signed [WIDTH-1:0] dataa, |
05 |
input signed [WIDTH-1:0] datab, |
06 |
input cin, |
07 |
output [WIDTH:0] result |
08 |
); |
09 |
10 |
assign result = dataa + datab + cin; |
11 |
12 |
endmodule |
2 无符号数加法器
01 |
module unsigned_adder |
02 |
#( parameter WIDTH=16) |
03 |
( |
04 |
input [WIDTH-1:0] dataa, |
05 |
input [WIDTH-1:0] datab, |
06 |
input cin, |
07 |
output [WIDTH:0] result |
08 |
); |
09 |
10 |
assign result = dataa + datab + cin; |
11 |
12 |
endmodule |
3 有符号数加法/减法器
01 |
module signed_adder_subtractor |
02 |
#( parameter WIDTH=16) |
03 |
( |
04 |
input signed [WIDTH-1:0] dataa, |
05 |
input signed [WIDTH-1:0] datab, |
06 |
input add_sub, // if this is 1, add; else subtract |
07 |
input clk, |
08 |
output reg [WIDTH:0] result |
09 |
); |
10 |
11 |
always @ ( posedge clk) |
12 |
begin |
13 |
if (add_sub) |
14 |
result <= dataa + datab; |
15 |
else |
16 |
result <= dataa - datab; |
17 |
end |
18 |
19 |
endmodule |
无符号数加法/减法器
01 |
module unsigned_adder_subtractor |
02 |
#( parameter WIDTH=16) |
03 |
( |
04 |
input [WIDTH-1:0] dataa, |
05 |
input [WIDTH-1:0] datab, |
06 |
input add_sub, // if this is 1, add; else subtract |
07 |
input clk, |
08 |
output reg [WIDTH:0] result |
09 |
); |
10 |
11 |
always @ ( posedge clk) |
12 |
begin |
13 |
if (add_sub) |
14 |
result <= dataa + datab; |
15 |
else |
16 |
result <= dataa - datab; |
17 |
end |
18 |
19 |
endmodule |
二进制流水线加法树
01 |
module pipelined_binary_adder_tree |
02 |
#( parameter WIDTH=16) |
03 |
( |
04 |
input [WIDTH-1:0] A, B, C, D, E, |
05 |
input clk, |
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 @ ( posedge clk) |
13 |
begin |
14 |
sumreg1 <= sum1; |
15 |
sumreg2 <= sum2; |
16 |
sumreg3 <= sum3; |
17 |
sumreg4 <= sum4; |
18 |
end |
19 |
20 |
// 2-bit additions |
21 |
assign sum1 = A + B; |
22 |
assign sum2 = C + D; |
23 |
assign sum3 = sumreg1 + sumreg2; |
24 |
assign sum4 = sumreg3 + E; |
25 |
assign out = sumreg4; |
26 |
27 |
endmodule |