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

Subversion Repositories System09

[/] [System09/] [trunk/] [rtl/] [VHDL/] [cpu09m.vhd] - Blame information for rev 216

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

Line No. Rev Author Line
1 130 dilbert57
--===========================================================================--
2
--                                                                           --
3
--        Synthesizable 6809 instruction compatible VHDL CPU core            --
4
--                                                                           --
5
--===========================================================================--
6
--
7
-- File name      : cpu09m.vhd
8
--
9
-- Entity name    : cpu09
10
--
11
-- Purpose        : 6809 instruction compatible CPU core written in VHDL
12
--                  with Last Instruction Cycle, bus available, bus status,
13
--                  and instruction fetch signals.
14
--                  Not cycle compatible with the original 6809 CPU
15
--
16
-- Dependencies   : ieee.std_logic_1164
17
--                  ieee.std_logic_unsigned
18
--
19
-- Author         : John E. Kent
20
--
21
-- Email          : dilbert57@opencores.org      
22
--
23
-- Web            : http://opencores.org/project,system09
24
--
25
-- Description    : VMA (valid memory address) is hight whenever a valid memory
26
--                  access is made by an instruction fetch, interrupt vector fetch
27
--                  or a data read or write otherwise it is low indicating an idle
28
--                  bus cycle.
29
--                  IFETCH (instruction fetch output) is high whenever an
30
--                  instruction byte is read i.e. the program counter is applied 
31
--                  to the address bus.
32
--                  LIC (last instruction cycle output) is normally low
33
--                  but goes high on the last cycle of an instruction.
34
--                  BA (bus available output) is normally low but goes high while
35
--                  waiting in a Sync instruction state or the CPU is halted
36
--                  i.e. a DMA grant.
37
--                  BS (bus status output) is normally low but goes high during an
38
--                  interrupt or reset vector fetch or the processor is halted
39
--                  i.e. a DMA grant.
40
-- 
41
--  Copyright (C) 2003 - 2010 John Kent
42
--
43
--  This program is free software: you can redistribute it and/or modify
44
--  it under the terms of the GNU General Public License as published by
45
--  the Free Software Foundation, either version 3 of the License, or
46
--  (at your option) any later version.
47
--
48
--  This program is distributed in the hope that it will be useful,
49
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
50
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51
--  GNU General Public License for more details.
52
--
53
--  You should have received a copy of the GNU General Public License
54
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
55
--
56
--===========================================================================--
57
--                                                                           --
58
--                                Revision History                           --
59
--                                                                           --
60
--===========================================================================--
61
--
62
-- Version 0.1 - 26 June 2003 - John Kent
63
-- Added extra level in state stack
64
-- fixed some calls to the extended addressing state
65
--
66
-- Version 0.2 - 5 Sept 2003 - John Kent
67
-- Fixed 16 bit indexed offset (was doing read rather than fetch)
68
-- Added/Fixed STY and STS instructions.
69
-- ORCC_STATE ANDed CC state rather than ORed it - Now fixed
70
-- CMPX Loaded ACCA and ACCB - Now fixed 
71
--
72
-- Version 1.0 - 6 Sep 2003 - John Kent 
73
-- Initial release to Open Cores
74
-- reversed clock edge
75
--
76
-- Version 1.1 - 29 November 2003 John kent
77
--      ACCA and ACCB indexed offsets are 2's complement.
78
-- ALU Right Mux now sign extends ACCA & ACCB offsets
79
-- Absolute Indirect addressing performed a read on the
80
-- second byte of the address rather than a fetch
81
-- so it formed an incorrect address. Now fixed. 
82
--
83
-- Version 1.2 - 29 November 2003 John Kent
84
-- LEAX and LEAY affect the Z bit only
85
--      LEAS and LEAU do not affect any condition codes
86
-- added an extra ALU control for LEA.
87
--
88
-- Version 1.3 - 12 December 2003 John Kent
89
-- CWAI did not work, was missed a PUSH_ST on calling
90
-- the ANDCC_STATE. Thanks go to Ghassan Kraidy for
91
-- finding this fault.
92
--
93
-- Version 1.4 - 12 December 2003 John Kent
94
-- Missing cc_ctrl assignment in otherwise case of 
95
-- lea_state resulted in cc_ctrl being latched in
96
-- that state.  
97
-- The otherwise statement should never be reached,
98
-- and has been fixed simply to resolve synthesis warnings.
99
--
100
-- Version 1.5 - 17 january 2004 John kent
101
-- The clear instruction used "alu_ld8" to control the ALU
102
-- rather than "alu_clr". This mean the Carry was not being
103
-- cleared correctly.
104
--
105
-- Version 1.6 - 24 January 2004 John Kent
106
-- Fixed problems in PSHU instruction
107
--
108
-- Version 1.7 - 25 January 2004 John Kent
109
-- removed redundant "alu_inx" and "alu_dex'
110
-- Removed "test_alu" and "test_cc"
111
-- STD instruction did not set condition codes
112
-- JMP direct was not decoded properly
113
-- CLR direct performed an unwanted read cycle
114
-- Bogus "latch_md" in Page2 indexed addressing
115
--
116
-- Version 1.8 - 27 January 2004 John Kent
117
-- CWAI in decode1_state should increment the PC.
118
-- ABX is supposed to be an unsigned addition.
119
-- Added extra ALU function
120
-- ASR8 slightly changed in the ALU.
121
--
122
--      Version 1.9 - 20 August 2005
123
-- LSR8 is now handled in ASR8 and ROR8 case in the ALU,
124
-- rather than LSR16. There was a problem with single 
125
-- operand instructions using the MD register which is
126
-- sign extended on the first 8 bit fetch.
127
--
128
-- Version 1.10 - 13 September 2005
129
-- TFR & EXG instructions did not work for the Condition Code Register
130
-- An extra case has been added to the ALU for the alu_tfr control 
131
-- to assign the left ALU input (alu_left) to the condition code
132
-- outputs (cc_out). 
133
--
134
-- Version 1.11 - 16 September 2005
135
-- JSR ,X should not predecrement S before calculating the jump address.
136
-- The reason is that JSR [0,S] needs S to point to the top of the stack
137
-- to fetch a valid vector address. The solution is to have the addressing
138
-- mode microcode called before decrementing S and then decrementing S in
139
-- JSR_STATE. JSR_STATE in turn calls PUSH_RETURN_LO_STATE rather than
140
-- PUSH_RETURN_HI_STATE so that both the High & Low halves of the PC are
141
-- pushed on the stack. This adds one extra bus cycle, but resolves the
142
-- addressing conflict. I've also removed the pre-decement S in 
143
-- JSR EXTENDED as it also calls JSR_STATE.
144
--
145
-- Version 1.12 - 6th June 2006
146
-- 6809 Programming reference manual says V is not affected by ASR, LSR and ROR
147
-- This is different to the 6800. CLR should reset the V bit.
148
--
149
-- Version 1.13 - 7th July 2006
150
-- Disable NMI on reset until S Stack pointer has been loaded.
151
-- Added nmi_enable signal in sp_reg process and nmi_handler process.
152
--
153
-- Version 1.14 - 11th July 2006
154
-- 1. Added new state to RTI called rti_entire_state.
155
-- This state tests the CC register after it has been loaded
156
-- from the stack. Previously the current CC was tested which
157
-- was incorrect. The Entire Flag should be set before the
158
-- interrupt stacks the CC.
159
-- 2. On bogus Interrupts, int_cc_state went to rti_state,
160
-- which was an enumerated state, but not defined anywhere.
161
-- rti_state has been changed to rti_cc_state so that bogus interrupt
162
-- will perform an RTI after entering that state.
163
-- 3. Sync should generate an interrupt if the interrupt masks
164
-- are cleared. If the interrupt masks are set, then an interrupt
165
-- will cause the the PC to advance to the next instruction.
166
-- Note that I don't wait for an interrupt to be asserted for
167
-- three clock cycles.
168
-- 4. Added new ALU control state "alu_mul". "alu_mul" is used in
169
-- the Multiply instruction replacing "alu_add16". This is similar 
170
-- to "alu_add16" except it sets the Carry bit to B7 of the result
171
-- in ACCB, sets the Zero bit if the 16 bit result is zero, but
172
-- does not affect The Half carry (H), Negative (N) or Overflow (V)
173
-- flags. The logic was re-arranged so that it adds md or zero so 
174
-- that the Carry condition code is set on zero multiplicands.
175
-- 5. DAA (Decimal Adjust Accumulator) should set the Negative (N)
176
-- and Zero Flags. It will also affect the Overflow (V) flag although
177
-- the operation is undefined. It's anyones guess what DAA does to V.
178
--
179
-- Version 1.15 - 25th Feb 2007 - John Kent
180
-- line 9672 changed "if Halt <= '1' then" to "if Halt = '1' then"
181
-- Changed sensitivity lists.
182
--
183
-- Version 1.16 - 5th February 2008 - John Kent
184
-- FIRQ interrupts should take priority over IRQ Interrupts.
185
-- This presumably means they should be tested for before IRQ
186
-- when they happen concurrently.
187
--
188
-- Version 1.17 - 18th February 2008 - John Kent
189
-- NMI in CWAI should mask IRQ and FIRQ interrupts
190
--
191
-- Version 1.18 - 21st February 2008 - John Kent
192
-- Removed default register settings in each case statement
193
-- and placed them at the beginning of the state sequencer.
194
-- Modified the SYNC instruction so that the interrupt vector(iv)
195
-- is not set unless an unmasked FIRQ or IRQ is received.
196
--
197
-- Version 1.19 - 25th February 2008 - John Kent
198
-- Enumerated separate states for FIRQ/FAST and NMIIRQ/ENTIRE
199
-- Enumerated separate states for MASKI and MASKIF states
200
-- Removed code on BSR/JSR in fetch cycle
201
--
202
-- Version 1.20 - 8th October 2011 - John Kent
203
-- added fetch output which should go high during the fetch cycle
204
--
205
-- Version 1.21 - 8th October 2011 - John Kent
206
-- added Last Instruction Cycle signal
207
-- replaced fetch with ifetch (instruction fetch) signal
208
-- added ba & bs (bus available & bus status) signals
209
--
210
-- Version 1.22 - 2011-10-29 John Kent
211
-- The halt state isn't correct. 
212
-- The halt state is entered into from the fetch_state
213
-- It returned to the fetch state which may re-run an execute cycle
214
-- on the accumulator and it won't necessarily be the last instruction cycle
215
-- I've changed the halt state to return to the decode1_state
216
--
217
-- Version 1.23 - 2011-10-30 John Kent
218
-- sample halt in the change_state process if lic is high (last instruction cycle)
219
--
220
-- Version 1.24 - 2011-11-01 John Kent
221
-- Handle interrupts in change_state process
222
-- Sample interrupt inputs on last instruction cycle
223
-- Remove iv_ctrl and implement iv (interrupt vector) in change_state process.
224
-- Generate fic (first instruction cycle) from lic (last instruction cycle)
225
-- and use it to complete the dual operand execute cycle before servicing
226
-- halt or interrupts requests.
227
-- rename lic to lic_out on the entity declaration so that lic can be tested internally.
228
-- add int_firq1_state and int_nmirq1_state to allow for the dual operand execute cycle
229
-- integrated nmi_ctrl into change_state process
230
-- Reduces the microcode state stack to one entry (saved_state)
231
-- imm16_state jumps directly to the fetch_state
232
-- pull_return_lo states jumps directly to the fetch_state
233
-- duplicate andcc_state as cwai_state
234
-- rename exg1_state as exg2 state and duplicate tfr_state as exg1_state
235
--
236
-- Version 1.25 - 2011-11-27 John Kent
237
-- Changed the microcode for saving registers on an interrupt into a microcode subroutine.
238
-- Removed SWI servicing from the change state process and made SWI, SWI2 & SWI3
239
-- call the interrupt microcode subroutine.
240
-- Added additional states for nmi, and irq for interrupt servicing.
241
-- Added additional states for nmi/irq, firq, and swi interrupts to mask I & F flags.
242
--
243
-- Version 1.26 - 2013-03-18 John Kent
244
-- pre-initialized cond_true variable to true in state sequencer
245
-- re-arranged change_state process slightly
246
--
247
-- Version 1.27 - 2015-05-30 John Kent
248
-- Added test in state machine for masked IRQ and FIRQ in Sync_state.
249
--
250
-- Version 1.28 - 2015-05-30 John Kent.
251
-- Moved IRQ and FIRQ test from state machine to the state sequencer Sync_state.
252
--
253
-- Version 1.29 - 2017-02-11 John Kent
254
-- Make MUL 11 cycles long rather than 13 cycles.
255
-- 1 fetch_state, 2 decode1_state, 
256
-- 3 mul_state, 4 mulea_state, 5 muld_state
257
-- 6 mul0_state, 7 mul1_state, 8 mul2_state, 9 mul3_state,
258
-- 10 mul4_state, 11 mul5_state, 12 mul6_state, 13 mul7_state,
259
-- move mul_state into decode1_state
260
-- move mul7_state into fetch_state
261
--
262
-- Version 1.29 - 2017-03-08 John Kent.
263
-- Moved LIC in ABX ?
264
--
265
library ieee;
266
use ieee.std_logic_1164.all;
267
use ieee.std_logic_unsigned.all;
268
 
269
entity cpu09 is
270
        port (
271
                clk      :      in std_logic;                     -- E clock input (falling edge)
272
                rst      :  in std_logic;                     -- reset input (active high)
273
                vma      : out std_logic;                     -- valid memory address (active high)
274
      lic_out  : out std_logic;                     -- last instruction cycle (active high)
275
      ifetch   : out std_logic;                     -- instruction fetch cycle (active high)
276
      opfetch  : out std_logic;                     -- opcode fetch (active high)
277
      ba       : out std_logic;                     -- bus available (high on sync wait or DMA grant)
278
      bs       : out std_logic;                     -- bus status (high on interrupt or reset vector fetch or DMA grant)
279
                addr     : out std_logic_vector(15 downto 0); -- address bus output
280
                rw       : out std_logic;                     -- read not write output
281
           data_out : out std_logic_vector(7 downto 0);  -- data bus output
282
           data_in  :  in std_logic_vector(7 downto 0);  -- data bus input
283
                irq      :  in std_logic;                     -- interrupt request input (active high)
284
                firq     :  in std_logic;                     -- fast interrupt request input (active high)
285
                nmi      :  in std_logic;                     -- non maskable interrupt request input (active high)
286
                halt     :  in std_logic;                     -- halt input (active high) grants DMA
287
                hold     :  in std_logic                      -- hold input (active high) extend bus cycle
288
                );
289
end cpu09;
290
 
291
architecture rtl of cpu09 is
292
 
293
  constant EBIT : integer := 7;
294
  constant FBIT : integer := 6;
295
  constant HBIT : integer := 5;
296
  constant IBIT : integer := 4;
297
  constant NBIT : integer := 3;
298
  constant ZBIT : integer := 2;
299
  constant VBIT : integer := 1;
300
  constant CBIT : integer := 0;
301
 
302
  --
303
  -- Interrupt vector modifiers
304
  --
305
  constant RST_VEC  : std_logic_vector(2 downto 0) := "111";
306
  constant NMI_VEC  : std_logic_vector(2 downto 0) := "110";
307
  constant SWI_VEC  : std_logic_vector(2 downto 0) := "101";
308
  constant IRQ_VEC  : std_logic_vector(2 downto 0) := "100";
309
  constant FIRQ_VEC : std_logic_vector(2 downto 0) := "011";
310
  constant SWI2_VEC : std_logic_vector(2 downto 0) := "010";
311
  constant SWI3_VEC : std_logic_vector(2 downto 0) := "001";
312
  constant RESV_VEC : std_logic_vector(2 downto 0) := "000";
313
 
314
        type state_type is (-- Start off in Reset
315
                            reset_state,
316
                                                          -- Fetch Interrupt Vectors (including reset)
317
                                                          vect_lo_state, vect_hi_state, vect_idle_state,
318
                       -- Fetch Instruction Cycle
319
                       fetch_state,
320
                                                          -- Decode Instruction Cycles
321
                       decode1_state, decode2_state, decode3_state,
322
                                                          -- Calculate Effective Address
323
                                                     imm16_state,
324
                                 indexed_state, index8_state, index16_state, index16_2_state,
325
                                                          pcrel8_state, pcrel16_state, pcrel16_2_state,
326
                                                          indexaddr_state, indexaddr2_state,
327
                                                     postincr1_state, postincr2_state,
328
                                                          indirect_state, indirect2_state, indirect3_state,
329
                       extended_state,
330
                                                          -- single ops
331
                                                          single_op_read_state,
332
                                                     single_op_exec_state,
333
                            single_op_write_state,
334
                                                          -- Dual op states
335
                                                          dual_op_read8_state, dual_op_read16_state, dual_op_read16_2_state,
336
                                                     dual_op_write8_state, dual_op_write16_state,
337
                       -- 
338
                                                     sync_state, halt_state, cwai_state,
339
                                                          --
340
                                                          andcc_state, orcc_state,
341
                                                          tfr_state,
342
                       exg_state, exg1_state, exg2_state,
343
                                                          lea_state,
344
                                                          -- Multiplication
345
                                                     mulea_state, muld_state,
346
                                                     mul0_state, mul1_state, mul2_state, mul3_state,
347
                                                     mul4_state, mul5_state, mul6_state,
348
                                                          --  Branches
349
                                                          lbranch_state, sbranch_state,
350
                                                          -- Jumps, Subroutine Calls and Returns
351
                       jsr_state, jmp_state,
352
                       push_return_hi_state, push_return_lo_state,
353
                       pull_return_hi_state, pull_return_lo_state,
354
                                                          -- Interrupt cycles
355
                                                          int_nmi_state, int_nmi1_state,
356
                                                          int_irq_state, int_irq1_state,
357
                       int_firq_state,  int_firq1_state,
358
                                                          int_entire_state, int_fast_state,
359
                                                          int_pcl_state,  int_pch_state,
360
                                                     int_upl_state,  int_uph_state,
361
                                                     int_iyl_state,  int_iyh_state,
362
                                                     int_ixl_state,  int_ixh_state,
363
                                                     int_dp_state,
364
                                           int_accb_state, int_acca_state,
365
                                                     int_cc_state,
366
                                                     int_cwai_state,
367
                                                          int_nmimask_state, int_firqmask_state, int_swimask_state, int_irqmask_state,
368
                                                          -- Return From Interrupt
369
                                                     rti_cc_state,   rti_entire_state,
370
                                                          rti_acca_state, rti_accb_state,
371
                                                     rti_dp_state,
372
                                                     rti_ixl_state,  rti_ixh_state,
373
                                                     rti_iyl_state,  rti_iyh_state,
374
                                                     rti_upl_state,  rti_uph_state,
375
                                                     rti_pcl_state,  rti_pch_state,
376
                                                          -- Push Registers using SP
377
                                                          pshs_state,
378
                                                     pshs_pcl_state,  pshs_pch_state,
379
                                                     pshs_upl_state,  pshs_uph_state,
380
                                                     pshs_iyl_state,  pshs_iyh_state,
381
                                                     pshs_ixl_state,  pshs_ixh_state,
382
                                                     pshs_dp_state,
383
                                                     pshs_acca_state, pshs_accb_state,
384
                                                     pshs_cc_state,
385
                                                          -- Pull Registers using SP
386
                                                          puls_state,
387
                                                          puls_cc_state,
388
                                                          puls_acca_state, puls_accb_state,
389
                                                          puls_dp_state,
390
                                                     puls_ixl_state,  puls_ixh_state,
391
                                                     puls_iyl_state,  puls_iyh_state,
392
                                                     puls_upl_state,  puls_uph_state,
393
                                                     puls_pcl_state,  puls_pch_state,
394
                                                          -- Push Registers using UP
395
                                                          pshu_state,
396
                                                     pshu_pcl_state,  pshu_pch_state,
397
                                                     pshu_spl_state,  pshu_sph_state,
398
                                                     pshu_iyl_state,  pshu_iyh_state,
399
                                                     pshu_ixl_state,  pshu_ixh_state,
400
                                                     pshu_dp_state,
401
                                                     pshu_acca_state, pshu_accb_state,
402
                                                     pshu_cc_state,
403
                                                          -- Pull Registers using UP
404
                                                          pulu_state,
405
                                                          pulu_cc_state,
406
                                                          pulu_acca_state, pulu_accb_state,
407
                                                          pulu_dp_state,
408
                                                     pulu_ixl_state,  pulu_ixh_state,
409
                                                     pulu_iyl_state,  pulu_iyh_state,
410
                                                     pulu_spl_state,  pulu_sph_state,
411
                                                     pulu_pcl_state,  pulu_pch_state );
412
 
413
        type st_type    is (reset_st, push_st, idle_st );
414
        type iv_type    is (latch_iv, swi3_iv, swi2_iv, firq_iv, irq_iv, swi_iv, nmi_iv, reset_iv);
415
        type addr_type  is (idle_ad, fetch_ad, read_ad, write_ad, pushu_ad, pullu_ad, pushs_ad, pulls_ad, int_hi_ad, int_lo_ad );
416
        type dout_type  is (cc_dout, acca_dout, accb_dout, dp_dout,
417
                       ix_lo_dout, ix_hi_dout, iy_lo_dout, iy_hi_dout,
418
                       up_lo_dout, up_hi_dout, sp_lo_dout, sp_hi_dout,
419
                       pc_lo_dout, pc_hi_dout, md_lo_dout, md_hi_dout );
420
   type op_type    is (reset_op, fetch_op, latch_op );
421
   type pre_type   is (reset_pre, fetch_pre, latch_pre );
422
   type cc_type    is (reset_cc, load_cc, pull_cc, latch_cc );
423
   type acca_type  is (reset_acca, load_acca, load_hi_acca, pull_acca, latch_acca );
424
   type accb_type  is (reset_accb, load_accb, pull_accb, latch_accb );
425
   type dp_type    is (reset_dp, load_dp, pull_dp, latch_dp );
426
        type ix_type    is (reset_ix, load_ix, pull_lo_ix, pull_hi_ix, latch_ix );
427
        type iy_type    is (reset_iy, load_iy, pull_lo_iy, pull_hi_iy, latch_iy );
428
        type sp_type    is (reset_sp, latch_sp, load_sp, pull_hi_sp, pull_lo_sp );
429
        type up_type    is (reset_up, latch_up, load_up, pull_hi_up, pull_lo_up );
430
        type pc_type    is (reset_pc, latch_pc, load_pc, pull_lo_pc, pull_hi_pc, incr_pc );
431
   type md_type    is (reset_md, latch_md, load_md, fetch_first_md, fetch_next_md, shiftl_md );
432
   type ea_type    is (reset_ea, latch_ea, load_ea, fetch_first_ea, fetch_next_ea );
433
        type left_type  is (cc_left, acca_left, accb_left, dp_left,
434
                                                          ix_left, iy_left, up_left, sp_left,
435
                       accd_left, md_left, pc_left, ea_left );
436
        type right_type is (ea_right, zero_right, one_right, two_right,
437
                       acca_right, accb_right, accd_right,
438
                                                          md_right, md_sign5_right, md_sign8_right );
439
   type alu_type   is (alu_add8, alu_sub8, alu_add16, alu_sub16, alu_adc, alu_sbc,
440
                       alu_and, alu_ora, alu_eor,
441
                       alu_tst, alu_inc, alu_dec, alu_clr, alu_neg, alu_com,
442
                                                     alu_lsr16, alu_lsl16,
443
                                                     alu_ror8, alu_rol8, alu_mul,
444
                                                     alu_asr8, alu_asl8, alu_lsr8,
445
                                                     alu_andcc, alu_orcc, alu_sex, alu_tfr, alu_abx,
446
                                                          alu_seif, alu_sei, alu_see, alu_cle,
447
                                                     alu_ld8, alu_st8, alu_ld16, alu_st16, alu_lea, alu_nop, alu_daa );
448
 
449
        signal op_code:     std_logic_vector(7 downto 0);
450
        signal pre_code:    std_logic_vector(7 downto 0);
451
        signal acca:        std_logic_vector(7 downto 0);
452
        signal accb:        std_logic_vector(7 downto 0);
453
   signal cc:          std_logic_vector(7 downto 0);
454
        signal cc_out:      std_logic_vector(7 downto 0);
455
        signal dp:          std_logic_vector(7 downto 0);
456
        signal xreg:        std_logic_vector(15 downto 0);
457
        signal yreg:        std_logic_vector(15 downto 0);
458
        signal sp:          std_logic_vector(15 downto 0);
459
        signal up:          std_logic_vector(15 downto 0);
460
        signal ea:          std_logic_vector(15 downto 0);
461
        signal pc:              std_logic_vector(15 downto 0);
462
        signal md:          std_logic_vector(15 downto 0);
463
   signal left:        std_logic_vector(15 downto 0);
464
   signal right:       std_logic_vector(15 downto 0);
465
        signal out_alu:     std_logic_vector(15 downto 0);
466
        signal iv:          std_logic_vector(2 downto 0);
467
        signal nmi_req:     std_logic;
468
        signal nmi_ack:     std_logic;
469
        signal nmi_enable:  std_logic;
470
   signal fic:         std_logic; -- first instruction cycle
471
   signal lic:         std_logic; -- last instruction cycle
472
 
473
        signal state:        state_type;
474
        signal next_state:   state_type;
475
        signal return_state: state_type;
476
        signal saved_state:  state_type;
477
        signal st_ctrl:      st_type;
478
        signal iv_ctrl:      iv_type;
479
   signal pc_ctrl:      pc_type;
480
   signal ea_ctrl:      ea_type;
481
   signal op_ctrl:      op_type;
482
        signal pre_ctrl:     pre_type;
483
        signal md_ctrl:      md_type;
484
        signal acca_ctrl:    acca_type;
485
        signal accb_ctrl:    accb_type;
486
        signal ix_ctrl:      ix_type;
487
        signal iy_ctrl:      iy_type;
488
        signal cc_ctrl:      cc_type;
489
        signal dp_ctrl:      dp_type;
490
        signal sp_ctrl:      sp_type;
491
        signal up_ctrl:      up_type;
492
        signal left_ctrl:    left_type;
493
        signal right_ctrl:   right_type;
494
   signal alu_ctrl:     alu_type;
495
   signal addr_ctrl:    addr_type;
496
   signal dout_ctrl:    dout_type;
497
 
498
 
499
begin
500
 
501
----------------------------------
502
--
503
-- State machine stack
504
--
505
----------------------------------
506
--state_stack_proc: process( clk, hold, state_stack, st_ctrl, 
507
--                           return_state, fetch_state  )
508
state_stack_proc: process( clk, st_ctrl, return_state )
509
begin
510
  if clk'event and clk = '0' then
511
    if hold = '0' then
512
           case st_ctrl is
513
      when reset_st =>
514
        saved_state <= fetch_state;
515
      when push_st =>
516
                  saved_state <= return_state;
517
                when others =>
518
        null;
519
           end case;
520
    end if;
521
  end if;
522
end process;
523
 
524
----------------------------------
525
--
526
-- Interrupt Vector control
527
--
528
----------------------------------
529
--
530
int_vec_proc: process( clk, iv_ctrl )
531
begin
532
  if clk'event and clk = '0' then
533
    if hold = '0' then
534
      case iv_ctrl is
535
      when reset_iv =>
536
        iv <= RST_VEC;
537
      when nmi_iv =>
538
        iv <= NMI_VEC;
539
      when swi_iv =>
540
        iv <= SWI_VEC;
541
      when irq_iv =>
542
        iv <= IRQ_VEC;
543
      when firq_iv =>
544
        iv <= FIRQ_VEC;
545
      when swi2_iv =>
546
        iv <= SWI2_VEC;
547
      when swi3_iv =>
548
        iv <= SWI3_VEC;
549
                when others =>
550
                  null;
551
      end case;
552
    end if; -- hold
553
  end if; -- clk
554
end process;
555
 
556
----------------------------------
557
--
558
-- Program Counter Control
559
--
560
----------------------------------
561
 
562
--pc_reg: process( clk, pc_ctrl, hold, pc, out_alu, data_in )
563
pc_reg: process( clk )
564
begin
565
  if clk'event and clk = '0' then
566
    if hold = '0' then
567
    case pc_ctrl is
568
         when reset_pc =>
569
           pc <= (others=>'0');
570
         when load_pc =>
571
           pc <= out_alu(15 downto 0);
572
         when pull_lo_pc =>
573
           pc(7 downto 0) <= data_in;
574
         when pull_hi_pc =>
575
           pc(15 downto 8) <= data_in;
576
         when incr_pc =>
577
           pc <= pc + 1;
578
         when others =>
579
      null;
580
    end case;
581
         end if;
582
  end if;
583
end process;
584
 
585
----------------------------------
586
--
587
-- Effective Address  Control
588
--
589
----------------------------------
590
 
591
--ea_reg: process( clk, ea_ctrl, hold, ea, out_alu, data_in, dp )
592
ea_reg: process( clk )
593
begin
594
 
595
  if clk'event and clk = '0' then
596
    if hold= '0' then
597
    case ea_ctrl is
598
         when reset_ea =>
599
           ea <= (others=>'0');
600
         when fetch_first_ea =>
601
           ea(7 downto 0) <= data_in;
602
      ea(15 downto 8) <= dp;
603
         when fetch_next_ea =>
604
           ea(15 downto 8) <= ea(7 downto 0);
605
      ea(7 downto 0)  <= data_in;
606
         when load_ea =>
607
           ea <= out_alu(15 downto 0);
608
         when others =>
609
      null;
610
    end case;
611
         end if;
612
  end if;
613
end process;
614
 
615
--------------------------------
616
--
617
-- Accumulator A
618
--
619
--------------------------------
620
--acca_reg : process( clk, acca_ctrl, hold, out_alu, acca, data_in )
621
acca_reg : process( clk )
622
begin
623
  if clk'event and clk = '0' then
624
    if hold = '0' then
625
    case acca_ctrl is
626
    when reset_acca =>
627
           acca <= (others=>'0');
628
         when load_acca =>
629
           acca <= out_alu(7 downto 0);
630
         when load_hi_acca =>
631
           acca <= out_alu(15 downto 8);
632
         when pull_acca =>
633
           acca <= data_in;
634
         when others =>
635
      null;
636
    end case;
637
         end if;
638
  end if;
639
end process;
640
 
641
--------------------------------
642
--
643
-- Accumulator B
644
--
645
--------------------------------
646
--accb_reg : process( clk, accb_ctrl, hold, out_alu, accb, data_in )
647
accb_reg : process( clk )
648
begin
649
  if clk'event and clk = '0' then
650
    if hold = '0' then
651
    case accb_ctrl is
652
    when reset_accb =>
653
           accb <= (others=>'0');
654
         when load_accb =>
655
           accb <= out_alu(7 downto 0);
656
         when pull_accb =>
657
           accb <= data_in;
658
         when others =>
659
      null;
660
    end case;
661
         end if;
662
  end if;
663
end process;
664
 
665
--------------------------------
666
--
667
-- X Index register
668
--
669
--------------------------------
670
--ix_reg : process( clk, ix_ctrl, hold, out_alu, xreg, data_in )
671
ix_reg : process( clk )
672
begin
673
  if clk'event and clk = '0' then
674
    if hold = '0' then
675
    case ix_ctrl is
676
    when reset_ix =>
677
           xreg <= (others=>'0');
678
         when load_ix =>
679
           xreg <= out_alu(15 downto 0);
680
         when pull_hi_ix =>
681
           xreg(15 downto 8) <= data_in;
682
         when pull_lo_ix =>
683
           xreg(7 downto 0) <= data_in;
684
         when others =>
685
      null;
686
    end case;
687
         end if;
688
  end if;
689
end process;
690
 
691
--------------------------------
692
--
693
-- Y Index register
694
--
695
--------------------------------
696
--iy_reg : process( clk, iy_ctrl, hold, out_alu, yreg, data_in )
697
iy_reg : process( clk )
698
begin
699
  if clk'event and clk = '0' then
700
    if hold = '0' then
701
    case iy_ctrl is
702
    when reset_iy =>
703
           yreg <= (others=>'0');
704
         when load_iy =>
705
           yreg <= out_alu(15 downto 0);
706
         when pull_hi_iy =>
707
           yreg(15 downto 8) <= data_in;
708
         when pull_lo_iy =>
709
           yreg(7 downto 0) <= data_in;
710
         when others =>
711
      null;
712
    end case;
713
         end if;
714
  end if;
715
end process;
716
 
717
--------------------------------
718
--
719
-- S stack pointer
720
--
721
--------------------------------
722
--sp_reg : process( clk, sp_ctrl, hold, sp, out_alu, data_in, nmi_enable )
723
sp_reg : process( clk )
724
begin
725
  if clk'event and clk = '0' then
726
    if hold = '0' then
727
    case sp_ctrl is
728
    when reset_sp =>
729
           sp <= (others=>'0');
730
                nmi_enable <= '0';
731
         when load_sp =>
732
           sp <= out_alu(15 downto 0);
733
                nmi_enable <= '1';
734
         when pull_hi_sp =>
735
           sp(15 downto 8) <= data_in;
736
         when pull_lo_sp =>
737
           sp(7 downto 0) <= data_in;
738
                nmi_enable <= '1';
739
         when others =>
740
      null;
741
    end case;
742
         end if;
743
  end if;
744
end process;
745
 
746
--------------------------------
747
--
748
-- U stack pointer
749
--
750
--------------------------------
751
--up_reg : process( clk, up_ctrl, hold, up, out_alu, data_in )
752
up_reg : process( clk )
753
begin
754
  if clk'event and clk = '0' then
755
    if hold = '0' then
756
    case up_ctrl is
757
    when reset_up =>
758
           up <= (others=>'0');
759
         when load_up =>
760
           up <= out_alu(15 downto 0);
761
         when pull_hi_up =>
762
           up(15 downto 8) <= data_in;
763
         when pull_lo_up =>
764
           up(7 downto 0) <= data_in;
765
         when others =>
766
      null;
767
    end case;
768
         end if;
769
  end if;
770
end process;
771
 
772
--------------------------------
773
--
774
-- Memory Data
775
--
776
--------------------------------
777
--md_reg : process( clk, md_ctrl, hold, out_alu, data_in, md )
778
md_reg : process( clk )
779
begin
780
  if clk'event and clk = '0' then
781
    if hold = '0' then
782
    case md_ctrl is
783
    when reset_md =>
784
           md <= (others=>'0');
785
         when load_md =>
786
           md <= out_alu(15 downto 0);
787
         when fetch_first_md => -- sign extend md for branches
788
           md(15 downto 8) <= data_in(7) & data_in(7) & data_in(7) & data_in(7) &
789
                              data_in(7) & data_in(7) & data_in(7) & data_in(7) ;
790
           md(7 downto 0) <= data_in;
791
         when fetch_next_md =>
792
           md(15 downto 8) <= md(7 downto 0);
793
                md(7 downto 0) <= data_in;
794
         when shiftl_md =>
795
           md(15 downto 1) <= md(14 downto 0);
796
                md(0) <= '0';
797
         when others =>
798
      null;
799
    end case;
800
         end if;
801
  end if;
802
end process;
803
 
804
 
805
----------------------------------
806
--
807
-- Condition Codes
808
--
809
----------------------------------
810
 
811
--cc_reg: process( clk, cc_ctrl, hold, cc_out, cc, data_in )
812
cc_reg: process( clk )
813
begin
814
  if clk'event and clk = '0' then
815
    if hold = '0' then
816
    case cc_ctrl is
817
         when reset_cc =>
818
           cc <= "11010000"; -- set EBIT, FBIT & IBIT
819
         when load_cc =>
820
           cc <= cc_out;
821
         when pull_cc =>
822
      cc <= data_in;
823
         when others =>
824
      null;
825
    end case;
826
         end if;
827
  end if;
828
end process;
829
 
830
----------------------------------
831
--
832
-- Direct Page register
833
--
834
----------------------------------
835
 
836
--dp_reg: process( clk, dp_ctrl, hold, out_alu, dp, data_in )
837
dp_reg: process( clk )
838
begin
839
  if clk'event and clk = '0' then
840
    if hold = '0' then
841
    case dp_ctrl is
842
         when reset_dp =>
843
           dp <= (others=>'0');
844
         when load_dp =>
845
           dp <= out_alu(7 downto 0);
846
         when pull_dp =>
847
      dp <= data_in;
848
         when others =>
849
      null;
850
    end case;
851
         end if;
852
  end if;
853
end process;
854
 
855
 
856
----------------------------------
857
--
858
-- op code register
859
--
860
----------------------------------
861
 
862
--op_reg: process( clk, op_ctrl, hold, op_code, data_in )
863
op_reg: process( clk )
864
begin
865
  if clk'event and clk = '0' then
866
    if hold = '0' then
867
    case op_ctrl is
868
         when reset_op =>
869
           op_code <= "00010010";
870
         when fetch_op =>
871
      op_code <= data_in;
872
         when others =>
873
      null;
874
    end case;
875
         end if;
876
  end if;
877
end process;
878
 
879
 
880
----------------------------------
881
--
882
-- pre byte op code register
883
--
884
----------------------------------
885
 
886
--pre_reg: process( clk, pre_ctrl, hold, pre_code, data_in )
887
pre_reg: process( clk )
888
begin
889
  if clk'event and clk = '0' then
890
    if hold = '0' then
891
    case pre_ctrl is
892
         when reset_pre =>
893
           pre_code <= (others=>'0');
894
         when fetch_pre =>
895
      pre_code <= data_in;
896
         when others =>
897
      null;
898
    end case;
899
         end if;
900
  end if;
901
end process;
902
 
903
--------------------------------
904
--
905
-- state machine
906
--
907
--------------------------------
908
 
909
--change_state: process( clk, rst, state, hold, next_state )
910
change_state: process( clk )
911
begin
912
  if clk'event and clk = '0' then
913
    if rst = '1' then
914
      fic     <= '0';
915
           nmi_ack <= '0';
916
           state   <= reset_state;
917
    elsif hold = '0' then
918
                  fic <= lic;
919
                  --
920
                  -- nmi request is not cleared until nmi input goes low
921
                  --
922
                  if (nmi_req = '0') and (nmi_ack='1') then
923
          nmi_ack <= '0';
924
                  end if;
925
 
926
                  if (nmi_req = '1') and (nmi_ack = '0')  and (state = int_nmimask_state) then
927
          nmi_ack <= '1';
928
                  end if;
929
 
930
        if lic = '1'  then
931
          if halt = '1' then
932
                           state <= halt_state;
933
 
934
          -- service non maskable interrupts
935
          elsif (nmi_req = '1') and (nmi_ack = '0') then
936
                           state <= int_nmi_state;
937
                                 --
938
                                 -- FIRQ & IRQ are level sensitive
939
                                 --
940
          elsif (firq = '1') and (cc(FBIT) = '0') then
941
                           state  <= int_firq_state;
942
 
943
                         elsif (irq = '1') and (cc(IBIT) = '0') then
944
                           state <= int_irq_state;
945
                         --
946
                         -- Version 1.27 2015-05-30
947
                         -- Exit sync_state on masked interrupt.
948
                         --
949
                         -- Version 1.28 2015-05-30
950
                         -- Move this code to the state sequencer
951
                         -- near line 5566.
952
                         --
953
                         -- elsif  (state = sync_state) and ((firq = '1') or (irq = '1'))then
954
                         --   state <= fetch_state;
955
          --
956
                         else
957
                           state <= next_state;
958
          end if; -- halt, nmi, firq, irq
959
                  else
960
                    state <= next_state;
961
        end if; -- lic
962
         end if; -- reset/hold
963
  end if; -- clk
964
end process;
965
 
966
------------------------------------
967
--
968
-- Detect Edge of NMI interrupt
969
--
970
------------------------------------
971
 
972
--nmi_handler : process( clk, rst, nmi, nmi_ack, nmi_req, nmi_enable )
973
nmi_handler : process( rst, clk )
974
begin
975
  if rst='1' then
976
         nmi_req <= '0';
977
  elsif clk'event and clk='0' then
978
           if (nmi='1') and (nmi_ack='0') and (nmi_enable='1') then
979
             nmi_req <= '1';
980
           else
981
                  if (nmi='0') and (nmi_ack='1') then
982
               nmi_req <= '0';
983
                  end if;
984
                end if;
985
  end if;
986
end process;
987
 
988
 
989
----------------------------------
990
--
991
-- Address output multiplexer
992
--
993
----------------------------------
994
 
995
addr_mux: process( addr_ctrl, pc, ea, up, sp, iv )
996
begin
997
  ifetch <= '0';
998
  vma    <= '1';
999
  case addr_ctrl is
1000
    when fetch_ad =>
1001
           addr   <= pc;
1002
      rw     <= '1';
1003
      ifetch <= '1';
1004
         when read_ad =>
1005
           addr   <= ea;
1006
      rw     <= '1';
1007
    when write_ad =>
1008
           addr   <= ea;
1009
                rw     <= '0';
1010
         when pushs_ad =>
1011
           addr   <= sp;
1012
                rw     <= '0';
1013
    when pulls_ad =>
1014
           addr   <= sp;
1015
      rw     <= '1';
1016
         when pushu_ad =>
1017
           addr   <= up;
1018
                rw     <= '0';
1019
    when pullu_ad =>
1020
           addr   <= up;
1021
      rw     <= '1';
1022
         when int_hi_ad =>
1023
           addr   <= "111111111111" & iv & "0";
1024
      rw     <= '1';
1025
    when int_lo_ad =>
1026
           addr   <= "111111111111" & iv & "1";
1027
      rw     <= '1';
1028
         when others =>
1029
      addr   <= "1111111111111111";
1030
      rw     <= '1';
1031
      vma    <= '0';
1032
  end case;
1033
end process;
1034
 
1035
--------------------------------
1036
--
1037
-- Data Bus output
1038
--
1039
--------------------------------
1040
dout_mux : process( dout_ctrl, md, acca, accb, dp, xreg, yreg, sp, up, pc, cc )
1041
begin
1042
    case dout_ctrl is
1043
         when cc_dout => -- condition code register
1044
           data_out <= cc;
1045
         when acca_dout => -- accumulator a
1046
           data_out <= acca;
1047
         when accb_dout => -- accumulator b
1048
           data_out <= accb;
1049
         when dp_dout => -- direct page register
1050
           data_out <= dp;
1051
         when ix_lo_dout => -- X index reg
1052
           data_out <= xreg(7 downto 0);
1053
         when ix_hi_dout => -- X index reg
1054
           data_out <= xreg(15 downto 8);
1055
         when iy_lo_dout => -- Y index reg
1056
           data_out <= yreg(7 downto 0);
1057
         when iy_hi_dout => -- Y index reg
1058
           data_out <= yreg(15 downto 8);
1059
         when up_lo_dout => -- U stack pointer
1060
           data_out <= up(7 downto 0);
1061
         when up_hi_dout => -- U stack pointer
1062
           data_out <= up(15 downto 8);
1063
         when sp_lo_dout => -- S stack pointer
1064
           data_out <= sp(7 downto 0);
1065
         when sp_hi_dout => -- S stack pointer
1066
           data_out <= sp(15 downto 8);
1067
         when md_lo_dout => -- alu output
1068
           data_out <= md(7 downto 0);
1069
         when md_hi_dout => -- alu output
1070
           data_out <= md(15 downto 8);
1071
         when pc_lo_dout => -- low order pc
1072
           data_out <= pc(7 downto 0);
1073
         when pc_hi_dout => -- high order pc
1074
           data_out <= pc(15 downto 8);
1075
    end case;
1076
end process;
1077
 
1078
----------------------------------
1079
--
1080
-- Left Mux
1081
--
1082
----------------------------------
1083
 
1084
left_mux: process( left_ctrl, acca, accb, cc, dp, xreg, yreg, up, sp, pc, ea, md )
1085
begin
1086
  case left_ctrl is
1087
         when cc_left =>
1088
           left(15 downto 8) <= "00000000";
1089
                left(7 downto 0)  <= cc;
1090
         when acca_left =>
1091
           left(15 downto 8) <= "00000000";
1092
                left(7 downto 0)  <= acca;
1093
         when accb_left =>
1094
           left(15 downto 8) <= "00000000";
1095
                left(7 downto 0)  <= accb;
1096
         when dp_left =>
1097
           left(15 downto 8) <= "00000000";
1098
                left(7 downto 0)  <= dp;
1099
         when accd_left =>
1100
           left(15 downto 8) <= acca;
1101
                left(7 downto 0)  <= accb;
1102
         when md_left =>
1103
           left <= md;
1104
         when ix_left =>
1105
           left <= xreg;
1106
         when iy_left =>
1107
           left <= yreg;
1108
         when sp_left =>
1109
           left <= sp;
1110
         when up_left =>
1111
           left <= up;
1112
         when pc_left =>
1113
           left <= pc;
1114
         when others =>
1115
--       when ea_left =>
1116
           left <= ea;
1117
    end case;
1118
end process;
1119
 
1120
----------------------------------
1121
--
1122
-- Right Mux
1123
--
1124
----------------------------------
1125
 
1126
right_mux: process( right_ctrl, md, acca, accb, ea )
1127
begin
1128
  case right_ctrl is
1129
         when ea_right =>
1130
           right <= ea;
1131
         when zero_right =>
1132
           right <= "0000000000000000";
1133
         when one_right =>
1134
           right <= "0000000000000001";
1135
         when two_right =>
1136
           right <= "0000000000000010";
1137
         when acca_right =>
1138
           if acca(7) = '0' then
1139
             right <= "00000000" & acca(7 downto 0);
1140
                else
1141
                  right <= "11111111" & acca(7 downto 0);
1142
                end if;
1143
         when accb_right =>
1144
           if accb(7) = '0' then
1145
             right <= "00000000" & accb(7 downto 0);
1146
                else
1147
                  right <= "11111111" & accb(7 downto 0);
1148
                end if;
1149
         when accd_right =>
1150
           right <= acca & accb;
1151
         when md_sign5_right =>
1152
           if md(4) = '0' then
1153
             right <= "00000000000" & md(4 downto 0);
1154
                else
1155
                  right <= "11111111111" & md(4 downto 0);
1156
                end if;
1157
         when md_sign8_right =>
1158
           if md(7) = '0' then
1159
             right <= "00000000" & md(7 downto 0);
1160
                else
1161
                  right <= "11111111" & md(7 downto 0);
1162
                end if;
1163
         when others =>
1164
--       when md_right =>
1165
           right <= md;
1166
    end case;
1167
end process;
1168
 
1169
----------------------------------
1170
--
1171
-- Arithmetic Logic Unit
1172
--
1173
----------------------------------
1174
 
1175
alu: process( alu_ctrl, cc, left, right, out_alu, cc_out )
1176
variable valid_lo, valid_hi : boolean;
1177
variable carry_in : std_logic;
1178
variable daa_reg : std_logic_vector(7 downto 0);
1179
begin
1180
 
1181
  case alu_ctrl is
1182
         when alu_adc | alu_sbc |
1183
              alu_rol8 | alu_ror8 =>
1184
           carry_in := cc(CBIT);
1185
    when alu_asr8 =>
1186
           carry_in := left(7);
1187
         when others =>
1188
           carry_in := '0';
1189
  end case;
1190
 
1191
  valid_lo := left(3 downto 0) <= 9;
1192
  valid_hi := left(7 downto 4) <= 9;
1193
 
1194
  --
1195
  -- CBIT HBIT VHI VLO DAA
1196
  --    0    0   0   0 66 (!VHI : hi_nybble>8)
1197
  --    0    0   0   1 60
1198
  --    0    0   1   1 00
1199
  --    0    0   1   0 06 ( VHI : hi_nybble<=8)
1200
  --
1201
  --    0    1   1   0 06
1202
  --    0    1   1   1 06
1203
  --    0    1   0   1 66
1204
  --    0    1   0   0 66
1205
  --
1206
  --    1    1   0   0 66
1207
  --    1    1   0   1 66
1208
  --    1    1   1   1 66
1209
  --    1    1   1   0 66
1210
  --
1211
  --    1    0   1   0 66
1212
  --    1    0   1   1 60
1213
  --    1    0   0   1 60
1214
  --    1    0   0   0 66
1215
  --
1216
  -- 66 = (!VHI & !VLO) + (CBIT & HBIT) + (HBIT & !VHI) + (CBIT & !VLO) 
1217
  --    = (CBIT & (HBIT + !VLO)) + (!VHI & (HBIT + !VLO))
1218
  --    = (!VLO & (CBIT + !VHI)) + (HBIT & (CBIT + !VHI))
1219
  -- 60 = (CBIT & !HBIT & VLO) + (!HBIT & !VHI & VLO) 
1220
  --    = (!HBIT & VLO & (CBIT + !VHI))
1221
  -- 06 = (!CBIT & VHI & (!VLO + VHI)
1222
  -- 00 = (!CBIT & !HBIT & VHI & VLO)
1223
  --
1224
  if (cc(CBIT) = '0') then
1225
    -- CBIT=0
1226
    if( cc(HBIT) = '0' ) then
1227
           -- HBIT=0
1228
                if valid_lo then
1229
                  -- lo <= 9 (no overflow in low nybble)
1230
                  if valid_hi then
1231
                    -- hi <= 9 (no overflow in either low or high nybble)
1232
                    daa_reg := "00000000";
1233
                  else
1234
                    -- hi > 9 (overflow in high nybble only)
1235
                    daa_reg := "01100000";
1236
                  end if;
1237
                else
1238
                  -- lo > 9 (overflow in low nybble)
1239
                  --
1240
                  -- since there is already an overflow in the low nybble
1241
                  -- you need to make room in the high nybble for the low nybble carry
1242
                  -- so compare the high nybble with 8 rather than 9
1243
                  -- if the high nybble is 9 there will be an overflow on the high nybble
1244
                  -- after the decimal adjust which means it will roll over to an invalid BCD digit
1245
                  --
1246
             if( left(7 downto 4) <= 8 ) then
1247
                    -- hi <= 8 (overflow in low nybble only)
1248
                    daa_reg := "00000110";
1249
                  else
1250
                    -- hi > 8 (overflow in low and high nybble)
1251
                         daa_reg := "01100110";
1252
                  end if;
1253
                end if;
1254
    else
1255
           -- HBIT=1 (overflow in low nybble)
1256
                if valid_hi then
1257
                  -- hi <= 9 (overflow in low nybble only)
1258
                  daa_reg := "00000110";
1259
                else
1260
                  -- hi > 9 (overflow in low and high nybble)
1261
                  daa_reg := "01100110";
1262
           end if;
1263
         end if;
1264
  else
1265
    -- CBIT=1 (carry => overflow in high nybble)
1266
    if ( cc(HBIT) = '0' )then
1267
           -- HBIT=0 (half carry clear => may or may not be an overflow in the low nybble)
1268
                if valid_lo then
1269
                  -- lo <=9  (overflow in high nybble only)
1270
                  daa_reg := "01100000";
1271
           else
1272
                  -- lo >9  (overflow in low and high nybble)
1273
                  daa_reg := "01100110";
1274
                end if;
1275
         else
1276
           -- HBIT=1 (overflow in low and high nybble)
1277
                daa_reg := "01100110";
1278
         end if;
1279
  end if;
1280
 
1281
  case alu_ctrl is
1282
         when alu_add8 | alu_inc |
1283
              alu_add16 | alu_adc | alu_mul =>
1284
                out_alu <= left + right + ("000000000000000" & carry_in);
1285
         when alu_sub8 | alu_dec |
1286
              alu_sub16 | alu_sbc =>
1287
           out_alu <= left - right - ("000000000000000" & carry_in);
1288
    when alu_abx =>
1289
           out_alu <= left + ("00000000" & right(7 downto 0)) ;
1290
         when alu_and =>
1291
           out_alu   <= left and right;         -- and/bit
1292
         when alu_ora =>
1293
           out_alu   <= left or right;  -- or
1294
         when alu_eor =>
1295
           out_alu   <= left xor right;         -- eor/xor
1296
         when alu_lsl16 | alu_asl8 | alu_rol8 =>
1297
           out_alu   <= left(14 downto 0) & carry_in;    -- rol8/asl8/lsl16
1298
         when alu_lsr16 =>
1299
           out_alu   <= carry_in & left(15 downto 1);   -- lsr16
1300
         when alu_lsr8 | alu_asr8 | alu_ror8 =>
1301
           out_alu   <= "00000000" & carry_in & left(7 downto 1);       -- ror8/asr8/lsr8
1302
         when alu_neg =>
1303
           out_alu   <= right - left;   -- neg (right=0)
1304
         when alu_com =>
1305
           out_alu   <= not left;
1306
         when alu_clr | alu_ld8 | alu_ld16 | alu_lea =>
1307
           out_alu   <= right;           -- clr, ld
1308
         when alu_st8 | alu_st16 | alu_andcc | alu_orcc | alu_tfr =>
1309
           out_alu   <= left;
1310
         when alu_daa =>
1311
           out_alu   <= left + ("00000000" & daa_reg);
1312
         when alu_sex =>
1313
           if left(7) = '0' then
1314
              out_alu <= "00000000" & left(7 downto 0);
1315
                else
1316
                   out_alu <= "11111111" & left(7 downto 0);
1317
                end if;
1318
         when others =>
1319
           out_alu   <= left; -- nop
1320
    end case;
1321
 
1322
         --
1323
         -- carry bit
1324
         --
1325
    case alu_ctrl is
1326
         when alu_add8 | alu_adc  =>
1327
      cc_out(CBIT) <= (left(7) and right(7)) or
1328
                                (left(7) and not out_alu(7)) or
1329
                                                   (right(7) and not out_alu(7));
1330
         when alu_sub8 | alu_sbc =>
1331
      cc_out(CBIT) <= ((not left(7)) and right(7)) or
1332
                                ((not left(7)) and out_alu(7)) or
1333
                                                         (right(7) and out_alu(7));
1334
         when alu_add16  =>
1335
      cc_out(CBIT) <= (left(15) and right(15)) or
1336
                                (left(15) and not out_alu(15)) or
1337
                                                   (right(15) and not out_alu(15));
1338
         when alu_sub16 =>
1339
      cc_out(CBIT) <= ((not left(15)) and right(15)) or
1340
                                ((not left(15)) and out_alu(15)) or
1341
                                                         (right(15) and out_alu(15));
1342
         when alu_ror8 | alu_lsr16 | alu_lsr8 | alu_asr8 =>
1343
           cc_out(CBIT) <= left(0);
1344
         when alu_rol8 | alu_asl8 =>
1345
           cc_out(CBIT) <= left(7);
1346
         when alu_lsl16 =>
1347
           cc_out(CBIT) <= left(15);
1348
         when alu_com =>
1349
           cc_out(CBIT) <= '1';
1350
         when alu_neg | alu_clr =>
1351
           cc_out(CBIT) <= out_alu(7) or out_alu(6) or out_alu(5) or out_alu(4) or
1352
                                out_alu(3) or out_alu(2) or out_alu(1) or out_alu(0);
1353
    when alu_mul =>
1354
                cc_out(CBIT) <= out_alu(7);
1355
    when alu_daa =>
1356
           if ( daa_reg(7 downto 4) = "0110" ) then
1357
                  cc_out(CBIT) <= '1';
1358
                else
1359
                  cc_out(CBIT) <= '0';
1360
           end if;
1361
         when alu_andcc =>
1362
      cc_out(CBIT) <= left(CBIT) and cc(CBIT);
1363
         when alu_orcc =>
1364
      cc_out(CBIT) <= left(CBIT) or cc(CBIT);
1365
         when alu_tfr =>
1366
      cc_out(CBIT) <= left(CBIT);
1367
         when others =>
1368
      cc_out(CBIT) <= cc(CBIT);
1369
    end case;
1370
         --
1371
         -- Zero flag
1372
         --
1373
    case alu_ctrl is
1374
         when alu_add8 | alu_sub8 |
1375
              alu_adc | alu_sbc |
1376
              alu_and | alu_ora | alu_eor |
1377
              alu_inc | alu_dec |
1378
                        alu_neg | alu_com | alu_clr |
1379
                        alu_rol8 | alu_ror8 | alu_asr8 | alu_asl8 | alu_lsr8 |
1380
                   alu_ld8  | alu_st8 | alu_sex | alu_daa =>
1381
      cc_out(ZBIT) <= not( out_alu(7)  or out_alu(6)  or out_alu(5)  or out_alu(4)  or
1382
                                out_alu(3)  or out_alu(2)  or out_alu(1)  or out_alu(0) );
1383
         when alu_add16 | alu_sub16 | alu_mul |
1384
              alu_lsl16 | alu_lsr16 |
1385
                   alu_ld16  | alu_st16 | alu_lea =>
1386
      cc_out(ZBIT) <= not( out_alu(15) or out_alu(14) or out_alu(13) or out_alu(12) or
1387
                                out_alu(11) or out_alu(10) or out_alu(9)  or out_alu(8)  or
1388
                                out_alu(7)  or out_alu(6)  or out_alu(5)  or out_alu(4)  or
1389
                                out_alu(3)  or out_alu(2)  or out_alu(1)  or out_alu(0) );
1390
         when alu_andcc =>
1391
      cc_out(ZBIT) <= left(ZBIT) and cc(ZBIT);
1392
         when alu_orcc =>
1393
      cc_out(ZBIT) <= left(ZBIT) or cc(ZBIT);
1394
         when alu_tfr =>
1395
      cc_out(ZBIT) <= left(ZBIT);
1396
         when others =>
1397
      cc_out(ZBIT) <= cc(ZBIT);
1398
    end case;
1399
 
1400
    --
1401
         -- negative flag
1402
         --
1403
    case alu_ctrl is
1404
         when alu_add8 | alu_sub8 |
1405
              alu_adc | alu_sbc |
1406
              alu_and | alu_ora | alu_eor |
1407
              alu_rol8 | alu_ror8 | alu_asr8 | alu_asl8 | alu_lsr8 |
1408
              alu_inc | alu_dec | alu_neg | alu_com | alu_clr |
1409
                        alu_ld8  | alu_st8 | alu_sex | alu_daa =>
1410
      cc_out(NBIT) <= out_alu(7);
1411
         when alu_add16 | alu_sub16 |
1412
              alu_lsl16 | alu_lsr16 |
1413
                        alu_ld16 | alu_st16 =>
1414
                cc_out(NBIT) <= out_alu(15);
1415
         when alu_andcc =>
1416
      cc_out(NBIT) <= left(NBIT) and cc(NBIT);
1417
         when alu_orcc =>
1418
      cc_out(NBIT) <= left(NBIT) or cc(NBIT);
1419
         when alu_tfr =>
1420
      cc_out(NBIT) <= left(NBIT);
1421
         when others =>
1422
      cc_out(NBIT) <= cc(NBIT);
1423
    end case;
1424
 
1425
    --
1426
         -- Interrupt mask flag
1427
    --
1428
    case alu_ctrl is
1429
         when alu_andcc =>
1430
      cc_out(IBIT) <= left(IBIT) and cc(IBIT);
1431
         when alu_orcc =>
1432
      cc_out(IBIT) <= left(IBIT) or cc(IBIT);
1433
         when alu_tfr =>
1434
      cc_out(IBIT) <= left(IBIT);
1435
    when alu_seif | alu_sei =>
1436
           cc_out(IBIT) <= '1';
1437
         when others =>
1438
                cc_out(IBIT) <= cc(IBIT);             -- interrupt mask
1439
    end case;
1440
 
1441
    --
1442
    -- Half Carry flag
1443
         --
1444
    case alu_ctrl is
1445
         when alu_add8 | alu_adc =>
1446
      cc_out(HBIT) <= (left(3) and right(3)) or
1447
                     (right(3) and not out_alu(3)) or
1448
                      (left(3) and not out_alu(3));
1449
         when alu_andcc =>
1450
      cc_out(HBIT) <= left(HBIT) and cc(HBIT);
1451
         when alu_orcc =>
1452
      cc_out(HBIT) <= left(HBIT) or cc(HBIT);
1453
         when alu_tfr =>
1454
      cc_out(HBIT) <= left(HBIT);
1455
         when others =>
1456
                cc_out(HBIT) <= cc(HBIT);
1457
    end case;
1458
 
1459
    --
1460
    -- Overflow flag
1461
         --
1462
    case alu_ctrl is
1463
         when alu_add8 | alu_adc =>
1464
      cc_out(VBIT) <= (left(7)  and      right(7)  and (not out_alu(7))) or
1465
                 ((not left(7)) and (not right(7)) and      out_alu(7));
1466
         when alu_sub8 | alu_sbc =>
1467
      cc_out(VBIT) <= (left(7)  and (not right(7)) and (not out_alu(7))) or
1468
                 ((not left(7)) and      right(7)  and      out_alu(7));
1469
         when alu_add16 =>
1470
      cc_out(VBIT) <= (left(15)  and      right(15)  and (not out_alu(15))) or
1471
                 ((not left(15)) and (not right(15)) and      out_alu(15));
1472
         when alu_sub16 =>
1473
      cc_out(VBIT) <= (left(15)  and (not right(15)) and (not out_alu(15))) or
1474
                 ((not left(15)) and      right(15) and       out_alu(15));
1475
         when alu_inc =>
1476
           cc_out(VBIT) <= ((not left(7)) and left(6) and left(5) and left(4) and
1477
                                      left(3)  and left(2) and left(1) and left(0));
1478
         when alu_dec | alu_neg =>
1479
           cc_out(VBIT) <= (left(7)  and (not left(6)) and (not left(5)) and (not left(4)) and
1480
                            (not left(3)) and (not left(2)) and (not left(1)) and (not left(0)));
1481
-- 6809 Programming reference manual says
1482
-- V not affected by ASR, LSR and ROR
1483
-- This is different to the 6800
1484
-- John Kent 6th June 2006
1485
--       when alu_asr8 =>
1486
--         cc_out(VBIT) <= left(0) xor left(7);
1487
--       when alu_lsr8 | alu_lsr16 =>
1488
--         cc_out(VBIT) <= left(0);
1489
--       when alu_ror8 =>
1490
--      cc_out(VBIT) <= left(0) xor cc(CBIT);
1491
    when alu_lsl16 =>
1492
      cc_out(VBIT) <= left(15) xor left(14);
1493
         when alu_rol8 | alu_asl8 =>
1494
      cc_out(VBIT) <= left(7) xor left(6);
1495
--
1496
-- 11th July 2006 - John Kent
1497
-- What DAA does with V is anyones guess
1498
-- It is undefined in the 6809 programming manual
1499
--
1500
         when alu_daa =>
1501
      cc_out(VBIT) <= left(7) xor out_alu(7) xor cc(CBIT);
1502
-- CLR resets V Bit
1503
-- John Kent 6th June 2006
1504
         when alu_and | alu_ora | alu_eor | alu_com | alu_clr |
1505
              alu_st8 | alu_st16 | alu_ld8 | alu_ld16 | alu_sex =>
1506
      cc_out(VBIT) <= '0';
1507
         when alu_andcc =>
1508
      cc_out(VBIT) <= left(VBIT) and cc(VBIT);
1509
         when alu_orcc =>
1510
      cc_out(VBIT) <= left(VBIT) or cc(VBIT);
1511
         when alu_tfr =>
1512
      cc_out(VBIT) <= left(VBIT);
1513
         when others =>
1514
                cc_out(VBIT) <= cc(VBIT);
1515
    end case;
1516
 
1517
         case alu_ctrl is
1518
         when alu_andcc =>
1519
      cc_out(FBIT) <= left(FBIT) and cc(FBIT);
1520
         when alu_orcc =>
1521
      cc_out(FBIT) <= left(FBIT) or cc(FBIT);
1522
         when alu_tfr =>
1523
      cc_out(FBIT) <= left(FBIT);
1524
    when alu_seif =>
1525
           cc_out(FBIT) <= '1';
1526
         when others =>
1527
      cc_out(FBIT) <= cc(FBIT);
1528
         end case;
1529
 
1530
         case alu_ctrl is
1531
         when alu_andcc =>
1532
      cc_out(EBIT) <= left(EBIT) and cc(EBIT);
1533
         when alu_orcc =>
1534
      cc_out(EBIT) <= left(EBIT) or cc(EBIT);
1535
         when alu_tfr =>
1536
      cc_out(EBIT) <= left(EBIT);
1537
    when alu_see =>
1538
           cc_out(EBIT) <= '1';
1539
    when alu_cle =>
1540
           cc_out(EBIT) <= '0';
1541
         when others =>
1542
           cc_out(EBIT) <= cc(EBIT);
1543
         end case;
1544
end process;
1545
 
1546
------------------------------------
1547
--
1548
-- state sequencer
1549
--
1550
------------------------------------
1551
process( state, saved_state,
1552
         op_code, pre_code,
1553
                        cc, ea, md, iv, fic, halt,
1554
         nmi_req, firq, irq, lic )
1555
variable cond_true : boolean;  -- variable used to evaluate coditional branches
1556
begin
1557
  cond_true  := (1=1);
1558
  ba         <= '0';
1559
  bs         <= '0';
1560
  lic        <= '0';
1561
  opfetch    <= '0';
1562
  iv_ctrl    <= latch_iv;
1563
  -- Registers preserved
1564
  cc_ctrl    <= latch_cc;
1565
  acca_ctrl  <= latch_acca;
1566
  accb_ctrl  <= latch_accb;
1567
  dp_ctrl    <= latch_dp;
1568
  ix_ctrl    <= latch_ix;
1569
  iy_ctrl    <= latch_iy;
1570
  up_ctrl    <= latch_up;
1571
  sp_ctrl    <= latch_sp;
1572
  pc_ctrl    <= latch_pc;
1573
  md_ctrl    <= latch_md;
1574
  ea_ctrl    <= latch_ea;
1575
  op_ctrl    <= latch_op;
1576
  pre_ctrl   <= latch_pre;
1577
  -- ALU Idle
1578
  left_ctrl  <= pc_left;
1579
  right_ctrl <= zero_right;
1580
  alu_ctrl   <= alu_nop;
1581
  -- Bus idle
1582
  addr_ctrl  <= idle_ad;
1583
  dout_ctrl  <= cc_dout;
1584
  -- Next State Fetch
1585
  st_ctrl      <= idle_st;
1586
  return_state <= fetch_state;
1587
  next_state   <= fetch_state;
1588
 
1589
  case state is
1590
  when reset_state =>        --  released from reset
1591
    -- reset the registers
1592
    iv_ctrl    <= reset_iv;
1593
    op_ctrl    <= reset_op;
1594
    pre_ctrl   <= reset_pre;
1595
    cc_ctrl    <= reset_cc;
1596
    acca_ctrl  <= reset_acca;
1597
    accb_ctrl  <= reset_accb;
1598
    dp_ctrl    <= reset_dp;
1599
    ix_ctrl    <= reset_ix;
1600
    iy_ctrl    <= reset_iy;
1601
    up_ctrl    <= reset_up;
1602
    sp_ctrl    <= reset_sp;
1603
    pc_ctrl    <= reset_pc;
1604
    ea_ctrl    <= reset_ea;
1605
    md_ctrl    <= reset_md;
1606
    st_ctrl    <= reset_st;
1607
    next_state <= vect_hi_state;
1608
 
1609
  --
1610
  -- Jump via interrupt vector
1611
  -- iv holds interrupt type
1612
  -- fetch PC hi from vector location
1613
  --
1614
  when vect_hi_state =>
1615
    -- fetch pc low interrupt vector
1616
    pc_ctrl    <= pull_hi_pc;
1617
    addr_ctrl  <= int_hi_ad;
1618
    bs         <= '1';
1619
    next_state <= vect_lo_state;
1620
 
1621
  --
1622
  -- jump via interrupt vector
1623
  -- iv holds vector type
1624
  -- fetch PC lo from vector location
1625
  --
1626
  when vect_lo_state =>
1627
    -- fetch the vector low byte
1628
    pc_ctrl    <= pull_lo_pc;
1629
    addr_ctrl  <= int_lo_ad;
1630
    bs         <= '1';
1631
    next_state <= fetch_state;
1632
 
1633
  when vect_idle_state =>
1634
    --
1635
    -- Last Instruction Cycle for SWI, SWI2 & SWI3
1636
    --
1637
    if op_code = "00111111" then
1638
      lic      <= '1';
1639
    end if;
1640
    next_state <= fetch_state;
1641
 
1642
  --
1643
  -- Here to fetch an instruction
1644
  -- PC points to opcode
1645
  --
1646
  when fetch_state =>
1647
    -- fetch the op code
1648
    opfetch    <= '1';
1649
    op_ctrl    <= fetch_op;
1650
    pre_ctrl   <= fetch_pre;
1651
    ea_ctrl    <= reset_ea;
1652
    -- Fetch op code
1653
    addr_ctrl  <= fetch_ad;
1654
    -- Advance the PC to fetch next instruction byte
1655
    pc_ctrl    <= incr_pc;
1656
    next_state <= decode1_state;
1657
 
1658
  --
1659
  -- Here to decode instruction
1660
  -- and fetch next byte of intruction
1661
  -- whether it be necessary or not
1662
  --
1663
  when decode1_state =>
1664
        -- fetch first byte of address or immediate data
1665
    ea_ctrl    <= fetch_first_ea;
1666
    md_ctrl    <= fetch_first_md;
1667
    addr_ctrl  <= fetch_ad;
1668
    case op_code(7 downto 4) is
1669
    --
1670
    -- direct single op (2 bytes)
1671
    -- 6809 => 6 cycles
1672
    -- cpu09 => 5 cycles
1673
    -- 1 op=(pc) / pc=pc+1
1674
    -- 2 ea_hi=dp / ea_lo=(pc) / pc=pc+1
1675
    -- 3 md_lo=(ea) / pc=pc
1676
    -- 4 alu_left=md / md=alu_out / pc=pc
1677
    -- 5 (ea)=md_lo / pc=pc
1678
    --
1679
    -- Exception is JMP
1680
    -- 6809 => 3 cycles
1681
    -- cpu09 => 3 cycles
1682
    -- 1 op=(pc) / pc=pc+1
1683
    -- 2 ea_hi=dp / ea_lo=(pc) / pc=pc+1
1684
    -- 3 pc=ea
1685
               --
1686
        when "0000" =>
1687
      -- advance the PC
1688
      pc_ctrl    <= incr_pc;
1689
 
1690
      case op_code(3 downto 0) is
1691
      when "1110" => -- jmp
1692
        next_state <= jmp_state;
1693
 
1694
      when "1111" => -- clr
1695
        next_state <= single_op_exec_state;
1696
 
1697
      when others =>
1698
        next_state <= single_op_read_state;
1699
 
1700
      end case;
1701
 
1702
    -- acca / accb inherent instructions
1703
        when "0001" =>
1704
      case op_code(3 downto 0) is
1705
      --
1706
      -- Page2 pre byte
1707
      -- pre=(pc) / pc=pc+1
1708
      -- op=(pc) / pc=pc+1
1709
      --
1710
      when "0000" => -- page2
1711
        opfetch    <= '1';
1712
        op_ctrl    <= fetch_op;
1713
        -- advance pc
1714
        pc_ctrl    <= incr_pc;
1715
        next_state <= decode2_state;
1716
 
1717
      --
1718
      -- Page3 pre byte
1719
      -- pre=(pc) / pc=pc+1
1720
      -- op=(pc) / pc=pc+1
1721
      --
1722
      when "0001" => -- page3
1723
        opfetch    <= '1';
1724
        op_ctrl    <= fetch_op;
1725
        -- advance pc
1726
        pc_ctrl    <= incr_pc;
1727
        next_state <= decode3_state;
1728
 
1729
      --
1730
      -- nop - No operation ( 1 byte )
1731
      -- 6809 => 2 cycles
1732
      -- cpu09 => 2 cycles
1733
      -- 1 op=(pc) / pc=pc+1
1734
      -- 2 decode
1735
      -- 
1736
      when "0010" => -- nop
1737
        lic          <= '1';
1738
        next_state   <= fetch_state;
1739
 
1740
      --
1741
      -- sync - halt execution until an interrupt is received
1742
      -- interrupt may be NMI, IRQ or FIRQ
1743
      -- program execution continues if the 
1744
      -- interrupt is asserted for 3 clock cycles
1745
      -- note that registers are not pushed onto the stack
1746
      -- CPU09 => Interrupts need only be asserted for one clock cycle
1747
      --
1748
      when "0011" => -- sync
1749
        next_state   <= sync_state;
1750
 
1751
      --
1752
      -- lbra -- long branch (3 bytes)
1753
      -- 6809 => 5 cycles
1754
      -- cpu09 => 4 cycles
1755
      -- 1 op=(pc) / pc=pc+1
1756
      -- 2 md_hi=sign(pc) / md_lo=(pc) / pc=pc+1
1757
      -- 3 md_hi=md_lo / md_lo=(pc) / pc=pc+1
1758
      -- 4 pc=pc+md
1759
      --
1760
      when "0110" =>
1761
        -- increment the pc
1762
        pc_ctrl    <= incr_pc;
1763
        next_state <= lbranch_state;
1764
 
1765
      --
1766
      -- lbsr - long branch to subroutine (3 bytes)
1767
      -- 6809 => 9 cycles
1768
      -- cpu09 => 6 cycles
1769
      -- 1 op=(pc) /pc=pc+1
1770
      -- 2 md_hi=sign(pc) / md_lo=(pc) / pc=pc+1 / sp=sp-1
1771
      -- 3 md_hi=md_lo / md_lo=(pc) / pc=pc+1
1772
      -- 4 (sp)= pc_lo / sp=sp-1 / pc=pc
1773
      -- 5 (sp)=pc_hi / pc=pc
1774
      -- 6 pc=pc+md
1775
      --
1776
      when "0111" =>
1777
        -- pre decrement sp
1778
        left_ctrl  <= sp_left;
1779
        right_ctrl <= one_right;
1780
        alu_ctrl   <= alu_sub16;
1781
        sp_ctrl    <= load_sp;
1782
        -- increment the pc
1783
        pc_ctrl    <= incr_pc;
1784
        next_state <= lbranch_state;
1785
 
1786
      --
1787
      -- Decimal Adjust Accumulator
1788
      --
1789
      when "1001" => -- daa
1790
        left_ctrl  <= acca_left;
1791
        right_ctrl <= accb_right;
1792
        alu_ctrl   <= alu_daa;
1793
        cc_ctrl    <= load_cc;
1794
        acca_ctrl  <= load_acca;
1795
        lic        <= '1';
1796
        next_state <= fetch_state;
1797
 
1798
      --
1799
      -- OR Condition Codes
1800
      --
1801
      when "1010" => -- orcc
1802
        -- increment the pc
1803
        pc_ctrl      <= incr_pc;
1804
        next_state   <= orcc_state;
1805
 
1806
      --
1807
      -- AND Condition Codes
1808
      --
1809
      when "1100" => -- andcc
1810
        -- increment the pc
1811
        pc_ctrl      <= incr_pc;
1812
        next_state   <= andcc_state;
1813
 
1814
      --
1815
      -- Sign Extend
1816
      --
1817
      when "1101" => -- sex
1818
        left_ctrl  <= accb_left;
1819
        right_ctrl <= zero_right;
1820
        alu_ctrl   <= alu_sex;
1821
        cc_ctrl    <= load_cc;
1822
        acca_ctrl  <= load_hi_acca;
1823
        lic        <= '1';
1824
        next_state <= fetch_state;
1825
 
1826
      --
1827
      -- Exchange Registers
1828
      --
1829
      when "1110" => -- exg
1830
        -- increment the pc
1831
        pc_ctrl    <= incr_pc;
1832
        next_state <= exg_state;
1833
 
1834
      --
1835
      -- Transfer Registers
1836
      --
1837
      when "1111" => -- tfr
1838
        -- increment the pc
1839
        pc_ctrl      <= incr_pc;
1840
        next_state   <= tfr_state;
1841
 
1842
      when others =>
1843
        -- increment the pc
1844
        pc_ctrl    <= incr_pc;
1845
        lic        <= '1';
1846
        next_state <= fetch_state;
1847
      end case;
1848
 
1849
    --
1850
    -- Short branch conditional
1851
    -- 6809 => always 3 cycles
1852
    -- cpu09 => always = 3 cycles
1853
    -- 1 op=(pc) / pc=pc+1
1854
    -- 2 md_hi=sign(pc) / md_lo=(pc) / pc=pc+1 / test cc
1855
    -- 3 if cc tru pc=pc+md else pc=pc
1856
    --
1857
    when "0010" => -- branch conditional
1858
      -- increment the pc
1859
      pc_ctrl    <= incr_pc;
1860
      next_state <= sbranch_state;
1861
 
1862
    --
1863
    -- Single byte stack operators
1864
    -- Do not advance PC
1865
    --
1866
    when "0011" =>
1867
      --
1868
      -- lea - load effective address (2+ bytes)
1869
      -- 6809 => 4 cycles + addressing mode
1870
      -- cpu09 => 4 cycles + addressing mode
1871
      -- 1 op=(pc) / pc=pc+1
1872
      -- 2 md_lo=(pc) / pc=pc+1
1873
      -- 3 calculate ea
1874
      -- 4 ix/iy/sp/up = ea
1875
      --
1876
      case op_code(3 downto 0) is
1877
           when "0000" |  -- leax
1878
           "0001" |  -- leay
1879
           "0010" |  -- leas
1880
           "0011" => -- leau
1881
        -- advance PC
1882
        pc_ctrl      <= incr_pc;
1883
        st_ctrl      <= push_st;
1884
        return_state <= lea_state;
1885
        next_state   <= indexed_state;
1886
 
1887
      --
1888
      -- pshs - push registers onto sp stack
1889
      -- 6809 => 5 cycles + registers
1890
      -- cpu09 => 3 cycles + registers
1891
      --  1 op=(pc) / pc=pc+1
1892
      --  2 ea_lo=(pc) / pc=pc+1 
1893
      --  3 if ea(7 downto 0) != "00000000" then sp=sp-1
1894
      --  4 if ea(7) = 1 (sp)=pcl, sp=sp-1
1895
      --  5 if ea(7) = 1 (sp)=pch
1896
      --    if ea(6 downto 0) != "0000000" then sp=sp-1
1897
      --  6 if ea(6) = 1 (sp)=upl, sp=sp-1
1898
      --  7 if ea(6) = 1 (sp)=uph
1899
      --    if ea(5 downto 0) != "000000" then sp=sp-1
1900
      --  8 if ea(5) = 1 (sp)=iyl, sp=sp-1
1901
      --  9 if ea(5) = 1 (sp)=iyh
1902
      --    if ea(4 downto 0) != "00000" then sp=sp-1
1903
      -- 10 if ea(4) = 1 (sp)=ixl, sp=sp-1
1904
      -- 11 if ea(4) = 1 (sp)=ixh
1905
      --    if ea(3 downto 0) != "0000" then sp=sp-1
1906
      -- 12 if ea(3) = 1 (sp)=dp
1907
      --    if ea(2 downto 0) != "000" then sp=sp-1
1908
      -- 13 if ea(2) = 1 (sp)=accb
1909
      --    if ea(1 downto 0) != "00" then sp=sp-1
1910
      -- 14 if ea(1) = 1 (sp)=acca
1911
      --    if ea(0 downto 0) != "0" then sp=sp-1
1912
      -- 15 if ea(0) = 1 (sp)=cc
1913
      --
1914
      when "0100" => -- pshs
1915
        -- advance PC
1916
        pc_ctrl    <= incr_pc;
1917
        next_state <= pshs_state;
1918
 
1919
      --
1920
      -- puls - pull registers of sp stack
1921
      -- 6809 => 5 cycles + registers
1922
      -- cpu09 => 3 cycles + registers
1923
      --
1924
      when "0101" => -- puls
1925
        -- advance PC
1926
        pc_ctrl    <= incr_pc;
1927
        next_state <= puls_state;
1928
 
1929
      --
1930
      -- pshu - push registers onto up stack
1931
      -- 6809 => 5 cycles + registers
1932
      -- cpu09 => 3 cycles + registers
1933
      --
1934
      when "0110" => -- pshu
1935
        -- advance PC
1936
        pc_ctrl    <= incr_pc;
1937
        next_state <= pshu_state;
1938
 
1939
      --
1940
      -- pulu - pull registers of up stack
1941
      -- 6809 => 5 cycles + registers
1942
      -- cpu09 => 3 cycles + registers
1943
      --
1944
      when "0111" => -- pulu
1945
        -- advance PC
1946
        pc_ctrl    <= incr_pc;
1947
        next_state <= pulu_state;
1948
 
1949
      --
1950
      -- rts - return from subroutine
1951
      -- 6809 => 5 cycles
1952
      -- cpu09 => 4 cycles 
1953
      -- 1 op=(pc) / pc=pc+1
1954
      -- 2 decode op
1955
      -- 3 pc_hi = (sp) / sp=sp+1
1956
      -- 4 pc_lo = (sp) / sp=sp+1
1957
      --
1958
      when "1001" =>
1959
                  next_state   <= pull_return_hi_state;
1960
 
1961
      --
1962
      -- ADD accb to index register
1963
      -- *** Note: this is an unsigned addition.
1964
      --           does not affect any condition codes
1965
      -- 6809 => 3 cycles
1966
      -- cpu09 => 2 cycles
1967
      -- 1 op=(pc) / pc=pc+1
1968
      -- 2 alu_left=ix / alu_right=accb / ix=alu_out / pc=pc
1969
      --
1970
      when "1010" => -- abx
1971
        left_ctrl    <= ix_left;
1972
        right_ctrl   <= accb_right;
1973
        alu_ctrl     <= alu_abx;
1974
        ix_ctrl      <= load_ix;
1975
        lic          <= '1';
1976
        next_state   <= fetch_state;
1977
 
1978
      --
1979
      -- Return From Interrupt
1980
      --
1981
      when "1011" => -- rti
1982
        next_state   <= rti_cc_state;
1983
 
1984
      --
1985
      -- CWAI
1986
      --
1987
      when "1100" => -- cwai #$<cc_mask>
1988
        -- pre decrement sp
1989
        left_ctrl    <= sp_left;
1990
        right_ctrl   <= one_right;
1991
        alu_ctrl     <= alu_sub16;
1992
        sp_ctrl      <= load_sp;
1993
        -- increment pc
1994
        pc_ctrl      <= incr_pc;
1995
        next_state   <= cwai_state;
1996
 
1997
      --
1998
      -- MUL Multiply
1999
      --
2000
      when "1101" => -- mul
2001
                        -- move acca to md
2002
         left_ctrl  <= acca_left;
2003
         right_ctrl <= zero_right;
2004
         alu_ctrl   <= alu_st16;
2005
         md_ctrl    <= load_md; -- over ride md_ctrl <= second byte fetch 
2006
                        next_state <= mulea_state;
2007
 
2008
      --
2009
      -- SWI Software Interrupt
2010
      --
2011
      when "1111" => -- swi
2012
        -- predecrement SP
2013
        left_ctrl    <= sp_left;
2014
        right_ctrl   <= one_right;
2015
        alu_ctrl     <= alu_sub16;
2016
        sp_ctrl      <= load_sp;
2017
        iv_ctrl      <= swi_iv;
2018
        st_ctrl      <= push_st;
2019
        return_state <= int_swimask_state;
2020
        next_state   <= int_entire_state;
2021
 
2022
      when others =>
2023
        lic          <= '1';
2024
        next_state   <= fetch_state;
2025
 
2026
      end case;
2027
                                 --
2028
    -- Accumulator A Single operand
2029
    -- source = acca, dest = acca
2030
    -- Do not advance PC
2031
    -- Typically 2 cycles 1 bytes
2032
    -- 1 opcode fetch
2033
    -- 2 post byte fetch / instruction decode
2034
    -- Note that there is no post byte
2035
    -- so do not advance PC in decode cycle
2036
    -- Re-run opcode fetch cycle after decode
2037
    -- 
2038
    when "0100" => -- acca single op
2039
      left_ctrl  <= acca_left;
2040
      case op_code(3 downto 0) is
2041
 
2042
      when "0000" => -- neg
2043
        right_ctrl <= zero_right;
2044
        alu_ctrl   <= alu_neg;
2045
        acca_ctrl  <= load_acca;
2046
        cc_ctrl    <= load_cc;
2047
 
2048
      when "0011" => -- com
2049
        right_ctrl <= zero_right;
2050
        alu_ctrl   <= alu_com;
2051
        acca_ctrl  <= load_acca;
2052
        cc_ctrl    <= load_cc;
2053
 
2054
      when "0100" => -- lsr
2055
        right_ctrl <= zero_right;
2056
        alu_ctrl   <= alu_lsr8;
2057
        acca_ctrl  <= load_acca;
2058
        cc_ctrl    <= load_cc;
2059
 
2060
           when "0110" => -- ror
2061
        right_ctrl <= zero_right;
2062
        alu_ctrl   <= alu_ror8;
2063
        acca_ctrl  <= load_acca;
2064
        cc_ctrl    <= load_cc;
2065
 
2066
      when "0111" => -- asr
2067
        right_ctrl <= zero_right;
2068
        alu_ctrl   <= alu_asr8;
2069
        acca_ctrl  <= load_acca;
2070
        cc_ctrl    <= load_cc;
2071
 
2072
      when "1000" => -- asl
2073
        right_ctrl <= zero_right;
2074
        alu_ctrl   <= alu_asl8;
2075
        acca_ctrl  <= load_acca;
2076
        cc_ctrl    <= load_cc;
2077
 
2078
      when "1001" => -- rol
2079
        right_ctrl <= zero_right;
2080
        alu_ctrl   <= alu_rol8;
2081
        acca_ctrl  <= load_acca;
2082
        cc_ctrl    <= load_cc;
2083
 
2084
      when "1010" => -- dec
2085
        right_ctrl <= one_right;
2086
        alu_ctrl   <= alu_dec;
2087
        acca_ctrl  <= load_acca;
2088
        cc_ctrl    <= load_cc;
2089
 
2090
      when "1011" => -- undefined
2091
        right_ctrl <= zero_right;
2092
        alu_ctrl   <= alu_nop;
2093
        acca_ctrl  <= latch_acca;
2094
        cc_ctrl    <= latch_cc;
2095
 
2096
      when "1100" => -- inc
2097
        right_ctrl <= one_right;
2098
        alu_ctrl   <= alu_inc;
2099
        acca_ctrl  <= load_acca;
2100
        cc_ctrl    <= load_cc;
2101
 
2102
      when "1101" => -- tst
2103
        right_ctrl <= zero_right;
2104
        alu_ctrl   <= alu_st8;
2105
        acca_ctrl  <= latch_acca;
2106
        cc_ctrl    <= load_cc;
2107
 
2108
      when "1110" => -- jmp (not defined)
2109
        right_ctrl <= zero_right;
2110
        alu_ctrl   <= alu_nop;
2111
        acca_ctrl  <= latch_acca;
2112
        cc_ctrl    <= latch_cc;
2113
 
2114
      when "1111" => -- clr
2115
        right_ctrl <= zero_right;
2116
        alu_ctrl   <= alu_clr;
2117
        acca_ctrl  <= load_acca;
2118
        cc_ctrl    <= load_cc;
2119
 
2120
      when others =>
2121
        right_ctrl <= zero_right;
2122
        alu_ctrl   <= alu_nop;
2123
        acca_ctrl  <= latch_acca;
2124
        cc_ctrl    <= latch_cc;
2125
 
2126
      end case;
2127
      lic        <= '1';
2128
      next_state <= fetch_state;
2129
 
2130
      --
2131
      -- Single Operand accb
2132
      -- source = accb, dest = accb
2133
      -- Typically 2 cycles 1 bytes
2134
      -- 1 opcode fetch
2135
      -- 2 post byte fetch / instruction decode
2136
      -- Note that there is no post byte
2137
      -- so do not advance PC in decode cycle
2138
      -- Re-run opcode fetch cycle after decode
2139
      --
2140
    when "0101" =>
2141
      left_ctrl  <= accb_left;
2142
      case op_code(3 downto 0) is
2143
      when "0000" => -- neg
2144
        right_ctrl <= zero_right;
2145
        alu_ctrl   <= alu_neg;
2146
        accb_ctrl  <= load_accb;
2147
        cc_ctrl    <= load_cc;
2148
 
2149
      when "0011" => -- com
2150
        right_ctrl <= zero_right;
2151
        alu_ctrl   <= alu_com;
2152
        accb_ctrl  <= load_accb;
2153
        cc_ctrl    <= load_cc;
2154
 
2155
      when "0100" => -- lsr
2156
        right_ctrl <= zero_right;
2157
        alu_ctrl   <= alu_lsr8;
2158
        accb_ctrl  <= load_accb;
2159
        cc_ctrl    <= load_cc;
2160
 
2161
      when "0110" => -- ror
2162
        right_ctrl <= zero_right;
2163
        alu_ctrl   <= alu_ror8;
2164
        accb_ctrl  <= load_accb;
2165
        cc_ctrl    <= load_cc;
2166
 
2167
      when "0111" => -- asr
2168
        right_ctrl <= zero_right;
2169
        alu_ctrl   <= alu_asr8;
2170
        accb_ctrl  <= load_accb;
2171
        cc_ctrl    <= load_cc;
2172
 
2173
      when "1000" => -- asl
2174
        right_ctrl <= zero_right;
2175
        alu_ctrl   <= alu_asl8;
2176
        accb_ctrl  <= load_accb;
2177
        cc_ctrl    <= load_cc;
2178
 
2179
      when "1001" => -- rol
2180
        right_ctrl <= zero_right;
2181
        alu_ctrl   <= alu_rol8;
2182
        accb_ctrl  <= load_accb;
2183
        cc_ctrl    <= load_cc;
2184
 
2185
      when "1010" => -- dec
2186
        right_ctrl <= one_right;
2187
        alu_ctrl   <= alu_dec;
2188
        accb_ctrl  <= load_accb;
2189
        cc_ctrl    <= load_cc;
2190
 
2191
      when "1011" => -- undefined
2192
        right_ctrl <= zero_right;
2193
        alu_ctrl   <= alu_nop;
2194
        accb_ctrl  <= latch_accb;
2195
        cc_ctrl    <= latch_cc;
2196
 
2197
      when "1100" => -- inc
2198
        right_ctrl <= one_right;
2199
        alu_ctrl   <= alu_inc;
2200
        accb_ctrl  <= load_accb;
2201
        cc_ctrl    <= load_cc;
2202
 
2203
      when "1101" => -- tst
2204
        right_ctrl <= zero_right;
2205
        alu_ctrl   <= alu_st8;
2206
        accb_ctrl  <= latch_accb;
2207
        cc_ctrl    <= load_cc;
2208
 
2209
      when "1110" => -- jmp (undefined)
2210
        right_ctrl <= zero_right;
2211
        alu_ctrl   <= alu_nop;
2212
        accb_ctrl  <= latch_accb;
2213
        cc_ctrl    <= latch_cc;
2214
 
2215
      when "1111" => -- clr
2216
        right_ctrl <= zero_right;
2217
        alu_ctrl   <= alu_clr;
2218
        accb_ctrl  <= load_accb;
2219
        cc_ctrl    <= load_cc;
2220
 
2221
      when others =>
2222
        right_ctrl <= zero_right;
2223
        alu_ctrl   <= alu_nop;
2224
        accb_ctrl  <= latch_accb;
2225
        cc_ctrl    <= latch_cc;
2226
      end case;
2227
      lic          <= '1';
2228
      next_state   <= fetch_state;
2229
 
2230
    --
2231
    -- Single operand indexed
2232
    -- Two byte instruction so advance PC
2233
    -- EA should hold index offset
2234
    --
2235
    when "0110" => -- indexed single op
2236
      -- increment the pc 
2237
      pc_ctrl    <= incr_pc;
2238
      st_ctrl    <= push_st;
2239
 
2240
      case op_code(3 downto 0) is
2241
      when "1110" => -- jmp
2242
        return_state <= jmp_state;
2243
 
2244
      when "1111" => -- clr
2245
        return_state <= single_op_exec_state;
2246
 
2247
      when others =>
2248
        return_state <= single_op_read_state;
2249
 
2250
      end case;
2251
      next_state <= indexed_state;
2252
 
2253
    --
2254
    -- Single operand extended addressing
2255
    -- three byte instruction so advance the PC
2256
    -- Low order EA holds high order address
2257
    --
2258
    when "0111" => -- extended single op
2259
      -- increment PC
2260
      pc_ctrl    <= incr_pc;
2261
      st_ctrl    <= push_st;
2262
 
2263
      case op_code(3 downto 0) is
2264
      when "1110" => -- jmp
2265
        return_state <= jmp_state;
2266
 
2267
      when "1111" => -- clr
2268
        return_state <= single_op_exec_state;
2269
 
2270
      when others =>
2271
        return_state <= single_op_read_state;
2272
 
2273
      end case;
2274
      next_state <= extended_state;
2275
 
2276
    when "1000" => -- acca immediate
2277
      -- increment the pc
2278
      pc_ctrl    <= incr_pc;
2279
 
2280
      case op_code(3 downto 0) is
2281
      when "0011" | -- subd #
2282
           "1100" | -- cmpx #
2283
           "1110" => -- ldx #
2284
        next_state   <= imm16_state;
2285
 
2286
      --
2287
      -- bsr offset - Branch to subroutine (2 bytes)
2288
      -- 6809 => 7 cycles
2289
      -- cpu09 => 5 cycles
2290
      -- 1 op=(pc) / pc=pc+1
2291
      -- 2 md_hi=sign(pc) / md_lo=(pc) / sp=sp-1 / pc=pc+1
2292
      -- 3 (sp)=pc_lo / sp=sp-1
2293
      -- 4 (sp)=pc_hi
2294
      -- 5 pc=pc+md
2295
      --
2296
      when "1101" => -- bsr
2297
        -- pre decrement SP
2298
        left_ctrl  <= sp_left;
2299
        right_ctrl <= one_right;
2300
        alu_ctrl   <= alu_sub16;
2301
        sp_ctrl    <= load_sp;
2302
             --
2303
             st_ctrl      <= push_st;
2304
        return_state <= sbranch_state;
2305
        next_state   <= push_return_lo_state;
2306
 
2307
      when others =>
2308
        lic          <= '1';
2309
        next_state   <= fetch_state;
2310
 
2311
      end case;
2312
 
2313
    when "1001" => -- acca direct
2314
      -- increment the pc
2315
      pc_ctrl    <= incr_pc;
2316
      case op_code(3 downto 0) is
2317
      when "0011" | -- subd
2318
           "1100" | -- cmpx
2319
           "1110" => -- ldx
2320
        next_state   <= dual_op_read16_state;
2321
 
2322
      when "0111" =>  -- sta direct
2323
        next_state <= dual_op_write8_state;
2324
 
2325
           --
2326
      -- jsr direct - Jump to subroutine in direct page (2 bytes)
2327
      -- 6809 => 7 cycles
2328
      -- cpu09 => 5 cycles
2329
      -- 1 op=(pc) / pc=pc+1
2330
      -- 2 ea_hi=0 / ea_lo=(pc) / sp=sp-1 / pc=pc+1
2331
      -- 3 (sp)=pc_lo / sp=sp-1
2332
      -- 4 (sp)=pc_hi
2333
      -- 5 pc=ea
2334
      --
2335
      when "1101" => -- jsr direct
2336
        -- pre decrement sp
2337
        left_ctrl  <= sp_left;
2338
        right_ctrl <= one_right;
2339
        alu_ctrl   <= alu_sub16;
2340
        sp_ctrl    <= load_sp;
2341
        --
2342
        st_ctrl      <= push_st;
2343
        return_state <= jmp_state;
2344
        next_state   <= push_return_lo_state;
2345
 
2346
 
2347
      when "1111" => -- stx direct
2348
        -- idle ALU
2349
        left_ctrl  <= ix_left;
2350
        right_ctrl <= zero_right;
2351
        alu_ctrl   <= alu_nop;
2352
        cc_ctrl    <= latch_cc;
2353
        sp_ctrl    <= latch_sp;
2354
        next_state <= dual_op_write16_state;
2355
 
2356
      when others =>
2357
        next_state   <= dual_op_read8_state;
2358
 
2359
      end case;
2360
 
2361
    when "1010" => -- acca indexed
2362
      -- increment the pc
2363
      pc_ctrl    <= incr_pc;
2364
      case op_code(3 downto 0) is
2365
      when "0011" | -- subd
2366
           "1100" | -- cmpx
2367
           "1110" => -- ldx
2368
        st_ctrl      <= push_st;
2369
        return_state <= dual_op_read16_state;
2370
        next_state   <= indexed_state;
2371
 
2372
      when "0111" =>  -- staa ,x
2373
        st_ctrl      <= push_st;
2374
        return_state <= dual_op_write8_state;
2375
        next_state   <= indexed_state;
2376
 
2377
      when "1101" => -- jsr ,x
2378
        -- DO NOT pre decrement SP
2379
        st_ctrl      <= push_st;
2380
        return_state <= jsr_state;
2381
        next_state   <= indexed_state;
2382
 
2383
      when "1111" => -- stx ,x
2384
        st_ctrl      <= push_st;
2385
        return_state <= dual_op_write16_state;
2386
        next_state   <= indexed_state;
2387
 
2388
      when others =>
2389
        st_ctrl      <= push_st;
2390
        return_state <= dual_op_read8_state;
2391
        next_state   <= indexed_state;
2392
 
2393
      end case;
2394
 
2395
   when "1011" => -- acca extended
2396
     -- increment the pc
2397
     pc_ctrl    <= incr_pc;
2398
     case op_code(3 downto 0) is
2399
     when "0011" | -- subd
2400
          "1100" | -- cmpx
2401
          "1110" => -- ldx
2402
       st_ctrl      <= push_st;
2403
       return_state <= dual_op_read16_state;
2404
       next_state   <= extended_state;
2405
 
2406
     when "0111" =>  -- staa >
2407
       st_ctrl      <= push_st;
2408
       return_state <= dual_op_write8_state;
2409
       next_state   <= extended_state;
2410
 
2411
     when "1101" => -- jsr >extended
2412
     -- DO NOT pre decrement sp
2413
       st_ctrl      <= push_st;
2414
       return_state <= jsr_state;
2415
       next_state   <= extended_state;
2416
 
2417
     when "1111" => -- stx >
2418
       st_ctrl      <= push_st;
2419
       return_state <= dual_op_write16_state;
2420
       next_state   <= extended_state;
2421
 
2422
     when others =>
2423
       st_ctrl      <= push_st;
2424
       return_state <= dual_op_read8_state;
2425
       next_state   <= extended_state;
2426
 
2427
     end case;
2428
 
2429
   when "1100" => -- accb immediate
2430
     -- increment the pc
2431
     pc_ctrl    <= incr_pc;
2432
     case op_code(3 downto 0) is
2433
     when "0011" | -- addd #
2434
          "1100" | -- ldd #
2435
          "1110" => -- ldu #
2436
       next_state   <= imm16_state;
2437
 
2438
     when others =>
2439
       lic          <= '1';
2440
       next_state   <= fetch_state;
2441
 
2442
     end case;
2443
 
2444
   when "1101" => -- accb direct
2445
     -- increment the pc
2446
     pc_ctrl    <= incr_pc;
2447
     case op_code(3 downto 0) is
2448
     when "0011" | -- addd
2449
          "1100" | -- ldd
2450
          "1110" => -- ldu
2451
       next_state   <= dual_op_read16_state;
2452
 
2453
     when "0111" =>  -- stab direct
2454
       next_state   <= dual_op_write8_state;
2455
 
2456
     when "1101" =>  -- std direct
2457
       next_state   <= dual_op_write16_state;
2458
 
2459
     when "1111" => -- stu direct
2460
       next_state   <= dual_op_write16_state;
2461
 
2462
     when others =>
2463
       next_state   <= dual_op_read8_state;
2464
 
2465
     end case;
2466
 
2467
   when "1110" => -- accb indexed
2468
     -- increment the pc
2469
     pc_ctrl    <= incr_pc;
2470
     case op_code(3 downto 0) is
2471
     when "0011" | -- addd
2472
          "1100" | -- ldd
2473
          "1110" => -- ldu
2474
       st_ctrl      <= push_st;
2475
       return_state <= dual_op_read16_state;
2476
       next_state   <= indexed_state;
2477
 
2478
     when "0111" =>  -- stab indexed
2479
       st_ctrl      <= push_st;
2480
       return_state <= dual_op_write8_state;
2481
       next_state   <= indexed_state;
2482
 
2483
     when "1101" =>  -- std indexed
2484
       st_ctrl      <= push_st;
2485
       return_state <= dual_op_write16_state;
2486
       next_state   <= indexed_state;
2487
 
2488
     when "1111" => -- stu indexed
2489
       st_ctrl      <= push_st;
2490
       return_state <= dual_op_write16_state;
2491
       next_state   <= indexed_state;
2492
 
2493
     when others =>
2494
       st_ctrl      <= push_st;
2495
       return_state <= dual_op_read8_state;
2496
       next_state   <= indexed_state;
2497
 
2498
     end case;
2499
 
2500
   when "1111" => -- accb extended
2501
     -- increment the pc
2502
     pc_ctrl    <= incr_pc;
2503
     case op_code(3 downto 0) is
2504
     when "0011" | -- addd
2505
                    "1100" | -- ldd
2506
               "1110" => -- ldu
2507
            st_ctrl      <= push_st;
2508
            return_state <= dual_op_read16_state;
2509
       next_state   <= extended_state;
2510
 
2511
     when "0111" =>  -- stab extended
2512
            st_ctrl      <= push_st;
2513
            return_state <= dual_op_write8_state;
2514
       next_state   <= extended_state;
2515
 
2516
     when "1101" =>  -- std extended
2517
       st_ctrl      <= push_st;
2518
            return_state <= dual_op_write16_state;
2519
            next_state   <= extended_state;
2520
 
2521
          when "1111" => -- stu  extended
2522
            st_ctrl      <= push_st;
2523
            return_state <= dual_op_write16_state;
2524
            next_state   <= extended_state;
2525
 
2526
          when others =>
2527
            st_ctrl      <= push_st;
2528
            return_state <= dual_op_read8_state;
2529
            next_state   <= extended_state;
2530
     end case;
2531
        --
2532
   -- not sure why I need this
2533
   --   
2534
   when others =>
2535
          lic <= '1';
2536
          next_state <= fetch_state;
2537
        end case;
2538
 
2539
                         --
2540
                         -- Here to decode prefix 2 instruction
2541
                         -- and fetch next byte of intruction
2542
                         -- whether it be necessary or not
2543
                         --
2544
          when decode2_state =>
2545
                                 -- fetch first byte of address or immediate data
2546
             ea_ctrl    <= fetch_first_ea;
2547
                                 md_ctrl    <= fetch_first_md;
2548
             addr_ctrl  <= fetch_ad;
2549
                            case op_code(7 downto 4) is
2550
                                 --
2551
                                 -- lbcc -- long branch conditional
2552
                                 -- 6809 => branch 6 cycles, no branch 5 cycles
2553
                                 -- cpu09 => always 5 cycles
2554
                                 -- 1 pre=(pc) / pc=pc+1
2555
                                 -- 2 op=(pc) / pc=pc+1
2556
                                 -- 3 md_hi=sign(pc) / md_lo=(pc) / pc=pc+1
2557
             -- 4 md_hi=md_lo / md_lo=(pc) / pc=pc+1
2558
                                 -- 5 if cond pc=pc+md else pc=pc
2559
                                 --
2560
                  when "0010" =>
2561
                                        -- increment the pc
2562
               pc_ctrl    <= incr_pc;
2563
                                        next_state <= lbranch_state;
2564
 
2565
                                 --
2566
                                 -- Single byte stack operators
2567
                                 -- Do not advance PC
2568
                                 --
2569
                  when "0011" =>
2570
                    case op_code(3 downto 0) is
2571
                         when "1111" => -- swi 2
2572
                                           -- predecrement sp
2573
                            left_ctrl    <= sp_left;
2574
                            right_ctrl   <= one_right;
2575
                                                alu_ctrl     <= alu_sub16;
2576
                                                sp_ctrl      <= load_sp;
2577
                                                iv_ctrl      <= swi2_iv;
2578
                                                st_ctrl      <= push_st;
2579
                                                return_state <= vect_hi_state;
2580
                                                next_state   <= int_entire_state;
2581
 
2582
                         when others =>
2583
                 lic          <= '1';
2584
                                          next_state   <= fetch_state;
2585
                         end case;
2586
 
2587
                  when "1000" => -- acca immediate
2588
                                   -- increment the pc
2589
               pc_ctrl    <= incr_pc;
2590
                                        case op_code(3 downto 0) is
2591
               when "0011" | -- cmpd #
2592
                                             "1100" | -- cmpy #
2593
                                             "1110" => -- ldy #
2594
                                          next_state   <= imm16_state;
2595
 
2596
                                        when others =>
2597
                 lic          <= '1';
2598
                                     next_state   <= fetch_state;
2599
 
2600
               end case;
2601
 
2602
                  when "1001" => -- acca direct
2603
                                        -- increment the pc
2604
               pc_ctrl    <= incr_pc;
2605
                                        case op_code(3 downto 0) is
2606
               when "0011" | -- cmpd <
2607
                                             "1100" | -- cmpy <
2608
                                             "1110" => -- ldy <
2609
                                          next_state   <= dual_op_read16_state;
2610
 
2611
                                        when "1111" => -- sty <
2612
                                     next_state   <= dual_op_write16_state;
2613
 
2614
                                        when others =>
2615
                 lic          <= '1';
2616
                                     next_state   <= fetch_state;
2617
 
2618
               end case;
2619
 
2620
                  when "1010" => -- acca indexed
2621
                                        -- increment the pc
2622
               pc_ctrl    <= incr_pc;
2623
                                        case op_code(3 downto 0) is
2624
               when "0011" | -- cmpd ,ind
2625
                                             "1100" | -- cmpy ,ind
2626
                                             "1110" => -- ldy ,ind
2627
                                     st_ctrl      <= push_st;
2628
                                     return_state <= dual_op_read16_state;
2629
                                          next_state   <= indexed_state;
2630
 
2631
                                        when "1111" => -- sty ,ind
2632
                                     st_ctrl      <= push_st;
2633
                                     return_state <= dual_op_write16_state;
2634
                                     next_state   <= indexed_state;
2635
 
2636
                                        when others =>
2637
                 lic          <= '1';
2638
                                     next_state   <= fetch_state;
2639
               end case;
2640
 
2641
             when "1011" => -- acca extended
2642
                                        -- increment the pc
2643
               pc_ctrl    <= incr_pc;
2644
                                        case op_code(3 downto 0) is
2645
               when "0011" | -- cmpd <
2646
                                             "1100" | -- cmpy <
2647
                                             "1110" => -- ldy <
2648
                                     st_ctrl      <= push_st;
2649
                                     return_state <= dual_op_read16_state;
2650
                                          next_state   <= extended_state;
2651
 
2652
                                        when "1111" => -- sty >
2653
                                     st_ctrl      <= push_st;
2654
                                     return_state <= dual_op_write16_state;
2655
                                     next_state   <= extended_state;
2656
 
2657
                                        when others =>
2658
                 lic          <= '1';
2659
                                     next_state   <= fetch_state;
2660
 
2661
               end case;
2662
 
2663
                  when "1100" => -- accb immediate
2664
                                        -- increment the pc
2665
               pc_ctrl    <= incr_pc;
2666
                                        case op_code(3 downto 0) is
2667
               when "0011" | -- undef #
2668
                                             "1100" | -- undef #
2669
                                             "1110" => -- lds #
2670
                                          next_state   <= imm16_state;
2671
 
2672
                                        when others =>
2673
                                     next_state   <= fetch_state;
2674
 
2675
               end case;
2676
 
2677
                  when "1101" => -- accb direct
2678
                                        -- increment the pc
2679
               pc_ctrl    <= incr_pc;
2680
                                        case op_code(3 downto 0) is
2681
               when "0011" | -- undef <
2682
                                             "1100" | -- undef <
2683
                                             "1110" => -- lds <
2684
                                          next_state   <= dual_op_read16_state;
2685
 
2686
                                        when "1111" => -- sts <
2687
                                     next_state   <= dual_op_write16_state;
2688
 
2689
                                        when others =>
2690
                 lic          <= '1';
2691
                                     next_state   <= fetch_state;
2692
 
2693
               end case;
2694
 
2695
                  when "1110" => -- accb indexed
2696
                                        -- increment the pc
2697
               pc_ctrl    <= incr_pc;
2698
                                        case op_code(3 downto 0) is
2699
               when "0011" | -- undef ,ind
2700
                                             "1100" | -- undef ,ind
2701
                                             "1110" => -- lds ,ind
2702
                                     st_ctrl      <= push_st;
2703
                                     return_state <= dual_op_read16_state;
2704
                                          next_state   <= indexed_state;
2705
 
2706
                                        when "1111" => -- sts ,ind
2707
                                     st_ctrl      <= push_st;
2708
                                     return_state <= dual_op_write16_state;
2709
                                     next_state   <= indexed_state;
2710
 
2711
                                        when others =>
2712
                 lic          <= '1';
2713
                                     next_state   <= fetch_state;
2714
 
2715
               end case;
2716
 
2717
             when "1111" => -- accb extended
2718
                                        -- increment the pc
2719
               pc_ctrl    <= incr_pc;
2720
                                        case op_code(3 downto 0) is
2721
               when "0011" | -- undef >
2722
                                             "1100" | -- undef >
2723
                                             "1110" => -- lds >
2724
                                     st_ctrl      <= push_st;
2725
                                     return_state <= dual_op_read16_state;
2726
                                          next_state   <= extended_state;
2727
 
2728
                                        when "1111" => -- sts >
2729
                                     st_ctrl      <= push_st;
2730
                                     return_state <= dual_op_write16_state;
2731
                                     next_state   <= extended_state;
2732
 
2733
                                        when others =>
2734
                 lic          <= '1';
2735
                                     next_state   <= fetch_state;
2736
               end case;
2737
 
2738
                  when others =>
2739
               lic          <= '1';
2740
                         next_state   <= fetch_state;
2741
             end case;
2742
                         --
2743
                         -- Here to decode instruction
2744
                         -- and fetch next byte of intruction
2745
                         -- whether it be necessary or not
2746
                         --
2747
          when decode3_state =>
2748
             ea_ctrl    <= fetch_first_ea;
2749
                                 md_ctrl    <= fetch_first_md;
2750
             addr_ctrl  <= fetch_ad;
2751
             dout_ctrl  <= md_lo_dout;
2752
                            case op_code(7 downto 4) is
2753
                                 --
2754
                                 -- Single byte stack operators
2755
                                 -- Do not advance PC
2756
                                 --
2757
                  when "0011" =>
2758
                    case op_code(3 downto 0) is
2759
                         when "1111" => -- swi3
2760
                                           -- predecrement sp
2761
                            left_ctrl    <= sp_left;
2762
                            right_ctrl   <= one_right;
2763
                                                alu_ctrl     <= alu_sub16;
2764
                                                sp_ctrl      <= load_sp;
2765
                                                iv_ctrl      <= swi3_iv;
2766
                                                st_ctrl      <= push_st;
2767
                                                return_state <= vect_hi_state;
2768
                                                next_state   <= int_entire_state;
2769
                         when others =>
2770
                 lic          <= '1';
2771
                                          next_state   <= fetch_state;
2772
                         end case;
2773
 
2774
                  when "1000" => -- acca immediate
2775
                                   -- increment the pc
2776
               pc_ctrl    <= incr_pc;
2777
                                        case op_code(3 downto 0) is
2778
               when "0011" | -- cmpu #
2779
                                             "1100" | -- cmps #
2780
                                             "1110" => -- undef #
2781
                                          next_state   <= imm16_state;
2782
                                        when others =>
2783
                 lic          <= '1';
2784
                                     next_state   <= fetch_state;
2785
               end case;
2786
 
2787
                  when "1001" => -- acca direct
2788
                                        -- increment the pc
2789
               pc_ctrl    <= incr_pc;
2790
                                        case op_code(3 downto 0) is
2791
               when "0011" | -- cmpu <
2792
                                             "1100" | -- cmps <
2793
                                             "1110" => -- undef <
2794
                                          next_state   <= dual_op_read16_state;
2795
 
2796
                                        when others =>
2797
                 lic          <= '1';
2798
                                     next_state   <= fetch_state;
2799
 
2800
               end case;
2801
 
2802
                  when "1010" => -- acca indexed
2803
                                        -- increment the pc
2804
               pc_ctrl    <= incr_pc;
2805
                                        case op_code(3 downto 0) is
2806
               when "0011" | -- cmpu ,X
2807
                                             "1100" | -- cmps ,X
2808
                                             "1110" => -- undef ,X
2809
                                     st_ctrl      <= push_st;
2810
                                     return_state <= dual_op_read16_state;
2811
                                          next_state   <= indexed_state;
2812
 
2813
                                        when others =>
2814
                 lic          <= '1';
2815
                                     next_state   <= fetch_state;
2816
 
2817
               end case;
2818
 
2819
             when "1011" => -- acca extended
2820
                                        -- increment the pc
2821
               pc_ctrl    <= incr_pc;
2822
                                        case op_code(3 downto 0) is
2823
               when "0011" | -- cmpu >
2824
                                             "1100" | -- cmps >
2825
                                             "1110" => -- undef >
2826
                                     st_ctrl      <= push_st;
2827
                                     return_state <= dual_op_read16_state;
2828
                                          next_state   <= extended_state;
2829
                                        when others =>
2830
                 lic          <= '1';
2831
                                     next_state   <= fetch_state;
2832
               end case;
2833
 
2834
                  when others =>
2835
               lic          <= '1';
2836
                         next_state   <= fetch_state;
2837
             end case;
2838
 
2839
           --
2840
                          -- here if ea holds low byte
2841
                          -- Direct
2842
                          -- Extended
2843
                          -- Indexed
2844
                          -- read memory location
2845
                          --
2846
                          when single_op_read_state =>
2847
                                        -- read memory into md
2848
                                   md_ctrl    <= fetch_first_md;
2849
               addr_ctrl  <= read_ad;
2850
                                        dout_ctrl  <= md_lo_dout;
2851
                                        next_state <= single_op_exec_state;
2852
 
2853
                when single_op_exec_state =>
2854
                    case op_code(3 downto 0) is
2855
                         when "0000" => -- neg
2856
                   left_ctrl  <= md_left;
2857
                                            right_ctrl <= zero_right;
2858
                                            alu_ctrl   <= alu_neg;
2859
                                            cc_ctrl    <= load_cc;
2860
                                       md_ctrl    <= load_md;
2861
                                       next_state <= single_op_write_state;
2862
                    when "0011" => -- com
2863
                   left_ctrl  <= md_left;
2864
                             right_ctrl <= zero_right;
2865
                                            alu_ctrl   <= alu_com;
2866
                                            cc_ctrl    <= load_cc;
2867
                                       md_ctrl    <= load_md;
2868
                                       next_state <= single_op_write_state;
2869
                         when "0100" => -- lsr
2870
                   left_ctrl  <= md_left;
2871
                                                 right_ctrl <= zero_right;
2872
                                            alu_ctrl   <= alu_lsr8;
2873
                                            cc_ctrl    <= load_cc;
2874
                                       md_ctrl    <= load_md;
2875
                                       next_state <= single_op_write_state;
2876
                         when "0110" => -- ror
2877
                   left_ctrl  <= md_left;
2878
                                                 right_ctrl <= zero_right;
2879
                                            alu_ctrl   <= alu_ror8;
2880
                                            cc_ctrl    <= load_cc;
2881
                                       md_ctrl    <= load_md;
2882
                                       next_state <= single_op_write_state;
2883
                         when "0111" => -- asr
2884
                   left_ctrl  <= md_left;
2885
                                                 right_ctrl <= zero_right;
2886
                                            alu_ctrl   <= alu_asr8;
2887
                                            cc_ctrl    <= load_cc;
2888
                                       md_ctrl    <= load_md;
2889
                                       next_state <= single_op_write_state;
2890
                         when "1000" => -- asl
2891
                   left_ctrl  <= md_left;
2892
                                                 right_ctrl <= zero_right;
2893
                                            alu_ctrl   <= alu_asl8;
2894
                                            cc_ctrl    <= load_cc;
2895
                                       md_ctrl    <= load_md;
2896
                                       next_state <= single_op_write_state;
2897
                         when "1001" => -- rol
2898
                   left_ctrl  <= md_left;
2899
                                                 right_ctrl <= zero_right;
2900
                                            alu_ctrl   <= alu_rol8;
2901
                                            cc_ctrl    <= load_cc;
2902
                                       md_ctrl    <= load_md;
2903
                                       next_state <= single_op_write_state;
2904
                         when "1010" => -- dec
2905
                   left_ctrl  <= md_left;
2906
                             right_ctrl <= one_right;
2907
                                            alu_ctrl   <= alu_dec;
2908
                                            cc_ctrl    <= load_cc;
2909
                                       md_ctrl    <= load_md;
2910
                                       next_state <= single_op_write_state;
2911
                         when "1011" => -- undefined
2912
                   lic        <= '1';
2913
                                       next_state <= fetch_state;
2914
                         when "1100" => -- inc
2915
                   left_ctrl  <= md_left;
2916
                             right_ctrl <= one_right;
2917
                                            alu_ctrl   <= alu_inc;
2918
                                            cc_ctrl    <= load_cc;
2919
                                       md_ctrl    <= load_md;
2920
                                       next_state <= single_op_write_state;
2921
                         when "1101" => -- tst
2922
                   left_ctrl  <= md_left;
2923
                             right_ctrl <= zero_right;
2924
                                            alu_ctrl   <= alu_st8;
2925
                                            cc_ctrl    <= load_cc;
2926
                   lic        <= '1';
2927
                                       next_state <= fetch_state;
2928
                         when "1110" => -- jmp
2929
                   left_ctrl  <= md_left;
2930
                                                 right_ctrl <= zero_right;
2931
                                            alu_ctrl   <= alu_ld16;
2932
                   pc_ctrl    <= load_pc;
2933
                   lic          <= '1';
2934
                                       next_state <= fetch_state;
2935
                         when "1111" => -- clr
2936
                   left_ctrl  <= md_left;
2937
                                                 right_ctrl <= zero_right;
2938
                                            alu_ctrl   <= alu_clr;
2939
                                            cc_ctrl    <= load_cc;
2940
                                       md_ctrl    <= load_md;
2941
                                       next_state <= single_op_write_state;
2942
                         when others =>
2943
                 lic        <= '1';
2944
                                     next_state <= fetch_state;
2945
                         end case;
2946
           --
2947
                          -- single operand 8 bit write
2948
                          -- Write low 8 bits of ALU output
2949
                          -- EA holds address
2950
                          -- MD holds data
2951
                          --
2952
                          when single_op_write_state =>
2953
                                 -- write ALU low byte output
2954
             addr_ctrl  <= write_ad;
2955
             dout_ctrl  <= md_lo_dout;
2956
             lic        <= '1';
2957
                                 next_state <= fetch_state;
2958
 
2959
           --
2960
                          -- here if ea holds address of low byte
2961
                          -- read memory location
2962
                          --
2963
                          when dual_op_read8_state =>
2964
                                   -- read first data byte from ea
2965
                                   md_ctrl    <= fetch_first_md;
2966
               addr_ctrl  <= read_ad;
2967
               lic        <= '1';
2968
                                        next_state <= fetch_state;
2969
 
2970
                                --
2971
                                -- Here to read a 16 bit value into MD
2972
                                -- pointed to by the EA register
2973
                                -- The first byte is read
2974
                                -- and the EA is incremented
2975
                                --
2976
                           when dual_op_read16_state =>
2977
                                        -- increment the effective address
2978
               left_ctrl  <= ea_left;
2979
               right_ctrl <= one_right;
2980
               alu_ctrl   <= alu_add16;
2981
               ea_ctrl    <= load_ea;
2982
                                        -- read the high byte of the 16 bit data
2983
                                   md_ctrl    <= fetch_first_md;
2984
               addr_ctrl  <= read_ad;
2985
                                        next_state <= dual_op_read16_2_state;
2986
 
2987
                                --
2988
                                -- here to read the second byte
2989
                           -- pointed to by EA into MD
2990
                                --
2991
                           when dual_op_read16_2_state =>
2992
                                        -- read the low byte of the 16 bit data
2993
                                   md_ctrl    <= fetch_next_md;
2994
               addr_ctrl  <= read_ad;
2995
               lic        <= '1';
2996
                                        next_state <= fetch_state;
2997
 
2998
           --
2999
                          -- 16 bit Write state
3000
                          -- EA hold address of memory to write to
3001
                          -- Advance the effective address in ALU
3002
                          -- decode op_code to determine which
3003
                          -- register to write
3004
                          --
3005
                          when dual_op_write16_state =>
3006
                                 -- increment the effective address
3007
                                 left_ctrl  <= ea_left;
3008
                                 right_ctrl <= one_right;
3009
                                 alu_ctrl   <= alu_add16;
3010
                            ea_ctrl    <= load_ea;
3011
                                 -- write the ALU hi byte at ea
3012
             addr_ctrl  <= write_ad;
3013
                                 if op_code(6) = '0' then
3014
                                   case op_code(3 downto 0) is
3015
                              when "1111" => -- stx / sty
3016
                                          case pre_code is
3017
                                          when "00010000" => -- page 2 -- sty
3018
                        dout_ctrl  <= iy_hi_dout;
3019
                                     when others =>     -- page 1 -- stx
3020
                   dout_ctrl  <= ix_hi_dout;
3021
                                          end case;
3022
                                   when others =>
3023
                 dout_ctrl  <= md_hi_dout;
3024
                                   end case;
3025
             else
3026
                                   case op_code(3 downto 0) is
3027
                              when "1101" => -- std
3028
                 dout_ctrl  <= acca_dout; -- acca is high byte of ACCD
3029
                              when "1111" => -- stu / sts
3030
                                          case pre_code is
3031
                                          when "00010000" => -- page 2 -- sts
3032
                        dout_ctrl  <= sp_hi_dout;
3033
                                          when others =>     -- page 1 -- stu
3034
                        dout_ctrl  <= up_hi_dout;
3035
                                          end case;
3036
                                   when others =>
3037
                 dout_ctrl  <= md_hi_dout;
3038
                                   end case;
3039
             end if;
3040
                                 next_state   <= dual_op_write8_state;
3041
 
3042
           --
3043
                          -- Dual operand 8 bit write
3044
           -- Write 8 bit accumulator
3045
                          -- or low byte of 16 bit register
3046
                          -- EA holds address
3047
                          -- decode opcode to determine
3048
                          -- which register to apply to the bus
3049
                          -- Also set the condition codes here
3050
                          --
3051
                          when dual_op_write8_state =>
3052
                            if op_code(6) = '0' then
3053
                                   case op_code(3 downto 0) is
3054
                                        when "0111" => -- sta
3055
                 dout_ctrl  <= acca_dout;
3056
                                        when "1111" => -- stx / sty
3057
                                          case pre_code is
3058
                                          when "00010000" => -- page 2 -- sty
3059
                        dout_ctrl  <= iy_lo_dout;
3060
                                     when others =>     -- page 1 -- stx
3061
                   dout_ctrl  <= ix_lo_dout;
3062
                                          end case;
3063
                                        when others =>
3064
                 dout_ctrl  <= md_lo_dout;
3065
                                        end case;
3066
             else
3067
                                   case op_code(3 downto 0) is
3068
                                        when "0111" => -- stb
3069
                 dout_ctrl  <= accb_dout;
3070
                                        when "1101" => -- std
3071
                 dout_ctrl  <= accb_dout; -- accb is low byte of accd
3072
                                        when "1111" => -- stu / sts
3073
                                          case pre_code is
3074
                                          when "00010000" => -- page 2 -- sts
3075
                        dout_ctrl  <= sp_lo_dout;
3076
                                          when others =>     -- page 1 -- stu
3077
                        dout_ctrl  <= up_lo_dout;
3078
                                          end case;
3079
                                        when others =>
3080
                 dout_ctrl  <= md_lo_dout;
3081
                                        end case;
3082
             end if;
3083
                                 -- write ALU low byte output
3084
             addr_ctrl    <= write_ad;
3085
             lic          <= '1';
3086
                                 next_state   <= fetch_state;
3087
 
3088
                          --
3089
                          -- 16 bit immediate addressing mode
3090
                          --
3091
                          when imm16_state =>
3092
                                   -- increment pc
3093
               pc_ctrl    <= incr_pc;
3094
                                   -- fetch next immediate byte
3095
                              md_ctrl    <= fetch_next_md;
3096
               addr_ctrl  <= fetch_ad;
3097
               lic        <= '1';
3098
                                        next_state <= fetch_state;
3099
 
3100
           --
3101
                          -- md & ea holds 8 bit index offset
3102
                          -- calculate the effective memory address
3103
                          -- using the alu
3104
                          --
3105
           when indexed_state =>
3106
                                 --
3107
                                 -- decode indexing mode
3108
                                 --
3109
                                 if md(7) = '0' then
3110
                                   case md(6 downto 5) is
3111
                                        when "00" =>
3112
                                left_ctrl  <= ix_left;
3113
                                        when "01" =>
3114
                                left_ctrl  <= iy_left;
3115
                                        when "10" =>
3116
                                left_ctrl  <= up_left;
3117
                                        when others =>
3118
                                        -- when "11" =>
3119
                                left_ctrl  <= sp_left;
3120
                                        end case;
3121
                                   right_ctrl   <= md_sign5_right;
3122
                                   alu_ctrl     <= alu_add16;
3123
               ea_ctrl      <= load_ea;
3124
                                        next_state   <= saved_state;
3125
 
3126
                                 else
3127
                                   case md(3 downto 0) is
3128
                                        when "0000" =>     -- ,R+
3129
                                     case md(6 downto 5) is
3130
                                          when "00" =>
3131
                                  left_ctrl  <= ix_left;
3132
                                          when "01" =>
3133
                                  left_ctrl  <= iy_left;
3134
                                          when "10" =>
3135
                                  left_ctrl  <= up_left;
3136
                                          when others =>
3137
                                  left_ctrl  <= sp_left;
3138
                                          end case;
3139
                                          --
3140
                                     right_ctrl <= zero_right;
3141
                                     alu_ctrl   <= alu_add16;
3142
                 ea_ctrl    <= load_ea;
3143
                 next_state <= postincr1_state;
3144
 
3145
                                        when "0001" =>     -- ,R++
3146
                                     case md(6 downto 5) is
3147
                                          when "00" =>
3148
                                  left_ctrl  <= ix_left;
3149
                                          when "01" =>
3150
                                  left_ctrl  <= iy_left;
3151
                                          when "10" =>
3152
                                  left_ctrl  <= up_left;
3153
                                          when others =>
3154
                                          -- when "11" =>
3155
                                  left_ctrl  <= sp_left;
3156
                                          end case;
3157
                                     right_ctrl <= zero_right;
3158
                                     alu_ctrl   <= alu_add16;
3159
                 ea_ctrl    <= load_ea;
3160
                 next_state <= postincr2_state;
3161
 
3162
                                        when "0010" =>     -- ,-R
3163
                                     case md(6 downto 5) is
3164
                                          when "00" =>
3165
                                  left_ctrl  <= ix_left;
3166
                   ix_ctrl    <= load_ix;
3167
                                          when "01" =>
3168
                                  left_ctrl  <= iy_left;
3169
                   iy_ctrl    <= load_iy;
3170
                                          when "10" =>
3171
                                  left_ctrl  <= up_left;
3172
                   up_ctrl    <= load_up;
3173
                                          when others =>
3174
                                          -- when "11" =>
3175
                                  left_ctrl  <= sp_left;
3176
                   sp_ctrl    <= load_sp;
3177
                                          end case;
3178
                                     right_ctrl   <= one_right;
3179
                                     alu_ctrl     <= alu_sub16;
3180
                 ea_ctrl      <= load_ea;
3181
                                          next_state   <= saved_state;
3182
 
3183
                                        when "0011" =>     -- ,--R
3184
                                     case md(6 downto 5) is
3185
                                          when "00" =>
3186
                                  left_ctrl  <= ix_left;
3187
                   ix_ctrl    <= load_ix;
3188
                                          when "01" =>
3189
                                  left_ctrl  <= iy_left;
3190
                   iy_ctrl    <= load_iy;
3191
                                          when "10" =>
3192
                                  left_ctrl  <= up_left;
3193
                   up_ctrl    <= load_up;
3194
                                          when others =>
3195
                                          -- when "11" =>
3196
                                  left_ctrl  <= sp_left;
3197
                   sp_ctrl    <= load_sp;
3198
                                          end case;
3199
                                     right_ctrl <= two_right;
3200
                                     alu_ctrl   <= alu_sub16;
3201
                 ea_ctrl    <= load_ea;
3202
                                          if md(4) = '0' then
3203
                                            next_state   <= saved_state;
3204
                                          else
3205
                                            next_state   <= indirect_state;
3206
                                          end if;
3207
 
3208
                                        when "0100" =>     -- ,R (zero offset)
3209
                                     case md(6 downto 5) is
3210
                                          when "00" =>
3211
                                  left_ctrl  <= ix_left;
3212
                                          when "01" =>
3213
                                  left_ctrl  <= iy_left;
3214
                                          when "10" =>
3215
                                  left_ctrl  <= up_left;
3216
                                          when others =>
3217
                                          -- when "11" =>
3218
                                  left_ctrl  <= sp_left;
3219
                                          end case;
3220
                                     right_ctrl <= zero_right;
3221
                                     alu_ctrl   <= alu_add16;
3222
                 ea_ctrl    <= load_ea;
3223
                                          if md(4) = '0' then
3224
                                            next_state   <= saved_state;
3225
                                          else
3226
                                            next_state   <= indirect_state;
3227
                                          end if;
3228
 
3229
                                        when "0101" =>     -- ACCB,R
3230
                                     case md(6 downto 5) is
3231
                                          when "00" =>
3232
                                  left_ctrl  <= ix_left;
3233
                                          when "01" =>
3234
                                  left_ctrl  <= iy_left;
3235
                                          when "10" =>
3236
                                  left_ctrl  <= up_left;
3237
                                          when others =>
3238
                                          -- when "11" =>
3239
                                  left_ctrl  <= sp_left;
3240
                                          end case;
3241
                                     right_ctrl <= accb_right;
3242
                                     alu_ctrl   <= alu_add16;
3243
                 ea_ctrl    <= load_ea;
3244
                                          if md(4) = '0' then
3245
                                            next_state   <= saved_state;
3246
                                          else
3247
                                            next_state   <= indirect_state;
3248
                                          end if;
3249
 
3250
                                        when "0110" =>     -- ACCA,R
3251
                                     case md(6 downto 5) is
3252
                                          when "00" =>
3253
                                  left_ctrl  <= ix_left;
3254
                                          when "01" =>
3255
                                  left_ctrl  <= iy_left;
3256
                                          when "10" =>
3257
                                  left_ctrl  <= up_left;
3258
                                          when others =>
3259
                                          -- when "11" =>
3260
                                  left_ctrl  <= sp_left;
3261
                                          end case;
3262
                                     right_ctrl <= acca_right;
3263
                                     alu_ctrl   <= alu_add16;
3264
                 ea_ctrl    <= load_ea;
3265
                                          if md(4) = '0' then
3266
                                            next_state   <= saved_state;
3267
                                          else
3268
                                            next_state   <= indirect_state;
3269
                                          end if;
3270
 
3271
                                        when "0111" =>     -- undefined
3272
                                     case md(6 downto 5) is
3273
                                          when "00" =>
3274
                                  left_ctrl  <= ix_left;
3275
                                          when "01" =>
3276
                                  left_ctrl  <= iy_left;
3277
                                          when "10" =>
3278
                                  left_ctrl  <= up_left;
3279
                                          when others =>
3280
                                          -- when "11" =>
3281
                                  left_ctrl  <= sp_left;
3282
                                          end case;
3283
                                     right_ctrl <= zero_right;
3284
                                     alu_ctrl   <= alu_add16;
3285
                 ea_ctrl    <= load_ea;
3286
                                          if md(4) = '0' then
3287
                                            next_state   <= saved_state;
3288
                                          else
3289
                                            next_state   <= indirect_state;
3290
                                          end if;
3291
 
3292
                                        when "1000" =>     -- offset8,R
3293
                 md_ctrl    <= fetch_first_md; -- pick up 8 bit offset
3294
                 addr_ctrl  <= fetch_ad;
3295
                 pc_ctrl    <= incr_pc;
3296
                 next_state <= index8_state;
3297
 
3298
                                        when "1001" =>     -- offset16,R
3299
                 md_ctrl    <= fetch_first_md; -- pick up first byte of 16 bit offset
3300
                 addr_ctrl  <= fetch_ad;
3301
                 pc_ctrl    <= incr_pc;
3302
                 next_state <= index16_state;
3303
 
3304
                                        when "1010" =>     -- undefined
3305
                                     case md(6 downto 5) is
3306
                                          when "00" =>
3307
                                  left_ctrl  <= ix_left;
3308
                                          when "01" =>
3309
                                  left_ctrl  <= iy_left;
3310
                                          when "10" =>
3311
                                  left_ctrl  <= up_left;
3312
                                          when others =>
3313
                                          -- when "11" =>
3314
                                  left_ctrl  <= sp_left;
3315
                                          end case;
3316
                                     right_ctrl <= zero_right;
3317
                                     alu_ctrl   <= alu_add16;
3318
                 ea_ctrl    <= load_ea;
3319
                                          --
3320
                                          if md(4) = '0' then
3321
                                            next_state   <= saved_state;
3322
                                          else
3323
                                            next_state   <= indirect_state;
3324
                                          end if;
3325
 
3326
                                        when "1011" =>     -- ACCD,R
3327
                                     case md(6 downto 5) is
3328
                                          when "00" =>
3329
                                  left_ctrl  <= ix_left;
3330
                                          when "01" =>
3331
                                  left_ctrl  <= iy_left;
3332
                                          when "10" =>
3333
                                  left_ctrl  <= up_left;
3334
                                          when others =>
3335
                                          -- when "11" =>
3336
                                  left_ctrl  <= sp_left;
3337
                                          end case;
3338
                                     right_ctrl <= accd_right;
3339
                                     alu_ctrl   <= alu_add16;
3340
                 ea_ctrl    <= load_ea;
3341
                                          if md(4) = '0' then
3342
                                            next_state   <= saved_state;
3343
                                          else
3344
                                            next_state   <= indirect_state;
3345
                                          end if;
3346
 
3347
                                        when "1100" =>     -- offset8,PC
3348
                                          -- fetch 8 bit offset
3349
                 md_ctrl    <= fetch_first_md;
3350
                 addr_ctrl  <= fetch_ad;
3351
                 pc_ctrl    <= incr_pc;
3352
                 next_state <= pcrel8_state;
3353
 
3354
                                        when "1101" =>     -- offset16,PC
3355
                                          -- fetch offset
3356
                 md_ctrl    <= fetch_first_md;
3357
                 addr_ctrl  <= fetch_ad;
3358
                 pc_ctrl    <= incr_pc;
3359
                 next_state <= pcrel16_state;
3360
 
3361
                                        when "1110" =>     -- undefined
3362
                                     case md(6 downto 5) is
3363
                                          when "00" =>
3364
                                  left_ctrl  <= ix_left;
3365
                                          when "01" =>
3366
                                  left_ctrl  <= iy_left;
3367
                                          when "10" =>
3368
                                  left_ctrl  <= up_left;
3369
                                          when others =>
3370
                                          -- when "11" =>
3371
                                  left_ctrl  <= sp_left;
3372
                                          end case;
3373
                                     right_ctrl <= zero_right;
3374
                                     alu_ctrl   <= alu_add16;
3375
                 ea_ctrl    <= load_ea;
3376
                                          if md(4) = '0' then
3377
                                            next_state   <= saved_state;
3378
                                          else
3379
                                            next_state   <= indirect_state;
3380
                                          end if;
3381
 
3382
               when others =>
3383
--                      when "1111" =>     -- [,address]
3384
                                          -- advance PC to pick up address
3385
                 md_ctrl    <= fetch_first_md;
3386
                 addr_ctrl  <= fetch_ad;
3387
                                          pc_ctrl    <= incr_pc;
3388
                 next_state <= indexaddr_state;
3389
                                        end case;
3390
                                 end if;
3391
 
3392
                          -- load index register with ea plus one
3393
                          when postincr1_state =>
3394
                            left_ctrl  <= ea_left;
3395
                            right_ctrl <= one_right;
3396
                            alu_ctrl   <= alu_add16;
3397
                                 case md(6 downto 5) is
3398
                            when "00" =>
3399
               ix_ctrl    <= load_ix;
3400
                                 when "01" =>
3401
               iy_ctrl    <= load_iy;
3402
                                 when "10" =>
3403
               up_ctrl    <= load_up;
3404
                                 when others =>
3405
                                 -- when "11" =>
3406
               sp_ctrl    <= load_sp;
3407
                            end case;
3408
                                 -- return to previous state
3409
                            if md(4) = '0' then
3410
                                         next_state   <= saved_state;
3411
                                 else
3412
                                         next_state   <= indirect_state;
3413
                                 end if;
3414
 
3415
                          -- load index register with ea plus two
3416
                          when postincr2_state =>
3417
                                 -- increment register by two (address)
3418
                            left_ctrl  <= ea_left;
3419
                            right_ctrl <= two_right;
3420
                            alu_ctrl   <= alu_add16;
3421
                                 case md(6 downto 5) is
3422
                            when "00" =>
3423
               ix_ctrl    <= load_ix;
3424
                                 when "01" =>
3425
               iy_ctrl    <= load_iy;
3426
                                 when "10" =>
3427
               up_ctrl    <= load_up;
3428
                                 when others =>
3429
                                 -- when "11" =>
3430
               sp_ctrl    <= load_sp;
3431
                            end case;
3432
                                 -- return to previous state
3433
                            if md(4) = '0' then
3434
                                         next_state   <= saved_state;
3435
                                 else
3436
                                         next_state   <= indirect_state;
3437
                                 end if;
3438
           --
3439
                          -- ea = index register + md (8 bit signed offset)
3440
                          -- ea holds post byte
3441
                          --
3442
                          when index8_state =>
3443
                                 case ea(6 downto 5) is
3444
                            when "00" =>
3445
                              left_ctrl  <= ix_left;
3446
                            when "01" =>
3447
                              left_ctrl  <= iy_left;
3448
                                 when "10" =>
3449
                              left_ctrl  <= up_left;
3450
                                 when others =>
3451
                                 -- when "11" =>
3452
                              left_ctrl  <= sp_left;
3453
                                 end case;
3454
                                 -- ea = index reg + md
3455
                            right_ctrl <= md_sign8_right;
3456
                            alu_ctrl   <= alu_add16;
3457
             ea_ctrl    <= load_ea;
3458
                                 -- return to previous state
3459
                            if ea(4) = '0' then
3460
                                         next_state   <= saved_state;
3461
                                 else
3462
                                         next_state   <= indirect_state;
3463
                                 end if;
3464
 
3465
                          -- fetch low byte of 16 bit indexed offset
3466
                          when index16_state =>
3467
                                 -- advance pc
3468
             pc_ctrl    <= incr_pc;
3469
                                 -- fetch low byte
3470
             md_ctrl    <= fetch_next_md;
3471
             addr_ctrl  <= fetch_ad;
3472
                                 next_state <= index16_2_state;
3473
 
3474
                          -- ea = index register + md (16 bit offset)
3475
                          -- ea holds post byte
3476
                          when index16_2_state =>
3477
                                 case ea(6 downto 5) is
3478
                            when "00" =>
3479
                              left_ctrl  <= ix_left;
3480
                            when "01" =>
3481
                              left_ctrl  <= iy_left;
3482
                                 when "10" =>
3483
                              left_ctrl  <= up_left;
3484
                                 when others =>
3485
                                 -- when "11" =>
3486
                              left_ctrl  <= sp_left;
3487
                                 end case;
3488
                                 -- ea = index reg + md
3489
                            right_ctrl <= md_right;
3490
                            alu_ctrl   <= alu_add16;
3491
             ea_ctrl    <= load_ea;
3492
                                 -- return to previous state
3493
                            if ea(4) = '0' then
3494
                                         next_state   <= saved_state;
3495
                                 else
3496
                                         next_state   <= indirect_state;
3497
                                 end if;
3498
           --
3499
                          -- pc relative with 8 bit signed offest
3500
                          -- md holds signed offset
3501
                          --
3502
                          when pcrel8_state =>
3503
                                 -- ea = pc + signed md
3504
                            left_ctrl  <= pc_left;
3505
                            right_ctrl <= md_sign8_right;
3506
                            alu_ctrl   <= alu_add16;
3507
             ea_ctrl    <= load_ea;
3508
                                 -- return to previous state
3509
                            if ea(4) = '0' then
3510
                                         next_state   <= saved_state;
3511
                                 else
3512
                                         next_state   <= indirect_state;
3513
                                 end if;
3514
 
3515
                          -- pc relative addressing with 16 bit offset
3516
                          -- pick up the low byte of the offset in md
3517
                          -- advance the pc
3518
                          when pcrel16_state =>
3519
                                 -- advance pc
3520
             pc_ctrl    <= incr_pc;
3521
                                 -- fetch low byte
3522
             md_ctrl    <= fetch_next_md;
3523
             addr_ctrl  <= fetch_ad;
3524
                                 next_state <= pcrel16_2_state;
3525
 
3526
                          -- pc relative with16 bit signed offest
3527
                          -- md holds signed offset
3528
                          when pcrel16_2_state =>
3529
                                 -- ea = pc +  md
3530
                            left_ctrl  <= pc_left;
3531
                            right_ctrl <= md_right;
3532
                            alu_ctrl   <= alu_add16;
3533
             ea_ctrl    <= load_ea;
3534
                                 -- return to previous state
3535
                            if ea(4) = '0' then
3536
                                         next_state   <= saved_state;
3537
                                 else
3538
                                         next_state   <= indirect_state;
3539
                                 end if;
3540
 
3541
                          -- indexed to address
3542
                          -- pick up the low byte of the address
3543
                          -- advance the pc
3544
                          when indexaddr_state =>
3545
                                 -- advance pc
3546
             pc_ctrl    <= incr_pc;
3547
                                 -- fetch low byte
3548
             md_ctrl    <= fetch_next_md;
3549
             addr_ctrl  <= fetch_ad;
3550
                                 next_state <= indexaddr2_state;
3551
 
3552
                          -- indexed to absolute address
3553
                          -- md holds address
3554
                          -- ea hold indexing mode byte
3555
                          when indexaddr2_state =>
3556
                                 -- ea = md
3557
                            left_ctrl  <= pc_left;
3558
                            right_ctrl <= md_right;
3559
                            alu_ctrl   <= alu_ld16;
3560
             ea_ctrl    <= load_ea;
3561
                                 -- return to previous state
3562
                            if ea(4) = '0' then
3563
                                         next_state   <= saved_state;
3564
                                 else
3565
                                         next_state   <= indirect_state;
3566
                                 end if;
3567
 
3568
           --
3569
                          -- load md with high byte of indirect address
3570
                          -- pointed to by ea
3571
                          -- increment ea
3572
                          --
3573
                          when indirect_state =>
3574
                                 -- increment ea
3575
                            left_ctrl  <= ea_left;
3576
                            right_ctrl <= one_right;
3577
                            alu_ctrl   <= alu_add16;
3578
             ea_ctrl    <= load_ea;
3579
                                 -- fetch high byte
3580
             md_ctrl    <= fetch_first_md;
3581
             addr_ctrl  <= read_ad;
3582
                                 next_state <= indirect2_state;
3583
           --
3584
                          -- load md with low byte of indirect address
3585
                          -- pointed to by ea
3586
                          -- ea has previously been incremented
3587
                          --
3588
                          when indirect2_state =>
3589
                                 -- fetch high byte
3590
             md_ctrl    <= fetch_next_md;
3591
             addr_ctrl  <= read_ad;
3592
             dout_ctrl  <= md_lo_dout;
3593
                                 next_state <= indirect3_state;
3594
                          --
3595
                          -- complete idirect addressing
3596
                          -- by loading ea with md
3597
                          --
3598
                          when indirect3_state =>
3599
                                 -- load ea with md
3600
                            left_ctrl  <= ea_left;
3601
                            right_ctrl <= md_right;
3602
                            alu_ctrl   <= alu_ld16;
3603
             ea_ctrl    <= load_ea;
3604
                                 -- return to previous state
3605
                                 next_state   <= saved_state;
3606
 
3607
           --
3608
                          -- ea holds the low byte of the absolute address
3609
                          -- Move ea low byte into ea high byte
3610
                          -- load new ea low byte to for absolute 16 bit address
3611
                          -- advance the program counter
3612
                          --
3613
                          when extended_state => -- fetch ea low byte
3614
                                        -- increment pc
3615
               pc_ctrl      <= incr_pc;
3616
                                        -- fetch next effective address bytes
3617
                                        ea_ctrl      <= fetch_next_ea;
3618
               addr_ctrl    <= fetch_ad;
3619
                                   -- return to previous state
3620
                                   next_state   <= saved_state;
3621
 
3622
                                when lea_state => -- here on load effective address
3623
                                        -- load index register with effective address
3624
               left_ctrl  <= pc_left;
3625
                                        right_ctrl <= ea_right;
3626
                                   alu_ctrl   <= alu_lea;
3627
                                        case op_code(3 downto 0) is
3628
                                        when "0000" => -- leax
3629
                   cc_ctrl    <= load_cc;
3630
                   ix_ctrl    <= load_ix;
3631
                                        when "0001" => -- leay
3632
                   cc_ctrl    <= load_cc;
3633
                   iy_ctrl    <= load_iy;
3634
                                        when "0010" => -- leas
3635
                   sp_ctrl    <= load_sp;
3636
                                        when "0011" => -- leau
3637
                   up_ctrl    <= load_up;
3638
                                        when others =>
3639
                                            null;
3640
                                        end case;
3641
               lic          <= '1';
3642
               next_state   <= fetch_state;
3643
 
3644
                                --
3645
                                -- jump to subroutine
3646
                                -- sp=sp-1
3647
                                -- call push_return_lo_state to save pc
3648
                                -- return to jmp_state
3649
                                --
3650
                                when jsr_state =>
3651
                                        -- decrement sp
3652
               left_ctrl    <= sp_left;
3653
                                        right_ctrl   <= one_right;
3654
                                   alu_ctrl     <= alu_sub16;
3655
               sp_ctrl      <= load_sp;
3656
                                        -- call push_return_state
3657
                                        st_ctrl      <= push_st;
3658
                                        return_state <= jmp_state;
3659
               next_state   <= push_return_lo_state;
3660
 
3661
                                --
3662
                                -- Load pc with ea
3663
                                -- (JMP)
3664
                                --
3665
                                when jmp_state =>
3666
                                        -- load PC with effective address
3667
               left_ctrl  <= pc_left;
3668
                                        right_ctrl <= ea_right;
3669
                                   alu_ctrl   <= alu_ld16;
3670
                                        pc_ctrl    <= load_pc;
3671
               lic        <= '1';
3672
               next_state <= fetch_state;
3673
 
3674
                                --
3675
                                -- long branch or branch to subroutine
3676
                                -- pick up next md byte
3677
                                -- md_hi = md_lo
3678
                                -- md_lo = (pc)
3679
                                -- pc=pc+1
3680
                                -- if a lbsr push return address
3681
                                -- continue to sbranch_state
3682
                                -- to evaluate conditional branches
3683
                                --
3684
                                when lbranch_state =>
3685
                                        pc_ctrl    <= incr_pc;
3686
                                        -- fetch the next byte into md_lo
3687
               md_ctrl    <= fetch_next_md;
3688
               addr_ctrl  <= fetch_ad;
3689
                                        -- if lbsr - push return address
3690
                                        -- then continue on to short branch
3691
                                        if op_code = "00010111" then
3692
                                          st_ctrl      <= push_st;
3693
                                          return_state <= sbranch_state;
3694
                 next_state   <= push_return_lo_state;
3695
                                        else
3696
                 next_state   <= sbranch_state;
3697
                                        end if;
3698
 
3699
                                 --
3700
                                 -- here to execute conditional branch
3701
                                 -- short conditional branch md = signed 8 bit offset
3702
                                 -- long branch md = 16 bit offset
3703
                                 -- 
3704
                  when sbranch_state =>
3705
               left_ctrl  <= pc_left;
3706
                                        right_ctrl <= md_right;
3707
                                   alu_ctrl   <= alu_add16;
3708
                                        -- Test condition for branch
3709
                              if op_code(7 downto 4) = "0010" then -- conditional branch
3710
                 case op_code(3 downto 0) is
3711
                           when "0000" => -- bra
3712
                                            cond_true := (1 = 1);
3713
                           when "0001" => -- brn
3714
                                            cond_true := (1 = 0);
3715
                           when "0010" => -- bhi
3716
                                            cond_true := ((cc(CBIT) or cc(ZBIT)) = '0');
3717
                           when "0011" => -- bls
3718
                                            cond_true := ((cc(CBIT) or cc(ZBIT)) = '1');
3719
                           when "0100" => -- bcc/bhs
3720
                                            cond_true := (cc(CBIT) = '0');
3721
                           when "0101" => -- bcs/blo
3722
                                            cond_true := (cc(CBIT) = '1');
3723
                           when "0110" => -- bne
3724
                             cond_true := (cc(ZBIT) = '0');
3725
                           when "0111" => -- beq
3726
                                            cond_true := (cc(ZBIT) = '1');
3727
                           when "1000" => -- bvc
3728
                                            cond_true := (cc(VBIT) = '0');
3729
                           when "1001" => -- bvs
3730
                                            cond_true := (cc(VBIT) = '1');
3731
                           when "1010" => -- bpl
3732
                                            cond_true := (cc(NBIT) = '0');
3733
                           when "1011" => -- bmi
3734
                                            cond_true := (cc(NBIT) = '1');
3735
                           when "1100" => -- bge
3736
                                            cond_true := ((cc(NBIT) xor cc(VBIT)) = '0');
3737
                           when "1101" => -- blt
3738
                                            cond_true := ((cc(NBIT) xor cc(VBIT)) = '1');
3739
                           when "1110" => -- bgt
3740
                                            cond_true := ((cc(ZBIT) or (cc(NBIT) xor cc(VBIT))) = '0');
3741
                           when "1111" => -- ble
3742
                                            cond_true := ((cc(ZBIT) or (cc(NBIT) xor cc(VBIT))) = '1');
3743
                           when others =>
3744
                                            null;
3745
                           end case;
3746
                                   end if;
3747
                                        if cond_true then
3748
                                          pc_ctrl    <= load_pc;
3749
               end if;
3750
               lic          <= '1';
3751
                                        next_state   <= fetch_state;
3752
 
3753
                                 --
3754
                                 -- push return address onto the S stack
3755
                                 --
3756
                                 -- (sp) = pc_lo
3757
                                 -- sp = sp - 1
3758
                                 --
3759
                                 when push_return_lo_state =>
3760
                                          -- decrement the sp
3761
                 left_ctrl  <= sp_left;
3762
                 right_ctrl <= one_right;
3763
                 alu_ctrl   <= alu_sub16;
3764
                 sp_ctrl    <= load_sp;
3765
                 -- write PC low
3766
                 addr_ctrl  <= pushs_ad;
3767
                 dout_ctrl  <= pc_lo_dout;
3768
                 next_state <= push_return_hi_state;
3769
 
3770
                                --
3771
                                -- push program counter hi byte onto the stack
3772
                                -- (sp) = pc_hi
3773
                                -- sp = sp
3774
                                -- return to originating state
3775
                                --
3776
                                when push_return_hi_state =>
3777
                                          -- write pc hi bytes
3778
                 addr_ctrl    <= pushs_ad;
3779
                 dout_ctrl    <= pc_hi_dout;
3780
                 next_state   <= saved_state;
3781
 
3782
             --
3783
             -- RTS pull return address from stack
3784
             --
3785
                                 when pull_return_hi_state =>
3786
                                          -- increment the sp
3787
                 left_ctrl  <= sp_left;
3788
                 right_ctrl <= one_right;
3789
                 alu_ctrl   <= alu_add16;
3790
                 sp_ctrl    <= load_sp;
3791
                 -- read pc hi
3792
                                          pc_ctrl    <= pull_hi_pc;
3793
                 addr_ctrl  <= pulls_ad;
3794
                 next_state <= pull_return_lo_state;
3795
 
3796
                                when pull_return_lo_state =>
3797
                                          -- increment the SP
3798
                 left_ctrl  <= sp_left;
3799
                 right_ctrl <= one_right;
3800
                 alu_ctrl   <= alu_add16;
3801
                 sp_ctrl    <= load_sp;
3802
                                          -- read pc low
3803
                                          pc_ctrl    <= pull_lo_pc;
3804
                 addr_ctrl  <= pulls_ad;
3805
                 dout_ctrl  <= pc_lo_dout;
3806
                                          --
3807
                 lic          <= '1';
3808
                 next_state   <= fetch_state;
3809
 
3810
                                 when andcc_state =>
3811
                                          -- AND CC with md
3812
                 left_ctrl  <= md_left;
3813
                 right_ctrl <= zero_right;
3814
                 alu_ctrl   <= alu_andcc;
3815
                 cc_ctrl    <= load_cc;
3816
                                          --
3817
                 lic        <= '1';
3818
                                     next_state <= fetch_state;
3819
 
3820
                                 when orcc_state =>
3821
                                          -- OR CC with md
3822
                 left_ctrl  <= md_left;
3823
                 right_ctrl <= zero_right;
3824
                 alu_ctrl   <= alu_orcc;
3825
                 cc_ctrl    <= load_cc;
3826
                                          --
3827
                 lic        <= '1';
3828
                                     next_state <= fetch_state;
3829
 
3830
                                 when tfr_state =>
3831
                                          -- select source register
3832
                                          case md(7 downto 4) is
3833
                                          when "0000" =>
3834
                                            left_ctrl <= accd_left;
3835
                                          when "0001" =>
3836
                                            left_ctrl <= ix_left;
3837
                                          when "0010" =>
3838
                                            left_ctrl <= iy_left;
3839
                                          when "0011" =>
3840
                                            left_ctrl <= up_left;
3841
                                          when "0100" =>
3842
                                            left_ctrl <= sp_left;
3843
                                          when "0101" =>
3844
                                            left_ctrl <= pc_left;
3845
                                          when "1000" =>
3846
                                            left_ctrl <= acca_left;
3847
                                          when "1001" =>
3848
                                            left_ctrl <= accb_left;
3849
                                          when "1010" =>
3850
                                            left_ctrl <= cc_left;
3851
                                          when "1011" =>
3852
                                            left_ctrl <= dp_left;
3853
                                          when others =>
3854
                   left_ctrl  <= md_left;
3855
                                          end case;
3856
                 right_ctrl <= zero_right;
3857
                 alu_ctrl   <= alu_tfr;
3858
                                          -- select destination register
3859
                                          case md(3 downto 0) is
3860
                                          when "0000" => -- accd
3861
                   acca_ctrl  <= load_hi_acca;
3862
                   accb_ctrl  <= load_accb;
3863
                                          when "0001" => -- ix
3864
                   ix_ctrl    <= load_ix;
3865
                                          when "0010" => -- iy
3866
                   iy_ctrl    <= load_iy;
3867
                                          when "0011" => -- up
3868
                   up_ctrl    <= load_up;
3869
                                          when "0100" => -- sp
3870
                   sp_ctrl    <= load_sp;
3871
                                          when "0101" => -- pc
3872
                   pc_ctrl    <= load_pc;
3873
                                          when "1000" => -- acca
3874
                   acca_ctrl  <= load_acca;
3875
                                          when "1001" => -- accb
3876
                   accb_ctrl  <= load_accb;
3877
                                          when "1010" => -- cc
3878
                   cc_ctrl    <= load_cc;
3879
                                          when "1011" => --dp
3880
                   dp_ctrl    <= load_dp;
3881
                                          when others =>
3882
                                            null;
3883
                                          end case;
3884
                                          --
3885
                 lic          <= '1';
3886
                                     next_state   <= fetch_state;
3887
 
3888
                                 when exg_state =>
3889
                                          -- save destination register
3890
                                          case md(3 downto 0) is
3891
                                          when "0000" =>
3892
                                            left_ctrl <= accd_left;
3893
                                          when "0001" =>
3894
                                            left_ctrl <= ix_left;
3895
                                          when "0010" =>
3896
                                            left_ctrl <= iy_left;
3897
                                          when "0011" =>
3898
                                            left_ctrl <= up_left;
3899
                                          when "0100" =>
3900
                                            left_ctrl <= sp_left;
3901
                                          when "0101" =>
3902
                                            left_ctrl <= pc_left;
3903
                                          when "1000" =>
3904
                                            left_ctrl <= acca_left;
3905
                                          when "1001" =>
3906
                                            left_ctrl <= accb_left;
3907
                                          when "1010" =>
3908
                                            left_ctrl <= cc_left;
3909
                                          when "1011" =>
3910
                                            left_ctrl <= dp_left;
3911
                                          when others =>
3912
                   left_ctrl  <= md_left;
3913
                                          end case;
3914
                 right_ctrl <= zero_right;
3915
                 alu_ctrl   <= alu_tfr;
3916
                 ea_ctrl    <= load_ea;
3917
                                          -- call tranfer microcode
3918
                                     next_state   <= exg1_state;
3919
 
3920
                                 when exg1_state =>
3921
                                          -- select source register
3922
                                          case md(7 downto 4) is
3923
                                          when "0000" =>
3924
                                            left_ctrl <= accd_left;
3925
                                          when "0001" =>
3926
                                            left_ctrl <= ix_left;
3927
                                          when "0010" =>
3928
                                            left_ctrl <= iy_left;
3929
                                          when "0011" =>
3930
                                            left_ctrl <= up_left;
3931
                                          when "0100" =>
3932
                                            left_ctrl <= sp_left;
3933
                                          when "0101" =>
3934
                                            left_ctrl <= pc_left;
3935
                                          when "1000" =>
3936
                                            left_ctrl <= acca_left;
3937
                                          when "1001" =>
3938
                                            left_ctrl <= accb_left;
3939
                                          when "1010" =>
3940
                                            left_ctrl <= cc_left;
3941
                                          when "1011" =>
3942
                                            left_ctrl <= dp_left;
3943
                                          when others =>
3944
                   left_ctrl  <= md_left;
3945
                                          end case;
3946
                 right_ctrl <= zero_right;
3947
                 alu_ctrl   <= alu_tfr;
3948
                                          -- select destination register
3949
                                          case md(3 downto 0) is
3950
                                          when "0000" => -- accd
3951
                   acca_ctrl  <= load_hi_acca;
3952
                   accb_ctrl  <= load_accb;
3953
                                          when "0001" => -- ix
3954
                   ix_ctrl    <= load_ix;
3955
                                          when "0010" => -- iy
3956
                   iy_ctrl    <= load_iy;
3957
                                          when "0011" => -- up
3958
                   up_ctrl    <= load_up;
3959
                                          when "0100" => -- sp
3960
                   sp_ctrl    <= load_sp;
3961
                                          when "0101" => -- pc
3962
                   pc_ctrl    <= load_pc;
3963
                                          when "1000" => -- acca
3964
                   acca_ctrl  <= load_acca;
3965
                                          when "1001" => -- accb
3966
                   accb_ctrl  <= load_accb;
3967
                                          when "1010" => -- cc
3968
                   cc_ctrl    <= load_cc;
3969
                                          when "1011" => --dp
3970
                   dp_ctrl    <= load_dp;
3971
                                          when others =>
3972
                                            null;
3973
                                          end case;
3974
                                     next_state   <= exg2_state;
3975
 
3976
                                 when exg2_state =>
3977
                                          -- restore destination
3978
                 left_ctrl  <= ea_left;
3979
                 right_ctrl <= zero_right;
3980
                 alu_ctrl   <= alu_tfr;
3981
                                          -- save as source register
3982
                                          case md(7 downto 4) is
3983
                                          when "0000" => -- accd
3984
                   acca_ctrl  <= load_hi_acca;
3985
                   accb_ctrl  <= load_accb;
3986
                                          when "0001" => -- ix
3987
                   ix_ctrl    <= load_ix;
3988
                                          when "0010" => -- iy
3989
                   iy_ctrl    <= load_iy;
3990
                                          when "0011" => -- up
3991
                   up_ctrl    <= load_up;
3992
                                          when "0100" => -- sp
3993
                   sp_ctrl    <= load_sp;
3994
                                          when "0101" => -- pc
3995
                   pc_ctrl    <= load_pc;
3996
                                          when "1000" => -- acca
3997
                   acca_ctrl  <= load_acca;
3998
                                          when "1001" => -- accb
3999
                   accb_ctrl  <= load_accb;
4000
                                          when "1010" => -- cc
4001
                   cc_ctrl    <= load_cc;
4002
                                          when "1011" => --dp
4003
                   dp_ctrl    <= load_dp;
4004
                                          when others =>
4005
                                            null;
4006
                                          end case;
4007
                 lic          <= '1';
4008
                                     next_state   <= fetch_state;
4009
 
4010
                                 when mulea_state =>
4011
                                          -- move accb to ea
4012
                 left_ctrl  <= accb_left;
4013
                 right_ctrl <= zero_right;
4014
                 alu_ctrl   <= alu_st16;
4015
                 ea_ctrl    <= load_ea;
4016
                                     next_state <= muld_state;
4017
 
4018
                                 when muld_state =>
4019
                                          -- clear accd
4020
                 left_ctrl  <= acca_left;
4021
                 right_ctrl <= zero_right;
4022
                 alu_ctrl   <= alu_ld8;
4023
                 acca_ctrl  <= load_hi_acca;
4024
                 accb_ctrl  <= load_accb;
4025
                                     next_state <= mul0_state;
4026
 
4027
                                 when mul0_state =>
4028
                                          -- if bit 0 of ea set, add accd to md
4029
                 left_ctrl  <= accd_left;
4030
                                          if ea(0) = '1' then
4031
                   right_ctrl <= md_right;
4032
                                          else
4033
                   right_ctrl <= zero_right;
4034
                                          end if;
4035
                 alu_ctrl   <= alu_mul;
4036
                 cc_ctrl    <= load_cc;
4037
                 acca_ctrl  <= load_hi_acca;
4038
                 accb_ctrl  <= load_accb;
4039
                 md_ctrl    <= shiftl_md;
4040
                                     next_state <= mul1_state;
4041
 
4042
                                 when mul1_state =>
4043
                                          -- if bit 1 of ea set, add accd to md
4044
                 left_ctrl  <= accd_left;
4045
                                          if ea(1) = '1' then
4046
                   right_ctrl <= md_right;
4047
                                          else
4048
                   right_ctrl <= zero_right;
4049
                                          end if;
4050
                 alu_ctrl   <= alu_mul;
4051
                 cc_ctrl    <= load_cc;
4052
                 acca_ctrl  <= load_hi_acca;
4053
                 accb_ctrl  <= load_accb;
4054
                 md_ctrl    <= shiftl_md;
4055
                                     next_state <= mul2_state;
4056
 
4057
                                 when mul2_state =>
4058
                                          -- if bit 2 of ea set, add accd to md
4059
                 left_ctrl  <= accd_left;
4060
                                          if ea(2) = '1' then
4061
                   right_ctrl <= md_right;
4062
                                          else
4063
                   right_ctrl <= zero_right;
4064
                                          end if;
4065
                 alu_ctrl   <= alu_mul;
4066
                 cc_ctrl    <= load_cc;
4067
                 acca_ctrl  <= load_hi_acca;
4068
                 accb_ctrl  <= load_accb;
4069
                 md_ctrl    <= shiftl_md;
4070
                                     next_state <= mul3_state;
4071
 
4072
                                 when mul3_state =>
4073
                                          -- if bit 3 of ea set, add accd to md
4074
                 left_ctrl  <= accd_left;
4075
                                          if ea(3) = '1' then
4076
                   right_ctrl <= md_right;
4077
                                          else
4078
                   right_ctrl <= zero_right;
4079
                                          end if;
4080
                 alu_ctrl   <= alu_mul;
4081
                 cc_ctrl    <= load_cc;
4082
                 acca_ctrl  <= load_hi_acca;
4083
                 accb_ctrl  <= load_accb;
4084
                 md_ctrl    <= shiftl_md;
4085
                                     next_state <= mul4_state;
4086
 
4087
                                 when mul4_state =>
4088
                                          -- if bit 4 of ea set, add accd to md
4089
                 left_ctrl  <= accd_left;
4090
                                          if ea(4) = '1' then
4091
                   right_ctrl <= md_right;
4092
                                          else
4093
                   right_ctrl <= zero_right;
4094
                                          end if;
4095
                 alu_ctrl   <= alu_mul;
4096
                 cc_ctrl    <= load_cc;
4097
                 acca_ctrl  <= load_hi_acca;
4098
                 accb_ctrl  <= load_accb;
4099
                 md_ctrl    <= shiftl_md;
4100
                                     next_state <= mul5_state;
4101
 
4102
                                 when mul5_state =>
4103
                                          -- if bit 5 of ea set, add accd to md
4104
                 left_ctrl  <= accd_left;
4105
                                          if ea(5) = '1' then
4106
                   right_ctrl <= md_right;
4107
                                          else
4108
                   right_ctrl <= zero_right;
4109
                                          end if;
4110
                 alu_ctrl   <= alu_mul;
4111
                 cc_ctrl    <= load_cc;
4112
                 acca_ctrl  <= load_hi_acca;
4113
                 accb_ctrl  <= load_accb;
4114
                 md_ctrl    <= shiftl_md;
4115
                                     next_state <= mul6_state;
4116
 
4117
                                 when mul6_state =>
4118
                                          -- if bit 6 of ea set, add accd to md
4119
                 left_ctrl  <= accd_left;
4120
                                          if ea(6) = '1' then
4121
                   right_ctrl <= md_right;
4122
                                          else
4123
                   right_ctrl <= zero_right;
4124
                                          end if;
4125
                 alu_ctrl   <= alu_mul;
4126
                 cc_ctrl    <= load_cc;
4127
                 acca_ctrl  <= load_hi_acca;
4128
                 accb_ctrl  <= load_accb;
4129
                 md_ctrl    <= shiftl_md;
4130
                 lic        <= '1';
4131
                                     next_state <= fetch_state;
4132
 
4133
                          --
4134
                          -- Enter here on pushs
4135
                          -- ea holds post byte
4136
                          --
4137
                          when pshs_state =>
4138
             -- decrement sp if any registers to be pushed
4139
             left_ctrl  <= sp_left;
4140
             right_ctrl <= one_right;
4141
             alu_ctrl   <= alu_sub16;
4142
                                 -- idle        address
4143
             addr_ctrl  <= idle_ad;
4144
                            dout_ctrl  <= cc_dout;
4145
                                 if ea(7 downto 0) = "00000000" then
4146
               sp_ctrl    <= latch_sp;
4147
                                 else
4148
               sp_ctrl    <= load_sp;
4149
                                 end if;
4150
                                 if ea(7) = '1' then
4151
               next_state <= pshs_pcl_state;
4152
                                 elsif ea(6) = '1' then
4153
               next_state <= pshs_upl_state;
4154
                                 elsif ea(5) = '1' then
4155
                                   next_state <= pshs_iyl_state;
4156
                                 elsif ea(4) = '1' then
4157
                                   next_state <= pshs_ixl_state;
4158
                                 elsif ea(3) = '1' then
4159
                                   next_state <= pshs_dp_state;
4160
                                 elsif ea(2) = '1' then
4161
                                   next_state <= pshs_accb_state;
4162
                                 elsif ea(1) = '1' then
4163
                                   next_state <= pshs_acca_state;
4164
                                 elsif ea(0) = '1' then
4165
                                   next_state <= pshs_cc_state;
4166
                                 else
4167
               lic        <= '1';
4168
                                   next_state <= fetch_state;
4169
                                 end if;
4170
 
4171
                          when pshs_pcl_state =>
4172
             -- decrement sp
4173
             left_ctrl  <= sp_left;
4174
             right_ctrl <= one_right;
4175
             alu_ctrl   <= alu_sub16;
4176
             sp_ctrl    <= load_sp;
4177
                                 -- write pc low
4178
             addr_ctrl  <= pushs_ad;
4179
                            dout_ctrl  <= pc_lo_dout;
4180
             next_state <= pshs_pch_state;
4181
 
4182
                          when pshs_pch_state =>
4183
             -- decrement sp
4184
             left_ctrl  <= sp_left;
4185
             right_ctrl <= one_right;
4186
             alu_ctrl   <= alu_sub16;
4187
                                 if ea(6 downto 0) = "0000000" then
4188
               sp_ctrl    <= latch_sp;
4189
                                 else
4190
               sp_ctrl    <= load_sp;
4191
                                 end if;
4192
                                 -- write pc hi
4193
             addr_ctrl  <= pushs_ad;
4194
                            dout_ctrl  <= pc_hi_dout;
4195
                                 if ea(6) = '1' then
4196
               next_state <= pshs_upl_state;
4197
                                 elsif ea(5) = '1' then
4198
                                   next_state <= pshs_iyl_state;
4199
                                 elsif ea(4) = '1' then
4200
                                   next_state <= pshs_ixl_state;
4201
                                 elsif ea(3) = '1' then
4202
                                   next_state <= pshs_dp_state;
4203
                                 elsif ea(2) = '1' then
4204
                                   next_state <= pshs_accb_state;
4205
                                 elsif ea(1) = '1' then
4206
                                   next_state <= pshs_acca_state;
4207
                                 elsif ea(0) = '1' then
4208
                                   next_state <= pshs_cc_state;
4209
                                 else
4210
               lic        <= '1';
4211
                                   next_state <= fetch_state;
4212
                                 end if;
4213
 
4214
 
4215
                          when pshs_upl_state =>
4216
             -- decrement sp
4217
             left_ctrl  <= sp_left;
4218
             right_ctrl <= one_right;
4219
             alu_ctrl   <= alu_sub16;
4220
             sp_ctrl    <= load_sp;
4221
                                 -- write pc low
4222
             addr_ctrl  <= pushs_ad;
4223
                            dout_ctrl  <= up_lo_dout;
4224
             next_state <= pshs_uph_state;
4225
 
4226
                          when pshs_uph_state =>
4227
             -- decrement sp
4228
             left_ctrl  <= sp_left;
4229
             right_ctrl <= one_right;
4230
             alu_ctrl   <= alu_sub16;
4231
                                 if ea(5 downto 0) = "000000" then
4232
               sp_ctrl    <= latch_sp;
4233
                                 else
4234
               sp_ctrl    <= load_sp;
4235
                                 end if;
4236
                                 -- write pc hi
4237
             addr_ctrl  <= pushs_ad;
4238
                            dout_ctrl  <= up_hi_dout;
4239
                                 if ea(5) = '1' then
4240
                                   next_state   <= pshs_iyl_state;
4241
                                 elsif ea(4) = '1' then
4242
                                   next_state   <= pshs_ixl_state;
4243
                                 elsif ea(3) = '1' then
4244
                                   next_state   <= pshs_dp_state;
4245
                                 elsif ea(2) = '1' then
4246
                                   next_state   <= pshs_accb_state;
4247
                                 elsif ea(1) = '1' then
4248
                                   next_state   <= pshs_acca_state;
4249
                                 elsif ea(0) = '1' then
4250
                                   next_state   <= pshs_cc_state;
4251
                                 else
4252
               lic          <= '1';
4253
                                   next_state   <= fetch_state;
4254
                                 end if;
4255
 
4256
                          when pshs_iyl_state =>
4257
             -- decrement sp
4258
             left_ctrl  <= sp_left;
4259
             right_ctrl <= one_right;
4260
             alu_ctrl   <= alu_sub16;
4261
             sp_ctrl    <= load_sp;
4262
                                 -- write iy low
4263
             addr_ctrl  <= pushs_ad;
4264
                            dout_ctrl  <= iy_lo_dout;
4265
             next_state <= pshs_iyh_state;
4266
 
4267
                          when pshs_iyh_state =>
4268
             -- decrement sp
4269
             left_ctrl  <= sp_left;
4270
             right_ctrl <= one_right;
4271
             alu_ctrl   <= alu_sub16;
4272
                                 if ea(4 downto 0) = "00000" then
4273
               sp_ctrl    <= latch_sp;
4274
                                 else
4275
               sp_ctrl    <= load_sp;
4276
                                 end if;
4277
                                 -- write iy hi
4278
             addr_ctrl  <= pushs_ad;
4279
                            dout_ctrl  <= iy_hi_dout;
4280
                                 if ea(4) = '1' then
4281
                                   next_state   <= pshs_ixl_state;
4282
                                 elsif ea(3) = '1' then
4283
                                   next_state   <= pshs_dp_state;
4284
                                 elsif ea(2) = '1' then
4285
                                   next_state   <= pshs_accb_state;
4286
                                 elsif ea(1) = '1' then
4287
                                   next_state   <= pshs_acca_state;
4288
                                 elsif ea(0) = '1' then
4289
                                   next_state   <= pshs_cc_state;
4290
                                 else
4291
               lic          <= '1';
4292
                                   next_state   <= fetch_state;
4293
                                 end if;
4294
 
4295
                          when pshs_ixl_state =>
4296
             -- decrement sp
4297
             left_ctrl  <= sp_left;
4298
             right_ctrl <= one_right;
4299
             alu_ctrl   <= alu_sub16;
4300
             sp_ctrl    <= load_sp;
4301
                                 -- write ix low
4302
             addr_ctrl  <= pushs_ad;
4303
                            dout_ctrl  <= ix_lo_dout;
4304
             next_state <= pshs_ixh_state;
4305
 
4306
                          when pshs_ixh_state =>
4307
             -- decrement sp
4308
             left_ctrl  <= sp_left;
4309
             right_ctrl <= one_right;
4310
             alu_ctrl   <= alu_sub16;
4311
                                 if ea(3 downto 0) = "0000" then
4312
               sp_ctrl    <= latch_sp;
4313
                                 else
4314
               sp_ctrl    <= load_sp;
4315
                                 end if;
4316
                                 -- write ix hi
4317
             addr_ctrl  <= pushs_ad;
4318
                            dout_ctrl  <= ix_hi_dout;
4319
                                 if ea(3) = '1' then
4320
                                   next_state   <= pshs_dp_state;
4321
                                 elsif ea(2) = '1' then
4322
                                   next_state   <= pshs_accb_state;
4323
                                 elsif ea(1) = '1' then
4324
                                   next_state   <= pshs_acca_state;
4325
                                 elsif ea(0) = '1' then
4326
                                   next_state   <= pshs_cc_state;
4327
                                 else
4328
               lic          <= '1';
4329
                                   next_state   <= fetch_state;
4330
                                 end if;
4331
 
4332
                          when pshs_dp_state =>
4333
             -- decrement sp
4334
             left_ctrl  <= sp_left;
4335
             right_ctrl <= one_right;
4336
             alu_ctrl   <= alu_sub16;
4337
                                 if ea(2 downto 0) = "000" then
4338
               sp_ctrl    <= latch_sp;
4339
                                 else
4340
               sp_ctrl    <= load_sp;
4341
                                 end if;
4342
                                 -- write dp
4343
             addr_ctrl  <= pushs_ad;
4344
                            dout_ctrl  <= dp_dout;
4345
                                 if ea(2) = '1' then
4346
                                   next_state   <= pshs_accb_state;
4347
                                 elsif ea(1) = '1' then
4348
                                   next_state   <= pshs_acca_state;
4349
                                 elsif ea(0) = '1' then
4350
                                   next_state   <= pshs_cc_state;
4351
                                 else
4352
               lic          <= '1';
4353
                                   next_state   <= fetch_state;
4354
                                 end if;
4355
 
4356
                          when pshs_accb_state =>
4357
             -- decrement sp
4358
             left_ctrl  <= sp_left;
4359
             right_ctrl <= one_right;
4360
             alu_ctrl   <= alu_sub16;
4361
                                 if ea(1 downto 0) = "00" then
4362
               sp_ctrl    <= latch_sp;
4363
                                 else
4364
               sp_ctrl    <= load_sp;
4365
                                 end if;
4366
                                 -- write accb
4367
             addr_ctrl  <= pushs_ad;
4368
                            dout_ctrl  <= accb_dout;
4369
                                 if ea(1) = '1' then
4370
                                   next_state   <= pshs_acca_state;
4371
                                 elsif ea(0) = '1' then
4372
                                   next_state   <= pshs_cc_state;
4373
                                 else
4374
               lic          <= '1';
4375
                                   next_state   <= fetch_state;
4376
                                 end if;
4377
 
4378
                          when pshs_acca_state =>
4379
             -- decrement sp
4380
             left_ctrl  <= sp_left;
4381
             right_ctrl <= one_right;
4382
             alu_ctrl   <= alu_sub16;
4383
                                 if ea(0) = '1' then
4384
               sp_ctrl    <= load_sp;
4385
                                 else
4386
               sp_ctrl    <= latch_sp;
4387
                                 end if;
4388
                                 -- write acca
4389
             addr_ctrl  <= pushs_ad;
4390
                            dout_ctrl  <= acca_dout;
4391
                                 if ea(0) = '1' then
4392
                                   next_state   <= pshs_cc_state;
4393
                                 else
4394
               lic          <= '1';
4395
                                   next_state   <= fetch_state;
4396
                                 end if;
4397
 
4398
                          when pshs_cc_state =>
4399
             -- idle sp
4400
                                 -- write cc
4401
             addr_ctrl  <= pushs_ad;
4402
                            dout_ctrl  <= cc_dout;
4403
             lic          <= '1';
4404
             next_state <= fetch_state;
4405
 
4406
                          --
4407
                          -- enter here on PULS
4408
                          -- ea hold register mask
4409
                          --
4410
                          when puls_state =>
4411
                                 if ea(0) = '1' then
4412
                                   next_state <= puls_cc_state;
4413
                                 elsif ea(1) = '1' then
4414
                                   next_state <= puls_acca_state;
4415
                                 elsif ea(2) = '1' then
4416
                                   next_state <= puls_accb_state;
4417
                                 elsif ea(3) = '1' then
4418
                                   next_state <= puls_dp_state;
4419
                                 elsif ea(4) = '1' then
4420
                                   next_state <= puls_ixh_state;
4421
                                 elsif ea(5) = '1' then
4422
                                   next_state <= puls_iyh_state;
4423
                                 elsif ea(6) = '1' then
4424
               next_state <= puls_uph_state;
4425
                                 elsif ea(7) = '1' then
4426
               next_state <= puls_pch_state;
4427
                                 else
4428
               lic        <= '1';
4429
                                   next_state <= fetch_state;
4430
                                 end if;
4431
 
4432
                          when puls_cc_state =>
4433
                                 -- increment sp
4434
             left_ctrl  <= sp_left;
4435
             right_ctrl <= one_right;
4436
             alu_ctrl   <= alu_add16;
4437
             sp_ctrl    <= load_sp;
4438
                                 -- read cc
4439
             cc_ctrl    <= pull_cc;
4440
             addr_ctrl  <= pulls_ad;
4441
                                 if ea(1) = '1' then
4442
                                   next_state <= puls_acca_state;
4443
                                 elsif ea(2) = '1' then
4444
                                   next_state <= puls_accb_state;
4445
                                 elsif ea(3) = '1' then
4446
                                   next_state <= puls_dp_state;
4447
                                 elsif ea(4) = '1' then
4448
                                   next_state <= puls_ixh_state;
4449
                                 elsif ea(5) = '1' then
4450
                                   next_state <= puls_iyh_state;
4451
                                 elsif ea(6) = '1' then
4452
               next_state <= puls_uph_state;
4453
                                 elsif ea(7) = '1' then
4454
               next_state <= puls_pch_state;
4455
                                 else
4456
               lic        <= '1';
4457
                                   next_state <= fetch_state;
4458
                                 end if;
4459
 
4460
                          when puls_acca_state =>
4461
                                 -- increment sp
4462
             left_ctrl  <= sp_left;
4463
             right_ctrl <= one_right;
4464
             alu_ctrl   <= alu_add16;
4465
             sp_ctrl    <= load_sp;
4466
                                 -- read acca
4467
                                 acca_ctrl  <= pull_acca;
4468
             addr_ctrl  <= pulls_ad;
4469
                                 if ea(2) = '1' then
4470
                                   next_state <= puls_accb_state;
4471
                                 elsif ea(3) = '1' then
4472
                                   next_state <= puls_dp_state;
4473
                                 elsif ea(4) = '1' then
4474
                                   next_state <= puls_ixh_state;
4475
                                 elsif ea(5) = '1' then
4476
                                   next_state <= puls_iyh_state;
4477
                                 elsif ea(6) = '1' then
4478
               next_state <= puls_uph_state;
4479
                                 elsif ea(7) = '1' then
4480
               next_state <= puls_pch_state;
4481
                                 else
4482
               lic          <= '1';
4483
                                   next_state <= fetch_state;
4484
                                 end if;
4485
 
4486
                          when puls_accb_state =>
4487
                                 -- increment sp
4488
             left_ctrl  <= sp_left;
4489
             right_ctrl <= one_right;
4490
             alu_ctrl   <= alu_add16;
4491
             sp_ctrl    <= load_sp;
4492
                                 -- read accb
4493
                                 accb_ctrl  <= pull_accb;
4494
             addr_ctrl  <= pulls_ad;
4495
                                 if ea(3) = '1' then
4496
                                   next_state <= puls_dp_state;
4497
                                 elsif ea(4) = '1' then
4498
                                   next_state <= puls_ixh_state;
4499
                                 elsif ea(5) = '1' then
4500
                                   next_state <= puls_iyh_state;
4501
                                 elsif ea(6) = '1' then
4502
               next_state <= puls_uph_state;
4503
                                 elsif ea(7) = '1' then
4504
               next_state <= puls_pch_state;
4505
                                 else
4506
               lic        <= '1';
4507
                                   next_state <= fetch_state;
4508
                                 end if;
4509
 
4510
                          when puls_dp_state =>
4511
                                 -- increment sp
4512
             left_ctrl  <= sp_left;
4513
             right_ctrl <= one_right;
4514
             alu_ctrl   <= alu_add16;
4515
             sp_ctrl    <= load_sp;
4516
                                 -- read dp
4517
                                 dp_ctrl    <= pull_dp;
4518
             addr_ctrl  <= pulls_ad;
4519
                                 if ea(4) = '1' then
4520
                                   next_state <= puls_ixh_state;
4521
                                 elsif ea(5) = '1' then
4522
                                   next_state <= puls_iyh_state;
4523
                                 elsif ea(6) = '1' then
4524
               next_state <= puls_uph_state;
4525
                                 elsif ea(7) = '1' then
4526
               next_state <= puls_pch_state;
4527
                                 else
4528
               lic        <= '1';
4529
                                   next_state <= fetch_state;
4530
                                 end if;
4531
 
4532
                          when puls_ixh_state =>
4533
             -- increment sp
4534
             left_ctrl  <= sp_left;
4535
             right_ctrl <= one_right;
4536
             alu_ctrl   <= alu_add16;
4537
             sp_ctrl    <= load_sp;
4538
                                 -- pull ix hi
4539
                                 ix_ctrl    <= pull_hi_ix;
4540
             addr_ctrl  <= pulls_ad;
4541
             next_state <= puls_ixl_state;
4542
 
4543
                          when puls_ixl_state =>
4544
                                 -- increment sp
4545
             left_ctrl  <= sp_left;
4546
             right_ctrl <= one_right;
4547
             alu_ctrl   <= alu_add16;
4548
             sp_ctrl    <= load_sp;
4549
                                 -- read ix low
4550
                                 ix_ctrl    <= pull_lo_ix;
4551
             addr_ctrl  <= pulls_ad;
4552
                                 if ea(5) = '1' then
4553
                                   next_state <= puls_iyh_state;
4554
                                 elsif ea(6) = '1' then
4555
               next_state <= puls_uph_state;
4556
                                 elsif ea(7) = '1' then
4557
               next_state <= puls_pch_state;
4558
                                 else
4559
               lic        <= '1';
4560
                                   next_state <= fetch_state;
4561
                                 end if;
4562
 
4563
                          when puls_iyh_state =>
4564
             -- increment sp
4565
             left_ctrl  <= sp_left;
4566
             right_ctrl <= one_right;
4567
             alu_ctrl   <= alu_add16;
4568
             sp_ctrl    <= load_sp;
4569
                                 -- pull iy hi
4570
                                 iy_ctrl    <= pull_hi_iy;
4571
             addr_ctrl  <= pulls_ad;
4572
             next_state   <= puls_iyl_state;
4573
 
4574
                          when puls_iyl_state =>
4575
             -- increment sp
4576
             left_ctrl  <= sp_left;
4577
             right_ctrl <= one_right;
4578
             alu_ctrl   <= alu_add16;
4579
             sp_ctrl    <= load_sp;
4580
                                 -- read iy low
4581
                                 iy_ctrl    <= pull_lo_iy;
4582
             addr_ctrl  <= pulls_ad;
4583
                                 if ea(6) = '1' then
4584
               next_state <= puls_uph_state;
4585
                                 elsif ea(7) = '1' then
4586
               next_state <= puls_pch_state;
4587
                                 else
4588
               lic        <= '1';
4589
                                   next_state <= fetch_state;
4590
                                 end if;
4591
 
4592
                          when puls_uph_state =>
4593
             -- increment sp
4594
             left_ctrl  <= sp_left;
4595
             right_ctrl <= one_right;
4596
             alu_ctrl   <= alu_add16;
4597
             sp_ctrl    <= load_sp;
4598
                                 -- pull up hi
4599
                                 up_ctrl    <= pull_hi_up;
4600
             addr_ctrl  <= pulls_ad;
4601
             next_state <= puls_upl_state;
4602
 
4603
                          when puls_upl_state =>
4604
                                 -- increment sp
4605
             left_ctrl  <= sp_left;
4606
             right_ctrl <= one_right;
4607
             alu_ctrl   <= alu_add16;
4608
             sp_ctrl    <= load_sp;
4609
                                 -- read up low
4610
                                 up_ctrl    <= pull_lo_up;
4611
             addr_ctrl  <= pulls_ad;
4612
                                 if ea(7) = '1' then
4613
               next_state <= puls_pch_state;
4614
                                 else
4615
               lic        <= '1';
4616
                                   next_state <= fetch_state;
4617
                                 end if;
4618
 
4619
                          when puls_pch_state =>
4620
             -- increment sp
4621
             left_ctrl  <= sp_left;
4622
             right_ctrl <= one_right;
4623
             alu_ctrl   <= alu_add16;
4624
             sp_ctrl    <= load_sp;
4625
                                 -- pull pc hi
4626
                                 pc_ctrl    <= pull_hi_pc;
4627
             addr_ctrl  <= pulls_ad;
4628
             next_state <= puls_pcl_state;
4629
 
4630
                          when puls_pcl_state =>
4631
                                 -- increment sp
4632
             left_ctrl  <= sp_left;
4633
             right_ctrl <= one_right;
4634
             alu_ctrl   <= alu_add16;
4635
             sp_ctrl    <= load_sp;
4636
                                 -- read pc low
4637
                                 pc_ctrl    <= pull_lo_pc;
4638
             addr_ctrl  <= pulls_ad;
4639
             lic        <= '1';
4640
             next_state <= fetch_state;
4641
 
4642
                          --
4643
                          -- Enter here on pshu
4644
                          -- ea holds post byte
4645
                          --
4646
                          when pshu_state =>
4647
             -- decrement up if any registers to be pushed
4648
             left_ctrl  <= up_left;
4649
             right_ctrl <= one_right;
4650
             alu_ctrl   <= alu_sub16;
4651
                                 if ea(7 downto 0) = "00000000" then
4652
               up_ctrl    <= latch_up;
4653
                                 else
4654
               up_ctrl    <= load_up;
4655
                                 end if;
4656
                                 -- write idle bus
4657
                                 if ea(7) = '1' then
4658
               next_state   <= pshu_pcl_state;
4659
                                 elsif ea(6) = '1' then
4660
               next_state   <= pshu_spl_state;
4661
                                 elsif ea(5) = '1' then
4662
                                   next_state   <= pshu_iyl_state;
4663
                                 elsif ea(4) = '1' then
4664
                                   next_state   <= pshu_ixl_state;
4665
                                 elsif ea(3) = '1' then
4666
                                   next_state   <= pshu_dp_state;
4667
                                 elsif ea(2) = '1' then
4668
                                   next_state   <= pshu_accb_state;
4669
                                 elsif ea(1) = '1' then
4670
                                   next_state   <= pshu_acca_state;
4671
                                 elsif ea(0) = '1' then
4672
                                   next_state   <= pshu_cc_state;
4673
                                 else
4674
               lic          <= '1';
4675
                                   next_state   <= fetch_state;
4676
                                 end if;
4677
                          --
4678
                          -- push PC onto U stack
4679
                          --
4680
                          when pshu_pcl_state =>
4681
             -- decrement up
4682
             left_ctrl  <= up_left;
4683
             right_ctrl <= one_right;
4684
             alu_ctrl   <= alu_sub16;
4685
             up_ctrl    <= load_up;
4686
                                 -- write pc low
4687
             addr_ctrl  <= pushu_ad;
4688
                            dout_ctrl  <= pc_lo_dout;
4689
             next_state <= pshu_pch_state;
4690
 
4691
                          when pshu_pch_state =>
4692
             -- decrement up
4693
             left_ctrl  <= up_left;
4694
             right_ctrl <= one_right;
4695
             alu_ctrl   <= alu_sub16;
4696
                                 if ea(6 downto 0) = "0000000" then
4697
               up_ctrl    <= latch_up;
4698
                                 else
4699
               up_ctrl    <= load_up;
4700
                                 end if;
4701
                                 -- write pc hi
4702
             addr_ctrl  <= pushu_ad;
4703
                            dout_ctrl  <= pc_hi_dout;
4704
                                 if ea(6) = '1' then
4705
               next_state   <= pshu_spl_state;
4706
                                 elsif ea(5) = '1' then
4707
                                   next_state   <= pshu_iyl_state;
4708
                                 elsif ea(4) = '1' then
4709
                                   next_state   <= pshu_ixl_state;
4710
                                 elsif ea(3) = '1' then
4711
                                   next_state   <= pshu_dp_state;
4712
                                 elsif ea(2) = '1' then
4713
                                   next_state   <= pshu_accb_state;
4714
                                 elsif ea(1) = '1' then
4715
                                   next_state   <= pshu_acca_state;
4716
                                 elsif ea(0) = '1' then
4717
                                   next_state   <= pshu_cc_state;
4718
                                 else
4719
               lic          <= '1';
4720
                                   next_state   <= fetch_state;
4721
                                 end if;
4722
 
4723
                          when pshu_spl_state =>
4724
             -- decrement up
4725
             left_ctrl  <= up_left;
4726
             right_ctrl <= one_right;
4727
             alu_ctrl   <= alu_sub16;
4728
             up_ctrl    <= load_up;
4729
                                 -- write sp low
4730
             addr_ctrl  <= pushu_ad;
4731
                            dout_ctrl  <= sp_lo_dout;
4732
             next_state <= pshu_sph_state;
4733
 
4734
                          when pshu_sph_state =>
4735
             -- decrement up
4736
             left_ctrl  <= up_left;
4737
             right_ctrl <= one_right;
4738
             alu_ctrl   <= alu_sub16;
4739
                                 if ea(5 downto 0) = "000000" then
4740
               up_ctrl    <= latch_up;
4741
                                 else
4742
               up_ctrl    <= load_up;
4743
                                 end if;
4744
                                 -- write sp hi
4745
             addr_ctrl  <= pushu_ad;
4746
                            dout_ctrl  <= sp_hi_dout;
4747
                                 if ea(5) = '1' then
4748
                                   next_state   <= pshu_iyl_state;
4749
                                 elsif ea(4) = '1' then
4750
                                   next_state   <= pshu_ixl_state;
4751
                                 elsif ea(3) = '1' then
4752
                                   next_state   <= pshu_dp_state;
4753
                                 elsif ea(2) = '1' then
4754
                                   next_state   <= pshu_accb_state;
4755
                                 elsif ea(1) = '1' then
4756
                                   next_state   <= pshu_acca_state;
4757
                                 elsif ea(0) = '1' then
4758
                                   next_state   <= pshu_cc_state;
4759
                                 else
4760
               lic          <= '1';
4761
                                   next_state   <= fetch_state;
4762
                                 end if;
4763
 
4764
                          when pshu_iyl_state =>
4765
             -- decrement up
4766
             left_ctrl  <= up_left;
4767
             right_ctrl <= one_right;
4768
             alu_ctrl   <= alu_sub16;
4769
             up_ctrl    <= load_up;
4770
                                 -- write iy low
4771
             addr_ctrl  <= pushu_ad;
4772
                            dout_ctrl  <= iy_lo_dout;
4773
             next_state <= pshu_iyh_state;
4774
 
4775
                          when pshu_iyh_state =>
4776
             -- decrement up
4777
             left_ctrl  <= up_left;
4778
             right_ctrl <= one_right;
4779
             alu_ctrl   <= alu_sub16;
4780
                                 if ea(4 downto 0) = "00000" then
4781
               up_ctrl    <= latch_up;
4782
                                 else
4783
               up_ctrl    <= load_up;
4784
                                 end if;
4785
                                 -- write iy hi
4786
             addr_ctrl  <= pushu_ad;
4787
                            dout_ctrl  <= iy_hi_dout;
4788
                                 if ea(4) = '1' then
4789
                                   next_state   <= pshu_ixl_state;
4790
                                 elsif ea(3) = '1' then
4791
                                   next_state   <= pshu_dp_state;
4792
                                 elsif ea(2) = '1' then
4793
                                   next_state   <= pshu_accb_state;
4794
                                 elsif ea(1) = '1' then
4795
                                   next_state   <= pshu_acca_state;
4796
                                 elsif ea(0) = '1' then
4797
                                   next_state   <= pshu_cc_state;
4798
                                 else
4799
               lic          <= '1';
4800
                                   next_state   <= fetch_state;
4801
                                 end if;
4802
 
4803
                          when pshu_ixl_state =>
4804
             -- decrement up
4805
             left_ctrl  <= up_left;
4806
             right_ctrl <= one_right;
4807
             alu_ctrl   <= alu_sub16;
4808
             up_ctrl    <= load_up;
4809
                                 -- write ix low
4810
             addr_ctrl  <= pushu_ad;
4811
                            dout_ctrl  <= ix_lo_dout;
4812
             next_state <= pshu_ixh_state;
4813
 
4814
                          when pshu_ixh_state =>
4815
             -- decrement up
4816
             left_ctrl  <= up_left;
4817
             right_ctrl <= one_right;
4818
             alu_ctrl   <= alu_sub16;
4819
                                 if ea(3 downto 0) = "0000" then
4820
               up_ctrl    <= latch_up;
4821
                                 else
4822
               up_ctrl    <= load_up;
4823
                                 end if;
4824
                                 -- write ix hi
4825
             addr_ctrl  <= pushu_ad;
4826
                            dout_ctrl  <= ix_hi_dout;
4827
                                 if ea(3) = '1' then
4828
                                   next_state   <= pshu_dp_state;
4829
                                 elsif ea(2) = '1' then
4830
                                   next_state   <= pshu_accb_state;
4831
                                 elsif ea(1) = '1' then
4832
                                   next_state   <= pshu_acca_state;
4833
                                 elsif ea(0) = '1' then
4834
                                   next_state   <= pshu_cc_state;
4835
                                 else
4836
               lic          <= '1';
4837
                                   next_state   <= fetch_state;
4838
                                 end if;
4839
 
4840
                          when pshu_dp_state =>
4841
             -- decrement up
4842
             left_ctrl  <= up_left;
4843
             right_ctrl <= one_right;
4844
             alu_ctrl   <= alu_sub16;
4845
                                 if ea(2 downto 0) = "000" then
4846
               up_ctrl    <= latch_up;
4847
                                 else
4848
               up_ctrl    <= load_up;
4849
                                 end if;
4850
                                 -- write dp
4851
             addr_ctrl  <= pushu_ad;
4852
                            dout_ctrl  <= dp_dout;
4853
                                 if ea(2) = '1' then
4854
                                   next_state   <= pshu_accb_state;
4855
                                 elsif ea(1) = '1' then
4856
                                   next_state   <= pshu_acca_state;
4857
                                 elsif ea(0) = '1' then
4858
                                   next_state   <= pshu_cc_state;
4859
                                 else
4860
               lic          <= '1';
4861
                                   next_state   <= fetch_state;
4862
                                 end if;
4863
 
4864
                          when pshu_accb_state =>
4865
             -- decrement up
4866
             left_ctrl  <= up_left;
4867
             right_ctrl <= one_right;
4868
             alu_ctrl   <= alu_sub16;
4869
                                 if ea(1 downto 0) = "00" then
4870
               up_ctrl    <= latch_up;
4871
                                 else
4872
               up_ctrl    <= load_up;
4873
                                 end if;
4874
                                 -- write accb
4875
             addr_ctrl  <= pushu_ad;
4876
                            dout_ctrl  <= accb_dout;
4877
                                 if ea(1) = '1' then
4878
                                   next_state   <= pshu_acca_state;
4879
                                 elsif ea(0) = '1' then
4880
                                   next_state   <= pshu_cc_state;
4881
                                 else
4882
               lic          <= '1';
4883
                                   next_state   <= fetch_state;
4884
                                 end if;
4885
 
4886
                          when pshu_acca_state =>
4887
             -- decrement up
4888
             left_ctrl  <= up_left;
4889
             right_ctrl <= one_right;
4890
             alu_ctrl   <= alu_sub16;
4891
                                 if ea(0) = '0' then
4892
               up_ctrl    <= latch_up;
4893
                                 else
4894
               up_ctrl    <= load_up;
4895
                                 end if;
4896
                                 -- write acca
4897
             addr_ctrl  <= pushu_ad;
4898
                            dout_ctrl  <= acca_dout;
4899
                                 if ea(0) = '1' then
4900
                                   next_state   <= pshu_cc_state;
4901
                                 else
4902
               lic          <= '1';
4903
                                   next_state   <= fetch_state;
4904
                                 end if;
4905
 
4906
                          when pshu_cc_state =>
4907
             -- idle up
4908
                                 -- write cc
4909
             addr_ctrl  <= pushu_ad;
4910
                            dout_ctrl  <= cc_dout;
4911
             lic        <= '1';
4912
             next_state <= fetch_state;
4913
 
4914
                          --
4915
                          -- enter here on PULU
4916
                          -- ea hold register mask
4917
                          --
4918
                          when pulu_state =>
4919
                                 -- idle UP
4920
                                 -- idle bus
4921
                                 if ea(0) = '1' then
4922
                                   next_state <= pulu_cc_state;
4923
                                 elsif ea(1) = '1' then
4924
                                   next_state <= pulu_acca_state;
4925
                                 elsif ea(2) = '1' then
4926
                                   next_state <= pulu_accb_state;
4927
                                 elsif ea(3) = '1' then
4928
                                   next_state <= pulu_dp_state;
4929
                                 elsif ea(4) = '1' then
4930
                                   next_state <= pulu_ixh_state;
4931
                                 elsif ea(5) = '1' then
4932
                                   next_state <= pulu_iyh_state;
4933
                                 elsif ea(6) = '1' then
4934
               next_state <= pulu_sph_state;
4935
                                 elsif ea(7) = '1' then
4936
               next_state <= pulu_pch_state;
4937
                                 else
4938
               lic        <= '1';
4939
                                   next_state <= fetch_state;
4940
                                 end if;
4941
 
4942
                          when pulu_cc_state =>
4943
                                 -- increment up
4944
             left_ctrl  <= up_left;
4945
             right_ctrl <= one_right;
4946
             alu_ctrl   <= alu_add16;
4947
             up_ctrl    <= load_up;
4948
                                 -- read cc
4949
             cc_ctrl    <= pull_cc;
4950
             addr_ctrl  <= pullu_ad;
4951
                                 if ea(1) = '1' then
4952
                                   next_state <= pulu_acca_state;
4953
                                 elsif ea(2) = '1' then
4954
                                   next_state <= pulu_accb_state;
4955
                                 elsif ea(3) = '1' then
4956
                                   next_state <= pulu_dp_state;
4957
                                 elsif ea(4) = '1' then
4958
                                   next_state <= pulu_ixh_state;
4959
                                 elsif ea(5) = '1' then
4960
                                   next_state <= pulu_iyh_state;
4961
                                 elsif ea(6) = '1' then
4962
               next_state <= pulu_sph_state;
4963
                                 elsif ea(7) = '1' then
4964
               next_state <= pulu_pch_state;
4965
                                 else
4966
               lic        <= '1';
4967
                                   next_state <= fetch_state;
4968
                                 end if;
4969
 
4970
                          when pulu_acca_state =>
4971
                                 -- increment up
4972
             left_ctrl  <= up_left;
4973
             right_ctrl <= one_right;
4974
             alu_ctrl   <= alu_add16;
4975
             up_ctrl    <= load_up;
4976
                                 -- read acca
4977
                                 acca_ctrl  <= pull_acca;
4978
             addr_ctrl  <= pullu_ad;
4979
                                 if ea(2) = '1' then
4980
                                   next_state <= pulu_accb_state;
4981
                                 elsif ea(3) = '1' then
4982
                                   next_state <= pulu_dp_state;
4983
                                 elsif ea(4) = '1' then
4984
                                   next_state <= pulu_ixh_state;
4985
                                 elsif ea(5) = '1' then
4986
                                   next_state <= pulu_iyh_state;
4987
                                 elsif ea(6) = '1' then
4988
               next_state <= pulu_sph_state;
4989
                                 elsif ea(7) = '1' then
4990
               next_state <= pulu_pch_state;
4991
                                 else
4992
               lic        <= '1';
4993
                                   next_state <= fetch_state;
4994
                                 end if;
4995
 
4996
                          when pulu_accb_state =>
4997
                                 -- increment up
4998
             left_ctrl  <= up_left;
4999
             right_ctrl <= one_right;
5000
             alu_ctrl   <= alu_add16;
5001
             up_ctrl    <= load_up;
5002
                                 -- read accb
5003
                                 accb_ctrl  <= pull_accb;
5004
             addr_ctrl  <= pullu_ad;
5005
                                 if ea(3) = '1' then
5006
                                   next_state <= pulu_dp_state;
5007
                                 elsif ea(4) = '1' then
5008
                                   next_state <= pulu_ixh_state;
5009
                                 elsif ea(5) = '1' then
5010
                                   next_state <= pulu_iyh_state;
5011
                                 elsif ea(6) = '1' then
5012
               next_state <= pulu_sph_state;
5013
                                 elsif ea(7) = '1' then
5014
               next_state <= pulu_pch_state;
5015
                                 else
5016
               lic        <= '1';
5017
                                   next_state <= fetch_state;
5018
                                 end if;
5019
 
5020
                          when pulu_dp_state =>
5021
                                 -- increment up
5022
             left_ctrl  <= up_left;
5023
             right_ctrl <= one_right;
5024
             alu_ctrl   <= alu_add16;
5025
             up_ctrl    <= load_up;
5026
                                 -- read dp
5027
                                 dp_ctrl    <= pull_dp;
5028
             addr_ctrl  <= pullu_ad;
5029
                                 if ea(4) = '1' then
5030
                                   next_state <= pulu_ixh_state;
5031
                                 elsif ea(5) = '1' then
5032
                                   next_state <= pulu_iyh_state;
5033
                                 elsif ea(6) = '1' then
5034
               next_state <= pulu_sph_state;
5035
                                 elsif ea(7) = '1' then
5036
               next_state <= pulu_pch_state;
5037
                                 else
5038
               lic        <= '1';
5039
                                   next_state <= fetch_state;
5040
                                 end if;
5041
 
5042
                          when pulu_ixh_state =>
5043
             -- increment up
5044
             left_ctrl  <= up_left;
5045
             right_ctrl <= one_right;
5046
             alu_ctrl   <= alu_add16;
5047
             up_ctrl    <= load_up;
5048
                                 -- read ix hi
5049
                                 ix_ctrl    <= pull_hi_ix;
5050
             addr_ctrl  <= pullu_ad;
5051
             next_state <= pulu_ixl_state;
5052
 
5053
                          when pulu_ixl_state =>
5054
                                 -- increment up
5055
             left_ctrl  <= up_left;
5056
             right_ctrl <= one_right;
5057
             alu_ctrl   <= alu_add16;
5058
             up_ctrl    <= load_up;
5059
                                 -- read ix low
5060
                                 ix_ctrl    <= pull_lo_ix;
5061
             addr_ctrl  <= pullu_ad;
5062
                                 if ea(5) = '1' then
5063
                                   next_state <= pulu_iyh_state;
5064
                                 elsif ea(6) = '1' then
5065
               next_state <= pulu_sph_state;
5066
                                 elsif ea(7) = '1' then
5067
               next_state <= pulu_pch_state;
5068
                                 else
5069
               lic        <= '1';
5070
                                   next_state <= fetch_state;
5071
                                 end if;
5072
 
5073
                          when pulu_iyh_state =>
5074
             -- increment up
5075
             left_ctrl  <= up_left;
5076
             right_ctrl <= one_right;
5077
             alu_ctrl   <= alu_add16;
5078
             up_ctrl    <= load_up;
5079
                                 -- read iy hi
5080
                                 iy_ctrl    <= pull_hi_iy;
5081
             addr_ctrl  <= pullu_ad;
5082
             next_state <= pulu_iyl_state;
5083
 
5084
                          when pulu_iyl_state =>
5085
             -- increment up
5086
             left_ctrl  <= up_left;
5087
             right_ctrl <= one_right;
5088
             alu_ctrl   <= alu_add16;
5089
             up_ctrl    <= load_up;
5090
                                 -- read iy low
5091
                                 iy_ctrl    <= pull_lo_iy;
5092
             addr_ctrl  <= pullu_ad;
5093
                                 if ea(6) = '1' then
5094
               next_state <= pulu_sph_state;
5095
                                 elsif ea(7) = '1' then
5096
               next_state <= pulu_pch_state;
5097
                                 else
5098
               lic        <= '1';
5099
                                   next_state <= fetch_state;
5100
                                 end if;
5101
 
5102
                          when pulu_sph_state =>
5103
             -- increment up
5104
             left_ctrl  <= up_left;
5105
             right_ctrl <= one_right;
5106
             alu_ctrl   <= alu_add16;
5107
             up_ctrl    <= load_up;
5108
                                 -- read sp hi
5109
                                 sp_ctrl    <= pull_hi_sp;
5110
             addr_ctrl  <= pullu_ad;
5111
             next_state <= pulu_spl_state;
5112
 
5113
                          when pulu_spl_state =>
5114
                                 -- increment up
5115
             left_ctrl  <= up_left;
5116
             right_ctrl <= one_right;
5117
             alu_ctrl   <= alu_add16;
5118
             up_ctrl    <= load_up;
5119
                                 -- read sp low
5120
                                 sp_ctrl    <= pull_lo_sp;
5121
             addr_ctrl  <= pullu_ad;
5122
                                 if ea(7) = '1' then
5123
               next_state <= pulu_pch_state;
5124
                                 else
5125
               lic        <= '1';
5126
                                   next_state <= fetch_state;
5127
                                 end if;
5128
 
5129
                          when pulu_pch_state =>
5130
             -- increment up
5131
             left_ctrl  <= up_left;
5132
             right_ctrl <= one_right;
5133
             alu_ctrl   <= alu_add16;
5134
             up_ctrl    <= load_up;
5135
                                 -- pull pc hi
5136
                                 pc_ctrl    <= pull_hi_pc;
5137
             addr_ctrl  <= pullu_ad;
5138
             next_state <= pulu_pcl_state;
5139
 
5140
                          when pulu_pcl_state =>
5141
                                 -- increment up
5142
             left_ctrl  <= up_left;
5143
             right_ctrl <= one_right;
5144
             alu_ctrl   <= alu_add16;
5145
             up_ctrl    <= load_up;
5146
                                 -- read pc low
5147
                                 pc_ctrl    <= pull_lo_pc;
5148
             addr_ctrl  <= pullu_ad;
5149
             lic        <= '1';
5150
             next_state <= fetch_state;
5151
 
5152
                          --
5153
                          -- pop the Condition codes
5154
                          --
5155
                          when rti_cc_state =>
5156
                                 -- increment sp
5157
             left_ctrl  <= sp_left;
5158
             right_ctrl <= one_right;
5159
             alu_ctrl   <= alu_add16;
5160
             sp_ctrl    <= load_sp;
5161
                                 -- read cc
5162
             cc_ctrl    <= pull_cc;
5163
             addr_ctrl  <= pulls_ad;
5164
             next_state <= rti_entire_state;
5165
 
5166
                          --
5167
                          -- Added RTI cycle 11th July 2006 John Kent.
5168
                          -- test the "Entire" Flag
5169
                          -- that has just been popped off the stack
5170
                          --
5171
                          when rti_entire_state =>
5172
                                 --
5173
                                 -- The Entire flag must be recovered from the stack
5174
                                 -- before testing.
5175
                                 --
5176
                                 if cc(EBIT) = '1' then
5177
               next_state   <= rti_acca_state;
5178
                                 else
5179
               next_state   <= rti_pch_state;
5180
                                 end if;
5181
 
5182
                          when rti_acca_state =>
5183
                                 -- increment sp
5184
             left_ctrl  <= sp_left;
5185
             right_ctrl <= one_right;
5186
             alu_ctrl   <= alu_add16;
5187
             sp_ctrl    <= load_sp;
5188
                                 -- read acca
5189
                                 acca_ctrl  <= pull_acca;
5190
             addr_ctrl  <= pulls_ad;
5191
             next_state <= rti_accb_state;
5192
 
5193
                          when rti_accb_state =>
5194
                                 -- increment sp
5195
             left_ctrl  <= sp_left;
5196
             right_ctrl <= one_right;
5197
             alu_ctrl   <= alu_add16;
5198
             sp_ctrl    <= load_sp;
5199
                                 -- read accb
5200
                                 accb_ctrl  <= pull_accb;
5201
             addr_ctrl  <= pulls_ad;
5202
             next_state <= rti_dp_state;
5203
 
5204
                          when rti_dp_state =>
5205
                                 -- increment sp
5206
             left_ctrl  <= sp_left;
5207
             right_ctrl <= one_right;
5208
             alu_ctrl   <= alu_add16;
5209
             sp_ctrl    <= load_sp;
5210
                                 -- read dp
5211
                                 dp_ctrl    <= pull_dp;
5212
             addr_ctrl  <= pulls_ad;
5213
             next_state <= rti_ixh_state;
5214
 
5215
                          when rti_ixh_state =>
5216
                                 -- increment sp
5217
             left_ctrl  <= sp_left;
5218
             right_ctrl <= one_right;
5219
             alu_ctrl   <= alu_add16;
5220
             sp_ctrl    <= load_sp;
5221
                                 -- read ix hi
5222
                                 ix_ctrl    <= pull_hi_ix;
5223
             addr_ctrl  <= pulls_ad;
5224
             next_state <= rti_ixl_state;
5225
 
5226
                          when rti_ixl_state =>
5227
                                 -- increment sp
5228
             left_ctrl  <= sp_left;
5229
             right_ctrl <= one_right;
5230
             alu_ctrl   <= alu_add16;
5231
             sp_ctrl    <= load_sp;
5232
                                 -- read ix low
5233
                                 ix_ctrl    <= pull_lo_ix;
5234
             addr_ctrl  <= pulls_ad;
5235
             next_state <= rti_iyh_state;
5236
 
5237
                          when rti_iyh_state =>
5238
                                 -- increment sp
5239
             left_ctrl  <= sp_left;
5240
             right_ctrl <= one_right;
5241
             alu_ctrl   <= alu_add16;
5242
             sp_ctrl    <= load_sp;
5243
                                 -- read iy hi
5244
                                 iy_ctrl    <= pull_hi_iy;
5245
             addr_ctrl  <= pulls_ad;
5246
             next_state <= rti_iyl_state;
5247
 
5248
                          when rti_iyl_state =>
5249
                                 -- increment sp
5250
             left_ctrl  <= sp_left;
5251
             right_ctrl <= one_right;
5252
             alu_ctrl   <= alu_add16;
5253
             sp_ctrl    <= load_sp;
5254
                                 -- read iy low
5255
                                 iy_ctrl    <= pull_lo_iy;
5256
             addr_ctrl  <= pulls_ad;
5257
             next_state <= rti_uph_state;
5258
 
5259
 
5260
                          when rti_uph_state =>
5261
                                 -- increment sp
5262
             left_ctrl  <= sp_left;
5263
             right_ctrl <= one_right;
5264
             alu_ctrl   <= alu_add16;
5265
             sp_ctrl    <= load_sp;
5266
                                 -- read up hi
5267
                                 up_ctrl    <= pull_hi_up;
5268
             addr_ctrl  <= pulls_ad;
5269
             next_state <= rti_upl_state;
5270
 
5271
                          when rti_upl_state =>
5272
                                 -- increment sp
5273
             left_ctrl  <= sp_left;
5274
             right_ctrl <= one_right;
5275
             alu_ctrl   <= alu_add16;
5276
             sp_ctrl    <= load_sp;
5277
                                 -- read up low
5278
             up_ctrl    <= pull_lo_up;
5279
             addr_ctrl  <= pulls_ad;
5280
             next_state <= rti_pch_state;
5281
 
5282
                          when rti_pch_state =>
5283
                  -- increment sp
5284
             left_ctrl  <= sp_left;
5285
             right_ctrl <= one_right;
5286
             alu_ctrl   <= alu_add16;
5287
             sp_ctrl    <= load_sp;
5288
                                 -- pull pc hi
5289
                                 pc_ctrl    <= pull_hi_pc;
5290
             addr_ctrl  <= pulls_ad;
5291
             next_state <= rti_pcl_state;
5292
 
5293
                          when rti_pcl_state =>
5294
                                 -- increment sp
5295
             left_ctrl  <= sp_left;
5296
             right_ctrl <= one_right;
5297
             alu_ctrl   <= alu_add16;
5298
             sp_ctrl    <= load_sp;
5299
                  -- pull pc low
5300
                                 pc_ctrl    <= pull_lo_pc;
5301
             addr_ctrl  <= pulls_ad;
5302
             lic        <= '1';
5303
             next_state <= fetch_state;
5304
 
5305
                          --
5306
                          -- here on NMI interrupt
5307
                          -- Complete execute cycle of the last instruction.
5308
           -- If it was a dual operand instruction
5309
           --
5310
                          when int_nmi_state =>
5311
             next_state   <= int_nmi1_state;
5312
 
5313
                          -- Idle bus cycle
5314
           when int_nmi1_state =>
5315
             -- pre decrement sp
5316
             left_ctrl    <= sp_left;
5317
             right_ctrl   <= one_right;
5318
             alu_ctrl     <= alu_sub16;
5319
             sp_ctrl      <= load_sp;
5320
                                 iv_ctrl      <= nmi_iv;
5321
                                 st_ctrl      <= push_st;
5322
                                 return_state <= int_nmimask_state;
5323
             next_state   <= int_entire_state;
5324
 
5325
                          --
5326
                          -- here on IRQ interrupt
5327
                          -- Complete execute cycle of the last instruction.
5328
           -- If it was a dual operand instruction
5329
           --
5330
                          when int_irq_state =>
5331
             next_state   <= int_irq1_state;
5332
 
5333
                          -- pre decrement the sp
5334
                          -- Idle bus cycle
5335
           when int_irq1_state =>
5336
             -- pre decrement sp
5337
             left_ctrl    <= sp_left;
5338
             right_ctrl   <= one_right;
5339
             alu_ctrl     <= alu_sub16;
5340
             sp_ctrl      <= load_sp;
5341
                                 iv_ctrl      <= irq_iv;
5342
                                 st_ctrl      <= push_st;
5343
                                 return_state <= int_irqmask_state;
5344
             next_state   <= int_entire_state;
5345
 
5346
                          --
5347
                          -- here on FIRQ interrupt
5348
                          -- Complete execution cycle of the last instruction
5349
           -- if it was a dual operand instruction
5350
           --
5351
                          when int_firq_state =>
5352
             next_state   <= int_firq1_state;
5353
 
5354
                          -- Idle bus cycle
5355
           when int_firq1_state =>
5356
             -- pre decrement sp
5357
             left_ctrl    <= sp_left;
5358
             right_ctrl   <= one_right;
5359
             alu_ctrl     <= alu_sub16;
5360
             sp_ctrl      <= load_sp;
5361
                                 iv_ctrl      <= firq_iv;
5362
                                 st_ctrl      <= push_st;
5363
                                 return_state <= int_firqmask_state;
5364
             next_state   <= int_fast_state;
5365
 
5366
           --
5367
           -- CWAI entry point
5368
           -- stack pointer already pre-decremented
5369
           -- mask condition codes
5370
           --
5371
           when cwai_state =>
5372
              -- AND CC with md
5373
              left_ctrl    <= md_left;
5374
              right_ctrl   <= zero_right;
5375
              alu_ctrl     <= alu_andcc;
5376
              cc_ctrl      <= load_cc;
5377
                                  st_ctrl      <= push_st;
5378
                                  return_state <= int_cwai_state;
5379
                                  next_state   <= int_entire_state;
5380
 
5381
                          --
5382
                          -- wait here for an interrupt
5383
                          --
5384
                          when int_cwai_state =>
5385
             if (nmi_req = '1') then
5386
                                   iv_ctrl    <= nmi_iv;
5387
                              next_state <= int_nmimask_state;
5388
                                 --
5389
                                 -- FIRQ & IRQ are level sensitive
5390
                                 --
5391
             elsif (firq = '1') and (cc(FBIT) = '0') then
5392
                                   iv_ctrl     <= firq_iv;
5393
                              next_state  <= int_firqmask_state;
5394
 
5395
                            elsif (irq = '1') and (cc(IBIT) = '0') then
5396
                                   iv_ctrl     <= irq_iv;
5397
                              next_state  <= int_irqmask_state;
5398
                            else
5399
                                   next_state <= int_cwai_state;
5400
             end if;
5401
 
5402
           --
5403
                          -- State to mask I Flag and F Flag (NMI)
5404
                          --
5405
                          when int_nmimask_state =>
5406
                            alu_ctrl   <= alu_seif;
5407
                                 cc_ctrl    <= load_cc;
5408
             next_state <= vect_hi_state;
5409
 
5410
           --
5411
                          -- State to mask I Flag and F Flag (FIRQ)
5412
                          --
5413
                          when int_firqmask_state =>
5414
                            alu_ctrl   <= alu_seif;
5415
                                 cc_ctrl    <= load_cc;
5416
             next_state <= vect_hi_state;
5417
 
5418
 
5419
           --
5420
                          -- State to mask I Flag and F Flag (SWI)
5421
                          --
5422
                          when int_swimask_state =>
5423
                            alu_ctrl   <= alu_seif;
5424
                                 cc_ctrl    <= load_cc;
5425
             next_state <= vect_hi_state;
5426
 
5427
           --
5428
                          -- State to mask I Flag only (IRQ)
5429
                          --
5430
           when int_irqmask_state =>
5431
                                 alu_ctrl   <= alu_sei;
5432
                                 cc_ctrl    <= load_cc;
5433
             next_state <= vect_hi_state;
5434
 
5435
                          --
5436
                          -- set Entire Flag on SWI, SWI2, SWI3 and CWAI, IRQ and NMI
5437
                          -- before stacking all registers
5438
                          --
5439
                          when int_entire_state =>
5440
             -- set entire flag
5441
             alu_ctrl   <= alu_see;
5442
             cc_ctrl    <= load_cc;
5443
             next_state <= int_pcl_state;
5444
 
5445
                          --
5446
                          -- clear Entire Flag on FIRQ
5447
                          -- before stacking all registers
5448
                          --
5449
                          when int_fast_state =>
5450
             -- clear entire flag
5451
             alu_ctrl   <= alu_cle;
5452
             cc_ctrl    <= load_cc;
5453
             next_state <= int_pcl_state;
5454
 
5455
                          when int_pcl_state =>
5456
             -- decrement sp
5457
             left_ctrl  <= sp_left;
5458
             right_ctrl <= one_right;
5459
             alu_ctrl   <= alu_sub16;
5460
             sp_ctrl    <= load_sp;
5461
                                 -- write pc low
5462
             addr_ctrl  <= pushs_ad;
5463
                            dout_ctrl  <= pc_lo_dout;
5464
             next_state <= int_pch_state;
5465
 
5466
                          when int_pch_state =>
5467
             -- decrement sp
5468
             left_ctrl  <= sp_left;
5469
             right_ctrl <= one_right;
5470
             alu_ctrl   <= alu_sub16;
5471
             sp_ctrl    <= load_sp;
5472
                                 -- write pc hi
5473
             addr_ctrl  <= pushs_ad;
5474
                            dout_ctrl  <= pc_hi_dout;
5475
                                 if cc(EBIT) = '1' then
5476
               next_state   <= int_upl_state;
5477
                                 else
5478
               next_state   <= int_cc_state;
5479
                                 end if;
5480
 
5481
                          when int_upl_state =>
5482
             -- decrement sp
5483
             left_ctrl  <= sp_left;
5484
             right_ctrl <= one_right;
5485
             alu_ctrl   <= alu_sub16;
5486
             sp_ctrl    <= load_sp;
5487
                                 -- write up low
5488
             addr_ctrl  <= pushs_ad;
5489
                            dout_ctrl  <= up_lo_dout;
5490
             next_state <= int_uph_state;
5491
 
5492
                          when int_uph_state =>
5493
             -- decrement sp
5494
             left_ctrl  <= sp_left;
5495
             right_ctrl <= one_right;
5496
             alu_ctrl   <= alu_sub16;
5497
             sp_ctrl    <= load_sp;
5498
                                 -- write ix hi
5499
             addr_ctrl  <= pushs_ad;
5500
                            dout_ctrl  <= up_hi_dout;
5501
             next_state <= int_iyl_state;
5502
 
5503
                          when int_iyl_state =>
5504
             -- decrement sp
5505
             left_ctrl  <= sp_left;
5506
             right_ctrl <= one_right;
5507
             alu_ctrl   <= alu_sub16;
5508
             sp_ctrl    <= load_sp;
5509
                                 -- write ix low
5510
             addr_ctrl  <= pushs_ad;
5511
                            dout_ctrl  <= iy_lo_dout;
5512
             next_state <= int_iyh_state;
5513
 
5514
                          when int_iyh_state =>
5515
             -- decrement sp
5516
             left_ctrl  <= sp_left;
5517
             right_ctrl <= one_right;
5518
             alu_ctrl   <= alu_sub16;
5519
             sp_ctrl    <= load_sp;
5520
                                 -- write ix hi
5521
             addr_ctrl  <= pushs_ad;
5522
                            dout_ctrl  <= iy_hi_dout;
5523
             next_state <= int_ixl_state;
5524
 
5525
                          when int_ixl_state =>
5526
             -- decrement sp
5527
             left_ctrl  <= sp_left;
5528
             right_ctrl <= one_right;
5529
             alu_ctrl   <= alu_sub16;
5530
             sp_ctrl    <= load_sp;
5531
                                 -- write ix low
5532
             addr_ctrl  <= pushs_ad;
5533
                            dout_ctrl  <= ix_lo_dout;
5534
             next_state <= int_ixh_state;
5535
 
5536
                          when int_ixh_state =>
5537
             -- decrement sp
5538
             left_ctrl  <= sp_left;
5539
             right_ctrl <= one_right;
5540
             alu_ctrl   <= alu_sub16;
5541
             sp_ctrl    <= load_sp;
5542
                                 -- write ix hi
5543
             addr_ctrl  <= pushs_ad;
5544
                            dout_ctrl  <= ix_hi_dout;
5545
             next_state <= int_dp_state;
5546
 
5547
                          when int_dp_state =>
5548
             -- decrement sp
5549
             left_ctrl  <= sp_left;
5550
             right_ctrl <= one_right;
5551
             alu_ctrl   <= alu_sub16;
5552
             sp_ctrl    <= load_sp;
5553
                                 -- write accb
5554
             addr_ctrl  <= pushs_ad;
5555
                            dout_ctrl  <= dp_dout;
5556
             next_state <= int_accb_state;
5557
 
5558
                          when int_accb_state =>
5559
             -- decrement sp
5560
             left_ctrl  <= sp_left;
5561
             right_ctrl <= one_right;
5562
             alu_ctrl   <= alu_sub16;
5563
             sp_ctrl    <= load_sp;
5564
                                 -- write accb
5565
             addr_ctrl  <= pushs_ad;
5566
                            dout_ctrl  <= accb_dout;
5567
             next_state <= int_acca_state;
5568
 
5569
                          when int_acca_state =>
5570
             -- decrement sp
5571
             left_ctrl  <= sp_left;
5572
             right_ctrl <= one_right;
5573
             alu_ctrl   <= alu_sub16;
5574
             sp_ctrl    <= load_sp;
5575
                                 -- write acca
5576
             addr_ctrl  <= pushs_ad;
5577
                            dout_ctrl  <= acca_dout;
5578
             next_state <= int_cc_state;
5579
 
5580
                          when int_cc_state =>
5581
                                 -- write cc
5582
             addr_ctrl  <= pushs_ad;
5583
                            dout_ctrl  <= cc_dout;
5584
                                 next_state <= saved_state;
5585
 
5586
                          --
5587
                          -- According to the 6809 programming manual:
5588
                          -- If an interrupt is received and is masked 
5589
                          -- or lasts for less than three cycles, the PC 
5590
                          -- will advance to the next instruction.
5591
                          -- If an interrupt is unmasked and lasts
5592
                          -- for more than three cycles, an interrupt
5593
                          -- will be generated.
5594
                          -- Note that I don't wait 3 clock cycles.
5595
                          -- John Kent 11th July 2006
5596
                          --
5597
                          when sync_state =>
5598
             lic        <= '1';
5599
             ba         <= '1';
5600
                         --
5601
                         -- Version 1.28 2015-05-30
5602
                         -- Exit sync_state on interrupt.
5603
                         -- If the interrupts are active
5604
                         -- they will be caught in the state_machine process
5605
                         -- and the interrupt service routine microcode will be executed.
5606
                         -- Masked interrupts will exit the sync_state.
5607
                         -- Moved from the state_machine process to the state_sequencer process
5608
                         --
5609
                                if (firq = '1') or (irq = '1') then
5610
               next_state <= fetch_state;
5611
                                else
5612
                    next_state <= sync_state;
5613
            end if;
5614
 
5615
                          when halt_state =>
5616
           --
5617
           -- 2011-10-30 John Kent
5618
           -- ba & bs should be high
5619
             ba           <= '1';
5620
             bs           <= '1';
5621
                                 if halt = '1' then
5622
               next_state   <= halt_state;
5623
                                 else
5624
               next_state   <= fetch_state;
5625
                                 end if;
5626
 
5627
                  end case;
5628
 
5629
--
5630
-- Ver 1.23 2011-10-30 John Kent
5631
-- First instruction cycle might be
5632
-- fetch_state
5633
-- halt_state
5634
-- int_nmirq_state
5635
-- int_firq_state
5636
--
5637
        if fic = '1' then
5638
                           --
5639
                                        case op_code(7 downto 6) is
5640
                                        --
5641
                                        -- ver 1.29 
5642
                                        -- hide last cycle of multiply in fetch
5643
                                        --
5644
                                        when "00" =>
5645
                                          case op_code(5 downto 0) is
5646
                                          when "111101" => -- mul
5647
                                            -- if bit 7 of ea set, add accd to md
5648
                   left_ctrl  <= accd_left;
5649
                                            if ea(7) = '1' then
5650
                     right_ctrl <= md_right;
5651
                                            else
5652
                     right_ctrl <= zero_right;
5653
                                            end if;
5654
                   alu_ctrl   <= alu_mul;
5655
                   cc_ctrl    <= load_cc;
5656
                   acca_ctrl  <= load_hi_acca;
5657
                   accb_ctrl  <= load_accb;
5658
                 when others =>
5659
                                             null;
5660
                                          end case;
5661
                                        when "10" => -- acca
5662
                                     case op_code(3 downto 0) is
5663
                                          when "0000" => -- suba
5664
                                            left_ctrl  <= acca_left;
5665
                                            right_ctrl <= md_right;
5666
                                            alu_ctrl   <= alu_sub8;
5667
                                                 cc_ctrl    <= load_cc;
5668
                                            acca_ctrl  <= load_acca;
5669
                                          when "0001" => -- cmpa
5670
                                            left_ctrl   <= acca_left;
5671
                                            right_ctrl  <= md_right;
5672
                                            alu_ctrl    <= alu_sub8;
5673
                                                 cc_ctrl     <= load_cc;
5674
                                          when "0010" => -- sbca
5675
                                            left_ctrl   <= acca_left;
5676
                                            right_ctrl  <= md_right;
5677
                                            alu_ctrl    <= alu_sbc;
5678
                                                 cc_ctrl     <= load_cc;
5679
                                            acca_ctrl   <= load_acca;
5680
                                          when "0011" =>
5681
                                            case pre_code is
5682
                                                 when "00010000" => -- page 2 -- cmpd
5683
                                              left_ctrl   <= accd_left;
5684
                                              right_ctrl  <= md_right;
5685
                                              alu_ctrl    <= alu_sub16;
5686
                                                   cc_ctrl     <= load_cc;
5687
                                                 when "00010001" => -- page 3 -- cmpu
5688
                                              left_ctrl   <= up_left;
5689
                                              right_ctrl  <= md_right;
5690
                                              alu_ctrl    <= alu_sub16;
5691
                                                   cc_ctrl     <= load_cc;
5692
                                                 when others => -- page 1 -- subd
5693
                                              left_ctrl   <= accd_left;
5694
                                              right_ctrl  <= md_right;
5695
                                              alu_ctrl    <= alu_sub16;
5696
                                                   cc_ctrl     <= load_cc;
5697
                                              acca_ctrl   <= load_hi_acca;
5698
                                                   accb_ctrl   <= load_accb;
5699
                                                 end case;
5700
                                          when "0100" => -- anda
5701
                                            left_ctrl   <= acca_left;
5702
                                            right_ctrl  <= md_right;
5703
                                            alu_ctrl    <= alu_and;
5704
                                                 cc_ctrl     <= load_cc;
5705
                                            acca_ctrl   <= load_acca;
5706
                                          when "0101" => -- bita
5707
                                            left_ctrl   <= acca_left;
5708
                                            right_ctrl  <= md_right;
5709
                                            alu_ctrl    <= alu_and;
5710
                                                 cc_ctrl     <= load_cc;
5711
                                          when "0110" => -- ldaa
5712
                                            left_ctrl   <= acca_left;
5713
                                            right_ctrl  <= md_right;
5714
                                            alu_ctrl    <= alu_ld8;
5715
                                                 cc_ctrl     <= load_cc;
5716
                                            acca_ctrl   <= load_acca;
5717
                                          when "0111" => -- staa
5718
                                            left_ctrl   <= acca_left;
5719
                                            right_ctrl  <= md_right;
5720
                                            alu_ctrl    <= alu_st8;
5721
                                                 cc_ctrl     <= load_cc;
5722
                                          when "1000" => -- eora
5723
                                            left_ctrl   <= acca_left;
5724
                                            right_ctrl  <= md_right;
5725
                                            alu_ctrl    <= alu_eor;
5726
                                                 cc_ctrl     <= load_cc;
5727
                                            acca_ctrl   <= load_acca;
5728
                                          when "1001" => -- adca
5729
                                            left_ctrl   <= acca_left;
5730
                                            right_ctrl  <= md_right;
5731
                                            alu_ctrl    <= alu_adc;
5732
                                                 cc_ctrl     <= load_cc;
5733
                                            acca_ctrl   <= load_acca;
5734
                                          when "1010" => -- oraa
5735
                                            left_ctrl   <= acca_left;
5736
                                            right_ctrl  <= md_right;
5737
                                            alu_ctrl    <= alu_ora;
5738
                                                 cc_ctrl     <= load_cc;
5739
                                            acca_ctrl   <= load_acca;
5740
                                          when "1011" => -- adda
5741
                                            left_ctrl   <= acca_left;
5742
                                            right_ctrl  <= md_right;
5743
                                            alu_ctrl    <= alu_add8;
5744
                                                 cc_ctrl     <= load_cc;
5745
                                            acca_ctrl   <= load_acca;
5746
                                          when "1100" =>
5747
                                            case pre_code is
5748
                                                 when "00010000" => -- page 2 -- cmpy
5749
                                              left_ctrl   <= iy_left;
5750
                                              right_ctrl  <= md_right;
5751
                                              alu_ctrl    <= alu_sub16;
5752
                                                   cc_ctrl     <= load_cc;
5753
                                                 when "00010001" => -- page 3 -- cmps
5754
                                              left_ctrl   <= sp_left;
5755
                                              right_ctrl  <= md_right;
5756
                                              alu_ctrl    <= alu_sub16;
5757
                                                   cc_ctrl     <= load_cc;
5758
                                                 when others => -- page 1 -- cmpx
5759
                                              left_ctrl   <= ix_left;
5760
                                              right_ctrl  <= md_right;
5761
                                              alu_ctrl    <= alu_sub16;
5762
                                                   cc_ctrl     <= load_cc;
5763
                                                 end case;
5764
                                          when "1101" => -- bsr / jsr
5765
                                            null;
5766
                                          when "1110" => -- ldx
5767
                                            case pre_code is
5768
                                                 when "00010000" => -- page 2 -- ldy
5769
                                              left_ctrl   <= iy_left;
5770
                                              right_ctrl  <= md_right;
5771
                                              alu_ctrl    <= alu_ld16;
5772
                                                   cc_ctrl     <= load_cc;
5773
                     iy_ctrl     <= load_iy;
5774
                                                 when others =>   -- page 1 -- ldx
5775
                                              left_ctrl   <= ix_left;
5776
                                              right_ctrl  <= md_right;
5777
                                              alu_ctrl    <= alu_ld16;
5778
                                                   cc_ctrl     <= load_cc;
5779
                     ix_ctrl     <= load_ix;
5780
                                                 end case;
5781
                                          when "1111" => -- stx
5782
                                            case pre_code is
5783
                                                 when "00010000" => -- page 2 -- sty
5784
                                              left_ctrl   <= iy_left;
5785
                                              right_ctrl  <= md_right;
5786
                                              alu_ctrl    <= alu_st16;
5787
                                                   cc_ctrl     <= load_cc;
5788
                                                 when others =>     -- page 1 -- stx
5789
                                              left_ctrl   <= ix_left;
5790
                                              right_ctrl  <= md_right;
5791
                                              alu_ctrl    <= alu_st16;
5792
                                                   cc_ctrl     <= load_cc;
5793
                                                 end case;
5794
                                          when others =>
5795
                                            null;
5796
                                          end case;
5797
                                        when "11" => -- accb dual op
5798
                                     case op_code(3 downto 0) is
5799
                                          when "0000" => -- subb
5800
                                            left_ctrl   <= accb_left;
5801
                                            right_ctrl  <= md_right;
5802
                                            alu_ctrl    <= alu_sub8;
5803
                                                 cc_ctrl     <= load_cc;
5804
                   accb_ctrl   <= load_accb;
5805
                                          when "0001" => -- cmpb
5806
                                            left_ctrl   <= accb_left;
5807
                                            right_ctrl  <= md_right;
5808
                                            alu_ctrl    <= alu_sub8;
5809
                                                 cc_ctrl     <= load_cc;
5810
                                          when "0010" => -- sbcb
5811
                                            left_ctrl   <= accb_left;
5812
                                            right_ctrl  <= md_right;
5813
                                            alu_ctrl    <= alu_sbc;
5814
                                                 cc_ctrl     <= load_cc;
5815
                   accb_ctrl   <= load_accb;
5816
                                          when "0011" => -- addd
5817
                                            left_ctrl   <= accd_left;
5818
                                            right_ctrl  <= md_right;
5819
                                            alu_ctrl    <= alu_add16;
5820
                                                 cc_ctrl     <= load_cc;
5821
                                            acca_ctrl   <= load_hi_acca;
5822
                                                 accb_ctrl   <= load_accb;
5823
                                          when "0100" => -- andb
5824
                                            left_ctrl   <= accb_left;
5825
                                            right_ctrl  <= md_right;
5826
                                            alu_ctrl    <= alu_and;
5827
                                                 cc_ctrl     <= load_cc;
5828
                   accb_ctrl   <= load_accb;
5829
                                          when "0101" => -- bitb
5830
                                            left_ctrl   <= accb_left;
5831
                                            right_ctrl  <= md_right;
5832
                                            alu_ctrl    <= alu_and;
5833
                                                 cc_ctrl     <= load_cc;
5834
                                          when "0110" => -- ldab
5835
                                            left_ctrl   <= accb_left;
5836
                                            right_ctrl  <= md_right;
5837
                                            alu_ctrl    <= alu_ld8;
5838
                                                 cc_ctrl     <= load_cc;
5839
                   accb_ctrl   <= load_accb;
5840
                                          when "0111" => -- stab
5841
                                            left_ctrl   <= accb_left;
5842
                                            right_ctrl  <= md_right;
5843
                                            alu_ctrl    <= alu_st8;
5844
                                                 cc_ctrl     <= load_cc;
5845
                                          when "1000" => -- eorb
5846
                                            left_ctrl   <= accb_left;
5847
                                            right_ctrl  <= md_right;
5848
                                            alu_ctrl    <= alu_eor;
5849
                                                 cc_ctrl     <= load_cc;
5850
                   accb_ctrl   <= load_accb;
5851
                                          when "1001" => -- adcb
5852
                                            left_ctrl   <= accb_left;
5853
                                            right_ctrl  <= md_right;
5854
                                            alu_ctrl    <= alu_adc;
5855
                                                 cc_ctrl     <= load_cc;
5856
                   accb_ctrl   <= load_accb;
5857
                                          when "1010" => -- orab
5858
                                            left_ctrl   <= accb_left;
5859
                                            right_ctrl  <= md_right;
5860
                                            alu_ctrl    <= alu_ora;
5861
                                                 cc_ctrl     <= load_cc;
5862
                   accb_ctrl   <= load_accb;
5863
                                          when "1011" => -- addb
5864
                                            left_ctrl   <= accb_left;
5865
                                            right_ctrl  <= md_right;
5866
                                            alu_ctrl    <= alu_add8;
5867
                                                 cc_ctrl     <= load_cc;
5868
                   accb_ctrl   <= load_accb;
5869
                                          when "1100" => -- ldd
5870
                                            left_ctrl   <= accd_left;
5871
                                            right_ctrl  <= md_right;
5872
                                            alu_ctrl    <= alu_ld16;
5873
                                                 cc_ctrl     <= load_cc;
5874
                                            acca_ctrl   <= load_hi_acca;
5875
                   accb_ctrl   <= load_accb;
5876
                                          when "1101" => -- std
5877
                                            left_ctrl   <= accd_left;
5878
                                            right_ctrl  <= md_right;
5879
                                            alu_ctrl    <= alu_st16;
5880
                                                 cc_ctrl     <= load_cc;
5881
                                          when "1110" => -- ldu
5882
                                            case pre_code is
5883
                                                 when "00010000" => -- page 2 -- lds
5884
                                              left_ctrl   <= sp_left;
5885
                                              right_ctrl  <= md_right;
5886
                                              alu_ctrl    <= alu_ld16;
5887
                                                   cc_ctrl     <= load_cc;
5888
                                                   sp_ctrl     <= load_sp;
5889
                                                 when others => -- page 1 -- ldu
5890
                                              left_ctrl   <= up_left;
5891
                                              right_ctrl  <= md_right;
5892
                                              alu_ctrl    <= alu_ld16;
5893
                                                   cc_ctrl     <= load_cc;
5894
                     up_ctrl     <= load_up;
5895
                                                 end case;
5896
                                          when "1111" =>
5897
                                            case pre_code is
5898
                                                 when "00010000" => -- page 2 -- sts
5899
                                              left_ctrl   <= sp_left;
5900
                                              right_ctrl  <= md_right;
5901
                                              alu_ctrl    <= alu_st16;
5902
                                                   cc_ctrl     <= load_cc;
5903
                                                 when others =>     -- page 1 -- stu
5904
                                              left_ctrl   <= up_left;
5905
                                              right_ctrl  <= md_right;
5906
                                              alu_ctrl    <= alu_st16;
5907
                                                   cc_ctrl     <= load_cc;
5908
                                                 end case;
5909
                                          when others =>
5910
                                            null;
5911
                                          end case;
5912
                                        when others =>
5913
                                          null;
5914
                                        end case;
5915
 
5916
       end if; -- first instruction cycle (fic)
5917
       lic_out <= lic;
5918
end process;
5919
 
5920
end rtl;
5921
 

powered by: WebSVN 2.1.0

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