OpenCores
URL https://opencores.org/ocsvn/light52/light52/trunk

Subversion Repositories light52

[/] [light52/] [trunk/] [vhdl/] [light52_cpu.vhdl] - Blame information for rev 14

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

Line No. Rev Author Line
1 2 ja_rd
--------------------------------------------------------------------------------
2
-- light52_cpu.vhdl -- light52 MCS51-compatible CPU core.
3
--------------------------------------------------------------------------------
4
-- This is a 'naive' implementation of the MCS51 architecture that trades area 
5
-- for speed. 
6
-- 
7
-- At the bottom of the file there are some design notes referenced throughout
8
-- the code ('@note1' etc.).
9
--------------------------------------------------------------------------------
10
-- GENERICS:
11
--
12
--
13
-- IMPLEMENT_BCD_INSTRUCTIONS   -- Whether or not to implement BCD instructions.
14
--  When true, instructions DA and XCHD will work as in the original MCS51.
15
--  When false, those instructions will work as NOP, saving some logic.
16
--
17
-- SEQUENTIAL_MULTIPLIER        -- Sequential vs. combinational multiplier.
18
--  When true, a sequential implementation will be used for the multiplier, 
19
--  which will usually save a lot of logic or a dedicated multiplier.
20
--  When false, a combinational registered multiplier will be used.
21
--  (NOT IMPLEMENTED -- setting it to true will raise an assertion failure).
22
--
23
-- USE_BRAM_FOR_XRAM            -- Use extra space in IRAM/uCode RAM as XRAM.
24
--  When true, extra logic will be generated so that the extra space in the 
25
--  RAM block used for IRAM/uCode can be used as XRAM.
26
--  This prevents RAM waste at some cost in area and clock rate.
27
--  When false, any extra space in the IRAM physical block will be wasted.
28
--
29
--------------------------------------------------------------------------------
30
-- INTERFACE SIGNALS:
31
--
32
-- clk :                Clock, active rising edge.
33
-- reset :              Synchronous reset, hold for at least 1 cycle.
34
--
35
--                      XCODE is assumed to be a synchronous ROM:
36
--
37
-- code_addr :          XCODE space address. 
38
-- code_rd :            XCODE read data. Must be valid at all times with 1 
39
--                      cycle of latency (synchronous memory):
40
--                      code_rd[n] := XCODE(code_addr[n-1])
41
--
42
--                      Interrupts are   
43
--
44
-- irq_source :         Interrupt inputs. 
45
--                      0 is for vector 03h, 4 is for vector 23h.
46
--                      Not registsred; must be synchronous and remain high 
47
--                      through a fetch_1 state in order to be acknowledged.
48
--                      Priorities are fixed: 0 highest, 4 lowest.
49
--
50
--                      XDATA is expected to be a synchronous 1-port RAM:
51
--
52
-- xdata_addr :         XDATA space address, valid when xdata_vma is '1'.
53
-- xdata_vma :          Asserted high when an XDATA rd/wr cycle is being done.
54
-- xdata_rd :           Read data. Must be valid the cycle after xdata_vma is
55
--                      asserted with xdata_we='0'.
56
-- xdata_wr :           Write data. Valid when xdata_vma is asserted with 
57
--                      xdata_we='1';
58
-- xdata_we :           '1' for XDATA write cycles, '0' for read cycles.
59
--
60
--                      SFRs external to the CPU are accessed as synchonous RAM:
61
--
62
-- sfr_addr :           SFR space address, valid when sfr_vma='1'.
63
-- sfr_vma :            Asserted high when an SFR rd/wr cycle is being done. 
64
-- sfr_rd :             Read data. Must be valid the cycle after sfr_vma is
65
--                      asserted with sfr_we='0'.
66
-- sfr_wr :             Write data. Valid when sfr_vma is asserted with 
67
--                      sfr_we='1';
68
-- sfr_we :             '1' for SFR write cycles, '0' for read cycles.
69
--
70
--  Note there's no code_vma. Even if there was one, see limitation #1 below. 
71
--------------------------------------------------------------------------------
72
-- MAJOR LIMITATIONS:
73
--
74
-- 1.-  Harvard architecture only.
75
--      The core does not support non-Harvard, unified memory space (i.e. XCODE 
76
--      and XDATA on the same blocks). Signal sfr_vma is asserted the cycle 
77
--      before a fetch_1 state, when a code byte needs to be present at code_rd.
78
--      In other words, XCODE and XDATA accesses are always simultaneous.
79
--      Until the core supports wait states it only supports Harvard memory
80
--      or a dual-port XCODE/XDATA memory.
81
--
82
-- 2.-  No support yet for sequential multiplier, only combinational.
83
--      Setting SEQUENTIAL_MULTIPLIER to true will result in a synthesis
84
--      or simulation failure. 
85
--      The core will use a dedicated multiplier block if the architecture & 
86
--      synthesis options allow for it (e.d. DSP48).
87
-- 
88
-- 3.-  Wasted space in RAM block used for IRAM.
89
--      Setting USE_BRAM_FOR_XRAM to true will result in a synthesis
90
--      or simulation failure.
91
--      Architectures with BRAM blocks larger than 512 bytes (e.g. Xilinx 
92
--      Spartan) will have a lot of wasted space in the IRAM/uCode RAM block.
93
--------------------------------------------------------------------------------
94
-- FIXMES:
95
-- 
96
-- 1.-  States fetch_1 and decode_0 might be overlapped with some other states
97
--      for a noticeable gain in performance.
98
-- 2.-  Brief description of architecture is missing.
99
-- 3.-  Add optional 2nd DPTR register and implicit INC DPTR feature.
100
-- 4.-  Everything coming straight from a _reg should be named _reg too.
101
--------------------------------------------------------------------------------
102
-- REFERENCES:
103
-- [1] Tips for vendor-agnostic BRAM inference:
104
--     http://www.danstrother.com/2010/09/11/inferring-rams-in-fpgas/
105
--------------------------------------------------------------------------------
106
-- Copyright (C) 2012 Jose A. Ruiz
107
--                                                              
108
-- This source file may be used and distributed without         
109
-- restriction provided that this copyright statement is not    
110
-- removed from the file and that any derivative work contains  
111
-- the original copyright notice and the associated disclaimer. 
112
--                                                              
113
-- This source file is free software; you can redistribute it   
114
-- and/or modify it under the terms of the GNU Lesser General   
115
-- Public License as published by the Free Software Foundation; 
116
-- either version 2.1 of the License, or (at your option) any   
117
-- later version.                                               
118
--                                                              
119
-- This source is distributed in the hope that it will be       
120
-- useful, but WITHOUT ANY WARRANTY; without even the implied   
121
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
122
-- PURPOSE.  See the GNU Lesser General Public License for more 
123
-- details.                                                     
124
--                                                              
125
-- You should have received a copy of the GNU Lesser General    
126
-- Public License along with this source; if not, download it   
127
-- from http://www.opencores.org/lgpl.shtml
128
--------------------------------------------------------------------------------
129
 
130
library ieee;
131
use ieee.std_logic_1164.all;
132
use ieee.numeric_std.all;
133
 
134
use work.light52_pkg.all;
135
use work.light52_ucode_pkg.all;
136
 
137
entity light52_cpu is
138
    generic (
139
        USE_BRAM_FOR_XRAM : boolean := false;
140
        IMPLEMENT_BCD_INSTRUCTIONS : boolean := false;
141
        SEQUENTIAL_MULTIPLIER : boolean := false
142
    );
143
    port(
144
        clk             : in std_logic;
145
        reset           : in std_logic;
146
 
147
        code_addr       : out std_logic_vector(15 downto 0);
148
        code_rd         : in std_logic_vector(7 downto 0);
149
 
150
        irq_source      : in std_logic_vector(4 downto 0);
151
 
152
        xdata_addr      : out std_logic_vector(15 downto 0);
153
        xdata_rd        : in std_logic_vector(7 downto 0);
154
        xdata_wr        : out std_logic_vector(7 downto 0);
155
        xdata_vma       : out std_logic;
156
        xdata_we        : out std_logic;
157
 
158
        sfr_addr        : out std_logic_vector(7 downto 0);
159
        sfr_rd          : in std_logic_vector(7 downto 0);
160
        sfr_wr          : out std_logic_vector(7 downto 0);
161
        sfr_vma         : out std_logic;
162
        sfr_we          : out std_logic
163
    );
164
end entity light52_cpu;
165
 
166
 
167
architecture microcoded of light52_cpu is
168
 
169
---- Microcode table & instruction decoding ------------------------------------
170
 
171
signal ucode :              unsigned(15 downto 0);  -- uC word
172
signal ucode_1st_half :     unsigned(15 downto 0);  -- uC if 1st-half opcode
173
signal ucode_2nd_half :     unsigned(15 downto 0);  -- uC if 2nd-half opcode 
174
signal ucode_2nd_half_reg : unsigned(15 downto 0);  --
175
signal ucode_is_2nd_half :  std_logic;              -- opcode is 2-nd half
176
signal ucode_pattern :      unsigned(2 downto 0);   -- table row to replicate
177
signal ucode_index :        unsigned(6 downto 0);   -- index into uC table
178
signal do_fetch :           std_logic;              -- opcode is in code_rd
179
 
180
-- uC class (t_class), only valid in state decode_0
181
signal uc_class_decode_0 :  t_class;
182
-- ALU instruction class (t_alu_class), only valid in state decode_0
183
signal uc_alu_class_decode_0 : t_alu_class;
184
-- registered uc_alu_class_decode_0, used in state machine
185
signal uc_alu_class_reg :   t_alu_class;
186
-- ALU control, valid only in state decode_0
187
signal uc_alu_fn_decode_0 : t_alu_fns;
188
-- uc_alu_fn_decode_0 registered
189
signal alu_fn_reg :         t_alu_fns;
190
-- Controls ALU/bitfield mux in the datapath.
191
signal dpath_mux0_reg :     std_logic;
192
-- Flag mask for ALU instructions
193
signal uc_alu_flag_mask :   t_flag_mask;
194
-- Flag mask for all instructions; valid in state decode_0 only
195
signal flag_mask_decode_0 : t_flag_mask;
196
-- Flag mask register for ALL instructions
197
signal flag_mask_reg :      t_flag_mask;
198
-- Index of Rn register, valid for Rn addressing instructions only
199
signal rn_index :           unsigned(2 downto 0);
200
 
201
 
202
---- Datapath ------------------------------------------------------------------
203
 
204
-- Operand selection for ALU class instructions
205
signal alu_class_op_sel_reg: t_alu_op_sel;
206
signal alu_class_op_sel :   t_alu_op_sel;
207
-- ALU result valid for non-bit operations -- faster as it skips one mux
208
signal nobit_alu_result :   t_byte;
209
-- ALU result
210
signal alu_result :         t_byte;
211
signal alu_result_is_zero : std_logic;  -- ALU result is zero
212
signal acc_is_zero :        std_logic;  -- ACC is zero
213
 
214
signal alu_cy :             std_logic;  -- ALU CY output
215
signal alu_ov :             std_logic;  -- ALU OV output
216
signal alu_ac :             std_logic;  -- ALU AC (aux cy) output
217
signal bit_input :          std_logic;  -- Value of ALU bit operand
218
 
219
signal load_b_sfr :         std_logic;  -- B register load enable
220
signal mul_ready :          std_logic;  -- Multiplier is finished
221
signal div_ready :          std_logic;  -- Divider is finished
222
signal div_ov :             std_logic;  -- OV flag from divider
223
 
224
---- State machine -------------------------------------------------------------
225
 
226
-- Present state register and Next state
227
signal ps, ns :             t_cpu_state;
228
 
229
---- Interrupt handling --------------------------------------------------------
230
 
231
-- IRQ inputs ANDed to IE mask bits
232
signal irq_masked_inputs :  std_logic_vector(4 downto 0);
233
-- Level of highest-level active IRQ input. 0 is highest, 4 lowest, 7 is none.
234
signal irq_level_inputs :   unsigned(2 downto 0);
235
-- Level of IRQ being serviced. 7 if none. Set by IRQ, reset to 7 by RETI.
236
signal irq_level_current :  unsigned(2 downto 0);
237
-- Low 6 bits of IRQ service address.
238
signal irq_vector :         unsigned(5 downto 0);
239
signal irq_active :         std_logic;  -- IRQ pending service
240
signal load_ie :            std_logic;  -- IE register load enable
241
signal irq_restore_level :  std_logic;  -- Restore irq_level_current to 7 (RETI)
242
 
243
---- CPU programmer's model registers ------------------------------------------
244
 
245
signal PC_reg :             t_address;      -- PC
246
signal pc_incremented :     t_address;      -- PC + 1 | PC, controlled by...
247
signal increment_pc :       std_logic;      -- ...PC increment enable 
248
signal A_reg :              t_byte;         -- ACC    
249
signal B_reg :              t_byte;         -- B
250
signal PSW_reg :            unsigned(7 downto 1); -- PSW excluding P flag
251
signal PSW :                t_byte;         -- PSW, full (P is combinational)
252
signal SP_reg :             t_byte;         -- SP
253
signal SP_next :            t_byte;         -- Next value for SP
254
signal IE_reg :             t_byte;         -- IE
255
 
256
signal alu_p :              std_logic;      -- P flag (from ALU)
257
signal DPTR_reg :           t_address;      -- DPTR
258
 
259
signal next_pc :            t_address;      -- Next value for PC
260
signal jump_target :        t_address;      -- Addr to jump to
261
signal jump_is_ljmp :       std_logic;      -- When doing jump, long jump
262
signal instr_jump_is_ljmp : std_logic;      -- For jump opcodes, long jump
263
signal rel_jump_target :    t_address;      -- Address of a short jump
264
signal rel_jump_delta :     t_address;      -- Offset of short jump
265
-- 2K block index for AJMP/ACALL. Straight from the opcode.
266
signal block_reg :          unsigned(2 downto 0);
267
signal code_byte :          t_byte;     -- Byte from XCODE (transient)
268
signal ri_addr :            t_byte;     -- IRAM address of Ri register
269
signal rn_addr :            t_byte;     -- IRAM address of Rn register
270
-- The current ALU instruction CLASS uses Ri, not Rn
271
signal alu_use_ri_by_class : std_logic;
272
-- The current ALU instruction uses Ri and not Rn.
273
signal alu_use_ri :         std_logic;
274
-- The current instruction is not an ALU instruction and uses Ri, not Rn
275
signal non_alu_use_ri :     std_logic;
276
-- The current instruction uses Ri and not Rn
277
signal use_ri :             std_logic;
278
-- Registered use_ri, controls the Ri/Rn mux.
279
signal use_ri_reg :         std_logic;
280
signal rx_addr :            t_byte;     -- Output of Ri/Rx address selection mux
281
signal bit_addr :           t_byte;     -- Byte address of bit operand
282
-- Index of bit operand within its byte.
283
signal bit_index :          unsigned(2 downto 0);
284
-- bit_index_registered in state decode_0
285
signal bit_index_reg :      unsigned(2 downto 0);
286
signal addr0_reg :          t_byte;     -- Aux address register 0...
287
signal addr0_reg_input :    t_byte;     -- ...and its input mux
288
signal addr1_reg :          t_byte;     -- Aux addr reg 1
289
 
290
-- Asserted when the PSW flags are implicitly updated by any instruction
291
signal update_psw_flags :   std_logic;
292
signal load_psw :           std_logic;  -- PSW explicit (SFR) load enable
293
signal load_acc_sfr :       std_logic;  -- ACC explicit (SFR) load enable
294
signal load_addr0 :         std_logic;  -- Addr0 load enable
295
signal load_sp :            std_logic;  -- SP explicit (SFR) load enable
296
signal load_sp_implicit :   std_logic;  -- SP implicit load enable
297
signal update_sp :          std_logic;  -- SP combined load enable
298
 
299
---- SFR interface -------------------------------------------------------------
300
 
301
signal sfr_addr_internal :  unsigned(7 downto 0);   -- SFR address
302
signal sfr_vma_internal :   std_logic;              -- SFR access enable 
303
signal sfr_we_internal :    std_logic;              -- SFR write enable
304
signal sfr_wr_internal :    t_byte;                 -- SFR write data bus
305
signal sfr_rd_internal :    t_byte;                 -- SFR read data bus
306
signal sfr_rd_internal_reg :t_byte;                 -- Registered SFR read data
307
 
308
---- Conditional jumps ---------------------------------------------------------
309
 
310
signal jump_cond_sel_decode_0 : t_cc;       -- Jump condition code...
311
signal jump_cond_sel_reg :  t_cc;           -- ...registered to control mux
312
signal jump_condition :     std_logic;      -- Value of jump condition
313
signal cjne_condition :     std_logic;      -- Value of CJNE jump condition
314
 
315
---- BRAM used for IRAM and uCode table ----------------------------------------
316
-- The BRAM is a 512 byte table: 256 bytes for the decoding table and 256 bytes
317
-- for the 8052 IRAM. 
318
-- FIXME handle Xilinx and arbitrary size FPGA BRAMs.
319
constant BRAM_ADDR_LEN : integer := log2(BRAM_SIZE);
320
subtype t_bram_addr is unsigned(BRAM_ADDR_LEN-1 downto 0);
321
signal bram_addr_p0 :       t_bram_addr;
322
signal bram_addr_p1 :       t_bram_addr;
323
signal bram_data_p0 :       t_byte;
324
signal bram_data_p1 :       t_byte;
325
signal bram_we :            std_logic;
326
signal bram_wr_data_p0 :    t_byte;
327
 
328
-- Part of the BRAM inference template -- see [1]
329
-- The BRAM is initializzed with the decoding table.
330
shared variable bram :      t_ucode_bram :=
331
                ucode_to_bram(build_decoding_table(IMPLEMENT_BCD_INSTRUCTIONS));
332
 
333
-- IRAM/SFR address; lowest 8 bits of actual BRAM address.
334
signal iram_sfr_addr :      t_byte;
335
-- IRAM/SFR read data
336
signal iram_sfr_rd :        t_byte;
337
-- '1' when using direct addressing mode, '0' otherwise.
338
signal direct_addressing :  std_logic;
339
-- '1' when using direct addressing to address range 80h..ffh, '0' otherwise.
340
signal sfr_addressing :     std_logic;
341
signal sfr_addressing_reg : std_logic;
342
 
343
-- Kind of addressing the current instruction is using
344
signal direct_addressing_alu_reg : std_logic;
345
signal alu_using_direct_addressing : std_logic;
346
signal alu_using_indirect_addressing : std_logic;
347
 
348
-- XRAM/MOVC interface and DPTR control logic ----------------------------------
349
 
350
signal load_dph :           std_logic;  -- DPTR(h) load enable
351
signal load_dpl :           std_logic;  -- DPTR(l) load enable
352
signal inc_dptr :           std_logic;  -- DPTR increment enable
353
 
354
signal acc_ext16 :          t_address;  -- Accumulator zero-extended to 16 bits
355
signal movc_base :          t_address;  -- Base for MOVC address
356
signal movc_addr :          t_address;  -- MOVC address
357
signal dptr_plus_a_reg :    t_address;  -- Registered DPTR+ACC adder
358
 
359
 
360
begin
361
 
362
--## 1.- State machine #########################################################
363
 
364
cpu_state_machine_reg:
365
process(clk)
366
begin
367
   if clk'event and clk='1' then
368
        if reset='1' then
369
            ps <= reset_0;
370
        else
371
            ps <= ns;
372
        end if;
373
    end if;
374
end process cpu_state_machine_reg;
375
 
376
 
377
cpu_state_machine_transitions:
378
process(ps, uc_class_decode_0, uc_alu_class_decode_0,
379
        uc_alu_class_reg, jump_condition, alu_fn_reg,
380
        mul_ready,div_ready,
381
        irq_active)
382
begin
383
    case ps is
384
    when reset_0 =>
385
        ns <= fetch_1;
386
 
387
    when fetch_0 =>
388
        ns <= fetch_1;
389
 
390
    when fetch_1 =>
391
        -- Here's where we sample the pending interrupt flags... 
392
        if irq_active='1' then
393
            -- ...and trigger interrupt response if necessary.
394
            ns <= irq_1;
395
        else
396
            ns <= decode_0;
397
        end if;
398
 
399
    when decode_0 =>
400
        if uc_class_decode_0(5 downto 4) = "00" then
401
            case uc_alu_class_decode_0 is
402
                when AC_RI_to_A => ns <= alu_rx_to_ab;
403
                when AC_A_RI_to_A => ns <= alu_rx_to_ab;
404
                when AC_RI_to_RI => ns <= alu_rx_to_ab;
405
                when AC_RI_to_D => ns <= alu_rx_to_ab;
406
 
407
                when AC_D_to_A => ns <= alu_code_to_ab;
408
                when AC_D1_to_D => ns <= alu_code_to_ab;
409
                when AC_D_to_RI => ns <= alu_code_to_ab;
410
                when AC_D_to_D => ns <= alu_code_to_ab;
411
 
412
                when AC_A_RN_to_A => ns <= alu_rx_to_ab;
413
                when AC_RN_to_RN => ns <= alu_rx_to_ab;
414
                when AC_RN_to_A => ns <= alu_rx_to_ab;
415
                when AC_D_to_RN => ns <= alu_code_to_ab;
416
                when AC_RN_to_D => ns <= alu_rx_to_ab;
417
                when AC_I_to_D => ns <= alu_code_to_ab;
418
 
419
                when AC_I_D_to_D => ns <= alu_code_to_ab;
420
                when AC_I_to_RI => ns <= alu_code_to_t_rx_to_ab;
421
                when AC_I_to_RN => ns <= alu_code_to_t_rx_to_ab;
422
                when AC_I_to_A => ns <= alu_code_to_t;
423
                when AC_A_to_RI => ns <= alu_rx_to_ab;
424
                when AC_A_to_D => ns <= alu_code_to_ab;
425
                when AC_A_D_to_A => ns <= alu_code_to_ab;
426
                when AC_A_D_to_D => ns <= alu_code_to_ab;
427
                when AC_A_to_RN => ns <= alu_rx_to_ab;
428
                when AC_A_I_to_A => ns <= alu_code_to_t;
429
                when AC_A_to_A => ns <= alu_res_to_a;
430
 
431
                when AC_DIV => ns <= alu_div_0;
432
                when AC_MUL => ns <= alu_mul_0;
433
                when AC_DA => ns <= alu_daa_0;
434
                when others => ns <= bug_bad_opcode_class;
435
            end case;
436
        else
437
            case uc_class_decode_0 is
438
            when F_JRB =>
439
                ns <= jrb_bit_0;
440
            when F_LJMP =>
441
                ns <= fetch_addr_1;
442
            when F_AJMP =>
443
                ns <= fetch_addr_0_ajmp;
444
            when F_LCALL =>
445
                ns <= lcall_0;
446
            when F_ACALL =>
447
                ns <= acall_0;
448
            when F_JR =>
449
                ns <= load_rel;
450
            when F_CJNE_A_IMM =>
451
                ns <= cjne_a_imm_0;
452
            when F_CJNE_A_DIR =>
453
                ns <= cjne_a_dir_0;
454
            when F_CJNE_RI_IMM =>
455
                ns <= cjne_ri_imm_0;
456
            when F_CJNE_RN_IMM =>
457
                ns <= cjne_rn_imm_0;
458
            when F_DJNZ_DIR =>
459
                ns <= djnz_dir_0;
460
            when F_DJNZ_RN =>
461
                ns <= djnz_rn_0;
462
            when F_OPC =>
463
                ns <= bit_res_to_c;
464
            when F_BIT =>
465
                ns <= bit_op_0;
466
            when F_SPECIAL =>
467
                ns <= special_0;
468
            when F_PUSH =>
469
                ns <= push_0;
470
            when F_POP =>
471
                ns <= pop_0;
472
            when F_MOV_DPTR =>
473
                ns <= mov_dptr_0;
474
            when F_MOVX_DPTR_A =>
475
                ns <= movx_dptr_a_0;
476
            when F_MOVX_A_DPTR =>
477
                ns <= movx_a_dptr_0;
478
            when F_MOVX_A_RI =>
479
                ns <= movx_a_ri_0;
480
            when F_MOVX_RI_A =>
481
                ns <= movx_ri_a_0;
482
            when F_MOVC_PC =>
483
                ns <= movc_pc_0;
484
            when F_MOVC_DPTR =>
485
                ns <= movc_dptr_0;
486
            when F_XCH_DIR =>
487
                ns <= xch_dir_0;
488
            when F_XCH_RI =>
489
                ns <= xch_rx_0;
490
            when F_XCH_RN =>
491
                ns <= xch_rn_0;
492
            when F_XCHD =>
493
                ns <= alu_xchd_0;
494
            when F_JMP_ADPTR =>
495
                ns <= jmp_adptr_0;
496
            when F_RET =>
497
                ns <= ret_0;
498
            when F_NOP =>
499
                ns <= fetch_1;
500
            when others =>
501
                ns <= bug_bad_opcode_class;
502
            end case;
503
        end if;
504
 
505
    -- Interrupt processing ------------------------------------------
506
    when irq_1 =>
507
        ns <= irq_2;
508
    when irq_2 =>
509
        ns <= irq_3;
510
    when irq_3 =>
511
        ns <= irq_4;
512
    when irq_4 =>
513
        ns <= fetch_0;
514
 
515
    -- Call/Ret instructions -----------------------------------------
516
    when acall_0 =>
517
        ns <= acall_1;
518
    when acall_1 =>
519
        ns <= acall_2;
520
    when acall_2 =>
521
        ns <= long_jump;
522
 
523
    when lcall_0 =>
524
        ns <= lcall_1;
525
    when lcall_1 =>
526
        ns <= lcall_2;
527
    when lcall_2 =>
528
        ns <= lcall_3;
529
    when lcall_3 =>
530
        ns <= lcall_4;
531
    when lcall_4 =>
532
        ns <= fetch_0;
533
 
534
    when ret_0 =>
535
        ns <= ret_1;
536
    when ret_1 =>
537
        ns <= ret_2;
538
    when ret_2 =>
539
        ns <= ret_3;
540
    when ret_3 =>
541
        ns <= fetch_0;
542
 
543
    -- Special instructions ------------------------------------------
544
    when special_0 =>
545
        ns <= fetch_1;
546
 
547
    when push_0 =>
548
        ns <= push_1;
549
    when push_1 =>
550
        ns <= push_2;
551
    when push_2 =>
552
        ns <= fetch_1;
553
 
554
    when pop_0 =>
555
        ns <= pop_1;
556
    when pop_1 =>
557
        ns <= pop_2;
558
    when pop_2 =>
559
        ns <= fetch_1;
560
 
561
    when mov_dptr_0 =>
562
        ns <= mov_dptr_1;
563
    when mov_dptr_1 =>
564
        ns <= mov_dptr_2;
565
    when mov_dptr_2 =>
566
        ns <= fetch_1;
567
 
568
    -- MOVC instructions ---------------------------------------------
569
    when movc_pc_0 =>
570
        ns <= movc_1;
571
    when movc_dptr_0 =>
572
        ns <= movc_1;
573
    when movc_1 =>
574
        ns <= fetch_1;
575
 
576
    -- MOVX instructions ---------------------------------------------
577
    when movx_a_dptr_0 =>
578
        ns <= fetch_1;
579
    when movx_dptr_a_0 =>
580
        ns <= fetch_1;
581
 
582
    when movx_a_ri_0 =>
583
        ns <= movx_a_ri_1;
584
    when movx_a_ri_1 =>
585
        ns <= movx_a_ri_2;
586
    when movx_a_ri_2 =>
587
        ns <= movx_a_ri_3;
588
    when movx_a_ri_3 =>
589
        ns <= fetch_1;
590
 
591
    when movx_ri_a_0 =>
592
        ns <= movx_ri_a_1;
593
    when movx_ri_a_1 =>
594
        ns <= movx_ri_a_2;
595
    when movx_ri_a_2 =>
596
        ns <= fetch_1;
597
 
598
    -- XCH instructions ----------------------------------------------
599
    when xch_dir_0 =>
600
        ns <= xch_1;
601
    when xch_rn_0 =>
602
        ns <= xch_1;
603
    when xch_rx_0 =>
604
        ns <= xch_rx_1;
605
    when xch_rx_1 =>
606
        ns <= xch_1;
607
    when xch_1 =>
608
        ns <= xch_2;
609
    when xch_2 =>
610
        ns <= xch_3;
611
    when xch_3 =>
612
        ns <= fetch_1;
613
 
614
    -- BIT instructions ----------------------------------------------
615
    when bit_res_to_c =>    -- SETB C, CPL C and CLR C only
616
        ns <= fetch_1;
617
 
618
    when bit_op_0 =>
619
        ns <= bit_op_1;
620
    when bit_op_1 =>
621
        if uc_alu_class_reg(0)='0' then
622
            ns <= bit_op_2;
623
        else
624
            ns <= bit_res_to_c;
625
        end if;
626
    when bit_op_2 =>
627
        ns <= fetch_1;
628
 
629
 
630
    -- BIT-testing relative jumps ------------------------------------
631
    when jrb_bit_0 =>
632
        ns <= jrb_bit_1;
633
    when jrb_bit_1 =>
634
        if alu_fn_reg(1 downto 0)="11" then
635
            -- This is JBC; state jrb_bit_2 is where the bit is clear.
636
            ns <= jrb_bit_2;
637
        else
638
            ns <= jrb_bit_3;
639
        end if;
640
    when jrb_bit_2 =>
641
        ns <= jrb_bit_3;
642
    when jrb_bit_3 =>
643
        if jump_condition='1' then
644
            ns <= jrb_bit_4;
645
        else
646
            ns <= fetch_0;
647
        end if;
648
 
649
    when jrb_bit_4 =>
650
        ns <= fetch_0;
651
 
652
    -- MUL/DIV instructions ------------------------------------------
653
    when alu_div_0 =>
654
        if div_ready='1' then
655
            ns <= fetch_1;
656
        else
657
            ns <= ps;
658
        end if;
659
 
660
    when alu_mul_0 =>
661
        if mul_ready='1' then
662
            ns <= fetch_1;
663
        else
664
            ns <= ps;
665
        end if;
666
 
667
    -- BCD instructions ----------------------------------------------
668
    when alu_daa_0 =>
669
        ns <= alu_daa_1;
670
 
671
    when alu_daa_1 =>
672
        ns <= fetch_1;
673
 
674
    when alu_xchd_0 =>
675
        ns <= alu_xchd_1;
676
 
677
    when alu_xchd_1 =>
678
        ns <= alu_xchd_2;
679
 
680
    when alu_xchd_2 =>
681
        ns <= alu_xchd_3;
682
 
683
    when alu_xchd_3 =>
684
        ns <= alu_xchd_4;
685
 
686
    when alu_xchd_4 =>
687
        ns <= alu_xchd_5;
688
 
689
    when alu_xchd_5 =>
690
        ns <= fetch_1;
691
 
692
    -- ALU instructions (other than MUL/DIV) -------------------------
693
    when alu_rx_to_ab =>
694
        case uc_alu_class_reg is
695
            when AC_RI_to_A | AC_A_RI_to_A | AC_RI_to_D | AC_RI_to_RI =>
696
                ns <= alu_ram_to_ar;
697
            when AC_RN_to_D =>
698
                ns <= alu_ram_to_t_code_to_ab;
699
            when AC_A_to_RI =>
700
                ns <= alu_ram_to_ar_2;
701
            when others =>
702
                ns <= alu_ram_to_t;
703
        end case;
704
 
705
    when alu_ram_to_ar_2 =>
706
        ns <= alu_res_to_ram_ar_to_ab;
707
 
708
    when alu_ram_to_ar =>
709
        ns <= alu_ar_to_ab;
710
 
711
    when alu_ar_to_ab =>
712
        if uc_alu_class_reg = AC_RI_to_D then
713
            ns <= alu_ram_to_t_code_to_ab;
714
        else
715
            ns <= alu_ram_to_t;
716
        end if;
717
 
718
    when alu_ram_to_t =>
719
        if uc_alu_class_reg = AC_D_to_A or
720
           uc_alu_class_reg = AC_A_D_to_A or
721
           uc_alu_class_reg = AC_RN_to_A or
722
           uc_alu_class_reg = AC_A_RN_to_A or
723
           uc_alu_class_reg = AC_RI_to_A or
724
           uc_alu_class_reg = AC_A_RI_to_A
725
           then
726
            ns <= alu_res_to_a;
727
        elsif uc_alu_class_reg = AC_D1_to_D then
728
            ns <= alu_res_to_ram_code_to_ab;
729
        else
730
            ns <= alu_res_to_ram;
731
        end if;
732
 
733
    when alu_res_to_ram_code_to_ab =>
734
        ns <= fetch_1;
735
 
736
    when alu_res_to_a =>
737
        ns <= fetch_1;
738
 
739
    when alu_ram_to_t_code_to_ab =>
740
        ns <= alu_res_to_ram;
741
 
742
    when alu_res_to_ram =>
743
        ns <= fetch_1;
744
 
745
    when alu_code_to_ab =>
746
        case uc_alu_class_reg is
747
            when AC_I_to_D =>
748
                ns <= alu_code_to_t;
749
            when AC_I_D_to_D =>
750
                ns <= alu_ram_to_v_code_to_t;
751
            when AC_D_to_RI | AC_D_to_RN =>
752
                ns <= alu_ram_to_t_rx_to_ab;
753
            when AC_A_to_D =>
754
                ns <= alu_res_to_ram;
755
            when others =>
756
                ns <= alu_ram_to_t;
757
        end case;
758
 
759
    when alu_ram_to_t_rx_to_ab =>
760
        if uc_alu_class_reg = AC_D_to_RI then
761
            ns <= alu_ram_to_ar_2;
762
        else
763
            ns <= alu_res_to_ram;
764
        end if;
765
 
766
    when alu_res_to_ram_ar_to_ab =>
767
        ns <= fetch_1;
768
 
769
    when alu_code_to_t =>
770
        if uc_alu_class_reg = AC_I_to_D then
771
            ns <= alu_res_to_ram;
772
        else
773
            ns <= alu_res_to_a;
774
        end if;
775
 
776
    when alu_ram_to_v_code_to_t =>
777
        ns <= alu_res_to_ram;
778
 
779
    when alu_code_to_t_rx_to_ab =>
780
        if uc_alu_class_reg = AC_I_to_RI then
781
            ns <= alu_ram_to_ar_2;
782
        else
783
            ns <= alu_res_to_ram;
784
        end if;
785
 
786
    -- DJNZ Rn -------------------------------------------------------
787
    when djnz_rn_0 =>
788
        ns <= djnz_dir_1;
789
 
790
    -- DJNZ dir ------------------------------------------------------
791
    when djnz_dir_0 =>
792
        ns <= djnz_dir_1;
793
    when djnz_dir_1 =>
794
        ns <= djnz_dir_2;
795
    when djnz_dir_2 =>
796
        ns <= djnz_dir_3;
797
    when djnz_dir_3 =>
798
        if jump_condition='1' then
799
            ns <= djnz_dir_4;
800
        else
801
            ns <= fetch_0;
802
        end if;
803
    when djnz_dir_4 =>
804
        ns <= fetch_0;
805
 
806
    -- CJNE A, dir --------------------------------------------------
807
    when cjne_a_dir_0 =>
808
        ns <= cjne_a_dir_1;
809
    when cjne_a_dir_1 =>
810
        ns <= cjne_a_dir_2;
811
    when cjne_a_dir_2 =>
812
        if jump_condition='1' then
813
            ns <= cjne_a_dir_3;
814
        else
815
            ns <= fetch_0;
816
        end if;
817
    when cjne_a_dir_3 =>
818
        ns <= fetch_0;
819
 
820
    -- CJNE Rn, #imm ------------------------------------------------
821
    when cjne_rn_imm_0 =>
822
        ns <= cjne_rn_imm_1;
823
    when cjne_rn_imm_1 =>
824
        ns <= cjne_rn_imm_2;
825
    when cjne_rn_imm_2 =>
826
        if jump_condition='1' then
827
            ns <= cjne_rn_imm_3;
828
        else
829
            ns <= fetch_0;
830
        end if;
831
    when cjne_rn_imm_3 =>
832
        ns <= fetch_0;
833
 
834
    -- CJNE @Ri, #imm ------------------------------------------------
835
    when cjne_ri_imm_0 =>
836
        ns <= cjne_ri_imm_1;
837
    when cjne_ri_imm_1 =>
838
        ns <= cjne_ri_imm_2;
839
    when cjne_ri_imm_2 =>
840
        ns <= cjne_ri_imm_3;
841
    when cjne_ri_imm_3 =>
842
        ns <= cjne_ri_imm_4;
843
    when cjne_ri_imm_4 =>
844
        if jump_condition='1' then
845
            ns <= cjne_ri_imm_5;
846
        else
847
            ns <= fetch_0;
848
        end if;
849
    when cjne_ri_imm_5 =>
850
        ns <= fetch_0;
851
 
852
 
853
    -- CJNE A, #imm -------------------------------------------------
854
    when cjne_a_imm_0 =>
855
        ns <= cjne_a_imm_1;
856
    when cjne_a_imm_1 =>
857
        if jump_condition='1' then
858
            ns <= cjne_a_imm_2;
859
        else
860
            ns <= fetch_0;
861
        end if;
862
    when cjne_a_imm_2 =>
863
        ns <= fetch_0;
864
 
865
    -- Relative and long jumps --------------------------------------
866
    when load_rel =>
867
        if jump_condition='1' then
868
            ns <= rel_jump;
869
        else
870
            ns <= fetch_1;
871
        end if;
872
    when rel_jump =>
873
        ns <= fetch_0;
874
 
875
    when fetch_addr_1 =>
876
        ns <= fetch_addr_0;
877
    when fetch_addr_0 =>
878
        ns <= long_jump;
879
    when fetch_addr_0_ajmp =>
880
        ns <= long_jump;
881
    when long_jump =>
882
        ns <= fetch_0;
883
 
884
    when jmp_adptr_0 =>
885
        ns <= fetch_0;
886
 
887
 
888
    -- Derailed state machine should end here -----------------------
889
    -- NOTE: This applies to undecoded or unimplemented instructions,
890
    -- not to a state machine derailed by EM event, etc. 
891
 
892
    when others =>
893
        ns <= bug_bad_opcode_class;
894
    end case;
895
end process cpu_state_machine_transitions;
896
 
897
 
898
--## 2.- Interrupt handling ####################################################
899
 
900
load_ie <= '1' when sfr_addr_internal=SFR_ADDR_IE and sfr_we_internal='1'
901
           else '0';
902
 
903
IE_register:
904
process(clk)
905
begin
906
    if clk'event and clk='1' then
907
        if reset='1' then
908
            IE_reg <= (others => '0');
909
        else
910
            if load_ie='1' then
911
                IE_reg <= alu_result;
912
            end if;
913
        end if;
914
    end if;
915
end process IE_register;
916
 
917
-- RETI restores the IRQ level to 7 (idle value). No interrupt will be serviced
918
-- if its level is below this. Remember 0 is top, 4 is bottom and 7 is idle. 
919
irq_restore_level <= '1' when ps=ret_0 and alu_fn_reg(0)='1' else '0';
920
 
921
-- Mask the irq inputs with IE register bits...
922
irq_masked_inputs <= irq_source and std_logic_vector(IE_reg(4 downto 0));
923
-- ...and encode the highest priority active input
924
irq_level_inputs <=
925
    "000" when irq_masked_inputs(0)='1' else
926
    "001" when irq_masked_inputs(1)='1' else
927
    "010" when irq_masked_inputs(2)='1' else
928
    "011" when irq_masked_inputs(3)='1' else
929
    "100" when irq_masked_inputs(4)='1' else
930
    "111";
931
 
932
-- We have a pending irq if interrupts are enabled...    
933
irq_active <= '1' when IE_reg(7)='1' and
934
                       -- ...and the active irq has higher priority than any 
935
                       -- ongoing irq routine.
936
                       (irq_level_inputs < irq_level_current)
937
              else '0';
938
 
939
irq_registered_priority_encoder:
940
process(clk)
941
begin
942
    if clk'event and clk='1' then
943
        if reset='1' or irq_restore_level='1' then
944
            -- After reset, irq level is 7, which means all irqs are accepted.
945
            -- Note that valid levels are 0 to 4 and the lower the number,
946
            -- the higher the priority.
947
            -- The level is restored to 7 by the RETI instruction too.
948
            irq_level_current <= "111";
949
        else
950
            -- Evaluate and register the interrupt priority in the same state 
951
            -- the irq is to be acknowledged.
952
            if ps=fetch_1 and irq_active='1' then
953
                irq_level_current <= irq_level_inputs;
954
            end if;
955
        end if;
956
    end if;
957
end process irq_registered_priority_encoder;
958
 
959
-- This irq vector is going to be used in state irq_2.
960
irq_vector <= irq_level_current & "011";
961
 
962
 
963
--## 3.- Combined register bank & decoding table ###############################
964
 
965
-- No support yet for XRAM on this RAM block
966
assert not USE_BRAM_FOR_XRAM
967
report "This version of the core does not support using the IRAM/uCode "&
968
       "RAM block wasted space for XRAM"
969
severity failure;
970
 
971
 
972
-- This is the row of the opcode table that will be replicated for the 2nd half
973
-- of the table. We always replicate row 7 (opcode X7h) except for 'DNJZ Rn'
974
-- (opcodes d8h..dfh) where we replicate row 5 -- opcode d7h is not a DJNZ and
975
-- breaks the pattern (see @note1).
976
ucode_pattern <= "101" when code_rd(7 downto 4)="1101" else "111";
977
 
978
with code_rd(3) select ucode_index <=
979
    ucode_pattern & unsigned(code_rd(7 downto 4))   when '1',
980
    unsigned(code_rd(2 downto 0)) &
981
    unsigned(code_rd(7 downto 4))                   when others;
982
 
983
-- IRAM/SFR address source mux; controlled by current state
984
with ps select iram_sfr_addr <=
985
    rx_addr         when alu_rx_to_ab,
986
    rx_addr         when alu_code_to_t_rx_to_ab,
987
    rx_addr         when alu_ram_to_t_rx_to_ab,
988
    rx_addr         when cjne_rn_imm_0,
989
    rx_addr         when cjne_ri_imm_0,
990
    rx_addr         when alu_xchd_0,
991
    rx_addr         when movx_a_ri_0,
992
    rx_addr         when movx_ri_a_0,
993
    rx_addr         when xch_rx_0,
994
    rx_addr         when xch_rn_0,
995
    addr0_reg       when alu_res_to_ram_ar_to_ab,
996
    bit_addr        when jrb_bit_0,
997
    bit_addr        when bit_op_0,
998
    SP_reg          when push_2, -- Write dir data to [sp]
999
    SP_reg          when pop_0 | ret_0 | ret_1 | ret_2,
1000
    SP_reg          when acall_1 | acall_2 | lcall_2 | lcall_3,
1001
    SP_reg          when irq_2 | irq_3,
1002
    addr0_reg       when djnz_dir_1 | djnz_dir_2,
1003
    addr0_reg       when push_1, -- Read dir data for PUSH
1004
    code_byte       when cjne_a_imm_1,
1005
    code_byte       when cjne_a_dir_0,
1006
    code_byte       when cjne_a_dir_2,
1007
    code_byte       when cjne_rn_imm_1,
1008
    code_byte       when cjne_rn_imm_2,
1009
    code_byte       when cjne_ri_imm_4,
1010
    code_byte       when djnz_dir_0, -- DIR addr 
1011
    code_byte       when djnz_dir_3, -- REL offset
1012
    rx_addr         when djnz_rn_0,
1013
    addr0_reg       when jrb_bit_2,
1014
    code_byte       when jrb_bit_3,
1015
    addr0_reg       when bit_op_2,
1016
    code_byte       when fetch_addr_0,
1017
    code_byte       when fetch_addr_0_ajmp,
1018
    code_byte       when alu_code_to_ab, -- DIRECT addressing
1019
    code_byte       when push_0, -- read dir address
1020
    code_byte       when pop_1,
1021
    code_byte       when acall_0 | lcall_0 | lcall_1,
1022
    code_byte       when alu_res_to_ram_code_to_ab,
1023
    code_byte       when alu_ram_to_t_code_to_ab,
1024
    code_byte       when load_rel,
1025
    SFR_ADDR_DPH    when mov_dptr_1,
1026
    SFR_ADDR_DPL    when mov_dptr_2,
1027
    code_byte       when xch_dir_0,
1028
    addr0_reg       when others;
1029
 
1030
-- Assert this when an ALU instruction is about to use direct addressing. 
1031
with ps select alu_using_direct_addressing <=
1032
    '1' when alu_code_to_ab,
1033
    '1' when xch_dir_0,
1034
    '1' when alu_ram_to_t_code_to_ab,
1035
    '0' when others;
1036
 
1037
-- Assert this when an ALU instruction is using direct addressing.    
1038
with ps select alu_using_indirect_addressing <=
1039
    '1' when alu_ar_to_ab,
1040
    '1' when alu_rx_to_ab,
1041
    '1' when xch_rx_0,
1042
    '1' when movx_a_ri_0,
1043
    '1' when movx_ri_a_0,
1044
    '1' when alu_ram_to_t_rx_to_ab,
1045
    '1' when alu_res_to_ram_ar_to_ab,
1046
    '1' when alu_code_to_t_rx_to_ab,
1047
    '0' when others;
1048
 
1049
-- This register remembers what kind of addressing the current ALU instruction 
1050
-- is using.
1051
-- This is necessary because the ALU instruction states are shared by many 
1052
-- different instructions with different addressing modes.
1053
alu_addressing_mode_flipflop:
1054
process(clk)
1055
begin
1056
    if clk'event and clk='1' then
1057
        if reset = '1' then
1058
            direct_addressing_alu_reg <= '0';
1059
        else
1060
            if alu_using_direct_addressing = '1' then
1061
                direct_addressing_alu_reg <= '1';
1062
            elsif alu_using_indirect_addressing = '1' then
1063
                direct_addressing_alu_reg <= '0';
1064
            end if;
1065
        end if;
1066
    end if;
1067
end process alu_addressing_mode_flipflop;
1068
 
1069
-- This signal controls the T reg input mux. it should be valid in the cycle 
1070
-- the read/write is performed AND in the cycle after a read.
1071
-- FIXME these should be asserted in READ or WRITE states; many are not! review!
1072
with ps select direct_addressing <=
1073
    -- For most instructions all we need to know is the current state...
1074
    '0'                         when alu_rx_to_ab,
1075
    '0'                         when cjne_a_imm_0,
1076
    '0'                         when cjne_a_imm_1,
1077
    '0'                         when cjne_ri_imm_4,
1078
    '0'                         when cjne_ri_imm_3,
1079
    '0'                         when fetch_addr_0,
1080
    '0'                         when load_rel,
1081
    '0'                         when cjne_rn_imm_1,
1082
    '0'                         when cjne_rn_imm_2,
1083
    '1'                         when jrb_bit_0 | jrb_bit_1 | jrb_bit_2,
1084
    '1'                         when bit_op_0 | bit_op_1 | bit_op_2,
1085
    -- FIXME these below have been verified and corrected
1086 14 ja_rd
    '1'                         when djnz_dir_0,
1087 2 ja_rd
    '1'                         when djnz_dir_1,
1088
    '1'                         when djnz_dir_2,
1089
    '1'                         when push_0,
1090
    '1'                         when pop_1 | pop_2,
1091
    '0'                         when movx_a_ri_0,
1092
    '0'                         when movx_ri_a_0,
1093
    '1'                         when xch_dir_0,
1094
    '1'                         when cjne_a_dir_0,
1095
    '1'                         when cjne_a_dir_1,
1096
    '0'                         when alu_xchd_2 | alu_xchd_3,
1097
    -- ... and for ALU instructions we use info recorded while decoding.
1098
    '1'                         when alu_code_to_ab,
1099
    direct_addressing_alu_reg   when xch_1,
1100
    direct_addressing_alu_reg   when xch_2,
1101
    direct_addressing_alu_reg   when alu_ar_to_ab,
1102
    direct_addressing_alu_reg   when alu_ram_to_t,
1103
    direct_addressing_alu_reg   when alu_ram_to_t_code_to_ab,
1104
    direct_addressing_alu_reg   when alu_ram_to_t_rx_to_ab,
1105
    direct_addressing_alu_reg   when alu_ram_to_v_code_to_t,
1106
    direct_addressing_alu_reg   when alu_res_to_ram,
1107
    '1'                         when alu_res_to_ram_code_to_ab, -- D1 -> D only
1108
    '0'                         when alu_res_to_ram_ar_to_ab,
1109
    '0' when others;
1110
 
1111
-- SFRs are only ever accessed by direct addresing to area 80h..ffh
1112
sfr_addressing <= direct_addressing and iram_sfr_addr(7);
1113
 
1114
-- In 'fetch' states (including the last state of all instructions) the BRAM 
1115
-- is used as a decode table indexed by the opcode -- both ports!
1116
-- The decode table is in the lowest 256 bytes and the actual IRAM data is 
1117
-- in the highest 256 bytes, thus the address MSB.
1118
with do_fetch select bram_addr_p0 <=
1119
    -- 'fetch' states
1120
    '0' & ucode_index & '0'     when '1',
1121
    '1' & iram_sfr_addr         when others;
1122
 
1123
with do_fetch select bram_addr_p1 <=
1124
    -- 'fetch' states
1125
    '0' & ucode_index & '1'     when '1',
1126
    '1' & iram_sfr_addr         when others;
1127
 
1128
-- FIXME SFR/RAM selection bit: make sure it checks
1129
with ps select bram_we <=
1130
    not sfr_addressing          when alu_res_to_ram,
1131
    not sfr_addressing          when alu_res_to_ram_code_to_ab,
1132
    '1'                         when alu_res_to_ram_ar_to_ab,
1133
    not sfr_addressing          when djnz_dir_2,
1134
    not sfr_addressing          when bit_op_2,
1135
    not sfr_addressing          when jrb_bit_2,
1136
    not sfr_addressing          when push_2, -- FIXME verify
1137
    not sfr_addressing          when pop_2,  -- FIXME verify
1138
    not sfr_addressing          when xch_2,
1139
    '1'                         when alu_xchd_4,
1140
    '1'                         when acall_1 | acall_2 | lcall_2 | lcall_3,
1141
    '1'                         when irq_2 | irq_3,
1142
    '0'                         when others;
1143
 
1144
-- The datapath result is what's written back to the IRAM/SFR, except when 
1145
-- pushing the PC in a xCALL instruction.
1146
with ps select bram_wr_data_p0 <=
1147
    PC_reg(7 downto 0)          when acall_1 | lcall_2 | irq_2,
1148
    PC_reg(15 downto 8)         when acall_2 | lcall_3 | irq_3,
1149
    alu_result                  when others;
1150
 
1151
 
1152
-- IMPORTANT: the 2-port BRAM is inferred using a template which is valid for
1153
-- both Altera and Xilinx. Otherwise, the synth tools would infer TWO BRAMs 
1154
-- instead of one.
1155
-- This template is FRAGILE: for example, changing the order of assignments in 
1156
-- process *_port0 will break the synthesis (i.e. 2 BRAMs again).
1157
-- See a more detailed explaination in [3].
1158
 
1159
-- BRAM port 0 is read/write (i.e. same address for read and write)
1160
register_bank_bram_port0:
1161
process(clk)
1162
begin
1163
   if clk'event and clk='1' then
1164
        if bram_we='1' then
1165
            bram(to_integer(bram_addr_p0)) := bram_wr_data_p0;
1166
        end if;
1167
        bram_data_p0 <= bram(to_integer(bram_addr_p0));
1168
   end if;
1169
end process register_bank_bram_port0;
1170
 
1171
-- Port 1 is read only
1172
register_bank_bram_port1:
1173
process(clk)
1174
begin
1175
   if clk'event and clk='1' then
1176
        bram_data_p1 <= bram(to_integer(bram_addr_p1));
1177
   end if;
1178
end process register_bank_bram_port1;
1179
 
1180
-- End of BRAM inference template 
1181
 
1182
--## 4.- Instruction decode logic ##############################################
1183
 
1184
-- Assert do_fetch on the states when the *opcode* is present in code_rd.
1185
-- (not asserted for other instruction bytes, only the opcode).
1186
with ps select do_fetch <=
1187
    '1' when fetch_1,
1188
    '0' when others;
1189
 
1190
-- The main decode word is the synchrous BRAM output. Which means it is only 
1191
-- valid for 1 clock cycle (state decode_0). Most state machine decisions are 
1192
-- taken in that state. All information which is needed at a later state, like
1193
-- ALU control signals, is registered.
1194
-- Note that the BRAM is used for the 1st half of the decode table (@note1)...
1195
ucode_1st_half <= bram_data_p0 & bram_data_p1;
1196
 
1197
-- ...the 2nd hald of the table is done combinationally and then registered.
1198
ucode_2nd_half(8 downto 0) <= ucode_1st_half(8 downto 0);
1199
-- We take advantage of the opcode table layout: the 2nd half columns are always
1200
-- identical to the 1st half column, 7th or 5th row opcode (see ucode_pattern), 
1201
-- except for the addressing mode:
1202
with code_rd(7 downto 4) select ucode_2nd_half(15 downto 9) <=
1203
    "00" & AC_RN_to_RN          when "0000",
1204
    "00" & AC_RN_to_RN          when "0001",
1205
    "00" & AC_A_RN_to_A         when "0010",
1206
    "00" & AC_A_RN_to_A         when "0011",
1207
 
1208
    "00" & AC_A_RN_to_A         when "0100",
1209
    "00" & AC_A_RN_to_A         when "0101",
1210
    "00" & AC_A_RN_to_A         when "0110",
1211
    "00" & AC_I_to_RN           when "0111",
1212
 
1213
    "00" & AC_RN_to_D           when "1000",
1214
    "00" & AC_A_RN_to_A         when "1001",
1215
    "00" & AC_D_to_RN           when "1010",
1216
    F_CJNE_RN_IMM & CC_CJNE(3 downto 3)     when "1011",
1217
 
1218
    F_XCH_RN & "0"              when "1100",
1219
    F_DJNZ_RN & CC_NZ(3 downto 3)           when "1101",
1220
    "00" & AC_RN_to_A           when "1110",
1221
    "00" & AC_A_to_RN           when others;
1222
 
1223
 
1224
-- Register the combinational half of the uCode table so its timing is the same
1225
-- as the BRAM (so that both halves have the same timing).    
1226
process(clk)
1227
begin
1228
    if clk'event and clk='1' then
1229
        -- Register the information as soon as it is available: get opcode bits
1230
        -- when the opcode is in code_rd...
1231
        if do_fetch='1' then
1232
            ucode_is_2nd_half <= code_rd(3);
1233
            rn_index <= unsigned(code_rd(2 downto 0));
1234
        end if;
1235
        -- ...and get the ucode word when it is valid: the lowest half of the
1236
        -- ucode word comes from the ucode BRAM and is valid in decode_0.
1237
        if ps = decode_0 then
1238
            ucode_2nd_half_reg <= ucode_2nd_half;
1239
        end if;
1240
    end if;
1241
end process;
1242
 
1243
-- uCode may come from the BRAM (1st half of the table) or from  combinational 
1244
-- logic (2nd half). This is the multiplexor.
1245
ucode <= ucode_1st_half when ucode_is_2nd_half='0' else ucode_2nd_half_reg;
1246
 
1247
-- Extract uCode fields for convenience.
1248
 
1249
-- For ALU instructions, we have the flag mask in the uCode word...
1250
uc_alu_flag_mask <= ucode_1st_half(8 downto 7);
1251
-- ...and all other instructions only update the C flag, if any.
1252
with uc_class_decode_0(5 downto 4) select flag_mask_decode_0 <=
1253
    uc_alu_flag_mask    when "00",
1254
    FM_C                when others; -- Will only be used in a few states.
1255
 
1256
-- The mux for signal ucode operates after state decode_0, because its input 
1257
-- ucode_2nd_half_reg ir registered in that state. These following signals 
1258
-- depend on the ucode but need to be valid IN state decode_0 so they are muxed 
1259
-- separately directly on the opcode bit. 
1260
-- Valid only in state decode_0, all of these are registered too for later use.
1261
 
1262
with ucode_is_2nd_half select jump_cond_sel_decode_0 <=
1263
    ucode_1st_half(9 downto 6)      when '0',
1264
    ucode_2nd_half(9 downto 6)      when others;
1265
 
1266
with ucode_is_2nd_half select uc_alu_fn_decode_0 <=
1267
    ucode_1st_half(5 downto 0)      when '0',
1268
    ucode_2nd_half(5 downto 0)      when others;
1269
 
1270
with ucode_is_2nd_half select uc_class_decode_0 <=
1271
    ucode_1st_half(15 downto 10)    when '0',
1272
    ucode_2nd_half(15 downto 10)    when others;
1273
 
1274
with ucode_is_2nd_half select uc_alu_class_decode_0 <= -- ALU opc. addrng. mode 
1275
    ucode_1st_half(13 downto 9)     when '0',
1276
    ucode_2nd_half(13 downto 9)     when others;
1277
 
1278
 
1279
-- Register ALU & conditional jump control signals for use in states 
1280
-- after decode_0.
1281
alu_control_registers:
1282
process(clk)
1283
begin
1284
    if clk'event and clk='1' then
1285
        if ps=decode_0 then
1286
            uc_alu_class_reg <= uc_alu_class_decode_0;
1287
            alu_class_op_sel_reg <= alu_class_op_sel;
1288
            use_ri_reg <= use_ri;
1289
            flag_mask_reg <= flag_mask_decode_0;
1290
            alu_fn_reg <= uc_alu_fn_decode_0;
1291
            dpath_mux0_reg <= uc_alu_fn_decode_0(1) and uc_alu_fn_decode_0(0);
1292
            jump_cond_sel_reg <= jump_cond_sel_decode_0;
1293
            instr_jump_is_ljmp <= ucode(10);
1294
        end if;
1295
    end if;
1296
end process alu_control_registers;
1297
 
1298
--## 5.- PC and code address generation ########################################
1299
 
1300
rel_jump_delta(15 downto 8) <= (others => addr0_reg(7));
1301
rel_jump_delta(7 downto 0) <= addr0_reg;
1302
rel_jump_target <= PC_reg + rel_jump_delta;
1303
 
1304
-- A jump is LONG when running LJMP/LCALL OR acknowledging an interrupt.
1305
jump_is_ljmp <= '1' when ps=irq_4 else instr_jump_is_ljmp;
1306
 
1307
-- Mux for two kinds of jump target, AJMP/LJMP (jumps and calls)
1308
with jump_is_ljmp select jump_target <=
1309
    PC_reg(15 downto 11) & block_reg & addr0_reg    when '0',    -- AJMP
1310
    addr1_reg & addr0_reg                           when others; -- LJMP
1311
 
1312
-- Decide whether or not to increment the PC by looking at the NEXT state, not
1313
-- the present one. See @note5.
1314
with ns select increment_pc <=
1315
    '1' when decode_0,  -- See @note6.
1316
    '1' when alu_ram_to_t_code_to_ab,
1317
    '1' when alu_code_to_ab,
1318
    '1' when alu_res_to_ram_code_to_ab,
1319
    '1' when alu_ram_to_v_code_to_t,
1320
    '1' when alu_code_to_t_rx_to_ab,
1321
    '1' when alu_code_to_t,
1322
    '1' when jrb_bit_0,
1323
    '1' when jrb_bit_3,
1324
    '1' when bit_op_0,
1325
    '1' when cjne_a_imm_0,
1326
    '1' when cjne_a_imm_1,
1327
    '1' when cjne_a_dir_0,
1328
    '1' when cjne_a_dir_2,
1329
    '1' when cjne_ri_imm_3,
1330
    '1' when cjne_ri_imm_4,
1331
    '1' when cjne_rn_imm_1,
1332
    '1' when cjne_rn_imm_2,
1333
    '1' when fetch_addr_0,
1334
    '1' when fetch_addr_1,
1335
    '1' when fetch_addr_0_ajmp,
1336
    '1' when acall_0 | lcall_0 | lcall_1,
1337
    '1' when load_rel,
1338
    '1' when djnz_dir_0,
1339
    '1' when djnz_dir_3,
1340
    '1' when push_0,
1341
    '1' when pop_1,
1342
    '1' when mov_dptr_0,
1343
    '1' when mov_dptr_1,
1344
    '1' when xch_dir_0,
1345
    '0' when others;
1346
 
1347
with increment_pc select pc_incremented <=
1348
    PC_reg + 1  when '1',
1349
    PC_reg      when others;
1350
 
1351
 
1352
with ps select next_pc <=
1353
    X"0000"             when reset_0,
1354
    jump_target         when long_jump | lcall_4 | ret_3 | irq_4,
1355
    dptr_plus_a_reg     when jmp_adptr_0,
1356
    rel_jump_target     when rel_jump,
1357
    rel_jump_target     when cjne_a_imm_2,
1358
    rel_jump_target     when cjne_a_dir_3,
1359
    rel_jump_target     when cjne_ri_imm_5,
1360
    rel_jump_target     when cjne_rn_imm_3,
1361
    rel_jump_target     when djnz_dir_4,
1362
    rel_jump_target     when jrb_bit_4,
1363
    pc_incremented      when others;
1364
 
1365
 
1366
program_counter:
1367
process(clk)
1368
begin
1369
    if clk'event and clk='1' then
1370
        if reset='1' then
1371
            PC_reg <= (others => '0');
1372
        else
1373
            PC_reg <= next_pc;
1374
        end if;
1375
    end if;
1376
end process program_counter;
1377
 
1378
with ps select code_addr <=
1379
    std_logic_vector(movc_addr)     when movc_pc_0 | movc_dptr_0,
1380
    std_logic_vector(PC_reg)        when others;
1381
 
1382
 
1383
---- MOVC logic ----------------------------------------------------------------
1384
acc_ext16 <= (X"00") & A_reg;
1385
 
1386
with ps select movc_base <=
1387
    PC_reg          when movc_pc_0,
1388
    DPTR_reg        when others;
1389
 
1390
movc_addr <= movc_base + acc_ext16;
1391
 
1392
-- This register will not always hold DPTR+A, at times it will hold PC+A. In the
1393
-- state in which it will be used, it will be DPTR+A.
1394
registered_dptr_plus_a:
1395
process(clk)
1396
begin
1397
    if clk'event and clk='1' then
1398
        dptr_plus_a_reg <= movc_addr;
1399
    end if;
1400
end process registered_dptr_plus_a;
1401
 
1402
 
1403
--## 6.- Conditional jump logic ################################################
1404
 
1405
cjne_condition <= not alu_result_is_zero; -- FIXME redundant, remove
1406
 
1407
with jump_cond_sel_reg select jump_condition <=
1408
    '1'                         when CC_ALWAYS,
1409
    not alu_result_is_zero      when CC_NZ,
1410
    alu_result_is_zero          when CC_Z,
1411
    not acc_is_zero             when CC_ACCNZ,
1412
    acc_is_zero                 when CC_ACCZ,
1413
    cjne_condition              when CC_CJNE,
1414
    PSW(7)                      when CC_C,
1415
    not PSW(7)                  when CC_NC,
1416
    bit_input                   when CC_BIT,
1417
    not bit_input               when CC_NOBIT,
1418
    '0'                         when others;
1419
 
1420
 
1421
--## 7.- Address registers #####################################################
1422
 
1423
---- Address 0 and 1 registers -------------------------------------------------
1424
 
1425
-- addr0_reg is the most frequent source of IRAM/SFR addresses and is used as
1426
-- an aux reg in jumps and rets.
1427
 
1428
-- Decide when to load the address register...
1429
with ps select load_addr0 <=
1430
    '1' when fetch_addr_0,
1431
    '1' when fetch_addr_0_ajmp,
1432
    '1' when irq_2,
1433
    '1' when acall_0 | lcall_1,
1434
    '1' when load_rel,
1435
    '1' when cjne_a_imm_1,
1436
    '1' when cjne_a_dir_0,
1437
    '1' when cjne_a_dir_2,
1438
    '1' when cjne_ri_imm_1,
1439
    '1' when cjne_ri_imm_4,
1440
    '1' when cjne_rn_imm_2,
1441
    '1' when alu_xchd_1,
1442
    '1' when djnz_dir_0,
1443
    '1' when djnz_dir_3,
1444
    '1' when djnz_rn_0,
1445
    '1' when jrb_bit_0,
1446
    '1' when jrb_bit_3,
1447
    '1' when bit_op_0,
1448
    '1' when pop_1,
1449
    '1' when ret_2,
1450
    '1' when alu_rx_to_ab,
1451
    '1' when movx_a_ri_1,
1452
    '1' when movx_ri_a_1,
1453
    '1' when xch_dir_0,
1454
    '1' when xch_rx_0,
1455
    '1' when xch_rn_0,
1456
    '1' when xch_rx_1,
1457
    '1' when alu_ram_to_ar,
1458
    '1' when alu_ram_to_t_code_to_ab,
1459
    '1' when alu_code_to_ab,
1460
    '1' when alu_res_to_ram_code_to_ab,
1461
    '1' when alu_ram_to_t_rx_to_ab,
1462
    --'1' when alu_res_to_ram_ram_to_ab,
1463
    '1' when alu_ram_to_ar_2,
1464
    '1' when alu_code_to_t_rx_to_ab,
1465
    '0' when others;
1466
 
1467
-- ...and decide what to load into it.
1468
with ps select addr0_reg_input <=
1469
    iram_sfr_rd         when alu_ram_to_ar,
1470
    iram_sfr_rd         when alu_ram_to_ar_2,
1471
    iram_sfr_rd         when cjne_ri_imm_1,
1472
    iram_sfr_rd         when alu_xchd_1,
1473
    iram_sfr_rd         when movx_a_ri_1,
1474
    iram_sfr_rd         when movx_ri_a_1,
1475
    iram_sfr_rd         when xch_rx_0,
1476
    iram_sfr_rd         when xch_rx_1,
1477
    iram_sfr_rd         when ret_2,
1478
    "00" & irq_vector   when irq_2,
1479
    iram_sfr_addr       when others;
1480
 
1481
-- Auxiliary address registers 0 and 1 plus flag bit_index_reg
1482
address_registers:
1483
process(clk)
1484
begin
1485
    if clk'event and clk='1' then
1486
        if load_addr0='1' then
1487
            -- Used to address IRAM/SCR and XRAM
1488
            addr0_reg <= addr0_reg_input;
1489
            -- The bit index is registered at the same time for use by bit ops.
1490
            -- Signal bit_index is valid at the same time as addr0_reg_input.
1491
            bit_index_reg <= bit_index;
1492
        end if;
1493
        if ps = lcall_0 or ps = fetch_addr_1 then
1494
            addr1_reg <= unsigned(code_rd);
1495
        elsif ps = irq_2 then
1496
            addr1_reg <= (others => '0');
1497
        elsif ps = ret_1 then
1498
            addr1_reg <= iram_sfr_rd;
1499
        end if;
1500
    end if;
1501
end process address_registers;
1502
 
1503
---- Opcode register(s) and signals directly derived from the opcode -----------
1504
 
1505
-- These register holds the instruction opcode byte.
1506
absolute_jump_block_register:
1507
process(clk)
1508
begin
1509
    if clk'event and clk='1' then
1510
        if ps=decode_0 then
1511
            block_reg <= unsigned(code_rd(7 downto 5));
1512
        end if;
1513
    end if;
1514
end process absolute_jump_block_register;
1515
 
1516
-- Unregistered dir address; straight from code memory.
1517
code_byte <= unsigned(code_rd);
1518
 
1519
-- Index of bit within byte. Will be registered along with bit_addr.
1520
bit_index <= unsigned(code_rd(2 downto 0));
1521
 
1522
-- Address of the byte containing the operand bit, if any.
1523
with code_rd(7) select bit_addr <=
1524
    "0010" & unsigned(code_rd(6 downto 3))        when '0',
1525
    "1" & unsigned(code_rd(6 downto 3)) & "000"   when others;
1526
 
1527
 
1528
---- Rn and @Ri addresses and multiplexor --------------------------------------
1529
ri_addr <= "000" & PSW_reg(4 downto 3) & "00" & rn_index(0);
1530
rn_addr <= "000" & PSW_reg(4 downto 3) & rn_index;
1531
 
1532
 
1533
-- This logic only gets evaluated in state decode_0; and it needs to be valid 
1534
-- only for Rn and @Ri instructions, otherwise it is not evaluated.
1535
-- Does the ALU instruction, if any, use @Ri?
1536
with uc_alu_class_decode_0 select alu_use_ri_by_class <=
1537
    '1' when AC_A_RI_to_A,
1538
    '1' when AC_RI_to_A,
1539
    '1' when AC_RI_to_RI,
1540
    '1' when AC_RI_to_D,
1541
    '1' when AC_D_to_RI,
1542
    '1' when AC_I_to_RI,
1543
    '1' when AC_A_to_RI,
1544
    '0' when others;
1545
 
1546
-- The instruction uses @Ri unless it uses Rn.
1547
-- This signal gets registered as use_ri_reg in state decode_0. 
1548
alu_use_ri <= '0' when code_rd(3)='1' else alu_use_ri_by_class;
1549
 
1550
non_alu_use_ri <= '1' when ucode(11 downto 10)="10" else '0';
1551
 
1552
use_ri <= alu_use_ri when ucode(15 downto 14)="00" else non_alu_use_ri;
1553
 
1554
-- This is the actual Rn/Ri multiplexor.     
1555
rx_addr <= ri_addr when use_ri_reg='1' else rn_addr;
1556
 
1557
 
1558
--## 8.- SFR interface #########################################################
1559
 
1560
-- Assert sfr_vma when a SFR read or write cycle is going on (address valid).
1561
-- FIXME some of these are not read cycles but the cycle after!
1562
with ps select sfr_vma_internal <=
1563
    sfr_addressing      when alu_ar_to_ab,
1564
    sfr_addressing      when alu_ram_to_t,
1565
    sfr_addressing      when alu_ram_to_t_code_to_ab,
1566
    sfr_addressing      when alu_ram_to_v_code_to_t,
1567
    sfr_addressing      when alu_res_to_ram,
1568
    sfr_addressing      when alu_res_to_ram_code_to_ab,
1569
    sfr_addressing      when alu_res_to_ram_ar_to_ab,
1570
    sfr_addressing      when djnz_dir_1,
1571
    sfr_addressing      when djnz_dir_2,
1572
    sfr_addressing      when cjne_ri_imm_3,
1573
    sfr_addressing      when cjne_ri_imm_2,
1574
    -- FIXME these are corrected states, the above may be bad
1575
    sfr_addressing      when cjne_a_dir_0,
1576
    sfr_addressing      when jrb_bit_0,
1577
    sfr_addressing      when bit_op_0,
1578
    sfr_addressing      when bit_op_2,
1579
    sfr_addressing      when xch_1,
1580
    sfr_addressing      when xch_2,
1581
    sfr_addressing      when alu_xchd_2,
1582
    -- FIXME some other states missing
1583
    '0'                 when others;
1584
 
1585
-- Assert sfr_we_internal when a SFR write cycle is done.
1586
-- FIXME there should be a direct_we, not separate decoding for iram/sfr
1587
with ps select sfr_we_internal <=
1588
    sfr_addressing      when alu_res_to_ram,
1589
    sfr_addressing      when alu_res_to_ram_code_to_ab,
1590
    sfr_addressing      when alu_res_to_ram_ar_to_ab,
1591
    sfr_addressing      when djnz_dir_2,
1592
    sfr_addressing      when bit_op_2,
1593
    sfr_addressing      when jrb_bit_2,
1594
    sfr_addressing      when pop_2,
1595
    '1'                 when mov_dptr_1,
1596
    '1'                 when mov_dptr_2,
1597
    sfr_addressing      when xch_2,
1598
    '0'                 when others;
1599
 
1600
-- The SFR address is the full 8 address bits; even if we know there are no
1601
-- SFRs in the 00h..7fh range.
1602
sfr_addr_internal <= iram_sfr_addr;
1603
sfr_addr <= std_logic_vector(sfr_addr_internal);
1604
-- Data written into the internal or externa SFR is the IRAM input data.
1605
sfr_wr_internal <= bram_wr_data_p0;
1606
sfr_wr <= std_logic_vector(sfr_wr_internal);
1607
-- Internal and external SFR bus is identical. 
1608
sfr_we <= sfr_we_internal;
1609
sfr_vma <= sfr_vma_internal;
1610
 
1611
-- Internal SFR read multiplexor. Will be registered.
1612
with sfr_addr_internal select sfr_rd_internal <=
1613
    PSW                     when SFR_ADDR_PSW,
1614
    SP_reg                  when SFR_ADDR_SP,
1615
    A_reg                   when SFR_ADDR_ACC,
1616
    B_reg                   when SFR_ADDR_B,
1617
    DPTR_reg(15 downto 8)   when SFR_ADDR_DPH,
1618
    DPTR_reg( 7 downto 0)   when SFR_ADDR_DPL,
1619
    IE_reg                  when SFR_ADDR_IE,
1620
    unsigned(sfr_rd)  when others;
1621
 
1622
-- Registering the SFR read mux gives the SFR block the same timing behavior as
1623
-- the IRAM block, and improves clock rate a lot.
1624
SFR_mux_register:
1625
process(clk)
1626
begin
1627
    if clk'event and clk='1' then
1628
        sfr_rd_internal_reg <= sfr_rd_internal;
1629
        sfr_addressing_reg <= sfr_addressing;
1630
    end if;
1631
end process SFR_mux_register;
1632
 
1633
-- Data read from the IRAM/SFR: this is the IRAM vs. SFR multiplexor.
1634
-- Note it is controlled with sfr_addressing_reg, which is in sync with both
1635
-- sfr_rd_internal_reg and bram_data_p0 because of the 1-cycle data latency
1636
-- of the BRAM and the SFR interface.
1637
iram_sfr_rd <= sfr_rd_internal_reg when sfr_addressing_reg='1' else bram_data_p0;
1638
 
1639
 
1640
--## 9.- PSW register and flag logic ###########################################
1641
 
1642
 
1643
-- PSW flag update enable.
1644
with ps select update_psw_flags <=
1645
    -- Flags are updated at the same time ACC/RAM/SFR is loaded...
1646
    '1' when alu_res_to_a,
1647
    '1' when alu_res_to_ram,
1648
    '1' when alu_res_to_ram_code_to_ab,
1649
    '1' when alu_res_to_ram_ar_to_ab,
1650
    '1' when alu_daa_1,
1651
        -- (XCHD states included for generality only; they never 
1652
        -- update any flags (FM_NONE)).
1653
    '1' when alu_xchd_4 | alu_xchd_5,
1654
    -- ...when the CJNE magnitude comparison is made...
1655
    '1' when cjne_a_imm_1,
1656
    '1' when cjne_a_dir_2,
1657
    '1' when cjne_ri_imm_4,
1658
    '1' when cjne_rn_imm_2,
1659
    -- ...when C is updated due by bit operation...
1660
    '1' when bit_res_to_c,
1661
    -- ...and when a mul/div is done.
1662
    mul_ready when alu_mul_0,
1663
    div_ready when alu_div_0,
1664
    -- Note some 
1665
    '0' when others;
1666
 
1667
-- PSW write enable for SFR accesses.    
1668
load_psw <= '1' when sfr_addr_internal=SFR_ADDR_PSW and sfr_we_internal='1'
1669
            else '0';
1670
 
1671
PSW_register:
1672
process(clk)
1673
begin
1674
    if clk'event and clk='1' then
1675
        if reset='1' then
1676
            PSW_reg <= (others => '0');
1677
        elsif load_psw = '1' then
1678
            PSW_reg <= alu_result(7 downto 1);
1679
        elsif update_psw_flags = '1' then
1680
 
1681
            -- C flag
1682
            if flag_mask_reg /= FM_NONE then
1683
                PSW_reg(7) <= alu_cy;
1684
            end if;
1685
            -- OV flag
1686
            if flag_mask_reg = FM_C_OV or flag_mask_reg = FM_ALL then
1687
                PSW_reg(2) <= alu_ov;
1688
            end if;
1689
            -- AC flag
1690
            if flag_mask_reg = FM_ALL then
1691
                PSW_reg(6) <= alu_ac;
1692
            end if;
1693
        end if;
1694
    end if;
1695
end process PSW_register;
1696
 
1697
-- Note that P flag (bit 0) is registered separately. Join flags up here.
1698
PSW <= PSW_reg(7 downto 1) & alu_p;
1699
 
1700
 
1701
--## 10.- Stack logic ##########################################################
1702
 
1703
-- SP write enable for SFR accesses.    
1704
load_sp <= '1' when sfr_addr_internal=SFR_ADDR_SP and sfr_we_internal='1'
1705
           else '0';
1706
 
1707
with ps select load_sp_implicit <=
1708
    '1' when push_1 | pop_1,
1709
    '1' when ret_0 | ret_1,
1710
    '1' when acall_0 | acall_1 | lcall_1 | lcall_2,
1711
    '1' when irq_1 | irq_2,
1712
    '0' when others;
1713
 
1714
update_sp <= load_sp or load_sp_implicit;
1715
 
1716
with ps select SP_next <=
1717
    SP_reg + 1          when push_1 | acall_0 | acall_1 |
1718
                             lcall_1 | lcall_2 |
1719
                             irq_1 | irq_2,
1720
    SP_reg - 1          when pop_1 | ret_0 | ret_1,
1721
    nobit_alu_result    when others;
1722
 
1723
 
1724
stack_pointer:
1725
process(clk)
1726
begin
1727
    if clk'event and clk='1' then
1728
        if reset = '1' then
1729
            SP_reg <= X"07";
1730
        else
1731
            if update_sp = '1' then
1732
                SP_reg <= SP_next;
1733
            end if;
1734
        end if;
1735
    end if;
1736
end process stack_pointer;
1737
 
1738
 
1739
--## 11.- Datapath: ALU and ALU operand multiplexors ###########################
1740
 
1741
-- ALU input operand mux control for ALU class instructions. Other instructions
1742
-- that use the ALU (CJNE, DJNZ...) will need to control those muxes. That logic
1743
-- is within the ALU module.
1744
-- Note that this signal gets registered for speed before being used.
1745
with uc_alu_class_decode_0 select alu_class_op_sel <=
1746
    AI_A_T  when AC_A_RI_to_A,
1747
    AI_A_T  when AC_A_RN_to_A,
1748
    AI_A_T  when AC_A_I_to_A,
1749
    AI_A_T  when AC_A_D_to_A,
1750
    AI_A_T  when AC_A_D_to_D,
1751
    AI_V_T  when AC_I_D_to_D,
1752
    AI_A_0  when AC_A_to_RI,
1753
    AI_A_0  when AC_A_to_D,
1754
    AI_A_0  when AC_A_to_RN,
1755
    AI_A_0  when AC_A_to_A,
1756
    AI_T_0  when others;
1757
 
1758
 
1759
    alu : entity work.light52_alu
1760
    generic map (
1761
        IMPLEMENT_BCD_INSTRUCTIONS => IMPLEMENT_BCD_INSTRUCTIONS,
1762
        SEQUENTIAL_MULTIPLIER => SEQUENTIAL_MULTIPLIER
1763
    )
1764
    port map (
1765
        clk =>                  clk,
1766
        reset =>                reset,
1767
 
1768
        -- Data outputs
1769
        result =>               alu_result,
1770
        nobit_result =>         nobit_alu_result,
1771
        bit_input_out =>        bit_input,
1772
 
1773
        -- Access to internal stuff
1774
        ACC =>                  A_reg,
1775
        B =>                    B_reg,
1776
 
1777
        -- XRAM, IRAM and CODE data interface
1778
        xdata_wr =>             xdata_wr,
1779
        xdata_rd =>             xdata_rd,
1780
        code_rd =>              code_rd,
1781
        iram_sfr_rd =>          iram_sfr_rd,
1782
 
1783
        -- Input and output flags
1784
        cy_in =>                PSW_reg(7),
1785
        ac_in =>                PSW_reg(6),
1786
        cy_out =>               alu_cy,
1787
        ov_out =>               alu_ov,
1788
        ac_out =>               alu_ac,
1789
        p_out =>                alu_p,
1790
        acc_is_zero =>          acc_is_zero,
1791
        result_is_zero =>       alu_result_is_zero,
1792
 
1793
        -- Control inputs        
1794
        use_bitfield =>         dpath_mux0_reg,
1795
        alu_fn_reg =>           alu_fn_reg,
1796
        bit_index_reg =>        bit_index_reg,
1797
        op_sel =>               alu_class_op_sel_reg,
1798
        load_acc_sfr =>         load_acc_sfr,
1799
 
1800
        load_b_sfr =>           load_b_sfr,
1801
        mul_ready =>            mul_ready,
1802
        div_ready =>            div_ready,
1803
 
1804
        ps =>                   ps
1805
    );
1806
 
1807
 
1808
load_b_sfr <= '1' when (sfr_addr_internal=SFR_ADDR_B and
1809
                        sfr_we_internal='1')
1810
              else '0';
1811
 
1812
load_acc_sfr <= '1' when (sfr_addr_internal=SFR_ADDR_ACC and
1813
                          sfr_we_internal='1')
1814
                else '0';
1815
 
1816
--## 12.- DPTR and XDATA address generation ####################################
1817
 
1818
-- FIXME this should be encapsulated so that a 2nd DPTR can be easily added.
1819
 
1820
load_dph <= '1' when sfr_addr_internal=SFR_ADDR_DPH and sfr_we_internal='1'
1821
            else '0';
1822
load_dpl <= '1' when sfr_addr_internal=SFR_ADDR_DPL and sfr_we_internal='1'
1823
            else '0';
1824
inc_dptr <= '1' when ps=special_0 else '0';
1825
 
1826
DPTR_register:
1827
process(clk)
1828
begin
1829
    if clk'event and clk='1' then
1830
        if inc_dptr='1' then
1831
            DPTR_reg <= DPTR_reg + 1;
1832
        else
1833
            if load_dph='1' then
1834
                DPTR_reg(15 downto 8) <= nobit_alu_result;
1835
            end if;
1836
            if load_dpl='1' then
1837
                DPTR_reg(7 downto 0) <= nobit_alu_result;
1838
            end if;
1839
        end if;
1840
    end if;
1841
end process DPTR_register;
1842
 
1843
with ps select xdata_addr <=
1844
    X"00" & std_logic_vector(addr0_reg)     when movx_ri_a_2,
1845
    X"00" & std_logic_vector(addr0_reg)     when movx_a_ri_2,
1846
    std_logic_vector(DPTR_reg)              when others;
1847
 
1848
with ps select xdata_vma <=
1849
    '1' when movx_dptr_a_0 | movx_a_dptr_0,
1850
    '1' when movx_a_ri_2 | movx_ri_a_2,
1851
    '0' when others;
1852
 
1853
with ps select xdata_we <=
1854
    '1' when movx_dptr_a_0,
1855
    '1' when movx_ri_a_2,
1856
    '0' when others;
1857
 
1858
 
1859
end architecture microcoded;
1860
 
1861
--------------------------------------------------------------------------------
1862
-- NOTES:
1863
--
1864
-- @note1: Decoding of '2nd half' opcodes.
1865
--      The top half of the opcode table is decoded using the BRAM initialized 
1866
--      as a decoding table. 
1867
--      The bottom half of the table (Rn opcodes, rows 8 to 15) is very 
1868
--      symmetric: you only need to decode the columns, and the opcode of each 
1869
--      column is the same as that of row 7 (with a couple exceptions). 
1870
--      This means we can replace the entire bottom half of the decoding table 
1871
--      with some logic, combined with row 7 (or 5). Logic marked with @note1 
1872
--      has this purpose.
1873
--
1874
-- @note2: Unnecessary reset values for data registers.
1875
--      Reset values are strictly unnecessary for data registers such as ACC (as
1876
--      opposed to control registers). They have been given a reset value only 
1877
--      so that the simulations logs are identical to those of B51 software 
1878
--      simulator.
1879
--
1880
-- @note3: Adder/subtractor.
1881
--      The adder/subtractor is coded as a simple adder in which the subtrahend
1882
--      is optionally negated -- this is for portability across synth tools.
1883
--
1884
-- @note4: End states of the instructions.
1885
--      All instructions end in either fetch_0 or fetch_1. State fetch_1 can be 
1886
--      thus considered the first state all instructions have in common. It is
1887
--      this state that does the the interrupt check. 
1888
--      In a future revision, many instructions may overlap fetch_0 and fetch_1
1889
--      with their last states and the irq check will change.
1890
--
1891
-- @note5: Use of ns (instead of ps) in pc_incremented.
1892
--      This is done because several states are 'shared' by more than one 
1893
--      instruction (i.e. they belong to more than one state machine paths) so 
1894
--      looking at the present state is not enough (Note that the *complete*
1895
--      state in fact includes ps and uc_class_decode_0).
1896
--      This hack simplifies the logic but hurts speed A LOT (like 10% or more).
1897
--      We should use a *cleaner* state machine; it'd be much larger but also
1898
--      simpler.
1899
--
1900
-- @note6: State on which PC is incremented.
1901
--      PC is incremented in state decode_0 and not fetch_1 so that it is still 
1902
--      valid in fetch_1 in case we have to push it for an interrupt response.
1903
--------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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