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 185

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
                Cache_Ctrl        <= CACHE_IDLE;
410
                SP_Ctrl.Oper      <= SP_POP;
411 169 jshamlet
 
412
              when SOP_BRK  =>
413 185 jshamlet
                CPU_Next_State    <= BRK_C1;
414
                PC_Ctrl.Offset    <= PC_REV2;
415 169 jshamlet
                -- If Break implements Wait for Interrupt, replace the normal
416
                --  flow with a modified version of the INT instruction
417
                if( BRK_Implements_WAI )then
418 185 jshamlet
                  CPU_Next_State  <= WAIT_FOR_INT;
419
                  PC_Ctrl.Offset  <= PC_NEXT;
420 169 jshamlet
                end if;
421
 
422
              when SOP_JMP  =>
423 185 jshamlet
                CPU_Next_State    <= JMP_C1;
424
                Cache_Ctrl        <= CACHE_OPER1;
425 169 jshamlet
 
426
              when SOP_SMSK =>
427 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
428 169 jshamlet
                INT_Ctrl.Mask_Set <= '1';
429
 
430
              when SOP_GMSK =>
431 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
432
                ALU_Ctrl.Oper     <= ALU_GMSK;
433 169 jshamlet
 
434
              when SOP_JSR =>
435
                CPU_Next_State <= JSR_C1;
436 185 jshamlet
                Cache_Ctrl        <= CACHE_OPER1;
437
                DP_Ctrl.Src       <= DATA_WR_PC;
438
                DP_Ctrl.Reg       <= PC_MSB;
439 169 jshamlet
 
440
              when others => null;
441
            end case;
442
 
443
          when OP_MUL =>
444
            CPU_Next_State   <= MUL_C1;
445 181 jshamlet
            -- Multiplication requires a single clock cycle to calculate PRIOR
446
            --  to the ALU writing the result to registers. As a result, this
447
            --  state needs to idle the ALU initially, and back the PC up by 1
448
            -- We can get away with only 1 extra clock by pre-fetching the
449
            --  next instruction, though.
450 169 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
451 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
452 181 jshamlet
            -- Note that both the multiply process AND ALU process need the
453
            --  source register for Rn (R1:R0 = R0 * Rn). Assert ALU_Ctrl.reg
454
            --  now, but hold off on the ALU command until the next state.
455 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_IDLE;
456
            ALU_Ctrl.Reg     <= SubOp;
457
 
458
          when OP_UPP =>
459
            CPU_Next_State   <= UPP_C1;
460
            Cache_Ctrl       <= CACHE_PREFETCH;
461 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
462 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
463
            ALU_Ctrl.Reg     <= SubOp;
464
 
465
          when OP_LDA =>
466
            CPU_Next_State   <= LDA_C1;
467
            Cache_Ctrl       <= CACHE_OPER1;
468
 
469
          when OP_LDI =>
470
            CPU_Next_State   <= LDI_C1;
471
            Cache_Ctrl       <= CACHE_OPER1;
472 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
473 169 jshamlet
 
474 185 jshamlet
 
475 169 jshamlet
          when OP_LDO =>
476
            CPU_Next_State   <= LDO_C1;
477
            Cache_Ctrl       <= CACHE_OPER1;
478 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
479 169 jshamlet
 
480
          when OP_LDX =>
481
            CPU_Next_State   <= LDX_C1;
482 181 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
483 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
484 169 jshamlet
 
485
          when OP_STA =>
486
            CPU_Next_State   <= STA_C1;
487
            Cache_Ctrl       <= CACHE_OPER1;
488
 
489
          when OP_STO =>
490
            CPU_Next_State   <= STO_C1;
491
            Cache_Ctrl       <= CACHE_OPER1;
492 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
493 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
494
            DP_Ctrl.Reg      <= ACCUM;
495
 
496
          when OP_STX =>
497
            CPU_Next_State   <= STX_C1;
498
            Cache_Ctrl       <= CACHE_PREFETCH;
499 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
500 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
501
            DP_Ctrl.Reg      <= ACCUM;
502
 
503
          when others =>
504 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
505 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
506
            ALU_Ctrl.Reg     <= SubOp;
507
 
508
        end case;
509
 
510
-------------------------------------------------------------------------------
511
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
512
-------------------------------------------------------------------------------
513
 
514
      when BRN_C1 =>
515
        CPU_Next_State       <= INSTR_DECODE;
516
        Cache_Ctrl           <= CACHE_INSTR;
517 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
518 169 jshamlet
        if( Flags(Reg) = Opcode(0) )then
519
          CPU_Next_State     <= PIPE_FILL_0;
520
          Cache_Ctrl         <= CACHE_IDLE;
521
          PC_Ctrl.Offset     <= Operand1;
522
        end if;
523
 
524
      when DBNZ_C1 =>
525
        CPU_Next_State       <= INSTR_DECODE;
526
        Cache_Ctrl           <= CACHE_INSTR;
527 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
528
        if( Flags(PSR_Z) = '0' )then
529 169 jshamlet
          CPU_Next_State     <= PIPE_FILL_0;
530
          Cache_Ctrl         <= CACHE_IDLE;
531
          PC_Ctrl.Offset     <= Operand1;
532
        end if;
533
 
534
      when JMP_C1 =>
535
        CPU_Next_State       <= JMP_C2;
536
        Cache_Ctrl           <= CACHE_OPER2;
537
 
538
      when JMP_C2 =>
539
        CPU_Next_State       <= PIPE_FILL_0;
540
        PC_Ctrl.Oper         <= PC_LOAD;
541
 
542
-------------------------------------------------------------------------------
543
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
544
-------------------------------------------------------------------------------
545
 
546
      when LDA_C1 =>
547
        CPU_Next_State       <= LDA_C2;
548
        Cache_Ctrl           <= CACHE_OPER2;
549
 
550
      when LDA_C2 =>
551
        CPU_Next_State       <= LDA_C3;
552
 
553
      when LDA_C3 =>
554
        CPU_Next_State       <= LDA_C4;
555 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
556 169 jshamlet
 
557
      when LDA_C4 =>
558
        CPU_Next_State       <= LDI_C1;
559
        Cache_Ctrl           <= CACHE_OPER1;
560 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
561 169 jshamlet
 
562
      when LDI_C1 =>
563
        CPU_Next_State       <= INSTR_DECODE;
564
        Cache_Ctrl           <= CACHE_INSTR;
565 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
566 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
567
        ALU_Ctrl.Reg         <= SubOp;
568
 
569
      when LDO_C1 =>
570 181 jshamlet
        CPU_Next_State       <= LDX_C2;
571 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
572 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
573
          ALU_Ctrl.Oper      <= ALU_UPP;
574
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
575 169 jshamlet
        end if;
576
 
577
      when LDX_C1 =>
578
        CPU_Next_State       <= LDX_C2;
579 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
580
          ALU_Ctrl.Oper      <= ALU_UPP;
581
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
582 181 jshamlet
        end if;
583 169 jshamlet
 
584
      when LDX_C2 =>
585
        CPU_Next_State       <= LDX_C3;
586 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
587 181 jshamlet
 
588
      when LDX_C3 =>
589
        CPU_Next_State       <= LDX_C4;
590 182 jshamlet
        Cache_Ctrl           <= CACHE_OPER1;
591 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
592 169 jshamlet
 
593 181 jshamlet
      when LDX_C4 =>
594 169 jshamlet
        CPU_Next_State       <= INSTR_DECODE;
595
        Cache_Ctrl           <= CACHE_INSTR;
596 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
597 181 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
598 169 jshamlet
        ALU_Ctrl.Reg         <= ACCUM;
599
 
600
-------------------------------------------------------------------------------
601
-- Data Storage - Store to memory (STA, STO, STX)
602
-------------------------------------------------------------------------------
603
      when STA_C1 =>
604
        CPU_Next_State       <= STA_C2;
605
        Cache_Ctrl           <= CACHE_OPER2;
606
        DP_Ctrl.Src          <= DATA_WR_REG;
607
        DP_Ctrl.Reg          <= SubOp;
608
 
609
      when STA_C2 =>
610
        CPU_Next_State       <= STA_C3;
611 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
612 169 jshamlet
 
613
      when STA_C3 =>
614
        CPU_Next_State       <= PIPE_FILL_2;
615
        Cache_Ctrl           <= CACHE_PREFETCH;
616 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
617 169 jshamlet
 
618
      when STO_C1 =>
619 182 jshamlet
        CPU_Next_State       <= PIPE_FILL_0;
620 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
621 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
622 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
623
          CPU_Next_State     <= STO_C2;
624
          ALU_Ctrl.Oper      <= ALU_UPP;
625
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
626 169 jshamlet
        end if;
627
 
628
      when STO_C2 =>
629
        CPU_Next_State       <= PIPE_FILL_1;
630 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
631 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
632
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
633
 
634
      when STX_C1 =>
635 182 jshamlet
        CPU_Next_State       <= PIPE_FILL_1;
636 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
637 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
638
          CPU_Next_State     <= STX_C2;
639
          ALU_Ctrl.Oper      <= ALU_UPP;
640
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
641 169 jshamlet
        end if;
642
 
643
      when STX_C2 =>
644
        CPU_Next_State       <= PIPE_FILL_2;
645 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
646 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
647
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
648
 
649
-------------------------------------------------------------------------------
650
-- Multi-Cycle Math Operations (UPP, MUL)
651
-------------------------------------------------------------------------------
652
 
653
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
654 181 jshamlet
      --  instruction/first operand, we have to return through PF2. Also, we
655
      --  need to tell the ALU to store the results to R1:R0 here. Note that
656
      --  there is no ALU_Ctrl.Reg, as this is implied in the ALU instruction
657 169 jshamlet
      when MUL_C1 =>
658
        CPU_Next_State       <= PIPE_FILL_2;
659 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
660 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_MUL;
661
 
662
      when UPP_C1 =>
663
        CPU_Next_State       <= PIPE_FILL_2;
664 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
665 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
666
        ALU_Ctrl.Reg         <= SubOp_p1;
667
 
668
-------------------------------------------------------------------------------
669
-- Basic Stack Manipulation (PSH, POP, RSP)
670
-------------------------------------------------------------------------------
671
      when PSH_C1 =>
672
        CPU_Next_State       <= PIPE_FILL_1;
673
        SP_Ctrl.Oper         <= SP_PUSH;
674
 
675
      when POP_C1 =>
676
        CPU_Next_State       <= POP_C2;
677
 
678
      when POP_C2 =>
679
        CPU_Next_State       <= POP_C3;
680 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
681 169 jshamlet
 
682
      when POP_C3 =>
683
        CPU_Next_State       <= POP_C4;
684
        Cache_Ctrl           <= CACHE_OPER1;
685 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
686 169 jshamlet
 
687
      when POP_C4 =>
688
        CPU_Next_State       <= INSTR_DECODE;
689
        Cache_Ctrl           <= CACHE_INSTR;
690 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
691 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_POP;
692
        ALU_Ctrl.Reg         <= SubOp;
693 172 jshamlet
 
694 169 jshamlet
-------------------------------------------------------------------------------
695
-- Subroutines & Interrupts (RTS, JSR)
696
-------------------------------------------------------------------------------
697
      when WAIT_FOR_INT => -- For soft interrupts only, halt the Program_Ctr
698 182 jshamlet
        CPU_Next_State       <= WAIT_FOR_INT;
699 169 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
700
 
701
      when ISR_C1 =>
702
        CPU_Next_State       <= ISR_C2;
703
        INT_Ctrl.Incr_ISR    <= '1';
704
 
705
      when ISR_C2 =>
706
        CPU_Next_State       <= ISR_C3;
707
        DP_Ctrl.Src          <= DATA_WR_FLAG;
708
 
709
      when ISR_C3 =>
710
        CPU_Next_State       <= JSR_C1;
711
        Cache_Ctrl           <= CACHE_OPER1;
712 182 jshamlet
        ALU_Ctrl.Oper        <= ALU_STP;
713 185 jshamlet
        ALU_Ctrl.Reg         <= conv_std_logic_vector(PSR_I,3);
714 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
715
        DP_Ctrl.Src          <= DATA_WR_PC;
716 182 jshamlet
        DP_Ctrl.Reg          <= PC_MSB;
717 169 jshamlet
        Ack_D                <= '1';
718
 
719
      when JSR_C1 =>
720
        CPU_Next_State       <= JSR_C2;
721
        Cache_Ctrl           <= CACHE_OPER2;
722
        SP_Ctrl.Oper         <= SP_PUSH;
723
        DP_Ctrl.Src          <= DATA_WR_PC;
724 182 jshamlet
        DP_Ctrl.Reg          <= PC_LSB;
725 169 jshamlet
 
726
      when JSR_C2 =>
727
        CPU_Next_State       <= PIPE_FILL_0;
728
        PC_Ctrl.Oper         <= PC_LOAD;
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 185 jshamlet
        -- if this is an RTI, then we need to clear the I bit
754 169 jshamlet
        if( SubOp = SOP_RTI )then
755
          CPU_Next_State     <= RTI_C6;
756
          Cache_Ctrl         <= CACHE_OPER1;
757 185 jshamlet
          ALU_Ctrl.Oper      <= ALU_CLP;
758
          ALU_Ctrl.Reg       <= conv_std_logic_vector(PSR_I,3);
759 169 jshamlet
        end if;
760
 
761
      when RTI_C6 =>
762
        CPU_Next_State       <= PIPE_FILL_1;
763 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
764 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_RFLG;
765
 
766
-------------------------------------------------------------------------------
767
-- Debugging (BRK) Performs a 5-clock NOP
768
-------------------------------------------------------------------------------
769
      when BRK_C1 =>
770
        CPU_Next_State       <= PIPE_FILL_0;
771
 
772
      when others =>
773
        null;
774
    end case;
775
 
776
    -- Interrupt service routines can only begin during the decode and wait
777
    --  states to avoid corruption due to incomplete instruction execution
778
    if( Int_Req = '1' )then
779
      if( CPU_State = INSTR_DECODE or CPU_State = WAIT_FOR_INT )then
780 182 jshamlet
        CPU_Next_State       <= ISR_C1;
781
        Cache_Ctrl           <= CACHE_IDLE;
782
        -- Rewind the PC by 3 to compensate for the pipeline registers
783 185 jshamlet
        PC_Ctrl.Offset       <= PC_REV3;
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_INCR =>
886
          Program_Ctr        <= Program_Ctr + Offset_SX - 2;
887
 
888
        when PC_LOAD =>
889 185 jshamlet
          Program_Ctr        <= Operand2 & Operand1;
890 169 jshamlet
 
891
        when others =>
892
          null;
893
      end case;
894
 
895
-------------------------------------------------------------------------------
896
-- (Write) Data Path
897
-------------------------------------------------------------------------------
898
      case DP_Ctrl.Src is
899
        when DATA_BUS_IDLE =>
900
          null;
901
 
902
        when DATA_RD_MEM =>
903
          Rd_Enable          <= '1';
904
 
905
        when DATA_WR_REG =>
906
          Wr_Enable          <= '1';
907
          Wr_Data            <= Regfile(conv_integer(DP_Ctrl.Reg));
908
 
909
        when DATA_WR_FLAG =>
910
          Wr_Enable          <= '1';
911
          Wr_Data            <= Flags;
912
 
913
        when DATA_WR_PC =>
914
          Wr_Enable          <= '1';
915
          Wr_Data            <= Program_Ctr(15 downto 8);
916 182 jshamlet
          if( DP_Ctrl.Reg = PC_LSB )then
917 169 jshamlet
            Wr_Data          <= Program_Ctr(7 downto 0);
918
          end if;
919
 
920
        when others =>
921
          null;
922
      end case;
923
 
924
-------------------------------------------------------------------------------
925
-- Stack Pointer
926
-------------------------------------------------------------------------------
927
      case SP_Ctrl.Oper is
928
        when SP_IDLE =>
929
          null;
930
 
931 181 jshamlet
        when SP_CLR =>
932 169 jshamlet
          Stack_Ptr          <= Stack_Start_Addr;
933
 
934 181 jshamlet
        when SP_SET =>
935
          Stack_Ptr          <= Regfile(1) & Regfile(0);
936
 
937 169 jshamlet
        when SP_POP  =>
938
          Stack_Ptr          <= Stack_Ptr + 1;
939
 
940
        when SP_PUSH =>
941
          Stack_Ptr          <= Stack_Ptr - 1;
942
 
943
        when others =>
944
          null;
945
 
946
      end case;
947
 
948
-------------------------------------------------------------------------------
949
-- Interrupt Controller
950
-------------------------------------------------------------------------------
951
      -- The interrupt control mask is always sourced out of R0
952
      if( INT_Ctrl.Mask_Set = '1' )then
953
        if( Enable_NMI )then
954
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
955
        else
956
          Int_Mask           <= Regfile(conv_integer(ACCUM));
957
        end if;
958
      end if;
959
 
960
      -- Combine external and internal interrupts, and mask the OR of the two
961
      --  with the mask. Record any incoming interrupts to the pending buffer
962
      i_Ints                 := (Interrupts or INT_Ctrl.Soft_Ints) and
963
                                Int_Mask;
964 172 jshamlet
 
965 169 jshamlet
      Pending                <= i_Ints or Pending;
966
 
967
      if( Wait_for_FSM = '0' )then
968
        if(    Pending(0) = '1' )then
969
          ISR_Addr           <= INT_VECTOR_0;
970
          Pending(0)         <= '0';
971
        elsif( Pending(1) = '1' )then
972
          ISR_Addr           <= INT_VECTOR_1;
973
          Pending(1)         <= '0';
974
        elsif( Pending(2) = '1' )then
975
          ISR_Addr           <= INT_VECTOR_2;
976
          Pending(2)         <= '0';
977
        elsif( Pending(3) = '1' )then
978
          ISR_Addr           <= INT_VECTOR_3;
979
          Pending(3)         <= '0';
980
        elsif( Pending(4) = '1' )then
981
          ISR_Addr           <= INT_VECTOR_4;
982
          Pending(4)         <= '0';
983
        elsif( Pending(5) = '1' )then
984
          ISR_Addr           <= INT_VECTOR_5;
985
          Pending(5)         <= '0';
986
        elsif( Pending(6) = '1' )then
987
          ISR_Addr           <= INT_VECTOR_6;
988
          Pending(6)         <= '0';
989
        elsif( Pending(7) = '1' )then
990
          ISR_Addr           <= INT_VECTOR_7;
991
          Pending(7)         <= '0';
992
        end if;
993 185 jshamlet
        Wait_for_FSM         <= or_reduce(Pending);
994 169 jshamlet
      end if;
995
 
996
      -- Reset the Wait_for_FSM flag on Int_Ack
997
      Ack_Q                  <= Ack_D;
998
      Ack_Q1                 <= Ack_Q;
999
      Int_Ack                <= Ack_Q1;
1000
      if( Int_Ack = '1' )then
1001
        Wait_for_FSM         <= '0';
1002
      end if;
1003
 
1004
      Int_Req                <= Wait_for_FSM and (not Int_Ack);
1005
 
1006
      -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1007
      --  lower half of the address.
1008
      if( INT_Ctrl.Incr_ISR = '1' )then
1009
        ISR_Addr             <= ISR_Addr + 1;
1010
      end if;
1011
 
1012
-------------------------------------------------------------------------------
1013
-- ALU (Arithmetic / Logic Unit)
1014
-------------------------------------------------------------------------------
1015
      Index                  := conv_integer(ALU_Ctrl.Reg);
1016
      Sum                    := (others => '0');
1017
      Temp                   := (others => '0');
1018
 
1019
      case ALU_Ctrl.Oper is
1020
        when ALU_INC => -- Rn = Rn + 1 : Flags N,C,Z
1021
          Sum                := ("0" & x"01") +
1022
                                ("0" & Regfile(Index));
1023 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1024
          Flags(PSR_C)       <= Sum(8);
1025
          Flags(PSR_N)      <= Sum(7);
1026 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1027
 
1028
        when ALU_UPP => -- Rn = Rn + 1
1029
          Sum                := ("0" & x"01") +
1030
                                ("0" & Regfile(Index));
1031 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1032 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1033
 
1034
        when ALU_UPP2 => -- Rn = Rn + C
1035
          Sum                := ("0" & x"00") +
1036
                                ("0" & Regfile(Index)) +
1037 185 jshamlet
                                Flags(PSR_C);
1038
          Flags(PSR_C)       <= Sum(8);
1039 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1040
 
1041
        when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1042
          Sum                := ("0" & Regfile(0)) +
1043
                                ("0" & Regfile(Index)) +
1044 185 jshamlet
                                Flags(PSR_C);
1045
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1046
          Flags(PSR_C)       <= Sum(8);
1047
          Flags(PSR_N)       <= Sum(7);
1048 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1049
 
1050
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1051
          Temp               := "0" & Regfile(Index);
1052 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1053
          Flags(PSR_N)       <= Temp(7);
1054 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1055
 
1056
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1057
          Temp(7 downto 0)   := Regfile(0) or Regfile(Index);
1058 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1059
          Flags(PSR_N)       <= Temp(7);
1060 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1061
 
1062
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1063
          Temp(7 downto 0)   := Regfile(0) and Regfile(Index);
1064 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1065
          Flags(PSR_N)       <= Temp(7);
1066 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1067
 
1068
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1069
          Temp(7 downto 0)   := Regfile(0) xor Regfile(Index);
1070 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1071
          Flags(PSR_N)       <= Temp(7);
1072 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1073
 
1074
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1075 185 jshamlet
          Temp               := Regfile(Index) & Flags(PSR_C);
1076
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1077
          Flags(PSR_C)       <= Temp(8);
1078
          Flags(PSR_N)       <= Temp(7);
1079 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1080
 
1081
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1082 185 jshamlet
          Temp               := Regfile(Index)(0) & Flags(PSR_C) &
1083 169 jshamlet
                                Regfile(Index)(7 downto 1);
1084 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1085
          Flags(PSR_C)       <= Temp(8);
1086
          Flags(PSR_N)       <= Temp(7);
1087 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1088
 
1089
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1090
          Sum                := ("0" & Regfile(Index)) +
1091
                                ("0" & x"FF");
1092 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1093
          Flags(PSR_C)       <= Sum(8);
1094
          Flags(PSR_N)       <= Sum(7);
1095 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1096
 
1097
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1098
          Sum                := ("0" & Regfile(0)) +
1099
                                ("1" & (not Regfile(Index))) +
1100 185 jshamlet
                                Flags(PSR_C);
1101
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1102
          Flags(PSR_C)       <= Sum(8);
1103
          Flags(PSR_N)       <= Sum(7);
1104 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1105
 
1106
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1107
          Sum                := ("0" & Regfile(0)) +
1108
                                ("0" & Regfile(Index));
1109 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1110 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1111 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1112
          Flags(PSR_N)       <= Sum(7);
1113 169 jshamlet
 
1114
        when ALU_STP => -- Sets bit(n) in the Flags register
1115
          Flags(Index)       <= '1';
1116
 
1117
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1118 185 jshamlet
          Flags(PSR_Z)       <= not Regfile(0)(Index);
1119
          Flags(PSR_N)       <= Regfile(0)(7);
1120 169 jshamlet
 
1121
        when ALU_CLP => -- Clears bit(n) in the Flags register
1122
          Flags(Index)       <= '0';
1123
 
1124
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1125
          Temp               := "0" & Regfile(0);
1126 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1127
          Flags(PSR_N)       <= Temp(7);
1128 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1129
 
1130
        when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1131
          Sum                := ("0" & Regfile(0)) +
1132
                                ("1" & (not Regfile(Index))) +
1133
                                '1';
1134 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1135
          Flags(PSR_C)       <= Sum(8);
1136
          Flags(PSR_N)       <= Sum(7);
1137 169 jshamlet
 
1138
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1139
          Regfile(0)         <= Mult(7 downto 0);
1140
          Regfile(1)         <= Mult(15 downto 8);
1141 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Mult);
1142 169 jshamlet
 
1143
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1144 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Operand1);
1145
          Flags(PSR_N)       <= Operand1(7);
1146
          Regfile(Index)     <= Operand1;
1147 169 jshamlet
 
1148
        when ALU_POP => -- Rn <= Data
1149 185 jshamlet
          Regfile(Index)     <= Operand1;
1150 169 jshamlet
 
1151
        when ALU_RFLG =>
1152 185 jshamlet
          Flags              <= Operand1;
1153 169 jshamlet
 
1154 185 jshamlet
        when ALU_RSP =>
1155 181 jshamlet
          Regfile(0)         <= Stack_Ptr(7 downto 0);
1156
          Regfile(1)         <= Stack_Ptr(15 downto 8);
1157
 
1158 185 jshamlet
        when ALU_GMSK =>
1159
          Flags(PSR_Z)       <= nor_reduce(Int_Mask);
1160
          Regfile(0)         <= Int_Mask;
1161
 
1162 169 jshamlet
        when others =>
1163
          null;
1164
      end case;
1165
 
1166
    end if;
1167
  end process;
1168
 
1169 182 jshamlet
-------------------------------------------------------------------------------
1170
-- Multiplier Logic
1171
--
1172
-- We need to infer a hardware multipler, so we create a special clocked
1173
--  process with no reset or clock enable
1174
-------------------------------------------------------------------------------
1175
 
1176
  Multiplier_proc: process( Clock )
1177
  begin
1178
    if( rising_edge(Clock) )then
1179
      Mult                   <= Regfile(0) *
1180 185 jshamlet
 

powered by: WebSVN 2.1.0

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