2 use ieee.std_logic_1164.all;
\r
5 generic ( dsize : integer := 8;
\r
9 signal dbg_instruction : out std_logic_vector(7 downto 0);
\r
10 signal dbg_int_d_bus : out std_logic_vector(7 downto 0);
\r
11 signal dbg_exec_cycle : out std_logic_vector (5 downto 0);
\r
12 signal dbg_ea_carry : out std_logic;
\r
14 -- signal dbg_index_bus : out std_logic_vector(7 downto 0);
\r
15 -- signal dbg_acc_bus : out std_logic_vector(7 downto 0);
\r
16 signal dbg_status : out std_logic_vector(7 downto 0);
\r
17 signal dbg_pcl, dbg_pch, dbg_sp, dbg_x, dbg_y, dbg_acc : out std_logic_vector(7 downto 0);
\r
18 signal dbg_dec_oe_n : out std_logic;
\r
19 signal dbg_dec_val : out std_logic_vector (7 downto 0);
\r
20 signal dbg_int_dbus : out std_logic_vector (7 downto 0);
\r
21 -- signal dbg_status_val : out std_logic_vector (7 downto 0);
\r
22 signal dbg_stat_we_n : out std_logic;
\r
23 signal dbg_idl_h, dbg_idl_l, dbg_dbb_r, dbg_dbb_w : out std_logic_vector (7 downto 0);
\r
25 input_clk : in std_logic; --phi0 input pin.
\r
27 rst_n : in std_logic;
\r
28 irq_n : in std_logic;
\r
29 nmi_n : in std_logic;
\r
31 r_nw : out std_logic;
\r
32 phi1 : out std_logic;
\r
33 phi2 : out std_logic;
\r
34 addr : out std_logic_vector ( asize - 1 downto 0);
\r
35 d_io : inout std_logic_vector ( dsize - 1 downto 0)
\r
39 architecture rtl of mos6502 is
\r
44 phi2 <= not input_clk;
\r
47 set_ppu_p : process (input_clk, rst_n)
\r
48 use ieee.std_logic_arith.conv_std_logic_vector;
\r
50 variable init_step_cnt, plt_step_cnt,
\r
51 nt_step_cnt, spr_step_cnt, dma_step_cnt, enable_ppu_step_cnt : integer;
\r
52 variable init_done : std_logic;
\r
53 variable global_step_cnt : integer;
\r
54 variable cpu_cnt : integer;
\r
55 constant cpu_io_multi : integer := 3; --io happens every 4 cpu cycle.
\r
56 variable i, j : integer;
\r
57 variable ch : integer := 16#41# ;
\r
59 procedure io_out (ad: in integer; dt : in integer) is
\r
62 addr <= conv_std_logic_vector(ad, 16);
\r
63 d_io <= conv_std_logic_vector(dt, 8);
\r
67 addr <= (others => 'Z');
\r
68 d_io <= (others => 'Z');
\r
73 if (rst_n = '0') then
\r
76 addr <= (others => 'Z');
\r
77 d_io <= (others => 'Z');
\r
80 global_step_cnt := 0;
\r
85 enable_ppu_step_cnt := 0;
\r
88 elsif (rising_edge(input_clk)) then
\r
89 cpu_cnt := cpu_cnt + 1;
\r
91 if (init_done = '0') then
\r
92 if (global_step_cnt = 0) then
\r
93 --step0.0 = init ppu.
\r
94 if (init_step_cnt = 0 * cpu_io_multi) then
\r
96 io_out(16#2000#, 16#00#);
\r
97 elsif (init_step_cnt = 1 * cpu_io_multi) then
\r
99 io_out(16#2001#, 16#00#);
\r
102 if (init_step_cnt > 2 * cpu_io_multi) then
\r
103 global_step_cnt := global_step_cnt + 1;
\r
106 init_step_cnt := init_step_cnt + 1;
\r
107 elsif (global_step_cnt = 1) then
\r
108 --step0.1 = palette set.
\r
111 -- .byte $0f, $00, $10, $20
\r
112 -- .byte $0f, $04, $14, $24
\r
113 -- .byte $0f, $08, $18, $28
\r
114 -- .byte $0f, $0c, $1c, $2c
\r
116 -- .byte $0f, $00, $10, $20
\r
117 -- .byte $0f, $06, $16, $26
\r
118 -- .byte $0f, $08, $18, $28
\r
119 -- .byte $0f, $0a, $1a, $2a
\r
122 if (plt_step_cnt = 0 * cpu_io_multi) then
\r
123 --set vram addr 3f00
\r
124 io_out(16#2006#, 16#3f#);
\r
125 elsif (plt_step_cnt = 1 * cpu_io_multi) then
\r
126 io_out(16#2006#, 16#00#);
\r
128 elsif (plt_step_cnt = 2 * cpu_io_multi) then
\r
129 --set palette bg data
\r
130 io_out(16#2007#, 16#11#);
\r
131 elsif (plt_step_cnt = 3 * cpu_io_multi) then
\r
132 io_out(16#2007#, 16#01#);
\r
133 elsif (plt_step_cnt = 4 * cpu_io_multi) then
\r
134 io_out(16#2007#, 16#03#);
\r
135 elsif (plt_step_cnt = 5 * cpu_io_multi) then
\r
136 io_out(16#2007#, 16#13#);
\r
138 elsif (plt_step_cnt = 6 * cpu_io_multi) then
\r
139 io_out(16#2007#, 16#0f#);
\r
140 elsif (plt_step_cnt = 7 * cpu_io_multi) then
\r
141 io_out(16#2007#, 16#04#);
\r
142 elsif (plt_step_cnt = 8 * cpu_io_multi) then
\r
143 io_out(16#2007#, 16#14#);
\r
144 elsif (plt_step_cnt = 9 * cpu_io_multi) then
\r
145 io_out(16#2007#, 16#24#);
\r
147 elsif (plt_step_cnt = 10 * cpu_io_multi) then
\r
148 io_out(16#2007#, 16#0f#);
\r
149 elsif (plt_step_cnt = 11 * cpu_io_multi) then
\r
150 io_out(16#2007#, 16#08#);
\r
151 elsif (plt_step_cnt = 12 * cpu_io_multi) then
\r
152 io_out(16#2007#, 16#18#);
\r
153 elsif (plt_step_cnt = 13 * cpu_io_multi) then
\r
154 io_out(16#2007#, 16#28#);
\r
156 elsif (plt_step_cnt = 14 * cpu_io_multi) then
\r
157 io_out(16#2007#, 16#05#);
\r
158 elsif (plt_step_cnt = 15 * cpu_io_multi) then
\r
159 io_out(16#2007#, 16#0c#);
\r
160 elsif (plt_step_cnt = 16 * cpu_io_multi) then
\r
161 io_out(16#2007#, 16#1c#);
\r
162 elsif (plt_step_cnt = 17 * cpu_io_multi) then
\r
163 io_out(16#2007#, 16#2c#);
\r
165 elsif (plt_step_cnt = 18 * cpu_io_multi) then
\r
166 --below is sprite pallete
\r
167 io_out(16#2007#, 16#00#);
\r
168 elsif (plt_step_cnt = 19 * cpu_io_multi) then
\r
169 io_out(16#2007#, 16#24#);
\r
170 elsif (plt_step_cnt = 20 * cpu_io_multi) then
\r
171 io_out(16#2007#, 16#1b#);
\r
172 elsif (plt_step_cnt = 21 * cpu_io_multi) then
\r
173 io_out(16#2007#, 16#11#);
\r
175 elsif (plt_step_cnt = 22 * cpu_io_multi) then
\r
176 io_out(16#2007#, 16#00#);
\r
177 elsif (plt_step_cnt = 23 * cpu_io_multi) then
\r
178 io_out(16#2007#, 16#32#);
\r
179 elsif (plt_step_cnt = 24 * cpu_io_multi) then
\r
180 io_out(16#2007#, 16#16#);
\r
181 elsif (plt_step_cnt = 25 * cpu_io_multi) then
\r
182 io_out(16#2007#, 16#20#);
\r
184 elsif (plt_step_cnt = 26 * cpu_io_multi) then
\r
185 io_out(16#2007#, 16#00#);
\r
186 elsif (plt_step_cnt = 27 * cpu_io_multi) then
\r
187 io_out(16#2007#, 16#26#);
\r
188 elsif (plt_step_cnt = 28 * cpu_io_multi) then
\r
189 io_out(16#2007#, 16#01#);
\r
190 elsif (plt_step_cnt = 29 * cpu_io_multi) then
\r
191 io_out(16#2007#, 16#31#);
\r
195 if (plt_step_cnt > 30 * cpu_io_multi) then
\r
196 global_step_cnt := global_step_cnt + 1;
\r
199 plt_step_cnt := plt_step_cnt + 1;
\r
201 elsif (global_step_cnt = 2) then
\r
202 --step1 = name table set.
\r
203 if (nt_step_cnt = 0 * cpu_io_multi) then
\r
204 --set vram addr 2005 (first row, 6th col)
\r
205 io_out(16#2006#, 16#20#);
\r
206 elsif (nt_step_cnt = 1 * cpu_io_multi) then
\r
207 io_out(16#2006#, 16#06#);
\r
208 elsif (nt_step_cnt = 2 * cpu_io_multi) then
\r
209 --set name tbl data
\r
210 --0x44, 45, 45 = DEE
\r
211 io_out(16#2007#, 16#44#);
\r
212 elsif (nt_step_cnt = 3 * cpu_io_multi) then
\r
213 io_out(16#2007#, 16#45#);
\r
214 elsif (nt_step_cnt = 4 * cpu_io_multi) then
\r
215 io_out(16#2007#, 16#45#);
\r
218 elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
219 --set vram addr 23c1 (attribute)
\r
220 io_out(16#2006#, 16#23#);
\r
221 elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
222 io_out(16#2006#, 16#c1#);
\r
223 elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
225 io_out(16#2007#, 16#d8#);
\r
228 elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
229 io_out(16#2006#, 16#20#);
\r
230 elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
231 io_out(16#2006#, 16#60#);
\r
233 elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
234 io_out(16#2007#, 48);
\r
235 elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
236 io_out(16#2007#, 49);
\r
237 elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
238 io_out(16#2007#, 50);
\r
239 elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
240 io_out(16#2007#, 51);
\r
241 elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
242 io_out(16#2007#, 52);
\r
243 elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
244 io_out(16#2007#, 53);
\r
245 elsif (nt_step_cnt = 16 * cpu_io_multi) then
\r
246 io_out(16#2007#, 54);
\r
247 elsif (nt_step_cnt = 17 * cpu_io_multi) then
\r
248 io_out(16#2007#, 55);
\r
249 elsif (nt_step_cnt = 18 * cpu_io_multi) then
\r
250 io_out(16#2007#, 56);
\r
252 -- elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
253 -- --set vram addr 21d1
\r
254 -- io_out(16#2006#, 16#21#);
\r
255 -- elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
256 -- io_out(16#2006#, 16#E6#);
\r
257 -- elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
258 -- --msg=DEE TEST !!!
\r
259 -- io_out(16#2007#, 16#44#);
\r
260 -- elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
261 -- io_out(16#2007#, 16#45#);
\r
262 -- elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
263 -- io_out(16#2007#, 16#45#);
\r
264 -- elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
265 -- io_out(16#2007#, 16#00#);
\r
266 -- elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
267 -- io_out(16#2007#, 16#54#);
\r
268 -- elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
269 -- io_out(16#2007#, 16#45#);
\r
270 -- elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
271 -- io_out(16#2007#, 16#53#);
\r
272 -- elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
273 -- io_out(16#2007#, 16#54#);
\r
274 -- elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
275 -- io_out(16#2007#, 16#21#);
\r
277 --display test pattern
\r
278 elsif (nt_step_cnt = 19 * cpu_io_multi) then
\r
279 io_out(16#2006#, 16#20#);
\r
280 elsif (nt_step_cnt = 20 * cpu_io_multi) then
\r
281 io_out(16#2006#, 16#20#);
\r
283 elsif (nt_step_cnt = 21 * cpu_io_multi) then
\r
284 io_out(16#2007#, 16#01#);
\r
285 elsif (nt_step_cnt = 22 * cpu_io_multi) then
\r
286 io_out(16#2007#, 16#02#);
\r
287 elsif (nt_step_cnt = 23 * cpu_io_multi) then
\r
288 io_out(16#2007#, 16#03#);
\r
289 elsif (nt_step_cnt = 24 * cpu_io_multi) then
\r
290 io_out(16#2007#, 16#04#);
\r
291 elsif (nt_step_cnt = 25 * cpu_io_multi) then
\r
292 io_out(16#2007#, 16#05#);
\r
293 elsif (nt_step_cnt = 26 * cpu_io_multi) then
\r
294 io_out(16#2007#, 16#06#);
\r
295 elsif (nt_step_cnt = 27 * cpu_io_multi) then
\r
296 io_out(16#2007#, 16#07#);
\r
297 elsif (nt_step_cnt = 28 * cpu_io_multi) then
\r
298 io_out(16#2007#, 16#08#);
\r
299 elsif (nt_step_cnt = 29 * cpu_io_multi) then
\r
300 io_out(16#2007#, 16#09#);
\r
301 elsif (nt_step_cnt = 30 * cpu_io_multi) then
\r
302 io_out(16#2007#, 16#0a#);
\r
303 elsif (nt_step_cnt = 31 * cpu_io_multi) then
\r
304 io_out(16#2007#, 16#0b#);
\r
305 elsif (nt_step_cnt = 32 * cpu_io_multi) then
\r
306 io_out(16#2007#, 16#0c#);
\r
307 elsif (nt_step_cnt = 33 * cpu_io_multi) then
\r
308 io_out(16#2007#, 16#0d#);
\r
309 elsif (nt_step_cnt = 34 * cpu_io_multi) then
\r
310 io_out(16#2007#, 16#0e#);
\r
311 elsif (nt_step_cnt = 35 * cpu_io_multi) then
\r
312 io_out(16#2007#, 16#0f#);
\r
314 elsif (nt_step_cnt = 36 * cpu_io_multi) then
\r
315 io_out(16#2006#, 16#20#);
\r
316 elsif (nt_step_cnt = 37 * cpu_io_multi) then
\r
317 io_out(16#2006#, 16#40#);
\r
319 elsif (nt_step_cnt = 38 * cpu_io_multi) then
\r
320 io_out(16#2007#, 16#10#);
\r
321 elsif (nt_step_cnt = 39 * cpu_io_multi) then
\r
322 io_out(16#2007#, 16#11#);
\r
323 elsif (nt_step_cnt = 40 * cpu_io_multi) then
\r
324 io_out(16#2007#, 16#12#);
\r
325 elsif (nt_step_cnt = 41 * cpu_io_multi) then
\r
326 io_out(16#2007#, 16#13#);
\r
327 elsif (nt_step_cnt = 42 * cpu_io_multi) then
\r
328 io_out(16#2007#, 16#14#);
\r
329 elsif (nt_step_cnt = 43 * cpu_io_multi) then
\r
330 io_out(16#2007#, 16#15#);
\r
331 elsif (nt_step_cnt = 44 * cpu_io_multi) then
\r
332 io_out(16#2007#, 16#16#);
\r
333 elsif (nt_step_cnt = 45 * cpu_io_multi) then
\r
334 io_out(16#2007#, 16#17#);
\r
335 elsif (nt_step_cnt = 46 * cpu_io_multi) then
\r
336 io_out(16#2007#, 16#18#);
\r
337 elsif (nt_step_cnt = 47 * cpu_io_multi) then
\r
338 io_out(16#2007#, 16#19#);
\r
339 elsif (nt_step_cnt = 48 * cpu_io_multi) then
\r
340 io_out(16#2007#, 16#1a#);
\r
341 elsif (nt_step_cnt = 49 * cpu_io_multi) then
\r
342 io_out(16#2007#, 16#1b#);
\r
343 elsif (nt_step_cnt = 50 * cpu_io_multi) then
\r
344 io_out(16#2007#, 16#1c#);
\r
345 elsif (nt_step_cnt = 51 * cpu_io_multi) then
\r
346 io_out(16#2007#, 16#1d#);
\r
347 elsif (nt_step_cnt = 52 * cpu_io_multi) then
\r
348 io_out(16#2007#, 16#1e#);
\r
349 elsif (nt_step_cnt = 53 * cpu_io_multi) then
\r
350 io_out(16#2007#, 16#1f#);
\r
354 if (nt_step_cnt > 4 * cpu_io_multi) then
\r
355 global_step_cnt := global_step_cnt + 1;
\r
359 nt_step_cnt := nt_step_cnt + 1;
\r
361 elsif (global_step_cnt = 3) then
\r
362 --step2 = sprite set.
\r
363 if (spr_step_cnt = 0) then
\r
364 --set sprite addr=00 (first sprite)
\r
365 io_out(16#2003#, 16#00#);
\r
366 elsif (spr_step_cnt = 1 * cpu_io_multi) then
\r
367 --set sprite data: y=02
\r
368 io_out(16#2004#, 16#13#);
\r
369 elsif (spr_step_cnt = 2 * cpu_io_multi) then
\r
370 --tile=0x4d (ascii 'M')
\r
371 io_out(16#2004#, 16#4d#);
\r
372 elsif (spr_step_cnt = 3 * cpu_io_multi) then
\r
373 --set sprite attr=03 (palette 03)
\r
374 io_out(16#2004#, 16#01#);
\r
375 elsif (spr_step_cnt = 4 * cpu_io_multi) then
\r
376 --set sprite data: x=100
\r
377 io_out(16#2004#, 16#64#);
\r
379 elsif (spr_step_cnt = 5 * cpu_io_multi) then
\r
380 --set sprite data: y=50
\r
381 io_out(16#2004#, 16#32#);
\r
382 elsif (spr_step_cnt = 6 * cpu_io_multi) then
\r
383 --tile=0x4d (ascii 'O')
\r
384 io_out(16#2004#, 16#4f#);
\r
385 elsif (spr_step_cnt = 7 * cpu_io_multi) then
\r
386 --set sprite attr=01
\r
387 io_out(16#2004#, 16#01#);
\r
388 elsif (spr_step_cnt = 8 * cpu_io_multi) then
\r
389 --set sprite data: x=30
\r
390 io_out(16#2004#, 16#1e#);
\r
392 elsif (spr_step_cnt = 9 * cpu_io_multi) then
\r
393 --set sprite data: y=60
\r
394 io_out(16#2004#, 60);
\r
395 elsif (spr_step_cnt = 10 * cpu_io_multi) then
\r
396 --tile=0x4d (ascii 'P')
\r
397 io_out(16#2004#, 16#50#);
\r
398 elsif (spr_step_cnt = 11 * cpu_io_multi) then
\r
399 --set sprite attr=01
\r
400 io_out(16#2004#, 16#01#);
\r
401 elsif (spr_step_cnt = 12 * cpu_io_multi) then
\r
402 --set sprite data: x=33
\r
403 io_out(16#2004#, 16#21#);
\r
405 elsif (spr_step_cnt = 13 * cpu_io_multi) then
\r
406 --set sprite data: y=61
\r
407 io_out(16#2004#, 16#3d#);
\r
408 elsif (spr_step_cnt = 14 * cpu_io_multi) then
\r
409 --tile=0x4d (ascii 'Q')
\r
410 io_out(16#2004#, 16#51#);
\r
411 elsif (spr_step_cnt = 15 * cpu_io_multi) then
\r
412 --set sprite attr=02
\r
413 io_out(16#2004#, 16#02#);
\r
414 elsif (spr_step_cnt = 16 * cpu_io_multi) then
\r
415 --set sprite data: x=45
\r
416 io_out(16#2004#, 45);
\r
420 if (spr_step_cnt > 4 * cpu_io_multi) then
\r
421 global_step_cnt := global_step_cnt + 1;
\r
424 spr_step_cnt := spr_step_cnt + 1;
\r
426 elsif (global_step_cnt = 4) then
\r
428 for i in 0 to 64 loop
\r
430 if (ch = 16#5b#) then
\r
437 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
438 io_out(16#0200# + dma_step_cnt, i);
\r
439 elsif (dma_step_cnt = (1 + j) * cpu_io_multi) then
\r
440 io_out(16#0201# + dma_step_cnt, ch);
\r
441 elsif (dma_step_cnt = (2 + j) * cpu_io_multi) then
\r
442 io_out(16#0202# + dma_step_cnt, 16#01#);
\r
443 elsif (dma_step_cnt = (3 + j) * cpu_io_multi) then
\r
444 io_out(16#0203# + dma_step_cnt, j);
\r
447 if (dma_step_cnt > 17 * cpu_io_multi) then
\r
448 global_step_cnt := global_step_cnt + 1;
\r
452 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
454 io_out(16#4014#, 16#02#);
\r
457 if (dma_step_cnt > 17 * cpu_io_multi) then
\r
458 global_step_cnt := global_step_cnt + 1;
\r
464 dma_step_cnt := dma_step_cnt + 1;
\r
466 elsif (global_step_cnt = 5) then
\r
467 --final step = enable ppu.
\r
468 if (enable_ppu_step_cnt = 0 * cpu_io_multi) then
\r
469 --scroll reg set x.
\r
470 io_out(16#2005#, 0);
\r
471 elsif (enable_ppu_step_cnt = 1 * cpu_io_multi) then
\r
472 --scroll reg set y.
\r
473 io_out(16#2005#, 0);
\r
474 elsif (enable_ppu_step_cnt = 2 * cpu_io_multi) then
\r
476 --PPUMASK=1e (show bg and sprite)
\r
477 --PPUMASK=0e (show bg only)
\r
478 io_out(16#2001#, 16#1e#);
\r
479 elsif (enable_ppu_step_cnt = 3 * cpu_io_multi) then
\r
482 io_out(16#2000#, 16#80#);
\r
485 if (enable_ppu_step_cnt > 4 * cpu_io_multi) then
\r
486 global_step_cnt := global_step_cnt + 1;
\r
489 enable_ppu_step_cnt := enable_ppu_step_cnt + 1;
\r
495 end if;--if (init_done = '0') then
\r
496 end if; --if (rst_n = '0') then
\r
506 -----------dummy prg rom
\r
508 use ieee.std_logic_1164.all;
\r
510 generic (abus_size : integer := 15; dbus_size : integer := 8);
\r
512 clk : in std_logic;
\r
513 ce_n : in std_logic; --active low.
\r
514 addr : in std_logic_vector (abus_size - 1 downto 0);
\r
515 data : out std_logic_vector (dbus_size - 1 downto 0)
\r
518 architecture rtl of prg_rom is
\r
520 data <= (others => 'Z');
\r