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

Subversion Repositories light8080

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

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

powered by: WebSVN 2.1.0

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