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 223

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

powered by: WebSVN 2.1.0

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