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 156

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

Line No. Rev Author Line
1 151 jshamlet
-- Copyright (c)2006, Jeremy Seth Henry
2
-- 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
-- VHDL Units :  Open8_CPU
25
-- Description:  VHDL model of the V8 uRISC 8-bit processor core
26
-- Notes      :  Generic definitions
27
--            :  Stack_Start_Address - determines the initial (reset) value of
28
--            :   the stack pointer. Also used for the RSP instruction if
29
--            :   Allow_Stack_Address_Move is 0.
30
--            :
31
--            :  Allow_Stack_Address_Move - When set to 1, allows the RSP to be
32
--            :   programmed via thet RSP instruction. If enabled, the contents
33
--            :   of R1:R0 are used to initialize the stack pointer.
34
--            :
35
--            :  ISR_Start_Addr - determines the location of the interrupt
36
--            :   service vector table. There are 8 service vectors, or 16
37
--            :   bytes, which must be allocated to either ROM or RAM.
38
--            :
39
--            :  Program_Start_Addr - Determines the initial value of the
40
--            :   program counter.
41
--            :
42
--            :  Default_Interrupt_Mask - Determines the intial value of the
43
--            :   interrupt mask. To remain true to the original core, which
44
--            :   had no interrupt mask, this should be set to x"FF". Otherwise
45
--            :   it can be initialized to any value.
46
--            :
47
--            :  Enable_CPU_Halt - determines whether the CPU_Halt pin is
48
--            :   connected or not. This signal is typically used to halt the
49
--            :   processor for a few cycles when accessing slower peripherals,
50
--            :   but may also be used to single step the processor. If this
51
--            :   feature isn't used, it can be disabled to increase Fmax.
52
--            :
53
--            :  The CPU_Halt signal can be used to access slower peripherals
54
--            :   by allowing the device to "pause" the CPU. This can be used,
55
--            :   for example, to write to a standard LCD panel, which requires
56
--            :   a 4MHz interface, by halting on writes. Alternately, devices
57
--            :   such as SDRAM controllers, can pause the processor until the
58
--            :   data is ready to be presented.
59
--            :
60
--            :  The Enable_Auto_Increment generic can be used to modify the
61
--            :   indexed instructions such that specifying an odd register
62
--            :   will use the next lower register pair, post-incrementing the
63
--            :   value in that pair. IOW, specifying STX R1 will instead
64
--            :   result in STX R0++, or R0 = {R1:R0}; {R1:R0} + 1
65
--            :
66
--            : Instructions USR and USR2 have been replaced with DBNZ, and MUL
67
--            :  respectively. DBNZ decrements the specified register, and will
68
--            :  branch if the result is non-zero (Zero flag is not set). MUL
69
--            :  places the result of R0 * Rn into R1:R0, and executes in two
70
--            :  cycles. (R1:R0 = R0 * Rn)
71
--            :
72
-- Revision History
73
-- Author          Date     Change
74
------------------ -------- ---------------------------------------------------
75
-- Seth Henry      07/19/06 Design Start
76
-- Seth Henry      01/18/11 Fixed BTT instruction to match V8
77 155 jshamlet
-- Seth Henry      07/22/11 Fixed interrupt transition logic to avoid data
78
--                           corruption issues.
79
-- Seth Henry      07/26/11 Optimized logic in ALU, stack pointer, and data path
80
--                           sections.
81 156 jshamlet
-- Seth Henry      07/27/11 Optimized logic for timing, merged blocks into
82
--                           single entity.
83 151 jshamlet
 
84
library ieee;
85
  use ieee.std_logic_1164.all;
86
  use ieee.std_logic_unsigned.all;
87
  use ieee.std_logic_arith.all;
88
 
89
library work;
90
use work.Open8_pkg.all;
91
 
92
entity Open8_CPU is
93
  generic(
94
    Stack_Start_Addr         : ADDRESS_TYPE := x"007F"; -- Top of Stack
95
    Allow_Stack_Address_Move : boolean      := false;   -- Use Normal v8 RSP
96
    ISR_Start_Addr           : ADDRESS_TYPE := x"0080"; -- Bottom of ISR vec's
97
    Program_Start_Addr       : ADDRESS_TYPE := x"0090"; -- Initial PC location
98
    Default_Interrupt_Mask   : DATA_TYPE    := x"FF";   -- Enable all Ints
99
    Enable_CPU_Halt          : boolean      := false;   -- Disable HALT pin
100
    Enable_Auto_Increment    : boolean      := false;   -- Modify indexed instr
101
    Reset_Level              : std_logic    := '0' );   -- Active reset level
102
  port(
103
    Clock                    : in  std_logic;
104
    Reset                    : in  std_logic;
105
    CPU_Halt                 : in  std_logic := '0';
106
    Interrupts               : in  INTERRUPT_BUNDLE;
107
    --
108
    Address                  : out ADDRESS_TYPE;
109
    Rd_Data                  : in  DATA_TYPE;
110
    Rd_Enable                : out std_logic;
111
    Wr_Data                  : out DATA_TYPE;
112
    Wr_Enable                : out std_logic );
113
end entity;
114
 
115 156 jshamlet
architecture behave of Open8_CPU is
116
 
117 151 jshamlet
  subtype OPCODE_TYPE  is std_logic_vector(4 downto 0);
118
  subtype SUBOP_TYPE   is std_logic_vector(2 downto 0);
119
 
120
  -- These are all the primary instructions/op-codes (upper 5-bits)
121
  constant OP_INC            : OPCODE_TYPE := "00000";
122
  constant OP_ADC            : OPCODE_TYPE := "00001";
123
  constant OP_TX0            : OPCODE_TYPE := "00010";
124
  constant OP_OR             : OPCODE_TYPE := "00011";
125
  constant OP_AND            : OPCODE_TYPE := "00100";
126
  constant OP_XOR            : OPCODE_TYPE := "00101";
127
  constant OP_ROL            : OPCODE_TYPE := "00110";
128
  constant OP_ROR            : OPCODE_TYPE := "00111";
129
  constant OP_DEC            : OPCODE_TYPE := "01000";
130
  constant OP_SBC            : OPCODE_TYPE := "01001";
131
  constant OP_ADD            : OPCODE_TYPE := "01010";
132
  constant OP_STP            : OPCODE_TYPE := "01011";
133
  constant OP_BTT            : OPCODE_TYPE := "01100";
134
  constant OP_CLP            : OPCODE_TYPE := "01101";
135
  constant OP_T0X            : OPCODE_TYPE := "01110";
136
  constant OP_CMP            : OPCODE_TYPE := "01111";
137
  constant OP_PSH            : OPCODE_TYPE := "10000";
138
  constant OP_POP            : OPCODE_TYPE := "10001";
139
  constant OP_BR0            : OPCODE_TYPE := "10010";
140
  constant OP_BR1            : OPCODE_TYPE := "10011";
141
  constant OP_DBNZ           : OPCODE_TYPE := "10100"; -- USR
142
  constant OP_INT            : OPCODE_TYPE := "10101";
143
  constant OP_MUL            : OPCODE_TYPE := "10110"; -- USR2
144
  constant OP_STK            : OPCODE_TYPE := "10111";
145
  constant OP_UPP            : OPCODE_TYPE := "11000";
146
  constant OP_STA            : OPCODE_TYPE := "11001";
147
  constant OP_STX            : OPCODE_TYPE := "11010";
148
  constant OP_STO            : OPCODE_TYPE := "11011";
149
  constant OP_LDI            : OPCODE_TYPE := "11100";
150
  constant OP_LDA            : OPCODE_TYPE := "11101";
151
  constant OP_LDX            : OPCODE_TYPE := "11110";
152
  constant OP_LDO            : OPCODE_TYPE := "11111";
153
 
154
  -- These are all specific sub-opcodes for OP_STK / 0xB8 (lower 3-bits)
155
  constant SOP_RSP           : SUBOP_TYPE := "000";
156
  constant SOP_RTS           : SUBOP_TYPE := "001";
157
  constant SOP_RTI           : SUBOP_TYPE := "010";
158
  constant SOP_BRK           : SUBOP_TYPE := "011";
159
  constant SOP_JMP           : SUBOP_TYPE := "100";
160
  constant SOP_SMSK          : SUBOP_TYPE := "101";
161
  constant SOP_GMSK          : SUBOP_TYPE := "110";
162
  constant SOP_JSR           : SUBOP_TYPE := "111";
163
 
164
  -- Preinitialization is for simulation only - check actual reset conditions
165
  type CPU_STATES is (
166
      -- Instruction fetch & Decode
167
    PIPE_FILL_0, PIPE_FILL_1, PIPE_FILL_2, INSTR_DECODE,
168
    -- Branching
169
    BRN_C1, DBNZ_C1, JMP_C1, JMP_C2,
170
    -- Loads
171
    LDA_C1, LDA_C2, LDA_C3, LDA_C4, LDI_C1, LDO_C1, LDX_C1, LDX_C2, LDX_C3,
172
    -- Stores
173
    STA_C1, STA_C2, STA_C3, STO_C1, STO_C2, STX_C1, STX_C2,
174
    -- 2-cycle math
175
    MUL_C1, UPP_C1,
176
    -- Stack
177
    PSH_C1, POP_C1, POP_C2, POP_C3, POP_C4,
178
    -- Subroutines & Interrupts
179
    WAIT_FOR_INT, ISR_C1, ISR_C2, ISR_C3, JSR_C1, JSR_C2,
180
    RTS_C1, RTS_C2, RTS_C3, RTS_C4, RTS_C5, RTI_C6,
181
    -- Debugging
182
    BRK_C1 );
183
 
184 156 jshamlet
  type CACHE_MODES is (CACHE_IDLE, CACHE_INSTR, CACHE_OPER1, CACHE_OPER2,
185
                       CACHE_PREFETCH );
186
 
187
  type PC_MODES is ( PC_IDLE, PC_REV1, PC_REV2, PC_INCR, PC_LOAD );
188
 
189
  type PC_CTRL_TYPE is record
190
    Oper                     : PC_MODES;
191
    Offset                   : DATA_TYPE;
192
    Addr                     : ADDRESS_TYPE;
193
  end record;
194
 
195
  type SP_MODES is ( SP_IDLE, SP_RSET, SP_POP, SP_PUSH );
196
 
197
  type SP_CTRL_TYPE is record
198
    Oper                     : SP_MODES;
199
    Addr                     : ADDRESS_TYPE;
200
  end record;
201
 
202
  type DP_MODES is ( DATA_IDLE, DATA_REG, DATA_FLAG, DATA_PC );
203
 
204
  type DATA_CTRL_TYPE is record
205
    Src                      : DP_MODES;
206
    Reg                      : SUBOP_TYPE;
207
  end record;
208
 
209
  -- Preinitialization is for simulation only - check actual reset conditions
210
  constant INT_VECTOR_0      : ADDRESS_TYPE := ISR_Start_Addr;
211
  constant INT_VECTOR_1      : ADDRESS_TYPE := ISR_Start_Addr+2;
212
  constant INT_VECTOR_2      : ADDRESS_TYPE := ISR_Start_Addr+4;
213
  constant INT_VECTOR_3      : ADDRESS_TYPE := ISR_Start_Addr+6;
214
  constant INT_VECTOR_4      : ADDRESS_TYPE := ISR_Start_Addr+8;
215
  constant INT_VECTOR_5      : ADDRESS_TYPE := ISR_Start_Addr+10;
216
  constant INT_VECTOR_6      : ADDRESS_TYPE := ISR_Start_Addr+12;
217
  constant INT_VECTOR_7      : ADDRESS_TYPE := ISR_Start_Addr+14;
218
 
219
  type INT_CTRL_TYPE is record
220
    Mask_Set                 : std_logic;
221
    Soft_Ints                : INTERRUPT_BUNDLE;
222
    Incr_ISR                 : std_logic;
223
  end record;
224
 
225
  type INT_HIST is array (0 to 8) of integer range 0 to 7;
226
 
227
  -- Most of the ALU instructions are the same as their Opcode equivalents with
228
  -- three exceptions (for IDLE, UPP2, and MUL2)
229
  constant ALU_INC           : OPCODE_TYPE := "00000"; -- x"00"
230
  constant ALU_ADC           : OPCODE_TYPE := "00001"; -- x"01"
231
  constant ALU_TX0           : OPCODE_TYPE := "00010"; -- x"02"
232
  constant ALU_OR            : OPCODE_TYPE := "00011"; -- x"03"
233
  constant ALU_AND           : OPCODE_TYPE := "00100"; -- x"04"
234
  constant ALU_XOR           : OPCODE_TYPE := "00101"; -- x"05"
235
  constant ALU_ROL           : OPCODE_TYPE := "00110"; -- x"06"
236
  constant ALU_ROR           : OPCODE_TYPE := "00111"; -- x"07"
237
  constant ALU_DEC           : OPCODE_TYPE := "01000"; -- x"08"
238
  constant ALU_SBC           : OPCODE_TYPE := "01001"; -- x"09"
239
  constant ALU_ADD           : OPCODE_TYPE := "01010"; -- x"0A"
240
  constant ALU_STP           : OPCODE_TYPE := "01011"; -- x"0B"
241
  constant ALU_BTT           : OPCODE_TYPE := "01100"; -- x"0C"
242
  constant ALU_CLP           : OPCODE_TYPE := "01101"; -- x"0D"
243
  constant ALU_T0X           : OPCODE_TYPE := "01110"; -- x"0E"
244
  constant ALU_CMP           : OPCODE_TYPE := "01111"; -- x"0F"
245
  constant ALU_POP           : OPCODE_TYPE := "10001"; -- x"11"
246
  constant ALU_MUL           : OPCODE_TYPE := "10110"; -- x"16"
247
  constant ALU_UPP           : OPCODE_TYPE := "11000"; -- x"18"
248
  constant ALU_LDI           : OPCODE_TYPE := "11100"; -- x"1C"
249
  constant ALU_LDX           : OPCODE_TYPE := "11110"; -- x"1E"
250
 
251
  constant ALU_IDLE          : OPCODE_TYPE := "10000"; -- x"10"
252
  constant ALU_UPP2          : OPCODE_TYPE := "10010"; -- x"12"
253
  constant ALU_RFLG          : OPCODE_TYPE := "10011"; -- x"13"
254
 
255
  constant FL_ZERO           : integer := 0;
256
  constant FL_CARRY          : integer := 1;
257
  constant FL_NEG            : integer := 2;
258
  constant FL_INT_EN         : integer := 3;
259
  constant FL_GP1            : integer := 4;
260
  constant FL_GP2            : integer := 5;
261
  constant FL_GP3            : integer := 6;
262
  constant FL_GP4            : integer := 7;
263
 
264
  type ALU_CTRL_TYPE is record
265
    Oper                     : OPCODE_TYPE;
266
    Reg                      : SUBOP_TYPE;
267
    Data                     : DATA_TYPE;
268
  end record;
269
 
270
  constant ACCUM             : SUBOP_TYPE := "000";
271
  constant INT_FLAG          : SUBOP_TYPE := "011";
272
 
273
  type REGFILE_TYPE is array (0 to 7) of DATA_TYPE;
274
 
275
  subtype FLAG_TYPE is DATA_TYPE;
276
 
277
  signal Halt                : std_logic;
278
 
279 151 jshamlet
  signal CPU_Next_State      : CPU_STATES := PIPE_FILL_0;
280
  signal CPU_State           : CPU_STATES := PIPE_FILL_0;
281
 
282
  signal Cache_Ctrl          : CACHE_MODES := CACHE_IDLE;
283
 
284
  signal Opcode              : OPCODE_TYPE := (others => '0');
285
  signal SubOp, SubOp_p1     : SUBOP_TYPE  := (others => '0');
286
 
287 156 jshamlet
  signal Prefetch            : DATA_TYPE   := x"00";
288
  signal Operand1, Operand2  : DATA_TYPE   := x"00";
289
 
290 151 jshamlet
  signal Instr_Prefetch      : std_logic   := '0';
291
 
292 156 jshamlet
  signal PC_Ctrl             : PC_CTRL_TYPE;
293
  signal Program_Ctr         : ADDRESS_TYPE := x"0000";
294
 
295
  signal SP_Ctrl             : SP_CTRL_TYPE;
296
  signal Stack_Ptr           : ADDRESS_TYPE := x"0000";
297
 
298
  signal DP_Ctrl             : DATA_CTRL_TYPE;
299
 
300
  signal INT_Ctrl            : INT_CTRL_TYPE;
301 151 jshamlet
  signal Ack_D, Ack_Q, Ack_Q1: std_logic   := '0';
302 156 jshamlet
  signal Int_RTI_D, Int_RTI  : std_logic   := '0';
303
  signal Int_Req, Int_Ack    : std_logic   := '0';
304
  signal Int_Mask            : DATA_TYPE   := x"00";
305
  signal ISR_Addr            : ADDRESS_TYPE := x"0000";
306
  signal i_Ints              : INTERRUPT_BUNDLE := x"00";
307
  signal Pending             : INTERRUPT_BUNDLE := x"00";
308
  signal Wait_for_FSM        : std_logic := '0';
309
  signal History             : INT_HIST := (others => 0);
310
  signal Hst_Ptr             : integer range 0 to 8 := 0;
311 151 jshamlet
 
312 156 jshamlet
  signal ALU_Ctrl            : ALU_CTRL_TYPE;
313
  signal Regfile             : REGFILE_TYPE;
314
  signal Flags               : FLAG_TYPE;
315
  signal Mult                : ADDRESS_TYPE := x"0000";
316
 
317 151 jshamlet
begin
318
 
319 156 jshamlet
Halt_Disabled_fn: if( not Enable_CPU_Halt )generate
320
  Halt                       <= '0';
321
end generate;
322 151 jshamlet
 
323 156 jshamlet
Halt_Enabled_fn: if( Enable_CPU_Halt )generate
324
  Halt                       <= CPU_Halt;
325
end generate;
326
 
327
-------------------------------------------------------------------------------
328
-- State Logic / Instruction Decoding & Execution
329
-- Combinatorial portion of CPU finite state machine
330
-------------------------------------------------------------------------------
331
 
332
  State_Logic: process(CPU_State, Regfile, Flags, Int_Mask, Opcode,
333
                       SubOp , SubOp_p1, Operand1, Operand2, Int_Req,
334
                       Program_Ctr, Stack_Ptr, ISR_Addr )
335 151 jshamlet
    variable Reg, Reg_1      : integer range 0 to 7 := 0;
336
    variable Offset_SX       : ADDRESS_TYPE;
337
  begin
338
    CPU_Next_State           <= CPU_State;
339
    Cache_Ctrl               <= CACHE_IDLE;
340
    --
341
    ALU_Ctrl.Oper            <= ALU_IDLE;
342
    ALU_Ctrl.Reg             <= ACCUM;
343
    ALU_Ctrl.Data            <= x"00";
344
    --
345
    PC_Ctrl.Oper             <= PC_IDLE;
346
    PC_Ctrl.Offset           <= x"03";
347
    PC_Ctrl.Addr             <= x"0000";
348
    --
349
    SP_Ctrl.Oper             <= SP_IDLE;
350
    --
351 156 jshamlet
    Address                  <= Program_Ctr;
352 151 jshamlet
    --
353
    DP_Ctrl.Src              <= DATA_IDLE;
354
    DP_Ctrl.Reg              <= ACCUM;
355
    --
356
    INT_Ctrl.Mask_Set        <= '0';
357
    INT_Ctrl.Soft_Ints       <= x"00";
358
    INT_Ctrl.Incr_ISR        <= '0';
359
    Ack_D                    <= '0';
360
    Int_RTI_D                <= '0';
361
 
362
    -- Assign the most common value of Reg and Reg1 outside the case structure
363
    --  to simplify things.
364
    Reg                      := conv_integer(SubOp);
365
    Reg_1                    := conv_integer(SubOp_p1);
366
    Offset_SX(15 downto 0)   := (others => Operand1(7));
367
    Offset_SX(7 downto 0)    := Operand1;
368
 
369
    case CPU_State is
370
-------------------------------------------------------------------------------
371
-- Initial Instruction fetch & decode
372
-------------------------------------------------------------------------------
373
      when PIPE_FILL_0 =>
374
        CPU_Next_State       <= PIPE_FILL_1;
375
        PC_Ctrl.Oper         <= PC_INCR;
376
 
377
      when PIPE_FILL_1 =>
378
        CPU_Next_State       <= PIPE_FILL_2;
379
        PC_Ctrl.Oper         <= PC_INCR;
380
 
381
      when PIPE_FILL_2 =>
382
        CPU_Next_State       <= INSTR_DECODE;
383
        Cache_Ctrl           <= CACHE_INSTR;
384
        PC_Ctrl.Oper         <= PC_INCR;
385
 
386
      when INSTR_DECODE =>
387
        CPU_Next_State       <= INSTR_DECODE;
388
        Cache_Ctrl           <= CACHE_INSTR;
389
 
390
        case Opcode is
391
          when OP_PSH =>
392
            CPU_Next_State   <= PSH_C1;
393
            Cache_Ctrl       <= CACHE_PREFETCH;
394
            PC_Ctrl.Oper     <= PC_REV1;
395
            DP_Ctrl.Src      <= DATA_REG;
396
            DP_Ctrl.Reg      <= SubOp;
397
 
398
          when OP_POP =>
399
            CPU_Next_State   <= POP_C1;
400
            Cache_Ctrl       <= CACHE_PREFETCH;
401
            PC_Ctrl.Oper     <= PC_REV2;
402
            SP_Ctrl.Oper     <= SP_POP;
403
 
404
          when OP_BR0 | OP_BR1 =>
405
            CPU_Next_State   <= BRN_C1;
406
            Cache_Ctrl       <= CACHE_OPER1;
407
            PC_Ctrl.Oper     <= PC_INCR;
408
 
409
          when OP_DBNZ =>
410
            CPU_Next_State   <= DBNZ_C1;
411
            Cache_Ctrl       <= CACHE_OPER1;
412
            PC_Ctrl.Oper     <= PC_INCR;
413
            ALU_Ctrl.Oper    <= ALU_DEC;
414
            ALU_Ctrl.Reg     <= SubOp;
415
 
416
          when OP_INT =>
417
            PC_Ctrl.Oper     <= PC_INCR;
418
            if( Int_Mask(Reg) = '1' )then
419
              CPU_Next_State <= WAIT_FOR_INT;
420
              INT_Ctrl.Soft_Ints(Reg) <= '1';
421
            end if;
422
 
423
          when OP_STK =>
424
            case SubOp is
425
              when SOP_RSP  =>
426
                PC_Ctrl.Oper <= PC_INCR;
427
                SP_Ctrl.Oper <= SP_RSET;
428
 
429
              when SOP_RTS | SOP_RTI =>
430
                CPU_Next_State <= RTS_C1;
431
                Cache_Ctrl   <= CACHE_IDLE;
432
                SP_Ctrl.Oper <= SP_POP;
433
 
434
              when SOP_BRK  =>
435
                CPU_Next_State <= BRK_C1;
436
                PC_Ctrl.Oper <= PC_REV2;
437
 
438
              when SOP_JMP  =>
439
                CPU_Next_State <= JMP_C1;
440
                Cache_Ctrl   <= CACHE_OPER1;
441
 
442
              when SOP_SMSK =>
443
                PC_Ctrl.Oper <= PC_INCR;
444
                INT_Ctrl.Mask_Set <= '1';
445
 
446
              when SOP_GMSK =>
447
                PC_Ctrl.Oper <= PC_INCR;
448
                ALU_Ctrl.Oper<= ALU_LDI;
449
                ALU_Ctrl.Reg <= ACCUM;
450
                ALU_Ctrl.Data<= Int_Mask;
451
 
452
              when SOP_JSR =>
453
                CPU_Next_State <= JSR_C1;
454
                Cache_Ctrl   <= CACHE_OPER1;
455
                DP_Ctrl.Src  <= DATA_PC;
456
                DP_Ctrl.Reg  <= ACCUM+1;
457
 
458
              when others => null;
459
            end case;
460
 
461
          when OP_MUL =>
462
            CPU_Next_State   <= MUL_C1;
463
            Cache_Ctrl       <= CACHE_PREFETCH;
464
 
465
            -- We need to back the PC up by 1, and allow it to refill. An
466
            --  unfortunate consequence of the pipelining. We can get away with
467
            --  only 1 extra clock by pre-fetching the next instruction, though
468
            PC_Ctrl.Oper     <= PC_REV1;
469
            -- Multiplication is automatic, but requires a single clock cycle.
470
            --  We need to specify the register for Rn (R1:R0 = R0 * Rn) now,
471
            --   but will issue the multiply command on the next clock to copy
472
            --   the results to the specified register.
473
            ALU_Ctrl.Oper    <= ALU_IDLE;
474
            ALU_Ctrl.Reg     <= SubOp; -- Rn
475
 
476
          when OP_UPP =>
477
            CPU_Next_State   <= UPP_C1;
478
            Cache_Ctrl       <= CACHE_PREFETCH;
479
            PC_Ctrl.Oper     <= PC_REV1;
480
            ALU_Ctrl.Oper    <= Opcode;
481
            ALU_Ctrl.Reg     <= SubOp;
482
 
483
          when OP_LDA =>
484
            CPU_Next_State   <= LDA_C1;
485
            Cache_Ctrl       <= CACHE_OPER1;
486
 
487
          when OP_LDI =>
488
            CPU_Next_State   <= LDI_C1;
489
            Cache_Ctrl       <= CACHE_OPER1;
490
            PC_Ctrl.Oper     <= PC_INCR;
491
 
492
          when OP_LDO =>
493
            CPU_Next_State   <= LDO_C1;
494
            Cache_Ctrl       <= CACHE_OPER1;
495
            PC_Ctrl.Oper     <= PC_REV2;
496
 
497
          when OP_LDX =>
498
            CPU_Next_State   <= LDX_C1;
499
            PC_Ctrl.Oper     <= PC_REV2;
500
            -- If auto-increment is disabled, use the specified register pair,
501
            --  otherwise, for an odd:even pair, and issue the first half of
502
            --  a UPP instruction to the ALU
503
            if( not Enable_Auto_Increment )then
504 156 jshamlet
              Address        <= Regfile(Reg_1) & Regfile(Reg);
505 151 jshamlet
            else
506
              Reg            := conv_integer(SubOp(2 downto 1) & '0');
507
              Reg_1          := conv_integer(SubOp(2 downto 1) & '1');
508 156 jshamlet
              Address        <= Regfile(Reg_1) & Regfile(Reg);
509 151 jshamlet
              if( SubOp(0) = '1' )then
510
                ALU_Ctrl.Oper<= ALU_UPP;
511
                ALU_Ctrl.Reg <= SubOp(2 downto 1) & '0';
512
              end if;
513
            end if;
514
 
515
          when OP_STA =>
516
            CPU_Next_State   <= STA_C1;
517
            Cache_Ctrl       <= CACHE_OPER1;
518
 
519
          when OP_STO =>
520
            CPU_Next_State   <= STO_C1;
521
            Cache_Ctrl       <= CACHE_OPER1;
522
            PC_Ctrl.Oper     <= PC_REV2;
523
            DP_Ctrl.Src      <= DATA_REG;
524
            DP_Ctrl.Reg      <= ACCUM;
525
 
526
          when OP_STX =>
527
            CPU_Next_State   <= STX_C1;
528
            Cache_Ctrl       <= CACHE_PREFETCH;
529
            PC_Ctrl.Oper     <= PC_REV2;
530
            DP_Ctrl.Src      <= DATA_REG;
531
            DP_Ctrl.Reg      <= ACCUM;
532
 
533
          when others =>
534
            PC_Ctrl.Oper     <= PC_INCR;
535
            ALU_Ctrl.Oper    <= Opcode;
536
            ALU_Ctrl.Reg     <= SubOp;
537
 
538
        end case;
539
 
540
-------------------------------------------------------------------------------
541
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
542
-------------------------------------------------------------------------------
543
 
544
      when BRN_C1 =>
545
        CPU_Next_State       <= INSTR_DECODE;
546
        Cache_Ctrl           <= CACHE_INSTR;
547
        PC_Ctrl.Oper         <= PC_INCR;
548 156 jshamlet
        if( Flags(Reg) = Opcode(0) )then
549 151 jshamlet
          CPU_Next_State     <= PIPE_FILL_0;
550
          Cache_Ctrl         <= CACHE_IDLE;
551
          PC_Ctrl.Offset     <= Operand1;
552
        end if;
553
 
554
      when DBNZ_C1 =>
555
        CPU_Next_State       <= INSTR_DECODE;
556
        Cache_Ctrl           <= CACHE_INSTR;
557
        PC_Ctrl.Oper         <= PC_INCR;
558 156 jshamlet
        if( Flags(FL_ZERO) = '0' )then
559 151 jshamlet
          CPU_Next_State     <= PIPE_FILL_0;
560
          Cache_Ctrl         <= CACHE_IDLE;
561
          PC_Ctrl.Offset     <= Operand1;
562
        end if;
563
 
564
      when JMP_C1 =>
565
        CPU_Next_State       <= JMP_C2;
566
        Cache_Ctrl           <= CACHE_OPER2;
567
 
568
      when JMP_C2 =>
569
        CPU_Next_State       <= PIPE_FILL_0;
570
        PC_Ctrl.Oper         <= PC_LOAD;
571
        PC_Ctrl.Addr         <= Operand2 & Operand1;
572
 
573
-------------------------------------------------------------------------------
574
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
575
-------------------------------------------------------------------------------
576
 
577
      when LDA_C1 =>
578
        CPU_Next_State       <= LDA_C2;
579
        Cache_Ctrl           <= CACHE_OPER2;
580
 
581
      when LDA_C2 =>
582
        CPU_Next_State       <= LDA_C3;
583 156 jshamlet
        Address              <= Operand2 & Operand1;
584 151 jshamlet
 
585
      when LDA_C3 =>
586
        CPU_Next_State       <= LDA_C4;
587
        PC_Ctrl.Oper         <= PC_INCR;
588
 
589
      when LDA_C4 =>
590
        CPU_Next_State       <= LDI_C1;
591
        Cache_Ctrl           <= CACHE_OPER1;
592
        PC_Ctrl.Oper         <= PC_INCR;
593
 
594
      when LDI_C1 =>
595
        CPU_Next_State       <= INSTR_DECODE;
596
        Cache_Ctrl           <= CACHE_INSTR;
597
        PC_Ctrl.Oper         <= PC_INCR;
598
        ALU_Ctrl.Oper        <= ALU_LDI;
599
        ALU_Ctrl.Reg         <= SubOp;
600
        ALU_Ctrl.Data        <= Operand1;
601
 
602
      when LDO_C1 =>
603
        CPU_Next_State       <= LDX_C1;
604
        PC_Ctrl.Oper         <= PC_INCR;
605
        if( Enable_Auto_Increment )then
606
          Reg                := conv_integer(SubOp(2 downto 1) & '0');
607
          Reg_1              := conv_integer(SubOp(2 downto 1) & '1');
608 156 jshamlet
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
609 151 jshamlet
          if( SubOp(0) = '1' )then
610
            ALU_Ctrl.Oper<= ALU_UPP;
611
            ALU_Ctrl.Reg <= SubOp(2 downto 1) & '0';
612
          end if;
613
        else
614 156 jshamlet
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
615 151 jshamlet
        end if;
616
 
617
      when LDX_C1 =>
618
        CPU_Next_State       <= LDX_C2;
619
        PC_Ctrl.Oper         <= PC_INCR;
620
 
621
      when LDX_C2 =>
622
        CPU_Next_State       <= LDX_C3;
623
        PC_Ctrl.Oper         <= PC_INCR;
624
        Cache_Ctrl           <= CACHE_OPER1;
625
 
626
      when LDX_C3 =>
627
        CPU_Next_State       <= INSTR_DECODE;
628
        Cache_Ctrl           <= CACHE_INSTR;
629
        PC_Ctrl.Oper         <= PC_INCR;
630
        ALU_Ctrl.Oper        <= ALU_LDX;
631
        ALU_Ctrl.Reg         <= ACCUM;
632
        ALU_Ctrl.Data        <= Operand1;
633
 
634
-------------------------------------------------------------------------------
635
-- Data Storage - Store to memory (STA, STO, STX)
636
-------------------------------------------------------------------------------
637
      when STA_C1 =>
638
        CPU_Next_State       <= STA_C2;
639
        Cache_Ctrl           <= CACHE_OPER2;
640
        DP_Ctrl.Src          <= DATA_REG;
641
        DP_Ctrl.Reg          <= SubOp;
642
 
643
      when STA_C2 =>
644
        CPU_Next_State       <= STA_C3;
645 156 jshamlet
        Address              <= Operand2 & Operand1;
646 151 jshamlet
        PC_Ctrl.Oper         <= PC_INCR;
647
 
648
      when STA_C3 =>
649
        CPU_Next_State       <= PIPE_FILL_2;
650
        Cache_Ctrl           <= CACHE_PREFETCH;
651 156 jshamlet
        PC_Ctrl.Oper         <= PC_INCR;
652 151 jshamlet
 
653
      when STO_C1 =>
654
        Cache_Ctrl           <= CACHE_PREFETCH;
655
        PC_Ctrl.Oper         <= PC_INCR;
656
        -- If auto-increment is disabled, just load the registers normally
657
        if( not Enable_Auto_Increment )then
658
          CPU_Next_State     <= PIPE_FILL_1;
659 156 jshamlet
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
660 151 jshamlet
        -- Otherwise, enforce the even register rule, and check the LSB to see
661
        --  if we should perform the auto-increment on the register pair
662
        else
663
          CPU_Next_State     <= PIPE_FILL_0;
664
          Reg                := conv_integer(SubOp(2 downto 1) & '0');
665
          Reg_1              := conv_integer(SubOp(2 downto 1) & '1');
666 156 jshamlet
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
667 151 jshamlet
          if( SubOp(0) = '1' )then
668
            CPU_Next_State   <= STO_C2;
669
            ALU_Ctrl.Oper    <= ALU_UPP;
670
            ALU_Ctrl.Reg     <= SubOp(2 downto 1) & '0';
671
          end if;
672
        end if;
673
 
674
      when STO_C2 =>
675
        CPU_Next_State       <= PIPE_FILL_1;
676
        PC_Ctrl.Oper         <= PC_INCR;
677
        ALU_Ctrl.Oper        <= ALU_UPP2;
678
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
679
 
680
      when STX_C1 =>
681
        PC_Ctrl.Oper         <= PC_INCR;
682
        -- If auto-increment is disabled, just load the registers normally
683
        if( not Enable_Auto_Increment )then
684
          CPU_Next_State     <= PIPE_FILL_1;
685 156 jshamlet
          Address            <= (Regfile(Reg_1) & Regfile(Reg));
686 151 jshamlet
        -- Otherwise, enforce the even register rule, and check the LSB to see
687
        --  if we should perform the auto-increment on the register pair
688
        else
689
          CPU_Next_State     <= PIPE_FILL_1;
690
          Reg                := conv_integer(SubOp(2 downto 1) & '0');
691
          Reg_1              := conv_integer(SubOp(2 downto 1) & '1');
692 156 jshamlet
          Address            <= (Regfile(Reg_1) & Regfile(Reg));
693 151 jshamlet
          if( SubOp(0) = '1' )then
694
            CPU_Next_State   <= STX_C2;
695
            ALU_Ctrl.Oper    <= ALU_UPP;
696
            ALU_Ctrl.Reg     <= SubOp(2 downto 1) & '0';
697
          end if;
698
        end if;
699
 
700
      when STX_C2 =>
701
        CPU_Next_State       <= PIPE_FILL_2;
702
        PC_Ctrl.Oper         <= PC_INCR;
703
        ALU_Ctrl.Oper        <= ALU_UPP2;
704
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
705
 
706
-------------------------------------------------------------------------------
707
-- Multi-Cycle Math Operations (UPP, MUL)
708
-------------------------------------------------------------------------------
709
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
710
      --  instruction/first operand, we have to return through PF2
711
 
712
      when MUL_C1 =>
713
        CPU_Next_State       <= PIPE_FILL_2;
714
        PC_Ctrl.Oper         <= PC_INCR;
715
        ALU_Ctrl.Oper        <= ALU_MUL;
716
 
717
      when UPP_C1 =>
718
        CPU_Next_State       <= PIPE_FILL_2;
719
        PC_Ctrl.Oper         <= PC_INCR;
720
        ALU_Ctrl.Oper        <= ALU_UPP2;
721
        ALU_Ctrl.Reg         <= SubOp_p1;
722
 
723
-------------------------------------------------------------------------------
724
-- Basic Stack Manipulation (PSH, POP, RSP)
725
-------------------------------------------------------------------------------
726
      when PSH_C1 =>
727
        CPU_Next_State       <= PIPE_FILL_1;
728 156 jshamlet
        Address              <= Stack_Ptr;
729 151 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
730
 
731
      when POP_C1 =>
732
        CPU_Next_State       <= POP_C2;
733 156 jshamlet
        Address              <= Stack_Ptr;
734 151 jshamlet
 
735
      when POP_C2 =>
736
        CPU_Next_State       <= POP_C3;
737
        PC_Ctrl.Oper         <= PC_INCR;
738
 
739
      when POP_C3 =>
740
        CPU_Next_State       <= POP_C4;
741
        Cache_Ctrl           <= CACHE_OPER1;
742
        PC_Ctrl.Oper         <= PC_INCR;
743
 
744
      when POP_C4 =>
745
        CPU_Next_State       <= INSTR_DECODE;
746
        Cache_Ctrl           <= CACHE_INSTR;
747
        PC_Ctrl.Oper         <= PC_INCR;
748
        ALU_Ctrl.Oper        <= ALU_POP;
749
        ALU_Ctrl.Reg         <= SubOp;
750
        ALU_Ctrl.Data        <= Operand1;
751
 
752
-------------------------------------------------------------------------------
753
-- Subroutines & Interrupts (RTS, JSR)
754
-------------------------------------------------------------------------------
755 156 jshamlet
      when WAIT_FOR_INT => -- For soft interrupts only, halt the Program_Ctr
756 151 jshamlet
        CPU_Next_State       <= WAIT_FOR_INT;
757
 
758
      when ISR_C1 =>
759
        CPU_Next_State       <= ISR_C2;
760 156 jshamlet
        Address              <= ISR_Addr;
761 151 jshamlet
        INT_Ctrl.Incr_ISR    <= '1';
762
 
763
      when ISR_C2 =>
764
        CPU_Next_State       <= ISR_C3;
765 156 jshamlet
        Address              <= ISR_Addr;
766 151 jshamlet
        DP_Ctrl.Src          <= DATA_FLAG;
767
 
768
      when ISR_C3 =>
769
        CPU_Next_State       <= JSR_C1;
770
        Cache_Ctrl           <= CACHE_OPER1;
771 156 jshamlet
        Address              <= Stack_Ptr;
772 151 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
773
        DP_Ctrl.Src          <= DATA_PC;
774
        DP_Ctrl.Reg          <= ACCUM+1;
775
        ALU_Ctrl.Oper        <= ALU_STP;
776
        ALU_Ctrl.Reg         <= INT_FLAG;
777
        Ack_D                <= '1';
778
 
779
      when JSR_C1 =>
780
        CPU_Next_State       <= JSR_C2;
781
        Cache_Ctrl           <= CACHE_OPER2;
782 156 jshamlet
        Address              <= Stack_Ptr;
783 151 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
784
        DP_Ctrl.Src          <= DATA_PC;
785
        DP_Ctrl.Reg          <= ACCUM;
786
 
787
      when JSR_C2 =>
788
        CPU_Next_State       <= PIPE_FILL_0;
789 156 jshamlet
        Address              <= Stack_Ptr;
790 151 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
791
        PC_Ctrl.Oper         <= PC_LOAD;
792
        PC_Ctrl.Addr         <= Operand2 & Operand1;
793
 
794
      when RTS_C1 =>
795
        CPU_Next_State       <= RTS_C2;
796 156 jshamlet
        Address              <= Stack_Ptr;
797 151 jshamlet
        SP_Ctrl.Oper         <= SP_POP;
798
 
799
      when RTS_C2 =>
800
        CPU_Next_State       <= RTS_C3;
801 156 jshamlet
        Address              <= Stack_Ptr;
802 151 jshamlet
        -- if this is an RTI, then we need to POP the flags
803
        if( SubOp = SOP_RTI )then
804
          SP_Ctrl.Oper       <= SP_POP;
805
        end if;
806
 
807
      when RTS_C3 =>
808
        CPU_Next_State       <= RTS_C4;
809
        Cache_Ctrl           <= CACHE_OPER1;
810
        -- It doesn't really matter what is on the address bus for RTS, while
811
        --  it does for RTI, so we make this the default
812 156 jshamlet
        Address              <= Stack_Ptr;
813 151 jshamlet
 
814
      when RTS_C4 =>
815
        CPU_Next_State       <= RTS_C5;
816
        Cache_Ctrl           <= CACHE_OPER2;
817
 
818
      when RTS_C5 =>
819
        CPU_Next_State       <= PIPE_FILL_0;
820
        PC_Ctrl.Oper         <= PC_LOAD;
821
        PC_Ctrl.Addr         <= Operand2 & Operand1;
822
        if( SubOp = SOP_RTI )then
823
          CPU_Next_State     <= RTI_C6;
824
          Cache_Ctrl         <= CACHE_OPER1;
825
        end if;
826
 
827
      when RTI_C6 =>
828
        CPU_Next_State       <= PIPE_FILL_1;
829
        PC_Ctrl.Oper         <= PC_INCR;
830
        ALU_Ctrl.Oper        <= ALU_RFLG;
831
        ALU_Ctrl.Data        <= Operand1;
832
        Int_RTI_D            <= '1';
833
 
834
-------------------------------------------------------------------------------
835
-- Debugging (BRK) Performs a 5-clock NOP
836
-------------------------------------------------------------------------------
837
      when BRK_C1 =>
838
        CPU_Next_State       <= PIPE_FILL_0;
839
 
840 156 jshamlet
      when others =>
841
        null;
842 151 jshamlet
    end case;
843
 
844
    -- Interrupt service routines can only begin during the decode and wait
845
    --  states to avoid corruption due to incomplete instruction execution
846
    if( Int_Req = '1' )then
847
      if( CPU_State = INSTR_DECODE or CPU_State = WAIT_FOR_INT )then
848 153 jshamlet
        -- Reset all of the sub-block controls to IDLE, to avoid unintended
849
        --  operation due to the current instruction
850
        ALU_Ctrl.Oper        <= ALU_IDLE;
851
        Cache_Ctrl           <= CACHE_IDLE;
852
        SP_Ctrl.Oper         <= SP_IDLE;
853 154 jshamlet
        DP_Ctrl.Src          <= DATA_IDLE; -- JSH 7/20
854 155 jshamlet
        INT_Ctrl.Soft_Ints   <= (others => '0'); -- JSH 7/22
855 153 jshamlet
        -- Rewind the PC by 3 to compensate for the pipeline registers
856 151 jshamlet
        PC_Ctrl.Oper         <= PC_INCR;
857
        PC_Ctrl.Offset       <= x"FF";
858 153 jshamlet
        CPU_Next_State       <= ISR_C1;
859
 
860 151 jshamlet
      end if;
861
    end if;
862
 
863
  end process;
864
 
865 156 jshamlet
  -- We need to infer a hardware multipler, so we create a special clocked
866
  --  process with no reset or clock enable
867
  Multiplier_proc: process( Clock )
868 151 jshamlet
  begin
869 156 jshamlet
    if( rising_edge(Clock) )then
870
      Mult                   <= Regfile(0) *
871
                                Regfile(conv_integer(ALU_Ctrl.Reg));
872
    end if;
873
  end process;
874
 
875
-------------------------------------------------------------------------------
876
-- Registered portion of CPU finite state machine
877
-------------------------------------------------------------------------------
878
  CPU_Regs: process( Reset, Clock )
879
    variable Offset_SX       : ADDRESS_TYPE;
880
    variable i_Ints          : INTERRUPT_BUNDLE := (others => '0');
881
    variable Sum             : std_logic_vector(8 downto 0) := "000000000";
882
    variable Index           : integer range 0 to 7 := 0;
883
    variable Temp            : std_logic_vector(8 downto 0);
884
  begin
885 151 jshamlet
    if( Reset = Reset_Level )then
886
      CPU_State              <= PIPE_FILL_0;
887
      Opcode                 <= OP_INC;
888
      SubOp                  <= ACCUM;
889
      SubOp_p1               <= ACCUM;
890
      Operand1               <= x"00";
891
      Operand2               <= x"00";
892
      Instr_Prefetch         <= '0';
893
      Prefetch               <= x"00";
894
 
895 156 jshamlet
      Wr_Data                <= (others => '0');
896
      Wr_Enable              <= '0';
897
      Rd_Enable              <= '1';
898
 
899
      Program_Ctr            <= Program_Start_Addr;
900
      Stack_Ptr              <= Stack_Start_Addr;
901
 
902 151 jshamlet
      Ack_Q                  <= '0';
903
      Ack_Q1                 <= '0';
904
      Int_Ack                <= '0';
905
      Int_RTI                <= '0';
906
 
907 156 jshamlet
      Int_Req                <= '0';
908
      Pending                <= x"00";
909
      Wait_for_FSM           <= '0';
910
      Int_Mask               <= Default_Interrupt_Mask(7 downto 1) & '1';
911
      ISR_Addr               <= INT_VECTOR_0;
912
      for i in 0 to 8 loop
913
        History(i)           <= 0;
914
      end loop;
915
      Hst_Ptr                <= 0;
916
 
917
      for i in 0 to 7 loop
918
        Regfile(i)           <= (others => '0');
919
      end loop;
920
      Flags                  <= x"00";
921
 
922 151 jshamlet
    elsif( rising_edge(Clock) )then
923 156 jshamlet
      Wr_Enable              <= '0';
924
      Rd_Enable              <= '0';
925
 
926 151 jshamlet
      if( Halt = '0' )then
927 156 jshamlet
        Rd_Enable            <= '1';
928
-------------------------------------------------------------------------------
929
-- Instruction/Operand caching for pipelined memory access
930
-------------------------------------------------------------------------------
931 151 jshamlet
        CPU_State            <= CPU_Next_State;
932
        case Cache_Ctrl is
933
          when CACHE_INSTR =>
934
            Opcode           <= Rd_Data(7 downto 3);
935
            SubOp            <= Rd_Data(2 downto 0);
936
            SubOp_p1         <= Rd_Data(2 downto 0) + 1;
937
            if( Instr_Prefetch = '1' )then
938
              Opcode         <= Prefetch(7 downto 3);
939
              SubOp          <= Prefetch(2 downto 0);
940
              SubOp_p1       <= Prefetch(2 downto 0) + 1;
941
              Instr_Prefetch <= '0';
942
            end if;
943 156 jshamlet
 
944 151 jshamlet
          when CACHE_OPER1 =>
945
            Operand1         <= Rd_Data;
946 156 jshamlet
 
947 151 jshamlet
          when CACHE_OPER2 =>
948
            Operand2         <= Rd_Data;
949 156 jshamlet
 
950 151 jshamlet
          when CACHE_PREFETCH =>
951
            Prefetch         <= Rd_Data;
952
            Instr_Prefetch   <= '1';
953 156 jshamlet
 
954 151 jshamlet
          when CACHE_IDLE =>
955
            null;
956
        end case;
957
 
958 156 jshamlet
-------------------------------------------------------------------------------
959
-- Program Counter
960
-------------------------------------------------------------------------------
961
        Offset_SX(15 downto 8) := (others => PC_Ctrl.Offset(7));
962
        Offset_SX(7 downto 0)  := PC_Ctrl.Offset;
963 151 jshamlet
 
964 156 jshamlet
        case PC_Ctrl.Oper is
965
          when PC_IDLE =>
966
            null;
967
 
968
          when PC_REV1 =>
969
            Program_Ctr      <= Program_Ctr - 1;
970
 
971
          when PC_REV2 =>
972
            Program_Ctr      <= Program_Ctr - 2;
973
 
974
          when PC_INCR =>
975
            Program_Ctr      <= Program_Ctr + Offset_SX - 2;
976
 
977
          when PC_LOAD =>
978
            Program_Ctr      <= PC_Ctrl.Addr;
979
 
980
          when others =>
981
            null;
982
        end case;
983
 
984 151 jshamlet
-------------------------------------------------------------------------------
985 156 jshamlet
-- (Write) Data Path
986 151 jshamlet
-------------------------------------------------------------------------------
987 156 jshamlet
        case DP_Ctrl.Src is
988
          when DATA_IDLE =>
989
            null;
990 151 jshamlet
 
991 156 jshamlet
          when DATA_REG =>
992
            Wr_Enable        <= '1';
993
            Rd_Enable        <= '0';
994
            Wr_Data          <= Regfile(conv_integer(DP_Ctrl.Reg));
995 151 jshamlet
 
996 156 jshamlet
          when DATA_FLAG =>
997
            Wr_Enable        <= '1';
998
            Rd_Enable        <= '0';
999
            Wr_Data          <= Flags;
1000
 
1001
          when DATA_PC =>
1002
            Wr_Enable        <= '1';
1003
            Rd_Enable        <= '0';
1004
            Wr_Data          <= Program_Ctr(15 downto 8);
1005
            if( DP_Ctrl.Reg = ACCUM )then
1006
              Wr_Data        <= Program_Ctr(7 downto 0);
1007
            end if;
1008
 
1009
          when others =>
1010
            null;
1011
        end case;
1012
 
1013
-------------------------------------------------------------------------------
1014
-- Stack Pointer
1015
-------------------------------------------------------------------------------
1016
        case SP_Ctrl.Oper is
1017
          when SP_IDLE =>
1018
            null;
1019
 
1020
          when SP_RSET =>
1021 151 jshamlet
-- The original RSP instruction simply reset the stack pointer to the preset
1022
--  address set at compile time. However, with little extra effort, we can
1023
--  modify the instruction to allow the stack pointer to be moved anywhere in
1024
--  the memory map. Since RSP can't have an sub-opcode, R1:R0 was chosen as
1025
--  a fixed source
1026 156 jshamlet
            Stack_Ptr        <= Stack_Start_Addr;
1027
            if( Allow_Stack_Address_Move )then
1028
              Stack_Ptr      <= Regfile(1) & Regfile(0);
1029
            end if;
1030 151 jshamlet
 
1031 156 jshamlet
          when SP_POP  =>
1032
            Stack_Ptr        <= Stack_Ptr + 1;
1033 151 jshamlet
 
1034 156 jshamlet
          when SP_PUSH =>
1035
            Stack_Ptr        <= Stack_Ptr - 1;
1036 151 jshamlet
 
1037 156 jshamlet
          when others =>
1038
            null;
1039 151 jshamlet
 
1040 156 jshamlet
        end case;
1041
 
1042
-------------------------------------------------------------------------------
1043
-- Interrupt Controller
1044
-------------------------------------------------------------------------------
1045
        -- The interrupt control mask is always sourced out of R0
1046
        if( INT_Ctrl.Mask_Set = '1' )then
1047
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
1048
        end if;
1049
 
1050
        -- Combine external and internal interrupts, and mask the OR or the two
1051
        --  with the mask. Record any incoming interrupts to the pending buffer
1052
        i_Ints               := (Interrupts or INT_Ctrl.Soft_Ints) and
1053
                                Int_Mask;
1054
        if( i_Ints > 0 )then
1055
          Pending            <= i_Ints;
1056
        end if;
1057
 
1058
        -- Only mess with interrupt signals while the CPU core is not currently
1059
        --  working with, or loading, an ISR address
1060
        if( Wait_for_FSM = '0' and Pending > 0 )then
1061
          if(   Pending(0) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 0))then
1062
            ISR_Addr         <= INT_VECTOR_0;
1063
            Pending(0)       <= '0';
1064
            History(Hst_Ptr+1) <= 0;
1065
            Hst_Ptr          <= Hst_Ptr + 1;
1066
            Wait_for_FSM     <= '1';
1067
          elsif(Pending(1) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 1))then
1068
            ISR_Addr         <= INT_VECTOR_1;
1069
            Pending(1)       <= '0';
1070
            History(Hst_Ptr+1) <= 1;
1071
            Hst_Ptr          <= Hst_Ptr + 1;
1072
            Wait_for_FSM     <= '1';
1073
          elsif(Pending(2) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 2))then
1074
            ISR_Addr         <= INT_VECTOR_2;
1075
            Pending(2)       <= '0';
1076
            History(Hst_Ptr+1) <= 1;
1077
            Hst_Ptr          <= Hst_Ptr + 1;
1078
            Wait_for_FSM     <= '1';
1079
          elsif(Pending(3) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 3))then
1080
            ISR_Addr         <= INT_VECTOR_3;
1081
            Pending(3)       <= '0';
1082
            History(Hst_Ptr+1) <= 3;
1083
            Hst_Ptr          <= Hst_Ptr + 1;
1084
            Wait_for_FSM     <= '1';
1085
          elsif(Pending(4) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 4))then
1086
            ISR_Addr         <= INT_VECTOR_4;
1087
            Pending(4)       <= '0';
1088
            History(Hst_Ptr+1) <= 4;
1089
            Hst_Ptr          <= Hst_Ptr + 1;
1090
            Wait_for_FSM     <= '1';
1091
          elsif(Pending(5) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 5))then
1092
            ISR_Addr         <= INT_VECTOR_5;
1093
            Pending(5)       <= '0';
1094
            History(Hst_Ptr+1) <= 5;
1095
            Hst_Ptr          <= Hst_Ptr + 1;
1096
            Wait_for_FSM     <= '1';
1097
          elsif(Pending(6) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 6))then
1098
            ISR_Addr         <= INT_VECTOR_6;
1099
            Pending(6)       <= '0';
1100
            History(Hst_Ptr+1) <= 6;
1101
            Hst_Ptr          <= Hst_Ptr + 1;
1102
            Wait_for_FSM     <= '1';
1103
          elsif(Pending(7) = '1' and (Hst_Ptr = 0 or History(Hst_Ptr) > 7))then
1104
            ISR_Addr         <= INT_VECTOR_7;
1105
            Pending(7)       <= '0';
1106
            History(Hst_Ptr+1) <= 7;
1107
            Hst_Ptr          <= Hst_Ptr + 1;
1108
            Wait_for_FSM     <= '1';
1109
          end if;
1110
        end if;
1111
 
1112
        -- Reset the Wait_for_FSM flag on Int_Ack
1113
        Ack_Q                <= Ack_D;
1114
        Ack_Q1               <= Ack_Q;
1115
        Int_Ack              <= Ack_Q1;
1116
        if( Int_Ack = '1' )then
1117
          Wait_for_FSM       <= '0';
1118
        end if;
1119
 
1120
        Int_Req              <= Wait_for_FSM and (not Int_Ack);
1121
 
1122
        Int_RTI              <= Int_RTI_D;
1123
        if( Int_RTI = '1' and Hst_Ptr > 0 )then
1124
          Hst_Ptr           <= Hst_Ptr - 1;
1125
        end if;
1126
 
1127
        -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1128
        --  lower half of the address.
1129
        if( INT_Ctrl.Incr_ISR = '1' )then
1130
          ISR_Addr           <= ISR_Addr + 1;
1131
        end if;
1132
 
1133
-------------------------------------------------------------------------------
1134
-- ALU (Arithmetic / Logic Unit)
1135
-------------------------------------------------------------------------------
1136
        Temp                 := (others => '0');
1137
        Index                := conv_integer(ALU_Ctrl.Reg);
1138
 
1139
        case ALU_Ctrl.Oper is
1140
          when ALU_INC | ALU_UPP => -- Rn = Rn + 1 : Flags N,C,Z
1141
            Sum              := ("0" & x"01") +
1142
                                ("0" & Regfile(Index));
1143
            Flags(FL_CARRY)  <= Sum(8);
1144
            Regfile(Index)   <= Sum(7 downto 0);
1145
           -- ALU_INC and ALU_UPP are essentially the same, except that ALU_UPP
1146
           --  doesn't set the N or Z flags. Note that the MSB can be used to
1147
           --  distinguish between the two ALU modes.
1148
           if( ALU_Ctrl.Oper(4) = '0' )then
1149
             Flags(FL_ZERO)  <= '0';
1150
             if( Sum(7 downto 0) = 0 )then
1151
               Flags(FL_ZERO)<= '1';
1152
             end if;
1153
             Flags(FL_NEG)   <= Sum(7);
1154
           end if;
1155
 
1156
          when ALU_UPP2 => -- Rn = Rn + C
1157
            Sum              := ("0" & x"00") +
1158
                                ("0" & Regfile(Index)) +
1159
                                Flags(FL_CARRY);
1160
            Flags(FL_CARRY)  <= Sum(8);
1161
            Regfile(Index)   <= Sum(7 downto 0);
1162
 
1163
          when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1164
            Sum              := ("0" & Regfile(0)) +
1165
                                ("0" & Regfile(Index)) +
1166
                                Flags(FL_CARRY);
1167
            Flags(FL_ZERO)   <= '0';
1168
            if( Sum(7 downto 0) = 0 )then
1169
              Flags(FL_ZERO) <= '1';
1170
            end if;
1171
            Flags(FL_CARRY)  <= Sum(8);
1172
            Flags(FL_NEG)    <= Sum(7);
1173
            Regfile(0)       <= Sum(7 downto 0);
1174
 
1175
          when ALU_TX0 => -- R0 = Rn : Flags N,Z
1176
            Temp                 := "0" & Regfile(Index);
1177
            Flags(FL_ZERO)   <= '0';
1178
            if( Temp(7 downto 0) = 0 )then
1179
              Flags(FL_ZERO) <= '1';
1180
            end if;
1181
            Flags(FL_NEG)    <= Temp(7);
1182
            Regfile(0)       <= Temp(7 downto 0);
1183
 
1184
          when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1185
            Temp(7 downto 0) := Regfile(0) or Regfile(Index);
1186
            Flags(FL_ZERO)   <= '0';
1187
            if( Temp(7 downto 0) = 0 )then
1188
              Flags(FL_ZERO) <= '1';
1189
            end if;
1190
            Flags(FL_NEG)    <= Temp(7);
1191
            Regfile(0)       <= Temp(7 downto 0);
1192
 
1193
          when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1194
            Temp(7 downto 0) := Regfile(0) and Regfile(Index);
1195
            Flags(FL_ZERO)   <= '0';
1196
            if( Temp(7 downto 0) = 0 )then
1197
              Flags(FL_ZERO) <= '1';
1198
            end if;
1199
            Flags(FL_NEG)    <= Temp(7);
1200
            Regfile(0)       <= Temp(7 downto 0);
1201
 
1202
          when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1203
            Temp(7 downto 0) := Regfile(0) xor Regfile(Index);
1204
            Flags(FL_ZERO)   <= '0';
1205
            if( Temp(7 downto 0) = 0 )then
1206
              Flags(FL_ZERO) <= '1';
1207
            end if;
1208
            Flags(FL_NEG)    <= Temp(7);
1209
            Regfile(0)       <= Temp(7 downto 0);
1210
 
1211
          when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1212
            Temp             := Regfile(Index) & Flags(FL_CARRY);
1213
            Flags(FL_ZERO)   <= '0';
1214
            if( Temp(7 downto 0) = 0 )then
1215
              Flags(FL_ZERO) <= '1';
1216
            end if;
1217
            Flags(FL_CARRY)  <= Temp(8);
1218
            Flags(FL_NEG)    <= Temp(7);
1219
            Regfile(Index)   <= Temp(7 downto 0);
1220
 
1221
          when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1222
            Temp             := Regfile(Index)(0) & Flags(FL_CARRY) &
1223
                                Regfile(Index)(7 downto 1);
1224
            Flags(FL_ZERO)   <= '0';
1225
            if( Temp(7 downto 0) = 0 )then
1226
              Flags(FL_ZERO) <= '1';
1227
            end if;
1228
            Flags(FL_CARRY)  <= Temp(8);
1229
            Flags(FL_NEG)    <= Temp(7);
1230
            Regfile(Index)   <= Temp(7 downto 0);
1231
 
1232
          when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1233
            Sum              := ("0" & Regfile(Index)) +
1234
                                ("0" & x"FF");
1235
            Flags(FL_ZERO)   <= '0';
1236
            if( Sum(7 downto 0) = 0 )then
1237
              Flags(FL_ZERO) <= '1';
1238
            end if;
1239
            Flags(FL_CARRY)  <= Sum(8);
1240
            Flags(FL_NEG)    <= Sum(7);
1241
            Regfile(Index)   <= Sum(7 downto 0);
1242
 
1243
          when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1244
            Sum              := ("0" & Regfile(0)) +
1245
                                ("0" & (not Regfile(Index))) +
1246
                                Flags(FL_CARRY);
1247
            Flags(FL_ZERO)   <= '0';
1248
            if( Sum(7 downto 0) = 0 )then
1249
              Flags(FL_ZERO) <= '1';
1250
            end if;
1251
            Flags(FL_CARRY)  <= Sum(8);
1252
            Flags(FL_NEG)    <= Sum(7);
1253
            Regfile(0)       <= Sum(7 downto 0);
1254
 
1255
          when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1256
            Sum              := ("0" & Regfile(0)) +
1257
                                ("0" & Regfile(Index));
1258
            Flags(FL_CARRY)  <= Sum(8);
1259
            Regfile(0)       <= Sum(7 downto 0);
1260
            Flags(FL_ZERO)   <= '0';
1261
            if( Sum(7 downto 0) = 0 )then
1262
              Flags(FL_ZERO) <= '1';
1263
            end if;
1264
            Flags(FL_NEG)    <= Sum(7);
1265
 
1266
          when ALU_STP => -- Sets bit(n) in the Flags register
1267
            Flags(Index)     <= '1';
1268
 
1269
          when ALU_BTT => -- Z = !R0(N), N = R0(7)
1270
            Flags(FL_ZERO)   <= not Regfile(0)(Index);
1271
            Flags(FL_NEG)    <= Regfile(0)(7);
1272
 
1273
          when ALU_CLP => -- Clears bit(n) in the Flags register
1274
            Flags(Index)     <= '0';
1275
 
1276
          when ALU_T0X => -- Rn = R0 : Flags N,Z
1277
            Temp             := "0" & Regfile(0);
1278
            Flags(FL_ZERO)   <= '0';
1279
            if( Temp(7 downto 0) = 0 )then
1280
              Flags(FL_ZERO) <= '1';
1281
            end if;
1282
            Flags(FL_NEG)    <= Temp(7);
1283
            Regfile(Index)   <= Temp(7 downto 0);
1284
 
1285
          when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1286
            Sum              := ("0" & Regfile(0)) +
1287
                                ("0" & (not Regfile(Index))) +
1288
                                '1';
1289
            Flags(FL_ZERO)   <= '0';
1290
            if( Sum(7 downto 0) = 0 )then
1291
              Flags(FL_ZERO) <= '1';
1292
            end if;
1293
            Flags(FL_CARRY)  <= Sum(8);
1294
            Flags(FL_NEG)    <= Sum(7);
1295
 
1296
          when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1297
            Regfile(0)       <= Mult(7 downto 0);
1298
            Regfile(1)       <= Mult(15 downto 8);
1299
            Flags(FL_ZERO)   <= '0';
1300
            if( Mult = 0 )then
1301
              Flags(FL_ZERO) <= '1';
1302
            end if;
1303
 
1304
          when ALU_LDI | ALU_POP => -- Rn <= Data : Flags N,Z
1305
            -- The POP instruction doesn't alter the flags, so we need to check
1306
            if( ALU_Ctrl.Oper = ALU_LDI )then
1307
              Flags(FL_ZERO) <= '0';
1308
              if( ALU_Ctrl.Data = 0 )then
1309
                Flags(FL_ZERO) <= '1';
1310
              end if;
1311
              Flags(FL_NEG)  <= ALU_Ctrl.Data(7);
1312
            end if;
1313
            Regfile(Index)   <= ALU_Ctrl.Data;
1314
 
1315
          when ALU_LDX => -- R0 <= Data : Flags N,Z
1316
            Flags(FL_ZERO)   <= '0';
1317
            if( ALU_Ctrl.Data = 0 )then
1318
              Flags(FL_ZERO) <= '1';
1319
            end if;
1320
            Flags(FL_NEG)    <= ALU_Ctrl.Data(7);
1321
            Regfile(0)       <= ALU_Ctrl.Data;
1322
 
1323
          when ALU_RFLG =>
1324
            Flags            <= ALU_Ctrl.Data;
1325
 
1326
          when others =>
1327
            null;
1328
        end case;
1329
 
1330
      end if;
1331
    end if;
1332
  end process;
1333
 
1334
end architecture;

powered by: WebSVN 2.1.0

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