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 254

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 187 jshamlet
  signal CPU_Next_State      : CPU_STATES := IPF_C0;
290
  signal CPU_State           : CPU_STATES := IPF_C0;
291 169 jshamlet
 
292 225 jshamlet
  signal CPU_Halt_Req        : std_logic := '0';
293
  signal CPU_Halt_Ack        : std_logic := '0';
294 187 jshamlet
 
295 169 jshamlet
  signal Cache_Ctrl          : CACHE_MODES := CACHE_IDLE;
296
 
297
  signal Opcode              : OPCODE_TYPE := (others => '0');
298
  signal SubOp, SubOp_p1     : SUBOP_TYPE  := (others => '0');
299
 
300
  signal Prefetch            : DATA_TYPE   := x"00";
301
  signal Operand1, Operand2  : DATA_TYPE   := x"00";
302
 
303
  signal Instr_Prefetch      : std_logic   := '0';
304
 
305
  signal PC_Ctrl             : PC_CTRL_TYPE;
306
  signal Program_Ctr         : ADDRESS_TYPE := x"0000";
307
 
308 182 jshamlet
  signal ALU_Ctrl            : ALU_CTRL_TYPE;
309
  signal Regfile             : REGFILE_TYPE;
310
  signal Flags               : FLAG_TYPE;
311
  signal Mult                : ADDRESS_TYPE := x"0000";
312
 
313 169 jshamlet
  signal SP_Ctrl             : SP_CTRL_TYPE;
314
  signal Stack_Ptr           : ADDRESS_TYPE := x"0000";
315
 
316
  signal DP_Ctrl             : DATA_CTRL_TYPE;
317
 
318
  signal INT_Ctrl            : INT_CTRL_TYPE;
319
  signal Ack_D, Ack_Q, Ack_Q1: std_logic   := '0';
320
  signal Int_Req, Int_Ack    : std_logic   := '0';
321 245 jshamlet
  signal Set_Mask            : std_logic   := '0';
322 169 jshamlet
  signal Int_Mask            : DATA_TYPE   := x"00";
323
  signal i_Ints              : INTERRUPT_BUNDLE := x"00";
324
  signal Pending             : INTERRUPT_BUNDLE := x"00";
325
  signal Wait_for_FSM        : std_logic := '0';
326 210 jshamlet
  signal Wait_for_ISR        : std_logic := '0';
327 169 jshamlet
 
328 254 jshamlet
  alias  ISR_Addr_Base       is ISR_Start_Addr(15 downto 4);
329
  signal ISR_Addr_Offset     : std_logic_vector(3 downto 0) := x"0";
330
 
331
  constant INT_VECTOR_0      : std_logic_vector(3 downto 0) := x"0";
332
  constant INT_VECTOR_1      : std_logic_vector(3 downto 0) := x"2";
333
  constant INT_VECTOR_2      : std_logic_vector(3 downto 0) := x"4";
334
  constant INT_VECTOR_3      : std_logic_vector(3 downto 0) := x"6";
335
  constant INT_VECTOR_4      : std_logic_vector(3 downto 0) := x"8";
336
  constant INT_VECTOR_5      : std_logic_vector(3 downto 0) := x"A";
337
  constant INT_VECTOR_6      : std_logic_vector(3 downto 0) := x"C";
338
  constant INT_VECTOR_7      : std_logic_vector(3 downto 0) := x"E";
339
 
340 252 jshamlet
  signal IDX_Offset          : ADDRESS_TYPE := x"0000";
341
 
342
  signal IDX_Reg_l           : integer := 0;
343
  signal IDX_Reg_h           : integer := 0;
344
 
345
  signal IDX_NoOffset_Calc   : ADDRESS_TYPE := x"0000";
346
  signal IDX_Offset_Calc     : ADDRESS_TYPE := x"0000";
347
 
348 169 jshamlet
begin
349
 
350 224 jshamlet
-------------------------------------------------------------------------------
351
-- Reset & uSec Tick
352
-------------------------------------------------------------------------------
353 185 jshamlet
 
354 224 jshamlet
  CPU_Reset_Sync: process( Clock, PLL_Locked )
355
  begin
356
    if( PLL_Locked = '0' )then
357
      Reset_q                <= Reset_Level;
358
      Reset                  <= Reset_Level;
359
    elsif( rising_edge(Clock) )then
360
      Reset_q                <= not Reset_Level;
361
      Reset                  <= Reset_q;
362
    end if;
363
  end process;
364
 
365
  uSec_Tick_proc: process( Clock, Reset )
366
  begin
367
    if( Reset = Reset_Level )then
368
      uSec_Cntr              <= USEC_DLY;
369
      uSec_Tick              <= '0';
370
    elsif( rising_edge( Clock ) )then
371
      uSec_Cntr              <= uSec_Cntr - 1;
372
      if( or_reduce(uSec_Cntr) = '0' )then
373
        uSec_Cntr            <= USEC_DLY;
374
      end if;
375
      uSec_Tick              <= nor_reduce(uSec_Cntr);
376
    end if;
377
  end process;
378
 
379
  Open8_Bus.Clock            <= Clock;
380
  Open8_Bus.Reset            <= Reset;
381
  Open8_Bus.uSec_Tick        <= uSec_Tick;
382
 
383 169 jshamlet
-------------------------------------------------------------------------------
384 182 jshamlet
-- Address bus selection/generation logic
385 169 jshamlet
-------------------------------------------------------------------------------
386
 
387 254 jshamlet
  -- Address selection logic based on current CPU state. This is combinatorial,
388
  --  as adding pipeline registration would add a clock cycle to every instr,
389
  --  without really adding the Fmax to compensate.
390
  Address_Logic: process(CPU_State, Operand1, Operand2, IDX_NoOffset_Calc,
391
                         IDX_Offset_Calc, ISR_Addr_Offset, Stack_Ptr, Program_Ctr )
392
  begin
393
    case( CPU_State )is
394
 
395
      when LDA_C2 | STA_C2 =>
396
        Open8_Bus.Address    <= Operand2 & Operand1;
397
 
398
      when LDX_C1 | STX_C1 =>
399
        Open8_Bus.Address    <= IDX_NoOffset_Calc;
400
 
401
      when LDO_C2 | STO_C2 =>
402
        Open8_Bus.Address    <= IDX_Offset_Calc;
403
 
404
      when ISR_C1 | ISR_C2 =>
405
        Open8_Bus.Address    <= ISR_Addr_Base & ISR_Addr_Offset;
406
 
407
      when PSH_C1 | POP_C1 | ISR_C3 | JSR_C1 | JSR_C2 | RTS_C1 | RTS_C2 | RTS_C3 =>
408
        Open8_Bus.Address    <= Stack_Ptr;
409
 
410
      when others =>
411
        Open8_Bus.Address    <= Program_Ctr;
412
 
413
    end case;
414
  end process;
415
 
416 252 jshamlet
  -- The original model treated the offset to LDO/STO as a signed value
417
  --  allowing access to locations -128 to +127 from [Rn+1:Rn]. This isn't
418
  --  always helpful, so the generic allows the CPU to use unsigned math
419
  --  for the offsets. This makes the range 0 to +255 instead.
420 253 jshamlet
 
421 252 jshamlet
Unsigned_Idx_Offsets : if( Unsigned_Index_Offsets )generate
422
  IDX_Offset(15 downto 8)    <= (others => '0');
423
  IDX_Offset(7 downto 0)     <= Operand1;
424
end generate;
425
 
426
Signed_Idx_Offsets: if( not Unsigned_Index_Offsets )generate
427
  IDX_Offset(15 downto 8)    <= (others => Operand1(7));
428
  IDX_Offset(7 downto 0)     <= Operand1;
429
end generate;
430
 
431
  -- Enable_Auto_Increment uses the LSB to determine whether or not to
432
  --  do the auto-increment, so we need to lock the LSB for each operand
433
  --  if it is enabled. This forces [ODD:EVEN] pairing.
434
 
435
Auto_Incr_Set: if( Enable_Auto_Increment )generate
436
  IDX_Reg_l                  <= conv_integer(SubOp(2 downto 1) & '0');
437
  IDX_Reg_h                  <= conv_integer(SubOp(2 downto 1) & '1');
438
end generate;
439
 
440
Auto_Incr_Not_Set: if( not Enable_Auto_Increment )generate
441
  IDX_Reg_l                  <= conv_integer(SubOp);
442
  IDX_Reg_h                  <= conv_integer(SubOp_p1);
443
end generate;
444
 
445
  -- Pipeline registers for the indexed and indexed with offset addresses.
446
  Idx_Addr_Calc_proc: process( Clock, Reset )
447 169 jshamlet
    variable Reg, Reg_1      : integer range 0 to 7 := 0;
448
  begin
449 252 jshamlet
    if( Reset = Reset_Level )then
450
      IDX_NoOffset_Calc      <= x"0000";
451
      IDX_Offset_Calc        <= x"0000";
452
    elsif( rising_edge(Clock))then
453
      IDX_NoOffset_Calc      <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l));
454
      IDX_Offset_Calc        <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l)) +
455
                                IDX_Offset;
456 182 jshamlet
    end if;
457 252 jshamlet
  end process;
458 182 jshamlet
 
459
-------------------------------------------------------------------------------
460
-- Combinatorial portion of CPU finite state machine
461
-- State Logic / Instruction Decoding & Execution
462
-------------------------------------------------------------------------------
463
 
464 187 jshamlet
  State_Logic: process(CPU_State, Flags, Int_Mask, CPU_Halt_Req, Opcode,
465 182 jshamlet
                       SubOp , SubOp_p1, Operand1, Operand2, Int_Req )
466
    variable Reg             : integer range 0 to 7 := 0;
467
  begin
468 169 jshamlet
    CPU_Next_State           <= CPU_State;
469
    Cache_Ctrl               <= CACHE_IDLE;
470
    --
471 185 jshamlet
    PC_Ctrl.Oper             <= PC_INCR;
472
    PC_Ctrl.Offset           <= PC_IDLE;
473 182 jshamlet
    --
474 169 jshamlet
    ALU_Ctrl.Oper            <= ALU_IDLE;
475
    ALU_Ctrl.Reg             <= ACCUM;
476
    --
477
    SP_Ctrl.Oper             <= SP_IDLE;
478
    --
479
    DP_Ctrl.Src              <= DATA_RD_MEM;
480
    DP_Ctrl.Reg              <= ACCUM;
481
    --
482
    INT_Ctrl.Mask_Set        <= '0';
483
    INT_Ctrl.Soft_Ints       <= x"00";
484
    INT_Ctrl.Incr_ISR        <= '0';
485
    Ack_D                    <= '0';
486 225 jshamlet
    --
487 182 jshamlet
    Reg                     := conv_integer(SubOp);
488 225 jshamlet
    --
489
    CPU_Halt_Ack             <= '0';
490 169 jshamlet
 
491
    case CPU_State is
492
-------------------------------------------------------------------------------
493
-- Initial Instruction fetch & decode
494
-------------------------------------------------------------------------------
495 187 jshamlet
      when IPF_C0 =>
496
        CPU_Next_State       <= IPF_C1;
497 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
498 169 jshamlet
 
499 187 jshamlet
      when IPF_C1 =>
500
        CPU_Next_State       <= IPF_C2;
501 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
502 169 jshamlet
 
503 187 jshamlet
      when IPF_C2 =>
504
        CPU_Next_State       <= IDC_C0;
505 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
506 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
507 169 jshamlet
 
508 187 jshamlet
      when IDC_C0 =>
509
        CPU_Next_State       <= IDC_C0;
510 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
511
 
512
        case Opcode is
513
          when OP_PSH =>
514
            CPU_Next_State   <= PSH_C1;
515
            Cache_Ctrl       <= CACHE_PREFETCH;
516 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
517 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
518
            DP_Ctrl.Reg      <= SubOp;
519
 
520
          when OP_POP =>
521
            CPU_Next_State   <= POP_C1;
522
            Cache_Ctrl       <= CACHE_PREFETCH;
523 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
524 169 jshamlet
            SP_Ctrl.Oper     <= SP_POP;
525
 
526
          when OP_BR0 | OP_BR1 =>
527
            CPU_Next_State   <= BRN_C1;
528
            Cache_Ctrl       <= CACHE_OPER1;
529 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
530 169 jshamlet
 
531 185 jshamlet
 
532 169 jshamlet
          when OP_DBNZ =>
533
            CPU_Next_State   <= DBNZ_C1;
534
            Cache_Ctrl       <= CACHE_OPER1;
535 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
536 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_DEC;
537
            ALU_Ctrl.Reg     <= SubOp;
538
 
539
          when OP_INT =>
540 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
541 187 jshamlet
            -- Make sure the requested interrupt is actually enabled first.
542
            --  Also, unlike CPU_Halt, the INT instruction is actually being
543
            --  executed, so go ahead and increment the program counter before
544
            --  pausing so the CPU restarts on the next instruction.
545 169 jshamlet
            if( Int_Mask(Reg) = '1' )then
546 187 jshamlet
              CPU_Next_State <= WAI_Cx;
547 169 jshamlet
              INT_Ctrl.Soft_Ints(Reg) <= '1';
548
            end if;
549
 
550
          when OP_STK =>
551
            case SubOp is
552
              when SOP_RSP  =>
553 185 jshamlet
                PC_Ctrl.Offset <= PC_NEXT;
554 181 jshamlet
                if( not Allow_Stack_Address_Move )then
555 187 jshamlet
                  -- The default behavior for this instruction is to simply
556
                  --  repoint the SP to the HDL default
557 185 jshamlet
                  SP_Ctrl.Oper    <= SP_CLR;
558 181 jshamlet
                end if;
559 187 jshamlet
                if( Allow_Stack_Address_Move and
560
                    Flags(Stack_Xfer_Flag) = '1' )then
561
                  -- If RSP is set to allow SP moves, and the specified flag
562
                  --  is true, then signal the stack pointer logic to load
563
                  --  from R1:R0
564 185 jshamlet
                  SP_Ctrl.Oper    <= SP_SET;
565 181 jshamlet
                end if;
566 187 jshamlet
                if( Allow_Stack_Address_Move and
567
                    Flags(Stack_Xfer_Flag) = '0')then
568
                  -- If RSP is set to allow SP moves, and the specified flag
569
                  --  is false, then signal the ALU to copy the stack pointer
570
                  --  to R1:R0
571 185 jshamlet
                  ALU_Ctrl.Oper   <= ALU_RSP;
572 181 jshamlet
                end if;
573 169 jshamlet
 
574
              when SOP_RTS | SOP_RTI =>
575 185 jshamlet
                CPU_Next_State    <= RTS_C1;
576 190 jshamlet
                Cache_Ctrl        <= CACHE_IDLE;
577 185 jshamlet
                SP_Ctrl.Oper      <= SP_POP;
578 169 jshamlet
 
579
              when SOP_BRK  =>
580
                if( BRK_Implements_WAI )then
581 187 jshamlet
                  -- If BRK_Implements_WAI, then jump to the WAI_Cx and
582
                  --  increment the PC similar to an ISR flow.
583
                  CPU_Next_State  <= WAI_Cx;
584 185 jshamlet
                  PC_Ctrl.Offset  <= PC_NEXT;
585 187 jshamlet
                else
586
                -- If Break is implemented normally, back the PC up by
587
                --  2 and return through IPF_C0 in order to execute a 5
588
                --  clock cycle delay
589
                  CPU_Next_State  <= BRK_C1;
590
                  PC_Ctrl.Offset  <= PC_REV2;
591 169 jshamlet
                end if;
592
 
593
              when SOP_JMP  =>
594 185 jshamlet
                CPU_Next_State    <= JMP_C1;
595
                Cache_Ctrl        <= CACHE_OPER1;
596 169 jshamlet
 
597
              when SOP_SMSK =>
598 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
599 169 jshamlet
                INT_Ctrl.Mask_Set <= '1';
600
 
601
              when SOP_GMSK =>
602 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
603
                ALU_Ctrl.Oper     <= ALU_GMSK;
604 169 jshamlet
 
605
              when SOP_JSR =>
606
                CPU_Next_State <= JSR_C1;
607 185 jshamlet
                Cache_Ctrl        <= CACHE_OPER1;
608
                DP_Ctrl.Src       <= DATA_WR_PC;
609
                DP_Ctrl.Reg       <= PC_MSB;
610 169 jshamlet
 
611
              when others => null;
612
            end case;
613
 
614
          when OP_MUL =>
615
            CPU_Next_State   <= MUL_C1;
616 181 jshamlet
            -- Multiplication requires a single clock cycle to calculate PRIOR
617
            --  to the ALU writing the result to registers. As a result, this
618
            --  state needs to idle the ALU initially, and back the PC up by 1
619
            -- We can get away with only 1 extra clock by pre-fetching the
620
            --  next instruction, though.
621 169 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
622 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
623 181 jshamlet
            -- Note that both the multiply process AND ALU process need the
624
            --  source register for Rn (R1:R0 = R0 * Rn). Assert ALU_Ctrl.reg
625
            --  now, but hold off on the ALU command until the next state.
626 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_IDLE;
627
            ALU_Ctrl.Reg     <= SubOp;
628
 
629
          when OP_UPP =>
630
            CPU_Next_State   <= UPP_C1;
631
            Cache_Ctrl       <= CACHE_PREFETCH;
632 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
633 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
634
            ALU_Ctrl.Reg     <= SubOp;
635
 
636
          when OP_LDA =>
637
            CPU_Next_State   <= LDA_C1;
638
            Cache_Ctrl       <= CACHE_OPER1;
639
 
640
          when OP_LDI =>
641
            CPU_Next_State   <= LDI_C1;
642
            Cache_Ctrl       <= CACHE_OPER1;
643 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
644 169 jshamlet
 
645
          when OP_LDO =>
646
            CPU_Next_State   <= LDO_C1;
647
            Cache_Ctrl       <= CACHE_OPER1;
648 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
649 169 jshamlet
 
650
          when OP_LDX =>
651
            CPU_Next_State   <= LDX_C1;
652 181 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
653 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
654 169 jshamlet
 
655
          when OP_STA =>
656
            CPU_Next_State   <= STA_C1;
657
            Cache_Ctrl       <= CACHE_OPER1;
658
 
659
          when OP_STO =>
660
            CPU_Next_State   <= STO_C1;
661
            Cache_Ctrl       <= CACHE_OPER1;
662 252 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
663 169 jshamlet
 
664
          when OP_STX =>
665
            CPU_Next_State   <= STX_C1;
666
            Cache_Ctrl       <= CACHE_PREFETCH;
667 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
668 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
669
            DP_Ctrl.Reg      <= ACCUM;
670
 
671 244 jshamlet
          when OP_STP =>
672
            PC_Ctrl.Offset   <= PC_NEXT;
673
            if( Supervisor_Mode )then
674
              if( SubOp /= PSR_I )then
675
                ALU_Ctrl.Oper  <= Opcode;
676
                ALU_Ctrl.Reg   <= SubOp;
677
              end if;
678
            else
679
              ALU_Ctrl.Oper  <= Opcode;
680
              ALU_Ctrl.Reg   <= SubOp;
681
            end if;
682
 
683 169 jshamlet
          when others =>
684 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
685 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
686
            ALU_Ctrl.Reg     <= SubOp;
687
 
688
        end case;
689
 
690 186 jshamlet
        if( Int_Req = '1' )then
691
          CPU_Next_State     <= ISR_C1;
692 187 jshamlet
        end if;
693
 
694
        if( CPU_Halt_Req = '1' )then
695
          CPU_Next_State     <= WAH_Cx;
696
        end if;
697
 
698
        -- If either of these override conditions are true, the decoder needs
699
        --  to undo everything it just setup, since even "single-cycle"
700
        --  instructions will be executed again upon return.
701
        if( Int_Req = '1' or CPU_Halt_Req = '1' )then
702
          -- In either case, we want to skip loading the cache, as the cache
703
          --  will be invalid by the time we get back.
704 186 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
705 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to the current instruction,
706
          -- compensating for the pipeline registers.
707 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
708
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
709 187 jshamlet
          --  operation due to the current instruction.
710 186 jshamlet
          ALU_Ctrl.Oper      <= ALU_IDLE;
711
          SP_Ctrl.Oper       <= SP_IDLE;
712 187 jshamlet
          -- Interrupt logic outside of the state machine needs this to be set
713
          --  to DATA_RD_MEM, while CPU_Halt considers this a "don't care".
714 186 jshamlet
          DP_Ctrl.Src        <= DATA_RD_MEM;
715 187 jshamlet
          -- If an INT/SMSK instruction was going to be executed, it will get
716
          --  executed again when normal processing resumes, so axe their
717
          --  requests for now.
718
          INT_Ctrl.Mask_Set       <= '0';
719
          INT_Ctrl.Soft_Ints(Reg) <= '0';
720 186 jshamlet
        end if;
721
 
722 169 jshamlet
-------------------------------------------------------------------------------
723
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
724
-------------------------------------------------------------------------------
725
 
726
      when BRN_C1 =>
727 187 jshamlet
        CPU_Next_State       <= IDC_C0;
728 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
729 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
730 169 jshamlet
        if( Flags(Reg) = Opcode(0) )then
731 187 jshamlet
          CPU_Next_State     <= IPF_C0;
732 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
733
          PC_Ctrl.Offset     <= Operand1;
734
        end if;
735
 
736
      when DBNZ_C1 =>
737 187 jshamlet
        CPU_Next_State       <= IDC_C0;
738 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
739 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
740
        if( Flags(PSR_Z) = '0' )then
741 187 jshamlet
          CPU_Next_State     <= IPF_C0;
742 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
743
          PC_Ctrl.Offset     <= Operand1;
744
        end if;
745
 
746
      when JMP_C1 =>
747
        CPU_Next_State       <= JMP_C2;
748
        Cache_Ctrl           <= CACHE_OPER2;
749
 
750
      when JMP_C2 =>
751 187 jshamlet
        CPU_Next_State       <= IPF_C0;
752 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
753
 
754
-------------------------------------------------------------------------------
755
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
756
-------------------------------------------------------------------------------
757
 
758
      when LDA_C1 =>
759
        CPU_Next_State       <= LDA_C2;
760
        Cache_Ctrl           <= CACHE_OPER2;
761
 
762
      when LDA_C2 =>
763
        CPU_Next_State       <= LDA_C3;
764
 
765
      when LDA_C3 =>
766
        CPU_Next_State       <= LDA_C4;
767 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
768 169 jshamlet
 
769
      when LDA_C4 =>
770
        CPU_Next_State       <= LDI_C1;
771
        Cache_Ctrl           <= CACHE_OPER1;
772 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
773 169 jshamlet
 
774
      when LDI_C1 =>
775 187 jshamlet
        CPU_Next_State       <= IDC_C0;
776 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
777 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
778 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
779
        ALU_Ctrl.Reg         <= SubOp;
780
 
781
      when LDO_C1 =>
782 252 jshamlet
        CPU_Next_State       <= LDO_C2;
783
 
784
      when LDO_C2 =>
785 181 jshamlet
        CPU_Next_State       <= LDX_C2;
786 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
787 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
788
          ALU_Ctrl.Oper      <= ALU_UPP;
789
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
790 169 jshamlet
        end if;
791
 
792
      when LDX_C1 =>
793
        CPU_Next_State       <= LDX_C2;
794 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
795
          ALU_Ctrl.Oper      <= ALU_UPP;
796
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
797 181 jshamlet
        end if;
798 169 jshamlet
 
799
      when LDX_C2 =>
800
        CPU_Next_State       <= LDX_C3;
801 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
802 181 jshamlet
 
803
      when LDX_C3 =>
804
        CPU_Next_State       <= LDX_C4;
805 182 jshamlet
        Cache_Ctrl           <= CACHE_OPER1;
806 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
807 169 jshamlet
 
808 181 jshamlet
      when LDX_C4 =>
809 187 jshamlet
        CPU_Next_State       <= IDC_C0;
810 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
811 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
812 181 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
813 169 jshamlet
        ALU_Ctrl.Reg         <= ACCUM;
814
 
815
-------------------------------------------------------------------------------
816
-- Data Storage - Store to memory (STA, STO, STX)
817
-------------------------------------------------------------------------------
818
      when STA_C1 =>
819
        CPU_Next_State       <= STA_C2;
820
        Cache_Ctrl           <= CACHE_OPER2;
821
        DP_Ctrl.Src          <= DATA_WR_REG;
822
        DP_Ctrl.Reg          <= SubOp;
823
 
824
      when STA_C2 =>
825
        CPU_Next_State       <= STA_C3;
826 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
827 169 jshamlet
 
828
      when STA_C3 =>
829 187 jshamlet
        CPU_Next_State       <= IPF_C2;
830 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
831 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
832 169 jshamlet
 
833
      when STO_C1 =>
834 252 jshamlet
        CPU_Next_State       <= STO_C2;
835 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
836 252 jshamlet
        DP_Ctrl.Src          <= DATA_WR_REG;
837
        DP_Ctrl.Reg          <= ACCUM;
838
 
839
      when STO_C2 =>
840
        CPU_Next_State       <= IPF_C1;
841 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
842 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
843 252 jshamlet
          CPU_Next_State     <= STO_C3;
844 182 jshamlet
          ALU_Ctrl.Oper      <= ALU_UPP;
845
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
846 169 jshamlet
        end if;
847
 
848 252 jshamlet
      when STO_C3 =>
849
        CPU_Next_State       <= IPF_C2;
850 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
851 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
852
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
853
 
854
      when STX_C1 =>
855 187 jshamlet
        CPU_Next_State       <= IPF_C1;
856 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
857 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
858
          CPU_Next_State     <= STX_C2;
859
          ALU_Ctrl.Oper      <= ALU_UPP;
860
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
861 169 jshamlet
        end if;
862
 
863
      when STX_C2 =>
864 187 jshamlet
        CPU_Next_State       <= IPF_C2;
865 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
866 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
867
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
868
 
869
-------------------------------------------------------------------------------
870
-- Multi-Cycle Math Operations (UPP, MUL)
871
-------------------------------------------------------------------------------
872
 
873
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
874 181 jshamlet
      --  instruction/first operand, we have to return through PF2. Also, we
875
      --  need to tell the ALU to store the results to R1:R0 here. Note that
876
      --  there is no ALU_Ctrl.Reg, as this is implied in the ALU instruction
877 169 jshamlet
      when MUL_C1 =>
878 187 jshamlet
        CPU_Next_State       <= IPF_C2;
879 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
880 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_MUL;
881
 
882
      when UPP_C1 =>
883 187 jshamlet
        CPU_Next_State       <= IPF_C2;
884 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
885 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
886
        ALU_Ctrl.Reg         <= SubOp_p1;
887
 
888
-------------------------------------------------------------------------------
889
-- Basic Stack Manipulation (PSH, POP, RSP)
890
-------------------------------------------------------------------------------
891
      when PSH_C1 =>
892 187 jshamlet
        CPU_Next_State       <= IPF_C1;
893 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
894
 
895
      when POP_C1 =>
896
        CPU_Next_State       <= POP_C2;
897
 
898
      when POP_C2 =>
899
        CPU_Next_State       <= POP_C3;
900 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
901 169 jshamlet
 
902
      when POP_C3 =>
903
        CPU_Next_State       <= POP_C4;
904
        Cache_Ctrl           <= CACHE_OPER1;
905 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
906 169 jshamlet
 
907
      when POP_C4 =>
908 187 jshamlet
        CPU_Next_State       <= IDC_C0;
909 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
910 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
911 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_POP;
912
        ALU_Ctrl.Reg         <= SubOp;
913 172 jshamlet
 
914 169 jshamlet
-------------------------------------------------------------------------------
915
-- Subroutines & Interrupts (RTS, JSR)
916
-------------------------------------------------------------------------------
917 187 jshamlet
      when WAI_Cx => -- For soft interrupts only, halt the Program_Ctr
918 169 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
919 186 jshamlet
        if( Int_Req = '1' )then
920
          CPU_Next_State     <= ISR_C1;
921 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to would have been the next
922
          --  instruction, compensating for the pipeline registers.
923 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
924
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
925
          --  operation due to the current instruction
926
          DP_Ctrl.Src        <= DATA_RD_MEM;
927
        end if;
928 169 jshamlet
 
929 187 jshamlet
      when WAH_Cx => -- Holds until CPU_Halt_Req is deasserted.
930 225 jshamlet
        CPU_Halt_Ack         <= '1';
931 187 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
932
        if( CPU_Halt_Req = '0' )then
933
          CPU_Next_State     <= IPF_C0;
934
          DP_Ctrl.Src        <= DATA_RD_MEM;
935
        end if;
936
 
937
      when BRK_C1 => -- Debugging (BRK) Performs a 5-clock NOP.
938
        CPU_Next_State       <= IPF_C0;
939
 
940 169 jshamlet
      when ISR_C1 =>
941
        CPU_Next_State       <= ISR_C2;
942
        INT_Ctrl.Incr_ISR    <= '1';
943
 
944
      when ISR_C2 =>
945
        CPU_Next_State       <= ISR_C3;
946
        DP_Ctrl.Src          <= DATA_WR_FLAG;
947
 
948
      when ISR_C3 =>
949
        CPU_Next_State       <= JSR_C1;
950
        Cache_Ctrl           <= CACHE_OPER1;
951 182 jshamlet
        ALU_Ctrl.Oper        <= ALU_STP;
952 185 jshamlet
        ALU_Ctrl.Reg         <= conv_std_logic_vector(PSR_I,3);
953 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
954
        DP_Ctrl.Src          <= DATA_WR_PC;
955 182 jshamlet
        DP_Ctrl.Reg          <= PC_MSB;
956 169 jshamlet
        Ack_D                <= '1';
957
 
958
      when JSR_C1 =>
959
        CPU_Next_State       <= JSR_C2;
960
        Cache_Ctrl           <= CACHE_OPER2;
961
        SP_Ctrl.Oper         <= SP_PUSH;
962
        DP_Ctrl.Src          <= DATA_WR_PC;
963 182 jshamlet
        DP_Ctrl.Reg          <= PC_LSB;
964 169 jshamlet
 
965
      when JSR_C2 =>
966 187 jshamlet
        CPU_Next_State       <= IPF_C0;
967 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
968 182 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
969 169 jshamlet
 
970
      when RTS_C1 =>
971
        CPU_Next_State       <= RTS_C2;
972
        SP_Ctrl.Oper         <= SP_POP;
973
 
974
      when RTS_C2 =>
975
        CPU_Next_State       <= RTS_C3;
976
        -- if this is an RTI, then we need to POP the flags
977
        if( SubOp = SOP_RTI )then
978
          SP_Ctrl.Oper       <= SP_POP;
979
        end if;
980
 
981
      when RTS_C3 =>
982
        CPU_Next_State       <= RTS_C4;
983
        Cache_Ctrl           <= CACHE_OPER1;
984
 
985
      when RTS_C4 =>
986
        CPU_Next_State       <= RTS_C5;
987
        Cache_Ctrl           <= CACHE_OPER2;
988
 
989
      when RTS_C5 =>
990 187 jshamlet
        CPU_Next_State       <= IPF_C0;
991 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
992 185 jshamlet
        -- if this is an RTI, then we need to clear the I bit
993 169 jshamlet
        if( SubOp = SOP_RTI )then
994
          CPU_Next_State     <= RTI_C6;
995
          Cache_Ctrl         <= CACHE_OPER1;
996 185 jshamlet
          ALU_Ctrl.Oper      <= ALU_CLP;
997
          ALU_Ctrl.Reg       <= conv_std_logic_vector(PSR_I,3);
998 169 jshamlet
        end if;
999
 
1000
      when RTI_C6 =>
1001 187 jshamlet
        CPU_Next_State       <= IPF_C1;
1002 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
1003 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_RFLG;
1004
 
1005
      when others =>
1006
        null;
1007
    end case;
1008
 
1009
  end process;
1010
 
1011
-------------------------------------------------------------------------------
1012
-- Registered portion of CPU finite state machine
1013
-------------------------------------------------------------------------------
1014 182 jshamlet
 
1015 169 jshamlet
  CPU_Regs: process( Reset, Clock )
1016
    variable Offset_SX       : ADDRESS_TYPE;
1017 188 jshamlet
    variable i_Ints          : INTERRUPT_BUNDLE := x"00";
1018 169 jshamlet
    variable Index           : integer range 0 to 7         := 0;
1019
    variable Sum             : std_logic_vector(8 downto 0) := "000000000";
1020
    variable Temp            : std_logic_vector(8 downto 0) := "000000000";
1021
  begin
1022
    if( Reset = Reset_Level )then
1023 187 jshamlet
      CPU_State              <= IPF_C0;
1024 169 jshamlet
      Opcode                 <= OP_INC;
1025
      SubOp                  <= ACCUM;
1026
      SubOp_p1               <= ACCUM;
1027
      Operand1               <= x"00";
1028
      Operand2               <= x"00";
1029
      Instr_Prefetch         <= '0';
1030
      Prefetch               <= x"00";
1031
 
1032 187 jshamlet
      CPU_Halt_Req           <= '0';
1033 225 jshamlet
      Halt_Ack               <= '0';
1034 187 jshamlet
 
1035 223 jshamlet
      Open8_Bus.Wr_En        <= '0';
1036
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1037
      Open8_Bus.Rd_En        <= '1';
1038 169 jshamlet
 
1039
      Program_Ctr            <= Program_Start_Addr;
1040
      Stack_Ptr              <= Stack_Start_Addr;
1041
 
1042
      Ack_Q                  <= '0';
1043
      Ack_Q1                 <= '0';
1044
      Int_Ack                <= '0';
1045
 
1046
      Int_Req                <= '0';
1047
      Pending                <= x"00";
1048
      Wait_for_FSM           <= '0';
1049 210 jshamlet
      Wait_for_ISR           <= '0';
1050 245 jshamlet
      Set_Mask               <= '0';
1051 169 jshamlet
      if( Enable_NMI )then
1052
        Int_Mask             <= Default_Interrupt_Mask(7 downto 1) & '1';
1053
      else
1054
        Int_Mask             <= Default_Interrupt_Mask;
1055
      end if;
1056 254 jshamlet
      ISR_Addr_Offset        <= INT_VECTOR_0;
1057 169 jshamlet
 
1058
      for i in 0 to 7 loop
1059 188 jshamlet
        Regfile(i)           <= x"00";
1060 169 jshamlet
      end loop;
1061
      Flags                  <= x"00";
1062 248 jshamlet
      if( Supervisor_Mode )then
1063 244 jshamlet
        Flags(PSR_I)         <= '1';
1064
      end if;
1065 169 jshamlet
 
1066 224 jshamlet
      Open8_Bus.GP_Flags     <= (others => '0');
1067 188 jshamlet
 
1068 169 jshamlet
    elsif( rising_edge(Clock) )then
1069 187 jshamlet
 
1070 225 jshamlet
      CPU_Halt_Req           <= Halt_Req;
1071
      Halt_Ack               <= CPU_Halt_Ack;
1072 187 jshamlet
 
1073 223 jshamlet
      Open8_Bus.Wr_En        <= '0';
1074
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1075
      Open8_Bus.Rd_En        <= '0';
1076 169 jshamlet
 
1077
-------------------------------------------------------------------------------
1078
-- Instruction/Operand caching for pipelined memory access
1079
-------------------------------------------------------------------------------
1080
      CPU_State              <= CPU_Next_State;
1081
      case Cache_Ctrl is
1082
        when CACHE_INSTR =>
1083
          Opcode             <= Rd_Data(7 downto 3);
1084
          SubOp              <= Rd_Data(2 downto 0);
1085
          SubOp_p1           <= Rd_Data(2 downto 0) + 1;
1086
          if( Instr_Prefetch = '1' )then
1087
            Opcode           <= Prefetch(7 downto 3);
1088
            SubOp            <= Prefetch(2 downto 0);
1089
            SubOp_p1         <= Prefetch(2 downto 0) + 1;
1090
            Instr_Prefetch   <= '0';
1091
          end if;
1092
 
1093
        when CACHE_OPER1 =>
1094
          Operand1           <= Rd_Data;
1095
 
1096
        when CACHE_OPER2 =>
1097
          Operand2           <= Rd_Data;
1098
 
1099
        when CACHE_PREFETCH =>
1100
          Prefetch           <= Rd_Data;
1101
          Instr_Prefetch     <= '1';
1102
 
1103
        when CACHE_IDLE =>
1104
          null;
1105
      end case;
1106
 
1107
-------------------------------------------------------------------------------
1108
-- Program Counter
1109
-------------------------------------------------------------------------------
1110
      Offset_SX(15 downto 8) := (others => PC_Ctrl.Offset(7));
1111
      Offset_SX(7 downto 0)  := PC_Ctrl.Offset;
1112
 
1113
      case PC_Ctrl.Oper is
1114
        when PC_INCR =>
1115
          Program_Ctr        <= Program_Ctr + Offset_SX - 2;
1116
 
1117
        when PC_LOAD =>
1118 185 jshamlet
          Program_Ctr        <= Operand2 & Operand1;
1119 169 jshamlet
 
1120
        when others =>
1121
          null;
1122
      end case;
1123
 
1124
-------------------------------------------------------------------------------
1125
-- (Write) Data Path
1126
-------------------------------------------------------------------------------
1127
      case DP_Ctrl.Src is
1128
        when DATA_BUS_IDLE =>
1129
          null;
1130
 
1131
        when DATA_RD_MEM =>
1132 223 jshamlet
          Open8_Bus.Rd_En    <= '1';
1133 169 jshamlet
 
1134
        when DATA_WR_REG =>
1135 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1136
          Open8_Bus.Wr_Data  <= Regfile(conv_integer(DP_Ctrl.Reg));
1137 169 jshamlet
 
1138
        when DATA_WR_FLAG =>
1139 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1140
          Open8_Bus.Wr_Data  <= Flags;
1141 169 jshamlet
 
1142
        when DATA_WR_PC =>
1143 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1144
          Open8_Bus.Wr_Data  <= Program_Ctr(15 downto 8);
1145 182 jshamlet
          if( DP_Ctrl.Reg = PC_LSB )then
1146 223 jshamlet
            Open8_Bus.Wr_Data <= Program_Ctr(7 downto 0);
1147 169 jshamlet
          end if;
1148
 
1149
        when others =>
1150
          null;
1151
      end case;
1152
 
1153
-------------------------------------------------------------------------------
1154
-- Stack Pointer
1155
-------------------------------------------------------------------------------
1156
      case SP_Ctrl.Oper is
1157
        when SP_IDLE =>
1158
          null;
1159
 
1160 181 jshamlet
        when SP_CLR =>
1161 169 jshamlet
          Stack_Ptr          <= Stack_Start_Addr;
1162
 
1163 181 jshamlet
        when SP_SET =>
1164 245 jshamlet
          if( Supervisor_Mode )then
1165
            if( Flags(PSR_I) = '1' )then
1166
              Stack_Ptr      <= Regfile(1) & Regfile(0);
1167
            end if;
1168
          else
1169
            Stack_Ptr        <= Regfile(1) & Regfile(0);
1170
          end if;
1171 181 jshamlet
 
1172 169 jshamlet
        when SP_POP  =>
1173
          Stack_Ptr          <= Stack_Ptr + 1;
1174
 
1175
        when SP_PUSH =>
1176
          Stack_Ptr          <= Stack_Ptr - 1;
1177
 
1178
        when others =>
1179
          null;
1180
 
1181
      end case;
1182
 
1183
-------------------------------------------------------------------------------
1184
-- Interrupt Controller
1185
-------------------------------------------------------------------------------
1186 245 jshamlet
 
1187
      -- If Supervisor_Mode is set, restrict the SMSK instruction such that it
1188
      --  requires the I bit to be set.
1189
      if( Supervisor_Mode )then
1190
        Set_Mask             <= INT_Ctrl.Mask_Set and Flags(PSR_I);
1191
      else
1192
        Set_Mask             <= INT_Ctrl.Mask_Set;
1193
      end if;
1194
 
1195 169 jshamlet
      -- The interrupt control mask is always sourced out of R0
1196 245 jshamlet
      if( Set_Mask = '1' )then
1197 169 jshamlet
        if( Enable_NMI )then
1198
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
1199
        else
1200
          Int_Mask           <= Regfile(conv_integer(ACCUM));
1201
        end if;
1202
      end if;
1203
 
1204
      -- Combine external and internal interrupts, and mask the OR of the two
1205
      --  with the mask. Record any incoming interrupts to the pending buffer
1206
      i_Ints                 := (Interrupts or INT_Ctrl.Soft_Ints) and
1207
                                Int_Mask;
1208 172 jshamlet
 
1209 169 jshamlet
      Pending                <= i_Ints or Pending;
1210
 
1211 210 jshamlet
      if( Sequential_Interrupts )then
1212
        Wait_for_ISR         <= Flags(PSR_I);
1213
      else
1214
        Wait_for_ISR         <= '0';
1215
      end if;
1216
 
1217
      if( Wait_for_FSM = '0' and Wait_for_ISR = '0' )then
1218 169 jshamlet
        if(    Pending(0) = '1' )then
1219 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_0;
1220 169 jshamlet
          Pending(0)         <= '0';
1221
        elsif( Pending(1) = '1' )then
1222 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_1;
1223 169 jshamlet
          Pending(1)         <= '0';
1224
        elsif( Pending(2) = '1' )then
1225 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_2;
1226 169 jshamlet
          Pending(2)         <= '0';
1227
        elsif( Pending(3) = '1' )then
1228 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_3;
1229 169 jshamlet
          Pending(3)         <= '0';
1230
        elsif( Pending(4) = '1' )then
1231 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_4;
1232 169 jshamlet
          Pending(4)         <= '0';
1233
        elsif( Pending(5) = '1' )then
1234 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_5;
1235 169 jshamlet
          Pending(5)         <= '0';
1236
        elsif( Pending(6) = '1' )then
1237 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_6;
1238 169 jshamlet
          Pending(6)         <= '0';
1239
        elsif( Pending(7) = '1' )then
1240 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_7;
1241 169 jshamlet
          Pending(7)         <= '0';
1242
        end if;
1243 185 jshamlet
        Wait_for_FSM         <= or_reduce(Pending);
1244 169 jshamlet
      end if;
1245
 
1246
      -- Reset the Wait_for_FSM flag on Int_Ack
1247
      Ack_Q                  <= Ack_D;
1248
      Ack_Q1                 <= Ack_Q;
1249
      Int_Ack                <= Ack_Q1;
1250
      if( Int_Ack = '1' )then
1251
        Wait_for_FSM         <= '0';
1252
      end if;
1253
 
1254
      Int_Req                <= Wait_for_FSM and (not Int_Ack);
1255
 
1256
      -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1257
      --  lower half of the address.
1258
      if( INT_Ctrl.Incr_ISR = '1' )then
1259 254 jshamlet
        ISR_Addr_Offset             <= ISR_Addr_Offset + 1;
1260 169 jshamlet
      end if;
1261
 
1262
-------------------------------------------------------------------------------
1263
-- ALU (Arithmetic / Logic Unit)
1264
-------------------------------------------------------------------------------
1265
      Index                  := conv_integer(ALU_Ctrl.Reg);
1266
      Sum                    := (others => '0');
1267
      Temp                   := (others => '0');
1268
 
1269
      case ALU_Ctrl.Oper is
1270
        when ALU_INC => -- Rn = Rn + 1 : Flags N,C,Z
1271
          Sum                := ("0" & x"01") +
1272
                                ("0" & Regfile(Index));
1273 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1274
          Flags(PSR_C)       <= Sum(8);
1275 209 jshamlet
          Flags(PSR_N)       <= Sum(7);
1276 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1277
 
1278
        when ALU_UPP => -- Rn = Rn + 1
1279
          Sum                := ("0" & x"01") +
1280
                                ("0" & Regfile(Index));
1281 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1282 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1283
 
1284
        when ALU_UPP2 => -- Rn = Rn + C
1285
          Sum                := ("0" & x"00") +
1286
                                ("0" & Regfile(Index)) +
1287 185 jshamlet
                                Flags(PSR_C);
1288
          Flags(PSR_C)       <= Sum(8);
1289 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1290
 
1291
        when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1292
          Sum                := ("0" & Regfile(0)) +
1293
                                ("0" & Regfile(Index)) +
1294 185 jshamlet
                                Flags(PSR_C);
1295
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1296
          Flags(PSR_C)       <= Sum(8);
1297
          Flags(PSR_N)       <= Sum(7);
1298 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1299
 
1300
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1301
          Temp               := "0" & Regfile(Index);
1302 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1303
          Flags(PSR_N)       <= Temp(7);
1304 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1305
 
1306
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1307
          Temp(7 downto 0)   := Regfile(0) or Regfile(Index);
1308 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1309
          Flags(PSR_N)       <= Temp(7);
1310 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1311
 
1312
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1313
          Temp(7 downto 0)   := Regfile(0) and Regfile(Index);
1314 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1315
          Flags(PSR_N)       <= Temp(7);
1316 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1317
 
1318
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1319
          Temp(7 downto 0)   := Regfile(0) xor Regfile(Index);
1320 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1321
          Flags(PSR_N)       <= Temp(7);
1322 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1323
 
1324
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1325 185 jshamlet
          Temp               := Regfile(Index) & Flags(PSR_C);
1326
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1327
          Flags(PSR_C)       <= Temp(8);
1328
          Flags(PSR_N)       <= Temp(7);
1329 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1330
 
1331
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1332 185 jshamlet
          Temp               := Regfile(Index)(0) & Flags(PSR_C) &
1333 169 jshamlet
                                Regfile(Index)(7 downto 1);
1334 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1335
          Flags(PSR_C)       <= Temp(8);
1336
          Flags(PSR_N)       <= Temp(7);
1337 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1338
 
1339
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1340
          Sum                := ("0" & Regfile(Index)) +
1341
                                ("0" & x"FF");
1342 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1343
          Flags(PSR_C)       <= Sum(8);
1344
          Flags(PSR_N)       <= Sum(7);
1345 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1346
 
1347
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1348
          Sum                := ("0" & Regfile(0)) +
1349
                                ("1" & (not Regfile(Index))) +
1350 185 jshamlet
                                Flags(PSR_C);
1351
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1352
          Flags(PSR_C)       <= Sum(8);
1353
          Flags(PSR_N)       <= Sum(7);
1354 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1355
 
1356
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1357
          Sum                := ("0" & Regfile(0)) +
1358
                                ("0" & Regfile(Index));
1359 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1360 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1361 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1362
          Flags(PSR_N)       <= Sum(7);
1363 169 jshamlet
 
1364
        when ALU_STP => -- Sets bit(n) in the Flags register
1365
          Flags(Index)       <= '1';
1366
 
1367
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1368 185 jshamlet
          Flags(PSR_Z)       <= not Regfile(0)(Index);
1369
          Flags(PSR_N)       <= Regfile(0)(7);
1370 169 jshamlet
 
1371
        when ALU_CLP => -- Clears bit(n) in the Flags register
1372
          Flags(Index)       <= '0';
1373
 
1374
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1375
          Temp               := "0" & Regfile(0);
1376 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1377
          Flags(PSR_N)       <= Temp(7);
1378 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1379
 
1380
        when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1381
          Sum                := ("0" & Regfile(0)) +
1382
                                ("1" & (not Regfile(Index))) +
1383
                                '1';
1384 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1385
          Flags(PSR_C)       <= Sum(8);
1386
          Flags(PSR_N)       <= Sum(7);
1387 169 jshamlet
 
1388
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1389
          Regfile(0)         <= Mult(7 downto 0);
1390
          Regfile(1)         <= Mult(15 downto 8);
1391 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Mult);
1392 169 jshamlet
 
1393
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1394 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Operand1);
1395
          Flags(PSR_N)       <= Operand1(7);
1396
          Regfile(Index)     <= Operand1;
1397 169 jshamlet
 
1398
        when ALU_POP => -- Rn <= Data
1399 185 jshamlet
          Regfile(Index)     <= Operand1;
1400 169 jshamlet
 
1401
        when ALU_RFLG =>
1402 188 jshamlet
          Flags(3 downto 0)  <= Operand1(3 downto 0);
1403
          if( not RTI_Ignores_GP_Flags )then
1404
            Flags(7 downto 4)<= Operand1(7 downto 4);
1405
          end if;
1406 169 jshamlet
 
1407 185 jshamlet
        when ALU_RSP =>
1408 181 jshamlet
          Regfile(0)         <= Stack_Ptr(7 downto 0);
1409
          Regfile(1)         <= Stack_Ptr(15 downto 8);
1410
 
1411 185 jshamlet
        when ALU_GMSK =>
1412
          Flags(PSR_Z)       <= nor_reduce(Int_Mask);
1413
          Regfile(0)         <= Int_Mask;
1414
 
1415 169 jshamlet
        when others =>
1416
          null;
1417
      end case;
1418
 
1419 224 jshamlet
      Open8_Bus.GP_Flags     <= Flags(7 downto 3);
1420 188 jshamlet
 
1421 169 jshamlet
    end if;
1422
  end process;
1423
 
1424 182 jshamlet
-------------------------------------------------------------------------------
1425
-- Multiplier Logic
1426
--
1427
-- We need to infer a hardware multipler, so we create a special clocked
1428
--  process with no reset or clock enable
1429
-------------------------------------------------------------------------------
1430
 
1431
  Multiplier_proc: process( Clock )
1432
  begin
1433
    if( rising_edge(Clock) )then
1434
      Mult                   <= Regfile(0) *
1435 186 jshamlet
                                Regfile(conv_integer(ALU_Ctrl.Reg));
1436
    end if;
1437
  end process;
1438
 
1439
end architecture;

powered by: WebSVN 2.1.0

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