[笔记].算法 - 乘法器.[Verilog]
0赞
发表于 2010/8/24 9:53:53
阅读(5339)
出自Quartus II自带模板。
1. 无符号数乘法器
01 |
module unsigned_multiply |
02 |
#( parameter WIDTH=8) |
03 |
( |
04 |
input [WIDTH-1:0] dataa, |
05 |
input [WIDTH-1:0] datab, |
06 |
output [2*WIDTH-1:0] dataout |
07 |
); |
08 |
09 |
assign dataout = dataa * datab; |
10 |
11 |
endmodule |
2. 有符号数乘法器
01 |
module signed_multiply |
02 |
#( parameter WIDTH=8) |
03 |
( |
04 |
input signed [WIDTH-1:0] dataa, |
05 |
input signed [WIDTH-1:0] datab, |
06 |
output [2*WIDTH-1:0] dataout |
07 |
); |
08 |
09 |
assign dataout = dataa * datab; |
10 |
11 |
endmodule |
3. 带输入输出寄存器的无符号数乘法器
01 |
module unsigned_multiply_with_input_and_output_registers |
02 |
#( parameter WIDTH=8) |
03 |
( |
04 |
input clk, |
05 |
input [WIDTH-1:0] dataa, |
06 |
input [WIDTH-1:0] datab, |
07 |
output reg [2*WIDTH-1:0] dataout |
08 |
); |
09 |
10 |
// Declare input and output registers |
11 |
reg [WIDTH-1:0] dataa_reg; |
12 |
reg [WIDTH-1:0] datab_reg; |
13 |
wire [2*WIDTH-1:0] mult_out; |
14 |
15 |
// Store the result of the multiply |
16 |
assign mult_out = dataa_reg * datab_reg; |
17 |
18 |
// Update data |
19 |
always @ ( posedge clk) |
20 |
begin |
21 |
dataa_reg <= dataa; |
22 |
datab_reg <= datab; |
23 |
dataout <= mult_out; |
24 |
end |
25 |
26 |
endmodule |
4. 带输入输出寄存器的有符号数乘法器
01 |
module signed_multiply_with_input_and_output_registers |
02 |
#( parameter WIDTH=8) |
03 |
( |
04 |
input clk, |
05 |
input signed [WIDTH-1:0] dataa, |
06 |
input signed [WIDTH-1:0] datab, |
07 |
output reg signed [2*WIDTH-1:0] dataout |
08 |
); |
09 |
10 |
// Declare input and output registers |
11 |
reg signed [WIDTH-1:0] dataa_reg; |
12 |
reg signed [WIDTH-1:0] datab_reg; |
13 |
wire signed [2*WIDTH-1:0] mult_out; |
14 |
15 |
// Store the result of the multiply |
16 |
assign mult_out = dataa_reg * datab_reg; |
17 |
18 |
// Update data |
19 |
always @ ( posedge clk) |
20 |
begin |
21 |
dataa_reg <= dataa; |
22 |
datab_reg <= datab; |
23 |
dataout <= mult_out; |
24 |
end |
25 |
26 |
endmodule |
5. 复数乘法器
01 |
module multiplier_for_complex_numbers |
02 |
#( parameter WIDTH=18) |
03 |
( |
04 |
input clk, ena, |
05 |
input signed [WIDTH-1:0] dataa_real, dataa_img, |
06 |
input signed [WIDTH-1:0] datab_real, datab_img, |
07 |
output reg signed [2*WIDTH-1:0] dataout_real, dataout_img |
08 |
); |
09 |
10 |
always @ ( posedge clk) |
11 |
begin |
12 |
if (ena == 1) |
13 |
begin |
14 |
dataout_real = dataa_real * datab_real - dataa_img * datab_img; |
15 |
dataout_img = dataa_real * datab_img + datab_real * dataa_img; |
16 |
end |
17 |
end |
18 |
19 |
endmodule |