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 186

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

powered by: WebSVN 2.1.0

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