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 155

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

powered by: WebSVN 2.1.0

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