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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
`include "util.svh"
module top
#( ADDR_BITS = 14
, DATA_BITS = 12
)
( input bit clk // verilator public
, input bit reset_n // verilator public
);
bit reset = 0;
bit have_reset = 0;
always_ff @(posedge clk) if (reset) have_reset <= 1;
assign reset = !reset_n || !have_reset;
bit mem_ready; `define mem_ready `past(mem_ready)
bit mem_valid;
bit mem_write;
bit [ADDR_BITS-1:0] mem_address;
bit [DATA_BITS-1:0] mem_write_data;
bit mem_read_valid; `define mem_read_valid `past(mem_read_valid)
bit [DATA_BITS-1:0] mem_read_data; `define mem_read_data `past(mem_read_data)
mem
#( .ADDR_BITS(ADDR_BITS)
, .DATA_BITS(DATA_BITS)
, .INIT_FILE("mem/mem.hex")
)
memory
( .clk(clk)
, .reset(reset)
, .ready(mem_ready)
, .valid(mem_valid)
, .address(mem_address)
, .write(mem_write)
, .write_data(mem_write_data)
, .read_valid(mem_read_valid)
, .read_data(mem_read_data)
);
bit rx_ready;
bit rx_valid;
bit [7:0] rx_data;
bit tx_ready;
bit tx_valid;
bit [7:0] tx_data;
jtag_uart
#( .INSTANCE(0)
) uart0
( .clk(clk)
, .reset(reset)
, .rx_ready(rx_ready)
, .rx_valid(rx_valid) `define rx_valid `past(rx_valid)
, .rx_data(rx_data) `define rx_data `past(rx_data)
, .tx_ready(tx_ready) `define tx_ready `past(tx_ready)
, .tx_valid(tx_valid)
, .tx_data(tx_data)
);
bit [DATA_BITS-1:0] pc;
bit [3:0] opcode;
bit [7:0] operand;
bit [DATA_BITS-1:0] acc;
bit [DATA_BITS-1:0] idx;
bit [ADDR_BITS-1:0] address;
bit [DATA_BITS-1:0] sign_extended_operand;
enum
{ FETCH
, DECODE
, INDIRECT
, INDIRECTED
, AGEN
, MEMORY
, HALT
} state;
always_ff @(posedge clk) begin
if (reset) begin
mem_valid = 0;
rx_ready = 0;
tx_valid = 0;
tx_data = 0;
pc = 0;
acc = 0;
idx = 0;
state = state.first;
end else begin
`ifdef DEBUG $display("s=%0d pc=%x (acc=%x idx=%x) (mem %b:%x)", state, pc, acc, idx, `mem_read_valid, `mem_read_data); `endif
if (`tx_ready) tx_valid = 0;
case (state)
FETCH: begin
`ifdef DEBUG $display("\tfetch"); `endif
mem_valid = 1;
mem_address = {2'b0, pc};
mem_write = 0;
if (`mem_ready) begin
state = DECODE;
++pc;
end
end
DECODE: begin
mem_valid = 0;
mem_write = 0;
if (`mem_read_valid) begin
state = FETCH;
{opcode, operand} = `mem_read_data;
sign_extended_operand = {{(DATA_BITS-8){operand[7]}}, operand};
`ifdef DEBUG $display("\tdecode %x:%x", opcode, operand); `endif
case (opcode)
'h0: acc = sign_extended_operand;
'h1, 'h2: begin
address = {7'b0, operand[6:0]};
state = operand[7] ? INDIRECT : AGEN;
end
'h3: if (acc != sign_extended_operand) ++pc;
'h4: pc = pc + sign_extended_operand;
'h5: begin
mem_write_data = acc % 10 + 'h30;
acc = acc / 10;
address = {7'b0, operand[6:0]};
state = operand[7] ? INDIRECT : AGEN;
end
'hf: begin
if (operand[0]) ++acc;
if (operand[1]) --acc;
if (operand[2]) ++idx;
if (operand[3]) --idx;
if (operand[4]) {idx, acc} = {acc, idx};
if (operand[5]) idx = acc;
if (operand[6]) state = MEMORY;
if (operand[7]) state = HALT;
end
endcase
end
end
INDIRECT: begin
mem_valid = 1;
mem_write = 0;
mem_address = address;
state = `mem_ready ? INDIRECTED : INDIRECT;
end
INDIRECTED: begin
if (`mem_ready) begin
mem_valid = 0;
mem_write = 0;
end
if (`mem_read_valid) begin
address = `mem_read_data;
state = AGEN;
end
end
AGEN: begin
mem_valid = 0;
mem_write = 0;
state = FETCH;
`ifdef DEBUG $display("\tagen"); `endif
case (opcode)
'h1: begin
mem_valid = 1;
mem_address = address + idx;
state = `mem_ready ? MEMORY : AGEN;
end
'h2: begin
mem_valid = 1;
mem_address = address + idx;
mem_write = 1;
mem_write_data = acc;
state = `mem_ready ? FETCH : AGEN;
end
'h5: begin
mem_valid = 1;
mem_address = address + idx;
mem_write = 1;
state = `mem_ready ? FETCH : AGEN;
end
endcase
end
MEMORY: begin
if (`mem_ready) begin
mem_valid = 0;
mem_write = 0;
end
state = FETCH;
`ifdef DEBUG $display("\tstall"); `endif
case (opcode)
'h1: begin
if (`mem_read_valid) begin
acc = acc + `mem_read_data;
end else begin
state = MEMORY;
end
end
'hf: begin
if (operand[6]) begin
if (tx_valid) begin
state = MEMORY;
end else begin
tx_valid = 1;
tx_data = acc[7:0];
end
end
end
endcase
end
HALT: begin
$display("Reached halt state.");
$finish;
end
endcase
`ifdef DEBUG if (mem_valid) $display("\tmem addr=%x w=%b", mem_address, mem_write); `endif
end
end
endmodule
|