1 |
4 |
Valerio63 |
----------------------------------------------------------------------
|
2 |
|
|
-- 8 bit microprocessor (65C02) with some enhances VHDL project --
|
3 |
|
|
-- Full RTL synchronous pipelined architecture --
|
4 |
|
|
-- Project by Valerio Venturi (Italy) --
|
5 |
|
|
-- Date: 14/04/2011 --
|
6 |
|
|
-- Last revision: 05/05/2011 --
|
7 |
|
|
----------------------------------------------------------------------
|
8 |
|
|
|
9 |
|
|
library IEEE;
|
10 |
|
|
use IEEE.std_logic_1164.all; -- defines std_logic types
|
11 |
|
|
use IEEE.STD_LOGIC_unsigned.all;
|
12 |
|
|
use IEEE.STD_LOGIC_arith.all;
|
13 |
|
|
|
14 |
|
|
-- global architecture
|
15 |
|
|
entity v6502 is
|
16 |
|
|
port( clk0: in STD_LOGIC; -- PHASE0 clock input
|
17 |
|
|
res: in STD_LOGIC; -- reset input
|
18 |
|
|
irq: in STD_LOGIC; -- interrupt request input
|
19 |
|
|
nmi: in STD_LOGIC; -- not maskable interrupt input
|
20 |
|
|
rdy: in STD_LOGIC; -- wait state input (read/write)
|
21 |
|
|
so: in STD_LOGIC; -- set overflow V input
|
22 |
|
|
rw: out STD_LOGIC; -- read/write out
|
23 |
|
|
sync: out STD_LOGIC; -- opcode fetch out
|
24 |
|
|
vp: out STD_LOGIC; -- vector pull
|
25 |
|
|
ope: out STD_LOGIC; -- microcode end
|
26 |
|
|
addr: out STD_LOGIC_VECTOR(15 downto 0); -- 16 bit address bus out
|
27 |
|
|
data_in: in STD_LOGIC_VECTOR(7 downto 0); -- 8 bit input data bus
|
28 |
|
|
data_out: out STD_LOGIC_VECTOR(7 downto 0) -- 8 bit output data bus
|
29 |
|
|
);
|
30 |
|
|
end v6502;
|
31 |
|
|
|
32 |
|
|
architecture struct of v6502 is
|
33 |
|
|
signal i_res: STD_LOGIC; -- internal global reset RES
|
34 |
|
|
signal i_irq: STD_LOGIC; -- internal interrupt request IRQ
|
35 |
|
|
signal i_nmi: STD_LOGIC; -- internal interrupt request NMI
|
36 |
|
|
signal i_rdy: STD_LOGIC; -- internal wait request RDY
|
37 |
|
|
signal i_so: STD_LOGIC; -- internal set overflow SO
|
38 |
|
|
signal i_vp: STD_LOGIC; -- internal VP (vector pull)
|
39 |
|
|
signal int: STD_LOGIC; -- internal global interrupt (instruction boundary synchronized)
|
40 |
|
|
signal we: STD_LOGIC; -- write enable (combinatorial from PLA)
|
41 |
|
|
signal we_r: STD_LOGIC; -- write enable (registered)
|
42 |
|
|
signal ien: STD_LOGIC; -- interrupt IRQ enable
|
43 |
|
|
|
44 |
|
|
-- microcode signals (register control)
|
45 |
|
|
signal regop: STD_LOGIC_VECTOR(3 downto 0); -- register operation microcode
|
46 |
|
|
signal rsel: STD_LOGIC_VECTOR(3 downto 0); -- register select microcode
|
47 |
|
|
signal a_l: STD_LOGIC; -- A load
|
48 |
|
|
signal x_l: STD_LOGIC; -- X load
|
49 |
|
|
signal y_l: STD_LOGIC; -- Y load
|
50 |
|
|
signal z_l: STD_LOGIC; -- Y load
|
51 |
|
|
signal p_l: STD_LOGIC; -- P load
|
52 |
|
|
signal o_l: STD_LOGIC; -- OPE load
|
53 |
|
|
signal sp_ll: STD_LOGIC; -- SP load lsb
|
54 |
|
|
signal sp_lh: STD_LOGIC; -- SP load msb
|
55 |
|
|
signal sp_u: STD_LOGIC; -- SP increment
|
56 |
|
|
signal sp_d: STD_LOGIC; -- SP decrement
|
57 |
|
|
signal dmux_sel: STD_LOGIC_VECTOR(1 downto 0); -- ALU operand #2 data multiplexer
|
58 |
|
|
|
59 |
|
|
-- microcode signals (ALU control)
|
60 |
|
|
signal aluop: STD_LOGIC_VECTOR(4 downto 0); -- ALU operation code
|
61 |
|
|
|
62 |
|
|
-- microcode signals CPU control logic
|
63 |
|
|
signal opfetch: STD_LOGIC; -- opcode fetch
|
64 |
|
|
signal i_sync: STD_LOGIC; -- internal SYNC not latched
|
65 |
|
|
signal m_sync: STD_LOGIC; -- internal SYNC latched
|
66 |
|
|
signal opdec: STD_LOGIC; -- opcode decode
|
67 |
|
|
signal pcmp: STD_LOGIC_VECTOR(1 downto 0); -- PC/MP out control effective
|
68 |
|
|
signal pcmp_mc: STD_LOGIC_VECTOR(1 downto 0); -- PC/MP out control microcode
|
69 |
|
|
signal pcinc: STD_LOGIC; -- PC increment
|
70 |
|
|
signal e_eop: STD_LOGIC; -- early microcode sequence end (for some opcodes)
|
71 |
|
|
signal mc_eop: STD_LOGIC; -- microcode sequence end (for some opcodes)
|
72 |
|
|
signal eop: STD_LOGIC; -- microcode sequence end (effective)
|
73 |
|
|
signal we_mc: STD_LOGIC; -- microcode write enable
|
74 |
|
|
signal we_mc_l: STD_LOGIC; -- microcode write enable to latch
|
75 |
|
|
signal fbrk: STD_LOGIC; -- force BRK opcode (used by hardware interrupts)
|
76 |
|
|
signal opbrk: STD_LOGIC; -- BRK opcode (used for distinguish between hardware/software interrupts)
|
77 |
|
|
signal bcf: STD_LOGIC; -- branch condition resolved
|
78 |
|
|
signal pcc: STD_LOGIC; -- PC carry
|
79 |
|
|
signal clri: STD_LOGIC; -- clear interrupt request pending microcode
|
80 |
|
|
signal vso: STD_LOGIC; -- SO input high to low edge flag
|
81 |
|
|
signal mc_branch: STD_LOGIC; -- branch (relative) opcode
|
82 |
|
|
signal adc_sbc_mc: STD_LOGIC; -- ADC/SBC opcode (used for decimal adjustment)
|
83 |
|
|
signal ai_op: STD_LOGIC; -- opcode with absolute indexed addressing mode
|
84 |
|
|
signal daa_req: STD_LOGIC; -- DAA required
|
85 |
|
|
signal mcad: STD_LOGIC_VECTOR(10 downto 0); -- microcode address
|
86 |
|
|
signal mcscan: STD_LOGIC_VECTOR(2 downto 0); -- microcode pointer control
|
87 |
|
|
signal p_op: STD_LOGIC_VECTOR(3 downto 0); -- microcode control bits register P
|
88 |
|
|
signal pcr_fc: STD_LOGIC_VECTOR(2 downto 0); -- microcode control PC
|
89 |
|
|
signal mpr_fc: STD_LOGIC_VECTOR(3 downto 0); -- microcode control MP
|
90 |
|
|
signal mcbit: STD_LOGIC_VECTOR(34 downto 0); -- microcode control bits
|
91 |
|
|
signal regbit: STD_LOGIC_VECTOR(8 downto 0); -- microcode control bits
|
92 |
|
|
signal ivoffs: STD_LOGIC_VECTOR(2 downto 0); -- microcode interrupt vector offset encoding
|
93 |
|
|
signal mcn: STD_LOGIC; -- microcode does NOPs
|
94 |
|
|
signal add_sub_op: STD_LOGIC; -- ADC/SBC opcode
|
95 |
|
|
|
96 |
|
|
-- ALU signals
|
97 |
|
|
signal bcd: STD_LOGIC; -- ALU binary/bcd mode
|
98 |
|
|
signal c_flg: STD_LOGIC; -- ALU carry flag
|
99 |
|
|
signal z_flg: STD_LOGIC; -- ALU zero flag
|
100 |
|
|
signal v_flg: STD_LOGIC; -- ALU overflow flag
|
101 |
|
|
signal n_flg: STD_LOGIC; -- ALU negative flag
|
102 |
|
|
signal pc_c_alu_flg: STD_LOGIC; -- ALU PC carry flag
|
103 |
|
|
signal acr_reg: STD_LOGIC; -- ALU auxiliary carry (registered)
|
104 |
|
|
signal bcd_lsb: STD_LOGIC; -- bcd lsb overflow flag
|
105 |
|
|
signal bcd_msb: STD_LOGIC; -- bcd msb overflow flag
|
106 |
|
|
signal branch_neg: STD_LOGIC; -- branch negative offset flag
|
107 |
|
|
|
108 |
|
|
-- bus
|
109 |
|
|
signal dbin: STD_LOGIC_VECTOR(7 downto 0); -- input data bus D0..D7
|
110 |
|
|
signal dbout: STD_LOGIC_VECTOR(7 downto 0); -- output data bus D0..D7
|
111 |
|
|
signal a_bus: STD_LOGIC_VECTOR(7 downto 0); -- accumulator register A bus
|
112 |
|
|
signal x_bus: STD_LOGIC_VECTOR(7 downto 0); -- index register X bus
|
113 |
|
|
signal y_bus: STD_LOGIC_VECTOR(7 downto 0); -- index register Y bus
|
114 |
|
|
signal z_bus: STD_LOGIC_VECTOR(7 downto 0); -- index register Z bus
|
115 |
|
|
signal sp_bus: STD_LOGIC_VECTOR(15 downto 0); -- stack pointer register S bus
|
116 |
|
|
signal p_bus: STD_LOGIC_VECTOR(7 downto 0); -- status register P bus
|
117 |
|
|
signal op_bus: STD_LOGIC_VECTOR(7 downto 0); -- opcode register bus
|
118 |
|
|
signal o_bus: STD_LOGIC_VECTOR(7 downto 0); -- operand register bus
|
119 |
|
|
signal bcd_bus: STD_LOGIC_VECTOR(7 downto 0); -- bcd constants bus (used for decimal adjustement)
|
120 |
|
|
signal oper_bus: STD_LOGIC_VECTOR(7 downto 0); -- operand bus (ALU operand #2 bus)
|
121 |
|
|
signal r_bus: STD_LOGIC_VECTOR(7 downto 0); -- general register bus (ALU operand #2 bus)
|
122 |
|
|
signal alu_bus: STD_LOGIC_VECTOR(7 downto 0); -- ALU output bus
|
123 |
|
|
signal pc_bus: STD_LOGIC_VECTOR(15 downto 0); -- program counter register PC bus
|
124 |
|
|
signal mp_bus: STD_LOGIC_VECTOR(15 downto 0); -- memory pointer register PC bus
|
125 |
|
|
signal ad_bus: STD_LOGIC_VECTOR(15 downto 0); -- address bus
|
126 |
|
|
|
127 |
|
|
-- 16 bit program counter register (PC)
|
128 |
|
|
component pcr
|
129 |
|
|
port( clk: in STD_LOGIC; -- clock
|
130 |
|
|
i: in STD_LOGIC; -- increment
|
131 |
|
|
fwait: in STD_LOGIC; -- wait
|
132 |
|
|
fc: in STD_LOGIC_VECTOR(3 downto 0); -- function code
|
133 |
|
|
din1: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
134 |
|
|
din2: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
135 |
|
|
dout: out STD_LOGIC_VECTOR(15 downto 0) -- output
|
136 |
|
|
);
|
137 |
|
|
end component;
|
138 |
|
|
|
139 |
|
|
-- 16 bit memory pointer register (MP)
|
140 |
|
|
component mpr
|
141 |
|
|
port( clk: in STD_LOGIC; -- clock
|
142 |
|
|
fwait: in STD_LOGIC; -- wait
|
143 |
|
|
c: in STD_LOGIC; -- carry input
|
144 |
|
|
fc: in STD_LOGIC_VECTOR(3 downto 0); -- function code
|
145 |
|
|
din_l: in STD_LOGIC_VECTOR(7 downto 0); -- input LSB
|
146 |
|
|
din_h: in STD_LOGIC_VECTOR(7 downto 0); -- input MSB
|
147 |
|
|
zp: in STD_LOGIC_VECTOR(7 downto 0); -- input zero bage register Z
|
148 |
|
|
v: in STD_LOGIC_VECTOR(2 downto 0); -- vector offset input
|
149 |
|
|
dout: out STD_LOGIC_VECTOR(15 downto 0) -- output
|
150 |
|
|
);
|
151 |
|
|
end component;
|
152 |
|
|
|
153 |
|
|
-- 8 bit opcode register opr (pipeline opcode prefetch register)
|
154 |
|
|
component opr
|
155 |
|
|
port( clk: in STD_LOGIC; -- clock
|
156 |
|
|
clr: in STD_LOGIC; -- force BRK opcode
|
157 |
|
|
fwait: in STD_LOGIC; -- wait
|
158 |
|
|
ld: in STD_LOGIC; -- load
|
159 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
160 |
|
|
b: out STD_LOGIC; -- BRK opcode
|
161 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0) -- output
|
162 |
|
|
);
|
163 |
|
|
end component;
|
164 |
|
|
|
165 |
|
|
-- 8 bit operand hold register oper
|
166 |
|
|
component oper
|
167 |
|
|
port( clk: in STD_LOGIC; -- clock
|
168 |
|
|
fwait: in STD_LOGIC; -- wait
|
169 |
|
|
ld: in STD_LOGIC; -- load
|
170 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
171 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0) -- output
|
172 |
|
|
);
|
173 |
|
|
end component;
|
174 |
|
|
|
175 |
|
|
-- 8 bit accumulator register A
|
176 |
|
|
component ar
|
177 |
|
|
port( clk: in STD_LOGIC; -- clock
|
178 |
|
|
fwait: in STD_LOGIC;
|
179 |
|
|
ld: in STD_LOGIC; -- load
|
180 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
181 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0) -- output
|
182 |
|
|
);
|
183 |
|
|
end component;
|
184 |
|
|
|
185 |
|
|
-- 8 bit index register X
|
186 |
|
|
component xr
|
187 |
|
|
port( clk: in STD_LOGIC; -- clock
|
188 |
|
|
fwait: in STD_LOGIC;
|
189 |
|
|
ld: in STD_LOGIC; -- load
|
190 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
191 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0) -- output
|
192 |
|
|
);
|
193 |
|
|
end component;
|
194 |
|
|
|
195 |
|
|
-- 8 bit index register Y
|
196 |
|
|
component yr
|
197 |
|
|
port( clk: in STD_LOGIC; -- clock
|
198 |
|
|
fwait: in STD_LOGIC;
|
199 |
|
|
ld: in STD_LOGIC; -- load
|
200 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
201 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0) -- output
|
202 |
|
|
);
|
203 |
|
|
end component;
|
204 |
|
|
|
205 |
|
|
-- 8 bit zero page register Z
|
206 |
|
|
-- cleared by any interrupts
|
207 |
|
|
component zr
|
208 |
|
|
port( clk: in STD_LOGIC; -- clock
|
209 |
|
|
clr: in STD_LOGIC; -- reset
|
210 |
|
|
fwait: in STD_LOGIC;
|
211 |
|
|
ld: in STD_LOGIC; -- load
|
212 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
213 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0) -- output
|
214 |
|
|
);
|
215 |
|
|
end component;
|
216 |
|
|
|
217 |
|
|
-- 16 bit stack pointer SP
|
218 |
|
|
component spr
|
219 |
|
|
port( clk: in STD_LOGIC; -- clock
|
220 |
|
|
fwait: in STD_LOGIC; -- wait
|
221 |
|
|
clr: in STD_LOGIC; -- load init value
|
222 |
|
|
ld_l: in STD_LOGIC; -- load lsb
|
223 |
|
|
ld_h: in STD_LOGIC; -- load msb
|
224 |
|
|
u: in STD_LOGIC; -- increment
|
225 |
|
|
d: in STD_LOGIC; -- decrement
|
226 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
227 |
|
|
dout: out STD_LOGIC_VECTOR(15 downto 0) -- output
|
228 |
|
|
);
|
229 |
|
|
end component;
|
230 |
|
|
|
231 |
|
|
-- 8 bit processor status register P
|
232 |
|
|
-- NV1BDIZC
|
233 |
|
|
-- 76543210
|
234 |
|
|
-- ||||||||
|
235 |
|
|
-- ||||||||--- C = carry/borrow flag
|
236 |
|
|
-- |||||||---- Z = zero flag
|
237 |
|
|
-- ||||||----- I = interrupt mask
|
238 |
|
|
-- |||||------ D = decimal/binary alu mode
|
239 |
|
|
-- ||||------- B = break opcode flag
|
240 |
|
|
-- |||-------- 1 = always "1'
|
241 |
|
|
-- ||--------- V = overflow flag
|
242 |
|
|
-- |---------- N = negative flag
|
243 |
|
|
-- The P register also contains an additional carry/borrow flag (ACR) used for effective address calculation but
|
244 |
|
|
-- it is not visible at program level
|
245 |
|
|
component pr
|
246 |
|
|
port( clk: in STD_LOGIC; -- clock
|
247 |
|
|
clr: in STD_LOGIC; -- clear
|
248 |
|
|
fwait: in STD_LOGIC; -- wait
|
249 |
|
|
n: in STD_LOGIC; -- N input
|
250 |
|
|
v: in STD_LOGIC; -- V input
|
251 |
|
|
z: in STD_LOGIC; -- Z input
|
252 |
|
|
c: in STD_LOGIC; -- C input
|
253 |
|
|
b: in STD_LOGIC; -- B input
|
254 |
|
|
sv: in STD_LOGIC; -- set overflow
|
255 |
|
|
acr_in: in STD_LOGIC; -- auxiliary carry in
|
256 |
|
|
fc: in STD_LOGIC_VECTOR(3 downto 0); -- function code
|
257 |
|
|
din: in STD_LOGIC_VECTOR(7 downto 0); -- input
|
258 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0); -- output
|
259 |
|
|
acr_out: out STD_LOGIC -- auxiliary carry out
|
260 |
|
|
);
|
261 |
|
|
end component;
|
262 |
|
|
|
263 |
|
|
-- BCD register (used for decimal adjustement)
|
264 |
|
|
component bcd_reg is
|
265 |
|
|
port( clk: in STD_LOGIC;
|
266 |
|
|
clr: in STD_LOGIC;
|
267 |
|
|
fwait: in STD_LOGIC;
|
268 |
|
|
en: in STD_LOGIC;
|
269 |
|
|
bcd_sl: in STD_LOGIC; -- loads "6" to lsb
|
270 |
|
|
bcd_sh: in STD_LOGIC; -- loads "6" to msb
|
271 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0)
|
272 |
|
|
);
|
273 |
|
|
end component;
|
274 |
|
|
|
275 |
|
|
-- 8 bit (binary/bcd) two-way through pass ALU
|
276 |
|
|
-- operation:
|
277 |
|
|
-- aluop = "0000" => dout <= op1 (pass/test)
|
278 |
|
|
-- aluop = "0001" => dout <= op1 + op2 + carry
|
279 |
|
|
-- aluop = "0010" => dout <= op1 - op2 - carry
|
280 |
|
|
-- aluop = "0011" => dout <= op1 and op2
|
281 |
|
|
-- aluop = "0100" => dout <= op1 or op2
|
282 |
|
|
-- aluop = "0101" => dout <= op1 xor op2
|
283 |
|
|
-- aluop = "0110" => dout <= op1 + 1
|
284 |
|
|
-- aluop = "0111" => dout <= op1 - 1
|
285 |
|
|
-- aluop = "1000" => dout <= op1 << 1 (ASL)
|
286 |
|
|
-- aluop = "1001" => dout <= op1 >> 1 (LSR)
|
287 |
|
|
-- aluop = "1010" => dout <= op1 << 1 (ROL)
|
288 |
|
|
-- aluop = "1011" => dout <= op1 >> 1 (ROR)
|
289 |
|
|
component alu_bin
|
290 |
|
|
port( alu_byp: in STD_LOGIC; -- ALU bypass (no operation)
|
291 |
|
|
cin: in STD_LOGIC; -- carry/borrow in
|
292 |
|
|
vin: in STD_LOGIC; -- overflow in
|
293 |
|
|
op1: in STD_LOGIC_VECTOR(7 downto 0); -- 8 bit operand #1
|
294 |
|
|
op2: in STD_LOGIC_VECTOR(7 downto 0); -- 8 bit operand #2
|
295 |
|
|
fc: in STD_LOGIC_VECTOR(5 downto 0); -- function code
|
296 |
|
|
cf: out STD_LOGIC; -- carry/borrow (byte) out
|
297 |
|
|
zf: out STD_LOGIC; -- zero flag out
|
298 |
|
|
nf: out STD_LOGIC; -- negative flag out
|
299 |
|
|
vf: out STD_LOGIC; -- overflow flag out
|
300 |
|
|
pc_cf: out STD_LOGIC; -- carry/borrow out for PC operation
|
301 |
|
|
bcd_ol: out STD_LOGIC; -- bcd lsb overflow
|
302 |
|
|
bcd_oh: out STD_LOGIC; -- bcd msb overflow
|
303 |
|
|
dout: out STD_LOGIC_VECTOR(7 downto 0) -- 8 bit result out
|
304 |
|
|
);
|
305 |
|
|
end component;
|
306 |
|
|
|
307 |
|
|
-- PC/MP address multiplexer
|
308 |
|
|
component addrmux
|
309 |
|
|
port( sel: in STD_LOGIC_VECTOR(1 downto 0);
|
310 |
|
|
a: in STD_LOGIC_VECTOR(15 downto 0);
|
311 |
|
|
b: in STD_LOGIC_VECTOR(15 downto 0);
|
312 |
|
|
s: in STD_LOGIC_VECTOR(15 downto 0);
|
313 |
|
|
y: out STD_LOGIC_VECTOR(15 downto 0)
|
314 |
|
|
);
|
315 |
|
|
end component;
|
316 |
|
|
|
317 |
|
|
-- register multiplexer
|
318 |
|
|
component regmux
|
319 |
|
|
port( sel: in STD_LOGIC_VECTOR(3 downto 0);
|
320 |
|
|
a: in STD_LOGIC_VECTOR(7 downto 0);
|
321 |
|
|
b: in STD_LOGIC_VECTOR(7 downto 0);
|
322 |
|
|
c: in STD_LOGIC_VECTOR(7 downto 0);
|
323 |
|
|
d: in STD_LOGIC_VECTOR(7 downto 0);
|
324 |
|
|
e: in STD_LOGIC_VECTOR(7 downto 0);
|
325 |
|
|
f: in STD_LOGIC_VECTOR(7 downto 0);
|
326 |
|
|
g: in STD_LOGIC_VECTOR(7 downto 0);
|
327 |
|
|
h: in STD_LOGIC_VECTOR(7 downto 0);
|
328 |
|
|
i: in STD_LOGIC_VECTOR(7 downto 0);
|
329 |
|
|
j: in STD_LOGIC_VECTOR(7 downto 0);
|
330 |
|
|
k: in STD_LOGIC_VECTOR(7 downto 0);
|
331 |
|
|
y: out STD_LOGIC_VECTOR(7 downto 0)
|
332 |
|
|
);
|
333 |
|
|
end component;
|
334 |
|
|
|
335 |
|
|
-- data multiplexer (register "O" bypass)
|
336 |
|
|
component dmux is
|
337 |
|
|
port( sel: in STD_LOGIC_VECTOR(1 downto 0);
|
338 |
|
|
a: in STD_LOGIC_VECTOR(7 downto 0);
|
339 |
|
|
b: in STD_LOGIC_VECTOR(7 downto 0);
|
340 |
|
|
c: in STD_LOGIC_VECTOR(7 downto 0);
|
341 |
|
|
y: out STD_LOGIC_VECTOR(7 downto 0)
|
342 |
|
|
);
|
343 |
|
|
end component dmux;
|
344 |
|
|
|
345 |
|
|
-- microcode sequencer logic
|
346 |
|
|
component mcseq
|
347 |
|
|
port( clk: in STD_LOGIC;
|
348 |
|
|
clr: in STD_LOGIC;
|
349 |
|
|
mc_nop: in STD_LOGIC;
|
350 |
|
|
fwait: in STD_LOGIC;
|
351 |
|
|
q: out STD_LOGIC_VECTOR(2 downto 0)
|
352 |
|
|
);
|
353 |
|
|
end component;
|
354 |
|
|
|
355 |
|
|
-- micropla logic
|
356 |
|
|
-- output fields format:
|
357 |
|
|
component mcpla
|
358 |
|
|
port( a: in STD_LOGIC_VECTOR(10 downto 0);
|
359 |
|
|
q: out STD_LOGIC_VECTOR(34 downto 0)
|
360 |
|
|
);
|
361 |
|
|
end component;
|
362 |
|
|
|
363 |
|
|
-- register operation decoding logic
|
364 |
|
|
component decreg
|
365 |
|
|
port( r: in STD_LOGIC_VECTOR(3 downto 0);
|
366 |
|
|
y: out STD_LOGIC_VECTOR(8 downto 0)
|
367 |
|
|
);
|
368 |
|
|
end component;
|
369 |
|
|
|
370 |
|
|
-- cpu main state machine
|
371 |
|
|
component cpufsm
|
372 |
|
|
port( clk: in STD_LOGIC;
|
373 |
|
|
clr: in STD_LOGIC;
|
374 |
|
|
fwait: in STD_LOGIC;
|
375 |
|
|
ireq: in STD_LOGIC;
|
376 |
|
|
branch: in STD_LOGIC;
|
377 |
|
|
bflag: in STD_LOGIC;
|
378 |
|
|
aim: in STD_LOGIC;
|
379 |
|
|
bcarry: in STD_LOGIC;
|
380 |
|
|
icarry: in STD_LOGIC;
|
381 |
|
|
p1: in STD_LOGIC_VECTOR(1 downto 0);
|
382 |
|
|
e_ei: in STD_LOGIC;
|
383 |
|
|
mc_ei: in STD_LOGIC;
|
384 |
|
|
addsub: in STD_LOGIC;
|
385 |
|
|
dec_mode: in STD_LOGIC;
|
386 |
|
|
fetch: out STD_LOGIC;
|
387 |
|
|
op_sync: out STD_LOGIC;
|
388 |
|
|
pci: out STD_LOGIC;
|
389 |
|
|
pq: out STD_LOGIC_VECTOR(1 downto 0);
|
390 |
|
|
fb: out STD_LOGIC;
|
391 |
|
|
od: out STD_LOGIC;
|
392 |
|
|
mc_nop: out STD_LOGIC
|
393 |
|
|
);
|
394 |
|
|
end component;
|
395 |
|
|
|
396 |
|
|
-- interrupt logic
|
397 |
|
|
component intlog
|
398 |
|
|
port( clk: in STD_LOGIC;
|
399 |
|
|
iack: in STD_LOGIC; -- interrupt acknowledge by microcode
|
400 |
|
|
r: in STD_LOGIC; -- RESET request
|
401 |
|
|
n: in STD_LOGIC; -- NMI request
|
402 |
|
|
i: in STD_LOGIC; -- IRQ request
|
403 |
|
|
b: in STD_LOGIC; -- BRK opcode
|
404 |
|
|
s: in STD_LOGIC; -- SO
|
405 |
|
|
imask: in STD_LOGIC; -- interrupt mask (valid only for IRQ)
|
406 |
|
|
ioffs: in STD_LOGIC_VECTOR(1 downto 0); -- interrupt servicing offset
|
407 |
|
|
ireq: out STD_LOGIC; -- global interrupt requestb (IRQ/NMI)
|
408 |
|
|
vset: out STD_LOGIC; -- SO output
|
409 |
|
|
voffs: out STD_LOGIC_VECTOR(2 downto 0) -- interrupt vector offset
|
410 |
|
|
);
|
411 |
|
|
end component;
|
412 |
|
|
|
413 |
|
|
-- branch logic
|
414 |
|
|
component branch
|
415 |
|
|
port( op: in STD_LOGIC_VECTOR(3 downto 0);
|
416 |
|
|
n: in STD_LOGIC;
|
417 |
|
|
v: in STD_LOGIC;
|
418 |
|
|
z: in STD_LOGIC;
|
419 |
|
|
c: in STD_LOGIC;
|
420 |
|
|
bres: out STD_LOGIC
|
421 |
|
|
);
|
422 |
|
|
end component;
|
423 |
|
|
|
424 |
|
|
-- opcode decimal instructions and prefetch prediction logic
|
425 |
|
|
component pre_dec
|
426 |
|
|
port( op: in STD_LOGIC_VECTOR(7 downto 0);
|
427 |
|
|
fetch: in STD_LOGIC;
|
428 |
|
|
ei: out STD_LOGIC;
|
429 |
|
|
dec: out STD_LOGIC
|
430 |
|
|
);
|
431 |
|
|
end component;
|
432 |
|
|
|
433 |
|
|
begin
|
434 |
|
|
u1:pcr port map(clk=>clk0,
|
435 |
|
|
i=>pcinc,
|
436 |
|
|
fwait=>i_rdy,
|
437 |
|
|
fc(3 downto 1)=>pcr_fc,
|
438 |
|
|
fc(0)=>o_bus(7),
|
439 |
|
|
din1=>alu_bus,
|
440 |
|
|
din2=>o_bus,
|
441 |
|
|
dout=>pc_bus
|
442 |
|
|
);
|
443 |
|
|
|
444 |
|
|
u2:mpr port map(clk=>clk0,
|
445 |
|
|
fwait=>i_rdy,
|
446 |
|
|
c=>acr_reg,
|
447 |
|
|
fc=>mpr_fc,
|
448 |
|
|
din_l=>alu_bus,
|
449 |
|
|
din_h=>dbin,
|
450 |
|
|
zp=>z_bus,
|
451 |
|
|
v=>ivoffs,
|
452 |
|
|
dout=>mp_bus
|
453 |
|
|
);
|
454 |
|
|
|
455 |
|
|
u3:ar port map(clk=>clk0,
|
456 |
|
|
fwait=>i_rdy,
|
457 |
|
|
ld=>a_l,
|
458 |
|
|
din=>alu_bus,
|
459 |
|
|
dout=>a_bus
|
460 |
|
|
);
|
461 |
|
|
|
462 |
|
|
u4:xr port map(clk=>clk0,
|
463 |
|
|
fwait=>i_rdy,
|
464 |
|
|
ld=>x_l,
|
465 |
|
|
din=>alu_bus,
|
466 |
|
|
dout=>x_bus
|
467 |
|
|
);
|
468 |
|
|
|
469 |
|
|
u5:yr port map(clk=>clk0,
|
470 |
|
|
fwait=>i_rdy,
|
471 |
|
|
ld=>y_l,
|
472 |
|
|
din=>alu_bus,
|
473 |
|
|
dout=>y_bus
|
474 |
|
|
);
|
475 |
|
|
|
476 |
|
|
u6:zr port map(clk=>clk0,
|
477 |
|
|
clr=>clri,
|
478 |
|
|
fwait=>i_rdy,
|
479 |
|
|
ld=>z_l,
|
480 |
|
|
din=>alu_bus,
|
481 |
|
|
dout=>z_bus
|
482 |
|
|
);
|
483 |
|
|
|
484 |
|
|
u7:spr port map(clk=>clk0,
|
485 |
|
|
clr=>i_res,
|
486 |
|
|
fwait=>i_rdy,
|
487 |
|
|
ld_l=>sp_ll,
|
488 |
|
|
ld_h=>sp_lh,
|
489 |
|
|
u=>sp_u,
|
490 |
|
|
d=>sp_d,
|
491 |
|
|
din=>alu_bus,
|
492 |
|
|
dout=>sp_bus
|
493 |
|
|
);
|
494 |
|
|
|
495 |
|
|
u8:pr port map(clk=>clk0,
|
496 |
|
|
clr=>i_res,
|
497 |
|
|
fwait=>i_rdy,
|
498 |
|
|
n=>n_flg,
|
499 |
|
|
v=>v_flg,
|
500 |
|
|
z=>z_flg,
|
501 |
|
|
c=>c_flg,
|
502 |
|
|
b=>opbrk,
|
503 |
|
|
sv=>vso,
|
504 |
|
|
acr_in=>pc_c_alu_flg,
|
505 |
|
|
fc=>p_op,
|
506 |
|
|
din=>dbin,
|
507 |
|
|
dout=>p_bus,
|
508 |
|
|
acr_out=>acr_reg
|
509 |
|
|
);
|
510 |
|
|
|
511 |
|
|
u9:opr port map(clk=>clk0,
|
512 |
|
|
clr=>fbrk,
|
513 |
|
|
fwait=>i_rdy,
|
514 |
|
|
ld=>opfetch,
|
515 |
|
|
din=>dbin,
|
516 |
|
|
b=>opbrk,
|
517 |
|
|
dout=>op_bus
|
518 |
|
|
);
|
519 |
|
|
|
520 |
|
|
u10:oper port map(clk=>clk0,
|
521 |
|
|
fwait=>i_rdy,
|
522 |
|
|
ld=>o_l,
|
523 |
|
|
din=>alu_bus,
|
524 |
|
|
dout=>o_bus
|
525 |
|
|
);
|
526 |
|
|
|
527 |
|
|
u11:bcd_reg port map(clk=>clk0,
|
528 |
|
|
clr=>opfetch,
|
529 |
|
|
fwait=>i_rdy,
|
530 |
|
|
en=>bcd,
|
531 |
|
|
bcd_sl=>bcd_lsb,
|
532 |
|
|
bcd_sh=>bcd_msb,
|
533 |
|
|
dout=>bcd_bus
|
534 |
|
|
);
|
535 |
|
|
|
536 |
|
|
u12:alu_bin port map(alu_byp=>acr_reg,
|
537 |
|
|
cin=>p_bus(0),
|
538 |
|
|
vin=>p_bus(6),
|
539 |
|
|
op1=>r_bus,
|
540 |
|
|
op2=>oper_bus,
|
541 |
|
|
fc(5 downto 1)=>aluop,
|
542 |
|
|
fc(0)=>branch_neg,
|
543 |
|
|
cf=>c_flg,
|
544 |
|
|
zf=>z_flg,
|
545 |
|
|
nf=>n_flg,
|
546 |
|
|
vf=>v_flg,
|
547 |
|
|
pc_cf=>pc_c_alu_flg,
|
548 |
|
|
bcd_ol=>bcd_lsb,
|
549 |
|
|
bcd_oh=>bcd_msb,
|
550 |
|
|
dout=>alu_bus
|
551 |
|
|
);
|
552 |
|
|
|
553 |
|
|
u13:addrmux port map(sel=>pcmp,
|
554 |
|
|
a=>pc_bus,
|
555 |
|
|
b=>mp_bus,
|
556 |
|
|
s=>sp_bus,
|
557 |
|
|
y=>addr
|
558 |
|
|
);
|
559 |
|
|
|
560 |
|
|
u14:regmux port map(sel=>rsel,
|
561 |
|
|
a=>dbin,
|
562 |
|
|
b=>a_bus,
|
563 |
|
|
c=>x_bus,
|
564 |
|
|
d=>y_bus,
|
565 |
|
|
e=>sp_bus(7 downto 0),
|
566 |
|
|
f=>sp_bus(15 downto 8),
|
567 |
|
|
g=>p_bus,
|
568 |
|
|
h=>pc_bus(7 downto 0),
|
569 |
|
|
i=>pc_bus(15 downto 8),
|
570 |
|
|
j=>o_bus,
|
571 |
|
|
k=>z_bus,
|
572 |
|
|
y=>r_bus
|
573 |
|
|
);
|
574 |
|
|
|
575 |
|
|
u15:dmux port map(sel=>dmux_sel,
|
576 |
|
|
a=>o_bus,
|
577 |
|
|
b=>dbin,
|
578 |
|
|
c=>bcd_bus,
|
579 |
|
|
y=>oper_bus
|
580 |
|
|
);
|
581 |
|
|
|
582 |
|
|
u16:mcseq port map(clk=>clk0,
|
583 |
|
|
clr=>opdec,
|
584 |
|
|
mc_nop=>mcn,
|
585 |
|
|
fwait=>i_rdy,
|
586 |
|
|
q=>mcscan
|
587 |
|
|
);
|
588 |
|
|
|
589 |
|
|
u17:mcpla port map(a=>mcad,
|
590 |
|
|
q=>mcbit
|
591 |
|
|
);
|
592 |
|
|
|
593 |
|
|
u18:decreg port map(r=>regop,
|
594 |
|
|
y=>regbit
|
595 |
|
|
);
|
596 |
|
|
|
597 |
|
|
u19:cpufsm port map(clk=>clk0,
|
598 |
|
|
clr=>i_res,
|
599 |
|
|
fwait=>i_rdy,
|
600 |
|
|
ireq=>int,
|
601 |
|
|
branch=>mc_branch,
|
602 |
|
|
bflag=>bcf,
|
603 |
|
|
aim=>ai_op,
|
604 |
|
|
bcarry=>pcc,
|
605 |
|
|
icarry=>acr_reg,
|
606 |
|
|
p1=>pcmp_mc,
|
607 |
|
|
e_ei=>e_eop,
|
608 |
|
|
mc_ei=>mc_eop,
|
609 |
|
|
addsub=>add_sub_op,
|
610 |
|
|
dec_mode=>p_bus(3),
|
611 |
|
|
fetch=>opfetch,
|
612 |
|
|
op_sync=>i_sync,
|
613 |
|
|
pci=>pcinc,
|
614 |
|
|
pq=>pcmp,
|
615 |
|
|
fb=>fbrk,
|
616 |
|
|
od=>opdec,
|
617 |
|
|
mc_nop=>mcn
|
618 |
|
|
);
|
619 |
|
|
|
620 |
|
|
u20:intlog port map(clk=>clk0,
|
621 |
|
|
iack=>clri,
|
622 |
|
|
r=>i_res,
|
623 |
|
|
n=>i_nmi,
|
624 |
|
|
i=>i_irq,
|
625 |
|
|
b=>opbrk,
|
626 |
|
|
s=>i_so,
|
627 |
|
|
imask=>ien,
|
628 |
|
|
ioffs=>mp_bus(2 downto 1),
|
629 |
|
|
ireq=>int,
|
630 |
|
|
vset=>vso,
|
631 |
|
|
voffs=>ivoffs
|
632 |
|
|
);
|
633 |
|
|
|
634 |
|
|
u21:branch port map(op=>op_bus(7 downto 4),
|
635 |
|
|
n=>p_bus(7),
|
636 |
|
|
v=>p_bus(6),
|
637 |
|
|
z=>p_bus(1),
|
638 |
|
|
c=>p_bus(0),
|
639 |
|
|
bres=>bcf
|
640 |
|
|
);
|
641 |
|
|
|
642 |
|
|
u22:pre_dec port map(op=>dbin,
|
643 |
|
|
fetch=>opfetch,
|
644 |
|
|
ei=>e_eop,
|
645 |
|
|
dec=>add_sub_op
|
646 |
|
|
);
|
647 |
|
|
|
648 |
|
|
-- asynchronous CPU link section
|
649 |
|
|
ien <= p_bus(2); -- P(I) flag
|
650 |
|
|
bcd <= p_bus(3); -- P(D) flag
|
651 |
|
|
i_res <= not res; -- internal reset
|
652 |
|
|
i_nmi <= not nmi; -- internal NMI
|
653 |
|
|
i_irq <= not irq; -- internal IRQ
|
654 |
|
|
i_rdy <= not rdy; -- internal RDY
|
655 |
|
|
i_so <= not so; -- internal SO
|
656 |
|
|
mcad <= op_bus & mcscan; -- microcode address
|
657 |
|
|
rsel <= mcbit(3 downto 0); -- registers read microcode
|
658 |
|
|
regop <= mcbit(7 downto 4); -- registers operation microcode
|
659 |
|
|
aluop <= mcbit(12 downto 8); -- ALU microcode
|
660 |
|
|
p_op <= mcbit(16 downto 13); -- register P microcode
|
661 |
|
|
mpr_fc <= mcbit(20 downto 17); -- MPR microcode
|
662 |
|
|
pcr_fc <= mcbit(23 downto 21); -- PCR microcode
|
663 |
|
|
pcmp_mc <= mcbit(25 downto 24); -- PCR/MPR multiplexer microcode
|
664 |
|
|
clri <= mcbit(26); -- clear interrupt request (also serves as register Z clear)
|
665 |
|
|
we_mc <= mcbit(27); -- write enable (combinatorial) microcode
|
666 |
|
|
we_mc_l <= mcbit(28); -- write enable (latched) microcode
|
667 |
|
|
mc_eop <= mcbit(29); -- end of instruction reached
|
668 |
|
|
mc_branch <= mcbit(30); -- branch opcode
|
669 |
|
|
i_vp <= mcbit(31); -- vector pull
|
670 |
|
|
ai_op <= mcbit(32); -- opcode with addressing indexed microcode
|
671 |
|
|
dmux_sel <= mcbit(34 downto 33); -- data multiplexer microcode
|
672 |
|
|
ope <= eop;
|
673 |
|
|
eop <= '1' when mc_eop = '1' or e_eop = '1' else '0';
|
674 |
|
|
branch_neg <= '0' when mc_branch = '0' else o_bus(7); -- flag for branch negative offset is valid only with branches opcode
|
675 |
|
|
vp <= not i_vp;
|
676 |
|
|
|
677 |
|
|
-- register operations
|
678 |
|
|
a_l <= regbit(0); -- A load
|
679 |
|
|
x_l <= regbit(1); -- X load
|
680 |
|
|
y_l <= regbit(2); -- Y load
|
681 |
|
|
z_l <= regbit(3); -- Z load
|
682 |
|
|
o_l <= regbit(4); -- O load
|
683 |
|
|
sp_ll <= regbit(5); -- S load lsb
|
684 |
|
|
sp_lh <= regbit(6); -- S load msb
|
685 |
|
|
sp_u <= regbit(7); -- S += 1
|
686 |
|
|
sp_d <= regbit(8); -- S -= 1
|
687 |
|
|
we <= we_mc or opfetch; -- write enable
|
688 |
|
|
sync <= m_sync;
|
689 |
|
|
|
690 |
|
|
-- SYNC latched
|
691 |
|
|
process(clk0)
|
692 |
|
|
begin
|
693 |
|
|
if (clk0'event and clk0 = '1') then
|
694 |
|
|
if i_rdy = '0' then
|
695 |
|
|
m_sync <= i_sync;
|
696 |
|
|
else
|
697 |
|
|
m_sync <= m_sync;
|
698 |
|
|
end if;
|
699 |
|
|
end if;
|
700 |
|
|
end process;
|
701 |
|
|
|
702 |
|
|
-- PC carry logic
|
703 |
|
|
process(o_bus,pc_c_alu_flg)
|
704 |
|
|
begin
|
705 |
|
|
if o_bus(7) = '0' then -- check for positive/negative branch offset (bit 7)
|
706 |
|
|
pcc <= pc_c_alu_flg;
|
707 |
|
|
else
|
708 |
|
|
pcc <= not pc_c_alu_flg;
|
709 |
|
|
end if;
|
710 |
|
|
end process;
|
711 |
|
|
|
712 |
|
|
-- write enable registered
|
713 |
|
|
process(clk0)
|
714 |
|
|
begin
|
715 |
|
|
if (clk0'event and clk0 = '1') then
|
716 |
|
|
if i_res = '1' then
|
717 |
|
|
we_r <= '1';
|
718 |
|
|
else
|
719 |
|
|
if i_rdy = '0' then
|
720 |
|
|
we_r <= we_mc_l;
|
721 |
|
|
else
|
722 |
|
|
we_r <= we_r;
|
723 |
|
|
end if;
|
724 |
|
|
end if;
|
725 |
|
|
end if;
|
726 |
|
|
end process;
|
727 |
|
|
|
728 |
|
|
rw <= we and we_r;
|
729 |
|
|
|
730 |
|
|
-- data bus tristate (buffer ring gated) control logic
|
731 |
|
|
--process(clk0,we,we_r,alu_bus)
|
732 |
|
|
--begin
|
733 |
|
|
-- if clock = '0' and (we = '0' or we_r = '0') then
|
734 |
|
|
-- data <= alu_bus;
|
735 |
|
|
-- else
|
736 |
|
|
-- data <= "ZZZZZZZZ";
|
737 |
|
|
-- end if;
|
738 |
|
|
--end process;
|
739 |
|
|
data_out <= alu_bus;
|
740 |
|
|
dbin <= data_in or "00000000";
|
741 |
|
|
|
742 |
|
|
end struct;
|
743 |
|
|
|
744 |
|
|
|
745 |
|
|
|