-
Notifications
You must be signed in to change notification settings - Fork 2
/
tinyalu.sv
99 lines (76 loc) · 1.98 KB
/
tinyalu.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
91
92
93
94
95
96
97
98
99
module tinyalu (input [7:0] A,
input [7:0] B,
input [2:0] op,
input clk,
input reset_n,
input start,
output done,
output [15:0] result);
wire [15:0] result_aax, result_mult;
wire start_single, start_mult;
assign start_single = start & ~op[2];
assign start_mult = start & op[2];
single_cycle and_add_xor (.A, .B, .op, .clk, .reset_n, .start(start_single),
.done(done_aax), .result(result_aax));
three_cycle mult (.A, .B, .op, .clk, .reset_n, .start(start_mult),
.done(done_mult), .result(result_mult));
assign done = (op[2]) ? done_mult : done_aax;
assign result = (op[2]) ? result_mult : result_aax;
endmodule // tinyalu
module single_cycle(input [7:0] A,
input [7:0] B,
input [2:0] op,
input clk,
input reset_n,
input start,
output logic done,
output logic [15:0] result);
always @(posedge clk)
if (!reset_n)
result <= 0;
else
case(op)
3'b001 : result <= A + B + 1'b1;
3'b010 : result <= A & B;
3'b011 : result <= A ^ B;
endcase // case (op)
always @(posedge clk)
if (!reset_n)
done <= 0;
else
done = ((start == 1'b1) && (op != 3'b000));
endmodule : single_cycle
module three_cycle(input [7:0] A,
input [7:0] B,
input [2:0] op,
input clk,
input reset_n,
input start,
output logic done,
output logic [15:0] result);
logic [7:0] a_int, b_int;
logic [15:0] mult1, mult2;
logic done1, done2, done3;
always @(posedge clk)
if (!reset_n) begin
done <= 0;
done3 <= 0;
done2 <= 0;
done1 <= 0;
a_int <= 0;
b_int <= 0;
mult1 <= 0;
mult2 <= 0;
result<= 0;
end else begin // if (!reset_n)
a_int <= A;
b_int <= B;
mult1 <= a_int * b_int;
mult2 <= mult1;
result <= mult2;
done3 <= start & !done;
done2 <= done3 & !done;
done1 <= done2 & !done;
done <= done1 & !done;
end // else: !if(!reset_n)
endmodule : three_cycle