TA的每日心情 | 开心 2021-1-16 22:44 |
---|
初级会员
- 积分
- 119
|
楼主 |
发表于 2018-6-12 21:47:59
|
显示全部楼层
自己回帖,已经搞定啦,哈哈哈哈
/********************************
Multiplier
31位定点乘法器
0-15位为小数,16-30位为整数,第31位为符号位
输出范围[-32768,32767],精度1.525e-5
********************************/
module Multiplier(
m_in_a,
m_in_b,
m_out
);
input [31:0]m_in_a;
input [31:0]m_in_b;
output [31:0]m_out;
reg a;
wire [47:0]b;
always@(*)
if(m_in_a[31] == m_in_b[31])
a = 1'b0;
else
a = 1'b1;
assign b = (m_in_a[30:0] * m_in_b[30:0]);
assign m_out = {a, b[46:16]};
endmodule
/********************************
Adder
31位有符号加法器
0-15位为小数,16-30位为整数,第31位为符号位
输出范围[-32768,32767],精度1.525e-5
********************************/
module Adder(
p_in_a,
p_in_b,
p_out
);
input [31:0]p_in_a;
input [31:0]p_in_b;
output reg[31:0]p_out;
reg [31:0]mid;
always@(*)
begin
if(!p_in_a[31])begin
// + +
if(!p_in_b[31])begin
mid = p_in_a + p_in_b;
if(!mid[31])
p_out = {1'b0, mid[30:0]};
else
p_out = 32'hefff_ffff;
end
// + -
else begin
if(p_in_a[30:0] >= p_in_b[30:0])begin
mid = (p_in_a + 32'h8000_0000) - p_in_b;
p_out = {1'b0, mid[30:0]};
end
else begin
mid = (p_in_b - 32'h8000_0000) - p_in_a;
p_out = {1'b1, mid[30:0]};
end
end
end
else begin
// - +
if(!p_in_b[31])begin
if(p_in_b[30:0] >= p_in_a[30:0])begin
mid = (p_in_b + 32'h8000_0000) - p_in_a;
p_out = {1'b0, mid[30:0]};
end
else begin
mid = (p_in_a - 32'h8000_0000) - p_in_b;
p_out = {1'b1, mid[30:0]};
end
end
// - -
else begin
mid = (p_in_a - 32'h8000_0000) + (p_in_b - 32'h8000_0000);
if(!mid[31])
p_out = {1'b1, mid[30:0]};
else
p_out = 32'hffff_ffff;
end
end
end
endmodule
|
|