-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubleq.v
106 lines (96 loc) · 2.86 KB
/
subleq.v
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
100
101
102
103
104
105
106
module subleq(
input iClock,
input iReset,
output [2:0] ocounter,
output [31:0] oIP,
output [31:0] oA,
output [31:0] oB,
output [31:0] oJ,
output [31:0] oq,
output [31:0] osub,
output oleq
);
// =============================================================================
// outputs
// =============================================================================
assign ocounter = counter;
assign oIP = IP;
assign oA = A;
assign oB = B;
assign oJ = J;
assign oq = q;
assign osub = sub;
assign oleq = leq;
// =============================================================================
// counter
// =============================================================================
reg [2:0] counter;
initial begin
counter <= 3'd0;
end
always @(posedge iClock) begin
counter <= (iReset || counter == 3'd5) ? 3'd0 : counter + 3'd1;
end
// =============================================================================
// IP
// =============================================================================
reg [31:0] IP;
initial begin
IP <= 32'd0;
end
always @(posedge iClock) begin
if (iReset) begin
IP <= 32'd0;
end else if (counter <= 3'd2) begin // in the first 3 cycles, increment
IP <= IP + 32'd1;
end else if (counter == 3'd5) begin // in the last cycle, jump or continue
IP <= leq ? J : IP;
end
end
// =============================================================================
// memory
// =============================================================================
wire [31:0] address;
wire [31:0] q;
always @(counter, IP, A, B) begin
if (counter <= 3'd2) begin // in the first 3 cycles, address comes from IP
address <= IP;
end else if (counter == 3'd3) begin // in the fourth cycle, read A
address <= A;
end else begin // in the fifth cycle, read B. in the last cycle, write B
address <= B;
end
end
mem mem0(
.address(address[12:0]),
.clock(iClock),
.data(sub),
.wren(counter == 3'd5), // write in the last cycle
.q(q)
);
// =============================================================================
// auxiliary registers
// =============================================================================
reg [31:0] A, B, J;
always @(posedge iClock) begin
// register A. write A address in the second cycle and A itself in the fifth
if (counter == 3'd1 || counter == 3'd4) begin
A <= q;
end
// register B. write B address in the third cycle
if (counter == 3'd2) begin
B <= q;
end
// register J. write the jump address in the fourth cycle
if (counter == 3'd3) begin
J <= q;
end
end
// =============================================================================
// subleq
// =============================================================================
wire [31:0] sub;
assign sub = q - A;
wire leq;
assign leq = sub[31] || sub == 32'b0;
endmodule