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 168

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

Line No. Rev Author Line
1 167 jshamlet
-- Copyright (c)2006,2013 Jeremy Seth Henry
2 151 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 167 jshamlet
--
24 151 jshamlet
-- VHDL Units :  Open8_CPU
25 167 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 151 jshamlet
-- Notes      :  Generic definitions
29 167 jshamlet
--            :
30
--            :  Program_Start_Addr - Determines the initial value of the
31
--            :   program counter.
32
--            :
33
--            :  ISR_Start_Addr - determines the location of the interrupt
34
--            :   service vector table. There are 8 service vectors, or 16
35
--            :   bytes, which must be allocated to either ROM or RAM.
36
--            :
37 151 jshamlet
--            :  Stack_Start_Address - determines the initial (reset) value of
38
--            :   the stack pointer. Also used for the RSP instruction if
39
--            :   Allow_Stack_Address_Move is 0.
40
--            :
41
--            :  Allow_Stack_Address_Move - When set to 1, allows the RSP to be
42
--            :   programmed via thet RSP instruction. If enabled, the contents
43
--            :   of R1:R0 are used to initialize the stack pointer.
44
--            :
45 167 jshamlet
--            :  The Enable_Auto_Increment generic can be used to modify the
46
--            :   indexed instructions such that specifying an odd register
47
--            :   will use the next lower register pair, post-incrementing the
48
--            :   value in that pair. IOW, specifying STX R1 will instead
49
--            :   result in STX R0++, or R0 = {R1:R0}; {R1:R0} + 1
50 151 jshamlet
--            :
51 167 jshamlet
--            :  BRK_Implements_WAI modifies the BRK instruction such that it
52
--            :   triggers the wait for interrupt state, but without triggering
53
--            :   a soft interrupt in lieu of its normal behavior, which is to
54
--            :   insert several dead clock cycles - essentially a long NOP
55 151 jshamlet
--            :
56 167 jshamlet
--            :  Enable_NMI overrides the mask bit for interrupt 0, creating a
57
--            :   non-maskable interrupt at the highest priority.
58
--            :
59 151 jshamlet
--            :  Default_Interrupt_Mask - Determines the intial value of the
60
--            :   interrupt mask. To remain true to the original core, which
61
--            :   had no interrupt mask, this should be set to x"FF". Otherwise
62 167 jshamlet
--            :   it can be initialized to any value. Enable_NMI will logically
63
--            :   force the LSB high.
64
--            : 
65
--            :  Reset_Level - Determines whether the processor registers reset
66
--            :   on a high or low level from higher logic.
67 151 jshamlet
--            :
68 167 jshamlet
--            : Architecture notes
69
--            :  This model deviates from the original ISA in a few important
70
--            :   ways.
71 151 jshamlet
--            :
72 167 jshamlet
--            :  First, there is only one set of registers. Interrupt service
73
--            :   routines must explicitely preserve context since the the
74
--            :   hardware doesn't. This was done to decrease size and code
75
--            :   complexity. Older code that assumes this behavior will not
76
--            :   execute correctly on this processor model.
77 151 jshamlet
--            :
78 167 jshamlet
--            :  Second, this model adds an additional pipeline stage between
79
--            :   the instruction decoder and the ALU. Unfortunately, this
80
--            :   means that the instruction stream has to be restarted after
81
--            :   any math instruction is executed, implying that any ALU
82
--            :   instruction now has a latency of 2 instead of 0. The
83
--            :   advantage is that the maximum frequency has gone up
84
--            :   significantly, as the ALU code is vastly more efficient.
85
--            :   As an aside, this now means that all math instructions,
86
--            :   including MUL (see below) and UPP have the same instruction
87
--            :   latency.
88 151 jshamlet
--            :
89 167 jshamlet
--            :  Third, the original ISA, also a soft core, had two reserved
90
--            :   instructions, USR and USR2. These have been implemented as
91
--            :   DBNZ, and MUL respectively.
92 151 jshamlet
--            :
93 167 jshamlet
--            :  DBNZ decrements the specified register and branches if the
94
--            :   result is non-zero. The instruction effectively executes a
95
--            :   DEC Rn instruction prior to branching, so the same flags will
96
--            :   be set.
97
--            :
98
--            :  MUL places the result of R0 * Rn into R1:R0. Instruction
99
--            :   latency is identical to other ALU instructions. Only the Z
100
--            :   flag is set, since there is no defined overflow or "negative
101
--            :   16-bit values"
102
--            :
103
--            :  Fourth, indexed load/store instructions now have an (optional)
104
--            :   ability to post-increment their index registers. If enabled,
105
--            :   using an odd operand for LDO,LDX, STO, STX will cause the
106
--            :   register pair to be incremented after the storage access.
107
--            :
108
--            :  Fifth, the RSP instruction has been (optionally) altered to
109
--            :   allow the stack pointer to be sourced from R1:R0.
110
--            :
111
--            :  Sixth, the BRK instruction can optionally implement a WAI,
112
--            :   which is the same as the INT instruction without the soft
113
--            :   interrupt, as a way to put the processor to "sleep" until the
114
--            :   next interrupt.
115
--            :
116
--            :  Seventh, the original CPU model had 8 non-maskable interrupts
117
--            :   with priority. This model has the same 8 interrupts, but
118
--            :   allows software to mask them (with an additional option to 
119
--            :   override the highest priority interrupt, making it the NMI.)
120
--            :   The interrupt code will retain memory of lower priority
121
--            :   interrupts, and execute them as it can.
122
--            :
123
--            :  Lastly, previous unmapped instructions in the OP_STK opcode
124
--            :   were repurposed to support a new interrupt mask.
125
--            :   SMSK and GMSK transfer the contents of R0 (accumulator)
126
--            :   to/from the interrupt mask register. SMSK is immediate, while
127
--            :   GMSK has the same overhead as a math instruction.
128 151 jshamlet
 
129
library ieee;
130
  use ieee.std_logic_1164.all;
131
  use ieee.std_logic_unsigned.all;
132 167 jshamlet
  use ieee.std_logic_misc.all;
133 151 jshamlet
 
134
library work;
135
use work.Open8_pkg.all;
136
 
137
entity Open8_CPU is
138
  generic(
139 167 jshamlet
    Program_Start_Addr       : ADDRESS_TYPE := x"0090"; -- Initial PC location
140
    ISR_Start_Addr           : ADDRESS_TYPE := x"0080"; -- Bottom of ISR vec's
141 151 jshamlet
    Stack_Start_Addr         : ADDRESS_TYPE := x"007F"; -- Top of Stack
142
    Allow_Stack_Address_Move : boolean      := false;   -- Use Normal v8 RSP
143 167 jshamlet
    Enable_Auto_Increment    : boolean      := false;   -- Modify indexed instr
144 162 jshamlet
    BRK_Implements_WAI       : boolean      := false;   -- BRK -> Wait for Int
145 167 jshamlet
    Enable_NMI               : boolean      := true;    -- force mask for int 0
146 151 jshamlet
    Default_Interrupt_Mask   : DATA_TYPE    := x"FF";   -- Enable all Ints
147
    Reset_Level              : std_logic    := '0' );   -- Active reset level
148
  port(
149
    Clock                    : in  std_logic;
150
    Reset                    : in  std_logic;
151
    Interrupts               : in  INTERRUPT_BUNDLE;
152
    --
153
    Address                  : out ADDRESS_TYPE;
154
    Rd_Data                  : in  DATA_TYPE;
155
    Rd_Enable                : out std_logic;
156
    Wr_Data                  : out DATA_TYPE;
157
    Wr_Enable                : out std_logic );
158
end entity;
159
 
160 156 jshamlet
architecture behave of Open8_CPU is
161
 
162 151 jshamlet
  subtype OPCODE_TYPE  is std_logic_vector(4 downto 0);
163
  subtype SUBOP_TYPE   is std_logic_vector(2 downto 0);
164
 
165 167 jshamlet
  -- All opcodes should be identical to the opcode used by the assembler
166
  -- In this case, they match the original V8/ARC uRISC ISA
167 151 jshamlet
  constant OP_INC            : OPCODE_TYPE := "00000";
168
  constant OP_ADC            : OPCODE_TYPE := "00001";
169
  constant OP_TX0            : OPCODE_TYPE := "00010";
170
  constant OP_OR             : OPCODE_TYPE := "00011";
171
  constant OP_AND            : OPCODE_TYPE := "00100";
172
  constant OP_XOR            : OPCODE_TYPE := "00101";
173
  constant OP_ROL            : OPCODE_TYPE := "00110";
174
  constant OP_ROR            : OPCODE_TYPE := "00111";
175
  constant OP_DEC            : OPCODE_TYPE := "01000";
176
  constant OP_SBC            : OPCODE_TYPE := "01001";
177
  constant OP_ADD            : OPCODE_TYPE := "01010";
178
  constant OP_STP            : OPCODE_TYPE := "01011";
179
  constant OP_BTT            : OPCODE_TYPE := "01100";
180
  constant OP_CLP            : OPCODE_TYPE := "01101";
181
  constant OP_T0X            : OPCODE_TYPE := "01110";
182
  constant OP_CMP            : OPCODE_TYPE := "01111";
183
  constant OP_PSH            : OPCODE_TYPE := "10000";
184
  constant OP_POP            : OPCODE_TYPE := "10001";
185
  constant OP_BR0            : OPCODE_TYPE := "10010";
186
  constant OP_BR1            : OPCODE_TYPE := "10011";
187 167 jshamlet
  constant OP_DBNZ           : OPCODE_TYPE := "10100";
188 151 jshamlet
  constant OP_INT            : OPCODE_TYPE := "10101";
189 167 jshamlet
  constant OP_MUL            : OPCODE_TYPE := "10110";
190 151 jshamlet
  constant OP_STK            : OPCODE_TYPE := "10111";
191
  constant OP_UPP            : OPCODE_TYPE := "11000";
192
  constant OP_STA            : OPCODE_TYPE := "11001";
193
  constant OP_STX            : OPCODE_TYPE := "11010";
194
  constant OP_STO            : OPCODE_TYPE := "11011";
195
  constant OP_LDI            : OPCODE_TYPE := "11100";
196
  constant OP_LDA            : OPCODE_TYPE := "11101";
197
  constant OP_LDX            : OPCODE_TYPE := "11110";
198
  constant OP_LDO            : OPCODE_TYPE := "11111";
199
 
200 167 jshamlet
  -- OP_STK uses the lower 3 bits to further refine the instruction by
201
  --  repurposing the source register field. These "sub opcodes" are
202
  --  take the place of the register select for the OP_STK opcode
203 151 jshamlet
  constant SOP_RSP           : SUBOP_TYPE := "000";
204
  constant SOP_RTS           : SUBOP_TYPE := "001";
205
  constant SOP_RTI           : SUBOP_TYPE := "010";
206
  constant SOP_BRK           : SUBOP_TYPE := "011";
207
  constant SOP_JMP           : SUBOP_TYPE := "100";
208
  constant SOP_SMSK          : SUBOP_TYPE := "101";
209
  constant SOP_GMSK          : SUBOP_TYPE := "110";
210
  constant SOP_JSR           : SUBOP_TYPE := "111";
211
 
212
  type CPU_STATES is (
213
      -- Instruction fetch & Decode
214
    PIPE_FILL_0, PIPE_FILL_1, PIPE_FILL_2, INSTR_DECODE,
215
    -- Branching
216 167 jshamlet
    BRN_C1, DBNZ_C1, DBNZ_C2, JMP_C1, JMP_C2,
217 151 jshamlet
    -- Loads
218 167 jshamlet
    LDA_C1, LDA_C2, LDA_C3, LDA_C4, LDI_C1,
219
    LDO_C1, LDX_C1, LDX_C2, LDX_C3, LDX_C4,
220 151 jshamlet
    -- Stores
221 167 jshamlet
    STA_C1, STA_C2, STO_C1, STO_C2, STX_C1, STX_C2,
222
    -- math
223
    MATH_C1, GMSK_C1, MUL_C1, UPP_C1,
224 151 jshamlet
    -- Stack
225
    PSH_C1, POP_C1, POP_C2, POP_C3, POP_C4,
226
    -- Subroutines & Interrupts
227
    WAIT_FOR_INT, ISR_C1, ISR_C2, ISR_C3, JSR_C1, JSR_C2,
228
    RTS_C1, RTS_C2, RTS_C3, RTS_C4, RTS_C5, RTI_C6,
229
    -- Debugging
230
    BRK_C1 );
231
 
232 167 jshamlet
  -- To simplify the logic, the first 16 of these should exactly match their
233
  --  corresponding Opcodes. This allows the state logic to simply pass the 
234
  --  opcode field to the ALU for most math operations.
235 156 jshamlet
  constant ALU_INC           : OPCODE_TYPE := "00000"; -- x"00"
236 167 jshamlet
  constant ALU_UPP1          : OPCODE_TYPE := "00000"; -- Alias of ALU_INC
237 156 jshamlet
  constant ALU_ADC           : OPCODE_TYPE := "00001"; -- x"01"
238
  constant ALU_TX0           : OPCODE_TYPE := "00010"; -- x"02"
239
  constant ALU_OR            : OPCODE_TYPE := "00011"; -- x"03"
240
  constant ALU_AND           : OPCODE_TYPE := "00100"; -- x"04"
241
  constant ALU_XOR           : OPCODE_TYPE := "00101"; -- x"05"
242
  constant ALU_ROL           : OPCODE_TYPE := "00110"; -- x"06"
243
  constant ALU_ROR           : OPCODE_TYPE := "00111"; -- x"07"
244
  constant ALU_DEC           : OPCODE_TYPE := "01000"; -- x"08"
245
  constant ALU_SBC           : OPCODE_TYPE := "01001"; -- x"09"
246
  constant ALU_ADD           : OPCODE_TYPE := "01010"; -- x"0A"
247
  constant ALU_STP           : OPCODE_TYPE := "01011"; -- x"0B"
248
  constant ALU_BTT           : OPCODE_TYPE := "01100"; -- x"0C"
249
  constant ALU_CLP           : OPCODE_TYPE := "01101"; -- x"0D"
250
  constant ALU_T0X           : OPCODE_TYPE := "01110"; -- x"0E"
251
  constant ALU_CMP           : OPCODE_TYPE := "01111"; -- x"0F"
252 167 jshamlet
  constant ALU_IDLE          : OPCODE_TYPE := "10000"; -- x"10"
253
  constant ALU_UPP2          : OPCODE_TYPE := "10010"; -- x"11"
254
  constant ALU_RFLG          : OPCODE_TYPE := "10011"; -- x"12"
255 156 jshamlet
  constant ALU_MUL           : OPCODE_TYPE := "10110"; -- x"16"
256
  constant ALU_LDI           : OPCODE_TYPE := "11100"; -- x"1C"
257
 
258
  constant FL_ZERO           : integer := 0;
259
  constant FL_CARRY          : integer := 1;
260
  constant FL_NEG            : integer := 2;
261
  constant FL_INT_EN         : integer := 3;
262
  constant FL_GP1            : integer := 4;
263
  constant FL_GP2            : integer := 5;
264
  constant FL_GP3            : integer := 6;
265
  constant FL_GP4            : integer := 7;
266
 
267
  constant ACCUM             : SUBOP_TYPE := "000";
268
  constant INT_FLAG          : SUBOP_TYPE := "011";
269
 
270
  type REGFILE_TYPE is array (0 to 7) of DATA_TYPE;
271
  subtype FLAG_TYPE is DATA_TYPE;
272
 
273 167 jshamlet
  constant INT_VECTOR_0      : ADDRESS_TYPE := ISR_Start_Addr +  0;
274
  constant INT_VECTOR_1      : ADDRESS_TYPE := ISR_Start_Addr +  2;
275
  constant INT_VECTOR_2      : ADDRESS_TYPE := ISR_Start_Addr +  4;
276
  constant INT_VECTOR_3      : ADDRESS_TYPE := ISR_Start_Addr +  6;
277
  constant INT_VECTOR_4      : ADDRESS_TYPE := ISR_Start_Addr +  8;
278
  constant INT_VECTOR_5      : ADDRESS_TYPE := ISR_Start_Addr + 10;
279
  constant INT_VECTOR_6      : ADDRESS_TYPE := ISR_Start_Addr + 12;
280
  constant INT_VECTOR_7      : ADDRESS_TYPE := ISR_Start_Addr + 14;
281 156 jshamlet
 
282 167 jshamlet
  type CPU_CTRL_TYPE is record
283
    State                    : CPU_STATES;
284
    LS_Address               : ADDRESS_TYPE;
285
    Program_Ctr              : ADDRESS_TYPE;
286
    Stack_Ptr                : ADDRESS_TYPE;
287
    Opcode                   : OPCODE_TYPE;
288
    SubOp_p0                 : SUBOP_TYPE;
289
    SubOp_p1                 : SUBOP_TYPE;
290
    Cache_Valid              : std_logic;
291
    Prefetch                 : DATA_TYPE;
292
    Operand1                 : DATA_TYPE;
293
    Operand2                 : DATA_TYPE;
294
    AutoIncr                 : std_logic;
295
    A_Oper                   : OPCODE_TYPE;
296
    A_Reg                    : SUBOP_TYPE;
297
    A_Data                   : DATA_TYPE;
298
    A_NoFlags                : std_logic;
299
    M_Reg                    : SUBOP_TYPE;
300
    M_Prod                   : ADDRESS_TYPE;
301
    Regfile                  : REGFILE_TYPE;
302
    Flags                    : FLAG_TYPE;
303
    Int_Mask                 : DATA_TYPE;
304
    Int_Addr                 : ADDRESS_TYPE;
305
    Int_Pending              : DATA_TYPE;
306
    Int_Level                : integer range 0 to 7;
307
    Wait_for_FSM             : std_logic;
308
  end record;
309 151 jshamlet
 
310 167 jshamlet
  signal CPU                 : CPU_CTRL_TYPE;
311 151 jshamlet
 
312 167 jshamlet
  alias  Accumulator         is CPU.Regfile(0);
313
  alias  Flags               is CPU.Flags;
314 151 jshamlet
 
315 167 jshamlet
  signal Ack_Q, Ack_Q1       : std_logic;
316
  signal Int_Req, Int_Ack    : std_logic;
317 156 jshamlet
 
318 167 jshamlet
  type IC_MODES is ( CACHE_IDLE, CACHE_INSTR, CACHE_OPER1, CACHE_OPER2,
319
                     CACHE_PREFETCH, CACHE_PFFLUSH, CACHE_INVALIDATE );
320 151 jshamlet
 
321 167 jshamlet
  type PC_MODES is ( PC_INCR, PC_IDLE, PC_REV1, PC_REV2, PC_REV3,
322
                     PC_BRANCH, PC_LOAD );
323 156 jshamlet
 
324 167 jshamlet
  type SP_MODES is ( SP_IDLE, SP_RSET, SP_POP, SP_PUSH );
325 156 jshamlet
 
326 167 jshamlet
  type DP_MODES is ( DATA_BUS_IDLE, DATA_RD_MEM,
327 168 jshamlet
                     DATA_WR_REG, DATA_WR_FLAG,
328
                                                        DATA_WR_PC_LOWER, DATA_WR_PC_UPPER );
329 156 jshamlet
 
330 167 jshamlet
  type DP_CTRL_TYPE is record
331
    Src                      : DP_MODES;
332
    Reg                      : SUBOP_TYPE;
333
  end record;
334 151 jshamlet
 
335 167 jshamlet
  type INT_CTRL_TYPE is record
336
    Mask_Set                 : std_logic;
337
    Soft_Ints                : INTERRUPT_BUNDLE;
338
    Incr_ISR                 : std_logic;
339
  end record;
340 156 jshamlet
 
341 151 jshamlet
begin
342
 
343 167 jshamlet
  Address_Sel: process( CPU )
344
    variable Offset_SX       : ADDRESS_TYPE;
345
  begin
346
    Offset_SX(15 downto 8)   := (others => CPU.Operand1(7));
347
    Offset_SX(7 downto 0)    := CPU.Operand1;
348 151 jshamlet
 
349 167 jshamlet
    case( CPU.State )is
350
      when LDO_C1 | LDX_C1 | STO_C1 | STX_C1 =>
351
        Address              <= CPU.LS_Address + Offset_SX;
352
      when LDA_C2 | STA_C2 =>
353
        Address              <= (CPU.Operand2 & CPU.Operand1);
354
      when PSH_C1 | POP_C1 | ISR_C3 | JSR_C1 | JSR_C2 |
355
           RTS_C1 | RTS_C2 | RTS_C3 =>
356
        Address              <= CPU.Stack_Ptr;
357
      when ISR_C1 | ISR_C2 =>
358
        Address              <= CPU.Int_Addr;
359
      when others =>
360
        Address              <= CPU.Program_Ctr;
361
    end case;
362
  end process;
363 156 jshamlet
 
364 167 jshamlet
  CPU_Proc: process( Clock, Reset )
365
    variable IC              : IC_MODES;
366
    variable PC              : PC_MODES;
367
    variable SP              : SP_MODES;
368
    variable DP              : DP_CTRL_TYPE;
369
    variable INT             : INT_CTRL_TYPE;
370
    variable RegSel          : integer range 0 to 7;
371
    variable Reg_l, Reg_u    : integer range 0 to 7;
372
    variable Ack_D           : std_logic;
373 151 jshamlet
    variable Offset_SX       : ADDRESS_TYPE;
374 167 jshamlet
    variable Index           : integer range 0 to 7;
375
    variable Temp            : std_logic_vector(8 downto 0);
376 151 jshamlet
  begin
377 167 jshamlet
    if( Reset = Reset_Level )then
378
      CPU.State              <= PIPE_FILL_0;
379
      CPU.LS_Address         <= (others => '0');
380
      CPU.Program_Ctr        <= Program_Start_Addr;
381
      CPU.Stack_Ptr          <= Stack_Start_Addr;
382
      CPU.Opcode             <= (others => '0');
383
      CPU.SubOp_p0           <= (others => '0');
384
      CPU.SubOp_p1           <= (others => '0');
385
      CPU.Prefetch           <= (others => '0');
386
      CPU.Operand1           <= (others => '0');
387
      CPU.Operand2           <= (others => '0');
388
      CPU.AutoIncr           <= '0';
389
      CPU.Cache_Valid        <= '0';
390
      CPU.A_Oper             <= ALU_IDLE;
391
      CPU.A_Reg              <= ACCUM;
392
      CPU.A_Data             <= x"00";
393
      CPU.A_NoFlags          <= '0';
394
      CPU.M_Reg              <= (others => '0');
395
      for i in 0 to 7 loop
396
        CPU.Regfile(i)       <= x"00";
397
      end loop;
398
      CPU.Flags              <= (others => '0');
399
      if( Enable_NMI )then
400
        CPU.Int_Mask         <= Default_Interrupt_Mask(7 downto 1) & '1';
401
      else
402
        CPU.Int_Mask         <= Default_Interrupt_Mask;
403
      end if;
404
      CPU.Int_Addr           <= (others => '0');
405
      CPU.Int_Pending        <= (others => '0');
406
      CPU.Int_Level          <= 7;
407
      CPU.Wait_for_FSM       <= '0';
408 151 jshamlet
 
409 167 jshamlet
      Ack_Q                  <= '0';
410
      Ack_Q1                 <= '0';
411
      Int_Ack                <= '0';
412
      Int_Req                <= '0';
413 151 jshamlet
 
414 167 jshamlet
      Wr_Data                <= x"00";
415
      Wr_Enable              <= '0';
416
      Rd_Enable              <= '1';
417
    elsif( rising_edge(Clock) )then
418 151 jshamlet
 
419 167 jshamlet
      IC                     := CACHE_IDLE;
420
      SP                     := SP_IDLE;
421
      DP.Src                 := DATA_RD_MEM;
422
      Ack_D                  := '0';
423
      INT.Mask_Set           := '0';
424
      INT.Soft_Ints          := x"00";
425
      INT.Incr_ISR           := '0';
426
      RegSel                 := conv_integer(CPU.SubOp_p0);
427 151 jshamlet
 
428 167 jshamlet
      if( Enable_Auto_Increment )then
429
        Reg_l                := conv_integer(CPU.SubOp_p0(2 downto 1) & '0');
430
        Reg_u                := conv_integer(CPU.SubOp_p0(2 downto 1) & '1');
431
      else
432
        Reg_l                := conv_integer(CPU.SubOp_p0);
433
        Reg_u                := conv_integer(CPU.SubOp_p1);
434
      end if;
435 151 jshamlet
 
436 167 jshamlet
      CPU.LS_Address         <= CPU.Regfile(Reg_u) & CPU.Regfile(Reg_l);
437 151 jshamlet
 
438 167 jshamlet
      CPU.AutoIncr           <= '0';
439
      if( Enable_Auto_Increment  )then
440
        CPU.AutoIncr         <= CPU.SubOp_p0(0);
441
      end if;
442 151 jshamlet
 
443 167 jshamlet
      CPU.A_Oper             <= ALU_IDLE;
444
      CPU.A_Reg              <= ACCUM;
445
      CPU.A_Data             <= x"00";
446
      CPU.A_NoFlags          <= '0';
447 151 jshamlet
 
448 167 jshamlet
      case( CPU.State )is
449
        when PIPE_FILL_0 =>
450
          PC                 := PC_INCR;
451
          CPU.State          <= PIPE_FILL_1;
452 151 jshamlet
 
453 167 jshamlet
        when PIPE_FILL_1 =>
454
          PC                 := PC_INCR;
455
          CPU.State          <= PIPE_FILL_2;
456 151 jshamlet
 
457 167 jshamlet
        when PIPE_FILL_2 =>
458
          IC                 := CACHE_INSTR;
459
          PC                 := PC_INCR;
460
          CPU.State          <= INSTR_DECODE;
461 151 jshamlet
 
462 167 jshamlet
-------------------------------------------------------------------------------
463
-- Instruction Decode and dispatch
464
-------------------------------------------------------------------------------
465 151 jshamlet
 
466 167 jshamlet
        when INSTR_DECODE =>
467
          IC                 := CACHE_INSTR;
468
          PC                 := PC_INCR;
469
          case CPU.Opcode is
470
            when OP_PSH =>
471
              IC             := CACHE_PREFETCH;
472
              PC             := PC_IDLE;
473
              DP.Src         := DATA_WR_REG;
474
              DP.Reg         := CPU.SubOp_p0;
475
              CPU.State      <= PSH_C1;
476 151 jshamlet
 
477 167 jshamlet
            when OP_POP =>
478
              IC             := CACHE_PREFETCH;
479
              PC             := PC_REV2;
480
              SP             := SP_POP;
481
              CPU.State      <= POP_C1;
482 151 jshamlet
 
483 167 jshamlet
            when OP_BR0 | OP_BR1 =>
484
              IC             := CACHE_OPER1;
485
              CPU.State      <= BRN_C1;
486 151 jshamlet
 
487 167 jshamlet
            when OP_DBNZ =>
488
              IC             := CACHE_OPER1;
489
              CPU.A_Oper     <= ALU_DEC;
490
              CPU.A_Reg      <= CPU.SubOp_p0;
491
              CPU.A_Data     <= CPU.Regfile(RegSel);
492
              CPU.State      <= DBNZ_C1;
493 151 jshamlet
 
494 167 jshamlet
            when OP_INT =>
495
              if( CPU.Int_Mask(RegSel) = '1' )then
496
                CPU.State    <= WAIT_FOR_INT;
497
                INT.Soft_Ints(RegSel) := '1';
498
              end if;
499 151 jshamlet
 
500 167 jshamlet
            when OP_STK =>
501
              case CPU.SubOp_p0 is
502
                when SOP_RSP  =>
503
                  SP         := SP_RSET;
504 151 jshamlet
 
505 167 jshamlet
                when SOP_RTS | SOP_RTI =>
506
                  IC         := CACHE_IDLE;
507
                  PC         := PC_IDLE;
508
                  SP         := SP_POP;
509
                  CPU.State  <= RTS_C1;
510 151 jshamlet
 
511 167 jshamlet
                when SOP_BRK  =>
512
                  if( BRK_Implements_WAI )then
513
                    CPU.State<= WAIT_FOR_INT;
514
                  else
515
                    PC       := PC_REV2;
516
                    CPU.State<= BRK_C1;
517
                  end if;
518 151 jshamlet
 
519 167 jshamlet
                when SOP_JMP  =>
520
                  IC         := CACHE_OPER1;
521
                  PC         := PC_IDLE;
522
                  CPU.State  <= JMP_C1;
523 151 jshamlet
 
524 167 jshamlet
                when SOP_SMSK =>
525
                  INT.Mask_Set := '1';
526 151 jshamlet
 
527 167 jshamlet
                when SOP_GMSK =>
528
                  IC         := CACHE_PREFETCH;
529
                  PC         := PC_REV1;
530
                  CPU.State  <= GMSK_C1;
531 151 jshamlet
 
532 167 jshamlet
                when SOP_JSR =>
533
                  IC         := CACHE_OPER1;
534
                  PC         := PC_IDLE;
535 168 jshamlet
                  DP.Src     := DATA_WR_PC_UPPER;
536 167 jshamlet
                  CPU.State  <= JSR_C1;
537 151 jshamlet
 
538 167 jshamlet
                when others => null;
539
              end case;
540 151 jshamlet
 
541 167 jshamlet
            when OP_MUL =>
542
              IC             := CACHE_PREFETCH;
543
              PC             := PC_REV1;
544
              CPU.M_Reg      <= CPU.SubOp_p0;
545
              CPU.State      <= MUL_C1;
546 151 jshamlet
 
547 167 jshamlet
            when OP_UPP =>
548
              IC             := CACHE_PREFETCH;
549
              PC             := PC_REV1;
550
              CPU.A_Oper     <= ALU_UPP1;
551
              CPU.A_NoFlags  <= '1';
552
              CPU.A_Reg      <= CPU.SubOp_p0;
553
              CPU.A_Data     <= CPU.Regfile(RegSel);
554
              CPU.State      <= UPP_C1;
555 151 jshamlet
 
556 167 jshamlet
            when OP_LDA =>
557
              IC             := CACHE_OPER1;
558
              PC             := PC_IDLE;
559
              CPU.State      <= LDA_C1;
560 151 jshamlet
 
561 167 jshamlet
            when OP_LDI =>
562
              IC             := CACHE_OPER1;
563
              PC             := PC_IDLE;
564
              CPU.State      <= LDI_C1;
565 151 jshamlet
 
566 167 jshamlet
            when OP_LDO =>
567
              IC             := CACHE_OPER1;
568
              PC             := PC_IDLE;
569
              CPU.State      <= LDO_C1;
570 151 jshamlet
 
571 167 jshamlet
            when OP_LDX =>
572
              IC             := CACHE_PFFLUSH;
573
              PC             := PC_REV1;
574
              CPU.State      <= LDX_C1;
575 151 jshamlet
 
576 167 jshamlet
            when OP_STA =>
577
              IC             := CACHE_OPER1;
578
              PC             := PC_IDLE;
579
              CPU.State      <= STA_C1;
580
 
581
            when OP_STO =>
582
              IC             := CACHE_OPER1;
583
              PC             := PC_REV2;
584
              DP.Src         := DATA_WR_REG;
585
              DP.Reg         := ACCUM;
586
              CPU.State      <= STO_C1;
587
 
588
            when OP_STX =>
589
              IC             := CACHE_PFFLUSH;
590
              PC             := PC_REV2;
591
              DP.Src         := DATA_WR_REG;
592
              DP.Reg         := ACCUM;
593
              CPU.State      <= STX_C1;
594
 
595
            when others =>
596
              IC             := CACHE_PREFETCH;
597
              PC             := PC_REV1;
598
              CPU.State      <= MATH_C1;
599
 
600
          end case;
601
 
602 151 jshamlet
-------------------------------------------------------------------------------
603
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
604
-------------------------------------------------------------------------------
605
 
606 167 jshamlet
        when BRN_C1 =>
607
          if( Flags(RegSel) = CPU.Opcode(0) )then
608
            IC               := CACHE_IDLE;
609
            PC               := PC_BRANCH;
610
            CPU.State        <= PIPE_FILL_0;
611
          else
612
            IC               := CACHE_INSTR;
613
            CPU.State        <= INSTR_DECODE;
614
          end if;
615 151 jshamlet
 
616 167 jshamlet
        when DBNZ_C1 =>
617
          IC                 := CACHE_PREFETCH;
618
          PC                 := PC_IDLE;
619
          CPU.State          <= DBNZ_C2;
620 151 jshamlet
 
621 167 jshamlet
        when DBNZ_C2 =>
622
          if( Flags(FL_ZERO) = '0' )then
623
            IC               := CACHE_INVALIDATE;
624
            PC               := PC_BRANCH;
625
            CPU.State        <= PIPE_FILL_0;
626
          else
627
            PC               := PC_REV1;
628
            CPU.State        <= PIPE_FILL_1;
629
          end if;
630 151 jshamlet
 
631 167 jshamlet
        when JMP_C1 =>
632
          IC                 := CACHE_OPER2;
633
          PC                 := PC_IDLE;
634
          CPU.State          <= JMP_C2;
635 151 jshamlet
 
636 167 jshamlet
        when JMP_C2 =>
637
          PC                 := PC_LOAD;
638
          CPU.State          <= PIPE_FILL_0;
639
 
640 151 jshamlet
-------------------------------------------------------------------------------
641
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
642
-------------------------------------------------------------------------------
643
 
644 167 jshamlet
        when LDA_C1 =>
645
          IC                 := CACHE_OPER2;
646
          PC                 := PC_IDLE;
647
          CPU.State          <= LDA_C2;
648 151 jshamlet
 
649 167 jshamlet
        when LDA_C2 =>
650
          PC                 := PC_IDLE;
651
          CPU.State          <= LDA_C3;
652 151 jshamlet
 
653 167 jshamlet
        when LDA_C3 =>
654
          PC                 := PC_IDLE;
655
          CPU.State          <= LDA_C4;
656 151 jshamlet
 
657 167 jshamlet
        when LDA_C4 =>
658
          IC                 := CACHE_OPER1;
659
          PC                 := PC_INCR;
660
          CPU.State          <= LDI_C1;
661 151 jshamlet
 
662 167 jshamlet
        when LDI_C1 =>
663
          IC                 := CACHE_PREFETCH;
664
          PC                 := PC_INCR;
665
          CPU.A_Oper         <= ALU_LDI;
666
          CPU.A_Reg          <= CPU.SubOp_p0;
667
          CPU.A_Data         <= CPU.Operand1;
668
          CPU.State          <= PIPE_FILL_2;
669 151 jshamlet
 
670 167 jshamlet
        when LDO_C1 =>
671
          IC                 := CACHE_PREFETCH;
672
          PC                 := PC_REV2;
673
          RegSel             := conv_integer(CPU.SubOp_p0(2 downto 1) & '0');
674
          if( Enable_Auto_Increment and CPU.AutoIncr = '1' )then
675
            CPU.A_Oper       <= ALU_UPP1;
676
            CPU.A_Reg        <= CPU.SubOp_p0(2 downto 1) & '0';
677
            CPU.A_NoFlags    <= '1';
678
            CPU.A_Data       <= CPU.RegFile(RegSel);
679 151 jshamlet
          end if;
680 167 jshamlet
          CPU.State          <= LDX_C2;
681 151 jshamlet
 
682 167 jshamlet
        when LDX_C1 =>
683
          PC                 := PC_REV2;
684
          RegSel             := conv_integer(CPU.SubOp_p0(2 downto 1) & '0');
685
          if( Enable_Auto_Increment and CPU.AutoIncr = '1' )then
686
            CPU.A_Oper       <= ALU_UPP1;
687
            CPU.A_Reg        <= CPU.SubOp_p0(2 downto 1) & '0';
688
            CPU.A_NoFlags    <= '1';
689
            CPU.A_Data       <= CPU.Regfile(RegSel);
690
          end if;
691
          CPU.State          <= LDX_C2;
692 151 jshamlet
 
693 167 jshamlet
        when LDX_C2 =>
694
          PC                 := PC_INCR;
695
          RegSel             := conv_integer(CPU.SubOp_p0(2 downto 1) & '1');
696
          if( Enable_Auto_Increment and CPU.AutoIncr = '1' )then
697
            CPU.A_Oper       <= ALU_UPP2;
698
            CPU.A_Reg        <= CPU.SubOp_p0(2 downto 1) & '1';
699
            CPU.A_Data       <= CPU.Regfile(RegSel);
700
          end if;
701
          CPU.State          <= LDX_C3;
702 151 jshamlet
 
703 167 jshamlet
        when LDX_C3 =>
704
          IC                 := CACHE_OPER1;
705
          PC                 := PC_INCR;
706
          CPU.State          <= LDX_C4;
707 151 jshamlet
 
708 167 jshamlet
        when LDX_C4 =>
709
          PC                 := PC_INCR;
710
          CPU.A_Oper         <= ALU_LDI;
711
          CPU.A_Reg          <= ACCUM;
712
          CPU.A_Data         <= CPU.Operand1;
713
          CPU.State          <= PIPE_FILL_2;
714
 
715 151 jshamlet
-------------------------------------------------------------------------------
716
-- Data Storage - Store to memory (STA, STO, STX)
717
-------------------------------------------------------------------------------
718 167 jshamlet
        when STA_C1 =>
719
          IC                 := CACHE_OPER2;
720
          PC                 := PC_IDLE;
721
          DP.Src             := DATA_WR_REG;
722
          DP.Reg             := CPU.SubOp_p0;
723
          CPU.State          <= STA_C2;
724 151 jshamlet
 
725 167 jshamlet
        when STA_C2 =>
726
          IC                 := CACHE_PREFETCH;
727
          PC                 := PC_INCR;
728
          CPU.State          <= PIPE_FILL_1;
729 151 jshamlet
 
730 167 jshamlet
        when STO_C1 =>
731
          IC                 := CACHE_PREFETCH;
732
          PC                 := PC_INCR;
733
          RegSel             := conv_integer(CPU.SubOp_p0(2 downto 1) & '0');
734
          if( not Enable_Auto_Increment )then
735
            CPU.State        <= PIPE_FILL_1;
736
          else
737
            CPU.State        <= PIPE_FILL_0;
738
            if( CPU.AutoIncr = '1' )then
739
              CPU.A_Oper     <= ALU_UPP1;
740
              CPU.A_Reg      <= CPU.SubOp_p0(2 downto 1) & '0';
741
              CPU.A_NoFlags  <= '1';
742
              CPU.A_Data     <= CPU.Regfile(RegSel);
743
              CPU.State      <= STO_C2;
744
            end if;
745 151 jshamlet
          end if;
746
 
747 167 jshamlet
        when STO_C2 =>
748
          PC                 := PC_INCR;
749
          RegSel             := conv_integer(CPU.SubOp_p0(2 downto 1) & '1');
750
          CPU.A_Oper         <= ALU_UPP2;
751
          CPU.A_Reg          <= CPU.SubOp_p0(2 downto 1) & '1';
752
          CPU.A_Data         <= CPU.Regfile(RegSel);
753
          CPU.State          <= PIPE_FILL_1;
754 151 jshamlet
 
755 167 jshamlet
        when STX_C1 =>
756
          PC                 := PC_INCR;
757
          if( not Enable_Auto_Increment )then
758
            CPU.State        <= PIPE_FILL_1;
759
          else
760
            RegSel           := conv_integer(CPU.SubOp_p0(2 downto 1) & '0');
761
            CPU.State        <= PIPE_FILL_1;
762
            if( CPU.AutoIncr = '1' )then
763
              CPU.A_Oper     <= ALU_UPP1;
764
              CPU.A_Reg      <= CPU.SubOp_p0(2 downto 1) & '0';
765
              CPU.A_NoFlags  <= '1';
766
              CPU.A_Data     <= CPU.Regfile(RegSel);
767
              CPU.State      <= STX_C2;
768
            end if;
769 151 jshamlet
          end if;
770
 
771 167 jshamlet
        when STX_C2 =>
772
          PC                 := PC_INCR;
773
          RegSel             := conv_integer(CPU.SubOp_p0(2 downto 1) & '1');
774
          CPU.A_Oper         <= ALU_UPP2;
775
          CPU.A_Reg          <= CPU.SubOp_p0(2 downto 1) & '1';
776
          CPU.A_Data         <= CPU.Regfile(RegSel);
777
          CPU.State          <= PIPE_FILL_2;
778 151 jshamlet
 
779
-------------------------------------------------------------------------------
780 167 jshamlet
-- Multi-Cycle Math Operations
781 151 jshamlet
-------------------------------------------------------------------------------
782
 
783 167 jshamlet
        when MATH_C1 =>
784
          PC                 := PC_INCR;
785
          CPU.A_Oper         <= CPU.Opcode;
786
          CPU.A_Reg          <= CPU.SubOp_p0;
787
          CPU.A_Data         <= CPU.Regfile(RegSel);
788
          CPU.State          <= PIPE_FILL_2;
789 151 jshamlet
 
790 167 jshamlet
        when GMSK_C1 =>
791
          PC                 := PC_INCR;
792
          CPU.A_Oper         <= ALU_LDI;
793
          CPU.A_Data         <= CPU.Int_Mask;
794
          CPU.State          <= PIPE_FILL_2;
795 151 jshamlet
 
796 167 jshamlet
        when MUL_C1 =>
797
          PC                 := PC_INCR;
798
          CPU.A_Oper         <= ALU_MUL;
799
          CPU.State          <= PIPE_FILL_2;
800
 
801
        when UPP_C1 =>
802
          PC                 := PC_INCR;
803
          RegSel             := conv_integer(CPU.SubOp_p1);
804
          CPU.A_Oper         <= ALU_UPP2;
805
          CPU.A_Reg          <= CPU.SubOp_p1;
806
          CPU.A_Data         <= CPU.Regfile(RegSel);
807
          CPU.State          <= PIPE_FILL_2;
808
 
809 151 jshamlet
-------------------------------------------------------------------------------
810
-- Basic Stack Manipulation (PSH, POP, RSP)
811
-------------------------------------------------------------------------------
812 167 jshamlet
        when PSH_C1 =>
813
          PC                 := PC_REV1;
814
          SP                 := SP_PUSH;
815
          CPU.State          <= PIPE_FILL_1;
816 151 jshamlet
 
817 167 jshamlet
        when POP_C1 =>
818
          PC                 := PC_IDLE;
819
          CPU.State          <= POP_C2;
820 151 jshamlet
 
821 167 jshamlet
        when POP_C2 =>
822
          PC                 := PC_IDLE;
823
          CPU.State          <= POP_C3;
824 151 jshamlet
 
825 167 jshamlet
        when POP_C3 =>
826
          IC                 := CACHE_OPER1;
827
          PC                 := PC_INCR;
828
          CPU.State          <= POP_C4;
829 151 jshamlet
 
830 167 jshamlet
        when POP_C4 =>
831
          PC                 := PC_INCR;
832
          CPU.A_Oper         <= ALU_LDI;
833
          CPU.A_Reg          <= CPU.SubOp_p0;
834
          CPU.A_NoFlags      <= '1';
835
          CPU.A_Data         <= CPU.Operand1;
836
          CPU.State          <= PIPE_FILL_2;
837
 
838 151 jshamlet
-------------------------------------------------------------------------------
839
-- Subroutines & Interrupts (RTS, JSR)
840
-------------------------------------------------------------------------------
841 167 jshamlet
        when WAIT_FOR_INT =>
842
          PC                 := PC_IDLE;
843
          DP.Src             := DATA_BUS_IDLE;
844
          CPU.State          <= WAIT_FOR_INT;
845 151 jshamlet
 
846 167 jshamlet
        when ISR_C1 =>
847
          PC                 := PC_IDLE;
848
          INT.Incr_ISR       := '1';
849
          CPU.State          <= ISR_C2;
850 151 jshamlet
 
851 167 jshamlet
        when ISR_C2 =>
852
          PC                 := PC_IDLE;
853
          DP.Src             := DATA_WR_FLAG;
854
          CPU.State          <= ISR_C3;
855 151 jshamlet
 
856 167 jshamlet
        when ISR_C3 =>
857
          IC                 := CACHE_OPER1;
858
          PC                 := PC_IDLE;
859
          SP                 := SP_PUSH;
860 168 jshamlet
          DP.Src             := DATA_WR_PC_UPPER;
861 167 jshamlet
          Ack_D              := '1';
862
          CPU.A_Oper         <= ALU_STP;
863
          CPU.A_Reg          <= INT_FLAG;
864
          CPU.State          <= JSR_C1;
865 151 jshamlet
 
866 167 jshamlet
        when JSR_C1 =>
867
          IC                 := CACHE_OPER2;
868
          PC                 := PC_IDLE;
869
          SP                 := SP_PUSH;
870 168 jshamlet
          DP.Src             := DATA_WR_PC_LOWER;
871 167 jshamlet
          CPU.State          <= JSR_C2;
872 151 jshamlet
 
873 167 jshamlet
        when JSR_C2 =>
874
          SP                 := SP_PUSH;
875
          PC                 := PC_LOAD;
876
          CPU.State          <= PIPE_FILL_0;
877 151 jshamlet
 
878 167 jshamlet
        when RTS_C1 =>
879
          PC                 := PC_IDLE;
880
          SP                 := SP_POP;
881
          CPU.State          <= RTS_C2;
882 151 jshamlet
 
883 167 jshamlet
        when RTS_C2 =>
884
          PC                 := PC_IDLE;
885
          if( CPU.SubOp_p0 = SOP_RTI )then
886
            SP               := SP_POP;
887
          end if;
888
          CPU.State          <= RTS_C3;
889 151 jshamlet
 
890 167 jshamlet
        when RTS_C3 =>
891
          IC                 := CACHE_OPER1;
892
          PC                 := PC_IDLE;
893
          CPU.State          <= RTS_C4;
894 151 jshamlet
 
895 167 jshamlet
        when RTS_C4 =>
896
          IC                 := CACHE_OPER2;
897
          PC                 := PC_IDLE;
898
          CPU.State          <= RTS_C5;
899 151 jshamlet
 
900 167 jshamlet
        when RTS_C5 =>
901
          PC                 := PC_LOAD;
902
          CPU.State          <= PIPE_FILL_0;
903
          if( CPU.SubOp_p0 = SOP_RTI )then
904
            IC               := CACHE_OPER1;
905
            CPU.State        <= RTI_C6;
906
          end if;
907 151 jshamlet
 
908 167 jshamlet
        when RTI_C6 =>
909
          PC                 := PC_INCR;
910
          CPU.Int_Level      <= 7;
911
          CPU.A_Oper         <= ALU_RFLG;
912
          CPU.A_Data         <= CPU.Operand1;
913
          CPU.State          <= PIPE_FILL_1;
914 151 jshamlet
 
915
-------------------------------------------------------------------------------
916
-- Debugging (BRK) Performs a 5-clock NOP
917
-------------------------------------------------------------------------------
918
 
919 167 jshamlet
        when BRK_C1 =>
920
          PC                 := PC_IDLE;
921
          CPU.State          <= PIPE_FILL_0;
922 151 jshamlet
 
923 167 jshamlet
        when others =>
924
          null;
925
 
926
      end case;
927
 
928
-------------------------------------------------------------------------------
929
-- Interrupt Override Logic
930
-------------------------------------------------------------------------------
931
 
932 151 jshamlet
    if( Int_Req = '1' )then
933 167 jshamlet
      if( CPU.State = INSTR_DECODE or CPU.State = WAIT_FOR_INT )then
934
        IC                   := CACHE_IDLE;
935
        PC                   := PC_REV3;
936
        SP                   := SP_IDLE;
937
        DP.Src               := DATA_RD_MEM;
938
        INT.Soft_Ints        := (others => '0');
939
        CPU.A_Oper           <= ALU_IDLE;
940
        CPU.State            <= ISR_C1;
941 153 jshamlet
 
942 151 jshamlet
      end if;
943
    end if;
944
 
945 156 jshamlet
-------------------------------------------------------------------------------
946 167 jshamlet
-- Vectored Interrupt Controller
947 156 jshamlet
-------------------------------------------------------------------------------
948 151 jshamlet
 
949 167 jshamlet
      CPU.Int_Pending        <= ((Interrupts or INT.Soft_Ints) and
950
                                 CPU.Int_Mask) or CPU.Int_Pending;
951 156 jshamlet
 
952 167 jshamlet
      if( CPU.Wait_for_FSM = '0' )then
953
        if( CPU.Int_Pending(0) = '1' )then
954
          CPU.Int_Addr       <= INT_VECTOR_0;
955
          CPU.Int_Level      <= 0;
956
          CPU.Int_Pending(0) <= '0';
957
          CPU.Wait_for_FSM   <= '1';
958
        elsif( CPU.Int_Pending(1) = '1' and CPU.Int_Level > 0 )then
959
          CPU.Int_Addr       <= INT_VECTOR_1;
960
          CPU.Int_Level      <= 1;
961
          CPU.Int_Pending(1) <= '0';
962
          CPU.Wait_for_FSM   <= '1';
963
        elsif( CPU.Int_Pending(2) = '1' and CPU.Int_Level > 1 )then
964
          CPU.Int_Addr       <= INT_VECTOR_2;
965
          CPU.Int_Level      <= 2;
966
          CPU.Int_Pending(2) <= '0';
967
          CPU.Wait_for_FSM   <= '1';
968
        elsif( CPU.Int_Pending(3) = '1' and CPU.Int_Level > 2 )then
969
          CPU.Int_Addr       <= INT_VECTOR_3;
970
          CPU.Int_Level      <= 3;
971
          CPU.Int_Pending(3) <= '0';
972
          CPU.Wait_for_FSM   <= '1';
973
        elsif( CPU.Int_Pending(4) = '1' and CPU.Int_Level > 3 )then
974
          CPU.Int_Addr       <= INT_VECTOR_4;
975
          CPU.Int_Level      <= 4;
976
          CPU.Int_Pending(4) <= '0';
977
          CPU.Wait_for_FSM   <= '1';
978
        elsif( CPU.Int_Pending(5) = '1' and CPU.Int_Level > 4 )then
979
          CPU.Int_Addr       <= INT_VECTOR_5;
980
          CPU.Int_Level      <= 5;
981
          CPU.Int_Pending(5) <= '0';
982
          CPU.Wait_for_FSM   <= '1';
983
        elsif( CPU.Int_Pending(6) = '1' and CPU.Int_Level > 6 )then
984
          CPU.Int_Addr       <= INT_VECTOR_6;
985
          CPU.Int_Level      <= 6;
986
          CPU.Int_Pending(6) <= '0';
987
          CPU.Wait_for_FSM   <= '1';
988
        elsif( CPU.Int_Pending(7) = '1' )then
989
          CPU.Int_Addr       <= INT_VECTOR_7;
990
          CPU.Int_Level      <= 7;
991
          CPU.Int_Pending(7) <= '0';
992
          CPU.Wait_for_FSM   <= '1';
993
        end if;
994
      end if;
995 156 jshamlet
 
996 167 jshamlet
      Ack_Q                  <= Ack_D;
997
      Ack_Q1                 <= Ack_Q;
998
      Int_Ack                <= Ack_Q1;
999
      if( Int_Ack = '1' )then
1000
        CPU.Wait_for_FSM     <= '0';
1001
      end if;
1002 151 jshamlet
 
1003 167 jshamlet
      Int_Req                <= CPU.Wait_for_FSM and (not Int_Ack);
1004 156 jshamlet
 
1005 167 jshamlet
      if( INT.Mask_Set = '1' )then
1006
        if( Enable_NMI )then
1007
          CPU.Int_Mask       <= Accumulator(7 downto 1) & '1';
1008
        else -- Disable NMI override
1009
          CPU.Int_Mask       <= Accumulator;
1010
        end if;
1011
      end if;
1012 156 jshamlet
 
1013 167 jshamlet
      if( INT.Incr_ISR = '1' )then
1014
        CPU.Int_Addr         <= CPU.Int_Addr + 1;
1015
      end if;
1016 156 jshamlet
 
1017
-------------------------------------------------------------------------------
1018 167 jshamlet
-- ALU (Arithmetic / Logic Unit)
1019 156 jshamlet
-------------------------------------------------------------------------------
1020 167 jshamlet
      Index                  := conv_integer(CPU.A_Reg);
1021 156 jshamlet
 
1022 167 jshamlet
      CPU.M_Prod             <= Accumulator *
1023
                                CPU.Regfile(conv_integer(CPU.M_Reg));
1024 156 jshamlet
 
1025 167 jshamlet
      case( CPU.A_Oper )is
1026
        when ALU_INC => -- Rn = Rn + 1 : CPU.Flags N,C,Z
1027
          Temp               := ("0" & x"01") +
1028
                                ("0" & CPU.A_Data);
1029
          Flags(FL_CARRY)    <= Temp(8);
1030
          CPU.Regfile(Index) <= Temp(7 downto 0);
1031
          if( CPU.A_NoFlags = '0' )then
1032
            Flags(FL_ZERO)   <= nor_reduce(Temp(7 downto 0));
1033
            Flags(FL_NEG)    <= Temp(7);
1034
          end if;
1035 156 jshamlet
 
1036 167 jshamlet
        when ALU_UPP2 => -- Rn = Rn + C : Flags C
1037
          Temp               := ("0" & x"00") +
1038
                                ("0" & CPU.A_Data) +
1039
                                 Flags(FL_CARRY);
1040
          Flags(FL_CARRY)    <= Temp(8);
1041
          CPU.Regfile(Index) <= Temp(7 downto 0);
1042 156 jshamlet
 
1043 167 jshamlet
        when ALU_ADC => -- R0 = R0 + Rn + C :  N,C,Z
1044
          Temp               := ("0" & Accumulator) +
1045
                                ("0" & CPU.A_Data) +
1046
                                Flags(FL_CARRY);
1047
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1048
          Flags(FL_CARRY)    <= Temp(8);
1049
          Flags(FL_NEG)      <= Temp(7);
1050
          Accumulator        <= Temp(7 downto 0);
1051 151 jshamlet
 
1052 167 jshamlet
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1053
          Temp               := "0" & CPU.A_Data;
1054
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1055
          Flags(FL_NEG)      <= Temp(7);
1056
          Accumulator        <= Temp(7 downto 0);
1057 151 jshamlet
 
1058 167 jshamlet
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1059
          Temp(7 downto 0)   := Accumulator or CPU.A_Data;
1060
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1061
          Flags(FL_NEG)      <= Temp(7);
1062
          Accumulator        <= Temp(7 downto 0);
1063 156 jshamlet
 
1064 167 jshamlet
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1065
          Temp(7 downto 0)   := Accumulator and CPU.A_Data;
1066
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1067
          Flags(FL_NEG)      <= Temp(7);
1068
          Accumulator        <= Temp(7 downto 0);
1069 156 jshamlet
 
1070 167 jshamlet
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1071
          Temp(7 downto 0)   := Accumulator xor CPU.A_Data;
1072
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1073
          Flags(FL_NEG)      <= Temp(7);
1074
          Accumulator        <= Temp(7 downto 0);
1075 156 jshamlet
 
1076 167 jshamlet
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1077
          Temp               := CPU.A_Data & Flags(FL_CARRY);
1078
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1079
          Flags(FL_CARRY)    <= Temp(8);
1080
          Flags(FL_NEG)      <= Temp(7);
1081
          CPU.Regfile(Index) <= Temp(7 downto 0);
1082 156 jshamlet
 
1083 167 jshamlet
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1084
          Temp               := CPU.A_Data(0) & Flags(FL_CARRY) &
1085
                                CPU.A_Data(7 downto 1);
1086
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1087
          Flags(FL_CARRY)    <= Temp(8);
1088
          Flags(FL_NEG)      <= Temp(7);
1089
          CPU.Regfile(Index) <= Temp(7 downto 0);
1090 156 jshamlet
 
1091 167 jshamlet
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1092
          Temp               := ("0" & CPU.A_Data) +
1093
                                ("0" & x"FF");
1094
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1095
          Flags(FL_CARRY)    <= Temp(8);
1096
          Flags(FL_NEG)      <= Temp(7);
1097
          CPU.Regfile(Index) <= Temp(7 downto 0);
1098 156 jshamlet
 
1099 167 jshamlet
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1100
          Temp               := ("0" & Accumulator) +
1101
                                ("0" & (not CPU.A_Data)) +
1102
                                     Flags(FL_CARRY);
1103
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1104
          Flags(FL_CARRY)    <= Temp(8);
1105
          Flags(FL_NEG)      <= Temp(7);
1106
          Accumulator        <= Temp(7 downto 0);
1107 151 jshamlet
 
1108 167 jshamlet
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1109
          Temp               := ("0" & Accumulator) +
1110
                                ("0" & CPU.A_Data);
1111
          Flags(FL_CARRY)    <= Temp(8);
1112
          Accumulator        <= Temp(7 downto 0);
1113
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1114
          Flags(FL_NEG)      <= Temp(7);
1115 164 jshamlet
 
1116 167 jshamlet
        when ALU_STP => -- Sets bit(n) in the CPU.Flags register
1117
          Flags(Index)       <= '1';
1118 151 jshamlet
 
1119 167 jshamlet
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1120
          Flags(FL_ZERO)     <= not Accumulator(Index);
1121
          Flags(FL_NEG)      <= Accumulator(7);
1122 156 jshamlet
 
1123 167 jshamlet
        when ALU_CLP => -- Clears bit(n) in the Flags register
1124
          Flags(Index)       <= '0';
1125 156 jshamlet
 
1126 167 jshamlet
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1127
          Temp               := "0" & Accumulator;
1128
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1129
          Flags(FL_NEG)      <= Temp(7);
1130
          CPU.Regfile(Index) <= Temp(7 downto 0);
1131 156 jshamlet
 
1132 167 jshamlet
        when ALU_CMP => -- Sets CPU.Flags on R0 - Rn : Flags N,C,Z
1133
          Temp               := ("0" & Accumulator) +
1134
                                ("0" & (not CPU.A_Data)) +
1135
                                 '1';
1136
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1137
          Flags(FL_CARRY)    <= Temp(8);
1138
          Flags(FL_NEG)      <= Temp(7);
1139 156 jshamlet
 
1140 167 jshamlet
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1141
          CPU.Regfile(0)     <= CPU.M_Prod(7 downto 0);
1142
          CPU.Regfile(1)     <= CPU.M_Prod(15 downto 8);
1143
          Flags(FL_ZERO)     <= nor_reduce(CPU.M_Prod);
1144 151 jshamlet
 
1145 167 jshamlet
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1146
          if( CPU.A_NoFlags = '0' )then
1147
            Flags(FL_ZERO)   <= nor_reduce(CPU.A_Data);
1148
            Flags(FL_NEG)    <= CPU.A_Data(7);
1149
          end if;
1150
          CPU.Regfile(Index) <= CPU.A_Data;
1151 151 jshamlet
 
1152 167 jshamlet
        when ALU_RFLG =>
1153
          Flags              <= CPU.A_Data;
1154 151 jshamlet
 
1155 167 jshamlet
        when others =>
1156
          null;
1157
      end case;
1158 151 jshamlet
 
1159 156 jshamlet
-------------------------------------------------------------------------------
1160 167 jshamlet
-- Instruction/Operand caching for pipelined memory access
1161 156 jshamlet
-------------------------------------------------------------------------------
1162
 
1163 167 jshamlet
      case( IC )is
1164
        when CACHE_INSTR =>
1165
          CPU.Opcode         <= Rd_Data(7 downto 3);
1166
          CPU.SubOp_p0       <= Rd_Data(2 downto 0);
1167
          CPU.SubOp_p1       <= Rd_Data(2 downto 0) + 1;
1168
          if( CPU.Cache_Valid = '1' )then
1169
            CPU.Opcode       <= CPU.Prefetch(7 downto 3);
1170
            CPU.SubOp_p0     <= CPU.Prefetch(2 downto 0);
1171
            CPU.SubOp_p1     <= CPU.Prefetch(2 downto 0) + 1;
1172
            CPU.Cache_Valid  <= '0';
1173 156 jshamlet
          end if;
1174
 
1175 167 jshamlet
        when CACHE_OPER1 =>
1176
          CPU.Operand1       <= Rd_Data;
1177 156 jshamlet
 
1178 167 jshamlet
        when CACHE_OPER2 =>
1179
          CPU.Operand2       <= Rd_Data;
1180 156 jshamlet
 
1181 167 jshamlet
        when CACHE_PREFETCH =>
1182
          CPU.Prefetch       <= Rd_Data;
1183
          CPU.Cache_Valid    <= '1';
1184 156 jshamlet
 
1185 167 jshamlet
        when CACHE_PFFLUSH =>
1186
          CPU.Prefetch       <= Rd_Data;
1187
          CPU.Operand1       <= x"00";
1188
          CPU.Operand2       <= x"00";
1189
          CPU.Cache_Valid    <= '1';
1190 156 jshamlet
 
1191 167 jshamlet
        when CACHE_INVALIDATE =>
1192
          CPU.Cache_Valid    <= '0';
1193
 
1194
        when CACHE_IDLE =>
1195
          null;
1196
      end case;
1197
 
1198 156 jshamlet
-------------------------------------------------------------------------------
1199 167 jshamlet
-- Program Counter
1200 156 jshamlet
-------------------------------------------------------------------------------
1201
 
1202 167 jshamlet
      Offset_SX(15 downto 8) := (others => CPU.Operand1(7));
1203
      Offset_SX(7 downto 0)  := CPU.Operand1;
1204 156 jshamlet
 
1205 167 jshamlet
      case( PC )is
1206 156 jshamlet
 
1207 167 jshamlet
        when PC_INCR =>
1208
          CPU.Program_Ctr    <= CPU.Program_ctr + 1;
1209
 
1210
        when PC_IDLE =>
1211
        --CPU.Program_Ctr    <= CPU.Program_Ctr + 0;
1212
          null;
1213 156 jshamlet
 
1214 167 jshamlet
        when PC_REV1 =>
1215
          CPU.Program_Ctr    <= CPU.Program_Ctr - 1;
1216 156 jshamlet
 
1217 167 jshamlet
        when PC_REV2 =>
1218
          CPU.Program_Ctr    <= CPU.Program_Ctr - 2;
1219 156 jshamlet
 
1220 167 jshamlet
        when PC_REV3 =>
1221
          CPU.Program_Ctr    <= CPU.Program_Ctr - 3;
1222 156 jshamlet
 
1223 167 jshamlet
        when PC_BRANCH =>
1224
          CPU.Program_Ctr    <= CPU.Program_Ctr + Offset_SX - 2;
1225 156 jshamlet
 
1226 167 jshamlet
        when PC_LOAD =>
1227
          CPU.Program_Ctr    <= CPU.Operand2 & CPU.Operand1;
1228 156 jshamlet
 
1229 167 jshamlet
        when others =>
1230
          null;
1231
      end case;
1232 156 jshamlet
 
1233 167 jshamlet
-------------------------------------------------------------------------------
1234
-- (Write) Data Path
1235
-------------------------------------------------------------------------------
1236 156 jshamlet
 
1237 167 jshamlet
      Wr_Data                <= x"00";
1238
      Wr_Enable              <= '0';
1239
      Rd_Enable              <= '0';
1240 156 jshamlet
 
1241 167 jshamlet
      case( DP.Src )is
1242
        when DATA_BUS_IDLE =>
1243
          null;
1244 156 jshamlet
 
1245 167 jshamlet
        when DATA_RD_MEM =>
1246
          Rd_Enable          <= '1';
1247 156 jshamlet
 
1248 167 jshamlet
        when DATA_WR_REG =>
1249
          Wr_Enable          <= '1';
1250
          Wr_Data            <= CPU.Regfile(conv_integer(DP.Reg));
1251 156 jshamlet
 
1252 167 jshamlet
        when DATA_WR_FLAG =>
1253
          Wr_Enable          <= '1';
1254
          Wr_Data            <= Flags;
1255 156 jshamlet
 
1256 168 jshamlet
        when DATA_WR_PC_LOWER =>
1257 167 jshamlet
          Wr_Enable          <= '1';
1258 168 jshamlet
          Wr_Data            <= CPU.Program_Ctr(7 downto 0);
1259
 
1260
                  when DATA_WR_PC_UPPER =>
1261
          Wr_Enable          <= '1';
1262 167 jshamlet
          Wr_Data            <= CPU.Program_Ctr(15 downto 8);
1263 156 jshamlet
 
1264 167 jshamlet
        when others =>
1265
          null;
1266
      end case;
1267 156 jshamlet
 
1268 167 jshamlet
-------------------------------------------------------------------------------
1269
-- Stack Pointer
1270
-------------------------------------------------------------------------------
1271
      case( SP )is
1272
        when SP_IDLE =>
1273
          null;
1274 156 jshamlet
 
1275 167 jshamlet
        when SP_RSET =>
1276
          CPU.Stack_Ptr      <= Stack_Start_Addr;
1277
          if( Allow_Stack_Address_Move )then
1278
            CPU.Stack_Ptr    <= CPU.Regfile(1) & CPU.Regfile(0);
1279
          end if;
1280 156 jshamlet
 
1281 167 jshamlet
        when SP_POP  =>
1282
          CPU.Stack_Ptr      <= CPU.Stack_Ptr + 1;
1283 156 jshamlet
 
1284 167 jshamlet
        when SP_PUSH =>
1285
          CPU.Stack_Ptr      <= CPU.Stack_Ptr - 1;
1286 156 jshamlet
 
1287 167 jshamlet
        when others =>
1288
          null;
1289 156 jshamlet
 
1290 167 jshamlet
      end case;
1291
 
1292 156 jshamlet
    end if;
1293
  end process;
1294
 
1295
end architecture;

powered by: WebSVN 2.1.0

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