OpenCores
URL https://opencores.org/ocsvn/light8080/light8080/trunk

Subversion Repositories light8080

[/] [light8080/] [trunk/] [vhdl/] [light8080.vhdl] - Blame information for rev 54

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ja_rd
--##############################################################################
2 10 ja_rd
-- light8080 : Intel 8080 binary compatible core
3 2 ja_rd
--##############################################################################
4 54 ja_rd
-- v1.2    (08 jul 2010) Fix: XOR operations were not clearing CY,ACY.
5 10 ja_rd
-- v1.1    (20 sep 2008) Microcode bug in INR fixed.
6
-- v1.0    (05 nov 2007) First release. Jose A. Ruiz.
7
--
8 19 ja_rd
-- This file and all the light8080 project files are freeware (See COPYING.TXT)
9 3 ja_rd
--##############################################################################
10 19 ja_rd
-- (See timing diagrams at bottom of file. More comprehensive explainations can 
11
-- be found in the design notes)
12 10 ja_rd
--##############################################################################
13 2 ja_rd
 
14
library IEEE;
15
use IEEE.STD_LOGIC_1164.ALL;
16
use IEEE.STD_LOGIC_ARITH.ALL;
17
use IEEE.STD_LOGIC_UNSIGNED.ALL;
18
 
19
--##############################################################################
20
-- vma :      enable a memory or io r/w access.
21
-- io :       access in progress is io (and not memory) 
22
-- rd :       read memory or io 
23
-- wr :       write memory or io
24
-- data_out : data output
25
-- addr_out : memory and io address
26
-- data_in :  data input
27
-- halt :     halt status (1 when in halt state)
28
-- inte :     interrupt status (1 when enabled)
29
-- intr :     interrupt request
30
-- inta :     interrupt acknowledge
31
-- reset :    synchronous reset
32
-- clk :      clock
33 19 ja_rd
--
34
-- (see timing diagrams at bottom of file)
35 2 ja_rd
--##############################################################################
36
entity light8080 is
37
    Port (
38
            addr_out :  out std_logic_vector(15 downto 0);
39
 
40
            inta :      out std_logic;
41
            inte :      out std_logic;
42
            halt :      out std_logic;
43
            intr :      in std_logic;
44
 
45
            vma :       out std_logic;
46
            io :        out std_logic;
47
            rd :        out std_logic;
48
            wr :        out std_logic;
49 19 ja_rd
            fetch :     out std_logic;
50 2 ja_rd
            data_in :   in std_logic_vector(7 downto 0);
51
            data_out :  out std_logic_vector(7 downto 0);
52
 
53
            clk :       in std_logic;
54
            reset :     in std_logic );
55
end light8080;
56
 
57
--##############################################################################
58 10 ja_rd
-- All memory and io accesses are synchronous (rising clock edge). Signal vma 
59
-- works as the master memory and io synchronous enable. More specifically:
60 2 ja_rd
--
61
--    * All memory/io control signals (io,rd,wr) are valid only when vma is 
62
--      high. They never activate when vms is inactive. 
63
--    * Signals data_out and address are only valid when vma='1'. The high 
64 10 ja_rd
--      address byte is 0x00 for all io accesses.
65
--    * Signal data_in should be valid by the end of the cycle after vma='1', 
66
--      data is clocked in by the rising clock edge.
67 2 ja_rd
--
68 10 ja_rd
-- All signals are assumed to be synchronous to the master clock. Prevention of
69
-- metastability, if necessary, is up to you.
70
-- 
71
-- Signal reset needs to be active for just 1 clock cycle (it is sampled on a 
72
-- positive clock edge and is subject to setup and hold times).
73 4 ja_rd
-- Once reset is deasserted, the first fetch at address 0x0000 will happen 4
74 2 ja_rd
-- cycles later.
75
--
76
-- Signal intr is sampled on all positive clock edges. If asserted when inte is
77 4 ja_rd
-- high, interrupts will be disabled, inta will be asserted high and a fetch 
78 39 ja_rd
-- cycle will occur immediately after the current instruction ends execution,
79
-- except if intr was asserted at the last cycle of an instruction. In that case
80
-- it will be honored after the next instruction ends.
81
-- The fetched instruction will be executed normally, except that PC will not 
82
-- be valid in any subsequent fetch cycles of the same instruction, 
83 10 ja_rd
-- and will not be incremented (In practice, the same as the original 8080).
84 39 ja_rd
-- inta will remain high for the duration of the fetched instruction, including
85
-- fetch and execution time (in the original 8080 it was high only for the 
86
-- opcode fetch cycle). 
87 10 ja_rd
-- PC will not be autoincremented while inta is high, but it can be explicitly 
88 39 ja_rd
-- modified (e.g. RST, CALL, etc.). Again, the same as the original.
89 2 ja_rd
-- Interrupts will be disabled upon assertion of inta, and remain disabled 
90 4 ja_rd
-- until explicitly enabled by the program (as in the original).
91 39 ja_rd
-- If intr is asserted when inte is low, the interrupt will not be attended but
92
-- it will be registered in an int_pending flag, so it will be honored when 
93
-- interrupts are enabled.
94
-- 
95 2 ja_rd
--
96 4 ja_rd
-- The above means that any instruction can be supplied in an inta cycle, 
97 10 ja_rd
-- either single byte or multibyte. See the design notes.
98 2 ja_rd
--##############################################################################
99
 
100
architecture microcoded of light8080 is
101
 
102
-- addr_low: low byte of address
103
signal addr_low :     std_logic_vector(7 downto 0);
104
-- IR: instruction register. some bits left unused.  
105
signal IR :           std_logic_vector(7 downto 0);
106
-- s_field: IR field, sss source reg code
107
signal s_field :      std_logic_vector(2 downto 0);
108
-- d_field: IR field, ddd destination reg code
109
signal d_field :      std_logic_vector(2 downto 0);
110
-- p_field: IR field, pp 16-bit reg pair code
111
signal p_field :      std_logic_vector(1 downto 0);
112
-- rbh: 1 when p_field=11, used in reg bank addressing for 'special' regs
113
signal rbh :          std_logic; -- 1 when P=11 (special case)  
114
-- alu_op: uinst field, ALU operation code 
115
signal alu_op :       std_logic_vector(3 downto 0);
116
-- DI: data input to ALU block from data_in, unregistered
117
signal DI :           std_logic_vector(7 downto 0);
118
-- uc_addr: microcode (ucode) address 
119
signal uc_addr :      std_logic_vector(7 downto 0);
120
-- next_uc_addr: computed next microcode address (uaddr++/jump/ret/fetch)
121
signal next_uc_addr : std_logic_vector(8 downto 0);
122
-- uc_jmp_addr: uinst field, absolute ucode jump address
123
signal uc_jmp_addr :  std_logic_vector(7 downto 0);
124
-- uc_ret_address: ucode return address saved in previous jump
125
signal uc_ret_addr :  std_logic_vector(7 downto 0);
126
-- addr_plus_1: uaddr + 1
127
signal addr_plus_1 :  std_logic_vector(7 downto 0);
128
-- do_reset: reset, delayed 1 cycle -- used to reset the microcode sequencer
129
signal do_reset :     std_logic;
130
 
131
-- uc_flags1: uinst field, encoded flag of group 1 (see ucode file)
132
signal uc_flags1 :    std_logic_vector(2 downto 0);
133
-- uc_flags2: uinst field, encoded flag of group 2 (see ucode file)
134
signal uc_flags2 :    std_logic_vector(2 downto 0);
135
-- uc_addr_sel: selection of next uc_addr, composition of 4 flags
136
signal uc_addr_sel :  std_logic_vector(3 downto 0);
137
-- NOTE: see microcode file for information on flags
138
signal uc_jsr :       std_logic;  -- uinst field, decoded 'jsr' flag
139
signal uc_tjsr :      std_logic;  -- uinst field, decoded 'tjsr' flag
140
signal uc_decode :    std_logic;  -- uinst field, decoded 'decode' flag
141
signal uc_end :       std_logic;  -- uinst field, decoded 'end' flag
142
signal condition_reg :std_logic;  -- registered tjst condition
143
-- condition: tjsr condition (computed ccc condition from '80 instructions)
144
signal condition :    std_logic;
145
-- condition_sel: IR field, ccc condition code
146
signal condition_sel :std_logic_vector(2 downto 0);
147
signal uc_do_jmp :    std_logic;  -- uinst jump (jsr/tjsr) flag, pipelined
148
signal uc_do_ret :    std_logic;  -- ret flag, pipelined
149
signal uc_halt_flag : std_logic;  -- uinst field, decoded 'halt' flag
150
signal uc_halt :      std_logic;  -- halt command
151
signal halt_reg :     std_logic;  -- halt status reg, output as 'halt' signal
152
signal uc_ei :        std_logic;  -- uinst field, decoded 'ei' flag
153 49 ja_rd
signal uc_di :        std_logic;  -- uinst field, decoded 'di' flag
154 2 ja_rd
signal inte_reg :     std_logic;  -- inte status reg, output as 'inte' signal
155
signal int_pending :  std_logic;  -- intr requested, inta not active yet
156
signal inta_reg :     std_logic;  -- inta status reg, output as 'inta'
157
signal clr_t1 :       std_logic;  -- uinst field, explicitly erase T1
158
signal do_clr_t1 :    std_logic;  -- clr_t1 pipelined
159
signal clr_t2 :       std_logic;  -- uinst field, explicitly erase T2
160
signal do_clr_t2 :    std_logic;  -- clr_t2 pipelined
161
signal ucode :        std_logic_vector(31 downto 0); -- microcode word
162
signal ucode_field2 : std_logic_vector(24 downto 0); -- pipelined microcode
163
 
164 49 ja_rd
-- used to delay interrup enable for one entire instruction after EI
165
signal delayed_ei :   std_logic;
166
 
167 2 ja_rd
-- microcode ROM : see design notes and microcode source file 
168
type t_rom is array (0 to 511) of std_logic_vector(31 downto 0);
169
 
170
signal rom : t_rom := (
171
"00000000000000000000000000000000", -- 000
172
"00000000000001001000000001000100", -- 001
173
"00000000000001000000000001000100", -- 002
174
"10111101101001001000000001001101", -- 003
175
"10110110101001000000000001001101", -- 004
176
"00100000000000000000000000000000", -- 005
177
"00000000000000000000000000000000", -- 006
178
"11100100000000000000000000000000", -- 007
179
"00000000101010000000000000000000", -- 008
180
"00000100000100000000000001010111", -- 009
181
"00001000000000000000110000011001", -- 00a
182
"00000100000100000000000001010111", -- 00b
183
"00000000101010000000000010010111", -- 00c
184
"00001000000000000000110000011100", -- 00d
185
"00001000000000000000110000011111", -- 00e
186
"00000100000100000000000001010111", -- 00f
187
"00001000000000000000110000011111", -- 010
188
"00001000000000000000110000011100", -- 011
189
"00001000000000000000110000011111", -- 012
190
"00000000000110001000000001010111", -- 013
191
"00001000000000000000110000011111", -- 014
192
"00000100000110000000000001010111", -- 015
193
"00001000000000000000110000101110", -- 016
194
"00001000000000000000110000100010", -- 017
195
"00000100000000111000000001010111", -- 018
196
"00001000000000000000110000101110", -- 019
197
"00000000101000111000000010010111", -- 01a
198
"00001000000000000000110000100101", -- 01b
199
"00001000000000000000110000101110", -- 01c
200
"10111101101001100000000001001101", -- 01d
201
"10110110101001101000000001001101", -- 01e
202
"00000000100000101000000001010111", -- 01f
203
"00001000000000000000110000100010", -- 020
204
"00000100000000100000000001010111", -- 021
205
"00001000000000000000110000101110", -- 022
206
"00000000101000101000000010010111", -- 023
207
"10111101101001100000000001001101", -- 024
208
"10111010101001101000000001001101", -- 025
209
"00000000101000100000000010010111", -- 026
210
"00001000000000000000110000100101", -- 027
211
"00001000000000000000110000101000", -- 028
212
"00000100000000111000000001010111", -- 029
213
"00000000101000111000000010010111", -- 02a
214
"00001000000000000000110000101011", -- 02b
215
"00000000101000010000000000000000", -- 02c
216
"00000000000001010000000001010111", -- 02d
217
"00000000101000011000000000000000", -- 02e
218
"00000000000001011000000001010111", -- 02f
219
"00000000101000100000000000000000", -- 030
220
"00000000000000010000000001010111", -- 031
221
"00000000101000101000000000000000", -- 032
222
"00000000000000011000000001010111", -- 033
223
"00000000101001010000000000000000", -- 034
224
"00000000000000100000000001010111", -- 035
225
"00000000101001011000000000000000", -- 036
226
"00000100000000101000000001010111", -- 037
227
"00001000000000000000110000011111", -- 038
228
"00000100011000111000001101001100", -- 039
229
"00001000000000000000110000011111", -- 03a
230
"00000100011000111000001101001101", -- 03b
231
"00001000000000000000110000011111", -- 03c
232
"00000100011000111000001101001110", -- 03d
233
"00001000000000000000110000011111", -- 03e
234
"00000100011000111000001101001111", -- 03f
235
"00001000000000000000110000011111", -- 040
236
"00000100011000111000001101000100", -- 041
237
"00001000000000000000110000011111", -- 042
238
"00000100011000111000001101000101", -- 043
239
"00001000000000000000110000011111", -- 044
240
"00000100011000111000001101000110", -- 045
241
"00001000000000000000110000011111", -- 046
242
"00000100011000111000001110001110", -- 047
243
"00000000101010000000000000000000", -- 048
244
"00000100011000111000001101001100", -- 049
245
"00000000101010000000000000000000", -- 04a
246
"00000100011000111000001101001101", -- 04b
247
"00000000101010000000000000000000", -- 04c
248
"00000100011000111000001101001110", -- 04d
249
"00000000101010000000000000000000", -- 04e
250
"00000100011000111000001101001111", -- 04f
251
"00000000101010000000000000000000", -- 050
252
"00000100011000111000001101000100", -- 051
253
"00000000101010000000000000000000", -- 052
254
"00000100011000111000001101000101", -- 053
255
"00000000101010000000000000000000", -- 054
256
"00000100011000111000001101000110", -- 055
257
"00000000101010000000000000000000", -- 056
258
"00000100011000111000001110001110", -- 057
259
"00001000000000000000110000011001", -- 058
260
"00000100011000111000001101001100", -- 059
261
"00001000000000000000110000011001", -- 05a
262
"00000100011000111000001101001101", -- 05b
263
"00001000000000000000110000011001", -- 05c
264
"00000100011000111000001101001110", -- 05d
265
"00001000000000000000110000011001", -- 05e
266
"00000100011000111000001101001111", -- 05f
267
"00001000000000000000110000011001", -- 060
268
"00000100011000111000001101000100", -- 061
269
"00001000000000000000110000011001", -- 062
270
"00000100011000111000001101000101", -- 063
271
"00001000000000000000110000011001", -- 064
272
"00000100011000111000001101000110", -- 065
273
"00001000000000000000110000011001", -- 066
274
"00000100011000111000001110001110", -- 067
275
"10111100101100000000001001001101", -- 068
276
"00000100000000000000000000000000", -- 069
277
"00001000000000000000110000011001", -- 06a
278 6 ja_rd
"10111100000000000000001010001101", -- 06b
279 2 ja_rd
"00001000000000000000110000011100", -- 06c
280
"10111100011100000000001001001111", -- 06d
281
"00000100000000000000000000000000", -- 06e
282
"00001000000000000000110000011001", -- 06f
283
"11000000000000000000000000000000", -- 070
284
"10111100011001010000001010001111", -- 071
285
"00001000000000000000110000011100", -- 072
286
"10111100101110001000000001001101", -- 073
287
"10100100101110000000000001001101", -- 074
288
"10111100011110001000000001001111", -- 075
289
"10100100011110000000000001001111", -- 076
290
"00000000011110001000000000000000", -- 077
291
"00000000101000101000000101001100", -- 078
292
"00000000011110000000000000000000", -- 079
293
"00000100101000100000000101001101", -- 07a
294
"00000000101000111000000010101000", -- 07b
295
"00000100101000111000001101101000", -- 07c
296
"00000100101000111000000101000000", -- 07d
297
"00000100101000111000000101000001", -- 07e
298
"00000100101000111000000101000010", -- 07f
299
"00000100101000111000000101000011", -- 080
300
"00000100101000111000000001000111", -- 081
301
"00000100000000000000000100101100", -- 082
302
"00000100000000000000000100101101", -- 083
303
"00001000000000000000110000101110", -- 084
304
"00000000101001100000000000000000", -- 085
305
"00000000000001001000000001010111", -- 086
306
"00000000101001101000000000000000", -- 087
307
"00000100000001000000000001010111", -- 088
308
"00000100000000000000000000000000", -- 089
309
"00001000000000000000110000101110", -- 08a
310
"00010000000000000000100000000101", -- 08b
311
"00001000000000000000110000101110", -- 08c
312
"11000000101001000000000010010111", -- 08d
313
"00001000000000000000110000110100", -- 08e
314
"11000000101001001000000010010111", -- 08f
315
"00001000000000000000110000110100", -- 090
316
"00000000101001100000000000000000", -- 091
317
"00000000000001001000000001010111", -- 092
318
"00000000101001101000000000000000", -- 093
319
"00000100000001000000000001010111", -- 094
320
"00001000000000000000110000101110", -- 095
321
"00010000000000000000100000001101", -- 096
322
"00001000000000000000110000111001", -- 097
323
"00000000000001001000000001010111", -- 098
324
"00001000000000000000110000111001", -- 099
325
"00000100000001000000000001010111", -- 09a
326
"00010000000000000000100000010111", -- 09b
327
"11000000101001000000000010010111", -- 09c
328
"00001000000000000000110000110100", -- 09d
329
"11000000101001001000000010010111", -- 09e
330
"00001000000000000000110000110100", -- 09f
331
"11000000000001001000000001011111", -- 0a0
332
"00000100000001000000000001000100", -- 0a1
333
"00000000101000101000000000000000", -- 0a2
334
"00000000000001001000000001010111", -- 0a3
335
"00000000101000100000000000000000", -- 0a4
336
"00000100000001000000000001010111", -- 0a5
337
"11000000101110000000000010010111", -- 0a6
338
"00001000000000000000110000110100", -- 0a7
339
"11000000101110001000000010010111", -- 0a8
340
"00001000000000000000110000110100", -- 0a9
341
"00000100000000000000000000000000", -- 0aa
342
"11000000101000111000000010010111", -- 0ab
343
"00001000000000000000110000110100", -- 0ac
344
"11000000000000000000000010110000", -- 0ad
345
"00001000000000000000110000110100", -- 0ae
346
"00000100000000000000000000000000", -- 0af
347
"00001000000000000000110000111001", -- 0b0
348
"00000000000110001000000001010111", -- 0b1
349
"00001000000000000000110000111001", -- 0b2
350
"00000100000110000000000001010111", -- 0b3
351
"00001000000000000000110000111001", -- 0b4
352
"00000000000000110000001101010111", -- 0b5
353
"00001000000000000000110000111001", -- 0b6
354
"00000100000000111000000001010111", -- 0b7
355
"00001000000000000000110000111001", -- 0b8
356
"00000000000001100000000001010111", -- 0b9
357
"00001000000000000000110000111001", -- 0ba
358
"00000000000001101000000001010111", -- 0bb
359
"11000000101000100000000010010111", -- 0bc
360
"00001000000000000000110000110100", -- 0bd
361
"11000000101000101000000010010111", -- 0be
362
"00001000000000000000110000110100", -- 0bf
363
"00000000101001100000000000000000", -- 0c0
364
"00000000000000101000000001010111", -- 0c1
365
"00000000101001101000000000000000", -- 0c2
366
"00000100000000100000000001010111", -- 0c3
367
"00000000101000101000000000000000", -- 0c4
368
"00000000000001111000000001010111", -- 0c5
369
"00000000101000100000000000000000", -- 0c6
370
"00000100000001110000000001010111", -- 0c7
371
"01100100000000000000000000000000", -- 0c8
372
"01000100000000000000000000000000", -- 0c9
373
"00000000000001101000000001010111", -- 0ca
374
"00001000000000000000110000011111", -- 0cb
375
"00000000000001100000000001010111", -- 0cc
376
"00000000000000000000000000000000", -- 0cd
377
"00000001101001100000000000000000", -- 0ce
378
"10010110101001101000000000000000", -- 0cf
379
"00000100100000111000000001010111", -- 0d0
380
"00000000000001101000000001010111", -- 0d1
381
"00001000000000000000110000011111", -- 0d2
382
"00000000000001100000000001010111", -- 0d3
383
"00000000101000111000000010010111", -- 0d4
384
"00000001101001100000000000000000", -- 0d5
385
"10011010101001101000000000000000", -- 0d6
386
"00000100000000000000000000000000", -- 0d7
387
"11100100000000000000000000000000", -- 0d8
388
"00000001101000101000000000000000", -- 0d9
389
"00010110101000100000000000000000", -- 0da
390
"00001100100001010000000001010111", -- 0db
391
"00000001101000101000000000000000", -- 0dc
392
"00011010101000100000000000000000", -- 0dd
393
"00000100000000000000000000000000", -- 0de
394
"10111101101001001000000001001101", -- 0df
395
"10110110101001000000000001001101", -- 0e0
396
"00001100100000000000000010010111", -- 0e1
397
"00000001101001100000000000000000", -- 0e2
398
"00010110101001101000000000000000", -- 0e3
399
"00001100100000000000000000000000", -- 0e4
400
"00000001101001100000000000000000", -- 0e5
401
"00011010101001101000000000000000", -- 0e6
402
"00000100000000000000000000000000", -- 0e7
403
"00000001101110001000000000000000", -- 0e8
404
"00010110101110000000000000000000", -- 0e9
405
"00001100100000000000000000000000", -- 0ea
406
"00000001101110001000000000000000", -- 0eb
407
"00011010101110000000000000000000", -- 0ec
408
"00000100000000000000000000000000", -- 0ed
409
"10111101101001001000000001001101", -- 0ee
410
"10110110101001000000000001001101", -- 0ef
411
"00000000100001100000000001010111", -- 0f0
412
"10111101101001001000000001001101", -- 0f1
413
"10110110101001000000000001001101", -- 0f2
414
"00001100100001101000000001010111", -- 0f3
415
"10111100011001111000000001001111", -- 0f4
416
"10100000011001110000000001001111", -- 0f5
417
"00000001101001111000000000000000", -- 0f6
418
"00011010101001110000000000000000", -- 0f7
419
"00001100000000000000000000000000", -- 0f8
420
"10111101101001111000000001001101", -- 0f9
421
"10110110101001110000000001001101", -- 0fa
422
"00001100100000000000000000000000", -- 0fb
423
"00000100000000000000000000000000", -- 0fc
424
"00000100000000000000000000000000", -- 0fd
425
"00000100000000000000000000000000", -- 0fe
426
"00000100000000000000000000000000", -- 0ff
427
"00001000000000000000100000001001", -- 100
428
"00001000000000000000000000010010", -- 101
429
"00001000000000000000000000101010", -- 102
430
"00001000000000000000010000110011", -- 103
431
"00001000000000000000010000101000", -- 104
432
"00001000000000000000010000101101", -- 105
433
"00001000000000000000000000001110", -- 106
434
"00001000000000000000010000111101", -- 107
435
"00001000000000000000000000000000", -- 108
436
"00001000000000000000010000110111", -- 109
437
"00001000000000000000000000101000", -- 10a
438
"00001000000000000000010000110101", -- 10b
439
"00001000000000000000010000101000", -- 10c
440
"00001000000000000000010000101101", -- 10d
441
"00001000000000000000000000001110", -- 10e
442
"00001000000000000000010000111110", -- 10f
443
"00001000000000000000000000000000", -- 110
444
"00001000000000000000000000010010", -- 111
445
"00001000000000000000000000101010", -- 112
446
"00001000000000000000010000110011", -- 113
447
"00001000000000000000010000101000", -- 114
448
"00001000000000000000010000101101", -- 115
449
"00001000000000000000000000001110", -- 116
450
"00001000000000000000010000111111", -- 117
451
"00001000000000000000000000000000", -- 118
452
"00001000000000000000010000110111", -- 119
453
"00001000000000000000000000101000", -- 11a
454
"00001000000000000000010000110101", -- 11b
455
"00001000000000000000010000101000", -- 11c
456
"00001000000000000000010000101101", -- 11d
457
"00001000000000000000000000001110", -- 11e
458
"00001000000000000000100000000000", -- 11f
459
"00001000000000000000000000000000", -- 120
460
"00001000000000000000000000010010", -- 121
461
"00001000000000000000000000100010", -- 122
462
"00001000000000000000010000110011", -- 123
463
"00001000000000000000010000101000", -- 124
464
"00001000000000000000010000101101", -- 125
465
"00001000000000000000000000001110", -- 126
466
"00001000000000000000010000111011", -- 127
467
"00001000000000000000000000000000", -- 128
468
"00001000000000000000010000110111", -- 129
469
"00001000000000000000000000011100", -- 12a
470
"00001000000000000000010000110101", -- 12b
471
"00001000000000000000010000101000", -- 12c
472
"00001000000000000000010000101101", -- 12d
473
"00001000000000000000000000001110", -- 12e
474
"00001000000000000000100000000001", -- 12f
475
"00001000000000000000000000000000", -- 130
476
"00001000000000000000000000010010", -- 131
477
"00001000000000000000000000011001", -- 132
478
"00001000000000000000010000110011", -- 133
479
"00001000000000000000010000101010", -- 134
480
"00001000000000000000010000101111", -- 135
481
"00001000000000000000000000010000", -- 136
482
"00001000000000000000100000000011", -- 137
483
"00001000000000000000000000000000", -- 138
484
"00001000000000000000010000110111", -- 139
485
"00001000000000000000000000010110", -- 13a
486
"00001000000000000000010000110101", -- 13b
487
"00001000000000000000010000101000", -- 13c
488
"00001000000000000000010000101101", -- 13d
489
"00001000000000000000000000001110", -- 13e
490
"00001000000000000000100000000010", -- 13f
491
"00001000000000000000000000001000", -- 140
492
"00001000000000000000000000001000", -- 141
493
"00001000000000000000000000001000", -- 142
494
"00001000000000000000000000001000", -- 143
495
"00001000000000000000000000001000", -- 144
496
"00001000000000000000000000001000", -- 145
497
"00001000000000000000000000001010", -- 146
498
"00001000000000000000000000001000", -- 147
499
"00001000000000000000000000001000", -- 148
500
"00001000000000000000000000001000", -- 149
501
"00001000000000000000000000001000", -- 14a
502
"00001000000000000000000000001000", -- 14b
503
"00001000000000000000000000001000", -- 14c
504
"00001000000000000000000000001000", -- 14d
505
"00001000000000000000000000001010", -- 14e
506
"00001000000000000000000000001000", -- 14f
507
"00001000000000000000000000001000", -- 150
508
"00001000000000000000000000001000", -- 151
509
"00001000000000000000000000001000", -- 152
510
"00001000000000000000000000001000", -- 153
511
"00001000000000000000000000001000", -- 154
512
"00001000000000000000000000001000", -- 155
513
"00001000000000000000000000001010", -- 156
514
"00001000000000000000000000001000", -- 157
515
"00001000000000000000000000001000", -- 158
516
"00001000000000000000000000001000", -- 159
517
"00001000000000000000000000001000", -- 15a
518
"00001000000000000000000000001000", -- 15b
519
"00001000000000000000000000001000", -- 15c
520
"00001000000000000000000000001000", -- 15d
521
"00001000000000000000000000001010", -- 15e
522
"00001000000000000000000000001000", -- 15f
523
"00001000000000000000000000001000", -- 160
524
"00001000000000000000000000001000", -- 161
525
"00001000000000000000000000001000", -- 162
526
"00001000000000000000000000001000", -- 163
527
"00001000000000000000000000001000", -- 164
528
"00001000000000000000000000001000", -- 165
529
"00001000000000000000000000001010", -- 166
530
"00001000000000000000000000001000", -- 167
531
"00001000000000000000000000001000", -- 168
532
"00001000000000000000000000001000", -- 169
533
"00001000000000000000000000001000", -- 16a
534
"00001000000000000000000000001000", -- 16b
535
"00001000000000000000000000001000", -- 16c
536
"00001000000000000000000000001000", -- 16d
537
"00001000000000000000000000001010", -- 16e
538
"00001000000000000000000000001000", -- 16f
539
"00001000000000000000000000001100", -- 170
540
"00001000000000000000000000001100", -- 171
541
"00001000000000000000000000001100", -- 172
542
"00001000000000000000000000001100", -- 173
543
"00001000000000000000000000001100", -- 174
544
"00001000000000000000000000001100", -- 175
545
"00001000000000000000110000011000", -- 176
546
"00001000000000000000000000001100", -- 177
547
"00001000000000000000000000001000", -- 178
548
"00001000000000000000000000001000", -- 179
549
"00001000000000000000000000001000", -- 17a
550
"00001000000000000000000000001000", -- 17b
551
"00001000000000000000000000001000", -- 17c
552
"00001000000000000000000000001000", -- 17d
553
"00001000000000000000000000001010", -- 17e
554
"00001000000000000000000000001000", -- 17f
555
"00001000000000000000010000001000", -- 180
556
"00001000000000000000010000001000", -- 181
557
"00001000000000000000010000001000", -- 182
558
"00001000000000000000010000001000", -- 183
559
"00001000000000000000010000001000", -- 184
560
"00001000000000000000010000001000", -- 185
561
"00001000000000000000010000011000", -- 186
562
"00001000000000000000010000001000", -- 187
563
"00001000000000000000010000001010", -- 188
564
"00001000000000000000010000001010", -- 189
565
"00001000000000000000010000001010", -- 18a
566
"00001000000000000000010000001010", -- 18b
567
"00001000000000000000010000001010", -- 18c
568
"00001000000000000000010000001010", -- 18d
569
"00001000000000000000010000011010", -- 18e
570
"00001000000000000000010000001010", -- 18f
571
"00001000000000000000010000001100", -- 190
572
"00001000000000000000010000001100", -- 191
573
"00001000000000000000010000001100", -- 192
574
"00001000000000000000010000001100", -- 193
575
"00001000000000000000010000001100", -- 194
576
"00001000000000000000010000001100", -- 195
577
"00001000000000000000010000011100", -- 196
578
"00001000000000000000010000001100", -- 197
579
"00001000000000000000010000001110", -- 198
580
"00001000000000000000010000001110", -- 199
581
"00001000000000000000010000001110", -- 19a
582
"00001000000000000000010000001110", -- 19b
583
"00001000000000000000010000001110", -- 19c
584
"00001000000000000000010000001110", -- 19d
585
"00001000000000000000010000011110", -- 19e
586
"00001000000000000000010000001110", -- 19f
587
"00001000000000000000010000010000", -- 1a0
588
"00001000000000000000010000010000", -- 1a1
589
"00001000000000000000010000010000", -- 1a2
590
"00001000000000000000010000010000", -- 1a3
591
"00001000000000000000010000010000", -- 1a4
592
"00001000000000000000010000010000", -- 1a5
593
"00001000000000000000010000100000", -- 1a6
594
"00001000000000000000010000010000", -- 1a7
595
"00001000000000000000010000010010", -- 1a8
596
"00001000000000000000010000010010", -- 1a9
597
"00001000000000000000010000010010", -- 1aa
598
"00001000000000000000010000010010", -- 1ab
599
"00001000000000000000010000010010", -- 1ac
600
"00001000000000000000010000010010", -- 1ad
601
"00001000000000000000010000100010", -- 1ae
602
"00001000000000000000010000010010", -- 1af
603
"00001000000000000000010000010100", -- 1b0
604
"00001000000000000000010000010100", -- 1b1
605
"00001000000000000000010000010100", -- 1b2
606
"00001000000000000000010000010100", -- 1b3
607
"00001000000000000000010000010100", -- 1b4
608
"00001000000000000000010000010100", -- 1b5
609
"00001000000000000000010000100100", -- 1b6
610
"00001000000000000000010000010100", -- 1b7
611
"00001000000000000000010000010110", -- 1b8
612
"00001000000000000000010000010110", -- 1b9
613
"00001000000000000000010000010110", -- 1ba
614
"00001000000000000000010000010110", -- 1bb
615
"00001000000000000000010000010110", -- 1bc
616
"00001000000000000000010000010110", -- 1bd
617
"00001000000000000000010000100110", -- 1be
618
"00001000000000000000010000010110", -- 1bf
619
"00001000000000000000100000011011", -- 1c0
620
"00001000000000000000100000110000", -- 1c1
621
"00001000000000000000100000001010", -- 1c2
622
"00001000000000000000100000000100", -- 1c3
623
"00001000000000000000100000010101", -- 1c4
624
"00001000000000000000100000100110", -- 1c5
625
"00001000000000000000000000111000", -- 1c6
626
"00001000000000000000100000011100", -- 1c7
627
"00001000000000000000100000011011", -- 1c8
628
"00001000000000000000100000010111", -- 1c9
629
"00001000000000000000100000001010", -- 1ca
630
"00001000000000000000000000000000", -- 1cb
631
"00001000000000000000100000010101", -- 1cc
632
"00001000000000000000100000001100", -- 1cd
633
"00001000000000000000000000111010", -- 1ce
634
"00001000000000000000100000011100", -- 1cf
635
"00001000000000000000100000011011", -- 1d0
636
"00001000000000000000100000110000", -- 1d1
637
"00001000000000000000100000001010", -- 1d2
638
"00001000000000000000110000010001", -- 1d3
639
"00001000000000000000100000010101", -- 1d4
640
"00001000000000000000100000100110", -- 1d5
641
"00001000000000000000000000111100", -- 1d6
642
"00001000000000000000100000011100", -- 1d7
643
"00001000000000000000100000011011", -- 1d8
644
"00001000000000000000000000000000", -- 1d9
645
"00001000000000000000100000001010", -- 1da
646
"00001000000000000000110000001010", -- 1db
647
"00001000000000000000100000010101", -- 1dc
648
"00001000000000000000000000000000", -- 1dd
649
"00001000000000000000000000111110", -- 1de
650
"00001000000000000000100000011100", -- 1df
651
"00001000000000000000100000011011", -- 1e0
652
"00001000000000000000100000110000", -- 1e1
653
"00001000000000000000100000001010", -- 1e2
654
"00001000000000000000100000111000", -- 1e3
655
"00001000000000000000100000010101", -- 1e4
656
"00001000000000000000100000100110", -- 1e5
657
"00001000000000000000010000000000", -- 1e6
658
"00001000000000000000100000011100", -- 1e7
659
"00001000000000000000100000011011", -- 1e8
660
"00001000000000000000100000100010", -- 1e9
661
"00001000000000000000100000001010", -- 1ea
662
"00001000000000000000000000101100", -- 1eb
663
"00001000000000000000100000010101", -- 1ec
664
"00001000000000000000000000000000", -- 1ed
665
"00001000000000000000010000000010", -- 1ee
666
"00001000000000000000100000011100", -- 1ef
667
"00001000000000000000100000011011", -- 1f0
668
"00001000000000000000100000110100", -- 1f1
669
"00001000000000000000100000001010", -- 1f2
670
"00001000000000000000110000001001", -- 1f3
671
"00001000000000000000100000010101", -- 1f4
672
"00001000000000000000100000101011", -- 1f5
673
"00001000000000000000010000000100", -- 1f6
674
"00001000000000000000100000011100", -- 1f7
675
"00001000000000000000100000011011", -- 1f8
676
"00001000000000000000110000000100", -- 1f9
677
"00001000000000000000100000001010", -- 1fa
678
"00001000000000000000110000001000", -- 1fb
679
"00001000000000000000100000010101", -- 1fc
680
"00001000000000000000000000000000", -- 1fd
681
"00001000000000000000010000000110", -- 1fe
682
"00001000000000000000100000011100"  -- 1ff
683
 
684
);
685
 
686
-- end of microcode ROM
687
 
688
signal load_al :      std_logic; -- uinst field, load AL reg from rbank
689
signal load_addr :    std_logic; -- uinst field, enable external addr reg load
690
signal load_t1 :      std_logic; -- uinst field, load reg T1 
691
signal load_t2 :      std_logic; -- uinst field, load reg T2
692
signal mux_in :       std_logic; -- uinst field, T1/T2 input data selection
693
signal load_do :      std_logic; -- uinst field, pipelined, load DO reg
694
-- rb_addr_sel: uinst field, rbank address selection: (sss,ddd,pp,ra_field)
695
signal rb_addr_sel :  std_logic_vector(1 downto 0);
696
-- ra_field: uinst field, explicit reg bank address
697
signal ra_field :     std_logic_vector(3 downto 0);
698
signal rbank_data :   std_logic_vector(7 downto 0); -- rbank output
699
signal alu_output :   std_logic_vector(7 downto 0); -- ALU output
700
-- data_output: datapath output: ALU output vs. F reg 
701
signal data_output :  std_logic_vector(7 downto 0);
702
signal T1 :           std_logic_vector(7 downto 0); -- T1 reg (ALU operand)
703
signal T2 :           std_logic_vector(7 downto 0); -- T2 reg (ALU operand)
704
-- alu_input: data loaded into T1, T2: rbank data vs. DI
705
signal alu_input :    std_logic_vector(7 downto 0);
706
signal we_rb :        std_logic; -- uinst field, commands a write to the rbank
707
signal inhibit_pc_increment : std_logic; -- avoid PC changes (during INTA)
708
signal rbank_rd_addr: std_logic_vector(3 downto 0); -- rbank rd addr
709
signal rbank_wr_addr: std_logic_vector(3 downto 0); -- rbank wr addr
710
signal DO :           std_logic_vector(7 downto 0); -- data output reg
711
 
712
-- Register bank as an array of 16 bytes (asynch. LUT ram)
713
type t_reg_bank is array(0 to 15) of std_logic_vector(7 downto 0);
714
-- Register bank : BC, DE, HL, AF, [PC, XY, ZW, SP]
715
signal rbank :        t_reg_bank;
716
 
717
signal flag_reg :     std_logic_vector(7 downto 0); -- F register
718
-- flag_pattern: uinst field, F update pattern: which flags are updated
719
signal flag_pattern : std_logic_vector(1 downto 0);
720
signal flag_s :       std_logic; -- new computed S flag  
721
signal flag_z :       std_logic; -- new computed Z flag
722
signal flag_p :       std_logic; -- new computed P flag
723
signal flag_cy :      std_logic; -- new computed C flag
724
signal flag_cy_1 :    std_logic; -- C flag computed from arith/logic operation
725
signal flag_cy_2 :    std_logic; -- C flag computed from CPC circuit
726
signal do_cy_op :     std_logic; -- ALU explicit CY operation (CPC, etc.)
727
signal do_cy_op_d :   std_logic; -- do_cy_op, pipelined
728
signal do_cpc :       std_logic; -- ALU operation is CPC
729
signal do_cpc_d :     std_logic; -- do_cpc, pipelined
730
signal do_daa :       std_logic; -- ALU operation is DAA
731 54 ja_rd
signal do_xor :       std_logic; -- ALU operation is some XOR (clears CY)
732 2 ja_rd
signal flag_ac :      std_logic; -- new computed half carry flag
733
-- flag_aux_cy: new computed half carry flag (used in 16-bit ops)
734
signal flag_aux_cy :  std_logic;
735
signal load_psw :     std_logic; -- load F register
736
 
737
-- aux carry computation and control signals
738
signal use_aux :      std_logic; -- decoded from flags in 1st phase
739
signal use_aux_cy :   std_logic; -- 2nd phase signal
740
signal reg_aux_cy :   std_logic;
741
signal aux_cy_in :    std_logic;
742
signal set_aux_cy :   std_logic;
743
signal set_aux  :     std_logic;
744
 
745
-- ALU control signals -- together they select ALU operation
746
signal alu_fn :       std_logic_vector(1 downto 0);
747
signal use_logic :    std_logic; -- logic/arith mux control 
748
signal mux_fn :       std_logic_vector(1 downto 0);
749
signal use_psw :      std_logic; -- ALU/F mux control
750
 
751
-- ALU arithmetic operands and result
752
signal arith_op1 :    std_logic_vector(8 downto 0);
753
signal arith_op2 :    std_logic_vector(8 downto 0);
754
signal arith_op2_sgn: std_logic_vector(8 downto 0);
755
signal arith_res :    std_logic_vector(8 downto 0);
756
signal arith_res8 :   std_logic_vector(7 downto 0);
757
 
758
-- ALU DAA intermediate signals (DAA has fully dedicated logic)
759
signal daa_res :      std_logic_vector(8 downto 0);
760
signal daa_res8 :     std_logic_vector(7 downto 0);
761
signal daa_res9 :     std_logic_vector(8 downto 0);
762
signal daa_test1 :    std_logic;
763
signal daa_test1a :   std_logic;
764
signal daa_test2 :    std_logic;
765
signal daa_test2a :   std_logic;
766
signal arith_daa_res :std_logic_vector(7 downto 0);
767
signal cy_daa :       std_logic;
768
 
769
-- ALU CY flag intermediate signals
770
signal cy_in_sgn :    std_logic;
771
signal cy_in :        std_logic;
772
signal cy_in_gated :  std_logic;
773
signal cy_adder :     std_logic;
774
signal cy_arith :     std_logic;
775
signal cy_shifter :   std_logic;
776
 
777
-- ALU intermediate results
778
signal logic_res :    std_logic_vector(7 downto 0);
779
signal shift_res :    std_logic_vector(7 downto 0);
780
signal alu_mux1 :     std_logic_vector(7 downto 0);
781
 
782 49 ja_rd
 
783 2 ja_rd
begin
784
 
785
DI <= data_in;
786
 
787
process(clk)    -- IR register, load when uc_decode flag activates
788
begin
789
  if clk'event and clk='1' then
790
    if uc_decode = '1' then
791
      IR <= DI;
792
    end if;
793
  end if;
794
end process;
795
 
796
s_field <= IR(2 downto 0); -- IR field extraction : sss reg code
797
d_field <= IR(5 downto 3); -- ddd reg code
798
p_field <= IR(5 downto 4); -- pp 16-bit reg pair code   
799
 
800
 
801
--##############################################################################
802
-- Microcode sequencer
803
 
804
process(clk)    -- do_reset is reset delayed 1 cycle
805
begin
806
  if clk'event and clk='1' then
807
    do_reset <= reset;
808
  end if;
809
end process;
810
 
811
uc_flags1 <= ucode(31 downto 29);
812
uc_flags2 <= ucode(28 downto 26);
813
 
814
-- microcode address control flags are gated by do_reset (reset has priority)
815
uc_do_ret <= '1' when uc_flags2 = "011" and do_reset = '0' else '0';
816
uc_jsr    <= '1' when uc_flags2 = "010" and do_reset = '0' else '0';
817
uc_tjsr   <= '1' when uc_flags2 = "100" and do_reset = '0' else '0';
818
uc_decode <= '1' when uc_flags1 = "001" and do_reset = '0' else '0';
819
uc_end    <= '1' when (uc_flags2 = "001" or (uc_tjsr='1' and condition_reg='0'))
820
                  and do_reset = '0' else '0';
821
 
822
-- other microinstruction flags are decoded
823
uc_halt_flag  <= '1' when uc_flags1 = "111" else '0';
824
uc_halt   <= '1' when uc_halt_flag='1' and inta_reg='0' else '0';
825
uc_ei     <= '1' when uc_flags1 = "011" else '0';
826
uc_di     <= '1' when uc_flags1 = "010" or inta_reg='1' else '0';
827
-- clr_t1/2 clears T1/T2 when explicitly commanded; T2 and T1 clear implicitly 
828
-- at the end of each instruction (by uc_decode)
829
clr_t2    <= '1' when uc_flags2 = "001" else '0';
830
clr_t1    <= '1' when uc_flags1 = "110" else '0';
831
use_aux   <= '1' when uc_flags1 = "101" else '0';
832
set_aux   <= '1' when uc_flags2 = "111" else '0';
833
 
834
load_al <= ucode(24);
835
load_addr <= ucode(25);
836
 
837
do_cy_op_d <= '1' when ucode(5 downto 2)="1011" else '0'; -- decode CY ALU op
838
do_cpc_d <= ucode(0); -- decode CPC ALU op
839
 
840
-- uinst jump command, either unconditional or on a given condition
841
uc_do_jmp <= uc_jsr or (uc_tjsr and condition_reg);
842
 
843
vma <= load_addr;  -- addr is valid, either for memmory or io
844
 
845 19 ja_rd
-- assume the only uinst that does memory access in the range 0..f is 'fetch'
846
fetch <= '1' when uc_addr(7 downto 4)=X"0" and load_addr='1' else '0';
847
 
848 2 ja_rd
-- external bus interface control signals
849
io <= '1' when uc_flags1="100" else '0'; -- IO access (vs. memory)
850
rd <= '1' when uc_flags2="101" else '0'; -- RD access
851
wr <= '1' when uc_flags2="110" else '0'; -- WR access  
852
 
853
uc_jmp_addr <= ucode(11 downto 10) & ucode(5 downto 0);
854
 
855
uc_addr_sel <= uc_do_ret & uc_do_jmp & uc_decode & uc_end;
856
 
857
addr_plus_1 <= uc_addr + 1;
858
 
859
-- TODO simplify this!!
860
 
861
-- NOTE: when end='1' we jump either to the FETCH ucode ot to the HALT ucode
862
-- depending on the value of the halt signal.
863
-- We use the unregistered uc_halt instead of halt_reg because otherwise #end
864
-- should be on the cycle following #halt, wasting a cycle.
865
-- This means that the flag #halt has to be used with #end or will be ignored. 
866
 
867
with uc_addr_sel select
868
  next_uc_addr <= '0'&uc_ret_addr when "1000", -- ret
869
                  '0'&uc_jmp_addr when "0100", -- jsr/tjsr
870
                  '0'&addr_plus_1 when "0000", -- uaddr++
871
                  "000000"&uc_halt&"11"
872
                                  when "0001", -- end: go to fetch/halt uaddr
873
                  '1'&DI          when others; -- decode fetched address 
874
 
875
-- Note how we used DI (containing instruction opcode) as a microcode address
876
 
877
-- read microcode rom 
878
process (clk)
879
begin
880
  if clk'event and clk='1' then
881
    ucode <= rom(conv_integer(next_uc_addr));
882
  end if;
883
end process;
884
 
885
-- microcode address register
886
process (clk)
887
begin
888
  if clk'event and clk='1' then
889
    if reset = '1' then
890
      uc_addr <= X"00";
891
    else
892
      uc_addr <= next_uc_addr(7 downto 0);
893
    end if;
894
  end if;
895
end process;
896
 
897
-- ucode address 1-level 'return stack'
898
process (clk)
899
begin
900
  if clk'event and clk='1' then
901
    if reset = '1' then
902
      uc_ret_addr <= X"00";
903
    elsif uc_do_jmp='1' then
904
      uc_ret_addr <= addr_plus_1;
905
    end if;
906
  end if;
907
end process;
908
 
909
 
910
alu_op <= ucode(3 downto 0);
911
 
912
-- pipeline uinst field2 for 1-cycle delayed execution.
913
-- note the same rbank addr field is used in cycles 1 and 2; this enforces
914
-- some constraints on uinst programming but simplifies the system.
915
process(clk)
916
begin
917
  if clk'event and clk='1' then
918
    ucode_field2 <= do_cy_op_d & do_cpc_d & clr_t2 & clr_t1 &
919
                    set_aux & use_aux & rbank_rd_addr &
920
                    ucode(14 downto 4) & alu_op;
921
  end if;
922
end process;
923
 
924
--#### HALT logic
925
process(clk)
926
begin
927
  if clk'event and clk='1' then
928
    if reset = '1' or int_pending = '1' then --inta_reg
929
      halt_reg <= '0';
930
    else
931
      if uc_halt = '1' then
932
        halt_reg <= '1';
933
      end if;
934
    end if;
935
  end if;
936
end process;
937
 
938
halt <= halt_reg;
939
 
940
--#### INTE logic -- inte_reg = '1' means interrupts ENABLED
941
process(clk)
942
begin
943
  if clk'event and clk='1' then
944
    if reset = '1' then
945
      inte_reg <= '0';
946 49 ja_rd
      delayed_ei <= '0';
947 2 ja_rd
    else
948 49 ja_rd
      if (uc_di='1' or uc_ei='1') and uc_end='1' then
949
        --inte_reg <= uc_ei;
950
        delayed_ei <= uc_ei; -- FIXME DI must not be delayed
951 2 ja_rd
      end if;
952 49 ja_rd
      if uc_end = '1' then -- at the last cycle of every instruction...
953
        if uc_di='1' then  -- ...disable interrupts if the instruction is DI...
954
          inte_reg <= '0';
955
        else
956
          -- ...of enable interrupts after the instruction following EI
957
          inte_reg <= delayed_ei;
958
        end if;
959
      end if;
960 2 ja_rd
    end if;
961
  end if;
962
end process;
963
 
964
inte <= inte_reg;
965
 
966 39 ja_rd
-- interrupts are ignored when inte='0' but they are registered and will be
967
-- honored when interrupts are enabled
968 2 ja_rd
process(clk)
969
begin
970
  if clk'event and clk='1' then
971
    if reset = '1' then
972
      int_pending <= '0';
973
    else
974 39 ja_rd
      -- intr will raise int_pending only if inta has not been asserted. 
975
      -- Otherwise, if intr overlapped inta, we'd enter a microcode endless 
976
      -- loop, executing the interrupt vector again and again.
977
      if intr='1' and inte_reg='1' and int_pending='0' and inta_reg='0' then
978 2 ja_rd
        int_pending <= '1';
979
      else
980 39 ja_rd
        -- int_pending is cleared when we're about to service the interrupt, 
981
        -- that is when interrupts are enabled and the current instruction ends.
982 2 ja_rd
        if inte_reg = '1' and uc_end='1' then
983
          int_pending <= '0';
984
        end if;
985
      end if;
986
    end if;
987
  end if;
988
end process;
989
 
990
 
991
--#### INTA logic
992
-- INTA goes high from END to END, that is for the entire time the instruction
993
-- takes to fetch and execute; in the original 8080 it was asserted only for 
994
-- the M1 cycle.
995
-- All instructions can be used in an inta cycle, including XTHL which was
996
-- forbidden in the original 8080. 
997
-- It's up to you figuring out which cycle is which in multibyte instructions.
998
process(clk)
999
begin
1000
  if clk'event and clk='1' then
1001
    if reset = '1' then
1002
      inta_reg <= '0';
1003
    else
1004
      if int_pending = '1' and uc_end='1' then
1005
        -- enter INTA state
1006
        inta_reg <= '1';
1007
      else
1008
        -- exit INTA state
1009
        -- NOTE: don't reset inta when exiting halt state (uc_halt_flag='1').
1010
        -- If we omit this condition, when intr happens on halt state, inta
1011
        -- will only last for 1 cycle, because in halt state uc_end is 
1012
        -- always asserted.
1013
        if uc_end = '1' and uc_halt_flag='0' then
1014
          inta_reg <= '0';
1015
        end if;
1016
      end if;
1017
    end if;
1018
  end if;
1019
end process;
1020
 
1021
inta <= inta_reg;
1022
 
1023
 
1024
--##############################################################################
1025
-- Datapath
1026
 
1027
-- extract pipelined microcode fields
1028
ra_field <= ucode(18 downto 15);
1029
load_t1 <= ucode(23);
1030
load_t2 <= ucode(22);
1031
mux_in <= ucode(21);
1032
rb_addr_sel <= ucode(20 downto 19);
1033
load_do <= ucode_field2(7);
1034
set_aux_cy <= ucode_field2(20);
1035
do_clr_t1 <= ucode_field2(21);
1036
do_clr_t2 <= ucode_field2(22);
1037
 
1038
 
1039
-- T1 register 
1040
process (clk)
1041
begin
1042
  if clk'event and clk='1' then
1043
    if reset = '1' or uc_decode = '1' or do_clr_t1='1' then
1044
      T1 <= X"00";
1045
    else
1046
      if load_t1 = '1' then
1047
        T1 <= alu_input;
1048
      end if;
1049
    end if;
1050
  end if;
1051
end process;
1052
 
1053
-- T2 register
1054
process (clk)
1055
begin
1056
  if clk'event and clk='1' then
1057
    if reset = '1' or uc_decode = '1' or do_clr_t2='1' then
1058
      T2 <= X"00";
1059
    else
1060
      if load_t2 = '1' then
1061
        T2 <= alu_input;
1062
      end if;
1063
    end if;
1064
  end if;
1065
end process;
1066
 
1067
-- T1/T2 input data mux
1068
alu_input <= rbank_data when mux_in = '1' else DI;
1069
 
1070
-- register bank address mux logic
1071
 
1072
rbh <= '1' when p_field = "11" else '0';
1073
 
1074
with rb_addr_sel select
1075
  rbank_rd_addr <=  ra_field    when "00",
1076
                    "0"&s_field when "01",
1077
                    "0"&d_field when "10",
1078
                    rbh&p_field&ra_field(0) when others;
1079
 
1080
-- RBank writes are inhibited in INTA state, but only for PC increments.
1081
inhibit_pc_increment <= '1' when inta_reg='1' and use_aux_cy='1'
1082
                                 and rbank_wr_addr(3 downto 1) = "100"
1083
                                 else '0';
1084
we_rb <= ucode_field2(6) and not inhibit_pc_increment;
1085
 
1086
-- Register bank logic
1087
-- NOTE: read is asynchronous, while write is synchronous; but note also
1088
-- that write phase for a given uinst happens the cycle after the read phase.
1089
-- This way we give the ALU time to do its job.
1090
rbank_wr_addr <= ucode_field2(18 downto 15);
1091
process(clk)
1092
begin
1093
  if clk'event and clk='1' then
1094
    if we_rb = '1' then
1095
      rbank(conv_integer(rbank_wr_addr)) <= alu_output;
1096
    end if;
1097
  end if;
1098
end process;
1099
rbank_data <= rbank(conv_integer(rbank_rd_addr));
1100
 
1101
-- should we read F register or ALU output?
1102
use_psw <= '1' when ucode_field2(5 downto 4)="11" else '0';
1103
data_output <= flag_reg when use_psw = '1' else alu_output;
1104
 
1105
 
1106
process (clk)
1107
begin
1108
  if clk'event and clk='1' then
1109
    if load_do = '1' then
1110
        DO <= data_output;
1111
    end if;
1112
  end if;
1113
end process;
1114
 
1115
--##############################################################################
1116
-- ALU 
1117
 
1118
alu_fn <= ucode_field2(1 downto 0);
1119
use_logic <= ucode_field2(2);
1120
mux_fn <= ucode_field2(4 downto 3);
1121
--#### make sure this is "00" in the microcode when no F updates should happen!
1122
flag_pattern <=  ucode_field2(9 downto 8);
1123
use_aux_cy <= ucode_field2(19);
1124
do_cpc <= ucode_field2(23);
1125
do_cy_op <= ucode_field2(24);
1126 54 ja_rd
do_daa <= '1' when ucode_field2(5 downto 2) = "1010" else '0';
1127
do_xor <= '1' when ucode_field2(5 downto 0) = "000101" else '0';
1128 2 ja_rd
 
1129
aux_cy_in <= reg_aux_cy when set_aux_cy = '0' else '1';
1130
 
1131
-- carry input selection: normal or aux (for 16 bit increments)?
1132
cy_in <= flag_reg(0) when use_aux_cy = '0' else aux_cy_in;
1133
 
1134
-- carry is not used (0) in add/sub operations
1135
cy_in_gated <= cy_in and alu_fn(0);
1136
 
1137
--##### Adder/substractor
1138
 
1139
-- zero extend adder operands to 9 bits to ease CY output synthesis
1140
-- use zero extension because we're only interested in cy from 7 to 8
1141
arith_op1 <= '0' & T2;
1142
arith_op2 <= '0' & T1;
1143
 
1144
-- The adder/substractor is done in 2 stages to help XSL synth it properly
1145
-- Other codings result in 1 adder + a substractor + 1 mux
1146
 
1147
-- do 2nd op 2's complement if substracting...
1148
arith_op2_sgn <=  arith_op2 when alu_fn(1) = '0' else not arith_op2;
1149
-- ...and complement cy input too
1150
cy_in_sgn <= cy_in_gated when alu_fn(1) = '0' else not cy_in_gated;
1151
 
1152
-- once 2nd operand has been negated (or not) add operands normally
1153
arith_res <= arith_op1 + arith_op2_sgn + cy_in_sgn;
1154
 
1155
-- take only 8 bits; 9th bit of adder is cy output
1156
arith_res8 <= arith_res(7 downto 0);
1157
cy_adder <= arith_res(8);
1158
 
1159
--##### DAA dedicated logic
1160
-- Note a DAA takes 2 cycles to complete! 
1161
 
1162
--daa_test1a='1' when daa_res9(7 downto 4) > 0x06
1163
daa_test1a <= arith_op2(3) and (arith_op2(2) or arith_op2(1) or arith_op2(0));
1164
daa_test1 <= '1' when flag_reg(4)='1' or daa_test1a='1' else '0';
1165
 
1166
process(clk)
1167
begin
1168
  if clk'event and clk='1' then
1169
    if reset='1' then
1170
      daa_res9 <= "000000000";
1171
    else
1172
      if daa_test1='1' then
1173
        daa_res9 <= arith_op2 + "000000110";
1174
      else
1175
        daa_res9 <= arith_op2;
1176
      end if;
1177
    end if;
1178
  end if;
1179
end process;
1180
 
1181
--daa_test2a='1' when daa_res9(7 downto 4) > 0x06 FIXME unused?
1182
daa_test2a <= daa_res9(7) and (daa_res9(6) or daa_res9(5) or daa_res9(4));
1183
daa_test2 <= '1' when flag_reg(0)='1' or daa_test1a='1' else '0';
1184
 
1185
daa_res <= '0'&daa_res9(7 downto 0) + "01100000" when daa_test2='1'
1186
           else daa_res9;
1187
 
1188
cy_daa <= daa_res(8);
1189
 
1190
-- DAA vs. adder mux
1191
arith_daa_res <= daa_res(7 downto 0) when do_daa='1' else arith_res8;
1192
 
1193
-- DAA vs. adder CY mux
1194
cy_arith <= cy_daa when do_daa='1' else cy_adder;
1195
 
1196
--##### Logic operations block
1197
logic_res <=  T1 and T2 when alu_fn = "00" else
1198
              T1 xor T2 when alu_fn = "01" else
1199
              T1 or  T2 when alu_fn = "10" else
1200
              not T1;
1201
 
1202
--##### Shifter
1203
shifter:
1204
for i in 1 to 6 generate
1205
begin
1206
  shift_res(i) <= T1(i-1) when alu_fn(0) = '0' else T1(i+1);
1207
end generate;
1208
shift_res(0) <= T1(7) when alu_fn = "00" else -- rot left 
1209
                cy_in when alu_fn = "10" else -- rot left through carry
1210
                T1(1); -- rot right
1211
shift_res(7) <= T1(0) when alu_fn = "01" else -- rot right
1212
                cy_in when alu_fn = "11" else -- rot right through carry
1213
                T1(6); -- rot left
1214
 
1215
cy_shifter   <= T1(7) when alu_fn(0) = '0' else -- left
1216
                T1(0);                          -- right
1217
 
1218
alu_mux1 <= logic_res when use_logic = '1' else shift_res;
1219
 
1220
 
1221
with mux_fn select
1222
  alu_output <= alu_mux1      when "00",
1223
                arith_daa_res when "01",
1224
                not alu_mux1  when "10",
1225
                "00"&d_field&"000" when others; -- RST  
1226
 
1227
--###### flag computation 
1228
 
1229
flag_s <= alu_output(7);
1230
flag_p <= not(alu_output(7) xor alu_output(6) xor alu_output(5) xor alu_output(4) xor
1231
         alu_output(3) xor alu_output(2) xor alu_output(1) xor alu_output(0));
1232
flag_z <= '1' when alu_output=X"00" else '0';
1233 54 ja_rd
-- FIXED 08/JUL/2010: XOR was  not clearing AC as it should
1234
--flag_ac <= (arith_op1(4) xor arith_op2_sgn(4) xor alu_output(4));
1235
flag_ac <= (arith_op1(4) xor arith_op2_sgn(4) xor alu_output(4)) and not do_xor;
1236 2 ja_rd
 
1237 54 ja_rd
-- FIXED 08/JUL/2010: XOR was not clearing CY as it should
1238
--flag_cy_1 <= cy_arith when use_logic = '1' else cy_shifter;
1239
flag_cy_1 <=  '0'       when do_xor='1' else
1240
              cy_arith  when use_logic = '1' and do_xor='0' else
1241
              cy_shifter;
1242 2 ja_rd
flag_cy_2 <= not flag_reg(0) when do_cpc='0' else '1'; -- cmc, stc
1243
flag_cy <= flag_cy_1 when do_cy_op='0' else flag_cy_2;
1244
 
1245
flag_aux_cy <= cy_adder;
1246
 
1247
-- auxiliary carry reg
1248
process(clk)
1249
begin
1250
  if clk'event and clk='1' then
1251
    if reset='1' or uc_decode = '1' then
1252
      reg_aux_cy <= '1'; -- inits to 0 every instruction
1253
    else
1254
      reg_aux_cy <= flag_aux_cy;
1255
    end if;
1256
  end if;
1257
end process;
1258
 
1259
-- load PSW from ALU (i.e. POP AF) or from flag signals
1260
load_psw <= '1' when we_rb='1' and rbank_wr_addr="0110" else '0';
1261
 
1262
-- The F register has been split in two separate groupt that always update
1263
-- together (C and all others).
1264
 
1265
-- F register, flags S,Z,AC,P
1266
process(clk)
1267
begin
1268
  if clk'event and clk='1' then
1269
    if reset='1' then
1270
      flag_reg(7) <= '0';
1271
      flag_reg(6) <= '0';
1272
      flag_reg(4) <= '0';
1273
      flag_reg(2) <= '0';
1274
    elsif flag_pattern(1) = '1' then
1275
      if load_psw = '1' then
1276
        flag_reg(7) <= alu_output(7);
1277
        flag_reg(6) <= alu_output(6);
1278
        flag_reg(4) <= alu_output(4);
1279
        flag_reg(2) <= alu_output(2);
1280
      else
1281
        flag_reg(7) <= flag_s;
1282
        flag_reg(6) <= flag_z;
1283
        flag_reg(4) <= flag_ac;
1284
        flag_reg(2) <= flag_p;
1285
      end if;
1286
    end if;
1287
  end if;
1288
end procesS;
1289
 
1290
-- F register, flag C
1291
process(clk)
1292
begin
1293
  if clk'event and clk='1' then
1294
    if reset = '1' then
1295
      flag_reg(0) <= '0';
1296
    elsif flag_pattern(0) = '1' then
1297
      if load_psw = '1' then
1298
        flag_reg(0) <= alu_output(0);
1299
      else
1300
        flag_reg(0) <= flag_cy;
1301
      end if;
1302
    end if;
1303
  end if;
1304
end procesS;
1305
 
1306
flag_reg(5) <= '0'; -- constant flag
1307
flag_reg(3) <= '0'; -- constant flag
1308
flag_reg(1) <= '1'; -- constant flag
1309
 
1310
--##### Condition computation
1311
 
1312
condition_sel <= d_field(2 downto 0);
1313
with condition_sel select
1314
  condition <=
1315
            not flag_reg(6) when "000", -- NZ
1316
                flag_reg(6) when "001", -- Z
1317
            not flag_reg(0) when "010", -- NC
1318
                flag_reg(0) when "011", -- C
1319
            not flag_reg(2) when "100", -- PO
1320
                flag_reg(2) when "101", -- PE  
1321
            not flag_reg(7) when "110", -- P  
1322
                flag_reg(7) when others;-- M                  
1323
 
1324
 
1325
-- condition is registered to shorten the delay path; the extra 1-cycle
1326
-- delay is not relevant because conditions are tested in the next instruction
1327
-- at the earliest, and there's at least the fetch uinsts intervening.                
1328
process(clk)
1329
begin
1330
  if clk'event and clk='1' then
1331
    if reset = '1' then
1332
      condition_reg <= '0';
1333
    else
1334
      condition_reg <= condition;
1335
    end if;
1336
  end if;
1337
end process;
1338
 
1339
-- low byte address register
1340
process(clk)
1341
begin
1342
  if clk'event and clk='1' then
1343
    if reset = '1' then
1344
      addr_low <= X"00";
1345
    elsif load_al = '1' then
1346
      addr_low <= rbank_data;
1347
    end if;
1348
  end if;
1349
end process;
1350
 
1351
-- note external address registers (high byte) are loaded directly from rbank
1352
addr_out <= rbank_data & addr_low;
1353
 
1354
data_out <= DO;
1355
 
1356
end microcoded;
1357 19 ja_rd
 
1358
--------------------------------------------------------------------------------
1359
-- Timing diagram 1: RD and WR cycles
1360
--------------------------------------------------------------------------------
1361
--            1     2     3     4     5     6     7     8     
1362
--             __    __    __    __    __    __    __    __   
1363
-- clk      __/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__
1364
--
1365 39 ja_rd
--          ==|=====|=====|=====|=====|=====|=====|=====|=====|
1366
--
1367 19 ja_rd
-- addr_o   xxxxxxxxxxxxxx< ADR >xxxxxxxxxxx< ADR >xxxxxxxxxxx
1368
--
1369
-- data_i   xxxxxxxxxxxxxxxxxxxx< Din >xxxxxxxxxxxxxxxxxxxxxxx
1370
--
1371
-- data_o   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< Dout>xxxxxxxxxxx
1372
--                         _____             _____
1373
-- vma_o    ______________/     \___________/     \___________
1374
--                         _____
1375
-- rd_o     ______________/     \_____________________________
1376
--                                           _____
1377
-- wr_o     ________________________________/     \___________
1378
--
1379
-- (functional diagram, actual time delays not shown)
1380
--------------------------------------------------------------------------------
1381
-- This diagram shows a read cycle and a write cycle back to back.
1382
-- In clock edges (4) and (7), the address is loaded into the external 
1383
-- synchronous RAM address register. 
1384
-- In clock edge (5), read data is loaded into the CPU.
1385
-- In clock edge (7), write data is loaded into the external synchronous RAM.
1386
-- In actual operation, the CPU does about 1 rd/wr cycle for each 5 clock 
1387
-- cycles, which is a waste of RAM bandwidth.
1388
--

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.