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 188

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
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
--
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 169 jshamlet
 
198
library ieee;
199
  use ieee.std_logic_1164.all;
200
  use ieee.std_logic_unsigned.all;
201
  use ieee.std_logic_arith.all;
202
  use ieee.std_logic_misc.all;
203
 
204
library work;
205
use work.Open8_pkg.all;
206
 
207 183 jshamlet
entity o8_cpu is
208 169 jshamlet
  generic(
209
    Program_Start_Addr       : ADDRESS_TYPE := x"0000"; -- Initial PC location
210
    ISR_Start_Addr           : ADDRESS_TYPE := x"FFF0"; -- Bottom of ISR vec's
211
    Stack_Start_Addr         : ADDRESS_TYPE := x"03FF"; -- Top of Stack
212
    Allow_Stack_Address_Move : boolean      := false;   -- Use Normal v8 RSP
213 188 jshamlet
    Stack_Xfer_Flag          : integer      := PSR_GP4; -- GP4 modifies RSP
214 169 jshamlet
    Enable_Auto_Increment    : boolean      := false;   -- Modify indexed instr
215
    BRK_Implements_WAI       : boolean      := false;   -- BRK -> Wait for Int
216
    Enable_NMI               : boolean      := true;    -- Force INTR0 enabled
217 188 jshamlet
    RTI_Ignores_GP_Flags     : boolean      := false;   -- RTI restores all flags
218 169 jshamlet
    Default_Interrupt_Mask   : DATA_TYPE    := x"FF";   -- Enable all Ints
219
    Reset_Level              : std_logic    := '0' );   -- Active reset level
220
  port(
221
    Clock                    : in  std_logic;
222
    Reset                    : in  std_logic;
223 187 jshamlet
    CPU_Halt                 : in  std_logic := '0';
224
    Interrupts               : in  INTERRUPT_BUNDLE := x"00";
225 188 jshamlet
    GP_Flags                 : out EXT_GP_FLAGS;
226 169 jshamlet
    --
227
    Address                  : out ADDRESS_TYPE;
228
    Rd_Data                  : in  DATA_TYPE;
229
    Rd_Enable                : out std_logic;
230
    Wr_Data                  : out DATA_TYPE;
231
    Wr_Enable                : out std_logic );
232
end entity;
233
 
234 183 jshamlet
architecture behave of o8_cpu is
235 169 jshamlet
 
236
  constant INT_VECTOR_0      : ADDRESS_TYPE := ISR_Start_Addr;
237
  constant INT_VECTOR_1      : ADDRESS_TYPE := ISR_Start_Addr+2;
238
  constant INT_VECTOR_2      : ADDRESS_TYPE := ISR_Start_Addr+4;
239
  constant INT_VECTOR_3      : ADDRESS_TYPE := ISR_Start_Addr+6;
240
  constant INT_VECTOR_4      : ADDRESS_TYPE := ISR_Start_Addr+8;
241
  constant INT_VECTOR_5      : ADDRESS_TYPE := ISR_Start_Addr+10;
242
  constant INT_VECTOR_6      : ADDRESS_TYPE := ISR_Start_Addr+12;
243
  constant INT_VECTOR_7      : ADDRESS_TYPE := ISR_Start_Addr+14;
244
 
245 187 jshamlet
  signal CPU_Next_State      : CPU_STATES := IPF_C0;
246
  signal CPU_State           : CPU_STATES := IPF_C0;
247 169 jshamlet
 
248 187 jshamlet
  signal CPU_Halt_Req        : std_logic;
249
 
250 169 jshamlet
  signal Cache_Ctrl          : CACHE_MODES := CACHE_IDLE;
251
 
252
  signal Opcode              : OPCODE_TYPE := (others => '0');
253
  signal SubOp, SubOp_p1     : SUBOP_TYPE  := (others => '0');
254
 
255
  signal Prefetch            : DATA_TYPE   := x"00";
256
  signal Operand1, Operand2  : DATA_TYPE   := x"00";
257
 
258
  signal Instr_Prefetch      : std_logic   := '0';
259
 
260
  signal PC_Ctrl             : PC_CTRL_TYPE;
261
  signal Program_Ctr         : ADDRESS_TYPE := x"0000";
262
 
263 182 jshamlet
  signal ALU_Ctrl            : ALU_CTRL_TYPE;
264
  signal Regfile             : REGFILE_TYPE;
265
  signal Flags               : FLAG_TYPE;
266
  signal Mult                : ADDRESS_TYPE := x"0000";
267
 
268 169 jshamlet
  signal SP_Ctrl             : SP_CTRL_TYPE;
269
  signal Stack_Ptr           : ADDRESS_TYPE := x"0000";
270
 
271
  signal DP_Ctrl             : DATA_CTRL_TYPE;
272
 
273
  signal INT_Ctrl            : INT_CTRL_TYPE;
274
  signal Ack_D, Ack_Q, Ack_Q1: std_logic   := '0';
275
  signal Int_Req, Int_Ack    : std_logic   := '0';
276
  signal Int_Mask            : DATA_TYPE   := x"00";
277
  signal ISR_Addr            : ADDRESS_TYPE := x"0000";
278
  signal i_Ints              : INTERRUPT_BUNDLE := x"00";
279
  signal Pending             : INTERRUPT_BUNDLE := x"00";
280
  signal Wait_for_FSM        : std_logic := '0';
281
 
282
begin
283
 
284 185 jshamlet
 
285 169 jshamlet
-------------------------------------------------------------------------------
286 182 jshamlet
-- Address bus selection/generation logic
287 169 jshamlet
-------------------------------------------------------------------------------
288
 
289 185 jshamlet
  Address_Logic: process(CPU_State, Regfile, SubOp, SubOp_p1, Operand1,
290
                         Operand2, Program_Ctr, Stack_Ptr, ISR_Addr )
291 169 jshamlet
    variable Reg, Reg_1      : integer range 0 to 7 := 0;
292
    variable Offset_SX       : ADDRESS_TYPE;
293
  begin
294 182 jshamlet
 
295
    if( Enable_Auto_Increment )then
296
      Reg                    := conv_integer(SubOp(2 downto 1) & '0');
297
      Reg_1                  := conv_integer(SubOp(2 downto 1) & '1');
298
    else
299
      Reg                    := conv_integer(SubOp);
300
      Reg_1                  := conv_integer(SubOp_p1);
301
    end if;
302
 
303
    Offset_SX(15 downto 0)   := (others => Operand1(7));
304
    Offset_SX(7 downto 0)    := Operand1;
305
 
306
    case( CPU_State )is
307
 
308
      when LDA_C2 | STA_C2 =>
309
        Address              <= Operand2 & Operand1;
310
 
311
      when LDX_C1 | STX_C1 =>
312
        Address              <= (Regfile(Reg_1) & Regfile(Reg));
313
 
314
      when LDO_C1 | STO_C1 =>
315
        Address              <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
316
 
317
      when ISR_C1 | ISR_C2 =>
318
        Address              <= ISR_Addr;
319
 
320
      when PSH_C1 | POP_C1 | ISR_C3 | JSR_C1 | JSR_C2 | RTS_C1 | RTS_C2 | RTS_C3 =>
321
        Address              <= Stack_Ptr;
322
 
323
      when others =>
324
        Address              <= Program_Ctr;
325
 
326
    end case;
327
 
328
  end process;
329
 
330
-------------------------------------------------------------------------------
331
-- Combinatorial portion of CPU finite state machine
332
-- State Logic / Instruction Decoding & Execution
333
-------------------------------------------------------------------------------
334
 
335 187 jshamlet
  State_Logic: process(CPU_State, Flags, Int_Mask, CPU_Halt_Req, Opcode,
336 182 jshamlet
                       SubOp , SubOp_p1, Operand1, Operand2, Int_Req )
337
    variable Reg             : integer range 0 to 7 := 0;
338
  begin
339 169 jshamlet
    CPU_Next_State           <= CPU_State;
340
    Cache_Ctrl               <= CACHE_IDLE;
341
    --
342 185 jshamlet
    PC_Ctrl.Oper             <= PC_INCR;
343
    PC_Ctrl.Offset           <= PC_IDLE;
344 182 jshamlet
    --
345 169 jshamlet
    ALU_Ctrl.Oper            <= ALU_IDLE;
346
    ALU_Ctrl.Reg             <= ACCUM;
347
    --
348
    SP_Ctrl.Oper             <= SP_IDLE;
349
    --
350
    DP_Ctrl.Src              <= DATA_RD_MEM;
351
    DP_Ctrl.Reg              <= ACCUM;
352
    --
353
    INT_Ctrl.Mask_Set        <= '0';
354
    INT_Ctrl.Soft_Ints       <= x"00";
355
    INT_Ctrl.Incr_ISR        <= '0';
356
    Ack_D                    <= '0';
357
 
358 182 jshamlet
    Reg                     := conv_integer(SubOp);
359 169 jshamlet
 
360
    case CPU_State is
361
-------------------------------------------------------------------------------
362
-- Initial Instruction fetch & decode
363
-------------------------------------------------------------------------------
364 187 jshamlet
      when IPF_C0 =>
365
        CPU_Next_State       <= IPF_C1;
366 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
367 169 jshamlet
 
368 187 jshamlet
      when IPF_C1 =>
369
        CPU_Next_State       <= IPF_C2;
370 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
371 169 jshamlet
 
372 187 jshamlet
      when IPF_C2 =>
373
        CPU_Next_State       <= IDC_C0;
374 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
375 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
376 169 jshamlet
 
377 187 jshamlet
      when IDC_C0 =>
378
        CPU_Next_State       <= IDC_C0;
379 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
380
 
381
        case Opcode is
382
          when OP_PSH =>
383
            CPU_Next_State   <= PSH_C1;
384
            Cache_Ctrl       <= CACHE_PREFETCH;
385 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
386 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
387
            DP_Ctrl.Reg      <= SubOp;
388
 
389
          when OP_POP =>
390
            CPU_Next_State   <= POP_C1;
391
            Cache_Ctrl       <= CACHE_PREFETCH;
392 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
393 169 jshamlet
            SP_Ctrl.Oper     <= SP_POP;
394
 
395
          when OP_BR0 | OP_BR1 =>
396
            CPU_Next_State   <= BRN_C1;
397
            Cache_Ctrl       <= CACHE_OPER1;
398 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
399 169 jshamlet
 
400 185 jshamlet
 
401 169 jshamlet
          when OP_DBNZ =>
402
            CPU_Next_State   <= DBNZ_C1;
403
            Cache_Ctrl       <= CACHE_OPER1;
404 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
405 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_DEC;
406
            ALU_Ctrl.Reg     <= SubOp;
407
 
408
          when OP_INT =>
409 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
410 187 jshamlet
            -- Make sure the requested interrupt is actually enabled first.
411
            --  Also, unlike CPU_Halt, the INT instruction is actually being
412
            --  executed, so go ahead and increment the program counter before
413
            --  pausing so the CPU restarts on the next instruction.
414 169 jshamlet
            if( Int_Mask(Reg) = '1' )then
415 187 jshamlet
              CPU_Next_State <= WAI_Cx;
416 169 jshamlet
              INT_Ctrl.Soft_Ints(Reg) <= '1';
417
            end if;
418
 
419
          when OP_STK =>
420
            case SubOp is
421
              when SOP_RSP  =>
422 185 jshamlet
                PC_Ctrl.Offset <= PC_NEXT;
423 181 jshamlet
                if( not Allow_Stack_Address_Move )then
424 187 jshamlet
                  -- The default behavior for this instruction is to simply
425
                  --  repoint the SP to the HDL default
426 185 jshamlet
                  SP_Ctrl.Oper    <= SP_CLR;
427 181 jshamlet
                end if;
428 187 jshamlet
                if( Allow_Stack_Address_Move and
429
                    Flags(Stack_Xfer_Flag) = '1' )then
430
                  -- If RSP is set to allow SP moves, and the specified flag
431
                  --  is true, then signal the stack pointer logic to load
432
                  --  from R1:R0
433 185 jshamlet
                  SP_Ctrl.Oper    <= SP_SET;
434 181 jshamlet
                end if;
435 187 jshamlet
                if( Allow_Stack_Address_Move and
436
                    Flags(Stack_Xfer_Flag) = '0')then
437
                  -- If RSP is set to allow SP moves, and the specified flag
438
                  --  is false, then signal the ALU to copy the stack pointer
439
                  --  to R1:R0
440 185 jshamlet
                  ALU_Ctrl.Oper   <= ALU_RSP;
441 181 jshamlet
                end if;
442 169 jshamlet
 
443
              when SOP_RTS | SOP_RTI =>
444 185 jshamlet
                CPU_Next_State    <= RTS_C1;
445
                SP_Ctrl.Oper      <= SP_POP;
446 169 jshamlet
 
447
              when SOP_BRK  =>
448
                if( BRK_Implements_WAI )then
449 187 jshamlet
                  -- If BRK_Implements_WAI, then jump to the WAI_Cx and
450
                  --  increment the PC similar to an ISR flow.
451
                  CPU_Next_State  <= WAI_Cx;
452 185 jshamlet
                  PC_Ctrl.Offset  <= PC_NEXT;
453 187 jshamlet
                else
454
                -- If Break is implemented normally, back the PC up by
455
                --  2 and return through IPF_C0 in order to execute a 5
456
                --  clock cycle delay
457
                  CPU_Next_State  <= BRK_C1;
458
                  PC_Ctrl.Offset  <= PC_REV2;
459 169 jshamlet
                end if;
460
 
461
              when SOP_JMP  =>
462 185 jshamlet
                CPU_Next_State    <= JMP_C1;
463
                Cache_Ctrl        <= CACHE_OPER1;
464 169 jshamlet
 
465
              when SOP_SMSK =>
466 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
467 169 jshamlet
                INT_Ctrl.Mask_Set <= '1';
468
 
469
              when SOP_GMSK =>
470 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
471
                ALU_Ctrl.Oper     <= ALU_GMSK;
472 169 jshamlet
 
473
              when SOP_JSR =>
474
                CPU_Next_State <= JSR_C1;
475 185 jshamlet
                Cache_Ctrl        <= CACHE_OPER1;
476
                DP_Ctrl.Src       <= DATA_WR_PC;
477
                DP_Ctrl.Reg       <= PC_MSB;
478 169 jshamlet
 
479
              when others => null;
480
            end case;
481
 
482
          when OP_MUL =>
483
            CPU_Next_State   <= MUL_C1;
484 181 jshamlet
            -- Multiplication requires a single clock cycle to calculate PRIOR
485
            --  to the ALU writing the result to registers. As a result, this
486
            --  state needs to idle the ALU initially, and back the PC up by 1
487
            -- We can get away with only 1 extra clock by pre-fetching the
488
            --  next instruction, though.
489 169 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
490 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
491 181 jshamlet
            -- Note that both the multiply process AND ALU process need the
492
            --  source register for Rn (R1:R0 = R0 * Rn). Assert ALU_Ctrl.reg
493
            --  now, but hold off on the ALU command until the next state.
494 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_IDLE;
495
            ALU_Ctrl.Reg     <= SubOp;
496
 
497
          when OP_UPP =>
498
            CPU_Next_State   <= UPP_C1;
499
            Cache_Ctrl       <= CACHE_PREFETCH;
500 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
501 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
502
            ALU_Ctrl.Reg     <= SubOp;
503
 
504
          when OP_LDA =>
505
            CPU_Next_State   <= LDA_C1;
506
            Cache_Ctrl       <= CACHE_OPER1;
507
 
508
          when OP_LDI =>
509
            CPU_Next_State   <= LDI_C1;
510
            Cache_Ctrl       <= CACHE_OPER1;
511 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
512 169 jshamlet
 
513
          when OP_LDO =>
514
            CPU_Next_State   <= LDO_C1;
515
            Cache_Ctrl       <= CACHE_OPER1;
516 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
517 169 jshamlet
 
518
          when OP_LDX =>
519
            CPU_Next_State   <= LDX_C1;
520 181 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
521 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
522 169 jshamlet
 
523
          when OP_STA =>
524
            CPU_Next_State   <= STA_C1;
525
            Cache_Ctrl       <= CACHE_OPER1;
526
 
527
          when OP_STO =>
528
            CPU_Next_State   <= STO_C1;
529
            Cache_Ctrl       <= CACHE_OPER1;
530 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
531 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
532
            DP_Ctrl.Reg      <= ACCUM;
533
 
534
          when OP_STX =>
535
            CPU_Next_State   <= STX_C1;
536
            Cache_Ctrl       <= CACHE_PREFETCH;
537 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
538 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
539
            DP_Ctrl.Reg      <= ACCUM;
540
 
541
          when others =>
542 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
543 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
544
            ALU_Ctrl.Reg     <= SubOp;
545
 
546
        end case;
547
 
548 186 jshamlet
        if( Int_Req = '1' )then
549
          CPU_Next_State     <= ISR_C1;
550 187 jshamlet
        end if;
551
 
552
        if( CPU_Halt_Req = '1' )then
553
          CPU_Next_State     <= WAH_Cx;
554
        end if;
555
 
556
        -- If either of these override conditions are true, the decoder needs
557
        --  to undo everything it just setup, since even "single-cycle"
558
        --  instructions will be executed again upon return.
559
        if( Int_Req = '1' or CPU_Halt_Req = '1' )then
560
          -- In either case, we want to skip loading the cache, as the cache
561
          --  will be invalid by the time we get back.
562 186 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
563 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to the current instruction,
564
          -- compensating for the pipeline registers.
565 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
566
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
567 187 jshamlet
          --  operation due to the current instruction.
568 186 jshamlet
          ALU_Ctrl.Oper      <= ALU_IDLE;
569
          SP_Ctrl.Oper       <= SP_IDLE;
570 187 jshamlet
          -- Interrupt logic outside of the state machine needs this to be set
571
          --  to DATA_RD_MEM, while CPU_Halt considers this a "don't care".
572 186 jshamlet
          DP_Ctrl.Src        <= DATA_RD_MEM;
573 187 jshamlet
          -- If an INT/SMSK instruction was going to be executed, it will get
574
          --  executed again when normal processing resumes, so axe their
575
          --  requests for now.
576
          INT_Ctrl.Mask_Set       <= '0';
577
          INT_Ctrl.Soft_Ints(Reg) <= '0';
578 186 jshamlet
        end if;
579
 
580 169 jshamlet
-------------------------------------------------------------------------------
581
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
582
-------------------------------------------------------------------------------
583
 
584
      when BRN_C1 =>
585 187 jshamlet
        CPU_Next_State       <= IDC_C0;
586 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
587 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
588 169 jshamlet
        if( Flags(Reg) = Opcode(0) )then
589 187 jshamlet
          CPU_Next_State     <= IPF_C0;
590 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
591
          PC_Ctrl.Offset     <= Operand1;
592
        end if;
593
 
594
      when DBNZ_C1 =>
595 187 jshamlet
        CPU_Next_State       <= IDC_C0;
596 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
597 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
598
        if( Flags(PSR_Z) = '0' )then
599 187 jshamlet
          CPU_Next_State     <= IPF_C0;
600 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
601
          PC_Ctrl.Offset     <= Operand1;
602
        end if;
603
 
604
      when JMP_C1 =>
605
        CPU_Next_State       <= JMP_C2;
606
        Cache_Ctrl           <= CACHE_OPER2;
607
 
608
      when JMP_C2 =>
609 187 jshamlet
        CPU_Next_State       <= IPF_C0;
610 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
611
 
612
-------------------------------------------------------------------------------
613
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
614
-------------------------------------------------------------------------------
615
 
616
      when LDA_C1 =>
617
        CPU_Next_State       <= LDA_C2;
618
        Cache_Ctrl           <= CACHE_OPER2;
619
 
620
      when LDA_C2 =>
621
        CPU_Next_State       <= LDA_C3;
622
 
623
      when LDA_C3 =>
624
        CPU_Next_State       <= LDA_C4;
625 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
626 169 jshamlet
 
627
      when LDA_C4 =>
628
        CPU_Next_State       <= LDI_C1;
629
        Cache_Ctrl           <= CACHE_OPER1;
630 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
631 169 jshamlet
 
632
      when LDI_C1 =>
633 187 jshamlet
        CPU_Next_State       <= IDC_C0;
634 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
635 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
636 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
637
        ALU_Ctrl.Reg         <= SubOp;
638
 
639
      when LDO_C1 =>
640 181 jshamlet
        CPU_Next_State       <= LDX_C2;
641 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
642 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
643
          ALU_Ctrl.Oper      <= ALU_UPP;
644
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
645 169 jshamlet
        end if;
646
 
647
      when LDX_C1 =>
648
        CPU_Next_State       <= LDX_C2;
649 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
650
          ALU_Ctrl.Oper      <= ALU_UPP;
651
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
652 181 jshamlet
        end if;
653 169 jshamlet
 
654
      when LDX_C2 =>
655
        CPU_Next_State       <= LDX_C3;
656 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
657 181 jshamlet
 
658
      when LDX_C3 =>
659
        CPU_Next_State       <= LDX_C4;
660 182 jshamlet
        Cache_Ctrl           <= CACHE_OPER1;
661 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
662 169 jshamlet
 
663 181 jshamlet
      when LDX_C4 =>
664 187 jshamlet
        CPU_Next_State       <= IDC_C0;
665 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
666 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
667 181 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
668 169 jshamlet
        ALU_Ctrl.Reg         <= ACCUM;
669
 
670
-------------------------------------------------------------------------------
671
-- Data Storage - Store to memory (STA, STO, STX)
672
-------------------------------------------------------------------------------
673
      when STA_C1 =>
674
        CPU_Next_State       <= STA_C2;
675
        Cache_Ctrl           <= CACHE_OPER2;
676
        DP_Ctrl.Src          <= DATA_WR_REG;
677
        DP_Ctrl.Reg          <= SubOp;
678
 
679
      when STA_C2 =>
680
        CPU_Next_State       <= STA_C3;
681 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
682 169 jshamlet
 
683
      when STA_C3 =>
684 187 jshamlet
        CPU_Next_State       <= IPF_C2;
685 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
686 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
687 169 jshamlet
 
688
      when STO_C1 =>
689 187 jshamlet
        CPU_Next_State       <= IPF_C0;
690 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
691 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
692 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
693
          CPU_Next_State     <= STO_C2;
694
          ALU_Ctrl.Oper      <= ALU_UPP;
695
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
696 169 jshamlet
        end if;
697
 
698
      when STO_C2 =>
699 187 jshamlet
        CPU_Next_State       <= IPF_C1;
700 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
701 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
702
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
703
 
704
      when STX_C1 =>
705 187 jshamlet
        CPU_Next_State       <= IPF_C1;
706 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
707 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
708
          CPU_Next_State     <= STX_C2;
709
          ALU_Ctrl.Oper      <= ALU_UPP;
710
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
711 169 jshamlet
        end if;
712
 
713
      when STX_C2 =>
714 187 jshamlet
        CPU_Next_State       <= IPF_C2;
715 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
716 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
717
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
718
 
719
-------------------------------------------------------------------------------
720
-- Multi-Cycle Math Operations (UPP, MUL)
721
-------------------------------------------------------------------------------
722
 
723
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
724 181 jshamlet
      --  instruction/first operand, we have to return through PF2. Also, we
725
      --  need to tell the ALU to store the results to R1:R0 here. Note that
726
      --  there is no ALU_Ctrl.Reg, as this is implied in the ALU instruction
727 169 jshamlet
      when MUL_C1 =>
728 187 jshamlet
        CPU_Next_State       <= IPF_C2;
729 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
730 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_MUL;
731
 
732
      when UPP_C1 =>
733 187 jshamlet
        CPU_Next_State       <= IPF_C2;
734 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
735 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
736
        ALU_Ctrl.Reg         <= SubOp_p1;
737
 
738
-------------------------------------------------------------------------------
739
-- Basic Stack Manipulation (PSH, POP, RSP)
740
-------------------------------------------------------------------------------
741
      when PSH_C1 =>
742 187 jshamlet
        CPU_Next_State       <= IPF_C1;
743 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
744
 
745
      when POP_C1 =>
746
        CPU_Next_State       <= POP_C2;
747
 
748
      when POP_C2 =>
749
        CPU_Next_State       <= POP_C3;
750 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
751 169 jshamlet
 
752
      when POP_C3 =>
753
        CPU_Next_State       <= POP_C4;
754
        Cache_Ctrl           <= CACHE_OPER1;
755 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
756 169 jshamlet
 
757
      when POP_C4 =>
758 187 jshamlet
        CPU_Next_State       <= IDC_C0;
759 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
760 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
761 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_POP;
762
        ALU_Ctrl.Reg         <= SubOp;
763 172 jshamlet
 
764 169 jshamlet
-------------------------------------------------------------------------------
765
-- Subroutines & Interrupts (RTS, JSR)
766
-------------------------------------------------------------------------------
767 187 jshamlet
      when WAI_Cx => -- For soft interrupts only, halt the Program_Ctr
768 169 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
769 186 jshamlet
        if( Int_Req = '1' )then
770
          CPU_Next_State     <= ISR_C1;
771 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to would have been the next
772
          --  instruction, compensating for the pipeline registers.
773 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
774
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
775
          --  operation due to the current instruction
776
          DP_Ctrl.Src        <= DATA_RD_MEM;
777
        end if;
778 169 jshamlet
 
779 187 jshamlet
      when WAH_Cx => -- Holds until CPU_Halt_Req is deasserted.
780
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
781
        if( CPU_Halt_Req = '0' )then
782
          CPU_Next_State     <= IPF_C0;
783
          DP_Ctrl.Src        <= DATA_RD_MEM;
784
        end if;
785
 
786
      when BRK_C1 => -- Debugging (BRK) Performs a 5-clock NOP.
787
        CPU_Next_State       <= IPF_C0;
788
 
789 169 jshamlet
      when ISR_C1 =>
790
        CPU_Next_State       <= ISR_C2;
791
        INT_Ctrl.Incr_ISR    <= '1';
792
 
793
      when ISR_C2 =>
794
        CPU_Next_State       <= ISR_C3;
795
        DP_Ctrl.Src          <= DATA_WR_FLAG;
796
 
797
      when ISR_C3 =>
798
        CPU_Next_State       <= JSR_C1;
799
        Cache_Ctrl           <= CACHE_OPER1;
800 182 jshamlet
        ALU_Ctrl.Oper        <= ALU_STP;
801 185 jshamlet
        ALU_Ctrl.Reg         <= conv_std_logic_vector(PSR_I,3);
802 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
803
        DP_Ctrl.Src          <= DATA_WR_PC;
804 182 jshamlet
        DP_Ctrl.Reg          <= PC_MSB;
805 169 jshamlet
        Ack_D                <= '1';
806
 
807
      when JSR_C1 =>
808
        CPU_Next_State       <= JSR_C2;
809
        Cache_Ctrl           <= CACHE_OPER2;
810
        SP_Ctrl.Oper         <= SP_PUSH;
811
        DP_Ctrl.Src          <= DATA_WR_PC;
812 182 jshamlet
        DP_Ctrl.Reg          <= PC_LSB;
813 169 jshamlet
 
814
      when JSR_C2 =>
815 187 jshamlet
        CPU_Next_State       <= IPF_C0;
816 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
817 182 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
818 169 jshamlet
 
819
      when RTS_C1 =>
820
        CPU_Next_State       <= RTS_C2;
821
        SP_Ctrl.Oper         <= SP_POP;
822
 
823
      when RTS_C2 =>
824
        CPU_Next_State       <= RTS_C3;
825
        -- if this is an RTI, then we need to POP the flags
826
        if( SubOp = SOP_RTI )then
827
          SP_Ctrl.Oper       <= SP_POP;
828
        end if;
829
 
830
      when RTS_C3 =>
831
        CPU_Next_State       <= RTS_C4;
832
        Cache_Ctrl           <= CACHE_OPER1;
833
 
834
      when RTS_C4 =>
835
        CPU_Next_State       <= RTS_C5;
836
        Cache_Ctrl           <= CACHE_OPER2;
837
 
838
      when RTS_C5 =>
839 187 jshamlet
        CPU_Next_State       <= IPF_C0;
840 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
841 185 jshamlet
        -- if this is an RTI, then we need to clear the I bit
842 169 jshamlet
        if( SubOp = SOP_RTI )then
843
          CPU_Next_State     <= RTI_C6;
844
          Cache_Ctrl         <= CACHE_OPER1;
845 185 jshamlet
          ALU_Ctrl.Oper      <= ALU_CLP;
846
          ALU_Ctrl.Reg       <= conv_std_logic_vector(PSR_I,3);
847 169 jshamlet
        end if;
848
 
849
      when RTI_C6 =>
850 187 jshamlet
        CPU_Next_State       <= IPF_C1;
851 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
852 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_RFLG;
853
 
854
      when others =>
855
        null;
856
    end case;
857
 
858
  end process;
859
 
860
-------------------------------------------------------------------------------
861
-- Registered portion of CPU finite state machine
862
-------------------------------------------------------------------------------
863 182 jshamlet
 
864 169 jshamlet
  CPU_Regs: process( Reset, Clock )
865
    variable Offset_SX       : ADDRESS_TYPE;
866 188 jshamlet
    variable i_Ints          : INTERRUPT_BUNDLE := x"00";
867 169 jshamlet
    variable Index           : integer range 0 to 7         := 0;
868
    variable Sum             : std_logic_vector(8 downto 0) := "000000000";
869
    variable Temp            : std_logic_vector(8 downto 0) := "000000000";
870
  begin
871
    if( Reset = Reset_Level )then
872 187 jshamlet
      CPU_State              <= IPF_C0;
873 169 jshamlet
      Opcode                 <= OP_INC;
874
      SubOp                  <= ACCUM;
875
      SubOp_p1               <= ACCUM;
876
      Operand1               <= x"00";
877
      Operand2               <= x"00";
878
      Instr_Prefetch         <= '0';
879
      Prefetch               <= x"00";
880
 
881 187 jshamlet
      CPU_Halt_Req           <= '0';
882
 
883 188 jshamlet
      Wr_Data                <= x"00";
884 169 jshamlet
      Wr_Enable              <= '0';
885
      Rd_Enable              <= '1';
886
 
887
      Program_Ctr            <= Program_Start_Addr;
888
      Stack_Ptr              <= Stack_Start_Addr;
889
 
890
      Ack_Q                  <= '0';
891
      Ack_Q1                 <= '0';
892
      Int_Ack                <= '0';
893
 
894
      Int_Req                <= '0';
895
      Pending                <= x"00";
896
      Wait_for_FSM           <= '0';
897
      if( Enable_NMI )then
898
        Int_Mask             <= Default_Interrupt_Mask(7 downto 1) & '1';
899
      else
900
        Int_Mask             <= Default_Interrupt_Mask;
901
      end if;
902
      ISR_Addr               <= INT_VECTOR_0;
903
 
904
      for i in 0 to 7 loop
905 188 jshamlet
        Regfile(i)           <= x"00";
906 169 jshamlet
      end loop;
907
      Flags                  <= x"00";
908
 
909 188 jshamlet
      GP_Flags               <= x"0";
910
 
911 169 jshamlet
    elsif( rising_edge(Clock) )then
912 187 jshamlet
 
913
      CPU_Halt_Req           <= CPU_Halt;
914
 
915 169 jshamlet
      Wr_Enable              <= '0';
916
      Wr_Data                <= x"00";
917
      Rd_Enable              <= '0';
918
 
919
-------------------------------------------------------------------------------
920
-- Instruction/Operand caching for pipelined memory access
921
-------------------------------------------------------------------------------
922
      CPU_State              <= CPU_Next_State;
923
      case Cache_Ctrl is
924
        when CACHE_INSTR =>
925
          Opcode             <= Rd_Data(7 downto 3);
926
          SubOp              <= Rd_Data(2 downto 0);
927
          SubOp_p1           <= Rd_Data(2 downto 0) + 1;
928
          if( Instr_Prefetch = '1' )then
929
            Opcode           <= Prefetch(7 downto 3);
930
            SubOp            <= Prefetch(2 downto 0);
931
            SubOp_p1         <= Prefetch(2 downto 0) + 1;
932
            Instr_Prefetch   <= '0';
933
          end if;
934
 
935
        when CACHE_OPER1 =>
936
          Operand1           <= Rd_Data;
937
 
938
        when CACHE_OPER2 =>
939
          Operand2           <= Rd_Data;
940
 
941
        when CACHE_PREFETCH =>
942
          Prefetch           <= Rd_Data;
943
          Instr_Prefetch     <= '1';
944
 
945
        when CACHE_IDLE =>
946
          null;
947
      end case;
948
 
949
-------------------------------------------------------------------------------
950
-- Program Counter
951
-------------------------------------------------------------------------------
952
      Offset_SX(15 downto 8) := (others => PC_Ctrl.Offset(7));
953
      Offset_SX(7 downto 0)  := PC_Ctrl.Offset;
954
 
955
      case PC_Ctrl.Oper is
956
        when PC_INCR =>
957
          Program_Ctr        <= Program_Ctr + Offset_SX - 2;
958
 
959
        when PC_LOAD =>
960 185 jshamlet
          Program_Ctr        <= Operand2 & Operand1;
961 169 jshamlet
 
962
        when others =>
963
          null;
964
      end case;
965
 
966
-------------------------------------------------------------------------------
967
-- (Write) Data Path
968
-------------------------------------------------------------------------------
969
      case DP_Ctrl.Src is
970
        when DATA_BUS_IDLE =>
971
          null;
972
 
973
        when DATA_RD_MEM =>
974
          Rd_Enable          <= '1';
975
 
976
        when DATA_WR_REG =>
977
          Wr_Enable          <= '1';
978
          Wr_Data            <= Regfile(conv_integer(DP_Ctrl.Reg));
979
 
980
        when DATA_WR_FLAG =>
981
          Wr_Enable          <= '1';
982
          Wr_Data            <= Flags;
983
 
984
        when DATA_WR_PC =>
985
          Wr_Enable          <= '1';
986
          Wr_Data            <= Program_Ctr(15 downto 8);
987 182 jshamlet
          if( DP_Ctrl.Reg = PC_LSB )then
988 169 jshamlet
            Wr_Data          <= Program_Ctr(7 downto 0);
989
          end if;
990
 
991
        when others =>
992
          null;
993
      end case;
994
 
995
-------------------------------------------------------------------------------
996
-- Stack Pointer
997
-------------------------------------------------------------------------------
998
      case SP_Ctrl.Oper is
999
        when SP_IDLE =>
1000
          null;
1001
 
1002 181 jshamlet
        when SP_CLR =>
1003 169 jshamlet
          Stack_Ptr          <= Stack_Start_Addr;
1004
 
1005 181 jshamlet
        when SP_SET =>
1006
          Stack_Ptr          <= Regfile(1) & Regfile(0);
1007
 
1008 169 jshamlet
        when SP_POP  =>
1009
          Stack_Ptr          <= Stack_Ptr + 1;
1010
 
1011
        when SP_PUSH =>
1012
          Stack_Ptr          <= Stack_Ptr - 1;
1013
 
1014
        when others =>
1015
          null;
1016
 
1017
      end case;
1018
 
1019
-------------------------------------------------------------------------------
1020
-- Interrupt Controller
1021
-------------------------------------------------------------------------------
1022
      -- The interrupt control mask is always sourced out of R0
1023
      if( INT_Ctrl.Mask_Set = '1' )then
1024
        if( Enable_NMI )then
1025
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
1026
        else
1027
          Int_Mask           <= Regfile(conv_integer(ACCUM));
1028
        end if;
1029
      end if;
1030
 
1031
      -- Combine external and internal interrupts, and mask the OR of the two
1032
      --  with the mask. Record any incoming interrupts to the pending buffer
1033
      i_Ints                 := (Interrupts or INT_Ctrl.Soft_Ints) and
1034
                                Int_Mask;
1035 172 jshamlet
 
1036 169 jshamlet
      Pending                <= i_Ints or Pending;
1037
 
1038
      if( Wait_for_FSM = '0' )then
1039
        if(    Pending(0) = '1' )then
1040
          ISR_Addr           <= INT_VECTOR_0;
1041
          Pending(0)         <= '0';
1042
        elsif( Pending(1) = '1' )then
1043
          ISR_Addr           <= INT_VECTOR_1;
1044
          Pending(1)         <= '0';
1045
        elsif( Pending(2) = '1' )then
1046
          ISR_Addr           <= INT_VECTOR_2;
1047
          Pending(2)         <= '0';
1048
        elsif( Pending(3) = '1' )then
1049
          ISR_Addr           <= INT_VECTOR_3;
1050
          Pending(3)         <= '0';
1051
        elsif( Pending(4) = '1' )then
1052
          ISR_Addr           <= INT_VECTOR_4;
1053
          Pending(4)         <= '0';
1054
        elsif( Pending(5) = '1' )then
1055
          ISR_Addr           <= INT_VECTOR_5;
1056
          Pending(5)         <= '0';
1057
        elsif( Pending(6) = '1' )then
1058
          ISR_Addr           <= INT_VECTOR_6;
1059
          Pending(6)         <= '0';
1060
        elsif( Pending(7) = '1' )then
1061
          ISR_Addr           <= INT_VECTOR_7;
1062
          Pending(7)         <= '0';
1063
        end if;
1064 185 jshamlet
        Wait_for_FSM         <= or_reduce(Pending);
1065 169 jshamlet
      end if;
1066
 
1067
      -- Reset the Wait_for_FSM flag on Int_Ack
1068
      Ack_Q                  <= Ack_D;
1069
      Ack_Q1                 <= Ack_Q;
1070
      Int_Ack                <= Ack_Q1;
1071
      if( Int_Ack = '1' )then
1072
        Wait_for_FSM         <= '0';
1073
      end if;
1074
 
1075
      Int_Req                <= Wait_for_FSM and (not Int_Ack);
1076
 
1077
      -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1078
      --  lower half of the address.
1079
      if( INT_Ctrl.Incr_ISR = '1' )then
1080
        ISR_Addr             <= ISR_Addr + 1;
1081
      end if;
1082
 
1083
-------------------------------------------------------------------------------
1084
-- ALU (Arithmetic / Logic Unit)
1085
-------------------------------------------------------------------------------
1086
      Index                  := conv_integer(ALU_Ctrl.Reg);
1087
      Sum                    := (others => '0');
1088
      Temp                   := (others => '0');
1089
 
1090
      case ALU_Ctrl.Oper is
1091
        when ALU_INC => -- Rn = Rn + 1 : Flags N,C,Z
1092
          Sum                := ("0" & x"01") +
1093
                                ("0" & Regfile(Index));
1094 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1095
          Flags(PSR_C)       <= Sum(8);
1096
          Flags(PSR_N)      <= Sum(7);
1097 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1098
 
1099
        when ALU_UPP => -- Rn = Rn + 1
1100
          Sum                := ("0" & x"01") +
1101
                                ("0" & Regfile(Index));
1102 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1103 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1104
 
1105
        when ALU_UPP2 => -- Rn = Rn + C
1106
          Sum                := ("0" & x"00") +
1107
                                ("0" & Regfile(Index)) +
1108 185 jshamlet
                                Flags(PSR_C);
1109
          Flags(PSR_C)       <= Sum(8);
1110 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1111
 
1112
        when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1113
          Sum                := ("0" & Regfile(0)) +
1114
                                ("0" & Regfile(Index)) +
1115 185 jshamlet
                                Flags(PSR_C);
1116
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1117
          Flags(PSR_C)       <= Sum(8);
1118
          Flags(PSR_N)       <= Sum(7);
1119 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1120
 
1121
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1122
          Temp               := "0" & Regfile(Index);
1123 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1124
          Flags(PSR_N)       <= Temp(7);
1125 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1126
 
1127
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1128
          Temp(7 downto 0)   := Regfile(0) or Regfile(Index);
1129 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1130
          Flags(PSR_N)       <= Temp(7);
1131 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1132
 
1133
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1134
          Temp(7 downto 0)   := Regfile(0) and Regfile(Index);
1135 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1136
          Flags(PSR_N)       <= Temp(7);
1137 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1138
 
1139
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1140
          Temp(7 downto 0)   := Regfile(0) xor Regfile(Index);
1141 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1142
          Flags(PSR_N)       <= Temp(7);
1143 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1144
 
1145
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1146 185 jshamlet
          Temp               := Regfile(Index) & Flags(PSR_C);
1147
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1148
          Flags(PSR_C)       <= Temp(8);
1149
          Flags(PSR_N)       <= Temp(7);
1150 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1151
 
1152
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1153 185 jshamlet
          Temp               := Regfile(Index)(0) & Flags(PSR_C) &
1154 169 jshamlet
                                Regfile(Index)(7 downto 1);
1155 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1156
          Flags(PSR_C)       <= Temp(8);
1157
          Flags(PSR_N)       <= Temp(7);
1158 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1159
 
1160
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1161
          Sum                := ("0" & Regfile(Index)) +
1162
                                ("0" & x"FF");
1163 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1164
          Flags(PSR_C)       <= Sum(8);
1165
          Flags(PSR_N)       <= Sum(7);
1166 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1167
 
1168
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1169
          Sum                := ("0" & Regfile(0)) +
1170
                                ("1" & (not Regfile(Index))) +
1171 185 jshamlet
                                Flags(PSR_C);
1172
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1173
          Flags(PSR_C)       <= Sum(8);
1174
          Flags(PSR_N)       <= Sum(7);
1175 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1176
 
1177
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1178
          Sum                := ("0" & Regfile(0)) +
1179
                                ("0" & Regfile(Index));
1180 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1181 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1182 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1183
          Flags(PSR_N)       <= Sum(7);
1184 169 jshamlet
 
1185
        when ALU_STP => -- Sets bit(n) in the Flags register
1186
          Flags(Index)       <= '1';
1187
 
1188
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1189 185 jshamlet
          Flags(PSR_Z)       <= not Regfile(0)(Index);
1190
          Flags(PSR_N)       <= Regfile(0)(7);
1191 169 jshamlet
 
1192
        when ALU_CLP => -- Clears bit(n) in the Flags register
1193
          Flags(Index)       <= '0';
1194
 
1195
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1196
          Temp               := "0" & Regfile(0);
1197 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1198
          Flags(PSR_N)       <= Temp(7);
1199 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1200
 
1201
        when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1202
          Sum                := ("0" & Regfile(0)) +
1203
                                ("1" & (not Regfile(Index))) +
1204
                                '1';
1205 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1206
          Flags(PSR_C)       <= Sum(8);
1207
          Flags(PSR_N)       <= Sum(7);
1208 169 jshamlet
 
1209
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1210
          Regfile(0)         <= Mult(7 downto 0);
1211
          Regfile(1)         <= Mult(15 downto 8);
1212 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Mult);
1213 169 jshamlet
 
1214
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1215 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Operand1);
1216
          Flags(PSR_N)       <= Operand1(7);
1217
          Regfile(Index)     <= Operand1;
1218 169 jshamlet
 
1219
        when ALU_POP => -- Rn <= Data
1220 185 jshamlet
          Regfile(Index)     <= Operand1;
1221 169 jshamlet
 
1222
        when ALU_RFLG =>
1223 188 jshamlet
          Flags(3 downto 0)  <= Operand1(3 downto 0);
1224
          if( not RTI_Ignores_GP_Flags )then
1225
            Flags(7 downto 4)<= Operand1(7 downto 4);
1226
          end if;
1227 169 jshamlet
 
1228 185 jshamlet
        when ALU_RSP =>
1229 181 jshamlet
          Regfile(0)         <= Stack_Ptr(7 downto 0);
1230
          Regfile(1)         <= Stack_Ptr(15 downto 8);
1231
 
1232 185 jshamlet
        when ALU_GMSK =>
1233
          Flags(PSR_Z)       <= nor_reduce(Int_Mask);
1234
          Regfile(0)         <= Int_Mask;
1235
 
1236 169 jshamlet
        when others =>
1237
          null;
1238
      end case;
1239
 
1240 188 jshamlet
      GP_Flags               <= Flags(7 downto 4);
1241
 
1242 169 jshamlet
    end if;
1243
  end process;
1244
 
1245 182 jshamlet
-------------------------------------------------------------------------------
1246
-- Multiplier Logic
1247
--
1248
-- We need to infer a hardware multipler, so we create a special clocked
1249
--  process with no reset or clock enable
1250
-------------------------------------------------------------------------------
1251
 
1252
  Multiplier_proc: process( Clock )
1253
  begin
1254
    if( rising_edge(Clock) )then
1255
      Mult                   <= Regfile(0) *
1256 186 jshamlet
                                Regfile(conv_integer(ALU_Ctrl.Reg));
1257
    end if;
1258
  end process;
1259
 
1260
end architecture;

powered by: WebSVN 2.1.0

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