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

Subversion Repositories light8080

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

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

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

powered by: WebSVN 2.1.0

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