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 316

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

powered by: WebSVN 2.1.0

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