-
Notifications
You must be signed in to change notification settings - Fork 4
/
muldiv.sv
62 lines (58 loc) · 1.42 KB
/
muldiv.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import definitions::*;
module muldiv(
input funct3_op_muldiv_t muldiv_op_i,
input logic [31:0] in1_i,
input logic [31:0] in2_i,
output logic [31:0] out_o
);
logic [63:0] in1_64_signed;
logic [63:0] in2_64_signed;
logic [63:0] in1_64_unsigned;
logic [63:0] in2_64_unsigned;
logic [63:0] mul_ss;
logic [63:0] mul_uu;
logic [63:0] mul_su;
assign in1_64_signed = {{32{in1_i[31]}}, in1_i};
assign in2_64_signed = {{32{in2_i[31]}}, in2_i};
assign in1_64_unsigned = {32'd0, in1_i};
assign in2_64_unsigned = {32'd0, in2_i};
assign mul_ss = in1_64_signed * in2_64_signed;
assign mul_uu = in1_64_unsigned * in2_64_unsigned;
assign mul_su = in1_64_signed * in2_64_unsigned;
always_comb begin
priority case (muldiv_op_i)
FUNCT3_OP_MULDIV_MUL:
out_o = mul_ss[0 +: 32];
FUNCT3_OP_MULDIV_MULH:
out_o = mul_ss[32 +: 32];
FUNCT3_OP_MULDIV_MULHSU:
out_o = mul_su[32 +: 32];
FUNCT3_OP_MULDIV_MULHU:
out_o = mul_uu[32 +: 32];
FUNCT3_OP_MULDIV_DIV: begin
if (in2_i == 0)
out_o = -1;
else
out_o = $signed(in1_i) / $signed(in2_i);
end
FUNCT3_OP_MULDIV_DIVU: begin
if (in2_i == 0)
out_o = 'hFFFFFFFF;
else
out_o = in1_i / in2_i;
end
FUNCT3_OP_MULDIV_REM: begin
if (in2_i == 0)
out_o = in1_i;
else
out_o = $signed(in1_i) % $signed(in2_i);
end
FUNCT3_OP_MULDIV_REMU: begin
if (in2_i == 0)
out_o = in1_i;
else
out_o = in1_i % in2_i;
end
endcase
end
endmodule