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 162

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

powered by: WebSVN 2.1.0

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