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 153

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

powered by: WebSVN 2.1.0

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