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

Subversion Repositories light8080

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

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

powered by: WebSVN 2.1.0

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