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 269

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

Line No. Rev Author Line
1 185 jshamlet
-- Copyright (c)2006, 2011, 2012, 2013, 2015, 2019, 2020 Jeremy Seth Henry
2 169 jshamlet
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 194 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 169 jshamlet
--
24 181 jshamlet
-- VHDL Units :  o8_cpu
25 169 jshamlet
-- Description:  VHDL model of a RISC 8-bit processor core loosely based on the
26
--            :   V8/ARC uRISC instruction set. Requires Open8_pkg.vhd
27
--            :
28
-- Notes      :  Generic definitions
29
--            :
30
--            :  Program_Start_Addr sets the initial value of the program
31
--            :   counter.
32
--            :
33
--            :  ISR_Start_Addr sets the location of the interrupt service
34
--            :   vector table. There are 8 service vectors, or 16 bytes, which
35
--            :   must be allocated to either ROM or RAM.
36
--            :
37
--            :  Stack_Start_Address sets the initial (reset) value of the
38
--            :   stack pointer. Also used for the RSP instruction if
39
--            :   Allow_Stack_Address_Move is false.
40
--            :
41
--            :  Allow_Stack_Address_Move, when set true, allows the RSP to be
42 181 jshamlet
--            :   programmed via thet RSP instruction. If enabled, the
43
--            :   instruction changes into TSX or TXS based on the flag
44
--            :   specified by Stack_Xfer_Flag. If the flag is '0', RSP will
45
--            :   copy the current stack pointer to R1:R0 (TSX). If the flag
46
--            :   is '1', RSP will copy R1:R0 to the stack pointer (TXS). This
47
--            :   allows the processor to backup and restore stack pointers
48
--            :   in a multi-process environment. Note that no flags are
49
--            :   modified by either form of this instruction.
50 169 jshamlet
--            :
51 181 jshamlet
--            :  Stack_Xfer_Flag instructs the core to use the specified ALU
52
--            :   flag to alter the behavior of the RSP instruction when
53 256 jshamlet
--            :   Allow_Stack_Address_Move is set TRUE, otherwise it's ignored.
54 181 jshamlet
--            :   While technically any of the status bits may be used, the
55
--            :   intent was to use FL_GP[1,2,3,4], as these are not modified
56
--            :   by ordinary ALU operations.
57
--            :
58 169 jshamlet
--            :  The Enable_Auto_Increment generic can be used to modify the
59
--            :   indexed instructions such that specifying an odd register
60
--            :   will use the next lower register pair, post-incrementing the
61
--            :   value in that pair. IOW, specifying STX R1 will instead
62
--            :   result in STX R0++, or R0 = {R1:R0}; {R1:R0} + 1
63
--            :
64
--            :  BRK_Implements_WAI modifies the BRK instruction such that it
65
--            :   triggers the wait for interrupt state, but without triggering
66
--            :   a soft interrupt in lieu of its normal behavior, which is to
67
--            :   insert several dead clock cycles - essentially a long NOP
68
--            :
69
--            :  Enable_NMI overrides the mask bit for interrupt 0, creating a
70
--            :   non-maskable interrupt at the highest priority. To remain
71
--            :   true to the original core, this should be set false.
72
--            :
73 260 jshamlet
--            :  Sequential_Interrupts, when set, prevents interrupt service
74
--            :   routines from  being interrupted by postponing an later
75
--            :   interrupts until the I bit is cleared (usually with an RTI,
76
--            :   but a CLP PSR_I will also work). This is potentially
77
--            :   dangerous, as it means a lower-priority ISR can "hog" the CPU
78
--            :   by failing to return. However, it can also prevent the
79
--            :   condition of an ISR interrupting itself until it causes a
80
--            :   memory fault. (For example, an interrupt source that whose
81
--            :   period is shorter than the ISR service time) Note that this
82
--            :   setting alters the way the pending logic works, so it affects
83
--            :   all interrupts, including the NMI. If this is set, special
84
--            :   care should be taken to make sure ISRs are short and always
85
--            :   execute an RTI at the end.
86
--            :
87 188 jshamlet
--            :  RTI_Ignores_GP_Flags alters the set of flag bits restored
88
--            :   after an interrupt. By default, all of the flag bits are put
89
--            :   back to their original state. If this flag is set true, only
90
--            :   the lower four bits are restored, allowing ISR code to alter
91
--            :   the GP flags persistently.
92
--            :
93 244 jshamlet
--            :  Supervisor_Mode, when set, disables the STP PSR_I instruction
94
--            :   preventing code from setting the I bit. When enabled, only
95
--            :   interrupts can set the I bit, allowing for more robust memory
96
--            :   protection by preventing errant code execution from
97
--            :   inadvertently entering an interrupt state.
98
--            :
99 248 jshamlet
--            :   This setting also sets I bit at startup so that any
100
--            :   initialization code may be run in an ISR context, initially
101
--            :   bypassing memory protection. Init code should clear the I bit
102
--            :   when done;
103 244 jshamlet
--            :
104 255 jshamlet
--            :  Unsigned_Index_Offsets alters the way offsets are added to
105 260 jshamlet
--            :   [Rn+1:Rn] during LDO/STO instructions. The original, default
106 255 jshamlet
--            :   behavior treats these offsets as signed values, allowing
107
--            :   instructions to offset by -128 to +127 from [Rn+1:Rn].
108
--            :   Setting this generic to TRUE will switch to unsigned offsets,
109
--            :   switching the range to 0 to 255 instead.
110
--            :
111 169 jshamlet
--            :  Default_Interrupt_Mask sets the intial/reset value of the
112
--            :   interrupt mask. To remain true to the original core, which
113
--            :   had no interrupt mask, this should be set to x"FF". Otherwise
114
--            :   it can be initialized to any value. Note that Enable_NMI
115
--            :   will logically force the LSB high.
116 172 jshamlet
--            :
117 169 jshamlet
--            :  Reset_Level determines whether the processor registers reset
118
--            :   on a high or low level from higher logic.
119
--            :
120
--            : Architecture notes
121
--            :  This model deviates from the original ISA in a few important
122
--            :   ways.
123
--            :
124
--            :  First, there is only one set of registers. Interrupt service
125
--            :   routines must explicitely preserve context since the the
126
--            :   hardware doesn't. This was done to decrease size and code
127
--            :   complexity. Older code that assumes this behavior will not
128
--            :   execute correctly on this processor model.
129
--            :
130
--            :  Second, this model adds an additional pipeline stage between
131
--            :   the instruction decoder and the ALU. Unfortunately, this
132
--            :   means that the instruction stream has to be restarted after
133
--            :   any math instruction is executed, implying that any ALU
134
--            :   instruction now has a latency of 2 instead of 0. The
135
--            :   advantage is that the maximum frequency has gone up
136
--            :   significantly, as the ALU code is vastly more efficient.
137
--            :   As an aside, this now means that all math instructions,
138
--            :   including MUL (see below) and UPP have the same instruction
139
--            :   latency.
140
--            :
141
--            :  Third, the original ISA, also a soft core, had two reserved
142
--            :   instructions, USR and USR2. These have been implemented as
143
--            :   DBNZ, and MUL respectively.
144
--            :
145
--            :  DBNZ decrements the specified register and branches if the
146
--            :   result is non-zero. The instruction effectively executes a
147
--            :   DEC Rn instruction prior to branching, so the same flags will
148
--            :   be set.
149
--            :
150
--            :  MUL places the result of R0 * Rn into R1:R0. Instruction
151
--            :   latency is identical to other ALU instructions. Only the Z
152
--            :   flag is set, since there is no defined overflow or "negative
153
--            :   16-bit values"
154
--            :
155
--            :  Fourth, indexed load/store instructions now have an (optional)
156
--            :   ability to post-increment their index registers. If enabled,
157
--            :   using an odd operand for LDO,LDX, STO, STX will cause the
158
--            :   register pair to be incremented after the storage access.
159
--            :
160
--            :  Fifth, the RSP instruction has been (optionally) altered to
161
--            :   allow the stack pointer to be sourced from R1:R0.
162
--            :
163
--            :  Sixth, the BRK instruction can optionally implement a WAI,
164
--            :   which is the same as the INT instruction without the soft
165
--            :   interrupt, as a way to put the processor to "sleep" until the
166
--            :   next external interrupt.
167
--            :
168
--            :  Seventh, the original CPU model had 8 non-maskable interrupts
169
--            :   with priority. This model has the same 8 interrupts, but
170 172 jshamlet
--            :   allows software to mask them (with an additional option to
171 169 jshamlet
--            :   override the highest priority interrupt, making it the NMI.)
172
--            :
173
--            :  Lastly, previous unmapped instructions in the OP_STK opcode
174
--            :   were repurposed to support a new interrupt mask.
175
--            :   SMSK and GMSK transfer the contents of R0 (accumulator)
176
--            :   to/from the interrupt mask register. SMSK is immediate, while
177
--            :   GMSK has the same overhead as a math instruction.
178
--
179
-- Revision History
180
-- Author          Date     Change
181
------------------ -------- ---------------------------------------------------
182
-- Seth Henry      07/19/06 Design Start
183
-- Seth Henry      01/18/11 Fixed BTT instruction to match V8
184
-- Seth Henry      07/22/11 Fixed interrupt transition logic to avoid data
185
--                           corruption issues.
186
-- Seth Henry      07/26/11 Optimized logic in ALU, stack pointer, and data
187
--                           path sections.
188
-- Seth Henry      07/27/11 Optimized logic for timing, merged blocks into
189
--                           single entity.
190
-- Seth Henry      09/20/11 Added BRK_Implements_WAI option, allowing the
191
--                           processor to wait for an interrupt instead of the
192
--                           normal BRK behavior.
193 187 jshamlet
-- Seth Henry      12/20/11 Modified core to allow WAI_Cx state to idle
194 169 jshamlet
--                           the bus entirely (Rd_Enable is low)
195
-- Seth Henry      02/03/12 Replaced complex interrupt controller with simpler,
196
--                           faster logic that simply does priority encoding.
197
-- Seth Henry      08/06/13 Removed HALT functionality
198
-- Seth Henry      10/29/15 Fixed inverted carry logic in CMP and SBC instrs
199 182 jshamlet
-- Seth Henry      12/19/19 Renamed to o8_cpu to fit "theme"
200 181 jshamlet
-- Seth Henry      03/09/20 Modified RSP instruction to work with a CPU flag
201
--                           allowing true backup/restore of the stack pointer
202 182 jshamlet
-- Seth Henry      03/11/20 Split the address logic from the main state machine
203
--                           in order to simplify things and eliminate
204
--                           redundancies. Came across and fixed a problem with
205
--                           the STO instruction when Enable_Auto_Increment is
206
--                           NOT set.
207 185 jshamlet
-- Seth Henry      03/12/20 Rationalized the naming of the CPU flags to match
208
--                           the assembler names. Also fixed an issue where
209
--                           the I bit wasn't being cleared after interrupts.
210
--                          Simplified the program counter logic to only use
211
--                           the offset for increments, redefining the
212
--                           original modes as fixed offset values.
213
--                          Modified the ALU section with a new ALU operation
214
--                           for GMSK. This allowed the .data field to be
215
--                           removed and Operand1 used in its place, which
216
--                           simplified the logic a great deal.
217 187 jshamlet
-- Seth Henry      03/16/20 Added CPU_Halt input back, only now as an input to
218
--                           the instruction decode state, where it acts as a
219
--                           modified form of the BRK instruction that holds
220
--                           state until CPU_Halt is deasserted. This has a
221
--                           much smaller impact on Fmax/complexity than the
222
--                           original clock enable, but imposes a mild impact
223
--                           due to the need to reset the instruction pipeline
224 188 jshamlet
-- Seth Henry      03/17/20 Added generic to control whether RTI full restores
225
--                           the flags, including the general purpose ones, or
226
--                           only the core ALU flags (Z, N, and C). Also
227
--                           brought out copies of the GP flags for external
228
--                           connection.
229 210 jshamlet
-- Seth Henry      04/09/20 Added a compile time setting to block interrupts
230
--                           while the I bit is set to avoid reentering ISRs
231
--                           This may slightly affect timing, as this will
232
--                           potentially block higher priority interrupts
233
--                           until the lower priority ISR returns or clears
234
--                           the I bit.
235
--                          Also added the I bit to the exported flags for
236
--                           use in memory protection schemes.
237 224 jshamlet
-- Seth Henry      04/16/20 Modified to use new Open8 bus record. Also added
238 225 jshamlet
--                           reset and usec_tick logic to drive utility
239
--                           signals. Also added Halt_Ack output.
240 244 jshamlet
-- Seth Henry      05/20/20 Added two new generics to alter the way the I bit
241
--                           is handled. The Supervisor_Mode setting disables
242
--                           STP PSR_I from being executed, preventing it
243
--                           from being set outside of an ISR. The
244
--                           Default_Int_Flag setting allows the I bit to
245
--                           start set so that initialization code can run,
246
--                           but not be hijacked later to corrupt any memory
247
--                           write protection later.
248 245 jshamlet
-- Seth Henry      05/21/20 Supervisor_Mode now protects the interrupt mask
249
--                           and stack pointer as well.
250 248 jshamlet
-- Seth Henry      05/24/20 Removed the Default_Int_Flag, as it is covered by
251
--                           Supervisor_Mode. If Supervisor_Mode isn't set,
252
--                           code can simply use STP to set the bit
253 252 jshamlet
-- Seth Henry      06/09/20 Added ability to use unsigned index offsets for
254 253 jshamlet
--                           LDO/STO. Also pipelined the address calculation
255 252 jshamlet
--                           for indexed instructions, reducing the final
256
--                           address generator to a multiplexor fed only by
257
--                           registers.
258 264 jshamlet
-- Seth Henry      07/10/20 Fixed a bug in the LDO/LDX logic where the register
259
--                           pair wasn't being incremented properly due to a
260
--                           missing UPP2 signal to the ALU.
261 269 jshamlet
-- Seth Henry      10/21/20 Modified the write data path to use separate
262
--                           enumerated states rather than reuse the .reg field
263
--                           to improve performance.
264 169 jshamlet
 
265
library ieee;
266
  use ieee.std_logic_1164.all;
267
  use ieee.std_logic_unsigned.all;
268
  use ieee.std_logic_arith.all;
269
  use ieee.std_logic_misc.all;
270
 
271
library work;
272 227 jshamlet
  use work.Open8_pkg.all;
273 169 jshamlet
 
274 183 jshamlet
entity o8_cpu is
275 169 jshamlet
  generic(
276
    Program_Start_Addr       : ADDRESS_TYPE := x"0000"; -- Initial PC location
277
    ISR_Start_Addr           : ADDRESS_TYPE := x"FFF0"; -- Bottom of ISR vec's
278
    Stack_Start_Addr         : ADDRESS_TYPE := x"03FF"; -- Top of Stack
279
    Allow_Stack_Address_Move : boolean      := false;   -- Use Normal v8 RSP
280 188 jshamlet
    Stack_Xfer_Flag          : integer      := PSR_GP4; -- GP4 modifies RSP
281 169 jshamlet
    Enable_Auto_Increment    : boolean      := false;   -- Modify indexed instr
282
    BRK_Implements_WAI       : boolean      := false;   -- BRK -> Wait for Int
283
    Enable_NMI               : boolean      := true;    -- Force INTR0 enabled
284 210 jshamlet
    Sequential_Interrupts    : boolean      := false;   -- Interruptable ISRs
285 224 jshamlet
    RTI_Ignores_GP_Flags     : boolean      := false;   -- RTI sets all flags
286 244 jshamlet
    Supervisor_Mode          : boolean      := false;   -- I bit is restricted
287 252 jshamlet
    Unsigned_Index_Offsets   : boolean      := false;   -- Offsets are signed
288 169 jshamlet
    Default_Interrupt_Mask   : DATA_TYPE    := x"FF";   -- Enable all Ints
289 224 jshamlet
    Clock_Frequency          : real                     -- Clock Frequency
290
);
291 169 jshamlet
  port(
292
    Clock                    : in  std_logic;
293 224 jshamlet
    PLL_Locked               : in  std_logic;
294 169 jshamlet
    --
295 225 jshamlet
    Halt_Req                 : in  std_logic := '0';
296
    Halt_Ack                 : out std_logic;
297
    --
298 223 jshamlet
    Open8_Bus                : out OPEN8_BUS_TYPE;
299 169 jshamlet
    Rd_Data                  : in  DATA_TYPE;
300 223 jshamlet
    Interrupts               : in  INTERRUPT_BUNDLE := x"00"
301
);
302 169 jshamlet
end entity;
303
 
304 183 jshamlet
architecture behave of o8_cpu is
305 169 jshamlet
 
306 224 jshamlet
  signal Reset_q             : std_logic := Reset_Level;
307
  signal Reset               : std_logic := Reset_Level;
308
 
309
  constant USEC_VAL          : integer := integer(Clock_Frequency / 1000000.0);
310
  constant USEC_WDT          : integer := ceil_log2(USEC_VAL - 1);
311
  constant USEC_DLY          : std_logic_vector :=
312
                                conv_std_logic_vector(USEC_VAL - 1, USEC_WDT);
313
  signal uSec_Cntr           : std_logic_vector( USEC_WDT - 1 downto 0 );
314
  signal uSec_Tick           : std_logic;
315
 
316 187 jshamlet
  signal CPU_Next_State      : CPU_STATES := IPF_C0;
317
  signal CPU_State           : CPU_STATES := IPF_C0;
318 169 jshamlet
 
319 225 jshamlet
  signal CPU_Halt_Req        : std_logic := '0';
320
  signal CPU_Halt_Ack        : std_logic := '0';
321 187 jshamlet
 
322 169 jshamlet
  signal Cache_Ctrl          : CACHE_MODES := CACHE_IDLE;
323
 
324
  signal Opcode              : OPCODE_TYPE := (others => '0');
325
  signal SubOp, SubOp_p1     : SUBOP_TYPE  := (others => '0');
326
 
327
  signal Prefetch            : DATA_TYPE   := x"00";
328
  signal Operand1, Operand2  : DATA_TYPE   := x"00";
329
 
330
  signal Instr_Prefetch      : std_logic   := '0';
331
 
332
  signal PC_Ctrl             : PC_CTRL_TYPE;
333
  signal Program_Ctr         : ADDRESS_TYPE := x"0000";
334
 
335 182 jshamlet
  signal ALU_Ctrl            : ALU_CTRL_TYPE;
336
  signal Regfile             : REGFILE_TYPE;
337
  signal Flags               : FLAG_TYPE;
338
  signal Mult                : ADDRESS_TYPE := x"0000";
339
 
340 169 jshamlet
  signal SP_Ctrl             : SP_CTRL_TYPE;
341
  signal Stack_Ptr           : ADDRESS_TYPE := x"0000";
342
 
343
  signal DP_Ctrl             : DATA_CTRL_TYPE;
344
 
345
  signal INT_Ctrl            : INT_CTRL_TYPE;
346
  signal Ack_D, Ack_Q, Ack_Q1: std_logic   := '0';
347
  signal Int_Req, Int_Ack    : std_logic   := '0';
348 245 jshamlet
  signal Set_Mask            : std_logic   := '0';
349 169 jshamlet
  signal Int_Mask            : DATA_TYPE   := x"00";
350
  signal i_Ints              : INTERRUPT_BUNDLE := x"00";
351
  signal Pending             : INTERRUPT_BUNDLE := x"00";
352
  signal Wait_for_FSM        : std_logic := '0';
353 210 jshamlet
  signal Wait_for_ISR        : std_logic := '0';
354 169 jshamlet
 
355 254 jshamlet
  alias  ISR_Addr_Base       is ISR_Start_Addr(15 downto 4);
356
  signal ISR_Addr_Offset     : std_logic_vector(3 downto 0) := x"0";
357
 
358
  constant INT_VECTOR_0      : std_logic_vector(3 downto 0) := x"0";
359
  constant INT_VECTOR_1      : std_logic_vector(3 downto 0) := x"2";
360
  constant INT_VECTOR_2      : std_logic_vector(3 downto 0) := x"4";
361
  constant INT_VECTOR_3      : std_logic_vector(3 downto 0) := x"6";
362
  constant INT_VECTOR_4      : std_logic_vector(3 downto 0) := x"8";
363
  constant INT_VECTOR_5      : std_logic_vector(3 downto 0) := x"A";
364
  constant INT_VECTOR_6      : std_logic_vector(3 downto 0) := x"C";
365
  constant INT_VECTOR_7      : std_logic_vector(3 downto 0) := x"E";
366
 
367 255 jshamlet
  signal IDX_Offset_SX       : std_logic := '0';
368
 
369 252 jshamlet
  signal IDX_Offset          : ADDRESS_TYPE := x"0000";
370
 
371 255 jshamlet
  signal IDX_Sel_l           : std_logic_vector(2 downto 0) := "000";
372
  signal IDX_Sel_h           : std_logic_vector(2 downto 0) := "000";
373
 
374 252 jshamlet
  signal IDX_Reg_l           : integer := 0;
375
  signal IDX_Reg_h           : integer := 0;
376
 
377
  signal IDX_NoOffset_Calc   : ADDRESS_TYPE := x"0000";
378
  signal IDX_Offset_Calc     : ADDRESS_TYPE := x"0000";
379
 
380 169 jshamlet
begin
381
 
382 224 jshamlet
-------------------------------------------------------------------------------
383
-- Reset & uSec Tick
384
-------------------------------------------------------------------------------
385 185 jshamlet
 
386 224 jshamlet
  CPU_Reset_Sync: process( Clock, PLL_Locked )
387
  begin
388
    if( PLL_Locked = '0' )then
389
      Reset_q                <= Reset_Level;
390
      Reset                  <= Reset_Level;
391
    elsif( rising_edge(Clock) )then
392
      Reset_q                <= not Reset_Level;
393
      Reset                  <= Reset_q;
394
    end if;
395
  end process;
396
 
397
  uSec_Tick_proc: process( Clock, Reset )
398
  begin
399
    if( Reset = Reset_Level )then
400
      uSec_Cntr              <= USEC_DLY;
401
      uSec_Tick              <= '0';
402
    elsif( rising_edge( Clock ) )then
403
      uSec_Cntr              <= uSec_Cntr - 1;
404
      if( or_reduce(uSec_Cntr) = '0' )then
405
        uSec_Cntr            <= USEC_DLY;
406
      end if;
407
      uSec_Tick              <= nor_reduce(uSec_Cntr);
408
    end if;
409
  end process;
410
 
411
  Open8_Bus.Clock            <= Clock;
412
  Open8_Bus.Reset            <= Reset;
413
  Open8_Bus.uSec_Tick        <= uSec_Tick;
414
 
415 169 jshamlet
-------------------------------------------------------------------------------
416 182 jshamlet
-- Address bus selection/generation logic
417 169 jshamlet
-------------------------------------------------------------------------------
418
 
419 254 jshamlet
  -- Address selection logic based on current CPU state. This is combinatorial,
420
  --  as adding pipeline registration would add a clock cycle to every instr,
421
  --  without really adding the Fmax to compensate.
422
  Address_Logic: process(CPU_State, Operand1, Operand2, IDX_NoOffset_Calc,
423 255 jshamlet
                         IDX_Offset_Calc, ISR_Addr_Offset, Stack_Ptr,
424
                         Program_Ctr )
425 254 jshamlet
  begin
426
    case( CPU_State )is
427
 
428
      when LDA_C2 | STA_C2 =>
429
        Open8_Bus.Address    <= Operand2 & Operand1;
430
 
431
      when LDX_C1 | STX_C1 =>
432
        Open8_Bus.Address    <= IDX_NoOffset_Calc;
433
 
434
      when LDO_C2 | STO_C2 =>
435
        Open8_Bus.Address    <= IDX_Offset_Calc;
436
 
437
      when ISR_C1 | ISR_C2 =>
438
        Open8_Bus.Address    <= ISR_Addr_Base & ISR_Addr_Offset;
439
 
440 255 jshamlet
      when PSH_C1 | POP_C1 |
441
           ISR_C3 | JSR_C1 | JSR_C2 |
442
           RTS_C1 | RTS_C2 | RTS_C3 =>
443 254 jshamlet
        Open8_Bus.Address    <= Stack_Ptr;
444
 
445
      when others =>
446
        Open8_Bus.Address    <= Program_Ctr;
447
 
448
    end case;
449
  end process;
450
 
451 252 jshamlet
  -- The original model treated the offset to LDO/STO as a signed value
452
  --  allowing access to locations -128 to +127 from [Rn+1:Rn]. This isn't
453
  --  always helpful, so the generic allows the CPU to use unsigned math
454
  --  for the offsets. This makes the range 0 to +255 instead.
455 253 jshamlet
 
456 255 jshamlet
  IDX_Offset_SX <= '0' when Unsigned_Index_Offsets else Operand1(7);
457 252 jshamlet
 
458 255 jshamlet
  IDX_Offset(15 downto 8)    <= (others => IDX_Offset_SX);
459 252 jshamlet
  IDX_Offset(7 downto 0)     <= Operand1;
460
 
461
  -- Enable_Auto_Increment uses the LSB to determine whether or not to
462
  --  do the auto-increment, so we need to lock the LSB for each operand
463
  --  if it is enabled. This forces [ODD:EVEN] pairing.
464
 
465 255 jshamlet
  IDX_Sel_l <= (SubOp(2 downto 1) & '0') when Enable_Auto_Increment else
466
               SubOp;
467 252 jshamlet
 
468 255 jshamlet
  IDX_Sel_h <= (SubOp(2 downto 1) & '1') when Enable_Auto_Increment else
469
               SubOp_p1;
470 252 jshamlet
 
471 255 jshamlet
  IDX_Reg_l <= conv_integer(IDX_Sel_l);
472
  IDX_Reg_h <= conv_integer(IDX_Sel_h);
473
 
474 252 jshamlet
  -- Pipeline registers for the indexed and indexed with offset addresses.
475
  Idx_Addr_Calc_proc: process( Clock, Reset )
476 169 jshamlet
    variable Reg, Reg_1      : integer range 0 to 7 := 0;
477
  begin
478 252 jshamlet
    if( Reset = Reset_Level )then
479
      IDX_NoOffset_Calc      <= x"0000";
480
      IDX_Offset_Calc        <= x"0000";
481
    elsif( rising_edge(Clock))then
482
      IDX_NoOffset_Calc      <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l));
483
      IDX_Offset_Calc        <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l)) +
484
                                IDX_Offset;
485 182 jshamlet
    end if;
486 252 jshamlet
  end process;
487 182 jshamlet
 
488
-------------------------------------------------------------------------------
489
-- Combinatorial portion of CPU finite state machine
490
-- State Logic / Instruction Decoding & Execution
491
-------------------------------------------------------------------------------
492
 
493 187 jshamlet
  State_Logic: process(CPU_State, Flags, Int_Mask, CPU_Halt_Req, Opcode,
494 182 jshamlet
                       SubOp , SubOp_p1, Operand1, Operand2, Int_Req )
495
    variable Reg             : integer range 0 to 7 := 0;
496
  begin
497 169 jshamlet
    CPU_Next_State           <= CPU_State;
498
    Cache_Ctrl               <= CACHE_IDLE;
499
    --
500 185 jshamlet
    PC_Ctrl.Oper             <= PC_INCR;
501
    PC_Ctrl.Offset           <= PC_IDLE;
502 182 jshamlet
    --
503 169 jshamlet
    ALU_Ctrl.Oper            <= ALU_IDLE;
504
    ALU_Ctrl.Reg             <= ACCUM;
505
    --
506
    SP_Ctrl.Oper             <= SP_IDLE;
507
    --
508
    DP_Ctrl.Src              <= DATA_RD_MEM;
509
    DP_Ctrl.Reg              <= ACCUM;
510
    --
511
    INT_Ctrl.Mask_Set        <= '0';
512
    INT_Ctrl.Soft_Ints       <= x"00";
513
    INT_Ctrl.Incr_ISR        <= '0';
514
    Ack_D                    <= '0';
515 225 jshamlet
    --
516 182 jshamlet
    Reg                     := conv_integer(SubOp);
517 225 jshamlet
    --
518
    CPU_Halt_Ack             <= '0';
519 169 jshamlet
 
520
    case CPU_State is
521
-------------------------------------------------------------------------------
522
-- Initial Instruction fetch & decode
523
-------------------------------------------------------------------------------
524 187 jshamlet
      when IPF_C0 =>
525
        CPU_Next_State       <= IPF_C1;
526 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
527 169 jshamlet
 
528 187 jshamlet
      when IPF_C1 =>
529
        CPU_Next_State       <= IPF_C2;
530 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
531 169 jshamlet
 
532 187 jshamlet
      when IPF_C2 =>
533
        CPU_Next_State       <= IDC_C0;
534 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
535 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
536 169 jshamlet
 
537 187 jshamlet
      when IDC_C0 =>
538
        CPU_Next_State       <= IDC_C0;
539 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
540
 
541
        case Opcode is
542
          when OP_PSH =>
543
            CPU_Next_State   <= PSH_C1;
544
            Cache_Ctrl       <= CACHE_PREFETCH;
545 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
546 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
547
            DP_Ctrl.Reg      <= SubOp;
548
 
549
          when OP_POP =>
550
            CPU_Next_State   <= POP_C1;
551
            Cache_Ctrl       <= CACHE_PREFETCH;
552 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
553 169 jshamlet
            SP_Ctrl.Oper     <= SP_POP;
554
 
555
          when OP_BR0 | OP_BR1 =>
556
            CPU_Next_State   <= BRN_C1;
557
            Cache_Ctrl       <= CACHE_OPER1;
558 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
559 169 jshamlet
 
560 185 jshamlet
 
561 169 jshamlet
          when OP_DBNZ =>
562
            CPU_Next_State   <= DBNZ_C1;
563
            Cache_Ctrl       <= CACHE_OPER1;
564 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
565 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_DEC;
566
            ALU_Ctrl.Reg     <= SubOp;
567
 
568
          when OP_INT =>
569 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
570 187 jshamlet
            -- Make sure the requested interrupt is actually enabled first.
571
            --  Also, unlike CPU_Halt, the INT instruction is actually being
572
            --  executed, so go ahead and increment the program counter before
573
            --  pausing so the CPU restarts on the next instruction.
574 169 jshamlet
            if( Int_Mask(Reg) = '1' )then
575 187 jshamlet
              CPU_Next_State <= WAI_Cx;
576 169 jshamlet
              INT_Ctrl.Soft_Ints(Reg) <= '1';
577
            end if;
578
 
579
          when OP_STK =>
580
            case SubOp is
581
              when SOP_RSP  =>
582 185 jshamlet
                PC_Ctrl.Offset <= PC_NEXT;
583 181 jshamlet
                if( not Allow_Stack_Address_Move )then
584 187 jshamlet
                  -- The default behavior for this instruction is to simply
585
                  --  repoint the SP to the HDL default
586 185 jshamlet
                  SP_Ctrl.Oper    <= SP_CLR;
587 181 jshamlet
                end if;
588 187 jshamlet
                if( Allow_Stack_Address_Move and
589
                    Flags(Stack_Xfer_Flag) = '1' )then
590
                  -- If RSP is set to allow SP moves, and the specified flag
591
                  --  is true, then signal the stack pointer logic to load
592
                  --  from R1:R0
593 185 jshamlet
                  SP_Ctrl.Oper    <= SP_SET;
594 181 jshamlet
                end if;
595 187 jshamlet
                if( Allow_Stack_Address_Move and
596
                    Flags(Stack_Xfer_Flag) = '0')then
597
                  -- If RSP is set to allow SP moves, and the specified flag
598
                  --  is false, then signal the ALU to copy the stack pointer
599
                  --  to R1:R0
600 185 jshamlet
                  ALU_Ctrl.Oper   <= ALU_RSP;
601 181 jshamlet
                end if;
602 169 jshamlet
 
603
              when SOP_RTS | SOP_RTI =>
604 185 jshamlet
                CPU_Next_State    <= RTS_C1;
605 190 jshamlet
                Cache_Ctrl        <= CACHE_IDLE;
606 185 jshamlet
                SP_Ctrl.Oper      <= SP_POP;
607 169 jshamlet
 
608
              when SOP_BRK  =>
609
                if( BRK_Implements_WAI )then
610 187 jshamlet
                  -- If BRK_Implements_WAI, then jump to the WAI_Cx and
611
                  --  increment the PC similar to an ISR flow.
612
                  CPU_Next_State  <= WAI_Cx;
613 185 jshamlet
                  PC_Ctrl.Offset  <= PC_NEXT;
614 187 jshamlet
                else
615
                -- If Break is implemented normally, back the PC up by
616 260 jshamlet
                --  2 and return through IPF_C0 in order to execute a 3
617 187 jshamlet
                --  clock cycle delay
618
                  CPU_Next_State  <= BRK_C1;
619
                  PC_Ctrl.Offset  <= PC_REV2;
620 169 jshamlet
                end if;
621
 
622
              when SOP_JMP  =>
623 185 jshamlet
                CPU_Next_State    <= JMP_C1;
624
                Cache_Ctrl        <= CACHE_OPER1;
625 169 jshamlet
 
626
              when SOP_SMSK =>
627 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
628 169 jshamlet
                INT_Ctrl.Mask_Set <= '1';
629
 
630
              when SOP_GMSK =>
631 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
632
                ALU_Ctrl.Oper     <= ALU_GMSK;
633 169 jshamlet
 
634
              when SOP_JSR =>
635 269 jshamlet
                CPU_Next_State    <= JSR_C1;
636 185 jshamlet
                Cache_Ctrl        <= CACHE_OPER1;
637 269 jshamlet
                DP_Ctrl.Src       <= DATA_WR_PC_H;
638 169 jshamlet
 
639
              when others => null;
640
            end case;
641
 
642
          when OP_MUL =>
643
            CPU_Next_State   <= MUL_C1;
644 181 jshamlet
            -- Multiplication requires a single clock cycle to calculate PRIOR
645
            --  to the ALU writing the result to registers. As a result, this
646
            --  state needs to idle the ALU initially, and back the PC up by 1
647
            -- We can get away with only 1 extra clock by pre-fetching the
648
            --  next instruction, though.
649 169 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
650 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
651 181 jshamlet
            -- Note that both the multiply process AND ALU process need the
652
            --  source register for Rn (R1:R0 = R0 * Rn). Assert ALU_Ctrl.reg
653
            --  now, but hold off on the ALU command until the next state.
654 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_IDLE;
655
            ALU_Ctrl.Reg     <= SubOp;
656
 
657
          when OP_UPP =>
658
            CPU_Next_State   <= UPP_C1;
659
            Cache_Ctrl       <= CACHE_PREFETCH;
660 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
661 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
662
            ALU_Ctrl.Reg     <= SubOp;
663
 
664
          when OP_LDA =>
665
            CPU_Next_State   <= LDA_C1;
666
            Cache_Ctrl       <= CACHE_OPER1;
667
 
668
          when OP_LDI =>
669
            CPU_Next_State   <= LDI_C1;
670
            Cache_Ctrl       <= CACHE_OPER1;
671 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
672 169 jshamlet
 
673
          when OP_LDO =>
674
            CPU_Next_State   <= LDO_C1;
675
            Cache_Ctrl       <= CACHE_OPER1;
676 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
677 169 jshamlet
 
678
          when OP_LDX =>
679
            CPU_Next_State   <= LDX_C1;
680 181 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
681 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
682 169 jshamlet
 
683
          when OP_STA =>
684
            CPU_Next_State   <= STA_C1;
685
            Cache_Ctrl       <= CACHE_OPER1;
686
 
687
          when OP_STO =>
688
            CPU_Next_State   <= STO_C1;
689
            Cache_Ctrl       <= CACHE_OPER1;
690 252 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
691 169 jshamlet
 
692
          when OP_STX =>
693
            CPU_Next_State   <= STX_C1;
694
            Cache_Ctrl       <= CACHE_PREFETCH;
695 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
696 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
697
            DP_Ctrl.Reg      <= ACCUM;
698
 
699 244 jshamlet
          when OP_STP =>
700
            PC_Ctrl.Offset   <= PC_NEXT;
701
            if( Supervisor_Mode )then
702
              if( SubOp /= PSR_I )then
703
                ALU_Ctrl.Oper  <= Opcode;
704
                ALU_Ctrl.Reg   <= SubOp;
705
              end if;
706
            else
707
              ALU_Ctrl.Oper  <= Opcode;
708
              ALU_Ctrl.Reg   <= SubOp;
709
            end if;
710
 
711 169 jshamlet
          when others =>
712 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
713 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
714
            ALU_Ctrl.Reg     <= SubOp;
715
 
716
        end case;
717
 
718 186 jshamlet
        if( Int_Req = '1' )then
719
          CPU_Next_State     <= ISR_C1;
720 187 jshamlet
        end if;
721
 
722
        if( CPU_Halt_Req = '1' )then
723
          CPU_Next_State     <= WAH_Cx;
724
        end if;
725
 
726
        -- If either of these override conditions are true, the decoder needs
727
        --  to undo everything it just setup, since even "single-cycle"
728
        --  instructions will be executed again upon return.
729
        if( Int_Req = '1' or CPU_Halt_Req = '1' )then
730
          -- In either case, we want to skip loading the cache, as the cache
731
          --  will be invalid by the time we get back.
732 186 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
733 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to the current instruction,
734
          -- compensating for the pipeline registers.
735 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
736
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
737 187 jshamlet
          --  operation due to the current instruction.
738 186 jshamlet
          ALU_Ctrl.Oper      <= ALU_IDLE;
739
          SP_Ctrl.Oper       <= SP_IDLE;
740 187 jshamlet
          -- Interrupt logic outside of the state machine needs this to be set
741
          --  to DATA_RD_MEM, while CPU_Halt considers this a "don't care".
742 186 jshamlet
          DP_Ctrl.Src        <= DATA_RD_MEM;
743 187 jshamlet
          -- If an INT/SMSK instruction was going to be executed, it will get
744
          --  executed again when normal processing resumes, so axe their
745
          --  requests for now.
746
          INT_Ctrl.Mask_Set       <= '0';
747
          INT_Ctrl.Soft_Ints(Reg) <= '0';
748 186 jshamlet
        end if;
749
 
750 169 jshamlet
-------------------------------------------------------------------------------
751
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
752
-------------------------------------------------------------------------------
753
 
754
      when BRN_C1 =>
755 187 jshamlet
        CPU_Next_State       <= IDC_C0;
756 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
757 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
758 169 jshamlet
        if( Flags(Reg) = Opcode(0) )then
759 187 jshamlet
          CPU_Next_State     <= IPF_C0;
760 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
761
          PC_Ctrl.Offset     <= Operand1;
762
        end if;
763
 
764
      when DBNZ_C1 =>
765 187 jshamlet
        CPU_Next_State       <= IDC_C0;
766 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
767 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
768
        if( Flags(PSR_Z) = '0' )then
769 187 jshamlet
          CPU_Next_State     <= IPF_C0;
770 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
771
          PC_Ctrl.Offset     <= Operand1;
772
        end if;
773
 
774
      when JMP_C1 =>
775
        CPU_Next_State       <= JMP_C2;
776
        Cache_Ctrl           <= CACHE_OPER2;
777
 
778
      when JMP_C2 =>
779 187 jshamlet
        CPU_Next_State       <= IPF_C0;
780 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
781
 
782
-------------------------------------------------------------------------------
783
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
784
-------------------------------------------------------------------------------
785
 
786
      when LDA_C1 =>
787
        CPU_Next_State       <= LDA_C2;
788
        Cache_Ctrl           <= CACHE_OPER2;
789
 
790
      when LDA_C2 =>
791
        CPU_Next_State       <= LDA_C3;
792
 
793
      when LDA_C3 =>
794
        CPU_Next_State       <= LDA_C4;
795 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
796 169 jshamlet
 
797
      when LDA_C4 =>
798
        CPU_Next_State       <= LDI_C1;
799
        Cache_Ctrl           <= CACHE_OPER1;
800 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
801 169 jshamlet
 
802
      when LDI_C1 =>
803 187 jshamlet
        CPU_Next_State       <= IDC_C0;
804 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
805 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
806 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
807
        ALU_Ctrl.Reg         <= SubOp;
808
 
809
      when LDO_C1 =>
810 252 jshamlet
        CPU_Next_State       <= LDO_C2;
811
 
812
      when LDO_C2 =>
813 181 jshamlet
        CPU_Next_State       <= LDX_C2;
814 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
815 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
816
          ALU_Ctrl.Oper      <= ALU_UPP;
817
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
818 169 jshamlet
        end if;
819
 
820
      when LDX_C1 =>
821
        CPU_Next_State       <= LDX_C2;
822 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
823
          ALU_Ctrl.Oper      <= ALU_UPP;
824
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
825 181 jshamlet
        end if;
826 169 jshamlet
 
827
      when LDX_C2 =>
828
        CPU_Next_State       <= LDX_C3;
829 263 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
830
          ALU_Ctrl.Oper      <= ALU_UPP2;
831
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '1';
832
        end if;
833 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
834 181 jshamlet
 
835
      when LDX_C3 =>
836
        CPU_Next_State       <= LDX_C4;
837 182 jshamlet
        Cache_Ctrl           <= CACHE_OPER1;
838 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
839 169 jshamlet
 
840 181 jshamlet
      when LDX_C4 =>
841 187 jshamlet
        CPU_Next_State       <= IDC_C0;
842 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
843 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
844 181 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
845 169 jshamlet
        ALU_Ctrl.Reg         <= ACCUM;
846
 
847
-------------------------------------------------------------------------------
848
-- Data Storage - Store to memory (STA, STO, STX)
849
-------------------------------------------------------------------------------
850
      when STA_C1 =>
851
        CPU_Next_State       <= STA_C2;
852
        Cache_Ctrl           <= CACHE_OPER2;
853
        DP_Ctrl.Src          <= DATA_WR_REG;
854
        DP_Ctrl.Reg          <= SubOp;
855
 
856
      when STA_C2 =>
857
        CPU_Next_State       <= STA_C3;
858 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
859 169 jshamlet
 
860
      when STA_C3 =>
861 187 jshamlet
        CPU_Next_State       <= IPF_C2;
862 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
863 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
864 169 jshamlet
 
865
      when STO_C1 =>
866 252 jshamlet
        CPU_Next_State       <= STO_C2;
867 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
868 252 jshamlet
        DP_Ctrl.Src          <= DATA_WR_REG;
869
        DP_Ctrl.Reg          <= ACCUM;
870
 
871
      when STO_C2 =>
872
        CPU_Next_State       <= IPF_C1;
873 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
874 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
875 252 jshamlet
          CPU_Next_State     <= STO_C3;
876 182 jshamlet
          ALU_Ctrl.Oper      <= ALU_UPP;
877
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
878 169 jshamlet
        end if;
879
 
880 252 jshamlet
      when STO_C3 =>
881
        CPU_Next_State       <= IPF_C2;
882 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
883 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
884
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
885
 
886
      when STX_C1 =>
887 187 jshamlet
        CPU_Next_State       <= IPF_C1;
888 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
889 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
890
          CPU_Next_State     <= STX_C2;
891
          ALU_Ctrl.Oper      <= ALU_UPP;
892
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
893 169 jshamlet
        end if;
894
 
895
      when STX_C2 =>
896 187 jshamlet
        CPU_Next_State       <= IPF_C2;
897 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
898 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
899
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
900
 
901
-------------------------------------------------------------------------------
902
-- Multi-Cycle Math Operations (UPP, MUL)
903
-------------------------------------------------------------------------------
904
 
905
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
906 181 jshamlet
      --  instruction/first operand, we have to return through PF2. Also, we
907
      --  need to tell the ALU to store the results to R1:R0 here. Note that
908
      --  there is no ALU_Ctrl.Reg, as this is implied in the ALU instruction
909 169 jshamlet
      when MUL_C1 =>
910 187 jshamlet
        CPU_Next_State       <= IPF_C2;
911 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
912 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_MUL;
913
 
914
      when UPP_C1 =>
915 187 jshamlet
        CPU_Next_State       <= IPF_C2;
916 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
917 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
918
        ALU_Ctrl.Reg         <= SubOp_p1;
919
 
920
-------------------------------------------------------------------------------
921
-- Basic Stack Manipulation (PSH, POP, RSP)
922
-------------------------------------------------------------------------------
923
      when PSH_C1 =>
924 187 jshamlet
        CPU_Next_State       <= IPF_C1;
925 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
926
 
927
      when POP_C1 =>
928
        CPU_Next_State       <= POP_C2;
929
 
930
      when POP_C2 =>
931
        CPU_Next_State       <= POP_C3;
932 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
933 169 jshamlet
 
934
      when POP_C3 =>
935
        CPU_Next_State       <= POP_C4;
936
        Cache_Ctrl           <= CACHE_OPER1;
937 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
938 169 jshamlet
 
939
      when POP_C4 =>
940 187 jshamlet
        CPU_Next_State       <= IDC_C0;
941 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
942 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
943 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_POP;
944
        ALU_Ctrl.Reg         <= SubOp;
945 172 jshamlet
 
946 169 jshamlet
-------------------------------------------------------------------------------
947
-- Subroutines & Interrupts (RTS, JSR)
948
-------------------------------------------------------------------------------
949 187 jshamlet
      when WAI_Cx => -- For soft interrupts only, halt the Program_Ctr
950 169 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
951 186 jshamlet
        if( Int_Req = '1' )then
952
          CPU_Next_State     <= ISR_C1;
953 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to would have been the next
954
          --  instruction, compensating for the pipeline registers.
955 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
956
          DP_Ctrl.Src        <= DATA_RD_MEM;
957
        end if;
958 169 jshamlet
 
959 187 jshamlet
      when WAH_Cx => -- Holds until CPU_Halt_Req is deasserted.
960 225 jshamlet
        CPU_Halt_Ack         <= '1';
961 187 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
962
        if( CPU_Halt_Req = '0' )then
963
          CPU_Next_State     <= IPF_C0;
964
          DP_Ctrl.Src        <= DATA_RD_MEM;
965
        end if;
966
 
967
      when BRK_C1 => -- Debugging (BRK) Performs a 5-clock NOP.
968
        CPU_Next_State       <= IPF_C0;
969
 
970 169 jshamlet
      when ISR_C1 =>
971
        CPU_Next_State       <= ISR_C2;
972
        INT_Ctrl.Incr_ISR    <= '1';
973
 
974
      when ISR_C2 =>
975
        CPU_Next_State       <= ISR_C3;
976
        DP_Ctrl.Src          <= DATA_WR_FLAG;
977
 
978
      when ISR_C3 =>
979
        CPU_Next_State       <= JSR_C1;
980
        Cache_Ctrl           <= CACHE_OPER1;
981 182 jshamlet
        ALU_Ctrl.Oper        <= ALU_STP;
982 185 jshamlet
        ALU_Ctrl.Reg         <= conv_std_logic_vector(PSR_I,3);
983 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
984 269 jshamlet
        DP_Ctrl.Src          <= DATA_WR_PC_H;
985 169 jshamlet
        Ack_D                <= '1';
986
 
987
      when JSR_C1 =>
988
        CPU_Next_State       <= JSR_C2;
989
        Cache_Ctrl           <= CACHE_OPER2;
990
        SP_Ctrl.Oper         <= SP_PUSH;
991 269 jshamlet
        DP_Ctrl.Src          <= DATA_WR_PC_L;
992 169 jshamlet
 
993
      when JSR_C2 =>
994 187 jshamlet
        CPU_Next_State       <= IPF_C0;
995 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
996 182 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
997 169 jshamlet
 
998
      when RTS_C1 =>
999
        CPU_Next_State       <= RTS_C2;
1000
        SP_Ctrl.Oper         <= SP_POP;
1001
 
1002
      when RTS_C2 =>
1003
        CPU_Next_State       <= RTS_C3;
1004
        -- if this is an RTI, then we need to POP the flags
1005
        if( SubOp = SOP_RTI )then
1006
          SP_Ctrl.Oper       <= SP_POP;
1007
        end if;
1008
 
1009
      when RTS_C3 =>
1010
        CPU_Next_State       <= RTS_C4;
1011
        Cache_Ctrl           <= CACHE_OPER1;
1012
 
1013
      when RTS_C4 =>
1014
        CPU_Next_State       <= RTS_C5;
1015
        Cache_Ctrl           <= CACHE_OPER2;
1016
 
1017
      when RTS_C5 =>
1018 187 jshamlet
        CPU_Next_State       <= IPF_C0;
1019 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
1020 185 jshamlet
        -- if this is an RTI, then we need to clear the I bit
1021 169 jshamlet
        if( SubOp = SOP_RTI )then
1022
          CPU_Next_State     <= RTI_C6;
1023
          Cache_Ctrl         <= CACHE_OPER1;
1024 185 jshamlet
          ALU_Ctrl.Oper      <= ALU_CLP;
1025
          ALU_Ctrl.Reg       <= conv_std_logic_vector(PSR_I,3);
1026 169 jshamlet
        end if;
1027
 
1028
      when RTI_C6 =>
1029 187 jshamlet
        CPU_Next_State       <= IPF_C1;
1030 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
1031 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_RFLG;
1032
 
1033
      when others =>
1034
        null;
1035
    end case;
1036
 
1037
  end process;
1038
 
1039
-------------------------------------------------------------------------------
1040
-- Registered portion of CPU finite state machine
1041
-------------------------------------------------------------------------------
1042 182 jshamlet
 
1043 169 jshamlet
  CPU_Regs: process( Reset, Clock )
1044
    variable Offset_SX       : ADDRESS_TYPE;
1045 188 jshamlet
    variable i_Ints          : INTERRUPT_BUNDLE := x"00";
1046 169 jshamlet
    variable Index           : integer range 0 to 7         := 0;
1047
    variable Sum             : std_logic_vector(8 downto 0) := "000000000";
1048
    variable Temp            : std_logic_vector(8 downto 0) := "000000000";
1049
  begin
1050
    if( Reset = Reset_Level )then
1051 187 jshamlet
      CPU_State              <= IPF_C0;
1052 260 jshamlet
 
1053
      CPU_Halt_Req           <= '0';
1054
      Halt_Ack               <= '0';
1055
 
1056 169 jshamlet
      Opcode                 <= OP_INC;
1057
      SubOp                  <= ACCUM;
1058
      SubOp_p1               <= ACCUM;
1059
      Operand1               <= x"00";
1060
      Operand2               <= x"00";
1061
      Instr_Prefetch         <= '0';
1062
      Prefetch               <= x"00";
1063
 
1064 223 jshamlet
      Open8_Bus.Wr_En        <= '0';
1065
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1066
      Open8_Bus.Rd_En        <= '1';
1067 169 jshamlet
 
1068
      Program_Ctr            <= Program_Start_Addr;
1069
      Stack_Ptr              <= Stack_Start_Addr;
1070
 
1071
      Ack_Q                  <= '0';
1072
      Ack_Q1                 <= '0';
1073
      Int_Ack                <= '0';
1074
 
1075
      Int_Req                <= '0';
1076
      Pending                <= x"00";
1077
      Wait_for_FSM           <= '0';
1078 210 jshamlet
      Wait_for_ISR           <= '0';
1079 245 jshamlet
      Set_Mask               <= '0';
1080 169 jshamlet
      if( Enable_NMI )then
1081
        Int_Mask             <= Default_Interrupt_Mask(7 downto 1) & '1';
1082
      else
1083
        Int_Mask             <= Default_Interrupt_Mask;
1084
      end if;
1085 254 jshamlet
      ISR_Addr_Offset        <= INT_VECTOR_0;
1086 169 jshamlet
 
1087
      for i in 0 to 7 loop
1088 188 jshamlet
        Regfile(i)           <= x"00";
1089 169 jshamlet
      end loop;
1090
      Flags                  <= x"00";
1091 248 jshamlet
      if( Supervisor_Mode )then
1092 244 jshamlet
        Flags(PSR_I)         <= '1';
1093
      end if;
1094 169 jshamlet
 
1095 224 jshamlet
      Open8_Bus.GP_Flags     <= (others => '0');
1096 188 jshamlet
 
1097 169 jshamlet
    elsif( rising_edge(Clock) )then
1098 187 jshamlet
 
1099 260 jshamlet
      CPU_State              <= CPU_Next_State;
1100
 
1101
-- Register the halt request and acknowledge lines
1102
 
1103 225 jshamlet
      CPU_Halt_Req           <= Halt_Req;
1104
      Halt_Ack               <= CPU_Halt_Ack;
1105 187 jshamlet
 
1106 169 jshamlet
-------------------------------------------------------------------------------
1107
-- Instruction/Operand caching for pipelined memory access
1108
-------------------------------------------------------------------------------
1109 260 jshamlet
 
1110
      -- To avoid putting too much load on the (usually massive) wire-OR'd bus,
1111
      --  the CPU loads Rd_Data into one of four registers - instruction,
1112
      --  operand 1 or 2, or the instruction prefetch registers. The first is
1113
      --  used to decode an instruction when the prefetch isn't valid, while
1114
      --  the two operand registers are used to hold any additional argument
1115
      --  for multi-byte instructions. Because of the memory pipelining, some
1116
      --  longer instructions can cache the next instruction as part of their
1117
      --  execution in a prefetch register, allowing the CPU to skip loading
1118
      --  it again later. Unfortunate, because instructions aren't all the same
1119
      --  length, it is not feasible to cache their operands without adding a
1120
      --  second partial decode stage that would obviate any savings.
1121
 
1122 169 jshamlet
      case Cache_Ctrl is
1123
        when CACHE_INSTR =>
1124
          Opcode             <= Rd_Data(7 downto 3);
1125
          SubOp              <= Rd_Data(2 downto 0);
1126
          SubOp_p1           <= Rd_Data(2 downto 0) + 1;
1127
          if( Instr_Prefetch = '1' )then
1128
            Opcode           <= Prefetch(7 downto 3);
1129
            SubOp            <= Prefetch(2 downto 0);
1130
            SubOp_p1         <= Prefetch(2 downto 0) + 1;
1131
            Instr_Prefetch   <= '0';
1132
          end if;
1133
 
1134
        when CACHE_OPER1 =>
1135
          Operand1           <= Rd_Data;
1136
 
1137
        when CACHE_OPER2 =>
1138
          Operand2           <= Rd_Data;
1139
 
1140
        when CACHE_PREFETCH =>
1141
          Prefetch           <= Rd_Data;
1142
          Instr_Prefetch     <= '1';
1143
 
1144
        when CACHE_IDLE =>
1145
          null;
1146
      end case;
1147
 
1148
-------------------------------------------------------------------------------
1149
-- Program Counter
1150
-------------------------------------------------------------------------------
1151 260 jshamlet
 
1152
      -- The program counter is a bit unusual in that it always subtracts two
1153
      --  from itself plus the signed offset. This is because of the way the
1154
      --  assembler works when computing branches. Thus, to "IDLE" the counter,
1155
      --  the offset is set to 2, while "NEXT" sets the offset to 3. Depending
1156
      --  on how an instruction interacts with memory, or is pipelined,  the
1157
      --  offset can vary from -1 to 3
1158
 
1159 169 jshamlet
      Offset_SX(15 downto 8) := (others => PC_Ctrl.Offset(7));
1160
      Offset_SX(7 downto 0)  := PC_Ctrl.Offset;
1161
 
1162
      case PC_Ctrl.Oper is
1163
        when PC_INCR =>
1164
          Program_Ctr        <= Program_Ctr + Offset_SX - 2;
1165
 
1166
        when PC_LOAD =>
1167 185 jshamlet
          Program_Ctr        <= Operand2 & Operand1;
1168 169 jshamlet
 
1169
        when others =>
1170
          null;
1171
      end case;
1172
 
1173
-------------------------------------------------------------------------------
1174
-- (Write) Data Path
1175
-------------------------------------------------------------------------------
1176 260 jshamlet
 
1177
      -- Note that this code handles both the Rd_En and Wr_En signals. These
1178
      --  were separated to make downstream logic simpler (As opposed to the
1179
      --  more classic RD_WRn and ADDR_STROBE scheme) It is also true to the
1180
      --  original core, which also had separate read and write enable outputs
1181
 
1182
      Open8_Bus.Wr_En        <= '0';
1183
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1184
      Open8_Bus.Rd_En        <= '0';
1185
 
1186 169 jshamlet
      case DP_Ctrl.Src is
1187
        when DATA_BUS_IDLE =>
1188
          null;
1189
 
1190
        when DATA_RD_MEM =>
1191 223 jshamlet
          Open8_Bus.Rd_En    <= '1';
1192 169 jshamlet
 
1193
        when DATA_WR_REG =>
1194 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1195
          Open8_Bus.Wr_Data  <= Regfile(conv_integer(DP_Ctrl.Reg));
1196 169 jshamlet
 
1197
        when DATA_WR_FLAG =>
1198 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1199
          Open8_Bus.Wr_Data  <= Flags;
1200 169 jshamlet
 
1201 269 jshamlet
        when DATA_WR_PC_L =>
1202 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1203 269 jshamlet
          Open8_Bus.Wr_Data  <= Program_Ctr(7 downto 0);
1204
 
1205
        when DATA_WR_PC_H =>
1206
          Open8_Bus.Wr_En    <= '1';
1207 223 jshamlet
          Open8_Bus.Wr_Data  <= Program_Ctr(15 downto 8);
1208 169 jshamlet
 
1209
        when others =>
1210
          null;
1211
      end case;
1212
 
1213
-------------------------------------------------------------------------------
1214
-- Stack Pointer
1215
-------------------------------------------------------------------------------
1216
      case SP_Ctrl.Oper is
1217
        when SP_IDLE =>
1218
          null;
1219
 
1220 181 jshamlet
        when SP_CLR =>
1221 169 jshamlet
          Stack_Ptr          <= Stack_Start_Addr;
1222
 
1223 181 jshamlet
        when SP_SET =>
1224 245 jshamlet
          if( Supervisor_Mode )then
1225
            if( Flags(PSR_I) = '1' )then
1226
              Stack_Ptr      <= Regfile(1) & Regfile(0);
1227
            end if;
1228
          else
1229
            Stack_Ptr        <= Regfile(1) & Regfile(0);
1230
          end if;
1231 181 jshamlet
 
1232 169 jshamlet
        when SP_POP  =>
1233
          Stack_Ptr          <= Stack_Ptr + 1;
1234
 
1235
        when SP_PUSH =>
1236
          Stack_Ptr          <= Stack_Ptr - 1;
1237
 
1238
        when others =>
1239
          null;
1240
 
1241
      end case;
1242
 
1243
-------------------------------------------------------------------------------
1244
-- Interrupt Controller
1245
-------------------------------------------------------------------------------
1246 245 jshamlet
 
1247
      -- If Supervisor_Mode is set, restrict the SMSK instruction such that it
1248
      --  requires the I bit to be set.
1249
      if( Supervisor_Mode )then
1250
        Set_Mask             <= INT_Ctrl.Mask_Set and Flags(PSR_I);
1251
      else
1252
        Set_Mask             <= INT_Ctrl.Mask_Set;
1253
      end if;
1254
 
1255 169 jshamlet
      -- The interrupt control mask is always sourced out of R0
1256 245 jshamlet
      if( Set_Mask = '1' )then
1257 169 jshamlet
        if( Enable_NMI )then
1258
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
1259
        else
1260
          Int_Mask           <= Regfile(conv_integer(ACCUM));
1261
        end if;
1262
      end if;
1263
 
1264
      -- Combine external and internal interrupts, and mask the OR of the two
1265
      --  with the mask. Record any incoming interrupts to the pending buffer
1266
      i_Ints                 := (Interrupts or INT_Ctrl.Soft_Ints) and
1267
                                Int_Mask;
1268 172 jshamlet
 
1269 169 jshamlet
      Pending                <= i_Ints or Pending;
1270
 
1271 260 jshamlet
      -- If Sequential_Interrupts is set true, Wait_for_ISR should follow the
1272
      --  I bit, preventing a new interrupt from starting until the I bit is
1273
      --  cleared.
1274 210 jshamlet
      if( Sequential_Interrupts )then
1275
        Wait_for_ISR         <= Flags(PSR_I);
1276
      else
1277
        Wait_for_ISR         <= '0';
1278
      end if;
1279
 
1280
      if( Wait_for_FSM = '0' and Wait_for_ISR = '0' )then
1281 169 jshamlet
        if(    Pending(0) = '1' )then
1282 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_0;
1283 169 jshamlet
          Pending(0)         <= '0';
1284
        elsif( Pending(1) = '1' )then
1285 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_1;
1286 169 jshamlet
          Pending(1)         <= '0';
1287
        elsif( Pending(2) = '1' )then
1288 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_2;
1289 169 jshamlet
          Pending(2)         <= '0';
1290
        elsif( Pending(3) = '1' )then
1291 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_3;
1292 169 jshamlet
          Pending(3)         <= '0';
1293
        elsif( Pending(4) = '1' )then
1294 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_4;
1295 169 jshamlet
          Pending(4)         <= '0';
1296
        elsif( Pending(5) = '1' )then
1297 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_5;
1298 169 jshamlet
          Pending(5)         <= '0';
1299
        elsif( Pending(6) = '1' )then
1300 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_6;
1301 169 jshamlet
          Pending(6)         <= '0';
1302
        elsif( Pending(7) = '1' )then
1303 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_7;
1304 169 jshamlet
          Pending(7)         <= '0';
1305
        end if;
1306 185 jshamlet
        Wait_for_FSM         <= or_reduce(Pending);
1307 169 jshamlet
      end if;
1308
 
1309
      -- Reset the Wait_for_FSM flag on Int_Ack
1310
      Ack_Q                  <= Ack_D;
1311
      Ack_Q1                 <= Ack_Q;
1312
      Int_Ack                <= Ack_Q1;
1313
      if( Int_Ack = '1' )then
1314
        Wait_for_FSM         <= '0';
1315
      end if;
1316
 
1317
      Int_Req                <= Wait_for_FSM and (not Int_Ack);
1318
 
1319
      -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1320
      --  lower half of the address.
1321
      if( INT_Ctrl.Incr_ISR = '1' )then
1322 254 jshamlet
        ISR_Addr_Offset             <= ISR_Addr_Offset + 1;
1323 169 jshamlet
      end if;
1324
 
1325
-------------------------------------------------------------------------------
1326
-- ALU (Arithmetic / Logic Unit)
1327
-------------------------------------------------------------------------------
1328 260 jshamlet
 
1329
      -- The ALU code is responsible for (and should be the only code altering)
1330
      --  the register file. Most of the "instructions" directly map to opcodes
1331
      --  but a few are for internal use only, such as operations involving the
1332 263 jshamlet
      --  stack pointer or interrupt mask.
1333 260 jshamlet
 
1334 169 jshamlet
      Index                  := conv_integer(ALU_Ctrl.Reg);
1335
      Sum                    := (others => '0');
1336
      Temp                   := (others => '0');
1337
 
1338
      case ALU_Ctrl.Oper is
1339
        when ALU_INC => -- Rn = Rn + 1 : Flags N,C,Z
1340
          Sum                := ("0" & x"01") +
1341
                                ("0" & Regfile(Index));
1342 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1343
          Flags(PSR_C)       <= Sum(8);
1344 209 jshamlet
          Flags(PSR_N)       <= Sum(7);
1345 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1346
 
1347
        when ALU_UPP => -- Rn = Rn + 1
1348
          Sum                := ("0" & x"01") +
1349
                                ("0" & Regfile(Index));
1350 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1351 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1352
 
1353
        when ALU_UPP2 => -- Rn = Rn + C
1354 263 jshamlet
          Sum                := (x"00" & Flags(PSR_C)) +
1355
                                ("0" & Regfile(Index));
1356 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1357 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1358
 
1359
        when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1360
          Sum                := ("0" & Regfile(0)) +
1361
                                ("0" & Regfile(Index)) +
1362 185 jshamlet
                                Flags(PSR_C);
1363
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1364
          Flags(PSR_C)       <= Sum(8);
1365
          Flags(PSR_N)       <= Sum(7);
1366 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1367
 
1368
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1369
          Temp               := "0" & Regfile(Index);
1370 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1371
          Flags(PSR_N)       <= Temp(7);
1372 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1373
 
1374
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1375
          Temp(7 downto 0)   := Regfile(0) or Regfile(Index);
1376 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1377
          Flags(PSR_N)       <= Temp(7);
1378 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1379
 
1380
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1381
          Temp(7 downto 0)   := Regfile(0) and Regfile(Index);
1382 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1383
          Flags(PSR_N)       <= Temp(7);
1384 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1385
 
1386
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1387
          Temp(7 downto 0)   := Regfile(0) xor Regfile(Index);
1388 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1389
          Flags(PSR_N)       <= Temp(7);
1390 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1391
 
1392
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1393 185 jshamlet
          Temp               := Regfile(Index) & Flags(PSR_C);
1394
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1395
          Flags(PSR_C)       <= Temp(8);
1396
          Flags(PSR_N)       <= Temp(7);
1397 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1398
 
1399
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1400 185 jshamlet
          Temp               := Regfile(Index)(0) & Flags(PSR_C) &
1401 169 jshamlet
                                Regfile(Index)(7 downto 1);
1402 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1403
          Flags(PSR_C)       <= Temp(8);
1404
          Flags(PSR_N)       <= Temp(7);
1405 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1406
 
1407
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1408
          Sum                := ("0" & Regfile(Index)) +
1409
                                ("0" & x"FF");
1410 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1411
          Flags(PSR_C)       <= Sum(8);
1412
          Flags(PSR_N)       <= Sum(7);
1413 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1414
 
1415
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1416
          Sum                := ("0" & Regfile(0)) +
1417
                                ("1" & (not Regfile(Index))) +
1418 185 jshamlet
                                Flags(PSR_C);
1419
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1420
          Flags(PSR_C)       <= Sum(8);
1421
          Flags(PSR_N)       <= Sum(7);
1422 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1423
 
1424
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1425
          Sum                := ("0" & Regfile(0)) +
1426
                                ("0" & Regfile(Index));
1427 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1428 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1429 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1430
          Flags(PSR_N)       <= Sum(7);
1431 169 jshamlet
 
1432
        when ALU_STP => -- Sets bit(n) in the Flags register
1433
          Flags(Index)       <= '1';
1434
 
1435
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1436 185 jshamlet
          Flags(PSR_Z)       <= not Regfile(0)(Index);
1437
          Flags(PSR_N)       <= Regfile(0)(7);
1438 169 jshamlet
 
1439
        when ALU_CLP => -- Clears bit(n) in the Flags register
1440
          Flags(Index)       <= '0';
1441
 
1442
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1443
          Temp               := "0" & Regfile(0);
1444 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1445
          Flags(PSR_N)       <= Temp(7);
1446 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1447
 
1448
        when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1449
          Sum                := ("0" & Regfile(0)) +
1450
                                ("1" & (not Regfile(Index))) +
1451
                                '1';
1452 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1453
          Flags(PSR_C)       <= Sum(8);
1454
          Flags(PSR_N)       <= Sum(7);
1455 169 jshamlet
 
1456
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1457
          Regfile(0)         <= Mult(7 downto 0);
1458
          Regfile(1)         <= Mult(15 downto 8);
1459 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Mult);
1460 169 jshamlet
 
1461
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1462 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Operand1);
1463
          Flags(PSR_N)       <= Operand1(7);
1464
          Regfile(Index)     <= Operand1;
1465 169 jshamlet
 
1466
        when ALU_POP => -- Rn <= Data
1467 185 jshamlet
          Regfile(Index)     <= Operand1;
1468 169 jshamlet
 
1469
        when ALU_RFLG =>
1470 188 jshamlet
          Flags(3 downto 0)  <= Operand1(3 downto 0);
1471
          if( not RTI_Ignores_GP_Flags )then
1472
            Flags(7 downto 4)<= Operand1(7 downto 4);
1473
          end if;
1474 169 jshamlet
 
1475 185 jshamlet
        when ALU_RSP =>
1476 181 jshamlet
          Regfile(0)         <= Stack_Ptr(7 downto 0);
1477
          Regfile(1)         <= Stack_Ptr(15 downto 8);
1478
 
1479 185 jshamlet
        when ALU_GMSK =>
1480
          Flags(PSR_Z)       <= nor_reduce(Int_Mask);
1481
          Regfile(0)         <= Int_Mask;
1482
 
1483 169 jshamlet
        when others =>
1484
          null;
1485
      end case;
1486
 
1487 224 jshamlet
      Open8_Bus.GP_Flags     <= Flags(7 downto 3);
1488 188 jshamlet
 
1489 169 jshamlet
    end if;
1490
  end process;
1491
 
1492 182 jshamlet
-------------------------------------------------------------------------------
1493
-- Multiplier Logic
1494
--
1495
-- We need to infer a hardware multipler, so we create a special clocked
1496
--  process with no reset or clock enable
1497
-------------------------------------------------------------------------------
1498
 
1499
  Multiplier_proc: process( Clock )
1500
  begin
1501
    if( rising_edge(Clock) )then
1502
      Mult                   <= Regfile(0) *
1503 186 jshamlet
                                Regfile(conv_integer(ALU_Ctrl.Reg));
1504
    end if;
1505
  end process;
1506
 
1507
end architecture;

powered by: WebSVN 2.1.0

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