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