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 183

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

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

powered by: WebSVN 2.1.0

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