2 use ieee.std_logic_1164.all;
\r
6 pi_base_clk : in std_logic;
\r
7 pi_cpu_en : in std_logic_vector (7 downto 0);
\r
8 pi_rdy : in std_logic;
\r
9 pi_rst_n : in std_logic;
\r
10 pi_irq_n : in std_logic;
\r
11 pi_nmi_n : in std_logic;
\r
12 po_r_nw : out std_logic;
\r
13 po_addr : out std_logic_vector ( 15 downto 0);
\r
14 pio_d_io : inout std_logic_vector ( 7 downto 0)
\r
18 architecture rtl of mos6502 is
\r
24 set_ppu_p : process (pi_base_clk, pi_rst_n)
\r
25 use ieee.std_logic_arith.conv_std_logic_vector;
\r
27 variable init_step_cnt, plt_step_cnt,
\r
28 nt_step_cnt, spr_step_cnt, dma_step_cnt, scl_step_cnt,
\r
29 enable_ppu_step_cnt, nmi_step_cnt : integer;
\r
30 variable init_done : std_logic;
\r
31 variable global_step_cnt : integer;
\r
32 constant cpu_io_multi : integer := 3; --io happens every 4 cpu cycle.
\r
33 variable i, j : integer;
\r
34 variable ch : integer := 16#41# ;
\r
35 variable nmi_oam_x : integer range 0 to 255;
\r
36 variable nmi_scl_y : integer range 0 to 255;
\r
38 variable ref_cnt : integer range 0 to 120;
\r
40 procedure io_out (ad: in integer; dt : in integer) is
\r
43 po_addr <= conv_std_logic_vector(ad, 16);
\r
44 pio_d_io <= conv_std_logic_vector(dt, 8);
\r
49 po_addr <= (others => 'Z');
\r
50 pio_d_io <= (others => 'Z');
\r
54 procedure io_read (ad: in integer) is
\r
57 po_addr <= conv_std_logic_vector(ad, 16);
\r
58 pio_d_io <= (others => 'Z');
\r
62 if (pi_rst_n = '0') then
\r
65 po_addr <= (others => 'Z');
\r
66 pio_d_io <= (others => 'Z');
\r
69 global_step_cnt := 0;
\r
76 enable_ppu_step_cnt := 0;
\r
82 elsif (rising_edge(pi_base_clk)) then
\r
83 if (pi_cpu_en(0) = '1') then
\r
84 if (pi_rdy = '1') then
\r
85 if (init_done = '0') then
\r
86 if (global_step_cnt = 0) then
\r
87 --step0.0 = init ppu.
\r
88 if (init_step_cnt = 0 * cpu_io_multi) then
\r
89 --PPUCTRL=01 for test...
\r
90 io_out(16#2000#, 16#01#);
\r
91 elsif (init_step_cnt = 1 * cpu_io_multi) then
\r
93 io_out(16#2001#, 16#02#);
\r
94 elsif (init_step_cnt = 2 * cpu_io_multi) then
\r
96 io_out(16#2000#, 16#00#);
\r
97 elsif (init_step_cnt = 3 * cpu_io_multi) then
\r
99 io_out(16#2001#, 16#00#);
\r
100 elsif (init_step_cnt = 4 * cpu_io_multi) then
\r
102 io_out(16#2006#, 16#02#);
\r
103 elsif (init_step_cnt = 5 * cpu_io_multi) then
\r
104 io_out(16#2006#, 16#40#);
\r
105 elsif (init_step_cnt = 6 * cpu_io_multi) then
\r
107 io_out(16#2005#, 16#21#);
\r
108 elsif (init_step_cnt = 7 * cpu_io_multi) then
\r
109 io_out(16#2005#, 16#5#);
\r
112 if (init_step_cnt > 8 * cpu_io_multi) then
\r
113 global_step_cnt := global_step_cnt + 1;
\r
116 init_step_cnt := init_step_cnt + 1;
\r
117 elsif (global_step_cnt = 1) then
\r
118 --step0.1 = palette set.
\r
121 -- .byte $0f, $00, $10, $20
\r
122 -- .byte $0f, $04, $14, $24
\r
123 -- .byte $0f, $08, $18, $28
\r
124 -- .byte $0f, $0c, $1c, $2c
\r
126 -- .byte $0f, $00, $10, $20
\r
127 -- .byte $0f, $06, $16, $26
\r
128 -- .byte $0f, $08, $18, $28
\r
129 -- .byte $0f, $0a, $1a, $2a
\r
132 if (plt_step_cnt = 0 * cpu_io_multi) then
\r
133 --set vram addr 3f00
\r
134 io_out(16#2006#, 16#3f#);
\r
135 elsif (plt_step_cnt = 1 * cpu_io_multi) then
\r
136 io_out(16#2006#, 16#00#);
\r
138 elsif (plt_step_cnt = 2 * cpu_io_multi) then
\r
139 --set palette bg data
\r
140 io_out(16#2007#, 16#11#);
\r
141 elsif (plt_step_cnt = 3 * cpu_io_multi) then
\r
142 io_out(16#2007#, 16#01#);
\r
143 elsif (plt_step_cnt = 4 * cpu_io_multi) then
\r
144 io_out(16#2007#, 16#03#);
\r
145 elsif (plt_step_cnt = 5 * cpu_io_multi) then
\r
146 io_out(16#2007#, 16#13#);
\r
148 elsif (plt_step_cnt = 6 * cpu_io_multi) then
\r
149 io_out(16#2007#, 16#0f#);
\r
150 elsif (plt_step_cnt = 7 * cpu_io_multi) then
\r
151 io_out(16#2007#, 16#04#);
\r
152 elsif (plt_step_cnt = 8 * cpu_io_multi) then
\r
153 io_out(16#2007#, 16#14#);
\r
154 elsif (plt_step_cnt = 9 * cpu_io_multi) then
\r
155 io_out(16#2007#, 16#24#);
\r
157 elsif (plt_step_cnt = 10 * cpu_io_multi) then
\r
158 io_out(16#2007#, 16#0f#);
\r
159 elsif (plt_step_cnt = 11 * cpu_io_multi) then
\r
160 io_out(16#2007#, 16#08#);
\r
161 elsif (plt_step_cnt = 12 * cpu_io_multi) then
\r
162 io_out(16#2007#, 16#18#);
\r
163 elsif (plt_step_cnt = 13 * cpu_io_multi) then
\r
164 io_out(16#2007#, 16#28#);
\r
166 elsif (plt_step_cnt = 14 * cpu_io_multi) then
\r
167 io_out(16#2007#, 16#05#);
\r
168 elsif (plt_step_cnt = 15 * cpu_io_multi) then
\r
169 io_out(16#2007#, 16#0c#);
\r
170 elsif (plt_step_cnt = 16 * cpu_io_multi) then
\r
171 io_out(16#2007#, 16#1c#);
\r
172 elsif (plt_step_cnt = 17 * cpu_io_multi) then
\r
173 io_out(16#2007#, 16#2c#);
\r
175 elsif (plt_step_cnt = 18 * cpu_io_multi) then
\r
176 --below is sprite pallete
\r
177 io_out(16#2007#, 16#00#);
\r
178 elsif (plt_step_cnt = 19 * cpu_io_multi) then
\r
179 io_out(16#2007#, 16#24#);
\r
180 elsif (plt_step_cnt = 20 * cpu_io_multi) then
\r
181 io_out(16#2007#, 16#1b#);
\r
182 elsif (plt_step_cnt = 21 * cpu_io_multi) then
\r
183 io_out(16#2007#, 16#11#);
\r
185 elsif (plt_step_cnt = 22 * cpu_io_multi) then
\r
186 io_out(16#2007#, 16#00#);
\r
187 elsif (plt_step_cnt = 23 * cpu_io_multi) then
\r
188 io_out(16#2007#, 16#32#);
\r
189 elsif (plt_step_cnt = 24 * cpu_io_multi) then
\r
190 io_out(16#2007#, 16#16#);
\r
191 elsif (plt_step_cnt = 25 * cpu_io_multi) then
\r
192 io_out(16#2007#, 16#20#);
\r
194 elsif (plt_step_cnt = 26 * cpu_io_multi) then
\r
195 io_out(16#2007#, 16#00#);
\r
196 elsif (plt_step_cnt = 27 * cpu_io_multi) then
\r
197 io_out(16#2007#, 16#26#);
\r
198 elsif (plt_step_cnt = 28 * cpu_io_multi) then
\r
199 io_out(16#2007#, 16#01#);
\r
200 elsif (plt_step_cnt = 29 * cpu_io_multi) then
\r
201 io_out(16#2007#, 16#31#);
\r
205 if (plt_step_cnt > 30 * cpu_io_multi) then
\r
206 global_step_cnt := global_step_cnt + 1;
\r
209 plt_step_cnt := plt_step_cnt + 1;
\r
211 elsif (global_step_cnt = 2) then
\r
212 --step1 = name table set.
\r
213 if (nt_step_cnt = 0 * cpu_io_multi) then
\r
214 --set vram addr 2005 (first row, 6th col)
\r
215 io_out(16#2006#, 16#20#);
\r
216 elsif (nt_step_cnt = 1 * cpu_io_multi) then
\r
217 io_out(16#2006#, 16#03#);
\r
218 elsif (nt_step_cnt = 2 * cpu_io_multi) then
\r
219 --set name tbl data
\r
220 --0x44, 45, 45 = DEE
\r
221 io_out(16#2007#, 16#44#);
\r
222 elsif (nt_step_cnt = 3 * cpu_io_multi) then
\r
223 io_out(16#2007#, 16#45#);
\r
224 elsif (nt_step_cnt = 4 * cpu_io_multi) then
\r
225 io_out(16#2007#, 16#45#);
\r
228 elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
229 io_out(16#2006#, 16#20#);
\r
230 elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
231 io_out(16#2006#, 16#2a#);
\r
232 elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
233 io_out(16#2007#, 16#44#);
\r
235 elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
236 io_out(16#2006#, 16#24#);
\r
237 elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
238 io_out(16#2006#, 16#43#);
\r
239 elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
240 io_out(16#2007#, 16#6d#);
\r
241 elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
242 io_out(16#2007#, 16#6f#);
\r
243 elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
244 io_out(16#2007#, 16#74#);
\r
245 elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
246 io_out(16#2007#, 16#6f#);
\r
248 elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
249 io_out(16#2006#, 16#2e#);
\r
250 elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
251 io_out(16#2006#, 16#93#);
\r
252 elsif (nt_step_cnt = 16 * cpu_io_multi) then
\r
253 io_out(16#2007#, 16#59#);
\r
255 elsif (nt_step_cnt = 17 * cpu_io_multi) then
\r
256 io_out(16#2007#, 16#00#);
\r
260 if (nt_step_cnt > 4 * cpu_io_multi) then
\r
261 global_step_cnt := global_step_cnt + 1;
\r
265 nt_step_cnt := nt_step_cnt + 1;
\r
267 elsif (global_step_cnt = 3) then
\r
268 --step2 = sprite set.
\r
269 if (spr_step_cnt = 0) then
\r
270 --set sprite addr=00 (first sprite)
\r
271 io_out(16#2003#, 16#00#);
\r
273 elsif (spr_step_cnt = 1 * cpu_io_multi) then
\r
274 --set sprite data: y=02
\r
275 io_out(16#2004#, 16#05#);
\r
276 elsif (spr_step_cnt = 2 * cpu_io_multi) then
\r
277 --tile=0x4d (ascii 'O')
\r
278 io_out(16#2004#, 16#4f#);
\r
279 elsif (spr_step_cnt = 3 * cpu_io_multi) then
\r
280 --set sprite attr=01 (palette 01)
\r
281 io_out(16#2004#, 16#01#);
\r
282 elsif (spr_step_cnt = 4 * cpu_io_multi) then
\r
283 --set sprite data: x=60
\r
284 io_out(16#2004#, 16#3c#);
\r
286 elsif (spr_step_cnt = 5 * cpu_io_multi) then
\r
287 --set sprite data: y=50
\r
288 io_out(16#2004#, 1);
\r
289 elsif (spr_step_cnt = 6 * cpu_io_multi) then
\r
290 --tile=0x4f (ascii 'M')
\r
291 io_out(16#2004#, 16#4d#);
\r
292 elsif (spr_step_cnt = 7 * cpu_io_multi) then
\r
293 --set sprite attr=02
\r
294 io_out(16#2004#, 16#02#);
\r
295 elsif (spr_step_cnt = 8 * cpu_io_multi) then
\r
296 --set sprite data: x=100
\r
297 io_out(16#2004#, 16#64#);
\r
299 elsif (spr_step_cnt = 9 * cpu_io_multi) then
\r
300 --set sprite data: y=60
\r
301 io_out(16#2004#, 60);
\r
302 elsif (spr_step_cnt = 10 * cpu_io_multi) then
\r
303 --tile=0x4d (ascii 'P')
\r
304 io_out(16#2004#, 16#50#);
\r
305 elsif (spr_step_cnt = 11 * cpu_io_multi) then
\r
306 --set sprite attr=01
\r
307 io_out(16#2004#, 16#01#);
\r
308 elsif (spr_step_cnt = 12 * cpu_io_multi) then
\r
309 --set sprite data: x=33
\r
310 io_out(16#2004#, 16#21#);
\r
312 elsif (spr_step_cnt = 13 * cpu_io_multi) then
\r
313 --set sprite data: y=61
\r
314 io_out(16#2004#, 16#3d#);
\r
315 elsif (spr_step_cnt = 14 * cpu_io_multi) then
\r
316 --tile=0x4d (ascii 'Q')
\r
317 io_out(16#2004#, 16#51#);
\r
318 elsif (spr_step_cnt = 15 * cpu_io_multi) then
\r
319 --set sprite attr=02
\r
320 io_out(16#2004#, 16#02#);
\r
321 elsif (spr_step_cnt = 16 * cpu_io_multi) then
\r
322 --set sprite data: x=45
\r
323 io_out(16#2004#, 45);
\r
327 if (spr_step_cnt > 8 * cpu_io_multi) then
\r
328 global_step_cnt := global_step_cnt + 2;
\r
331 spr_step_cnt := spr_step_cnt + 1;
\r
333 elsif (global_step_cnt = 4) then
\r
335 for i in 0 to 64 loop
\r
337 if (ch = 16#5b#) then
\r
345 --set dma value on the ram.
\r
346 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
347 io_out(16#0200# + j, i);
\r
348 elsif (dma_step_cnt = (1 + j) * cpu_io_multi) then
\r
349 io_out(16#0201# + j, ch);
\r
350 elsif (dma_step_cnt = (2 + j) * cpu_io_multi) then
\r
351 io_out(16#0202# + j, 16#01#);
\r
352 elsif (dma_step_cnt = (3 + j) * cpu_io_multi) then
\r
353 io_out(16#0203# + j, j);
\r
354 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) or
\r
355 (dma_step_cnt = (1 + j) * cpu_io_multi + 1) or
\r
356 (dma_step_cnt = (2 + j) * cpu_io_multi + 1) or
\r
357 (dma_step_cnt = (3 + j) * cpu_io_multi + 1) then
\r
361 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
363 io_out(16#4014#, 16#02#);
\r
364 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) then
\r
366 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 2) then
\r
368 global_step_cnt := global_step_cnt + 1;
\r
373 dma_step_cnt := dma_step_cnt + 1;
\r
375 elsif (global_step_cnt = 5) then
\r
376 --step4 = scroll test and ppu reg read test.
\r
377 if (scl_step_cnt = 0) then
\r
379 io_out(16#2005#, 11);
\r
380 elsif (scl_step_cnt = 1 * cpu_io_multi) then
\r
382 io_out(16#2005#, 0);
\r
384 elsif (scl_step_cnt = 2 * cpu_io_multi) then
\r
388 elsif (scl_step_cnt = 3 * cpu_io_multi) then
\r
390 io_out(16#2006#, 16#20#);
\r
392 elsif (scl_step_cnt = 4 * cpu_io_multi) then
\r
393 io_out(16#2006#, 16#03#);
\r
395 elsif (scl_step_cnt = 5 * cpu_io_multi) then
\r
398 elsif (scl_step_cnt = 6 * cpu_io_multi) then
\r
400 io_out(16#2006#, 16#04#);
\r
402 elsif (scl_step_cnt = 7 * cpu_io_multi) then
\r
403 io_out(16#2006#, 16#d1#);
\r
405 elsif (scl_step_cnt = 8 * cpu_io_multi) then
\r
408 elsif (scl_step_cnt = 9 * cpu_io_multi) then
\r
410 io_out(16#2006#, 16#23#);
\r
412 elsif (scl_step_cnt = 10 * cpu_io_multi) then
\r
413 io_out(16#2006#, 16#c0#);
\r
415 elsif (scl_step_cnt = 11 * cpu_io_multi) then
\r
417 io_out(16#2007#, 16#5a#);
\r
419 elsif (scl_step_cnt = 12 * cpu_io_multi) then
\r
420 io_out(16#2006#, 16#23#);
\r
422 elsif (scl_step_cnt = 13 * cpu_io_multi) then
\r
423 io_out(16#2006#, 16#c0#);
\r
425 elsif (scl_step_cnt = 14 * cpu_io_multi) then
\r
428 elsif (scl_step_cnt = 15 * cpu_io_multi) then
\r
429 --read palette tbl.
\r
430 io_out(16#2006#, 16#3f#);
\r
432 elsif (scl_step_cnt = 16 * cpu_io_multi) then
\r
433 io_out(16#2006#, 16#11#);
\r
435 elsif (scl_step_cnt = 17 * cpu_io_multi) then
\r
440 if (scl_step_cnt > 17 * cpu_io_multi) then
\r
441 global_step_cnt := global_step_cnt + 1;
\r
444 scl_step_cnt := scl_step_cnt + 1;
\r
446 elsif (global_step_cnt = 6) then
\r
447 --final step = enable ppu.
\r
448 if (enable_ppu_step_cnt = 0 * cpu_io_multi) then
\r
450 --PPUMASK=1e (show bg and sprite)
\r
451 --PPUMASK=0e (show bg only)
\r
452 io_out(16#2001#, 16#1e#);
\r
453 elsif (enable_ppu_step_cnt = 1 * cpu_io_multi) then
\r
456 io_out(16#2000#, 16#80#);
\r
459 if (enable_ppu_step_cnt > 1 * cpu_io_multi) then
\r
460 global_step_cnt := global_step_cnt + 1;
\r
463 enable_ppu_step_cnt := enable_ppu_step_cnt + 1;
\r
465 elsif (global_step_cnt = 7) then
\r
467 if (pi_nmi_n = '0') then
\r
469 if (nmi_step_cnt = 0 * cpu_io_multi) then
\r
470 --set sprite addr=00 (first sprite)
\r
471 io_out(16#2003#, 16#03#);
\r
472 elsif (nmi_step_cnt = 1 * cpu_io_multi) then
\r
473 --set sprite data: x=100
\r
474 io_out(16#2004#, nmi_oam_x);
\r
475 elsif (nmi_step_cnt = 2 * cpu_io_multi) then
\r
477 -- io_out(16#2005#, nmi_scl_y);
\r
479 elsif (nmi_step_cnt = 3 * cpu_io_multi) then
\r
481 -- io_out(16#2005#, nmi_scl_y);
\r
483 elsif (nmi_step_cnt = 4 * cpu_io_multi) then
\r
484 --set sprite addr=00 (first sprite)
\r
485 io_out(16#2003#, 16#04#);
\r
486 elsif (nmi_step_cnt = 5 * cpu_io_multi) then
\r
487 --set sprite data: x=100
\r
488 io_out(16#2004#, nmi_oam_x);
\r
490 if (ref_cnt = 0) then
\r
491 nmi_oam_x := nmi_oam_x + 1;
\r
493 if (nmi_step_cnt mod 10 = 0) then
\r
494 nmi_scl_y := nmi_scl_y + 1;
\r
497 if (nmi_step_cnt > 5 * cpu_io_multi) then
\r
498 ref_cnt := ref_cnt + 1;
\r
499 global_step_cnt := global_step_cnt + 1;
\r
502 nmi_step_cnt := nmi_step_cnt + 1;
\r
504 elsif (global_step_cnt = 8) then
\r
505 ----back to nmi tests.....
\r
506 if (pi_nmi_n = '1') then
\r
508 global_step_cnt := global_step_cnt - 1;
\r
514 end if;--if (init_done = '0') then
\r
517 po_addr <= (others => 'Z');
\r
518 pio_d_io <= (others => 'Z');
\r
519 end if;--if (rdy = '1') then
\r
520 end if;--if (pi_cpu_en(0) = '1') then
\r
521 end if; --if (rst_n = '0') then
\r