-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.sv
90 lines (74 loc) · 1.88 KB
/
math.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// A few subunits you can use if necessary. Only important for very specific instructions
module mult (
input logic [63:0] A, B,
input logic doSigned, // 1: signed multiply 0: unsigned multiply
output logic [63:0] mult_low, mult_high
);
logic signed [63:0] signedA, signedB;
logic signed [127:0] signedResult;
logic [127:0] unsignedResult;
// --- Signed math ---
always_comb begin
signedA = A;
signedB = B;
signedResult = signedA * signedB;
end
// --- Unsigned math ---
always_comb
unsignedResult = A * B;
// --- Pick the right output ---
always_comb
if (doSigned)
{mult_high, mult_low} = signedResult;
else
{mult_high, mult_low} = unsignedResult;
endmodule
module shifter(
input logic [63:0] value,
input logic direction, // 0: left, 1: right
input logic [5:0] distance,
output logic [63:0] result
);
always_comb begin
if (direction == 0)
result = value << distance;
else
result = value >> distance;
end
endmodule
module shifter_testbench();
logic [63:0] value;
logic direction;
logic [5:0] distance;
logic [63:0] result;
shifter dut (.value, .direction, .distance, .result);
integer i, dir;
initial begin
value = 64'hDEADBEEFDECAFBAD;
for(dir=0; dir<2; dir++) begin
direction <= dir[0];
for(i=0; i<64; i++) begin
distance <= i; #10;
end
end
end
endmodule
module mult_testbench();
logic [63:0] A, B;
logic doSigned;
logic [63:0] mult_low, mult_high;
logic [127:0] fullVal;
mult dut (.A, .B, .doSigned, .mult_low, .mult_high);
assign fullVal = {mult_high, mult_low};
integer i;
initial begin
for(i=0; i<2; i++) begin
doSigned <= i[0];
A <= 0; B <= 0; #10;
A <= 1; B <= 2; #10;
A <= -1; B <= 1; #10;
A <= -1; B <= -1; #10;
A <= 5<<35; B <= 6<<35; #10;
end
end
endmodule