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 314

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

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

powered by: WebSVN 2.1.0

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