-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCPU.vhd
More file actions
450 lines (403 loc) · 12.9 KB
/
CPU.vhd
File metadata and controls
450 lines (403 loc) · 12.9 KB
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
441
442
443
444
445
446
447
448
449
450
--ALTERA EP4CE6E22C8N
--Configuration device EPCS16N
--Requires POF format file for Active Serial programming
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity CPU is
port(
i_master_clk : in std_logic;
i_master_reset : in std_logic;
i_manual_step : in std_logic;
i_clk_select : in std_logic;
i_clk_freq_select : in std_logic_vector(1 downto 0);
o_clk_freq_select_dbg : out std_logic_vector(1 downto 0);
o_clk_led : out std_logic;
o_inv_clk_led : out std_logic;
o_digit : out std_logic_vector(3 downto 0);
o_segments : out std_logic_vector(7 downto 0);
o_dbg_led_bus : out std_logic_vector(7 downto 0);
o_activate_dbg_leds : out std_logic_vector(11 downto 0)
);
end CPU;
architecture rtl of cpu is
--CPU Clock
signal w_CLK_1HZ : std_logic;
signal w_HLT : std_logic;
signal w_CLK_FREQ_DBG_LED : std_logic_vector(1 downto 0);
--Master reset for the entire CPU
signal w_RESET : std_logic;
--Data bus connections
signal w_DATA_BUS_IN : std_logic_vector(7 downto 0);
signal w_DATA_BUS_OUT_PC : std_logic_vector(7 downto 0);
signal w_DATA_BUS_OUT_INR : std_logic_vector(7 downto 0);
--Memory address register controls and output
signal w_MI : std_logic; --Memory address register in
signal w_ADDR : std_logic_vector(3 downto 0); --Address to be put into RAM
--RAM control and outputs
signal w_RI : std_logic;
signal w_RO : std_logic;
signal w_DATA_BUS_OUT_RAM : std_logic_vector(7 downto 0);
signal w_RAM_DBG_LED : std_logic_vector(7 downto 0);
--Program counter controls and output
signal w_CO : std_logic; --Counter out
signal w_CE : std_logic; --Counter enable (Increments)
signal w_J : std_logic; --Jump
signal w_PC_DBG_LED : std_logic_vector(3 downto 0);
--Register A and B for the ALU (A is the accumulator)
signal w_AI : std_logic;
signal w_AO : std_logic;
signal w_DATA_BUS_OUT_A : std_logic_vector(7 downto 0);
signal w_ALU_REG_OUT_A : std_logic_vector(7 downto 0);
signal w_REG_DBG_LED_A : std_logic_vector(7 downto 0);
signal w_BI : std_logic;
signal w_BO : std_logic;
signal w_DATA_BUS_OUT_B : std_logic_vector(7 downto 0);
signal w_ALU_REG_OUT_B : std_logic_vector(7 downto 0);
signal w_REG_DBG_LED_B : std_logic_vector(7 downto 0);
--ALU
signal w_EO : std_logic;
signal w_SU : std_logic;
signal w_CY : std_logic;
signal w_ZR : std_logic;
signal w_DATA_BUS_OUT_ALU : std_logic_vector(7 downto 0);
signal w_ALU_DBG_LED : std_logic_vector(7 downto 0);
--Flags register
signal w_FI : std_logic;
signal w_FLAGS : std_logic_vector(1 downto 0);
signal w_FLAGS_DBG_LED : std_logic_vector(1 downto 0);
--Instruction register controls and output
signal w_II : std_logic;
signal w_IO : std_logic;
signal w_INSTRUCTION : std_logic_vector(3 downto 0);
signal w_INSTREG_DBG_LED : std_logic_vector(7 downto 0);
--Instruction decoder
signal w_INV_CLK : std_logic;
signal w_INST_STEP_DBG_LED : std_logic_vector(2 downto 0);
signal w_CTRL_WORD_DBG_LED : std_logic_vector(15 downto 0);
signal w_CTRL_WORD_DBG_LED_INV : std_logic_vector(15 downto 0);
--Output register
signal w_OI : std_logic;
component clock
port(
i_clkin : in std_logic;
i_man_clk : in std_logic;
i_select : in std_logic;
i_sel_clk_freq : in std_logic_vector(1 downto 0);
i_halt : in std_logic;
o_clkout : out std_logic;
o_clk_freq_dbg_led : out std_logic_vector(1 downto 0)
);
end component clock;
component debounce
port(
i_clk : in std_logic;
i_switch : in std_logic;
o_switch : out std_logic
);
end component debounce;
component memory_addr_reg
port(
i_clkin : in std_logic;
i_reset : in std_logic;
i_mi : in std_logic; --Signal to take in the 4 LSB and put it into the register (Active LOW)
i_data_bus_in : in std_logic_vector(7 downto 0);
o_addr : out std_logic_vector(3 downto 0) --Outputs the 4 LSB stored in the memory address register
);
end component memory_addr_reg;
component single_port_ram
port(
i_clkin : in std_logic;
i_we : in std_logic; --Active LOW
i_oe : in std_logic; --Active LOW
i_addr : in std_logic_vector(3 downto 0);
i_data_bus_in : in std_logic_vector(7 downto 0);
o_data_bus_out : out std_logic_vector(7 downto 0);
o_ram_dbg_led : out std_logic_vector(7 downto 0)
);
end component single_port_ram;
component prog_counter
port(
i_clkin : in std_logic;
i_reset : in std_logic;
i_ce : in std_logic; --Counter enable (Active LOW)
i_co : in std_logic; --Counter output to bus (Active LOW)
i_j : in std_logic; --Set the counter to a specfic value by reading in the 4 LSB of the bus (Used in jump instruction) (Active LOW)
i_data_bus_in : in std_logic_vector(7 downto 0); --All th 8 bits are used but only the 4 LSB are considered
o_data_bus_out : out std_logic_vector(7 downto 0);
o_pc_dbg_led : out std_logic_vector(3 downto 0)
);
end component prog_counter;
component alu_register
port(
i_clkin : in std_logic;
i_reset : in std_logic;
i_we : in std_logic; --Load enable (Active low)
i_oe : in std_logic; --Write enable (Active low)
i_data_bus_in: in std_logic_vector (7 downto 0); --For internal buses seperate buses are used for data in/out
o_data_bus_out : out std_logic_vector (7 downto 0); --No tri-state logic internally available on at IO pins
o_alu_reg_out : out std_logic_vector (7 downto 0); --This is connected to the ALU directly (Always outputs)
o_reg_dbg_led : out std_logic_vector (7 downto 0)
);
end component alu_register;
component alu
port(
i_eo : in std_logic; --Output the result (Active low)
i_su : in std_logic; --Subtraction when high and addition when low
i_a : in std_logic_vector (7 downto 0);
i_b : in std_logic_vector (7 downto 0);
o_cy : out std_logic;
o_zr : out std_logic;
o_alu_bus_out : out std_logic_vector (7 downto 0);
o_alu_dbg_led : out std_logic_vector(7 downto 0)
);
end component alu;
component flags_reg
port(
i_clkin : in std_logic;
i_reset : in std_logic;
i_cb : in std_logic; --Carry bit
i_zb : in std_logic; --Zero bit
i_we : in std_logic; --Write to flags register(Active LOW)
o_flags : out std_logic_vector(1 downto 0);
o_flags_dbg_led : out std_logic_vector(1 downto 0)
);
end component flags_reg;
component instruction_reg
port(
i_clkin : in std_logic;
i_reset : in std_logic;
i_we : in std_logic; --Instruction register in (Active LOW)
i_oe : in std_logic; --Instruction register output (Active LOW)
i_data_bus_in : in std_logic_vector(7 downto 0);
o_data_bus_out : out std_logic_vector(7 downto 0);
o_instuction : out std_logic_vector(3 downto 0);
o_instreg_dbg_led : out std_logic_vector(7 downto 0)
);
end component instruction_reg;
component instruction_decoder
port(
i_clkin : in std_logic;
i_reset : in std_logic;
i_instruction : in std_logic_vector(3 downto 0);
i_flags : in std_logic_vector(1 downto 0);
o_hlt : out std_logic;
o_mi : out std_logic;
o_ri : out std_logic;
o_ro : out std_logic;
o_io : out std_logic;
o_ii : out std_logic;
o_ai : out std_logic;
o_ao : out std_logic;
o_eps : out std_logic;
o_su : out std_logic;
o_bi : out std_logic;
o_bo : out std_logic;
o_oi : out std_logic;
o_ce : out std_logic;
o_co : out std_logic;
o_j : out std_logic;
o_fi : out std_logic;
o_inv_clk : out std_logic;
o_inst_step_dbg_led : out std_logic_vector(2 downto 0);
o_ctrl_word_dbg_led : out std_logic_vector(15 downto 0)
);
end component instruction_decoder;
component display_7_segment
port(
i_mclk : in std_logic;
i_clkin : in std_logic;
i_reset : in std_logic;
i_we : in std_logic;
i_display_signed : in std_logic;
i_data_bus_in : in std_logic_vector(7 downto 0);
o_dig : out std_logic_vector(3 downto 0);
o_segment_drive : out std_logic_vector(7 downto 0)
);
end component display_7_segment;
component debugger_led is
port(
i_mclk : in std_logic;
i_pc_dbg : in std_logic_vector(3 downto 0);
i_a_reg_dbg : in std_logic_vector(7 downto 0);
i_alu_reg_dbg : in std_logic_vector(7 downto 0);
i_flags_reg_dbg : in std_logic_vector(1 downto 0);
i_b_reg_dbg : in std_logic_vector(7 downto 0);
i_mem_addr_dbg : in std_logic_vector(3 downto 0);
i_ram_content_dbg : in std_logic_vector(7 downto 0);
i_inst_reg_dbg : in std_logic_vector(7 downto 0);
i_inst_step_dbg : in std_logic_vector(2 downto 0);
i_ctrl_word_dbg : in std_logic_vector(15 downto 0);
i_bus_dbg : in std_logic_vector(7 downto 0);
o_muxed_dbg_led_bus : out std_logic_vector(7 downto 0) := (others => '0');
o_act_led_bus : out std_logic_vector(11 downto 0) := (others => '0')
);
end component debugger_led;
begin
--Bus connections
w_DATA_BUS_IN <= w_DATA_BUS_OUT_PC when(w_CO = '0') else
w_DATA_BUS_OUT_A when(w_AO = '0') else
w_DATA_BUS_OUT_ALU when(w_EO = '0') else
w_DATA_BUS_OUT_B when(w_BO = '0') else
w_DATA_BUS_OUT_RAM when(w_RO = '0') else
w_DATA_BUS_OUT_INR when(w_IO = '0') else
(others => '0');
cpu_clk : clock
port map(
i_clkin => i_master_clk,
i_man_clk => i_manual_step,
i_select => i_clk_select,
i_sel_clk_freq => i_clk_freq_select,
i_halt => w_HLT,
o_clkout => w_CLK_1HZ,
o_clk_freq_dbg_led => w_CLK_FREQ_DBG_LED
);
reset_debounce : debounce
port map(
i_clk => i_master_clk,
i_switch => i_master_reset,
o_switch => w_RESET
);
pc : prog_counter
port map(
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_ce => w_CE,
i_co => w_CO,
i_j => w_J,
i_data_bus_in => w_DATA_BUS_IN,
o_data_bus_out => w_DATA_BUS_OUT_PC,
o_pc_dbg_led => w_PC_DBG_LED
);
a_reg : alu_register
port map(
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_we => w_AI,
i_oe => w_AO,
i_data_bus_in => w_DATA_BUS_IN,
o_data_bus_out => w_DATA_BUS_OUT_A,
o_alu_reg_out => w_ALU_REG_OUT_A,
o_reg_dbg_led => w_REG_DBG_LED_A
);
arith_logic_unit : alu
port map(
i_eo => w_EO,
i_su => w_SU,
i_a => w_ALU_REG_OUT_A,
i_b => w_ALU_REG_OUT_B,
o_cy => w_CY,
o_zr => w_ZR,
o_alu_bus_out => w_DATA_BUS_OUT_ALU,
o_alu_dbg_led => w_ALU_DBG_LED
);
flags_register : flags_reg
port map(
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_cb => w_CY,
i_zb => w_ZR,
i_we => w_FI,
o_flags => w_FLAGS,
o_flags_dbg_led => w_FLAGS_DBG_LED
);
b_reg : alu_register
port map(
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_we => w_BI,
i_oe => w_BO,
i_data_bus_in => w_DATA_BUS_IN,
o_data_bus_out => w_DATA_BUS_OUT_B,
o_alu_reg_out => w_ALU_REG_OUT_B,
o_reg_dbg_led => w_REG_DBG_LED_B
);
mem_addr_reg : memory_addr_reg
port map(
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_mi => w_MI,
i_data_bus_in => w_DATA_BUS_IN,
o_addr => w_ADDR
);
ram : single_port_ram
port map(
i_clkin => w_CLK_1HZ,
i_we => w_RI,
i_oe => w_RO,
i_addr => w_ADDR,
i_data_bus_in => w_DATA_BUS_IN,
o_data_bus_out => w_DATA_BUS_OUT_RAM,
o_ram_dbg_led => w_RAM_DBG_LED
);
inst_reg : instruction_reg
port map(
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_we => w_II,
i_oe => w_IO,
i_data_bus_in => w_DATA_BUS_IN,
o_data_bus_out => w_DATA_BUS_OUT_INR,
o_instuction => w_INSTRUCTION,
o_instreg_dbg_led => w_INSTREG_DBG_LED
);
inst_decode : instruction_decoder
port map(
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_instruction => w_INSTRUCTION,
i_flags => w_FLAGS,
o_hlt => w_HLT,
o_mi => w_MI,
o_ri => w_RI,
o_ro => w_RO,
o_io => w_IO,
o_ii => w_II,
o_ai => w_AI,
o_ao => w_AO,
o_eps => w_EO,
o_su => w_SU,
o_bi => w_BI,
o_bo => w_BO,
o_oi => w_OI,
o_ce => w_CE,
o_co => w_CO,
o_j => w_J,
o_fi => w_FI,
o_inv_clk => w_INV_CLK,
o_inst_step_dbg_led => w_INST_STEP_DBG_LED,
o_ctrl_word_dbg_led => w_CTRL_WORD_DBG_LED
);
output_disp : display_7_segment
port map(
i_mclk => i_master_clk,
i_clkin => w_CLK_1HZ,
i_reset => w_RESET,
i_we => w_OI,
i_display_signed => '1',
i_data_bus_in => w_DATA_BUS_IN,
o_dig => o_digit,
o_segment_drive => o_segments
);
debug_led : debugger_led
port map(
i_mclk => i_master_clk,
i_pc_dbg => w_PC_DBG_LED,
i_a_reg_dbg => w_REG_DBG_LED_A,
i_alu_reg_dbg => w_ALU_DBG_LED,
i_flags_reg_dbg => w_FLAGS_DBG_LED,
i_b_reg_dbg => w_REG_DBG_LED_B,
i_mem_addr_dbg => w_ADDR, --The address itself is used for debug
i_ram_content_dbg => w_RAM_DBG_LED,
i_inst_reg_dbg => w_INSTREG_DBG_LED,
i_inst_step_dbg => w_INST_STEP_DBG_LED,
i_ctrl_word_dbg => w_CTRL_WORD_DBG_LED_INV,
i_bus_dbg => w_DATA_BUS_IN,
o_muxed_dbg_led_bus => o_dbg_led_bus,
o_act_led_bus => o_activate_dbg_leds
);
o_clk_led <= w_CLK_1HZ;
o_inv_clk_led <= w_INV_CLK;
o_clk_freq_select_dbg <= w_CLK_FREQ_DBG_LED;
w_CTRL_WORD_DBG_LED_INV <= not w_CTRL_WORD_DBG_LED;
end rtl;