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 187

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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