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 224

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

powered by: WebSVN 2.1.0

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