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 263

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 169 jshamlet
 
259
library ieee;
260
  use ieee.std_logic_1164.all;
261
  use ieee.std_logic_unsigned.all;
262
  use ieee.std_logic_arith.all;
263
  use ieee.std_logic_misc.all;
264
 
265
library work;
266 227 jshamlet
  use work.Open8_pkg.all;
267 169 jshamlet
 
268 183 jshamlet
entity o8_cpu is
269 169 jshamlet
  generic(
270
    Program_Start_Addr       : ADDRESS_TYPE := x"0000"; -- Initial PC location
271
    ISR_Start_Addr           : ADDRESS_TYPE := x"FFF0"; -- Bottom of ISR vec's
272
    Stack_Start_Addr         : ADDRESS_TYPE := x"03FF"; -- Top of Stack
273
    Allow_Stack_Address_Move : boolean      := false;   -- Use Normal v8 RSP
274 188 jshamlet
    Stack_Xfer_Flag          : integer      := PSR_GP4; -- GP4 modifies RSP
275 169 jshamlet
    Enable_Auto_Increment    : boolean      := false;   -- Modify indexed instr
276
    BRK_Implements_WAI       : boolean      := false;   -- BRK -> Wait for Int
277
    Enable_NMI               : boolean      := true;    -- Force INTR0 enabled
278 210 jshamlet
    Sequential_Interrupts    : boolean      := false;   -- Interruptable ISRs
279 224 jshamlet
    RTI_Ignores_GP_Flags     : boolean      := false;   -- RTI sets all flags
280 244 jshamlet
    Supervisor_Mode          : boolean      := false;   -- I bit is restricted
281 252 jshamlet
    Unsigned_Index_Offsets   : boolean      := false;   -- Offsets are signed
282 169 jshamlet
    Default_Interrupt_Mask   : DATA_TYPE    := x"FF";   -- Enable all Ints
283 224 jshamlet
    Clock_Frequency          : real                     -- Clock Frequency
284
);
285 169 jshamlet
  port(
286
    Clock                    : in  std_logic;
287 224 jshamlet
    PLL_Locked               : in  std_logic;
288 169 jshamlet
    --
289 225 jshamlet
    Halt_Req                 : in  std_logic := '0';
290
    Halt_Ack                 : out std_logic;
291
    --
292 223 jshamlet
    Open8_Bus                : out OPEN8_BUS_TYPE;
293 169 jshamlet
    Rd_Data                  : in  DATA_TYPE;
294 223 jshamlet
    Interrupts               : in  INTERRUPT_BUNDLE := x"00"
295
);
296 169 jshamlet
end entity;
297
 
298 183 jshamlet
architecture behave of o8_cpu is
299 169 jshamlet
 
300 224 jshamlet
  signal Reset_q             : std_logic := Reset_Level;
301
  signal Reset               : std_logic := Reset_Level;
302
 
303
  constant USEC_VAL          : integer := integer(Clock_Frequency / 1000000.0);
304
  constant USEC_WDT          : integer := ceil_log2(USEC_VAL - 1);
305
  constant USEC_DLY          : std_logic_vector :=
306
                                conv_std_logic_vector(USEC_VAL - 1, USEC_WDT);
307
  signal uSec_Cntr           : std_logic_vector( USEC_WDT - 1 downto 0 );
308
  signal uSec_Tick           : std_logic;
309
 
310 187 jshamlet
  signal CPU_Next_State      : CPU_STATES := IPF_C0;
311
  signal CPU_State           : CPU_STATES := IPF_C0;
312 169 jshamlet
 
313 225 jshamlet
  signal CPU_Halt_Req        : std_logic := '0';
314
  signal CPU_Halt_Ack        : std_logic := '0';
315 187 jshamlet
 
316 169 jshamlet
  signal Cache_Ctrl          : CACHE_MODES := CACHE_IDLE;
317
 
318
  signal Opcode              : OPCODE_TYPE := (others => '0');
319
  signal SubOp, SubOp_p1     : SUBOP_TYPE  := (others => '0');
320
 
321
  signal Prefetch            : DATA_TYPE   := x"00";
322
  signal Operand1, Operand2  : DATA_TYPE   := x"00";
323
 
324
  signal Instr_Prefetch      : std_logic   := '0';
325
 
326
  signal PC_Ctrl             : PC_CTRL_TYPE;
327
  signal Program_Ctr         : ADDRESS_TYPE := x"0000";
328
 
329 182 jshamlet
  signal ALU_Ctrl            : ALU_CTRL_TYPE;
330
  signal Regfile             : REGFILE_TYPE;
331
  signal Flags               : FLAG_TYPE;
332
  signal Mult                : ADDRESS_TYPE := x"0000";
333
 
334 169 jshamlet
  signal SP_Ctrl             : SP_CTRL_TYPE;
335
  signal Stack_Ptr           : ADDRESS_TYPE := x"0000";
336
 
337
  signal DP_Ctrl             : DATA_CTRL_TYPE;
338
 
339
  signal INT_Ctrl            : INT_CTRL_TYPE;
340
  signal Ack_D, Ack_Q, Ack_Q1: std_logic   := '0';
341
  signal Int_Req, Int_Ack    : std_logic   := '0';
342 245 jshamlet
  signal Set_Mask            : std_logic   := '0';
343 169 jshamlet
  signal Int_Mask            : DATA_TYPE   := x"00";
344
  signal i_Ints              : INTERRUPT_BUNDLE := x"00";
345
  signal Pending             : INTERRUPT_BUNDLE := x"00";
346
  signal Wait_for_FSM        : std_logic := '0';
347 210 jshamlet
  signal Wait_for_ISR        : std_logic := '0';
348 169 jshamlet
 
349 254 jshamlet
  alias  ISR_Addr_Base       is ISR_Start_Addr(15 downto 4);
350
  signal ISR_Addr_Offset     : std_logic_vector(3 downto 0) := x"0";
351
 
352
  constant INT_VECTOR_0      : std_logic_vector(3 downto 0) := x"0";
353
  constant INT_VECTOR_1      : std_logic_vector(3 downto 0) := x"2";
354
  constant INT_VECTOR_2      : std_logic_vector(3 downto 0) := x"4";
355
  constant INT_VECTOR_3      : std_logic_vector(3 downto 0) := x"6";
356
  constant INT_VECTOR_4      : std_logic_vector(3 downto 0) := x"8";
357
  constant INT_VECTOR_5      : std_logic_vector(3 downto 0) := x"A";
358
  constant INT_VECTOR_6      : std_logic_vector(3 downto 0) := x"C";
359
  constant INT_VECTOR_7      : std_logic_vector(3 downto 0) := x"E";
360
 
361 255 jshamlet
  signal IDX_Offset_SX       : std_logic := '0';
362
 
363 252 jshamlet
  signal IDX_Offset          : ADDRESS_TYPE := x"0000";
364
 
365 255 jshamlet
  signal IDX_Sel_l           : std_logic_vector(2 downto 0) := "000";
366
  signal IDX_Sel_h           : std_logic_vector(2 downto 0) := "000";
367
 
368 252 jshamlet
  signal IDX_Reg_l           : integer := 0;
369
  signal IDX_Reg_h           : integer := 0;
370
 
371
  signal IDX_NoOffset_Calc   : ADDRESS_TYPE := x"0000";
372
  signal IDX_Offset_Calc     : ADDRESS_TYPE := x"0000";
373
 
374 169 jshamlet
begin
375
 
376 224 jshamlet
-------------------------------------------------------------------------------
377
-- Reset & uSec Tick
378
-------------------------------------------------------------------------------
379 185 jshamlet
 
380 224 jshamlet
  CPU_Reset_Sync: process( Clock, PLL_Locked )
381
  begin
382
    if( PLL_Locked = '0' )then
383
      Reset_q                <= Reset_Level;
384
      Reset                  <= Reset_Level;
385
    elsif( rising_edge(Clock) )then
386
      Reset_q                <= not Reset_Level;
387
      Reset                  <= Reset_q;
388
    end if;
389
  end process;
390
 
391
  uSec_Tick_proc: process( Clock, Reset )
392
  begin
393
    if( Reset = Reset_Level )then
394
      uSec_Cntr              <= USEC_DLY;
395
      uSec_Tick              <= '0';
396
    elsif( rising_edge( Clock ) )then
397
      uSec_Cntr              <= uSec_Cntr - 1;
398
      if( or_reduce(uSec_Cntr) = '0' )then
399
        uSec_Cntr            <= USEC_DLY;
400
      end if;
401
      uSec_Tick              <= nor_reduce(uSec_Cntr);
402
    end if;
403
  end process;
404
 
405
  Open8_Bus.Clock            <= Clock;
406
  Open8_Bus.Reset            <= Reset;
407
  Open8_Bus.uSec_Tick        <= uSec_Tick;
408
 
409 169 jshamlet
-------------------------------------------------------------------------------
410 182 jshamlet
-- Address bus selection/generation logic
411 169 jshamlet
-------------------------------------------------------------------------------
412
 
413 254 jshamlet
  -- Address selection logic based on current CPU state. This is combinatorial,
414
  --  as adding pipeline registration would add a clock cycle to every instr,
415
  --  without really adding the Fmax to compensate.
416
  Address_Logic: process(CPU_State, Operand1, Operand2, IDX_NoOffset_Calc,
417 255 jshamlet
                         IDX_Offset_Calc, ISR_Addr_Offset, Stack_Ptr,
418
                         Program_Ctr )
419 254 jshamlet
  begin
420
    case( CPU_State )is
421
 
422
      when LDA_C2 | STA_C2 =>
423
        Open8_Bus.Address    <= Operand2 & Operand1;
424
 
425
      when LDX_C1 | STX_C1 =>
426
        Open8_Bus.Address    <= IDX_NoOffset_Calc;
427
 
428
      when LDO_C2 | STO_C2 =>
429
        Open8_Bus.Address    <= IDX_Offset_Calc;
430
 
431
      when ISR_C1 | ISR_C2 =>
432
        Open8_Bus.Address    <= ISR_Addr_Base & ISR_Addr_Offset;
433
 
434 255 jshamlet
      when PSH_C1 | POP_C1 |
435
           ISR_C3 | JSR_C1 | JSR_C2 |
436
           RTS_C1 | RTS_C2 | RTS_C3 =>
437 254 jshamlet
        Open8_Bus.Address    <= Stack_Ptr;
438
 
439
      when others =>
440
        Open8_Bus.Address    <= Program_Ctr;
441
 
442
    end case;
443
  end process;
444
 
445 252 jshamlet
  -- The original model treated the offset to LDO/STO as a signed value
446
  --  allowing access to locations -128 to +127 from [Rn+1:Rn]. This isn't
447
  --  always helpful, so the generic allows the CPU to use unsigned math
448
  --  for the offsets. This makes the range 0 to +255 instead.
449 253 jshamlet
 
450 255 jshamlet
  IDX_Offset_SX <= '0' when Unsigned_Index_Offsets else Operand1(7);
451 252 jshamlet
 
452 255 jshamlet
  IDX_Offset(15 downto 8)    <= (others => IDX_Offset_SX);
453 252 jshamlet
  IDX_Offset(7 downto 0)     <= Operand1;
454
 
455
  -- Enable_Auto_Increment uses the LSB to determine whether or not to
456
  --  do the auto-increment, so we need to lock the LSB for each operand
457
  --  if it is enabled. This forces [ODD:EVEN] pairing.
458
 
459 255 jshamlet
  IDX_Sel_l <= (SubOp(2 downto 1) & '0') when Enable_Auto_Increment else
460
               SubOp;
461 252 jshamlet
 
462 255 jshamlet
  IDX_Sel_h <= (SubOp(2 downto 1) & '1') when Enable_Auto_Increment else
463
               SubOp_p1;
464 252 jshamlet
 
465 255 jshamlet
  IDX_Reg_l <= conv_integer(IDX_Sel_l);
466
  IDX_Reg_h <= conv_integer(IDX_Sel_h);
467
 
468 252 jshamlet
  -- Pipeline registers for the indexed and indexed with offset addresses.
469
  Idx_Addr_Calc_proc: process( Clock, Reset )
470 169 jshamlet
    variable Reg, Reg_1      : integer range 0 to 7 := 0;
471
  begin
472 252 jshamlet
    if( Reset = Reset_Level )then
473
      IDX_NoOffset_Calc      <= x"0000";
474
      IDX_Offset_Calc        <= x"0000";
475
    elsif( rising_edge(Clock))then
476
      IDX_NoOffset_Calc      <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l));
477
      IDX_Offset_Calc        <= (Regfile(IDX_Reg_h) & Regfile(IDX_Reg_l)) +
478
                                IDX_Offset;
479 182 jshamlet
    end if;
480 252 jshamlet
  end process;
481 182 jshamlet
 
482
-------------------------------------------------------------------------------
483
-- Combinatorial portion of CPU finite state machine
484
-- State Logic / Instruction Decoding & Execution
485
-------------------------------------------------------------------------------
486
 
487 187 jshamlet
  State_Logic: process(CPU_State, Flags, Int_Mask, CPU_Halt_Req, Opcode,
488 182 jshamlet
                       SubOp , SubOp_p1, Operand1, Operand2, Int_Req )
489
    variable Reg             : integer range 0 to 7 := 0;
490
  begin
491 169 jshamlet
    CPU_Next_State           <= CPU_State;
492
    Cache_Ctrl               <= CACHE_IDLE;
493
    --
494 185 jshamlet
    PC_Ctrl.Oper             <= PC_INCR;
495
    PC_Ctrl.Offset           <= PC_IDLE;
496 182 jshamlet
    --
497 169 jshamlet
    ALU_Ctrl.Oper            <= ALU_IDLE;
498
    ALU_Ctrl.Reg             <= ACCUM;
499
    --
500
    SP_Ctrl.Oper             <= SP_IDLE;
501
    --
502
    DP_Ctrl.Src              <= DATA_RD_MEM;
503
    DP_Ctrl.Reg              <= ACCUM;
504
    --
505
    INT_Ctrl.Mask_Set        <= '0';
506
    INT_Ctrl.Soft_Ints       <= x"00";
507
    INT_Ctrl.Incr_ISR        <= '0';
508
    Ack_D                    <= '0';
509 225 jshamlet
    --
510 182 jshamlet
    Reg                     := conv_integer(SubOp);
511 225 jshamlet
    --
512
    CPU_Halt_Ack             <= '0';
513 169 jshamlet
 
514
    case CPU_State is
515
-------------------------------------------------------------------------------
516
-- Initial Instruction fetch & decode
517
-------------------------------------------------------------------------------
518 187 jshamlet
      when IPF_C0 =>
519
        CPU_Next_State       <= IPF_C1;
520 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
521 169 jshamlet
 
522 187 jshamlet
      when IPF_C1 =>
523
        CPU_Next_State       <= IPF_C2;
524 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
525 169 jshamlet
 
526 187 jshamlet
      when IPF_C2 =>
527
        CPU_Next_State       <= IDC_C0;
528 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
529 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
530 169 jshamlet
 
531 187 jshamlet
      when IDC_C0 =>
532
        CPU_Next_State       <= IDC_C0;
533 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
534
 
535
        case Opcode is
536
          when OP_PSH =>
537
            CPU_Next_State   <= PSH_C1;
538
            Cache_Ctrl       <= CACHE_PREFETCH;
539 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
540 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
541
            DP_Ctrl.Reg      <= SubOp;
542
 
543
          when OP_POP =>
544
            CPU_Next_State   <= POP_C1;
545
            Cache_Ctrl       <= CACHE_PREFETCH;
546 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
547 169 jshamlet
            SP_Ctrl.Oper     <= SP_POP;
548
 
549
          when OP_BR0 | OP_BR1 =>
550
            CPU_Next_State   <= BRN_C1;
551
            Cache_Ctrl       <= CACHE_OPER1;
552 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
553 169 jshamlet
 
554 185 jshamlet
 
555 169 jshamlet
          when OP_DBNZ =>
556
            CPU_Next_State   <= DBNZ_C1;
557
            Cache_Ctrl       <= CACHE_OPER1;
558 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
559 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_DEC;
560
            ALU_Ctrl.Reg     <= SubOp;
561
 
562
          when OP_INT =>
563 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
564 187 jshamlet
            -- Make sure the requested interrupt is actually enabled first.
565
            --  Also, unlike CPU_Halt, the INT instruction is actually being
566
            --  executed, so go ahead and increment the program counter before
567
            --  pausing so the CPU restarts on the next instruction.
568 169 jshamlet
            if( Int_Mask(Reg) = '1' )then
569 187 jshamlet
              CPU_Next_State <= WAI_Cx;
570 169 jshamlet
              INT_Ctrl.Soft_Ints(Reg) <= '1';
571
            end if;
572
 
573
          when OP_STK =>
574
            case SubOp is
575
              when SOP_RSP  =>
576 185 jshamlet
                PC_Ctrl.Offset <= PC_NEXT;
577 181 jshamlet
                if( not Allow_Stack_Address_Move )then
578 187 jshamlet
                  -- The default behavior for this instruction is to simply
579
                  --  repoint the SP to the HDL default
580 185 jshamlet
                  SP_Ctrl.Oper    <= SP_CLR;
581 181 jshamlet
                end if;
582 187 jshamlet
                if( Allow_Stack_Address_Move and
583
                    Flags(Stack_Xfer_Flag) = '1' )then
584
                  -- If RSP is set to allow SP moves, and the specified flag
585
                  --  is true, then signal the stack pointer logic to load
586
                  --  from R1:R0
587 185 jshamlet
                  SP_Ctrl.Oper    <= SP_SET;
588 181 jshamlet
                end if;
589 187 jshamlet
                if( Allow_Stack_Address_Move and
590
                    Flags(Stack_Xfer_Flag) = '0')then
591
                  -- If RSP is set to allow SP moves, and the specified flag
592
                  --  is false, then signal the ALU to copy the stack pointer
593
                  --  to R1:R0
594 185 jshamlet
                  ALU_Ctrl.Oper   <= ALU_RSP;
595 181 jshamlet
                end if;
596 169 jshamlet
 
597
              when SOP_RTS | SOP_RTI =>
598 185 jshamlet
                CPU_Next_State    <= RTS_C1;
599 190 jshamlet
                Cache_Ctrl        <= CACHE_IDLE;
600 185 jshamlet
                SP_Ctrl.Oper      <= SP_POP;
601 169 jshamlet
 
602
              when SOP_BRK  =>
603
                if( BRK_Implements_WAI )then
604 187 jshamlet
                  -- If BRK_Implements_WAI, then jump to the WAI_Cx and
605
                  --  increment the PC similar to an ISR flow.
606
                  CPU_Next_State  <= WAI_Cx;
607 185 jshamlet
                  PC_Ctrl.Offset  <= PC_NEXT;
608 187 jshamlet
                else
609
                -- If Break is implemented normally, back the PC up by
610 260 jshamlet
                --  2 and return through IPF_C0 in order to execute a 3
611 187 jshamlet
                --  clock cycle delay
612
                  CPU_Next_State  <= BRK_C1;
613
                  PC_Ctrl.Offset  <= PC_REV2;
614 169 jshamlet
                end if;
615
 
616
              when SOP_JMP  =>
617 185 jshamlet
                CPU_Next_State    <= JMP_C1;
618
                Cache_Ctrl        <= CACHE_OPER1;
619 169 jshamlet
 
620
              when SOP_SMSK =>
621 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
622 169 jshamlet
                INT_Ctrl.Mask_Set <= '1';
623
 
624
              when SOP_GMSK =>
625 185 jshamlet
                PC_Ctrl.Offset    <= PC_NEXT;
626
                ALU_Ctrl.Oper     <= ALU_GMSK;
627 169 jshamlet
 
628
              when SOP_JSR =>
629
                CPU_Next_State <= JSR_C1;
630 185 jshamlet
                Cache_Ctrl        <= CACHE_OPER1;
631
                DP_Ctrl.Src       <= DATA_WR_PC;
632
                DP_Ctrl.Reg       <= PC_MSB;
633 169 jshamlet
 
634
              when others => null;
635
            end case;
636
 
637
          when OP_MUL =>
638
            CPU_Next_State   <= MUL_C1;
639 181 jshamlet
            -- Multiplication requires a single clock cycle to calculate PRIOR
640
            --  to the ALU writing the result to registers. As a result, this
641
            --  state needs to idle the ALU initially, and back the PC up by 1
642
            -- We can get away with only 1 extra clock by pre-fetching the
643
            --  next instruction, though.
644 169 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
645 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
646 181 jshamlet
            -- Note that both the multiply process AND ALU process need the
647
            --  source register for Rn (R1:R0 = R0 * Rn). Assert ALU_Ctrl.reg
648
            --  now, but hold off on the ALU command until the next state.
649 169 jshamlet
            ALU_Ctrl.Oper    <= ALU_IDLE;
650
            ALU_Ctrl.Reg     <= SubOp;
651
 
652
          when OP_UPP =>
653
            CPU_Next_State   <= UPP_C1;
654
            Cache_Ctrl       <= CACHE_PREFETCH;
655 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
656 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
657
            ALU_Ctrl.Reg     <= SubOp;
658
 
659
          when OP_LDA =>
660
            CPU_Next_State   <= LDA_C1;
661
            Cache_Ctrl       <= CACHE_OPER1;
662
 
663
          when OP_LDI =>
664
            CPU_Next_State   <= LDI_C1;
665
            Cache_Ctrl       <= CACHE_OPER1;
666 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
667 169 jshamlet
 
668
          when OP_LDO =>
669
            CPU_Next_State   <= LDO_C1;
670
            Cache_Ctrl       <= CACHE_OPER1;
671 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
672 169 jshamlet
 
673
          when OP_LDX =>
674
            CPU_Next_State   <= LDX_C1;
675 181 jshamlet
            Cache_Ctrl       <= CACHE_PREFETCH;
676 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
677 169 jshamlet
 
678
          when OP_STA =>
679
            CPU_Next_State   <= STA_C1;
680
            Cache_Ctrl       <= CACHE_OPER1;
681
 
682
          when OP_STO =>
683
            CPU_Next_State   <= STO_C1;
684
            Cache_Ctrl       <= CACHE_OPER1;
685 252 jshamlet
            PC_Ctrl.Offset   <= PC_REV1;
686 169 jshamlet
 
687
          when OP_STX =>
688
            CPU_Next_State   <= STX_C1;
689
            Cache_Ctrl       <= CACHE_PREFETCH;
690 185 jshamlet
            PC_Ctrl.Offset   <= PC_REV2;
691 169 jshamlet
            DP_Ctrl.Src      <= DATA_WR_REG;
692
            DP_Ctrl.Reg      <= ACCUM;
693
 
694 244 jshamlet
          when OP_STP =>
695
            PC_Ctrl.Offset   <= PC_NEXT;
696
            if( Supervisor_Mode )then
697
              if( SubOp /= PSR_I )then
698
                ALU_Ctrl.Oper  <= Opcode;
699
                ALU_Ctrl.Reg   <= SubOp;
700
              end if;
701
            else
702
              ALU_Ctrl.Oper  <= Opcode;
703
              ALU_Ctrl.Reg   <= SubOp;
704
            end if;
705
 
706 169 jshamlet
          when others =>
707 185 jshamlet
            PC_Ctrl.Offset   <= PC_NEXT;
708 169 jshamlet
            ALU_Ctrl.Oper    <= Opcode;
709
            ALU_Ctrl.Reg     <= SubOp;
710
 
711
        end case;
712
 
713 186 jshamlet
        if( Int_Req = '1' )then
714
          CPU_Next_State     <= ISR_C1;
715 187 jshamlet
        end if;
716
 
717
        if( CPU_Halt_Req = '1' )then
718
          CPU_Next_State     <= WAH_Cx;
719
        end if;
720
 
721
        -- If either of these override conditions are true, the decoder needs
722
        --  to undo everything it just setup, since even "single-cycle"
723
        --  instructions will be executed again upon return.
724
        if( Int_Req = '1' or CPU_Halt_Req = '1' )then
725
          -- In either case, we want to skip loading the cache, as the cache
726
          --  will be invalid by the time we get back.
727 186 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
728 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to the current instruction,
729
          -- compensating for the pipeline registers.
730 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
731
          -- Reset all of the sub-block controls to IDLE, to avoid unintended
732 187 jshamlet
          --  operation due to the current instruction.
733 186 jshamlet
          ALU_Ctrl.Oper      <= ALU_IDLE;
734
          SP_Ctrl.Oper       <= SP_IDLE;
735 187 jshamlet
          -- Interrupt logic outside of the state machine needs this to be set
736
          --  to DATA_RD_MEM, while CPU_Halt considers this a "don't care".
737 186 jshamlet
          DP_Ctrl.Src        <= DATA_RD_MEM;
738 187 jshamlet
          -- If an INT/SMSK instruction was going to be executed, it will get
739
          --  executed again when normal processing resumes, so axe their
740
          --  requests for now.
741
          INT_Ctrl.Mask_Set       <= '0';
742
          INT_Ctrl.Soft_Ints(Reg) <= '0';
743 186 jshamlet
        end if;
744
 
745 169 jshamlet
-------------------------------------------------------------------------------
746
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
747
-------------------------------------------------------------------------------
748
 
749
      when BRN_C1 =>
750 187 jshamlet
        CPU_Next_State       <= IDC_C0;
751 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
752 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
753 169 jshamlet
        if( Flags(Reg) = Opcode(0) )then
754 187 jshamlet
          CPU_Next_State     <= IPF_C0;
755 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
756
          PC_Ctrl.Offset     <= Operand1;
757
        end if;
758
 
759
      when DBNZ_C1 =>
760 187 jshamlet
        CPU_Next_State       <= IDC_C0;
761 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
762 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
763
        if( Flags(PSR_Z) = '0' )then
764 187 jshamlet
          CPU_Next_State     <= IPF_C0;
765 169 jshamlet
          Cache_Ctrl         <= CACHE_IDLE;
766
          PC_Ctrl.Offset     <= Operand1;
767
        end if;
768
 
769
      when JMP_C1 =>
770
        CPU_Next_State       <= JMP_C2;
771
        Cache_Ctrl           <= CACHE_OPER2;
772
 
773
      when JMP_C2 =>
774 187 jshamlet
        CPU_Next_State       <= IPF_C0;
775 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
776
 
777
-------------------------------------------------------------------------------
778
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
779
-------------------------------------------------------------------------------
780
 
781
      when LDA_C1 =>
782
        CPU_Next_State       <= LDA_C2;
783
        Cache_Ctrl           <= CACHE_OPER2;
784
 
785
      when LDA_C2 =>
786
        CPU_Next_State       <= LDA_C3;
787
 
788
      when LDA_C3 =>
789
        CPU_Next_State       <= LDA_C4;
790 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
791 169 jshamlet
 
792
      when LDA_C4 =>
793
        CPU_Next_State       <= LDI_C1;
794
        Cache_Ctrl           <= CACHE_OPER1;
795 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
796 169 jshamlet
 
797
      when LDI_C1 =>
798 187 jshamlet
        CPU_Next_State       <= IDC_C0;
799 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
800 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
801 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
802
        ALU_Ctrl.Reg         <= SubOp;
803
 
804
      when LDO_C1 =>
805 252 jshamlet
        CPU_Next_State       <= LDO_C2;
806
 
807
      when LDO_C2 =>
808 181 jshamlet
        CPU_Next_State       <= LDX_C2;
809 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
810 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
811
          ALU_Ctrl.Oper      <= ALU_UPP;
812
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
813 169 jshamlet
        end if;
814
 
815
      when LDX_C1 =>
816
        CPU_Next_State       <= LDX_C2;
817 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
818
          ALU_Ctrl.Oper      <= ALU_UPP;
819
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
820 181 jshamlet
        end if;
821 169 jshamlet
 
822
      when LDX_C2 =>
823
        CPU_Next_State       <= LDX_C3;
824 263 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
825
          ALU_Ctrl.Oper      <= ALU_UPP2;
826
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '1';
827
        end if;
828 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
829 181 jshamlet
 
830
      when LDX_C3 =>
831
        CPU_Next_State       <= LDX_C4;
832 182 jshamlet
        Cache_Ctrl           <= CACHE_OPER1;
833 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
834 169 jshamlet
 
835 181 jshamlet
      when LDX_C4 =>
836 187 jshamlet
        CPU_Next_State       <= IDC_C0;
837 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
838 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
839 181 jshamlet
        ALU_Ctrl.Oper        <= ALU_LDI;
840 169 jshamlet
        ALU_Ctrl.Reg         <= ACCUM;
841
 
842
-------------------------------------------------------------------------------
843
-- Data Storage - Store to memory (STA, STO, STX)
844
-------------------------------------------------------------------------------
845
      when STA_C1 =>
846
        CPU_Next_State       <= STA_C2;
847
        Cache_Ctrl           <= CACHE_OPER2;
848
        DP_Ctrl.Src          <= DATA_WR_REG;
849
        DP_Ctrl.Reg          <= SubOp;
850
 
851
      when STA_C2 =>
852
        CPU_Next_State       <= STA_C3;
853 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
854 169 jshamlet
 
855
      when STA_C3 =>
856 187 jshamlet
        CPU_Next_State       <= IPF_C2;
857 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
858 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
859 169 jshamlet
 
860
      when STO_C1 =>
861 252 jshamlet
        CPU_Next_State       <= STO_C2;
862 169 jshamlet
        Cache_Ctrl           <= CACHE_PREFETCH;
863 252 jshamlet
        DP_Ctrl.Src          <= DATA_WR_REG;
864
        DP_Ctrl.Reg          <= ACCUM;
865
 
866
      when STO_C2 =>
867
        CPU_Next_State       <= IPF_C1;
868 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
869 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
870 252 jshamlet
          CPU_Next_State     <= STO_C3;
871 182 jshamlet
          ALU_Ctrl.Oper      <= ALU_UPP;
872
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
873 169 jshamlet
        end if;
874
 
875 252 jshamlet
      when STO_C3 =>
876
        CPU_Next_State       <= IPF_C2;
877 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
878 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
879
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
880
 
881
      when STX_C1 =>
882 187 jshamlet
        CPU_Next_State       <= IPF_C1;
883 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
884 182 jshamlet
        if( Enable_Auto_Increment and SubOp(0) = '1' )then
885
          CPU_Next_State     <= STX_C2;
886
          ALU_Ctrl.Oper      <= ALU_UPP;
887
          ALU_Ctrl.Reg       <= SubOp(2 downto 1) & '0';
888 169 jshamlet
        end if;
889
 
890
      when STX_C2 =>
891 187 jshamlet
        CPU_Next_State       <= IPF_C2;
892 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
893 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
894
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
895
 
896
-------------------------------------------------------------------------------
897
-- Multi-Cycle Math Operations (UPP, MUL)
898
-------------------------------------------------------------------------------
899
 
900
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
901 181 jshamlet
      --  instruction/first operand, we have to return through PF2. Also, we
902
      --  need to tell the ALU to store the results to R1:R0 here. Note that
903
      --  there is no ALU_Ctrl.Reg, as this is implied in the ALU instruction
904 169 jshamlet
      when MUL_C1 =>
905 187 jshamlet
        CPU_Next_State       <= IPF_C2;
906 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
907 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_MUL;
908
 
909
      when UPP_C1 =>
910 187 jshamlet
        CPU_Next_State       <= IPF_C2;
911 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
912 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_UPP2;
913
        ALU_Ctrl.Reg         <= SubOp_p1;
914
 
915
-------------------------------------------------------------------------------
916
-- Basic Stack Manipulation (PSH, POP, RSP)
917
-------------------------------------------------------------------------------
918
      when PSH_C1 =>
919 187 jshamlet
        CPU_Next_State       <= IPF_C1;
920 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
921
 
922
      when POP_C1 =>
923
        CPU_Next_State       <= POP_C2;
924
 
925
      when POP_C2 =>
926
        CPU_Next_State       <= POP_C3;
927 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
928 169 jshamlet
 
929
      when POP_C3 =>
930
        CPU_Next_State       <= POP_C4;
931
        Cache_Ctrl           <= CACHE_OPER1;
932 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
933 169 jshamlet
 
934
      when POP_C4 =>
935 187 jshamlet
        CPU_Next_State       <= IDC_C0;
936 169 jshamlet
        Cache_Ctrl           <= CACHE_INSTR;
937 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
938 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_POP;
939
        ALU_Ctrl.Reg         <= SubOp;
940 172 jshamlet
 
941 169 jshamlet
-------------------------------------------------------------------------------
942
-- Subroutines & Interrupts (RTS, JSR)
943
-------------------------------------------------------------------------------
944 187 jshamlet
      when WAI_Cx => -- For soft interrupts only, halt the Program_Ctr
945 169 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
946 186 jshamlet
        if( Int_Req = '1' )then
947
          CPU_Next_State     <= ISR_C1;
948 187 jshamlet
          -- Rewind the PC by 3 to put the PC back to would have been the next
949
          --  instruction, compensating for the pipeline registers.
950 186 jshamlet
          PC_Ctrl.Offset     <= PC_REV3;
951
          DP_Ctrl.Src        <= DATA_RD_MEM;
952
        end if;
953 169 jshamlet
 
954 187 jshamlet
      when WAH_Cx => -- Holds until CPU_Halt_Req is deasserted.
955 225 jshamlet
        CPU_Halt_Ack         <= '1';
956 187 jshamlet
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
957
        if( CPU_Halt_Req = '0' )then
958
          CPU_Next_State     <= IPF_C0;
959
          DP_Ctrl.Src        <= DATA_RD_MEM;
960
        end if;
961
 
962
      when BRK_C1 => -- Debugging (BRK) Performs a 5-clock NOP.
963
        CPU_Next_State       <= IPF_C0;
964
 
965 169 jshamlet
      when ISR_C1 =>
966
        CPU_Next_State       <= ISR_C2;
967
        INT_Ctrl.Incr_ISR    <= '1';
968
 
969
      when ISR_C2 =>
970
        CPU_Next_State       <= ISR_C3;
971
        DP_Ctrl.Src          <= DATA_WR_FLAG;
972
 
973
      when ISR_C3 =>
974
        CPU_Next_State       <= JSR_C1;
975
        Cache_Ctrl           <= CACHE_OPER1;
976 182 jshamlet
        ALU_Ctrl.Oper        <= ALU_STP;
977 185 jshamlet
        ALU_Ctrl.Reg         <= conv_std_logic_vector(PSR_I,3);
978 169 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
979
        DP_Ctrl.Src          <= DATA_WR_PC;
980 182 jshamlet
        DP_Ctrl.Reg          <= PC_MSB;
981 169 jshamlet
        Ack_D                <= '1';
982
 
983
      when JSR_C1 =>
984
        CPU_Next_State       <= JSR_C2;
985
        Cache_Ctrl           <= CACHE_OPER2;
986
        SP_Ctrl.Oper         <= SP_PUSH;
987
        DP_Ctrl.Src          <= DATA_WR_PC;
988 182 jshamlet
        DP_Ctrl.Reg          <= PC_LSB;
989 169 jshamlet
 
990
      when JSR_C2 =>
991 187 jshamlet
        CPU_Next_State       <= IPF_C0;
992 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
993 182 jshamlet
        SP_Ctrl.Oper         <= SP_PUSH;
994 169 jshamlet
 
995
      when RTS_C1 =>
996
        CPU_Next_State       <= RTS_C2;
997
        SP_Ctrl.Oper         <= SP_POP;
998
 
999
      when RTS_C2 =>
1000
        CPU_Next_State       <= RTS_C3;
1001
        -- if this is an RTI, then we need to POP the flags
1002
        if( SubOp = SOP_RTI )then
1003
          SP_Ctrl.Oper       <= SP_POP;
1004
        end if;
1005
 
1006
      when RTS_C3 =>
1007
        CPU_Next_State       <= RTS_C4;
1008
        Cache_Ctrl           <= CACHE_OPER1;
1009
 
1010
      when RTS_C4 =>
1011
        CPU_Next_State       <= RTS_C5;
1012
        Cache_Ctrl           <= CACHE_OPER2;
1013
 
1014
      when RTS_C5 =>
1015 187 jshamlet
        CPU_Next_State       <= IPF_C0;
1016 169 jshamlet
        PC_Ctrl.Oper         <= PC_LOAD;
1017 185 jshamlet
        -- if this is an RTI, then we need to clear the I bit
1018 169 jshamlet
        if( SubOp = SOP_RTI )then
1019
          CPU_Next_State     <= RTI_C6;
1020
          Cache_Ctrl         <= CACHE_OPER1;
1021 185 jshamlet
          ALU_Ctrl.Oper      <= ALU_CLP;
1022
          ALU_Ctrl.Reg       <= conv_std_logic_vector(PSR_I,3);
1023 169 jshamlet
        end if;
1024
 
1025
      when RTI_C6 =>
1026 187 jshamlet
        CPU_Next_State       <= IPF_C1;
1027 185 jshamlet
        PC_Ctrl.Offset       <= PC_NEXT;
1028 169 jshamlet
        ALU_Ctrl.Oper        <= ALU_RFLG;
1029
 
1030
      when others =>
1031
        null;
1032
    end case;
1033
 
1034
  end process;
1035
 
1036
-------------------------------------------------------------------------------
1037
-- Registered portion of CPU finite state machine
1038
-------------------------------------------------------------------------------
1039 182 jshamlet
 
1040 169 jshamlet
  CPU_Regs: process( Reset, Clock )
1041
    variable Offset_SX       : ADDRESS_TYPE;
1042 188 jshamlet
    variable i_Ints          : INTERRUPT_BUNDLE := x"00";
1043 169 jshamlet
    variable Index           : integer range 0 to 7         := 0;
1044
    variable Sum             : std_logic_vector(8 downto 0) := "000000000";
1045
    variable Temp            : std_logic_vector(8 downto 0) := "000000000";
1046
  begin
1047
    if( Reset = Reset_Level )then
1048 187 jshamlet
      CPU_State              <= IPF_C0;
1049 260 jshamlet
 
1050
      CPU_Halt_Req           <= '0';
1051
      Halt_Ack               <= '0';
1052
 
1053 169 jshamlet
      Opcode                 <= OP_INC;
1054
      SubOp                  <= ACCUM;
1055
      SubOp_p1               <= ACCUM;
1056
      Operand1               <= x"00";
1057
      Operand2               <= x"00";
1058
      Instr_Prefetch         <= '0';
1059
      Prefetch               <= x"00";
1060
 
1061 223 jshamlet
      Open8_Bus.Wr_En        <= '0';
1062
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1063
      Open8_Bus.Rd_En        <= '1';
1064 169 jshamlet
 
1065
      Program_Ctr            <= Program_Start_Addr;
1066
      Stack_Ptr              <= Stack_Start_Addr;
1067
 
1068
      Ack_Q                  <= '0';
1069
      Ack_Q1                 <= '0';
1070
      Int_Ack                <= '0';
1071
 
1072
      Int_Req                <= '0';
1073
      Pending                <= x"00";
1074
      Wait_for_FSM           <= '0';
1075 210 jshamlet
      Wait_for_ISR           <= '0';
1076 245 jshamlet
      Set_Mask               <= '0';
1077 169 jshamlet
      if( Enable_NMI )then
1078
        Int_Mask             <= Default_Interrupt_Mask(7 downto 1) & '1';
1079
      else
1080
        Int_Mask             <= Default_Interrupt_Mask;
1081
      end if;
1082 254 jshamlet
      ISR_Addr_Offset        <= INT_VECTOR_0;
1083 169 jshamlet
 
1084
      for i in 0 to 7 loop
1085 188 jshamlet
        Regfile(i)           <= x"00";
1086 169 jshamlet
      end loop;
1087
      Flags                  <= x"00";
1088 248 jshamlet
      if( Supervisor_Mode )then
1089 244 jshamlet
        Flags(PSR_I)         <= '1';
1090
      end if;
1091 169 jshamlet
 
1092 224 jshamlet
      Open8_Bus.GP_Flags     <= (others => '0');
1093 188 jshamlet
 
1094 169 jshamlet
    elsif( rising_edge(Clock) )then
1095 187 jshamlet
 
1096 260 jshamlet
      CPU_State              <= CPU_Next_State;
1097
 
1098
-- Register the halt request and acknowledge lines
1099
 
1100 225 jshamlet
      CPU_Halt_Req           <= Halt_Req;
1101
      Halt_Ack               <= CPU_Halt_Ack;
1102 187 jshamlet
 
1103 169 jshamlet
-------------------------------------------------------------------------------
1104
-- Instruction/Operand caching for pipelined memory access
1105
-------------------------------------------------------------------------------
1106 260 jshamlet
 
1107
      -- To avoid putting too much load on the (usually massive) wire-OR'd bus,
1108
      --  the CPU loads Rd_Data into one of four registers - instruction,
1109
      --  operand 1 or 2, or the instruction prefetch registers. The first is
1110
      --  used to decode an instruction when the prefetch isn't valid, while
1111
      --  the two operand registers are used to hold any additional argument
1112
      --  for multi-byte instructions. Because of the memory pipelining, some
1113
      --  longer instructions can cache the next instruction as part of their
1114
      --  execution in a prefetch register, allowing the CPU to skip loading
1115
      --  it again later. Unfortunate, because instructions aren't all the same
1116
      --  length, it is not feasible to cache their operands without adding a
1117
      --  second partial decode stage that would obviate any savings.
1118
 
1119 169 jshamlet
      case Cache_Ctrl is
1120
        when CACHE_INSTR =>
1121
          Opcode             <= Rd_Data(7 downto 3);
1122
          SubOp              <= Rd_Data(2 downto 0);
1123
          SubOp_p1           <= Rd_Data(2 downto 0) + 1;
1124
          if( Instr_Prefetch = '1' )then
1125
            Opcode           <= Prefetch(7 downto 3);
1126
            SubOp            <= Prefetch(2 downto 0);
1127
            SubOp_p1         <= Prefetch(2 downto 0) + 1;
1128
            Instr_Prefetch   <= '0';
1129
          end if;
1130
 
1131
        when CACHE_OPER1 =>
1132
          Operand1           <= Rd_Data;
1133
 
1134
        when CACHE_OPER2 =>
1135
          Operand2           <= Rd_Data;
1136
 
1137
        when CACHE_PREFETCH =>
1138
          Prefetch           <= Rd_Data;
1139
          Instr_Prefetch     <= '1';
1140
 
1141
        when CACHE_IDLE =>
1142
          null;
1143
      end case;
1144
 
1145
-------------------------------------------------------------------------------
1146
-- Program Counter
1147
-------------------------------------------------------------------------------
1148 260 jshamlet
 
1149
      -- The program counter is a bit unusual in that it always subtracts two
1150
      --  from itself plus the signed offset. This is because of the way the
1151
      --  assembler works when computing branches. Thus, to "IDLE" the counter,
1152
      --  the offset is set to 2, while "NEXT" sets the offset to 3. Depending
1153
      --  on how an instruction interacts with memory, or is pipelined,  the
1154
      --  offset can vary from -1 to 3
1155
 
1156 169 jshamlet
      Offset_SX(15 downto 8) := (others => PC_Ctrl.Offset(7));
1157
      Offset_SX(7 downto 0)  := PC_Ctrl.Offset;
1158
 
1159
      case PC_Ctrl.Oper is
1160
        when PC_INCR =>
1161
          Program_Ctr        <= Program_Ctr + Offset_SX - 2;
1162
 
1163
        when PC_LOAD =>
1164 185 jshamlet
          Program_Ctr        <= Operand2 & Operand1;
1165 169 jshamlet
 
1166
        when others =>
1167
          null;
1168
      end case;
1169
 
1170
-------------------------------------------------------------------------------
1171
-- (Write) Data Path
1172
-------------------------------------------------------------------------------
1173 260 jshamlet
 
1174
      -- Note that this code handles both the Rd_En and Wr_En signals. These
1175
      --  were separated to make downstream logic simpler (As opposed to the
1176
      --  more classic RD_WRn and ADDR_STROBE scheme) It is also true to the
1177
      --  original core, which also had separate read and write enable outputs
1178
 
1179
      Open8_Bus.Wr_En        <= '0';
1180
      Open8_Bus.Wr_Data      <= OPEN8_NULLBUS;
1181
      Open8_Bus.Rd_En        <= '0';
1182
 
1183 169 jshamlet
      case DP_Ctrl.Src is
1184
        when DATA_BUS_IDLE =>
1185
          null;
1186
 
1187
        when DATA_RD_MEM =>
1188 223 jshamlet
          Open8_Bus.Rd_En    <= '1';
1189 169 jshamlet
 
1190
        when DATA_WR_REG =>
1191 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1192
          Open8_Bus.Wr_Data  <= Regfile(conv_integer(DP_Ctrl.Reg));
1193 169 jshamlet
 
1194
        when DATA_WR_FLAG =>
1195 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1196
          Open8_Bus.Wr_Data  <= Flags;
1197 169 jshamlet
 
1198
        when DATA_WR_PC =>
1199 223 jshamlet
          Open8_Bus.Wr_En    <= '1';
1200
          Open8_Bus.Wr_Data  <= Program_Ctr(15 downto 8);
1201 182 jshamlet
          if( DP_Ctrl.Reg = PC_LSB )then
1202 223 jshamlet
            Open8_Bus.Wr_Data <= Program_Ctr(7 downto 0);
1203 169 jshamlet
          end if;
1204
 
1205
        when others =>
1206
          null;
1207
      end case;
1208
 
1209
-------------------------------------------------------------------------------
1210
-- Stack Pointer
1211
-------------------------------------------------------------------------------
1212
      case SP_Ctrl.Oper is
1213
        when SP_IDLE =>
1214
          null;
1215
 
1216 181 jshamlet
        when SP_CLR =>
1217 169 jshamlet
          Stack_Ptr          <= Stack_Start_Addr;
1218
 
1219 181 jshamlet
        when SP_SET =>
1220 245 jshamlet
          if( Supervisor_Mode )then
1221
            if( Flags(PSR_I) = '1' )then
1222
              Stack_Ptr      <= Regfile(1) & Regfile(0);
1223
            end if;
1224
          else
1225
            Stack_Ptr        <= Regfile(1) & Regfile(0);
1226
          end if;
1227 181 jshamlet
 
1228 169 jshamlet
        when SP_POP  =>
1229
          Stack_Ptr          <= Stack_Ptr + 1;
1230
 
1231
        when SP_PUSH =>
1232
          Stack_Ptr          <= Stack_Ptr - 1;
1233
 
1234
        when others =>
1235
          null;
1236
 
1237
      end case;
1238
 
1239
-------------------------------------------------------------------------------
1240
-- Interrupt Controller
1241
-------------------------------------------------------------------------------
1242 245 jshamlet
 
1243
      -- If Supervisor_Mode is set, restrict the SMSK instruction such that it
1244
      --  requires the I bit to be set.
1245
      if( Supervisor_Mode )then
1246
        Set_Mask             <= INT_Ctrl.Mask_Set and Flags(PSR_I);
1247
      else
1248
        Set_Mask             <= INT_Ctrl.Mask_Set;
1249
      end if;
1250
 
1251 169 jshamlet
      -- The interrupt control mask is always sourced out of R0
1252 245 jshamlet
      if( Set_Mask = '1' )then
1253 169 jshamlet
        if( Enable_NMI )then
1254
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
1255
        else
1256
          Int_Mask           <= Regfile(conv_integer(ACCUM));
1257
        end if;
1258
      end if;
1259
 
1260
      -- Combine external and internal interrupts, and mask the OR of the two
1261
      --  with the mask. Record any incoming interrupts to the pending buffer
1262
      i_Ints                 := (Interrupts or INT_Ctrl.Soft_Ints) and
1263
                                Int_Mask;
1264 172 jshamlet
 
1265 169 jshamlet
      Pending                <= i_Ints or Pending;
1266
 
1267 260 jshamlet
      -- If Sequential_Interrupts is set true, Wait_for_ISR should follow the
1268
      --  I bit, preventing a new interrupt from starting until the I bit is
1269
      --  cleared.
1270 210 jshamlet
      if( Sequential_Interrupts )then
1271
        Wait_for_ISR         <= Flags(PSR_I);
1272
      else
1273
        Wait_for_ISR         <= '0';
1274
      end if;
1275
 
1276
      if( Wait_for_FSM = '0' and Wait_for_ISR = '0' )then
1277 169 jshamlet
        if(    Pending(0) = '1' )then
1278 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_0;
1279 169 jshamlet
          Pending(0)         <= '0';
1280
        elsif( Pending(1) = '1' )then
1281 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_1;
1282 169 jshamlet
          Pending(1)         <= '0';
1283
        elsif( Pending(2) = '1' )then
1284 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_2;
1285 169 jshamlet
          Pending(2)         <= '0';
1286
        elsif( Pending(3) = '1' )then
1287 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_3;
1288 169 jshamlet
          Pending(3)         <= '0';
1289
        elsif( Pending(4) = '1' )then
1290 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_4;
1291 169 jshamlet
          Pending(4)         <= '0';
1292
        elsif( Pending(5) = '1' )then
1293 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_5;
1294 169 jshamlet
          Pending(5)         <= '0';
1295
        elsif( Pending(6) = '1' )then
1296 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_6;
1297 169 jshamlet
          Pending(6)         <= '0';
1298
        elsif( Pending(7) = '1' )then
1299 254 jshamlet
          ISR_Addr_Offset    <= INT_VECTOR_7;
1300 169 jshamlet
          Pending(7)         <= '0';
1301
        end if;
1302 185 jshamlet
        Wait_for_FSM         <= or_reduce(Pending);
1303 169 jshamlet
      end if;
1304
 
1305
      -- Reset the Wait_for_FSM flag on Int_Ack
1306
      Ack_Q                  <= Ack_D;
1307
      Ack_Q1                 <= Ack_Q;
1308
      Int_Ack                <= Ack_Q1;
1309
      if( Int_Ack = '1' )then
1310
        Wait_for_FSM         <= '0';
1311
      end if;
1312
 
1313
      Int_Req                <= Wait_for_FSM and (not Int_Ack);
1314
 
1315
      -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1316
      --  lower half of the address.
1317
      if( INT_Ctrl.Incr_ISR = '1' )then
1318 254 jshamlet
        ISR_Addr_Offset             <= ISR_Addr_Offset + 1;
1319 169 jshamlet
      end if;
1320
 
1321
-------------------------------------------------------------------------------
1322
-- ALU (Arithmetic / Logic Unit)
1323
-------------------------------------------------------------------------------
1324 260 jshamlet
 
1325
      -- The ALU code is responsible for (and should be the only code altering)
1326
      --  the register file. Most of the "instructions" directly map to opcodes
1327
      --  but a few are for internal use only, such as operations involving the
1328 263 jshamlet
      --  stack pointer or interrupt mask.
1329 260 jshamlet
 
1330 169 jshamlet
      Index                  := conv_integer(ALU_Ctrl.Reg);
1331
      Sum                    := (others => '0');
1332
      Temp                   := (others => '0');
1333
 
1334
      case ALU_Ctrl.Oper is
1335
        when ALU_INC => -- Rn = Rn + 1 : Flags N,C,Z
1336
          Sum                := ("0" & x"01") +
1337
                                ("0" & Regfile(Index));
1338 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1339
          Flags(PSR_C)       <= Sum(8);
1340 209 jshamlet
          Flags(PSR_N)       <= Sum(7);
1341 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1342
 
1343
        when ALU_UPP => -- Rn = Rn + 1
1344
          Sum                := ("0" & x"01") +
1345
                                ("0" & Regfile(Index));
1346 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1347 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1348
 
1349
        when ALU_UPP2 => -- Rn = Rn + C
1350 263 jshamlet
          Sum                := (x"00" & Flags(PSR_C)) +
1351
                                ("0" & Regfile(Index));
1352 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1353 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1354
 
1355
        when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1356
          Sum                := ("0" & Regfile(0)) +
1357
                                ("0" & Regfile(Index)) +
1358 185 jshamlet
                                Flags(PSR_C);
1359
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1360
          Flags(PSR_C)       <= Sum(8);
1361
          Flags(PSR_N)       <= Sum(7);
1362 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1363
 
1364
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1365
          Temp               := "0" & Regfile(Index);
1366 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1367
          Flags(PSR_N)       <= Temp(7);
1368 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1369
 
1370
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1371
          Temp(7 downto 0)   := Regfile(0) or Regfile(Index);
1372 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1373
          Flags(PSR_N)       <= Temp(7);
1374 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1375
 
1376
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1377
          Temp(7 downto 0)   := Regfile(0) and Regfile(Index);
1378 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1379
          Flags(PSR_N)       <= Temp(7);
1380 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1381
 
1382
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1383
          Temp(7 downto 0)   := Regfile(0) xor Regfile(Index);
1384 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1385
          Flags(PSR_N)       <= Temp(7);
1386 169 jshamlet
          Regfile(0)         <= Temp(7 downto 0);
1387
 
1388
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1389 185 jshamlet
          Temp               := Regfile(Index) & Flags(PSR_C);
1390
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1391
          Flags(PSR_C)       <= Temp(8);
1392
          Flags(PSR_N)       <= Temp(7);
1393 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1394
 
1395
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1396 185 jshamlet
          Temp               := Regfile(Index)(0) & Flags(PSR_C) &
1397 169 jshamlet
                                Regfile(Index)(7 downto 1);
1398 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1399
          Flags(PSR_C)       <= Temp(8);
1400
          Flags(PSR_N)       <= Temp(7);
1401 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1402
 
1403
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1404
          Sum                := ("0" & Regfile(Index)) +
1405
                                ("0" & x"FF");
1406 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1407
          Flags(PSR_C)       <= Sum(8);
1408
          Flags(PSR_N)       <= Sum(7);
1409 169 jshamlet
          Regfile(Index)     <= Sum(7 downto 0);
1410
 
1411
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1412
          Sum                := ("0" & Regfile(0)) +
1413
                                ("1" & (not Regfile(Index))) +
1414 185 jshamlet
                                Flags(PSR_C);
1415
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1416
          Flags(PSR_C)       <= Sum(8);
1417
          Flags(PSR_N)       <= Sum(7);
1418 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1419
 
1420
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1421
          Sum                := ("0" & Regfile(0)) +
1422
                                ("0" & Regfile(Index));
1423 185 jshamlet
          Flags(PSR_C)       <= Sum(8);
1424 169 jshamlet
          Regfile(0)         <= Sum(7 downto 0);
1425 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1426
          Flags(PSR_N)       <= Sum(7);
1427 169 jshamlet
 
1428
        when ALU_STP => -- Sets bit(n) in the Flags register
1429
          Flags(Index)       <= '1';
1430
 
1431
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1432 185 jshamlet
          Flags(PSR_Z)       <= not Regfile(0)(Index);
1433
          Flags(PSR_N)       <= Regfile(0)(7);
1434 169 jshamlet
 
1435
        when ALU_CLP => -- Clears bit(n) in the Flags register
1436
          Flags(Index)       <= '0';
1437
 
1438
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1439
          Temp               := "0" & Regfile(0);
1440 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Temp(7 downto 0));
1441
          Flags(PSR_N)       <= Temp(7);
1442 169 jshamlet
          Regfile(Index)     <= Temp(7 downto 0);
1443
 
1444
        when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1445
          Sum                := ("0" & Regfile(0)) +
1446
                                ("1" & (not Regfile(Index))) +
1447
                                '1';
1448 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Sum(7 downto 0));
1449
          Flags(PSR_C)       <= Sum(8);
1450
          Flags(PSR_N)       <= Sum(7);
1451 169 jshamlet
 
1452
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1453
          Regfile(0)         <= Mult(7 downto 0);
1454
          Regfile(1)         <= Mult(15 downto 8);
1455 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Mult);
1456 169 jshamlet
 
1457
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1458 185 jshamlet
          Flags(PSR_Z)       <= nor_reduce(Operand1);
1459
          Flags(PSR_N)       <= Operand1(7);
1460
          Regfile(Index)     <= Operand1;
1461 169 jshamlet
 
1462
        when ALU_POP => -- Rn <= Data
1463 185 jshamlet
          Regfile(Index)     <= Operand1;
1464 169 jshamlet
 
1465
        when ALU_RFLG =>
1466 188 jshamlet
          Flags(3 downto 0)  <= Operand1(3 downto 0);
1467
          if( not RTI_Ignores_GP_Flags )then
1468
            Flags(7 downto 4)<= Operand1(7 downto 4);
1469
          end if;
1470 169 jshamlet
 
1471 185 jshamlet
        when ALU_RSP =>
1472 181 jshamlet
          Regfile(0)         <= Stack_Ptr(7 downto 0);
1473
          Regfile(1)         <= Stack_Ptr(15 downto 8);
1474
 
1475 185 jshamlet
        when ALU_GMSK =>
1476
          Flags(PSR_Z)       <= nor_reduce(Int_Mask);
1477
          Regfile(0)         <= Int_Mask;
1478
 
1479 169 jshamlet
        when others =>
1480
          null;
1481
      end case;
1482
 
1483 224 jshamlet
      Open8_Bus.GP_Flags     <= Flags(7 downto 3);
1484 188 jshamlet
 
1485 169 jshamlet
    end if;
1486
  end process;
1487
 
1488 182 jshamlet
-------------------------------------------------------------------------------
1489
-- Multiplier Logic
1490
--
1491
-- We need to infer a hardware multipler, so we create a special clocked
1492
--  process with no reset or clock enable
1493
-------------------------------------------------------------------------------
1494
 
1495
  Multiplier_proc: process( Clock )
1496
  begin
1497
    if( rising_edge(Clock) )then
1498
      Mult                   <= Regfile(0) *
1499 186 jshamlet
                                Regfile(conv_integer(ALU_Ctrl.Reg));
1500
    end if;
1501
  end process;
1502
 
1503
end architecture;

powered by: WebSVN 2.1.0

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