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 260

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

powered by: WebSVN 2.1.0

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