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 169

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

Line No. Rev Author Line
1 169 jshamlet
-- Copyright (c)2006,2011,2012,2013,2015 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
--
24
-- VHDL Units :  Open8_CPU
25
-- Description:  VHDL model of a RISC 8-bit processor core loosely based on the
26
--            :   V8/ARC uRISC instruction set. Requires Open8_pkg.vhd
27
--            :
28
-- Notes      :  Generic definitions
29
--            :
30
--            :  Program_Start_Addr sets the initial value of the program
31
--            :   counter.
32
--            :
33
--            :  ISR_Start_Addr sets the location of the interrupt service
34
--            :   vector table. There are 8 service vectors, or 16 bytes, which
35
--            :   must be allocated to either ROM or RAM.
36
--            :
37
--            :  Stack_Start_Address sets the initial (reset) value of the
38
--            :   stack pointer. Also used for the RSP instruction if
39
--            :   Allow_Stack_Address_Move is false.
40
--            :
41
--            :  Allow_Stack_Address_Move, when set true, allows the RSP to be
42
--            :   programmed via thet RSP instruction. If enabled, the contents
43
--            :   of R1:R0 are used to initialize the stack pointer.
44
--            :
45
--            :  The Enable_Auto_Increment generic can be used to modify the
46
--            :   indexed instructions such that specifying an odd register
47
--            :   will use the next lower register pair, post-incrementing the
48
--            :   value in that pair. IOW, specifying STX R1 will instead
49
--            :   result in STX R0++, or R0 = {R1:R0}; {R1:R0} + 1
50
--            :
51
--            :  BRK_Implements_WAI modifies the BRK instruction such that it
52
--            :   triggers the wait for interrupt state, but without triggering
53
--            :   a soft interrupt in lieu of its normal behavior, which is to
54
--            :   insert several dead clock cycles - essentially a long NOP
55
--            :
56
--            :  Enable_NMI overrides the mask bit for interrupt 0, creating a
57
--            :   non-maskable interrupt at the highest priority. To remain
58
--            :   true to the original core, this should be set false.
59
--            :
60
--            :  Default_Interrupt_Mask sets the intial/reset value of the
61
--            :   interrupt mask. To remain true to the original core, which
62
--            :   had no interrupt mask, this should be set to x"FF". Otherwise
63
--            :   it can be initialized to any value. Note that Enable_NMI
64
--            :   will logically force the LSB high.
65
--            : 
66
--            :  Reset_Level determines whether the processor registers reset
67
--            :   on a high or low level from higher logic.
68
--            :
69
--            : Architecture notes
70
--            :  This model deviates from the original ISA in a few important
71
--            :   ways.
72
--            :
73
--            :  First, there is only one set of registers. Interrupt service
74
--            :   routines must explicitely preserve context since the the
75
--            :   hardware doesn't. This was done to decrease size and code
76
--            :   complexity. Older code that assumes this behavior will not
77
--            :   execute correctly on this processor model.
78
--            :
79
--            :  Second, this model adds an additional pipeline stage between
80
--            :   the instruction decoder and the ALU. Unfortunately, this
81
--            :   means that the instruction stream has to be restarted after
82
--            :   any math instruction is executed, implying that any ALU
83
--            :   instruction now has a latency of 2 instead of 0. The
84
--            :   advantage is that the maximum frequency has gone up
85
--            :   significantly, as the ALU code is vastly more efficient.
86
--            :   As an aside, this now means that all math instructions,
87
--            :   including MUL (see below) and UPP have the same instruction
88
--            :   latency.
89
--            :
90
--            :  Third, the original ISA, also a soft core, had two reserved
91
--            :   instructions, USR and USR2. These have been implemented as
92
--            :   DBNZ, and MUL respectively.
93
--            :
94
--            :  DBNZ decrements the specified register and branches if the
95
--            :   result is non-zero. The instruction effectively executes a
96
--            :   DEC Rn instruction prior to branching, so the same flags will
97
--            :   be set.
98
--            :
99
--            :  MUL places the result of R0 * Rn into R1:R0. Instruction
100
--            :   latency is identical to other ALU instructions. Only the Z
101
--            :   flag is set, since there is no defined overflow or "negative
102
--            :   16-bit values"
103
--            :
104
--            :  Fourth, indexed load/store instructions now have an (optional)
105
--            :   ability to post-increment their index registers. If enabled,
106
--            :   using an odd operand for LDO,LDX, STO, STX will cause the
107
--            :   register pair to be incremented after the storage access.
108
--            :
109
--            :  Fifth, the RSP instruction has been (optionally) altered to
110
--            :   allow the stack pointer to be sourced from R1:R0.
111
--            :
112
--            :  Sixth, the BRK instruction can optionally implement a WAI,
113
--            :   which is the same as the INT instruction without the soft
114
--            :   interrupt, as a way to put the processor to "sleep" until the
115
--            :   next external interrupt.
116
--            :
117
--            :  Seventh, the original CPU model had 8 non-maskable interrupts
118
--            :   with priority. This model has the same 8 interrupts, but
119
--            :   allows software to mask them (with an additional option to 
120
--            :   override the highest priority interrupt, making it the NMI.)
121
--            :
122
--            :  Lastly, previous unmapped instructions in the OP_STK opcode
123
--            :   were repurposed to support a new interrupt mask.
124
--            :   SMSK and GMSK transfer the contents of R0 (accumulator)
125
--            :   to/from the interrupt mask register. SMSK is immediate, while
126
--            :   GMSK has the same overhead as a math instruction.
127
--
128
-- Revision History
129
-- Author          Date     Change
130
------------------ -------- ---------------------------------------------------
131
-- Seth Henry      07/19/06 Design Start
132
-- Seth Henry      01/18/11 Fixed BTT instruction to match V8
133
-- Seth Henry      07/22/11 Fixed interrupt transition logic to avoid data
134
--                           corruption issues.
135
-- Seth Henry      07/26/11 Optimized logic in ALU, stack pointer, and data
136
--                           path sections.
137
-- Seth Henry      07/27/11 Optimized logic for timing, merged blocks into
138
--                           single entity.
139
-- Seth Henry      09/20/11 Added BRK_Implements_WAI option, allowing the
140
--                           processor to wait for an interrupt instead of the
141
--                           normal BRK behavior.
142
-- Seth Henry      12/20/11 Modified core to allow WAIT_FOR_INT state to idle
143
--                           the bus entirely (Rd_Enable is low)
144
-- Seth Henry      02/03/12 Replaced complex interrupt controller with simpler,
145
--                           faster logic that simply does priority encoding.
146
-- Seth Henry      08/06/13 Removed HALT functionality
147
-- Seth Henry      10/29/15 Fixed inverted carry logic in CMP and SBC instrs
148
 
149
library ieee;
150
  use ieee.std_logic_1164.all;
151
  use ieee.std_logic_unsigned.all;
152
  use ieee.std_logic_arith.all;
153
  use ieee.std_logic_misc.all;
154
 
155
library work;
156
use work.Open8_pkg.all;
157
 
158
entity Open8_CPU is
159
  generic(
160
    Program_Start_Addr       : ADDRESS_TYPE := x"0000"; -- Initial PC location
161
    ISR_Start_Addr           : ADDRESS_TYPE := x"FFF0"; -- Bottom of ISR vec's
162
    Stack_Start_Addr         : ADDRESS_TYPE := x"03FF"; -- Top of Stack
163
    Allow_Stack_Address_Move : boolean      := false;   -- Use Normal v8 RSP
164
    Enable_Auto_Increment    : boolean      := false;   -- Modify indexed instr
165
    BRK_Implements_WAI       : boolean      := false;   -- BRK -> Wait for Int
166
    Enable_NMI               : boolean      := true;    -- Force INTR0 enabled
167
    Default_Interrupt_Mask   : DATA_TYPE    := x"FF";   -- Enable all Ints
168
    Reset_Level              : std_logic    := '0' );   -- Active reset level
169
  port(
170
    Clock                    : in  std_logic;
171
    Reset                    : in  std_logic;
172
    Interrupts               : in  INTERRUPT_BUNDLE;
173
    --
174
    Address                  : out ADDRESS_TYPE;
175
    Rd_Data                  : in  DATA_TYPE;
176
    Rd_Enable                : out std_logic;
177
    Wr_Data                  : out DATA_TYPE;
178
    Wr_Enable                : out std_logic );
179
end entity;
180
 
181
architecture behave of Open8_CPU is
182
 
183
  constant INT_VECTOR_0      : ADDRESS_TYPE := ISR_Start_Addr;
184
  constant INT_VECTOR_1      : ADDRESS_TYPE := ISR_Start_Addr+2;
185
  constant INT_VECTOR_2      : ADDRESS_TYPE := ISR_Start_Addr+4;
186
  constant INT_VECTOR_3      : ADDRESS_TYPE := ISR_Start_Addr+6;
187
  constant INT_VECTOR_4      : ADDRESS_TYPE := ISR_Start_Addr+8;
188
  constant INT_VECTOR_5      : ADDRESS_TYPE := ISR_Start_Addr+10;
189
  constant INT_VECTOR_6      : ADDRESS_TYPE := ISR_Start_Addr+12;
190
  constant INT_VECTOR_7      : ADDRESS_TYPE := ISR_Start_Addr+14;
191
 
192
  signal Halt                : std_logic;
193
 
194
  signal CPU_Next_State      : CPU_STATES := PIPE_FILL_0;
195
  signal CPU_State           : CPU_STATES := PIPE_FILL_0;
196
 
197
  signal Cache_Ctrl          : CACHE_MODES := CACHE_IDLE;
198
 
199
  signal Opcode              : OPCODE_TYPE := (others => '0');
200
  signal SubOp, SubOp_p1     : SUBOP_TYPE  := (others => '0');
201
 
202
  signal Prefetch            : DATA_TYPE   := x"00";
203
  signal Operand1, Operand2  : DATA_TYPE   := x"00";
204
 
205
  signal Instr_Prefetch      : std_logic   := '0';
206
 
207
  signal PC_Ctrl             : PC_CTRL_TYPE;
208
  signal Program_Ctr         : ADDRESS_TYPE := x"0000";
209
 
210
  signal SP_Ctrl             : SP_CTRL_TYPE;
211
  signal Stack_Ptr           : ADDRESS_TYPE := x"0000";
212
 
213
  signal DP_Ctrl             : DATA_CTRL_TYPE;
214
 
215
  signal INT_Ctrl            : INT_CTRL_TYPE;
216
  signal Ack_D, Ack_Q, Ack_Q1: std_logic   := '0';
217
  signal Int_Req, Int_Ack    : std_logic   := '0';
218
  signal Int_Mask            : DATA_TYPE   := x"00";
219
  signal ISR_Addr            : ADDRESS_TYPE := x"0000";
220
  signal i_Ints              : INTERRUPT_BUNDLE := x"00";
221
  signal Pending             : INTERRUPT_BUNDLE := x"00";
222
  signal Wait_for_FSM        : std_logic := '0';
223
 
224
  signal ALU_Ctrl            : ALU_CTRL_TYPE;
225
  signal Regfile             : REGFILE_TYPE;
226
  signal Flags               : FLAG_TYPE;
227
  signal Mult                : ADDRESS_TYPE := x"0000";
228
 
229
begin
230
 
231
-------------------------------------------------------------------------------
232
-- State Logic / Instruction Decoding & Execution
233
-- Combinatorial portion of CPU finite state machine
234
-------------------------------------------------------------------------------
235
 
236
  State_Logic: process(CPU_State, Regfile, Flags, Int_Mask, Opcode,
237
                       SubOp , SubOp_p1, Operand1, Operand2, Int_Req,
238
                       Program_Ctr, Stack_Ptr, ISR_Addr )
239
    variable Reg, Reg_1      : integer range 0 to 7 := 0;
240
    variable Offset_SX       : ADDRESS_TYPE;
241
  begin
242
    CPU_Next_State           <= CPU_State;
243
    Cache_Ctrl               <= CACHE_IDLE;
244
    --
245
    ALU_Ctrl.Oper            <= ALU_IDLE;
246
    ALU_Ctrl.Reg             <= ACCUM;
247
    ALU_Ctrl.Data            <= x"00";
248
    --
249
    PC_Ctrl.Oper             <= PC_IDLE;
250
    PC_Ctrl.Offset           <= x"03";
251
    PC_Ctrl.Addr             <= x"0000";
252
    --
253
    SP_Ctrl.Oper             <= SP_IDLE;
254
    --
255
    Address                  <= Program_Ctr;
256
    --
257
    DP_Ctrl.Src              <= DATA_RD_MEM;
258
    DP_Ctrl.Reg              <= ACCUM;
259
    --
260
    INT_Ctrl.Mask_Set        <= '0';
261
    INT_Ctrl.Soft_Ints       <= x"00";
262
    INT_Ctrl.Incr_ISR        <= '0';
263
    Ack_D                    <= '0';
264
 
265
    -- Assign the most common value of Reg and Reg1 outside the case structure
266
    --  to simplify things.
267
    Reg                      := conv_integer(SubOp);
268
    Reg_1                    := conv_integer(SubOp_p1);
269
    Offset_SX(15 downto 0)   := (others => Operand1(7));
270
    Offset_SX(7 downto 0)    := Operand1;
271
 
272
    case CPU_State is
273
-------------------------------------------------------------------------------
274
-- Initial Instruction fetch & decode
275
-------------------------------------------------------------------------------
276
      when PIPE_FILL_0 =>
277
        CPU_Next_State       <= PIPE_FILL_1;
278
        PC_Ctrl.Oper         <= PC_INCR;
279
 
280
      when PIPE_FILL_1 =>
281
        CPU_Next_State       <= PIPE_FILL_2;
282
        PC_Ctrl.Oper         <= PC_INCR;
283
 
284
      when PIPE_FILL_2 =>
285
        CPU_Next_State       <= INSTR_DECODE;
286
        Cache_Ctrl           <= CACHE_INSTR;
287
        PC_Ctrl.Oper         <= PC_INCR;
288
 
289
      when INSTR_DECODE =>
290
        CPU_Next_State       <= INSTR_DECODE;
291
        Cache_Ctrl           <= CACHE_INSTR;
292
 
293
        case Opcode is
294
          when OP_PSH =>
295
            CPU_Next_State   <= PSH_C1;
296
            Cache_Ctrl       <= CACHE_PREFETCH;
297
            PC_Ctrl.Oper     <= PC_REV1;
298
            DP_Ctrl.Src      <= DATA_WR_REG;
299
            DP_Ctrl.Reg      <= SubOp;
300
 
301
          when OP_POP =>
302
            CPU_Next_State   <= POP_C1;
303
            Cache_Ctrl       <= CACHE_PREFETCH;
304
            PC_Ctrl.Oper     <= PC_REV2;
305
            SP_Ctrl.Oper     <= SP_POP;
306
 
307
          when OP_BR0 | OP_BR1 =>
308
            CPU_Next_State   <= BRN_C1;
309
            Cache_Ctrl       <= CACHE_OPER1;
310
            PC_Ctrl.Oper     <= PC_INCR;
311
 
312
          when OP_DBNZ =>
313
            CPU_Next_State   <= DBNZ_C1;
314
            Cache_Ctrl       <= CACHE_OPER1;
315
            PC_Ctrl.Oper     <= PC_INCR;
316
            ALU_Ctrl.Oper    <= ALU_DEC;
317
            ALU_Ctrl.Reg     <= SubOp;
318
 
319
          when OP_INT =>
320
            PC_Ctrl.Oper     <= PC_INCR;
321
            -- Make sure the requested interrupt is actually enabled first
322
            if( Int_Mask(Reg) = '1' )then
323
              CPU_Next_State <= WAIT_FOR_INT;
324
              INT_Ctrl.Soft_Ints(Reg) <= '1';
325
            end if;
326
 
327
          when OP_STK =>
328
            case SubOp is
329
              when SOP_RSP  =>
330
                PC_Ctrl.Oper <= PC_INCR;
331
                SP_Ctrl.Oper <= SP_RSET;
332
 
333
              when SOP_RTS | SOP_RTI =>
334
                CPU_Next_State <= RTS_C1;
335
                Cache_Ctrl   <= CACHE_IDLE;
336
                SP_Ctrl.Oper <= SP_POP;
337
 
338
              when SOP_BRK  =>
339
                CPU_Next_State   <= BRK_C1;
340
                PC_Ctrl.Oper     <= PC_REV2;
341
                -- If Break implements Wait for Interrupt, replace the normal
342
                --  flow with a modified version of the INT instruction
343
                if( BRK_Implements_WAI )then
344
                  CPU_Next_State <= WAIT_FOR_INT;
345
                  PC_Ctrl.Oper   <= PC_INCR;
346
                end if;
347
 
348
              when SOP_JMP  =>
349
                CPU_Next_State <= JMP_C1;
350
                Cache_Ctrl   <= CACHE_OPER1;
351
 
352
              when SOP_SMSK =>
353
                PC_Ctrl.Oper <= PC_INCR;
354
                INT_Ctrl.Mask_Set <= '1';
355
 
356
              when SOP_GMSK =>
357
                PC_Ctrl.Oper <= PC_INCR;
358
                ALU_Ctrl.Oper<= ALU_LDI;
359
                ALU_Ctrl.Reg <= ACCUM;
360
                ALU_Ctrl.Data<= Int_Mask;
361
 
362
              when SOP_JSR =>
363
                CPU_Next_State <= JSR_C1;
364
                Cache_Ctrl   <= CACHE_OPER1;
365
                DP_Ctrl.Src  <= DATA_WR_PC;
366
                DP_Ctrl.Reg  <= ACCUM+1;
367
 
368
              when others => null;
369
            end case;
370
 
371
          when OP_MUL =>
372
            CPU_Next_State   <= MUL_C1;
373
            -- We need to back the PC up by 1, and allow it to refill. An
374
            --  unfortunate consequence of the pipelining. We can get away with
375
            --  only 1 extra clock by pre-fetching the next instruction, though
376
            Cache_Ctrl       <= CACHE_PREFETCH;
377
            PC_Ctrl.Oper     <= PC_REV1;
378
            -- Multiplication is automatic, but requires a single clock cycle.
379
            --  We need to specify the register for Rn (R1:R0 = R0 * Rn) now,
380
            --   but will issue the multiply command on the next clock to copy
381
            --   the results to the specified register.
382
            ALU_Ctrl.Oper    <= ALU_IDLE;
383
            ALU_Ctrl.Reg     <= SubOp;
384
 
385
          when OP_UPP =>
386
            CPU_Next_State   <= UPP_C1;
387
            Cache_Ctrl       <= CACHE_PREFETCH;
388
            PC_Ctrl.Oper     <= PC_REV1;
389
            ALU_Ctrl.Oper    <= Opcode;
390
            ALU_Ctrl.Reg     <= SubOp;
391
 
392
          when OP_LDA =>
393
            CPU_Next_State   <= LDA_C1;
394
            Cache_Ctrl       <= CACHE_OPER1;
395
 
396
          when OP_LDI =>
397
            CPU_Next_State   <= LDI_C1;
398
            Cache_Ctrl       <= CACHE_OPER1;
399
            PC_Ctrl.Oper     <= PC_INCR;
400
 
401
          when OP_LDO =>
402
            CPU_Next_State   <= LDO_C1;
403
            Cache_Ctrl       <= CACHE_OPER1;
404
            PC_Ctrl.Oper     <= PC_REV2;
405
 
406
          when OP_LDX =>
407
            CPU_Next_State   <= LDX_C1;
408
            PC_Ctrl.Oper     <= PC_REV2;
409
            -- If auto-increment is disabled, use the specified register pair,
410
            --  otherwise, for an odd:even pair, and issue the first half of
411
            --  a UPP instruction to the ALU
412
            if( not Enable_Auto_Increment )then
413
              Address        <= Regfile(Reg_1) & Regfile(Reg);
414
            else
415
              Reg            := conv_integer(SubOp(2 downto 1) & '0');
416
              Reg_1          := conv_integer(SubOp(2 downto 1) & '1');
417
              Address        <= Regfile(Reg_1) & Regfile(Reg);
418
              if( SubOp(0) = '1' )then
419
                ALU_Ctrl.Oper<= ALU_UPP;
420
                ALU_Ctrl.Reg <= SubOp(2 downto 1) & '0';
421
              end if;
422
            end if;
423
 
424
          when OP_STA =>
425
            CPU_Next_State   <= STA_C1;
426
            Cache_Ctrl       <= CACHE_OPER1;
427
 
428
          when OP_STO =>
429
            CPU_Next_State   <= STO_C1;
430
            Cache_Ctrl       <= CACHE_OPER1;
431
            PC_Ctrl.Oper     <= PC_REV2;
432
            DP_Ctrl.Src      <= DATA_WR_REG;
433
            DP_Ctrl.Reg      <= ACCUM;
434
 
435
          when OP_STX =>
436
            CPU_Next_State   <= STX_C1;
437
            Cache_Ctrl       <= CACHE_PREFETCH;
438
            PC_Ctrl.Oper     <= PC_REV2;
439
            DP_Ctrl.Src      <= DATA_WR_REG;
440
            DP_Ctrl.Reg      <= ACCUM;
441
 
442
          when others =>
443
            PC_Ctrl.Oper     <= PC_INCR;
444
            ALU_Ctrl.Oper    <= Opcode;
445
            ALU_Ctrl.Reg     <= SubOp;
446
 
447
        end case;
448
 
449
-------------------------------------------------------------------------------
450
-- Program Control (BR0_C1, BR1_C1, DBNZ_C1, JMP )
451
-------------------------------------------------------------------------------
452
 
453
      when BRN_C1 =>
454
        CPU_Next_State       <= INSTR_DECODE;
455
        Cache_Ctrl           <= CACHE_INSTR;
456
        PC_Ctrl.Oper         <= PC_INCR;
457
        if( Flags(Reg) = Opcode(0) )then
458
          CPU_Next_State     <= PIPE_FILL_0;
459
          Cache_Ctrl         <= CACHE_IDLE;
460
          PC_Ctrl.Offset     <= Operand1;
461
        end if;
462
 
463
      when DBNZ_C1 =>
464
        CPU_Next_State       <= INSTR_DECODE;
465
        Cache_Ctrl           <= CACHE_INSTR;
466
        PC_Ctrl.Oper         <= PC_INCR;
467
        if( Flags(FL_ZERO) = '0' )then
468
          CPU_Next_State     <= PIPE_FILL_0;
469
          Cache_Ctrl         <= CACHE_IDLE;
470
          PC_Ctrl.Offset     <= Operand1;
471
        end if;
472
 
473
      when JMP_C1 =>
474
        CPU_Next_State       <= JMP_C2;
475
        Cache_Ctrl           <= CACHE_OPER2;
476
 
477
      when JMP_C2 =>
478
        CPU_Next_State       <= PIPE_FILL_0;
479
        PC_Ctrl.Oper         <= PC_LOAD;
480
        PC_Ctrl.Addr         <= Operand2 & Operand1;
481
 
482
-------------------------------------------------------------------------------
483
-- Data Storage - Load from memory (LDA, LDI, LDO, LDX)
484
-------------------------------------------------------------------------------
485
 
486
      when LDA_C1 =>
487
        CPU_Next_State       <= LDA_C2;
488
        Cache_Ctrl           <= CACHE_OPER2;
489
 
490
      when LDA_C2 =>
491
        CPU_Next_State       <= LDA_C3;
492
        Address              <= Operand2 & Operand1;
493
 
494
      when LDA_C3 =>
495
        CPU_Next_State       <= LDA_C4;
496
        PC_Ctrl.Oper         <= PC_INCR;
497
 
498
      when LDA_C4 =>
499
        CPU_Next_State       <= LDI_C1;
500
        Cache_Ctrl           <= CACHE_OPER1;
501
        PC_Ctrl.Oper         <= PC_INCR;
502
 
503
      when LDI_C1 =>
504
        CPU_Next_State       <= INSTR_DECODE;
505
        Cache_Ctrl           <= CACHE_INSTR;
506
        PC_Ctrl.Oper         <= PC_INCR;
507
        ALU_Ctrl.Oper        <= ALU_LDI;
508
        ALU_Ctrl.Reg         <= SubOp;
509
        ALU_Ctrl.Data        <= Operand1;
510
 
511
      when LDO_C1 =>
512
        CPU_Next_State       <= LDX_C1;
513
        PC_Ctrl.Oper         <= PC_INCR;
514
        if( Enable_Auto_Increment )then
515
          Reg                := conv_integer(SubOp(2 downto 1) & '0');
516
          Reg_1              := conv_integer(SubOp(2 downto 1) & '1');
517
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
518
          if( SubOp(0) = '1' )then
519
            ALU_Ctrl.Oper<= ALU_UPP;
520
            ALU_Ctrl.Reg <= SubOp(2 downto 1) & '0';
521
          end if;
522
        else
523
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
524
        end if;
525
 
526
      when LDX_C1 =>
527
        CPU_Next_State       <= LDX_C2;
528
        PC_Ctrl.Oper         <= PC_INCR;
529
 
530
      when LDX_C2 =>
531
        CPU_Next_State       <= LDX_C3;
532
        PC_Ctrl.Oper         <= PC_INCR;
533
        Cache_Ctrl           <= CACHE_OPER1;
534
 
535
      when LDX_C3 =>
536
        CPU_Next_State       <= INSTR_DECODE;
537
        Cache_Ctrl           <= CACHE_INSTR;
538
        PC_Ctrl.Oper         <= PC_INCR;
539
        ALU_Ctrl.Oper        <= ALU_LDX;
540
        ALU_Ctrl.Reg         <= ACCUM;
541
        ALU_Ctrl.Data        <= Operand1;
542
 
543
-------------------------------------------------------------------------------
544
-- Data Storage - Store to memory (STA, STO, STX)
545
-------------------------------------------------------------------------------
546
      when STA_C1 =>
547
        CPU_Next_State       <= STA_C2;
548
        Cache_Ctrl           <= CACHE_OPER2;
549
        DP_Ctrl.Src          <= DATA_WR_REG;
550
        DP_Ctrl.Reg          <= SubOp;
551
 
552
      when STA_C2 =>
553
        CPU_Next_State       <= STA_C3;
554
        Address              <= Operand2 & Operand1;
555
        PC_Ctrl.Oper         <= PC_INCR;
556
 
557
      when STA_C3 =>
558
        CPU_Next_State       <= PIPE_FILL_2;
559
        Cache_Ctrl           <= CACHE_PREFETCH;
560
        PC_Ctrl.Oper         <= PC_INCR;
561
 
562
      when STO_C1 =>
563
        Cache_Ctrl           <= CACHE_PREFETCH;
564
        PC_Ctrl.Oper         <= PC_INCR;
565
        -- If auto-increment is disabled, just load the registers normally
566
        if( not Enable_Auto_Increment )then
567
          CPU_Next_State     <= PIPE_FILL_1;
568
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
569
        -- Otherwise, enforce the even register rule, and check the LSB to see
570
        --  if we should perform the auto-increment on the register pair
571
        else
572
          CPU_Next_State     <= PIPE_FILL_0;
573
          Reg                := conv_integer(SubOp(2 downto 1) & '0');
574
          Reg_1              := conv_integer(SubOp(2 downto 1) & '1');
575
          Address            <= (Regfile(Reg_1) & Regfile(Reg)) + Offset_SX;
576
          if( SubOp(0) = '1' )then
577
            CPU_Next_State   <= STO_C2;
578
            ALU_Ctrl.Oper    <= ALU_UPP;
579
            ALU_Ctrl.Reg     <= SubOp(2 downto 1) & '0';
580
          end if;
581
        end if;
582
 
583
      when STO_C2 =>
584
        CPU_Next_State       <= PIPE_FILL_1;
585
        PC_Ctrl.Oper         <= PC_INCR;
586
        ALU_Ctrl.Oper        <= ALU_UPP2;
587
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
588
 
589
      when STX_C1 =>
590
        PC_Ctrl.Oper         <= PC_INCR;
591
        -- If auto-increment is disabled, just load the registers normally
592
        if( not Enable_Auto_Increment )then
593
          CPU_Next_State     <= PIPE_FILL_1;
594
          Address            <= (Regfile(Reg_1) & Regfile(Reg));
595
        -- Otherwise, enforce the even register rule, and check the LSB to see
596
        --  if we should perform the auto-increment on the register pair
597
        else
598
          CPU_Next_State     <= PIPE_FILL_1;
599
          Reg                := conv_integer(SubOp(2 downto 1) & '0');
600
          Reg_1              := conv_integer(SubOp(2 downto 1) & '1');
601
          Address            <= (Regfile(Reg_1) & Regfile(Reg));
602
          if( SubOp(0) = '1' )then
603
            CPU_Next_State   <= STX_C2;
604
            ALU_Ctrl.Oper    <= ALU_UPP;
605
            ALU_Ctrl.Reg     <= SubOp(2 downto 1) & '0';
606
          end if;
607
        end if;
608
 
609
      when STX_C2 =>
610
        CPU_Next_State       <= PIPE_FILL_2;
611
        PC_Ctrl.Oper         <= PC_INCR;
612
        ALU_Ctrl.Oper        <= ALU_UPP2;
613
        ALU_Ctrl.Reg         <= SubOp(2 downto 1) & '1';
614
 
615
-------------------------------------------------------------------------------
616
-- Multi-Cycle Math Operations (UPP, MUL)
617
-------------------------------------------------------------------------------
618
 
619
      -- Because we have to backup the pipeline by 1 to refetch the 2nd
620
      --  instruction/first operand, we have to return through PF2
621
      when MUL_C1 =>
622
        CPU_Next_State       <= PIPE_FILL_2;
623
        PC_Ctrl.Oper         <= PC_INCR;
624
        ALU_Ctrl.Oper        <= ALU_MUL;
625
 
626
      when UPP_C1 =>
627
        CPU_Next_State       <= PIPE_FILL_2;
628
        PC_Ctrl.Oper         <= PC_INCR;
629
        ALU_Ctrl.Oper        <= ALU_UPP2;
630
        ALU_Ctrl.Reg         <= SubOp_p1;
631
 
632
-------------------------------------------------------------------------------
633
-- Basic Stack Manipulation (PSH, POP, RSP)
634
-------------------------------------------------------------------------------
635
      when PSH_C1 =>
636
        CPU_Next_State       <= PIPE_FILL_1;
637
        Address              <= Stack_Ptr;
638
        SP_Ctrl.Oper         <= SP_PUSH;
639
 
640
      when POP_C1 =>
641
        CPU_Next_State       <= POP_C2;
642
        Address              <= Stack_Ptr;
643
 
644
      when POP_C2 =>
645
        CPU_Next_State       <= POP_C3;
646
        PC_Ctrl.Oper         <= PC_INCR;
647
 
648
      when POP_C3 =>
649
        CPU_Next_State       <= POP_C4;
650
        Cache_Ctrl           <= CACHE_OPER1;
651
        PC_Ctrl.Oper         <= PC_INCR;
652
 
653
      when POP_C4 =>
654
        CPU_Next_State       <= INSTR_DECODE;
655
        Cache_Ctrl           <= CACHE_INSTR;
656
        PC_Ctrl.Oper         <= PC_INCR;
657
        ALU_Ctrl.Oper        <= ALU_POP;
658
        ALU_Ctrl.Reg         <= SubOp;
659
        ALU_Ctrl.Data        <= Operand1;
660
 
661
-------------------------------------------------------------------------------
662
-- Subroutines & Interrupts (RTS, JSR)
663
-------------------------------------------------------------------------------
664
      when WAIT_FOR_INT => -- For soft interrupts only, halt the Program_Ctr
665
        DP_Ctrl.Src          <= DATA_BUS_IDLE;
666
        CPU_Next_State       <= WAIT_FOR_INT;
667
 
668
      when ISR_C1 =>
669
        CPU_Next_State       <= ISR_C2;
670
        Address              <= ISR_Addr;
671
        INT_Ctrl.Incr_ISR    <= '1';
672
 
673
      when ISR_C2 =>
674
        CPU_Next_State       <= ISR_C3;
675
        Address              <= ISR_Addr;
676
        DP_Ctrl.Src          <= DATA_WR_FLAG;
677
 
678
      when ISR_C3 =>
679
        CPU_Next_State       <= JSR_C1;
680
        Cache_Ctrl           <= CACHE_OPER1;
681
        Address              <= Stack_Ptr;
682
        SP_Ctrl.Oper         <= SP_PUSH;
683
        DP_Ctrl.Src          <= DATA_WR_PC;
684
        DP_Ctrl.Reg          <= ACCUM+1;
685
        ALU_Ctrl.Oper        <= ALU_STP;
686
        ALU_Ctrl.Reg         <= INT_FLAG;
687
        Ack_D                <= '1';
688
 
689
      when JSR_C1 =>
690
        CPU_Next_State       <= JSR_C2;
691
        Cache_Ctrl           <= CACHE_OPER2;
692
        Address              <= Stack_Ptr;
693
        SP_Ctrl.Oper         <= SP_PUSH;
694
        DP_Ctrl.Src          <= DATA_WR_PC;
695
        DP_Ctrl.Reg          <= ACCUM;
696
 
697
      when JSR_C2 =>
698
        CPU_Next_State       <= PIPE_FILL_0;
699
        Address              <= Stack_Ptr;
700
        SP_Ctrl.Oper         <= SP_PUSH;
701
        PC_Ctrl.Oper         <= PC_LOAD;
702
        PC_Ctrl.Addr         <= Operand2 & Operand1;
703
 
704
      when RTS_C1 =>
705
        CPU_Next_State       <= RTS_C2;
706
        Address              <= Stack_Ptr;
707
        SP_Ctrl.Oper         <= SP_POP;
708
 
709
      when RTS_C2 =>
710
        CPU_Next_State       <= RTS_C3;
711
        Address              <= Stack_Ptr;
712
        -- if this is an RTI, then we need to POP the flags
713
        if( SubOp = SOP_RTI )then
714
          SP_Ctrl.Oper       <= SP_POP;
715
        end if;
716
 
717
      when RTS_C3 =>
718
        CPU_Next_State       <= RTS_C4;
719
        Cache_Ctrl           <= CACHE_OPER1;
720
        -- It doesn't really matter what is on the address bus for RTS, while
721
        --  it does for RTI, so we make this the default
722
        Address              <= Stack_Ptr;
723
 
724
      when RTS_C4 =>
725
        CPU_Next_State       <= RTS_C5;
726
        Cache_Ctrl           <= CACHE_OPER2;
727
 
728
      when RTS_C5 =>
729
        CPU_Next_State       <= PIPE_FILL_0;
730
        PC_Ctrl.Oper         <= PC_LOAD;
731
        PC_Ctrl.Addr         <= Operand2 & Operand1;
732
        if( SubOp = SOP_RTI )then
733
          CPU_Next_State     <= RTI_C6;
734
          Cache_Ctrl         <= CACHE_OPER1;
735
        end if;
736
 
737
      when RTI_C6 =>
738
        CPU_Next_State       <= PIPE_FILL_1;
739
        PC_Ctrl.Oper         <= PC_INCR;
740
        ALU_Ctrl.Oper        <= ALU_RFLG;
741
        ALU_Ctrl.Data        <= Operand1;
742
 
743
-------------------------------------------------------------------------------
744
-- Debugging (BRK) Performs a 5-clock NOP
745
-------------------------------------------------------------------------------
746
      when BRK_C1 =>
747
        CPU_Next_State       <= PIPE_FILL_0;
748
 
749
      when others =>
750
        null;
751
    end case;
752
 
753
    -- Interrupt service routines can only begin during the decode and wait
754
    --  states to avoid corruption due to incomplete instruction execution
755
    if( Int_Req = '1' )then
756
      if( CPU_State = INSTR_DECODE or CPU_State = WAIT_FOR_INT )then
757
        -- Reset all of the sub-block controls to IDLE, to avoid unintended
758
        --  operation due to the current instruction
759
        ALU_Ctrl.Oper        <= ALU_IDLE;
760
        Cache_Ctrl           <= CACHE_IDLE;
761
        SP_Ctrl.Oper         <= SP_IDLE;
762
        DP_Ctrl.Src          <= DATA_RD_MEM;
763
        INT_Ctrl.Soft_Ints   <= (others => '0');
764
        -- Rewind the PC by 3 to compensate for the pipeline registers
765
        PC_Ctrl.Oper         <= PC_INCR;
766
        PC_Ctrl.Offset       <= x"FF";
767
        CPU_Next_State       <= ISR_C1;
768
 
769
      end if;
770
    end if;
771
 
772
  end process;
773
 
774
  -- We need to infer a hardware multipler, so we create a special clocked
775
  --  process with no reset or clock enable
776
  Multiplier_proc: process( Clock )
777
  begin
778
    if( rising_edge(Clock) )then
779
      Mult                   <= Regfile(0) *
780
                                Regfile(conv_integer(ALU_Ctrl.Reg));
781
    end if;
782
  end process;
783
 
784
-------------------------------------------------------------------------------
785
-- Registered portion of CPU finite state machine
786
-------------------------------------------------------------------------------
787
  CPU_Regs: process( Reset, Clock )
788
    variable Offset_SX       : ADDRESS_TYPE;
789
    variable i_Ints          : INTERRUPT_BUNDLE := (others => '0');
790
    variable Index           : integer range 0 to 7         := 0;
791
    variable Sum             : std_logic_vector(8 downto 0) := "000000000";
792
    variable Temp            : std_logic_vector(8 downto 0) := "000000000";
793
  begin
794
    if( Reset = Reset_Level )then
795
      CPU_State              <= PIPE_FILL_0;
796
      Opcode                 <= OP_INC;
797
      SubOp                  <= ACCUM;
798
      SubOp_p1               <= ACCUM;
799
      Operand1               <= x"00";
800
      Operand2               <= x"00";
801
      Instr_Prefetch         <= '0';
802
      Prefetch               <= x"00";
803
 
804
      Wr_Data                <= (others => '0');
805
      Wr_Enable              <= '0';
806
      Rd_Enable              <= '1';
807
 
808
      Program_Ctr            <= Program_Start_Addr;
809
      Stack_Ptr              <= Stack_Start_Addr;
810
 
811
      Ack_Q                  <= '0';
812
      Ack_Q1                 <= '0';
813
      Int_Ack                <= '0';
814
 
815
      Int_Req                <= '0';
816
      Pending                <= x"00";
817
      Wait_for_FSM           <= '0';
818
      if( Enable_NMI )then
819
        Int_Mask             <= Default_Interrupt_Mask(7 downto 1) & '1';
820
      else
821
        Int_Mask             <= Default_Interrupt_Mask;
822
      end if;
823
      ISR_Addr               <= INT_VECTOR_0;
824
 
825
      for i in 0 to 7 loop
826
        Regfile(i)           <= (others => '0');
827
      end loop;
828
      Flags                  <= x"00";
829
 
830
    elsif( rising_edge(Clock) )then
831
      Wr_Enable              <= '0';
832
      Wr_Data                <= x"00";
833
      Rd_Enable              <= '0';
834
 
835
-------------------------------------------------------------------------------
836
-- Instruction/Operand caching for pipelined memory access
837
-------------------------------------------------------------------------------
838
      CPU_State              <= CPU_Next_State;
839
      case Cache_Ctrl is
840
        when CACHE_INSTR =>
841
          Opcode             <= Rd_Data(7 downto 3);
842
          SubOp              <= Rd_Data(2 downto 0);
843
          SubOp_p1           <= Rd_Data(2 downto 0) + 1;
844
          if( Instr_Prefetch = '1' )then
845
            Opcode           <= Prefetch(7 downto 3);
846
            SubOp            <= Prefetch(2 downto 0);
847
            SubOp_p1         <= Prefetch(2 downto 0) + 1;
848
            Instr_Prefetch   <= '0';
849
          end if;
850
 
851
        when CACHE_OPER1 =>
852
          Operand1           <= Rd_Data;
853
 
854
        when CACHE_OPER2 =>
855
          Operand2           <= Rd_Data;
856
 
857
        when CACHE_PREFETCH =>
858
          Prefetch           <= Rd_Data;
859
          Instr_Prefetch     <= '1';
860
 
861
        when CACHE_IDLE =>
862
          null;
863
      end case;
864
 
865
-------------------------------------------------------------------------------
866
-- Program Counter
867
-------------------------------------------------------------------------------
868
      Offset_SX(15 downto 8) := (others => PC_Ctrl.Offset(7));
869
      Offset_SX(7 downto 0)  := PC_Ctrl.Offset;
870
 
871
      case PC_Ctrl.Oper is
872
        when PC_IDLE =>
873
          null;
874
 
875
        when PC_REV1 =>
876
          Program_Ctr        <= Program_Ctr - 1;
877
 
878
        when PC_REV2 =>
879
          Program_Ctr        <= Program_Ctr - 2;
880
 
881
        when PC_INCR =>
882
          Program_Ctr        <= Program_Ctr + Offset_SX - 2;
883
 
884
        when PC_LOAD =>
885
          Program_Ctr        <= PC_Ctrl.Addr;
886
 
887
        when others =>
888
          null;
889
      end case;
890
 
891
-------------------------------------------------------------------------------
892
-- (Write) Data Path
893
-------------------------------------------------------------------------------
894
      case DP_Ctrl.Src is
895
        when DATA_BUS_IDLE =>
896
          null;
897
 
898
        when DATA_RD_MEM =>
899
          Rd_Enable          <= '1';
900
 
901
        when DATA_WR_REG =>
902
          Wr_Enable          <= '1';
903
          Wr_Data            <= Regfile(conv_integer(DP_Ctrl.Reg));
904
 
905
        when DATA_WR_FLAG =>
906
          Wr_Enable          <= '1';
907
          Wr_Data            <= Flags;
908
 
909
        when DATA_WR_PC =>
910
          Wr_Enable          <= '1';
911
          Wr_Data            <= Program_Ctr(15 downto 8);
912
          if( DP_Ctrl.Reg = ACCUM )then
913
            Wr_Data          <= Program_Ctr(7 downto 0);
914
          end if;
915
 
916
        when others =>
917
          null;
918
      end case;
919
 
920
-------------------------------------------------------------------------------
921
-- Stack Pointer
922
-------------------------------------------------------------------------------
923
      case SP_Ctrl.Oper is
924
        when SP_IDLE =>
925
          null;
926
 
927
        when SP_RSET =>
928
-- The original RSP instruction simply reset the stack pointer to the preset
929
--  address set at compile time. However, with little extra effort, we can
930
--  modify the instruction to allow the stack pointer to be moved anywhere in
931
--  the memory map. Since RSP can't have an sub-opcode, R1:R0 was chosen as
932
--  a fixed source
933
          Stack_Ptr          <= Stack_Start_Addr;
934
          if( Allow_Stack_Address_Move )then
935
            Stack_Ptr        <= Regfile(1) & Regfile(0);
936
          end if;
937
 
938
        when SP_POP  =>
939
          Stack_Ptr          <= Stack_Ptr + 1;
940
 
941
        when SP_PUSH =>
942
          Stack_Ptr          <= Stack_Ptr - 1;
943
 
944
        when others =>
945
          null;
946
 
947
      end case;
948
 
949
-------------------------------------------------------------------------------
950
-- Interrupt Controller
951
-------------------------------------------------------------------------------
952
      -- The interrupt control mask is always sourced out of R0
953
      if( INT_Ctrl.Mask_Set = '1' )then
954
        -- If the Enable_NMI generic is set, force the LSB high
955
        if( Enable_NMI )then
956
          Int_Mask           <= Regfile(conv_integer(ACCUM))(7 downto 1) & '1';
957
        else
958
          Int_Mask           <= Regfile(conv_integer(ACCUM));
959
        end if;
960
      end if;
961
 
962
      -- Combine external and internal interrupts, and mask the OR of the two
963
      --  with the mask. Record any incoming interrupts to the pending buffer
964
      i_Ints                 := (Interrupts or INT_Ctrl.Soft_Ints) and
965
                                Int_Mask;
966
 
967
      Pending                <= i_Ints or Pending;
968
 
969
      if( Wait_for_FSM = '0' )then
970
        if(    Pending(0) = '1' )then
971
          ISR_Addr           <= INT_VECTOR_0;
972
          Pending(0)         <= '0';
973
          Wait_for_FSM       <= '1';
974
        elsif( Pending(1) = '1' )then
975
          ISR_Addr           <= INT_VECTOR_1;
976
          Pending(1)         <= '0';
977
          Wait_for_FSM       <= '1';
978
        elsif( Pending(2) = '1' )then
979
          ISR_Addr           <= INT_VECTOR_2;
980
          Pending(2)         <= '0';
981
          Wait_for_FSM       <= '1';
982
        elsif( Pending(3) = '1' )then
983
          ISR_Addr           <= INT_VECTOR_3;
984
          Pending(3)         <= '0';
985
          Wait_for_FSM       <= '1';
986
        elsif( Pending(4) = '1' )then
987
          ISR_Addr           <= INT_VECTOR_4;
988
          Pending(4)         <= '0';
989
          Wait_for_FSM       <= '1';
990
        elsif( Pending(5) = '1' )then
991
          ISR_Addr           <= INT_VECTOR_5;
992
          Pending(5)         <= '0';
993
          Wait_for_FSM       <= '1';
994
        elsif( Pending(6) = '1' )then
995
          ISR_Addr           <= INT_VECTOR_6;
996
          Pending(6)         <= '0';
997
          Wait_for_FSM       <= '1';
998
        elsif( Pending(7) = '1' )then
999
          ISR_Addr           <= INT_VECTOR_7;
1000
          Pending(7)         <= '0';
1001
          Wait_for_FSM       <= '1';
1002
        end if;
1003
      end if;
1004
 
1005
      -- Reset the Wait_for_FSM flag on Int_Ack
1006
      Ack_Q                  <= Ack_D;
1007
      Ack_Q1                 <= Ack_Q;
1008
      Int_Ack                <= Ack_Q1;
1009
      if( Int_Ack = '1' )then
1010
        Wait_for_FSM         <= '0';
1011
      end if;
1012
 
1013
      Int_Req                <= Wait_for_FSM and (not Int_Ack);
1014
 
1015
      -- Incr_ISR allows the CPU Core to advance the vector address to pop the
1016
      --  lower half of the address.
1017
      if( INT_Ctrl.Incr_ISR = '1' )then
1018
        ISR_Addr             <= ISR_Addr + 1;
1019
      end if;
1020
 
1021
-------------------------------------------------------------------------------
1022
-- ALU (Arithmetic / Logic Unit)
1023
-------------------------------------------------------------------------------
1024
      Index                  := conv_integer(ALU_Ctrl.Reg);
1025
      Sum                    := (others => '0');
1026
      Temp                   := (others => '0');
1027
 
1028
      case ALU_Ctrl.Oper is
1029
        when ALU_INC => -- Rn = Rn + 1 : Flags N,C,Z
1030
          Sum                := ("0" & x"01") +
1031
                                ("0" & Regfile(Index));
1032
          Flags(FL_ZERO)     <= nor_reduce(Sum(7 downto 0));
1033
          Flags(FL_CARRY)    <= Sum(8);
1034
          Flags(FL_NEG)      <= Sum(7);
1035
          Regfile(Index)     <= Sum(7 downto 0);
1036
 
1037
        when ALU_UPP => -- Rn = Rn + 1
1038
          Sum                := ("0" & x"01") +
1039
                                ("0" & Regfile(Index));
1040
          Flags(FL_CARRY)    <= Sum(8);
1041
          Regfile(Index)     <= Sum(7 downto 0);
1042
 
1043
        when ALU_UPP2 => -- Rn = Rn + C
1044
          Sum                := ("0" & x"00") +
1045
                                ("0" & Regfile(Index)) +
1046
                                Flags(FL_CARRY);
1047
          Flags(FL_CARRY)    <= Sum(8);
1048
          Regfile(Index)     <= Sum(7 downto 0);
1049
 
1050
        when ALU_ADC => -- R0 = R0 + Rn + C : Flags N,C,Z
1051
          Sum                := ("0" & Regfile(0)) +
1052
                                ("0" & Regfile(Index)) +
1053
                                Flags(FL_CARRY);
1054
          Flags(FL_ZERO)     <= nor_reduce(Sum(7 downto 0));
1055
          Flags(FL_CARRY)    <= Sum(8);
1056
          Flags(FL_NEG)      <= Sum(7);
1057
          Regfile(0)         <= Sum(7 downto 0);
1058
 
1059
        when ALU_TX0 => -- R0 = Rn : Flags N,Z
1060
          Temp               := "0" & Regfile(Index);
1061
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1062
          Flags(FL_NEG)      <= Temp(7);
1063
          Regfile(0)         <= Temp(7 downto 0);
1064
 
1065
        when ALU_OR  => -- R0 = R0 | Rn : Flags N,Z
1066
          Temp(7 downto 0)   := Regfile(0) or Regfile(Index);
1067
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1068
          Flags(FL_NEG)      <= Temp(7);
1069
          Regfile(0)         <= Temp(7 downto 0);
1070
 
1071
        when ALU_AND => -- R0 = R0 & Rn : Flags N,Z
1072
          Temp(7 downto 0)   := Regfile(0) and Regfile(Index);
1073
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1074
          Flags(FL_NEG)      <= Temp(7);
1075
          Regfile(0)         <= Temp(7 downto 0);
1076
 
1077
        when ALU_XOR => -- R0 = R0 ^ Rn : Flags N,Z
1078
          Temp(7 downto 0)   := Regfile(0) xor Regfile(Index);
1079
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1080
          Flags(FL_NEG)      <= Temp(7);
1081
          Regfile(0)         <= Temp(7 downto 0);
1082
 
1083
        when ALU_ROL => -- Rn = Rn<<1,C : Flags N,C,Z
1084
          Temp               := Regfile(Index) & Flags(FL_CARRY);
1085
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1086
          Flags(FL_CARRY)    <= Temp(8);
1087
          Flags(FL_NEG)      <= Temp(7);
1088
          Regfile(Index)     <= Temp(7 downto 0);
1089
 
1090
        when ALU_ROR => -- Rn = C,Rn>>1 : Flags N,C,Z
1091
          Temp               := Regfile(Index)(0) & Flags(FL_CARRY) &
1092
                                Regfile(Index)(7 downto 1);
1093
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1094
          Flags(FL_CARRY)    <= Temp(8);
1095
          Flags(FL_NEG)      <= Temp(7);
1096
          Regfile(Index)     <= Temp(7 downto 0);
1097
 
1098
        when ALU_DEC => -- Rn = Rn - 1 : Flags N,C,Z
1099
          Sum                := ("0" & Regfile(Index)) +
1100
                                ("0" & x"FF");
1101
          Flags(FL_ZERO)     <= nor_reduce(Sum(7 downto 0));
1102
          Flags(FL_CARRY)    <= Sum(8);
1103
          Flags(FL_NEG)      <= Sum(7);
1104
          Regfile(Index)     <= Sum(7 downto 0);
1105
 
1106
        when ALU_SBC => -- Rn = R0 - Rn - C : Flags N,C,Z
1107
          Sum                := ("0" & Regfile(0)) +
1108
                                ("1" & (not Regfile(Index))) +
1109
                                Flags(FL_CARRY);
1110
          Flags(FL_ZERO)     <= nor_reduce(Sum(7 downto 0));
1111
          Flags(FL_CARRY)    <= Sum(8);
1112
          Flags(FL_NEG)      <= Sum(7);
1113
          Regfile(0)         <= Sum(7 downto 0);
1114
 
1115
        when ALU_ADD => -- R0 = R0 + Rn : Flags N,C,Z
1116
          Sum                := ("0" & Regfile(0)) +
1117
                                ("0" & Regfile(Index));
1118
          Flags(FL_CARRY)    <= Sum(8);
1119
          Regfile(0)         <= Sum(7 downto 0);
1120
          Flags(FL_ZERO)     <= nor_reduce(Sum(7 downto 0));
1121
          Flags(FL_NEG)      <= Sum(7);
1122
 
1123
        when ALU_STP => -- Sets bit(n) in the Flags register
1124
          Flags(Index)       <= '1';
1125
 
1126
        when ALU_BTT => -- Z = !R0(N), N = R0(7)
1127
          Flags(FL_ZERO)     <= not Regfile(0)(Index);
1128
          Flags(FL_NEG)      <= Regfile(0)(7);
1129
 
1130
        when ALU_CLP => -- Clears bit(n) in the Flags register
1131
          Flags(Index)       <= '0';
1132
 
1133
        when ALU_T0X => -- Rn = R0 : Flags N,Z
1134
          Temp               := "0" & Regfile(0);
1135
          Flags(FL_ZERO)     <= nor_reduce(Temp(7 downto 0));
1136
          Flags(FL_NEG)      <= Temp(7);
1137
          Regfile(Index)     <= Temp(7 downto 0);
1138
 
1139
        when ALU_CMP => -- Sets Flags on R0 - Rn : Flags N,C,Z
1140
          Sum                := ("0" & Regfile(0)) +
1141
                                ("1" & (not Regfile(Index))) +
1142
                                '1';
1143
          Flags(FL_ZERO)     <= nor_reduce(Sum(7 downto 0));
1144
          Flags(FL_CARRY)    <= Sum(8);
1145
          Flags(FL_NEG)      <= Sum(7);
1146
 
1147
        when ALU_MUL => -- Stage 1 of 2 {R1:R0} = R0 * Rn : Flags Z
1148
          Regfile(0)         <= Mult(7 downto 0);
1149
          Regfile(1)         <= Mult(15 downto 8);
1150
          Flags(FL_ZERO)     <= nor_reduce(Mult);
1151
 
1152
        when ALU_LDI => -- Rn <= Data : Flags N,Z
1153
          Flags(FL_ZERO)     <= nor_reduce(ALU_Ctrl.Data);
1154
          Flags(FL_NEG)      <= ALU_Ctrl.Data(7);
1155
          Regfile(Index)     <= ALU_Ctrl.Data;
1156
 
1157
        when ALU_POP => -- Rn <= Data
1158
          Regfile(Index)     <= ALU_Ctrl.Data;
1159
 
1160
        when ALU_LDX => -- R0 <= Data : Flags N,Z
1161
          Flags(FL_ZERO)     <= nor_reduce(ALU_Ctrl.Data);
1162
          Flags(FL_NEG)      <= ALU_Ctrl.Data(7);
1163
          Regfile(0)         <= ALU_Ctrl.Data;
1164
 
1165
        when ALU_RFLG =>
1166
          Flags              <= ALU_Ctrl.Data;
1167
 
1168
        when others =>
1169
          null;
1170
      end case;
1171
 
1172
    end if;
1173
  end process;
1174
 
1175
end architecture;

powered by: WebSVN 2.1.0

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