-
Notifications
You must be signed in to change notification settings - Fork 1
/
i8080_core.vhd
441 lines (338 loc) · 9.27 KB
/
i8080_core.vhd
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.i8080_util.all;
entity i8080_core is
port (
clk, en, reset: in std_logic;
fin: in i8080_core_in;
fout: out i8080_core_out := I8080_CORE_OUT_DEFAULT
);
end;
architecture rtl of i8080_core is
signal cs: cpu_control_signals := CPU_CONTROL_SIGNALS_DEFAULT;
signal cpuinfo: ucode_ctrl_cpuinfo := (
read_done => '0',
cond => false,
int => '0',
ie => '0'
);
-- Output address register
signal addr: std_logic_vector(15 downto 0) := X"0000";
-- Output buffer for 16 bit loads so we dont clobber addr above mid instruction
signal addr_buffer: std_logic_vector(15 downto 0) := X"0000";
-- Data input register
signal reg_di: std_logic_vector(7 downto 0) := X"00";
-- Data output register
signal reg_do: std_logic_vector(7 downto 0) := X"00";
-- Interrupt enable
signal reg_ie: std_logic := '0';
-- Instruction register
signal reg_ir: std_logic_vector(7 downto 0) := X"00";
-- Program counter
signal reg_pc: std_logic_vector(15 downto 0) := X"0000";
-- Temporary register for indirect loads
signal reg_temp: std_logic_vector(15 downto 0) := X"0000";
signal reg_flags: std_logic_vector(7 downto 0) := X"00";
-- Register file interface
signal rf_di, rf_do: std_logic_vector(7 downto 0) := X"00";
-- ALU interface
signal alu_result: std_logic_vector(7 downto 0) := X"00";
signal alu_lhs, alu_rhs: std_logic_vector(7 downto 0) := X"00";
signal flags: i8080_flags := ('0', '1', '0', '1', '0');
signal rp_tmp: std_logic_vector(15 downto 0) := X"0000";
signal curr_cc: i8080_cond_code;
signal curr_cc_value: boolean;
signal reg_pc_buffer: std_logic_vector(15 downto 0) := X"0000";
-- Helper signals (not used)
signal curr_inst: inst_type := INST_NOP;
begin
curr_inst <= get_inst(reg_ir);
curr_cc <= get_cond_code(reg_ir);
curr_cc_value <= eval_cond_code(curr_cc, flags);
cpuinfo.read_done <= fin.ready;
cpuinfo.cond <= curr_cc_value;
cpuinfo.ie <= reg_ie;
cpuinfo.int <= fin.int;
fout.addr <= addr;
fout.do <= reg_do;
fout.port_en <= cs.m.port_en;
fout.intack <= cs.uc.intack;
fout.ie <= reg_ie;
reg_ie_ctrl: process (clk, reset, cs.r.ie) is
begin
if rising_edge(clk) then
if reset = '1' then
reg_ie <= '0';
else
case cs.r.ie is
when ENABLE =>
reg_ie <= '1';
when DISABLE =>
reg_ie <= '0';
when NONE =>
null;
end case;
end if;
end if;
end process;
reg_pc_buffer_ctrl: process (clk, cs.r.pc_buffer, reg_di) is
begin
if rising_edge(clk) then
case cs.r.pc_buffer is
when PC_BUFFER_LOAD_DATA_IN_LOW =>
reg_pc_buffer(7 downto 0) <= reg_di;
when PC_BUFFER_LOAD_DATA_IN_HIGH =>
reg_pc_buffer(15 downto 8) <= reg_di;
when NONE =>
null;
end case;
end if;
end process;
rp_op_ctrl: process (clk, cs.r.rp, rf_do, rp_tmp) is
begin
if rising_edge(clk) then
case cs.r.rp is
when LOAD_RF_DO_HIGH =>
rp_tmp(15 downto 8) <= rf_do;
when LOAD_RF_DO_LOW =>
rp_tmp(7 downto 0) <= rf_do;
when RP_OP_INC =>
rp_tmp <= std_logic_vector(unsigned(rp_tmp) + 1);
when RP_OP_DEC =>
rp_tmp <= std_logic_vector(unsigned(rp_tmp) - 1);
when RP_OP_NONE =>
null;
when others =>
assert false report "Unhandled case in rp_op_ctrl proc"
severity Failure;
end case;
end if;
end process;
reg_do_ctrl: process (clk, cs.r.do, reg_di, rf_do, alu_result, flags, reg_pc)
is
begin
if rising_edge(clk) then
case cs.r.do is
when LOAD_DI =>
reg_do <= reg_di;
when LOAD_RF =>
reg_do <= rf_do;
when LOAD_ALU =>
reg_do <= alu_result;
when LOAD_FLAGS =>
reg_do <= get_flags_byte(flags);
when LOAD_PC_LOW =>
reg_do <= reg_pc(7 downto 0);
when LOAD_PC_HIGH =>
reg_do <= reg_pc(15 downto 8);
when NONE =>
null;
end case;
end if;
end process;
output_addr_ctrl: process (clk, reset, cs.a, rf_do, addr, reg_pc, reg_di) is
function compute_addr_offset(addr: std_logic_vector(15 downto 0);
offset: i8080_addr_offset_T) return std_logic_vector is
begin
return std_logic_vector(unsigned(addr) + to_unsigned(offset, addr'length));
end function;
begin
if rising_edge(clk) then
if reset = '1' then
addr <= X"0000";
else
case cs.a.load is
when LOAD_PC =>
addr <= reg_pc;
when LOAD_OFFSET =>
addr <= compute_addr_offset(addr, cs.a.offset);
when LOAD_RF_L =>
addr(7 downto 0) <= rf_do;
when LOAD_RF_H =>
addr(15 downto 8) <= rf_do;
when LOAD_DATA_IN =>
addr(15 downto 8) <= reg_di;
addr(7 downto 0) <= reg_di;
when LOAD_DATA_IN_L =>
addr(7 downto 0) <= reg_di;
when LOAD_DATA_IN_H =>
addr(15 downto 8) <= reg_di;
when LOAD_BUFFER =>
addr <= addr_buffer;
when NONE =>
null;
end case;
end if;
end if;
end process;
addr_buffer_ctrl: process (clk, reset, cs.a.loadbuf, reg_di) is
begin
if rising_edge(clk) then
if reset = '1' then
addr_buffer <= X"0000";
else
case cs.a.loadbuf is
when LOAD_DATA_IN_L =>
addr_buffer(7 downto 0) <= reg_di;
when LOAD_DATA_IN_H =>
addr_buffer(15 downto 8) <= reg_di;
when NONE =>
null;
when others =>
null;
assert false report "Invalid state for addr_buffer"
severity Failure;
end case;
end if;
end if;
end process;
-- Memory access controller
mem_ctrl: process (cs.m.op) is
begin
case cs.m.op is
when Idle =>
fout.en <= '0';
fout.wren <= '0';
when Read =>
fout.en <= '1';
fout.wren <= '0';
when Write =>
fout.en <= '1';
fout.wren <= '1';
end case;
end process;
-- Data input register controller
reg_di_ctrl: process (clk, reset, cs.r.di_ce, fin.di) is
begin
if rising_edge(clk) then
if reset = '1' then
reg_di <= X"00";
elsif cs.r.di_ce = '1' then
reg_di <= fin.di;
end if;
end if;
end process;
-- Instruction register controller
reg_inst_ctrl: process (clk, reset, cs.r.ir_ce, reg_di) is
begin
if rising_edge(clk) then
if reset = '1' then
reg_ir <= X"00";
elsif cs.r.ir_ce = '1' then
reg_ir <= reg_di;
end if;
end if;
end process;
reg_pc_ctrl: process (clk, en, reset, cs.r.pc, reg_pc, reg_ir,
rf_do, reg_di, reg_pc_buffer
)
is
variable nia: std_logic_vector(15 downto 0) := X"0000";
function get_nia(
reg_pc: std_logic_vector(15 downto 0);
reg_ir: std_logic_vector(7 downto 0)) return std_logic_vector
is
variable inst: inst_type := INST_NOP;
variable inst_arg_count: natural := 0;
begin
inst := get_inst(reg_ir);
inst_arg_count := get_inst_args(inst);
return std_logic_vector(
unsigned(reg_pc) + to_unsigned(inst_arg_count + 1, reg_pc'length)
);
end function;
begin
if rising_edge(clk) then
if reset = '1' then
reg_pc <= X"0000";
else
case cs.r.pc is
when PC_LOAD_NIA =>
reg_pc <= get_nia(reg_pc, reg_ir);
when PC_LOAD_RF_LOW =>
reg_pc(7 downto 0) <= rf_do;
when PC_LOAD_RF_HIGH =>
reg_pc(15 downto 8) <= rf_do;
when PC_LOAD_DATA_IN_LOW =>
reg_pc(7 downto 0) <= reg_di;
when PC_LOAD_DATA_IN_HIGH =>
reg_pc(15 downto 8) <= reg_di;
when PC_LOAD_DATA_IN_COND_LOW =>
if curr_cc_value then
reg_pc(7 downto 0) <= reg_di;
end if;
when PC_LOAD_DATA_IN_COND_HIGH =>
if curr_cc_value then
reg_pc(15 downto 8) <= reg_di;
end if;
when PC_LOAD_BUFFER =>
reg_pc <= reg_pc_buffer;
when PC_LOAD_RESET_VECTOR =>
reg_pc <= get_reset_vector_address(reg_ir);
when NONE =>
null;
end case;
end if;
end if;
end process;
alu_input_select_L: process (cs.alu.lsel, rf_do) is
begin
case cs.alu.lsel is
when ALU_INPUT_RF_DO =>
alu_lhs <= rf_do;
when others =>
assert false report "Unhandled L case option in alu_input_select_L"
severity Failure;
end case;
end process;
alu_input_select_R: process (cs.alu.rsel, rf_do, reg_di) is
begin
case cs.alu.rsel is
when ALU_INPUT_RF_DO =>
alu_rhs <= rf_do;
when ALU_INPUT_DATA_IN =>
alu_rhs <= reg_di;
when others =>
assert false report "Unhandled R case option in alu_input_select_R"
severity Failure;
end case;
end process;
alu_inst: entity work.i8080_alu(rtl)
port map (
clk => clk,
cs => cs.alu,
lhs => alu_lhs, rhs => alu_rhs,
result => alu_result,
flags => flags
);
rf_di_select: process (cs.r.rf.sel_input_class, rf_do, reg_di, alu_result) is
begin
case cs.r.rf.sel_input_class is
when RF_SEL_SELF =>
rf_di <= rf_do;
when RF_SEL_DATA_IN =>
rf_di <= reg_di;
when RF_SEL_ALU =>
rf_di <= alu_result;
when RF_SEL_RPOP_HIGH =>
rf_di <= rp_tmp(15 downto 8);
when RF_SEL_RPOP_LOW =>
rf_di <= rp_tmp(7 downto 0);
end case;
end process;
register_file: entity work.i8080_register_file(rtl)
port map (
clk => clk,
cs => cs.r.rf,
di => rf_di,
do => rf_do
);
ucode_ctrl: entity work.i8080_microcode_controller(rtl)
port map (
clk => clk, en => en, reset => reset,
inst_byte => reg_ir,
cpuinfo => cpuinfo,
cs => cs
);
end architecture;