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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_cpu.vhd] - Blame information for rev 253

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

Line No. Rev Author Line
1 185 jshamlet
-- Copyright (c)2006, 2011, 2012, 2013, 2015, 2019, 2020 Jeremy Seth Henry
2 169 jshamlet
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 194 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 169 jshamlet
--
24 181 jshamlet
-- VHDL Units :  o8_cpu
25 169 jshamlet
-- Description:  VHDL model of a RISC 8-bit processor core loosely based on the
26
--            :   V8/ARC uRISC instruction set. Requires Open8_pkg.vhd
27
--            :
28
-- Notes      :  Generic definitions
29
--            :
30
--            :  Program_Start_Addr sets the initial value of the program
31
--            :   counter.
32
--            :
33
--            :  ISR_Start_Addr sets the location of the interrupt service
34
--            :   vector table. There are 8 service vectors, or 16 bytes, which
35
--            :   must be allocated to either ROM or RAM.
36
--            :
37
--            :  Stack_Start_Address sets the initial (reset) value of the
38
--            :   stack pointer. Also used for the RSP instruction if
39
--            :   Allow_Stack_Address_Move is false.
40
--            :
41
--            :  Allow_Stack_Address_Move, when set true, allows the RSP to be
42 181 jshamlet
--            :   programmed via thet RSP instruction. If enabled, the
43
--            :   instruction changes into TSX or TXS based on the flag
44
--            :   specified by Stack_Xfer_Flag. If the flag is '0', RSP will
45
--            :   copy the current stack pointer to R1:R0 (TSX). If the flag
46
--            :   is '1', RSP will copy R1:R0 to the stack pointer (TXS). This
47
--            :   allows the processor to backup and restore stack pointers
48
--            :   in a multi-process environment. Note that no flags are
49
--            :   modified by either form of this instruction.
50 169 jshamlet
--            :
51 181 jshamlet
--            :  Stack_Xfer_Flag instructs the core to use the specified ALU
52
--            :   flag to alter the behavior of the RSP instruction when
53
--            :   Allow_Stack_Address_Move is set TRUE, otherwise it is ignored.
54
--            :   While technically any of the status bits may be used, the
55
--            :   intent was to use FL_GP[1,2,3,4], as these are not modified
56
--            :   by ordinary ALU operations.
57
--            :
58 169 jshamlet
--            :  The Enable_Auto_Increment generic can be used to modify the
59
--            :   indexed instructions such that specifying an odd register
60
--            :   will use the next lower register pair, post-incrementing the
61
--            :   value in that pair. IOW, specifying STX R1 will instead
62
--            :   result in STX R0++, or R0 = {R1:R0}; {R1:R0} + 1
63
--            :
64
--            :  BRK_Implements_WAI modifies the BRK instruction such that it
65
--            :   triggers the wait for interrupt state, but without triggering
66
--            :   a soft interrupt in lieu of its normal behavior, which is to
67
--            :   insert several dead clock cycles - essentially a long NOP
68
--            :
69
--            :  Enable_NMI overrides the mask bit for interrupt 0, creating a
70
--            :   non-maskable interrupt at the highest priority. To remain
71
--            :   true to the original core, this should be set false.
72
--            :
73 188 jshamlet
--            :  RTI_Ignores_GP_Flags alters the set of flag bits restored
74
--            :   after an interrupt. By default, all of the flag bits are put
75
--            :   back to their original state. If this flag is set true, only
76
--            :   the lower four bits are restored, allowing ISR code to alter
77
--            :   the GP flags persistently.
78
--            :
79 244 jshamlet
--            :  Supervisor_Mode, when set, disables the STP PSR_I instruction
80
--            :   preventing code from setting the I bit. When enabled, only
81
--            :   interrupts can set the I bit, allowing for more robust memory
82
--            :   protection by preventing errant code execution from
83
--            :   inadvertently entering an interrupt state.
84
--            :
85 248 jshamlet
--            :   This setting also sets I bit at startup so that any
86
--            :   initialization code may be run in an ISR context, initially
87
--            :   bypassing memory protection. Init code should clear the I bit
88
--            :   when done;
89 244 jshamlet
--            :
90 169 jshamlet
--            :  Default_Interrupt_Mask sets the intial/reset value of the
91
--            :   interrupt mask. To remain true to the original core, which
92
--            :   had no interrupt mask, this should be set to x"FF". Otherwise
93
--            :   it can be initialized to any value. Note that Enable_NMI
94
--            :   will logically force the LSB high.
95 172 jshamlet
--            :
96 169 jshamlet
--            :  Reset_Level determines whether the processor registers reset
97
--            :   on a high or low level from higher logic.
98
--            :
99
--            : Architecture notes
100
--            :  This model deviates from the original ISA in a few important
101
--            :   ways.
102
--            :
103
--            :  First, there is only one set of registers. Interrupt service
104
--            :   routines must explicitely preserve context since the the
105
--            :   hardware doesn't. This was done to decrease size and code
106
--            :   complexity. Older code that assumes this behavior will not
107
--            :   execute correctly on this processor model.
108
--            :
109
--            :  Second, this model adds an additional pipeline stage between
110
--            :   the instruction decoder and the ALU. Unfortunately, this
111
--            :   means that the instruction stream has to be restarted after
112
--            :   any math instruction is executed, implying that any ALU
113
--            :   instruction now has a latency of 2 instead of 0. The
114
--            :   advantage is that the maximum frequency has gone up
115
--            :   significantly, as the ALU code is vastly more efficient.
116
--            :   As an aside, this now means that all math instructions,
117
--            :   including MUL (see below) and UPP have the same instruction
118
--            :   latency.
119
--            :
120
--            :  Third, the original ISA, also a soft core, had two reserved
121
--            :   instructions, USR and USR2. These have been implemented as
122
--            :   DBNZ, and MUL respectively.
123
--            :
124
--            :  DBNZ decrements the specified register and branches if the
125
--            :   result is non-zero. The instruction effectively executes a
126
--            :   DEC Rn instruction prior to branching, so the same flags will
127
--            :   be set.
128
--            :
129
--            :  MUL places the result of R0 * Rn into R1:R0. Instruction
130
--            :   latency is identical to other ALU instructions. Only the Z
131
--            :   flag is set, since there is no defined overflow or "negative
132
--            :   16-bit values"
133
--            :
134
--            :  Fourth, indexed load/store instructions now have an (optional)
135
--            :   ability to post-increment their index registers. If enabled,
136
--            :   using an odd operand for LDO,LDX, STO, STX will cause the
137
--            :   register pair to be incremented after the storage access.
138
--            :
139
--            :  Fifth, the RSP instruction has been (optionally) altered to
140
--            :   allow the stack pointer to be sourced from R1:R0.
141
--            :
142
--            :  Sixth, the BRK instruction can optionally implement a WAI,
143
--            :   which is the same as the INT instruction without the soft
144
--            :   interrupt, as a way to put the processor to "sleep" until the
145
--            :   next external interrupt.
146
--            :
147
--            :  Seventh, the original CPU model had 8 non-maskable interrupts
148
--            :   with priority. This model has the same 8 interrupts, but
149 172 jshamlet
--            :   allows software to mask them (with an additional option to
150 169 jshamlet
--            :   override the highest priority interrupt, making it the NMI.)
151
--            :
152
--            :  Lastly, previous unmapped instructions in the OP_STK opcode
153
--            :   were repurposed to support a new interrupt mask.
154
--            :   SMSK and GMSK transfer the contents of R0 (accumulator)
155
--            :   to/from the interrupt mask register. SMSK is immediate, while
156
--            :   GMSK has the same overhead as a math instruction.
157
--
158
-- Revision History
159
-- Author          Date     Change
160
------------------ -------- ---------------------------------------------------
161
-- Seth Henry      07/19/06 Design Start
162
-- Seth Henry      01/18/11 Fixed BTT instruction to match V8
163
-- Seth Henry      07/22/11 Fixed interrupt transition logic to avoid data
164
--                           corruption issues.
165
-- Seth Henry      07/26/11 Optimized logic in ALU, stack pointer, and data
166
--                           path sections.
167
-- Seth Henry      07/27/11 Optimized logic for timing, merged blocks into
168
--                           single entity.
169
-- Seth Henry      09/20/11 Added BRK_Implements_WAI option, allowing the
170
--                           processor to wait for an interrupt instead of the
171
--                           normal BRK behavior.
172 187 jshamlet
-- Seth Henry      12/20/11 Modified core to allow WAI_Cx state to idle
173 169 jshamlet
--                           the bus entirely (Rd_Enable is low)
174
-- Seth Henry      02/03/12 Replaced complex interrupt controller with simpler,
175
--                           faster logic that simply does priority encoding.
176
-- Seth Henry      08/06/13 Removed HALT functionality
177
-- Seth Henry      10/29/15 Fixed inverted carry logic in CMP and SBC instrs
178 182 jshamlet
-- Seth Henry      12/19/19 Renamed to o8_cpu to fit "theme"
179 181 jshamlet
-- Seth Henry      03/09/20 Modified RSP instruction to work with a CPU flag
180
--                           allowing true backup/restore of the stack pointer
181 182 jshamlet
-- Seth Henry      03/11/20 Split the address logic from the main state machine
182
--                           in order to simplify things and eliminate
183
--                           redundancies. Came across and fixed a problem with
184
--                           the STO instruction when Enable_Auto_Increment is
185
--                           NOT set.
186 185 jshamlet
-- Seth Henry      03/12/20 Rationalized the naming of the CPU flags to match
187
--                           the assembler names. Also fixed an issue where
188
--                           the I bit wasn't being cleared after interrupts.
189
--                          Simplified the program counter logic to only use
190
--                           the offset for increments, redefining the
191
--                           original modes as fixed offset values.
192
--                          Modified the ALU section with a new ALU operation
193
--                           for GMSK. This allowed the .data field to be
194
--                           removed and Operand1 used in its place, which
195
--                           simplified the logic a great deal.
196 187 jshamlet
-- Seth Henry      03/16/20 Added CPU_Halt input back, only now as an input to
197
--                           the instruction decode state, where it acts as a
198
--                           modified form of the BRK instruction that holds
199
--                           state until CPU_Halt is deasserted. This has a
200
--                           much smaller impact on Fmax/complexity than the
201
--                           original clock enable, but imposes a mild impact
202
--                           due to the need to reset the instruction pipeline
203 188 jshamlet
-- Seth Henry      03/17/20 Added generic to control whether RTI full restores
204
--                           the flags, including the general purpose ones, or
205
--                           only the core ALU flags (Z, N, and C). Also
206
--                           brought out copies of the GP flags for external
207
--                           connection.
208 210 jshamlet
-- Seth Henry      04/09/20 Added a compile time setting to block interrupts
209
--                           while the I bit is set to avoid reentering ISRs
210
--                           This may slightly affect timing, as this will
211
--                           potentially block higher priority interrupts
212
--                           until the lower priority ISR returns or clears
213
--                           the I bit.
214
--                          Also added the I bit to the exported flags for
215
--                           use in memory protection schemes.
216 224 jshamlet
-- Seth Henry      04/16/20 Modified to use new Open8 bus record. Also added
217 225 jshamlet
--                           reset and usec_tick logic to drive utility
218
--                           signals. Also added Halt_Ack output.
219 244 jshamlet
-- Seth Henry      05/20/20 Added two new generics to alter the way the I bit
220
--                           is handled. The Supervisor_Mode setting disables
221
--                           STP PSR_I from being executed, preventing it
222
--                           from being set outside of an ISR. The
223
--                           Default_Int_Flag setting allows the I bit to
224
--                           start set so that initialization code can run,
225
--                           but not be hijacked later to corrupt any memory
226
--                           write protection later.
227 245 jshamlet
-- Seth Henry      05/21/20 Supervisor_Mode now protects the interrupt mask
228
--                           and stack pointer as well.
229 248 jshamlet
-- Seth Henry      05/24/20 Removed the Default_Int_Flag, as it is covered by
230
--                           Supervisor_Mode. If Supervisor_Mode isn't set,
231
--                           code can simply use STP to set the bit
232 252 jshamlet
-- Seth Henry      06/09/20 Added ability to use unsigned index offsets for
233 253 jshamlet
--                           LDO/STO. Also pipelined the address calculation
234 252 jshamlet
--                           for indexed instructions, reducing the final
235
--                           address generator to a multiplexor fed only by
236
--                           registers.
237 169 jshamlet
 
238
library ieee;
239
  use ieee.std_logic_1164.all;
240
  use ieee.std_logic_unsigned.all;
241
  use ieee.std_logic_arith.all;
242
  use ieee.std_logic_misc.all;
243
 
244
library work;
245 227 jshamlet
  use work.Open8_pkg.all;
246 169 jshamlet
 
247 183 jshamlet
entity o8_cpu is
248 169 jshamlet
  generic(
249
    Program_Start_Addr       : ADDRESS_TYPE := x"0000"; -- Initial PC location
250
    ISR_Start_Addr           : ADDRESS_TYPE := x"FFF0"; -- Bottom of ISR vec's
251
    Stack_Start_Addr         : ADDRESS_TYPE := x"03FF"; -- Top of Stack
252
    Allow_Stack_Address_Move : boolean      := false;   -- Use Normal v8 RSP
253 188 jshamlet
    Stack_Xfer_Flag          : integer      := PSR_GP4; -- GP4 modifies RSP
254 169 jshamlet
    Enable_Auto_Increment    : boolean      := false;   -- Modify indexed instr
255
    BRK_Implements_WAI       : boolean      := false;   -- BRK -> Wait for Int
256
    Enable_NMI               : boolean      := true;    -- Force INTR0 enabled
257 210 jshamlet
    Sequential_Interrupts    : boolean      := false;   -- Interruptable ISRs
258 224 jshamlet
    RTI_Ignores_GP_Flags     : boolean      := false;   -- RTI sets all flags
259 244 jshamlet
    Supervisor_Mode          : boolean      := false;   -- I bit is restricted
260 252 jshamlet
    Unsigned_Index_Offsets   : boolean      := false;   -- Offsets are signed
261 169 jshamlet
    Default_Interrupt_Mask   : DATA_TYPE    := x"FF";   -- Enable all Ints
262 224 jshamlet
    Clock_Frequency          : real                     -- Clock Frequency
263
);
264 169 jshamlet
  port(
265
    Clock                    : in  std_logic;
266 224 jshamlet
    PLL_Locked               : in  std_logic;
267 169 jshamlet
    --
268 225 jshamlet
    Halt_Req                 : in  std_logic := '0';
269
    Halt_Ack                 : out std_logic;
270
    --
271 223 jshamlet
    Open8_Bus                : out OPEN8_BUS_TYPE;
272 169 jshamlet
    Rd_Data                  : in  DATA_TYPE;
273 223 jshamlet
    Interrupts               : in  INTERRUPT_BUNDLE := x"00"
274
);
275 169 jshamlet
end entity;
276
 
277 183 jshamlet
architecture behave of o8_cpu is
278 169 jshamlet
 
279 224 jshamlet
  signal Reset_q             : std_logic := Reset_Level;
280
  signal Reset               : std_logic := Reset_Level;
281
 
282
  constant USEC_VAL          : integer := integer(Clock_Frequency / 1000000.0);
283
  constant USEC_WDT          : integer := ceil_log2(USEC_VAL - 1);
284
  constant USEC_DLY          : std_logic_vector :=
285
                                conv_std_logic_vector(USEC_VAL - 1, USEC_WDT);
286
  signal uSec_Cntr           : std_logic_vector( USEC_WDT - 1 downto 0 );
287
  signal uSec_Tick           : std_logic;
288
 
289 169 jshamlet
  constant INT_VECTOR_0      : ADDRESS_TYPE := ISR_Start_Addr;
290
  constant INT_VECTOR_1      : ADDRESS_TYPE := ISR_Start_Addr+2;
291
  constant INT_VECTOR_2      : ADDRESS_TYPE := ISR_Start_Addr+4;
292
  constant INT_VECTOR_3      : ADDRESS_TYPE := ISR_Start_Addr+6;
293
  constant INT_VECTOR_4      : ADDRESS_TYPE := ISR_Start_Addr+8;
294
  constant INT_VECTOR_5      : ADDRESS_TYPE := ISR_Start_Addr+10;
295
  constant INT_VECTOR_6      : ADDRESS_TYPE := ISR_Start_Addr+12;
296
  constant INT_VECTOR_7      : ADDRESS_TYPE := ISR_Start_Addr+14;
297
 
298 187 jshamlet
  signal CPU_Next_State      : CPU_STATES := IPF_C0;
299
  signal CPU_State           : CPU_STATES := IPF_C0;
300 169 jshamlet
 
301 225 jshamlet
  signal CPU_Halt_Req        : std_logic := '0';
302
  signal CPU_Halt_Ack        : std_logic := '0';
303 187 jshamlet
 
304 169 jshamlet
  signal Cache_Ctrl          : CACHE_MODES := CACHE_IDLE;
305
 
306
  signal Opcode              : OPCODE_TYPE := (others => '0');
307
  signal SubOp, SubOp_p1     : SUBOP_TYPE  := (others => '0');
308
 
309
  signal Prefetch            : DATA_TYPE   := x"00";
310
  signal Operand1, Operand2  : DATA_TYPE   := x"00";
311
 
312
  signal Instr_Prefetch      : std_logic   := '0';
313
 
314
  signal PC_Ctrl             : PC_CTRL_TYPE;
315
  signal Program_Ctr         : ADDRESS_TYPE := x"0000";
316
 
317 182 jshamlet
  signal ALU_Ctrl            : ALU_CTRL_TYPE;
318
  signal Regfile             : REGFILE_TYPE;
319
  signal Flags               : FLAG_TYPE;
320
  signal Mult                : ADDRESS_TYPE := x"0000";
321
 
322 169 jshamlet
  signal SP_Ctrl             : SP_CTRL_TYPE;
323
  signal Stack_Ptr           : ADDRESS_TYPE := x"0000";
324
 
325
  signal DP_Ctrl             : DATA_CTRL_TYPE;
326
 
327
  signal INT_Ctrl            : INT_CTRL_TYPE;
328
  signal Ack_D, Ack_Q, Ack_Q1: std_logic   := '0';
329
  signal Int_Req, Int_Ack    : std_logic   := '0';
330 245 jshamlet
  signal Set_Mask            : std_logic   := '0';
331 169 jshamlet
  signal Int_Mask            : DATA_TYPE   := x"00";
332
  signal ISR_Addr            : ADDRESS_TYPE := x"0000";
333
  signal i_Ints              : INTERRUPT_BUNDLE := x"00";
334
  signal Pending             : INTERRUPT_BUNDLE := x"00";
335
  signal Wait_for_FSM        : std_logic := '0';
336 210 jshamlet
  signal Wait_for_ISR        : std_logic := '0';
337 169 jshamlet
 
338 252 jshamlet
  signal IDX_Offset          : ADDRESS_TYPE := x"0000";
339
 
340
  signal IDX_Reg_l           : integer := 0;
341
  signal IDX_Reg_h           : integer := 0;
342
 
343
  signal IDX_NoOffset_Calc   : ADDRESS_TYPE := x"0000";
344
  signal IDX_Offset_Calc     : ADDRESS_TYPE := x"0000";
345
 
346 169 jshamlet
begin
347
 
348 224 jshamlet
-------------------------------------------------------------------------------
349
-- Reset & uSec Tick
350
-------------------------------------------------------------------------------
351 185 jshamlet
 
352 224 jshamlet
  CPU_Reset_Sync: process( Clock, PLL_Locked )
353
  begin
354
    if( PLL_Locked = '0' )then
355
      Reset_q                <= Reset_Level;
356
      Reset                  <= Reset_Level;
357
    elsif( rising_edge(Clock) )then
358
      Reset_q                <= not Reset_Level;
359
      Reset                  <= Reset_q;
360
    end if;
361
  end process;
362
 
363
  uSec_Tick_proc: process( Clock, Reset )
364
  begin
365
    if( Reset = Reset_Level )then
366
      uSec_Cntr              <= USEC_DLY;
367
      uSec_Tick              <= '0';
368
    elsif( rising_edge( Clock ) )then
369
      uSec_Cntr              <= uSec_Cntr - 1;
370
      if( or_reduce(uSec_Cntr) = '0' )then
371
        uSec_Cntr            <= USEC_DLY;
372
      end if;
373
      uSec_Tick              <= nor_reduce(uSec_Cntr);
374
    end if;
375
  end process;
376
 
377
  Open8_Bus.Clock            <= Clock;
378
  Open8_Bus.Reset            <= Reset;
379
  Open8_Bus.uSec_Tick        <= uSec_Tick;
380
 
381 169 jshamlet
-------------------------------------------------------------------------------
382 182 jshamlet
-- Address bus selection/generation logic
383 169 jshamlet
-------------------------------------------------------------------------------
384
 
385 252 jshamlet
  -- The original model treated the offset to LDO/STO as a signed value
386
  --  allowing access to locations -128 to +127 from [Rn+1:Rn]. This isn't
387
  --  always helpful, so the generic allows the CPU to use unsigned math
388
  --  for the offsets. This makes the range 0 to +255 instead.
389 253 jshamlet
 
390 252 jshamlet
Unsigned_Idx_Offsets : if( Unsigned_Index_Offsets )generate
391
  IDX_Offset(15 downto 8)    <= (others => '0');
392
  IDX_Offset(7 downto 0)     <= Operand1;
393
end generate;
394
 
395
Signed_Idx_Offsets: if( not Unsigned_Index_Offsets )generate
396
  IDX_Offset(15 downto 8)    <= (others => Operand1(7));
397
  IDX_Offset(7 downto 0)     <= Operand1;
398
end generate;
399
 
400
  -- Enable_Auto_Increment uses the LSB to determine whether or not to
401
  --  do the auto-increment, so we need to lock the LSB for each operand
402
  --  if it is enabled. This forces [ODD:EVEN] pairing.
403
 
404
Auto_Incr_Set: if( Enable_Auto_Increment )generate
405
  IDX_Reg_l                  <= conv_integer(SubOp(2 downto 1) & '0');
406
  IDX_Reg_h                  <= conv_integer(SubOp(2 downto 1) & '1');
407
end generate;
408
 
409
Auto_Incr_Not_Set: if( not Enable_Auto_Increment )generate
410
  IDX_Reg_l                  <= conv_integer(SubOp);
411
  IDX_Reg_h                  <= conv_integer(SubOp_p1);
412
end generate;
413
 
414
  -- Pipeline registers for the indexed and indexed with offset addresses.
415
  Idx_Addr_Calc_proc: process( Clock, Reset )
416 169 jshamlet
    variable Reg, Reg_1      : integer range 0 to 7 := 0;
417
  begin
418 252 jshamlet
    if( Reset = Reset_Level )then
419
      IDX_NoOffset_Calc      <= x"0000";
420
      IDX_Offset_Calc        <= x"0000";
421
    elsif( rising_edge(Clock))then
422
      IDX_NoOffset_Calc      <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l));
423
      IDX_Offset_Calc        <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l)) +
424
                                IDX_Offset;
425 182 jshamlet
    end if;
426 252 jshamlet
  end process;
427 182 jshamlet
 
428 252 jshamlet
  -- Address selection logic based on current CPU state. This is combinatorial,
429
  --  as adding pipeline registration would add a clock cycle to every instr,
430
  --  without really adding the Fmax to compensate.
431
  Address_Logic: process(CPU_State, Operand1, Operand2, IDX_NoOffset_Calc,
432
                         IDX_Offset_Calc, ISR_Addr, Stack_Ptr, Program_Ctr )
433
  begin
434 182 jshamlet
    case( CPU_State )is
435
 
436
      when LDA_C2 | STA_C2 =>
437 223 jshamlet
        Open8_Bus.Address    <= Operand2 & Operand1;
438 182 jshamlet
 
439
      when LDX_C1 | STX_C1 =>
440 252 jshamlet
        Open8_Bus.Address    <= IDX_NoOffset_Calc;
441 182 jshamlet
 
442 252 jshamlet
      when LDO_C2 | STO_C2 =>
443
        Open8_Bus.Address    <= IDX_Offset_Calc;
444 182 jshamlet
 
445
      when ISR_C1 | ISR_C2 =>
446 223 jshamlet
        Open8_Bus.Address    <= ISR_Addr;
447 182 jshamlet
 
448
      when PSH_C1 | POP_C1 | ISR_C3 | JSR_C1 | JSR_C2 | RTS_C1 | RTS_C2 | RTS_C3 =>
449 223 jshamlet
        Open8_Bus.Address    <= Stack_Ptr;
450 182 jshamlet
 
451
      when others =>
452 223 jshamlet
        Open8_Bus.Address    <= Program_Ctr;
453 182 jshamlet
 
454
    end case;
455
  end process;
456
 
457
-------------------------------------------------------------------------------
458
-- Combinatorial portion of CPU finite state machine
459
-- State Logic / Instruction Decoding & Execution
460
-------------------------------------------------------------------------------
461
 
462 187 jshamlet
  State_Logic: process(CPU_State, Flags, Int_Mask, CPU_Halt_Req, Opcode,
463 182 jshamlet
                       SubOp , SubOp_p1, Operand1, Operand2, Int_Req )
464
    variable Reg             : integer range 0 to 7 := 0;
465
  begin
466 169 jshamlet
    CPU_Next_State           <= CPU_State;
467
    Cache_Ctrl               <= CACHE_IDLE;
468
    --
469 185 jshamlet
    PC_Ctrl.Oper             <= PC_INCR;
470
    PC_Ctrl.Offset           <= PC_IDLE;
471 182 jshamlet
    --
472 169 jshamlet
    ALU_Ctrl.Oper            <= ALU_IDLE;
473
    ALU_Ctrl.Reg             <= ACCUM;
474
    --
475
    SP_Ctrl.Oper             <= SP_IDLE;
476
    --
477
    DP_Ctrl.Src              <= DATA_RD_MEM;
478
    DP_Ctrl.Reg              <= ACCUM;
479
    --
480
    INT_Ctrl.Mask_Set        <= '0';
481
    INT_Ctrl.Soft_Ints       <= x"00";
482
    INT_Ctrl.Incr_ISR        <= '0';
483
    Ack_D                    <= '0';
484 225 jshamlet
    --
485 182 jshamlet
    Reg                     := conv_integer(SubOp);
486 225 jshamlet
    --
487
    CPU_Halt_Ack             <= '0';
488 169 jshamlet
 
489
    case CPU_State is
490
-------------------------------------------------------------------------------
491
-- Initial Instruction fetch & decode
492
-------------------------------------------------------------------------------
493 187 jshamlet
      when IPF_C0 =>
494
        CPU_Next_State       <= IPF_C1;
495 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
496 169 jshamlet
 
497 187 jshamlet
      when IPF_C1 =>
498
        CPU_Next_State       <= IPF_C2;
499 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
500 169 jshamlet
 
501 187 jshamlet
      when IPF_C2 =>
502
        CPU_Next_State       <= IDC_C0;
503 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
504 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
505 169 jshamlet
 
506 187 jshamlet
      when IDC_C0 =>
507
        CPU_Next_State       <= IDC_C0;
508 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
509
 
510
        case Opcode is
511
          when OP_PSH =>
512
            CPU_Next_State   <= PSH_C1;
513
            Cache_Ctrl       <= CACHE_PREFETCH;
514 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
515 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
516
            DP_Ctrl.Reg      <= SubOp;
517
 
518
          when OP_POP =>
519
            CPU_Next_State   <= POP_C1;
520
            Cache_Ctrl       <= CACHE_PREFETCH;
521 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
522 169 jshamlet
            SP_Ctrl.Oper     <= SP_POP;
523
 
524
          when OP_BR0 | OP_BR1 =>
525
            CPU_Next_State   <= BRN_C1;
526
            Cache_Ctrl       <= CACHE_OPER1;
527 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
528 169 jshamlet
 
529 185 jshamlet
 
530 169 jshamlet
          when OP_DBNZ =>
531
            CPU_Next_State   <= DBNZ_C1;
532
            Cache_Ctrl       <= CACHE_OPER1;
533 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
534 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_DEC;
535
            ALU_Ctrl.Reg     <= SubOp;
536
 
537
          when OP_INT =>
538 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
539 187 jshamlet
            -- Make sure the requested interrupt is actually enabled first.
540
            --  Also, unlike CPU_Halt, the INT instruction is actually being
541
            --  executed, so go ahead and increment the program counter before
542
            --  pausing so the CPU restarts on the next instruction.
543 169 jshamlet
            if( Int_Mask(Reg) = '1' )then
544 187 jshamlet
              CPU_Next_State <= WAI_Cx;
545 169 jshamlet
              INT_Ctrl.Soft_Ints(Reg) <= '1';
546
            end if;
547
 
548
          when OP_STK =>
549
            case SubOp is
550
              when SOP_RSP  =>
551 185 jshamlet
                PC_Ctrl.Offset <= PC_NEXT;
552 181 jshamlet
                if( not Allow_Stack_Address_Move )then
553 187 jshamlet
                  -- The default behavior for this instruction is to simply
554
                  --  repoint the SP to the HDL default
555 185 jshamlet
                  SP_Ctrl.Oper    <= SP_CLR;
556 181 jshamlet
                end if;
557 187 jshamlet
                if( Allow_Stack_Address_Move and
558
                    Flags(Stack_Xfer_Flag) = '1' )then
559
                  -- If RSP is set to allow SP moves, and the specified flag
560
                  --  is true, then signal the stack pointer logic to load
561
                  --  from R1:R0
562 185 jshamlet
                  SP_Ctrl.Oper    <= SP_SET;
563 181 jshamlet
                end if;
564 187 jshamlet
                if( Allow_Stack_Address_Move and
565
                    Flags(Stack_Xfer_Flag) = '0')then
566
                  -- If RSP is set to allow SP moves, and the specified flag
567
                  --  is false, then signal the ALU to copy the stack pointer
568
                  --  to R1:R0
569 185 jshamlet
                  ALU_Ctrl.Oper   <= ALU_RSP;
570 181 jshamlet
                end if;
571 169 jshamlet
 
572
              when SOP_RTS | SOP_RTI =>
573 185 jshamlet
                CPU_Next_State    <= RTS_C1;
574 190 jshamlet
                Cache_Ctrl        <= CACHE_IDLE;
575 185 jshamlet
                SP_Ctrl.Oper      <= SP_POP;
576 169 jshamlet
 
577
              when SOP_BRK  =>
578
                if( BRK_Implements_WAI )then
579 187 jshamlet
                  -- If BRK_Implements_WAI, then jump to the WAI_Cx and
580
                  --  increment the PC similar to an ISR flow.
581
                  CPU_Next_State  <= WAI_Cx;
582 185 jshamlet
                  PC_Ctrl.Offset  <= PC_NEXT;
583 187 jshamlet
                else
584
                -- If Break is implemented normally, back the PC up by
585
                --  2 and return through IPF_C0 in order to execute a 5
586
                --  clock cycle delay
587
                  CPU_Next_State  <= BRK_C1;
588
                  PC_Ctrl.Offset  <= PC_REV2;
589 169 jshamlet
                end if;
590
 
591
              when SOP_JMP  =>
592 185 jshamlet
                CPU_Next_State    <= JMP_C1;
593
                Cache_Ctrl        <= CACHE_OPER1;
594 169 jshamlet
 
595
              when SOP_SMSK =>
596 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
597 169 jshamlet
                INT_Ctrl.Mask_Set <= '1';
598
 
599
              when SOP_GMSK =>
600 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
601
                ALU_Ctrl.Oper     <= ALU_GMSK;
602 169 jshamlet
 
603
              when SOP_JSR =>
604
                CPU_Next_State <= JSR_C1;
605 185 jshamlet
                Cache_Ctrl        <= CACHE_OPER1;
606
                DP_Ctrl.Src       <= DATA_WR_PC;
607
                DP_Ctrl.Reg       <= PC_MSB;
608 169 jshamlet
 
609
              when others => null;
610
            end case;
611
 
612
          when OP_MUL =>
613
            CPU_Next_State   <= MUL_C1;
614 181 jshamlet
            -- Multiplication requires a single clock cycle to calculate PRIOR
615
            --  to the ALU writing the result to registers. As a result, this
616
            --  state needs to idle the ALU initially, and back the PC up by 1
617
            -- We can get away with only 1 extra clock by pre-fetching the
618
            --  next instruction, though.
619 169 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
620 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
621 181 jshamlet
            -- Note that both the multiply process AND ALU process need the
622
            --  source register for Rn (R1:R0 = R0 * Rn). Assert ALU_Ctrl.reg
623
            --  now, but hold off on the ALU command until the next state.
624 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_IDLE;
625
            ALU_Ctrl.Reg     <= SubOp;
626
 
627
          when OP_UPP =>
628
            CPU_Next_State   <= UPP_C1;
629
            Cache_Ctrl       <= CACHE_PREFETCH;
630 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
631 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
632
            ALU_Ctrl.Reg     <= SubOp;
633
 
634
          when OP_LDA =>
635
            CPU_Next_State   <= LDA_C1;
636
            Cache_Ctrl       <= CACHE_OPER1;
637
 
638
          when OP_LDI =>
639
            CPU_Next_State   <= LDI_C1;
640
            Cache_Ctrl       <= CACHE_OPER1;
641 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
642 169 jshamlet
 
643
          when OP_LDO =>
644
            CPU_Next_State   <= LDO_C1;
645
            Cache_Ctrl       <= CACHE_OPER1;
646 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
647 169 jshamlet
 
648
          when OP_LDX =>
649
            CPU_Next_State   <= LDX_C1;
650 181 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
651 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
652 169 jshamlet
 
653
          when OP_STA =>
654
            CPU_Next_State   <= STA_C1;
655
            Cache_Ctrl       <= CACHE_OPER1;
656
 
657
          when OP_STO =>
658
            CPU_Next_State   <= STO_C1;
659
            Cache_Ctrl       <= CACHE_OPER1;
660 252 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
661 169 jshamlet
 
662
          when OP_STX =>
663
            CPU_Next_State   <= STX_C1;
664
            Cache_Ctrl       <= CACHE_PREFETCH;
665 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
666 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
667
            DP_Ctrl.Reg      <= ACCUM;
668
 
669 244 jshamlet
          when OP_STP =>
670
            PC_Ctrl.Offset   <= PC_NEXT;
671
            if( Supervisor_Mode )then
672
              if( SubOp /= PSR_I )then
673
                ALU_Ctrl.Oper  <= Opcode;
674
                ALU_Ctrl.Reg   <= SubOp;
675
              end if;
676
            else
677
              ALU_Ctrl.Oper  <= Opcode;
678
              ALU_Ctrl.Reg   <= SubOp;
679
            end if;
680
 
681 169 jshamlet
          when others =>
682 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
683 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
684
            ALU_Ctrl.Reg     <= SubOp;
685
 
686
        end case;
687
 
688 186 jshamlet
        if( Int_Req = '1' )then
689
          CPU_Next_State     <= ISR_C1;
690 187 jshamlet
        end if;
691
 
692
        if( CPU_Halt_Req = '1' )then
693
          CPU_Next_State     <= WAH_Cx;
694
        end if;
695
 
696
        -- If either of these override conditions are true, the decoder needs
697
        --  to undo everything it just setup, since even "single-cycle"
698
        --  instructions will be executed again upon return.
699
        if( Int_Req = '1' or CPU_Halt_Req = '1' )then
700
          -- In either case, we want to skip loading the cache, as the cache
701
          --  will be invalid by the time we get back.
702 186 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
703 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to the current instruction,
704
          -- compensating for the pipeline registers.
705 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
706
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
707 187 jshamlet
          --  operation due to the current instruction.
708 186 jshamlet
          ALU_Ctrl.Oper      <= ALU_IDLE;
709
          SP_Ctrl.Oper       <= SP_IDLE;
710 187 jshamlet
          -- Interrupt logic outside of the state machine needs this to be set
711
          --  to DATA_RD_MEM, while CPU_Halt considers this a "don't care".
712 186 jshamlet
          DP_Ctrl.Src        <= DATA_RD_MEM;
713 187 jshamlet
          -- If an INT/SMSK instruction was going to be executed, it will get
714
          --  executed again when normal processing resumes, so axe their
715
          --  requests for now.
716
          INT_Ctrl.Mask_Set       <= '0';
717
          INT_Ctrl.Soft_Ints(Reg) <= '0';
718 186 jshamlet
        end if;
719
 
720 169 jshamlet
-------------------------------------------------------------------------------
721
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
722
-------------------------------------------------------------------------------
723
 
724
      when BRN_C1 =>
725 187 jshamlet
        CPU_Next_State       <= IDC_C0;
726 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
727 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
728 169 jshamlet
        if( Flags(Reg) = Opcode(0) )then
729 187 jshamlet
          CPU_Next_State     <= IPF_C0;
730 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
731
          PC_Ctrl.Offset     <= Operand1;
732
        end if;
733
 
734
      when DBNZ_C1 =>
735 187 jshamlet
        CPU_Next_State       <= IDC_C0;
736 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
737 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
738
        if( Flags(PSR_Z) = '0' )then
739 187 jshamlet
          CPU_Next_State     <= IPF_C0;
740 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
741
          PC_Ctrl.Offset     <= Operand1;
742
        end if;
743
 
744
      when JMP_C1 =>
745
        CPU_Next_State       <= JMP_C2;
746
        Cache_Ctrl           <= CACHE_OPER2;
747
 
748
      when JMP_C2 =>
749 187 jshamlet
        CPU_Next_State       <= IPF_C0;
750 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
751
 
752
-------------------------------------------------------------------------------
753
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
754
-------------------------------------------------------------------------------
755
 
756
      when LDA_C1 =>
757
        CPU_Next_State       <= LDA_C2;
758
        Cache_Ctrl           <= CACHE_OPER2;
759
 
760
      when LDA_C2 =>
761
        CPU_Next_State       <= LDA_C3;
762
 
763
      when LDA_C3 =>
764
        CPU_Next_State       <= LDA_C4;
765 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
766 169 jshamlet
 
767
      when LDA_C4 =>
768
        CPU_Next_State       <= LDI_C1;
769
        Cache_Ctrl           <= CACHE_OPER1;
770 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
771 169 jshamlet
 
772
      when LDI_C1 =>
773 187 jshamlet
        CPU_Next_State       <= IDC_C0;
774 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
775 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
776 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
777
        ALU_Ctrl.Reg         <= SubOp;
778
 
779
      when LDO_C1 =>
780 252 jshamlet
        CPU_Next_State       <= LDO_C2;
781
 
782
      when LDO_C2 =>
783 181 jshamlet
        CPU_Next_State       <= LDX_C2;
784 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
785 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
786
          ALU_Ctrl.Oper      <= ALU_UPP;
787
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
788 169 jshamlet
        end if;
789
 
790
      when LDX_C1 =>
791
        CPU_Next_State       <= LDX_C2;
792 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
793
          ALU_Ctrl.Oper      <= ALU_UPP;
794
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
795 181 jshamlet
        end if;
796 169 jshamlet
 
797
      when LDX_C2 =>
798
        CPU_Next_State       <= LDX_C3;
799 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
800 181 jshamlet
 
801
      when LDX_C3 =>
802
        CPU_Next_State       <= LDX_C4;
803 182 jshamlet
        Cache_Ctrl           <= CACHE_OPER1;
804 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
805 169 jshamlet
 
806 181 jshamlet
      when LDX_C4 =>
807 187 jshamlet
        CPU_Next_State       <= IDC_C0;
808 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
809 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
810 181 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
811 169 jshamlet
        ALU_Ctrl.Reg         <= ACCUM;
812
 
813
-------------------------------------------------------------------------------
814
-- Data Storage - Store to memory (STA, STO, STX)
815
-------------------------------------------------------------------------------
816
      when STA_C1 =>
817
        CPU_Next_State       <= STA_C2;
818
        Cache_Ctrl           <= CACHE_OPER2;
819
        DP_Ctrl.Src          <= DATA_WR_REG;
820
        DP_Ctrl.Reg          <= SubOp;
821
 
822
      when STA_C2 =>
823
        CPU_Next_State       <= STA_C3;
824 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
825 169 jshamlet
 
826
      when STA_C3 =>
827 187 jshamlet
        CPU_Next_State       <= IPF_C2;
828 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
829 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
830 169 jshamlet
 
831
      when STO_C1 =>
832 252 jshamlet
        CPU_Next_State       <= STO_C2;
833 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
834 252 jshamlet
        DP_Ctrl.Src          <= DATA_WR_REG;
835
        DP_Ctrl.Reg          <= ACCUM;
836
 
837
      when STO_C2 =>
838
        CPU_Next_State       <= IPF_C1;
839 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
840 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
841 252 jshamlet
          CPU_Next_State     <= STO_C3;
842 182 jshamlet
          ALU_Ctrl.Oper      <= ALU_UPP;
843
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
844 169 jshamlet
        end if;
845
 
846 252 jshamlet
      when STO_C3 =>
847
        CPU_Next_State       <= IPF_C2;
848 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
849 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
850
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
851
 
852
      when STX_C1 =>
853 187 jshamlet
        CPU_Next_State       <= IPF_C1;
854 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
855 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
856
          CPU_Next_State     <= STX_C2;
857
          ALU_Ctrl.Oper      <= ALU_UPP;
858
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
859 169 jshamlet
        end if;
860
 
861
      when STX_C2 =>
862 187 jshamlet
        CPU_Next_State       <= IPF_C2;
863 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
864 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
865
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
866
 
867
-------------------------------------------------------------------------------
868
-- Multi-Cycle Math Operations (UPP, MUL)
869
-------------------------------------------------------------------------------
870
 
871
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
872 181 jshamlet
      --  instruction/first operand, we have to return through PF2. Also, we
873
      --  need to tell the ALU to store the results to R1:R0 here. Note that
874
      --  there is no ALU_Ctrl.Reg, as this is implied in the ALU instruction
875 169 jshamlet
      when MUL_C1 =>
876 187 jshamlet
        CPU_Next_State       <= IPF_C2;
877 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
878 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_MUL;
879
 
880
      when UPP_C1 =>
881 187 jshamlet
        CPU_Next_State       <= IPF_C2;
882 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
883 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
884
        ALU_Ctrl.Reg         <= SubOp_p1;
885
 
886
-------------------------------------------------------------------------------
887
-- Basic Stack Manipulation (PSH, POP, RSP)
888
-------------------------------------------------------------------------------
889
      when PSH_C1 =>
890 187 jshamlet
        CPU_Next_State       <= IPF_C1;
891 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
892
 
893
      when POP_C1 =>
894
        CPU_Next_State       <= POP_C2;
895
 
896
      when POP_C2 =>
897
        CPU_Next_State       <= POP_C3;
898 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
899 169 jshamlet
 
900
      when POP_C3 =>
901
        CPU_Next_State       <= POP_C4;
902
        Cache_Ctrl           <= CACHE_OPER1;
903 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
904 169 jshamlet
 
905
      when POP_C4 =>
906 187 jshamlet
        CPU_Next_State       <= IDC_C0;
907 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
908 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
909 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_POP;
910
        ALU_Ctrl.Reg         <= SubOp;
911 172 jshamlet
 
912 169 jshamlet
-------------------------------------------------------------------------------
913
-- Subroutines & Interrupts (RTS, JSR)
914
-------------------------------------------------------------------------------
915 187 jshamlet
      when WAI_Cx => -- For soft interrupts only, halt the Program_Ctr
916 169 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
917 186 jshamlet
        if( Int_Req = '1' )then
918
          CPU_Next_State     <= ISR_C1;
919 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to would have been the next
920
          --  instruction, compensating for the pipeline registers.
921 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
922
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
923
          --  operation due to the current instruction
924
          DP_Ctrl.Src        <= DATA_RD_MEM;
925
        end if;
926 169 jshamlet
 
927 187 jshamlet
      when WAH_Cx => -- Holds until CPU_Halt_Req is deasserted.
928 225 jshamlet
        CPU_Halt_Ack         <= '1';
929 187 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
930
        if( CPU_Halt_Req = '0' )then
931
          CPU_Next_State     <= IPF_C0;
932
          DP_Ctrl.Src        <= DATA_RD_MEM;
933
        end if;
934
 
935
      when BRK_C1 => -- Debugging (BRK) Performs a 5-clock NOP.
936
        CPU_Next_State       <= IPF_C0;
937
 
938 169 jshamlet
      when ISR_C1 =>
939
        CPU_Next_State       <= ISR_C2;
940
        INT_Ctrl.Incr_ISR    <= '1';
941
 
942
      when ISR_C2 =>
943
        CPU_Next_State       <= ISR_C3;
944
        DP_Ctrl.Src          <= DATA_WR_FLAG;
945
 
946
      when ISR_C3 =>
947
        CPU_Next_State       <= JSR_C1;
948
        Cache_Ctrl           <= CACHE_OPER1;
949 182 jshamlet
        ALU_Ctrl.Oper        <= ALU_STP;
950 185 jshamlet
        ALU_Ctrl.Reg         <= conv_std_logic_vector(PSR_I,3);
951 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
952
        DP_Ctrl.Src          <= DATA_WR_PC;
953 182 jshamlet
        DP_Ctrl.Reg          <= PC_MSB;
954 169 jshamlet
        Ack_D                <= '1';
955
 
956
      when JSR_C1 =>
957
        CPU_Next_State       <= JSR_C2;
958
        Cache_Ctrl           <= CACHE_OPER2;
959
        SP_Ctrl.Oper         <= SP_PUSH;
960
        DP_Ctrl.Src          <= DATA_WR_PC;
961 182 jshamlet
        DP_Ctrl.Reg          <= PC_LSB;
962 169 jshamlet
 
963
      when JSR_C2 =>
964 187 jshamlet
        CPU_Next_State       <= IPF_C0;
965 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
966 182 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
967 169 jshamlet
 
968
      when RTS_C1 =>
969
        CPU_Next_State       <= RTS_C2;
970
        SP_Ctrl.Oper         <= SP_POP;
971
 
972
      when RTS_C2 =>
973
        CPU_Next_State       <= RTS_C3;
974
        -- if this is an RTI, then we need to POP the flags
975
        if( SubOp = SOP_RTI )then
976
          SP_Ctrl.Oper       <= SP_POP;
977
        end if;
978
 
979
      when RTS_C3 =>
980
        CPU_Next_State       <= RTS_C4;
981
        Cache_Ctrl           <= CACHE_OPER1;
982
 
983
      when RTS_C4 =>
984
        CPU_Next_State       <= RTS_C5;
985
        Cache_Ctrl           <= CACHE_OPER2;
986
 
987
      when RTS_C5 =>
988 187 jshamlet
        CPU_Next_State       <= IPF_C0;
989 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
990 185 jshamlet
        -- if this is an RTI, then we need to clear the I bit
991 169 jshamlet
        if( SubOp = SOP_RTI )then
992
          CPU_Next_State     <= RTI_C6;
993
          Cache_Ctrl         <= CACHE_OPER1;
994 185 jshamlet
          ALU_Ctrl.Oper      <= ALU_CLP;
995
          ALU_Ctrl.Reg       <= conv_std_logic_vector(PSR_I,3);
996 169 jshamlet
        end if;
997
 
998
      when RTI_C6 =>
999 187 jshamlet
        CPU_Next_State       <= IPF_C1;
1000 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
1001 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_RFLG;
1002
 
1003
      when others =>
1004
        null;
1005
    end case;
1006
 
1007
  end process;
1008
 
1009
-------------------------------------------------------------------------------
1010
-- Registered portion of CPU finite state machine
1011
-------------------------------------------------------------------------------
1012 182 jshamlet
 
1013 169 jshamlet
  CPU_Regs: process( Reset, Clock )
1014
    variable Offset_SX       : ADDRESS_TYPE;
1015 188 jshamlet
    variable i_Ints          : INTERRUPT_BUNDLE := x"00";
1016 169 jshamlet
    variable Index           : integer range 0 to 7         := 0;
1017
    variable Sum             : std_logic_vector(8 downto 0) := "000000000";
1018
    variable Temp            : std_logic_vector(8 downto 0) := "000000000";
1019
  begin
1020
    if( Reset = Reset_Level )then
1021 187 jshamlet
      CPU_State              <= IPF_C0;
1022 169 jshamlet
      Opcode                 <= OP_INC;
1023
      SubOp                  <= ACCUM;
1024
      SubOp_p1               <= ACCUM;
1025
      Operand1               <= x"00";
1026
      Operand2               <= x"00";
1027
      Instr_Prefetch         <= '0';
1028
      Prefetch               <= x"00";
1029
 
1030 187 jshamlet
      CPU_Halt_Req           <= '0';
1031 225 jshamlet
      Halt_Ack               <= '0';
1032 187 jshamlet
 
1033 223 jshamlet
      Open8_Bus.Wr_En        <= '0';
1034
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1035
      Open8_Bus.Rd_En        <= '1';
1036 169 jshamlet
 
1037
      Program_Ctr            <= Program_Start_Addr;
1038
      Stack_Ptr              <= Stack_Start_Addr;
1039
 
1040
      Ack_Q                  <= '0';
1041
      Ack_Q1                 <= '0';
1042
      Int_Ack                <= '0';
1043
 
1044
      Int_Req                <= '0';
1045
      Pending                <= x"00";
1046
      Wait_for_FSM           <= '0';
1047 210 jshamlet
      Wait_for_ISR           <= '0';
1048 245 jshamlet
      Set_Mask               <= '0';
1049 169 jshamlet
      if( Enable_NMI )then
1050
        Int_Mask             <= Default_Interrupt_Mask(7 downto 1) & '1';
1051
      else
1052
        Int_Mask             <= Default_Interrupt_Mask;
1053
      end if;
1054
      ISR_Addr               <= INT_VECTOR_0;
1055
 
1056
      for i in 0 to 7 loop
1057 188 jshamlet
        Regfile(i)           <= x"00";
1058 169 jshamlet
      end loop;
1059
      Flags                  <= x"00";
1060 248 jshamlet
      if( Supervisor_Mode )then
1061 244 jshamlet
        Flags(PSR_I)         <= '1';
1062
      end if;
1063 169 jshamlet
 
1064 224 jshamlet
      Open8_Bus.GP_Flags     <= (others => '0');
1065 188 jshamlet
 
1066 169 jshamlet
    elsif( rising_edge(Clock) )then
1067 187 jshamlet
 
1068 225 jshamlet
      CPU_Halt_Req           <= Halt_Req;
1069
      Halt_Ack               <= CPU_Halt_Ack;
1070 187 jshamlet
 
1071 223 jshamlet
      Open8_Bus.Wr_En        <= '0';
1072
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1073
      Open8_Bus.Rd_En        <= '0';
1074 169 jshamlet
 
1075
-------------------------------------------------------------------------------
1076
-- Instruction/Operand caching for pipelined memory access
1077
-------------------------------------------------------------------------------
1078
      CPU_State              <= CPU_Next_State;
1079
      case Cache_Ctrl is
1080
        when CACHE_INSTR =>
1081
          Opcode             <= Rd_Data(7 downto 3);
1082
          SubOp              <= Rd_Data(2 downto 0);
1083
          SubOp_p1           <= Rd_Data(2 downto 0) + 1;
1084
          if( Instr_Prefetch = '1' )then
1085
            Opcode           <= Prefetch(7 downto 3);
1086
            SubOp            <= Prefetch(2 downto 0);
1087
            SubOp_p1         <= Prefetch(2 downto 0) + 1;
1088
            Instr_Prefetch   <= '0';
1089
          end if;
1090
 
1091
        when CACHE_OPER1 =>
1092
          Operand1           <= Rd_Data;
1093
 
1094
        when CACHE_OPER2 =>
1095
          Operand2           <= Rd_Data;
1096
 
1097
        when CACHE_PREFETCH =>
1098
          Prefetch           <= Rd_Data;
1099
          Instr_Prefetch     <= '1';
1100
 
1101
        when CACHE_IDLE =>
1102
          null;
1103
      end case;
1104
 
1105
-------------------------------------------------------------------------------
1106
-- Program Counter
1107
-------------------------------------------------------------------------------
1108
      Offset_SX(15 downto 8) := (others => PC_Ctrl.Offset(7));
1109
      Offset_SX(7 downto 0)  := PC_Ctrl.Offset;
1110
 
1111
      case PC_Ctrl.Oper is
1112
        when PC_INCR =>
1113
          Program_Ctr        <= Program_Ctr + Offset_SX - 2;
1114
 
1115
        when PC_LOAD =>
1116 185 jshamlet
          Program_Ctr        <= Operand2 & Operand1;
1117 169 jshamlet
 
1118
        when others =>
1119
          null;
1120
      end case;
1121
 
1122
-------------------------------------------------------------------------------
1123
-- (Write) Data Path
1124
-------------------------------------------------------------------------------
1125
      case DP_Ctrl.Src is
1126
        when DATA_BUS_IDLE =>
1127
          null;
1128
 
1129
        when DATA_RD_MEM =>
1130 223 jshamlet
          Open8_Bus.Rd_En    <= '1';
1131 169 jshamlet
 
1132
        when DATA_WR_REG =>
1133 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1134
          Open8_Bus.Wr_Data  <= Regfile(conv_integer(DP_Ctrl.Reg));
1135 169 jshamlet
 
1136
        when DATA_WR_FLAG =>
1137 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1138
          Open8_Bus.Wr_Data  <= Flags;
1139 169 jshamlet
 
1140
        when DATA_WR_PC =>
1141 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1142
          Open8_Bus.Wr_Data  <= Program_Ctr(15 downto 8);
1143 182 jshamlet
          if( DP_Ctrl.Reg = PC_LSB )then
1144 223 jshamlet
            Open8_Bus.Wr_Data <= Program_Ctr(7 downto 0);
1145 169 jshamlet
          end if;
1146
 
1147
        when others =>
1148
          null;
1149
      end case;
1150
 
1151
-------------------------------------------------------------------------------
1152
-- Stack Pointer
1153
-------------------------------------------------------------------------------
1154
      case SP_Ctrl.Oper is
1155
        when SP_IDLE =>
1156
          null;
1157
 
1158 181 jshamlet
        when SP_CLR =>
1159 169 jshamlet
          Stack_Ptr          <= Stack_Start_Addr;
1160
 
1161 181 jshamlet
        when SP_SET =>
1162 245 jshamlet
          if( Supervisor_Mode )then
1163
            if( Flags(PSR_I) = '1' )then
1164
              Stack_Ptr      <= Regfile(1) & Regfile(0);
1165
            end if;
1166
          else
1167
            Stack_Ptr        <= Regfile(1) & Regfile(0);
1168
          end if;
1169 181 jshamlet
 
1170 169 jshamlet
        when SP_POP  =>
1171
          Stack_Ptr          <= Stack_Ptr + 1;
1172
 
1173
        when SP_PUSH =>
1174
          Stack_Ptr          <= Stack_Ptr - 1;
1175
 
1176
        when others =>
1177
          null;
1178
 
1179
      end case;
1180
 
1181
-------------------------------------------------------------------------------
1182
-- Interrupt Controller
1183
-------------------------------------------------------------------------------
1184 245 jshamlet
 
1185
      -- If Supervisor_Mode is set, restrict the SMSK instruction such that it
1186
      --  requires the I bit to be set.
1187
      if( Supervisor_Mode )then
1188
        Set_Mask             <= INT_Ctrl.Mask_Set and Flags(PSR_I);
1189
      else
1190
        Set_Mask             <= INT_Ctrl.Mask_Set;
1191
      end if;
1192
 
1193 169 jshamlet
      -- The interrupt control mask is always sourced out of R0
1194 245 jshamlet
      if( Set_Mask = '1' )then
1195 169 jshamlet
        if( Enable_NMI )then
1196
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
1197
        else
1198
          Int_Mask           <= Regfile(conv_integer(ACCUM));
1199
        end if;
1200
      end if;
1201
 
1202
      -- Combine external and internal interrupts, and mask the OR of the two
1203
      --  with the mask. Record any incoming interrupts to the pending buffer
1204
      i_Ints                 := (Interrupts or INT_Ctrl.Soft_Ints) and
1205
                                Int_Mask;
1206 172 jshamlet
 
1207 169 jshamlet
      Pending                <= i_Ints or Pending;
1208
 
1209 210 jshamlet
      if( Sequential_Interrupts )then
1210
        Wait_for_ISR         <= Flags(PSR_I);
1211
      else
1212
        Wait_for_ISR         <= '0';
1213
      end if;
1214
 
1215
      if( Wait_for_FSM = '0' and Wait_for_ISR = '0' )then
1216 169 jshamlet
        if(    Pending(0) = '1' )then
1217
          ISR_Addr           <= INT_VECTOR_0;
1218
          Pending(0)         <= '0';
1219
        elsif( Pending(1) = '1' )then
1220
          ISR_Addr           <= INT_VECTOR_1;
1221
          Pending(1)         <= '0';
1222
        elsif( Pending(2) = '1' )then
1223
          ISR_Addr           <= INT_VECTOR_2;
1224
          Pending(2)         <= '0';
1225
        elsif( Pending(3) = '1' )then
1226
          ISR_Addr           <= INT_VECTOR_3;
1227
          Pending(3)         <= '0';
1228
        elsif( Pending(4) = '1' )then
1229
          ISR_Addr           <= INT_VECTOR_4;
1230
          Pending(4)         <= '0';
1231
        elsif( Pending(5) = '1' )then
1232
          ISR_Addr           <= INT_VECTOR_5;
1233
          Pending(5)         <= '0';
1234
        elsif( Pending(6) = '1' )then
1235
          ISR_Addr           <= INT_VECTOR_6;
1236
          Pending(6)         <= '0';
1237
        elsif( Pending(7) = '1' )then
1238
          ISR_Addr           <= INT_VECTOR_7;
1239
          Pending(7)         <= '0';
1240
        end if;
1241 185 jshamlet
        Wait_for_FSM         <= or_reduce(Pending);
1242 169 jshamlet
      end if;
1243
 
1244
      -- Reset the Wait_for_FSM flag on Int_Ack
1245
      Ack_Q                  <= Ack_D;
1246
      Ack_Q1                 <= Ack_Q;
1247
      Int_Ack                <= Ack_Q1;
1248
      if( Int_Ack = '1' )then
1249
        Wait_for_FSM         <= '0';
1250
      end if;
1251
 
1252
      Int_Req                <= Wait_for_FSM and (not Int_Ack);
1253
 
1254
      -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1255
      --  lower half of the address.
1256
      if( INT_Ctrl.Incr_ISR = '1' )then
1257
        ISR_Addr             <= ISR_Addr + 1;
1258
      end if;
1259
 
1260
-------------------------------------------------------------------------------
1261
-- ALU (Arithmetic / Logic Unit)
1262
-------------------------------------------------------------------------------
1263
      Index                  := conv_integer(ALU_Ctrl.Reg);
1264
      Sum                    := (others => '0');
1265
      Temp                   := (others => '0');
1266
 
1267
      case ALU_Ctrl.Oper is
1268
        when ALU_INC => -- Rn = Rn + 1 : Flags N,C,Z
1269
          Sum                := ("0" & x"01") +
1270
                                ("0" & Regfile(Index));
1271 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1272
          Flags(PSR_C)       <= Sum(8);
1273 209 jshamlet
          Flags(PSR_N)       <= Sum(7);
1274 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1275
 
1276
        when ALU_UPP => -- Rn = Rn + 1
1277
          Sum                := ("0" & x"01") +
1278
                                ("0" & Regfile(Index));
1279 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1280 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1281
 
1282
        when ALU_UPP2 => -- Rn = Rn + C
1283
          Sum                := ("0" & x"00") +
1284
                                ("0" & Regfile(Index)) +
1285 185 jshamlet
                                Flags(PSR_C);
1286
          Flags(PSR_C)       <= Sum(8);
1287 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1288
 
1289
        when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1290
          Sum                := ("0" & Regfile(0)) +
1291
                                ("0" & Regfile(Index)) +
1292 185 jshamlet
                                Flags(PSR_C);
1293
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1294
          Flags(PSR_C)       <= Sum(8);
1295
          Flags(PSR_N)       <= Sum(7);
1296 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1297
 
1298
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1299
          Temp               := "0" & Regfile(Index);
1300 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1301
          Flags(PSR_N)       <= Temp(7);
1302 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1303
 
1304
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1305
          Temp(7 downto 0)   := Regfile(0) or Regfile(Index);
1306 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1307
          Flags(PSR_N)       <= Temp(7);
1308 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1309
 
1310
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1311
          Temp(7 downto 0)   := Regfile(0) and Regfile(Index);
1312 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1313
          Flags(PSR_N)       <= Temp(7);
1314 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1315
 
1316
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1317
          Temp(7 downto 0)   := Regfile(0) xor Regfile(Index);
1318 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1319
          Flags(PSR_N)       <= Temp(7);
1320 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1321
 
1322
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1323 185 jshamlet
          Temp               := Regfile(Index) & Flags(PSR_C);
1324
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1325
          Flags(PSR_C)       <= Temp(8);
1326
          Flags(PSR_N)       <= Temp(7);
1327 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1328
 
1329
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1330 185 jshamlet
          Temp               := Regfile(Index)(0) & Flags(PSR_C) &
1331 169 jshamlet
                                Regfile(Index)(7 downto 1);
1332 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1333
          Flags(PSR_C)       <= Temp(8);
1334
          Flags(PSR_N)       <= Temp(7);
1335 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1336
 
1337
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1338
          Sum                := ("0" & Regfile(Index)) +
1339
                                ("0" & x"FF");
1340 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1341
          Flags(PSR_C)       <= Sum(8);
1342
          Flags(PSR_N)       <= Sum(7);
1343 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1344
 
1345
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1346
          Sum                := ("0" & Regfile(0)) +
1347
                                ("1" & (not Regfile(Index))) +
1348 185 jshamlet
                                Flags(PSR_C);
1349
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1350
          Flags(PSR_C)       <= Sum(8);
1351
          Flags(PSR_N)       <= Sum(7);
1352 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1353
 
1354
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1355
          Sum                := ("0" & Regfile(0)) +
1356
                                ("0" & Regfile(Index));
1357 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1358 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1359 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1360
          Flags(PSR_N)       <= Sum(7);
1361 169 jshamlet
 
1362
        when ALU_STP => -- Sets bit(n) in the Flags register
1363
          Flags(Index)       <= '1';
1364
 
1365
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1366 185 jshamlet
          Flags(PSR_Z)       <= not Regfile(0)(Index);
1367
          Flags(PSR_N)       <= Regfile(0)(7);
1368 169 jshamlet
 
1369
        when ALU_CLP => -- Clears bit(n) in the Flags register
1370
          Flags(Index)       <= '0';
1371
 
1372
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1373
          Temp               := "0" & Regfile(0);
1374 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1375
          Flags(PSR_N)       <= Temp(7);
1376 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1377
 
1378
        when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1379
          Sum                := ("0" & Regfile(0)) +
1380
                                ("1" & (not Regfile(Index))) +
1381
                                '1';
1382 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1383
          Flags(PSR_C)       <= Sum(8);
1384
          Flags(PSR_N)       <= Sum(7);
1385 169 jshamlet
 
1386
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1387
          Regfile(0)         <= Mult(7 downto 0);
1388
          Regfile(1)         <= Mult(15 downto 8);
1389 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Mult);
1390 169 jshamlet
 
1391
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1392 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Operand1);
1393
          Flags(PSR_N)       <= Operand1(7);
1394
          Regfile(Index)     <= Operand1;
1395 169 jshamlet
 
1396
        when ALU_POP => -- Rn <= Data
1397 185 jshamlet
          Regfile(Index)     <= Operand1;
1398 169 jshamlet
 
1399
        when ALU_RFLG =>
1400 188 jshamlet
          Flags(3 downto 0)  <= Operand1(3 downto 0);
1401
          if( not RTI_Ignores_GP_Flags )then
1402
            Flags(7 downto 4)<= Operand1(7 downto 4);
1403
          end if;
1404 169 jshamlet
 
1405 185 jshamlet
        when ALU_RSP =>
1406 181 jshamlet
          Regfile(0)         <= Stack_Ptr(7 downto 0);
1407
          Regfile(1)         <= Stack_Ptr(15 downto 8);
1408
 
1409 185 jshamlet
        when ALU_GMSK =>
1410
          Flags(PSR_Z)       <= nor_reduce(Int_Mask);
1411
          Regfile(0)         <= Int_Mask;
1412
 
1413 169 jshamlet
        when others =>
1414
          null;
1415
      end case;
1416
 
1417 224 jshamlet
      Open8_Bus.GP_Flags     <= Flags(7 downto 3);
1418 188 jshamlet
 
1419 169 jshamlet
    end if;
1420
  end process;
1421
 
1422 182 jshamlet
-------------------------------------------------------------------------------
1423
-- Multiplier Logic
1424
--
1425
-- We need to infer a hardware multipler, so we create a special clocked
1426
--  process with no reset or clock enable
1427
-------------------------------------------------------------------------------
1428
 
1429
  Multiplier_proc: process( Clock )
1430
  begin
1431
    if( rising_edge(Clock) )then
1432
      Mult                   <= Regfile(0) *
1433 186 jshamlet
                                Regfile(conv_integer(ALU_Ctrl.Reg));
1434
    end if;
1435
  end process;
1436
 
1437
end architecture;

powered by: WebSVN 2.1.0

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