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 210

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

powered by: WebSVN 2.1.0

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