2 use ieee.std_logic_1164.all;
\r
6 pi_rst_n : in std_logic;
\r
7 pi_base_clk : in std_logic;
\r
8 pi_cpu_en : in std_logic_vector (7 downto 0);
\r
9 pi_rdy : 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
20 signal reg_r_nw : std_logic;
\r
21 signal reg_addr : std_logic_vector ( 15 downto 0);
\r
22 signal reg_d_in : std_logic_vector ( 7 downto 0);
\r
23 signal reg_d_out : std_logic_vector ( 7 downto 0);
\r
25 signal dummy_ad : std_logic_vector (12 downto 0) := "0000000000000";
\r
26 signal dummy_cnt : integer := 0;
\r
30 po_r_nw <= reg_r_nw;
\r
31 po_addr <= reg_addr;
\r
32 pio_d_io <= reg_d_out;
\r
33 reg_d_in <= pio_d_io;
\r
36 set_ppu_p : process (pi_base_clk, pi_rst_n)
\r
37 use ieee.std_logic_arith.conv_std_logic_vector;
\r
39 variable init_step_cnt, plt_step_cnt,
\r
40 nt_step_cnt, spr_step_cnt, dma_step_cnt, scl_step_cnt,
\r
41 enable_ppu_step_cnt, nmi_step_cnt : integer;
\r
42 variable init_done : std_logic;
\r
43 variable global_step_cnt : integer;
\r
44 constant cpu_io_multi : integer := 3; --io happens every 4 cpu cycle.
\r
45 variable i, j : integer;
\r
46 variable ch : integer := 16#41# ;
\r
47 variable nmi_oam_x : integer range 0 to 255;
\r
48 variable nmi_scl_y : integer range 0 to 255;
\r
50 variable ref_cnt : integer range 0 to 120;
\r
52 procedure io_out (ad: in integer; dt : in integer) is
\r
55 reg_addr <= conv_std_logic_vector(ad, 16);
\r
56 reg_d_out <= conv_std_logic_vector(dt, 8);
\r
60 use ieee.std_logic_unsigned.all;
\r
62 --fake ram read/write to emulate dummy i/o.
\r
63 reg_addr <= "000" & dummy_ad;
\r
64 dummy_ad <= dummy_ad + 1;
\r
65 if (dummy_cnt = 0) then
\r
66 reg_d_out <= (others => 'Z');
\r
70 reg_d_out <= dummy_ad(7 downto 0);
\r
76 procedure io_read (ad: in integer) is
\r
79 reg_addr <= conv_std_logic_vector(ad, 16);
\r
80 reg_d_out <= (others => 'Z');
\r
84 if (pi_rst_n = '0') then
\r
87 reg_addr <= (others => 'Z');
\r
88 reg_d_out <= (others => 'Z');
\r
91 global_step_cnt := 0;
\r
98 enable_ppu_step_cnt := 0;
\r
104 elsif (rising_edge(pi_base_clk)) then
\r
105 if (pi_cpu_en(0) = '1') then
\r
106 if (pi_rdy = '1') then
\r
107 if (init_done = '0') then
\r
108 if (global_step_cnt = 0) then
\r
109 --step0.0 = init ppu.
\r
110 if (init_step_cnt = 0 * cpu_io_multi) then
\r
111 --PPUCTRL=01 for test...
\r
112 io_out(16#2000#, 16#01#);
\r
113 elsif (init_step_cnt = 1 * cpu_io_multi) then
\r
115 io_out(16#2001#, 16#02#);
\r
116 elsif (init_step_cnt = 2 * cpu_io_multi) then
\r
118 io_out(16#2000#, 16#00#);
\r
119 elsif (init_step_cnt = 3 * cpu_io_multi) then
\r
121 io_out(16#2001#, 16#00#);
\r
122 elsif (init_step_cnt = 4 * cpu_io_multi) then
\r
124 io_out(16#2006#, 16#02#);
\r
125 elsif (init_step_cnt = 5 * cpu_io_multi) then
\r
126 io_out(16#2006#, 16#40#);
\r
127 elsif (init_step_cnt = 6 * cpu_io_multi) then
\r
129 io_out(16#2005#, 16#21#);
\r
130 elsif (init_step_cnt = 7 * cpu_io_multi) then
\r
131 io_out(16#2005#, 16#5#);
\r
134 if (init_step_cnt > 8 * cpu_io_multi) then
\r
135 global_step_cnt := global_step_cnt + 1;
\r
138 init_step_cnt := init_step_cnt + 1;
\r
139 elsif (global_step_cnt = 1) then
\r
140 --step0.1 = palette set.
\r
143 -- .byte $0f, $00, $10, $20
\r
144 -- .byte $0f, $04, $14, $24
\r
145 -- .byte $0f, $08, $18, $28
\r
146 -- .byte $0f, $0c, $1c, $2c
\r
148 -- .byte $0f, $00, $10, $20
\r
149 -- .byte $0f, $06, $16, $26
\r
150 -- .byte $0f, $08, $18, $28
\r
151 -- .byte $0f, $0a, $1a, $2a
\r
154 if (plt_step_cnt = 0 * cpu_io_multi) then
\r
155 --set vram addr 3f00
\r
156 io_out(16#2006#, 16#3f#);
\r
157 elsif (plt_step_cnt = 1 * cpu_io_multi) then
\r
158 io_out(16#2006#, 16#00#);
\r
160 elsif (plt_step_cnt = 2 * cpu_io_multi) then
\r
161 --set palette bg data
\r
162 io_out(16#2007#, 16#11#);
\r
163 elsif (plt_step_cnt = 3 * cpu_io_multi) then
\r
164 io_out(16#2007#, 16#01#);
\r
165 elsif (plt_step_cnt = 4 * cpu_io_multi) then
\r
166 io_out(16#2007#, 16#03#);
\r
167 elsif (plt_step_cnt = 5 * cpu_io_multi) then
\r
168 io_out(16#2007#, 16#13#);
\r
170 elsif (plt_step_cnt = 6 * cpu_io_multi) then
\r
171 io_out(16#2007#, 16#0f#);
\r
172 elsif (plt_step_cnt = 7 * cpu_io_multi) then
\r
173 io_out(16#2007#, 16#04#);
\r
174 elsif (plt_step_cnt = 8 * cpu_io_multi) then
\r
175 io_out(16#2007#, 16#14#);
\r
176 elsif (plt_step_cnt = 9 * cpu_io_multi) then
\r
177 io_out(16#2007#, 16#24#);
\r
179 elsif (plt_step_cnt = 10 * cpu_io_multi) then
\r
180 io_out(16#2007#, 16#0f#);
\r
181 elsif (plt_step_cnt = 11 * cpu_io_multi) then
\r
182 io_out(16#2007#, 16#08#);
\r
183 elsif (plt_step_cnt = 12 * cpu_io_multi) then
\r
184 io_out(16#2007#, 16#18#);
\r
185 elsif (plt_step_cnt = 13 * cpu_io_multi) then
\r
186 io_out(16#2007#, 16#28#);
\r
188 elsif (plt_step_cnt = 14 * cpu_io_multi) then
\r
189 io_out(16#2007#, 16#05#);
\r
190 elsif (plt_step_cnt = 15 * cpu_io_multi) then
\r
191 io_out(16#2007#, 16#0c#);
\r
192 elsif (plt_step_cnt = 16 * cpu_io_multi) then
\r
193 io_out(16#2007#, 16#1c#);
\r
194 elsif (plt_step_cnt = 17 * cpu_io_multi) then
\r
195 io_out(16#2007#, 16#2c#);
\r
197 elsif (plt_step_cnt = 18 * cpu_io_multi) then
\r
198 --below is sprite pallete
\r
199 io_out(16#2007#, 16#00#);
\r
200 elsif (plt_step_cnt = 19 * cpu_io_multi) then
\r
201 io_out(16#2007#, 16#24#);
\r
202 elsif (plt_step_cnt = 20 * cpu_io_multi) then
\r
203 io_out(16#2007#, 16#1b#);
\r
204 elsif (plt_step_cnt = 21 * cpu_io_multi) then
\r
205 io_out(16#2007#, 16#11#);
\r
207 elsif (plt_step_cnt = 22 * cpu_io_multi) then
\r
208 io_out(16#2007#, 16#00#);
\r
209 elsif (plt_step_cnt = 23 * cpu_io_multi) then
\r
210 io_out(16#2007#, 16#32#);
\r
211 elsif (plt_step_cnt = 24 * cpu_io_multi) then
\r
212 io_out(16#2007#, 16#16#);
\r
213 elsif (plt_step_cnt = 25 * cpu_io_multi) then
\r
214 io_out(16#2007#, 16#20#);
\r
216 elsif (plt_step_cnt = 26 * cpu_io_multi) then
\r
217 io_out(16#2007#, 16#00#);
\r
218 elsif (plt_step_cnt = 27 * cpu_io_multi) then
\r
219 io_out(16#2007#, 16#26#);
\r
220 elsif (plt_step_cnt = 28 * cpu_io_multi) then
\r
221 io_out(16#2007#, 16#01#);
\r
222 elsif (plt_step_cnt = 29 * cpu_io_multi) then
\r
223 io_out(16#2007#, 16#31#);
\r
227 if (plt_step_cnt > 30 * cpu_io_multi) then
\r
228 global_step_cnt := global_step_cnt + 1;
\r
231 plt_step_cnt := plt_step_cnt + 1;
\r
233 elsif (global_step_cnt = 2) then
\r
234 --step1 = name table set.
\r
235 if (nt_step_cnt = 0 * cpu_io_multi) then
\r
236 --set vram addr 2005 (first row, 6th col)
\r
237 io_out(16#2006#, 16#20#);
\r
238 elsif (nt_step_cnt = 1 * cpu_io_multi) then
\r
239 io_out(16#2006#, 16#08#);
\r
240 elsif (nt_step_cnt = 2 * cpu_io_multi) then
\r
241 --set name tbl data
\r
242 --0x44, 45, 45 = DEM
\r
243 io_out(16#2007#, 16#44#);
\r
244 elsif (nt_step_cnt = 3 * cpu_io_multi) then
\r
245 io_out(16#2007#, 16#45#);
\r
246 elsif (nt_step_cnt = 4 * cpu_io_multi) then
\r
247 io_out(16#2007#, 16#4d#);
\r
250 elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
251 io_out(16#2006#, 16#20#);
\r
252 elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
253 io_out(16#2006#, 16#2a#);
\r
254 elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
255 io_out(16#2007#, 16#44#);
\r
257 elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
258 io_out(16#2006#, 16#24#);
\r
259 elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
260 io_out(16#2006#, 16#43#);
\r
261 elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
262 io_out(16#2007#, 16#6d#);
\r
263 elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
264 io_out(16#2007#, 16#6f#);
\r
265 elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
266 io_out(16#2007#, 16#74#);
\r
267 elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
268 io_out(16#2007#, 16#6f#);
\r
270 elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
271 io_out(16#2006#, 16#2e#);
\r
272 elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
273 io_out(16#2006#, 16#93#);
\r
274 elsif (nt_step_cnt = 16 * cpu_io_multi) then
\r
275 io_out(16#2007#, 16#59#);
\r
277 elsif (nt_step_cnt = 17 * cpu_io_multi) then
\r
278 io_out(16#2007#, 16#00#);
\r
282 if (nt_step_cnt > 4 * cpu_io_multi) then
\r
283 global_step_cnt := global_step_cnt + 1;
\r
287 nt_step_cnt := nt_step_cnt + 1;
\r
289 elsif (global_step_cnt = 3) then
\r
290 --step2 = sprite set.
\r
291 if (spr_step_cnt = 0) then
\r
292 --set sprite addr=00 (first sprite)
\r
293 io_out(16#2003#, 16#00#);
\r
295 elsif (spr_step_cnt = 1 * cpu_io_multi) then
\r
296 --set sprite data: y=02
\r
297 io_out(16#2004#, 16#05#);
\r
298 elsif (spr_step_cnt = 2 * cpu_io_multi) then
\r
299 --tile=0x4d (ascii 'O')
\r
300 io_out(16#2004#, 16#4f#);
\r
301 elsif (spr_step_cnt = 3 * cpu_io_multi) then
\r
302 --set sprite attr=01 (palette 01)
\r
303 io_out(16#2004#, 16#01#);
\r
304 elsif (spr_step_cnt = 4 * cpu_io_multi) then
\r
305 --set sprite data: x=60
\r
306 io_out(16#2004#, 16#3c#);
\r
308 elsif (spr_step_cnt = 5 * cpu_io_multi) then
\r
309 --set sprite data: y=50
\r
310 io_out(16#2004#, 1);
\r
311 elsif (spr_step_cnt = 6 * cpu_io_multi) then
\r
312 --tile=0x4f (ascii 'M')
\r
313 io_out(16#2004#, 16#4d#);
\r
314 elsif (spr_step_cnt = 7 * cpu_io_multi) then
\r
315 --set sprite attr=02
\r
316 io_out(16#2004#, 16#02#);
\r
317 elsif (spr_step_cnt = 8 * cpu_io_multi) then
\r
318 --set sprite data: x=100
\r
319 io_out(16#2004#, 16#64#);
\r
321 elsif (spr_step_cnt = 9 * cpu_io_multi) then
\r
322 --set sprite data: y=60
\r
323 io_out(16#2004#, 60);
\r
324 elsif (spr_step_cnt = 10 * cpu_io_multi) then
\r
325 --tile=0x4d (ascii 'P')
\r
326 io_out(16#2004#, 16#50#);
\r
327 elsif (spr_step_cnt = 11 * cpu_io_multi) then
\r
328 --set sprite attr=01
\r
329 io_out(16#2004#, 16#01#);
\r
330 elsif (spr_step_cnt = 12 * cpu_io_multi) then
\r
331 --set sprite data: x=33
\r
332 io_out(16#2004#, 16#21#);
\r
334 elsif (spr_step_cnt = 13 * cpu_io_multi) then
\r
335 --set sprite data: y=61
\r
336 io_out(16#2004#, 16#3d#);
\r
337 elsif (spr_step_cnt = 14 * cpu_io_multi) then
\r
338 --tile=0x4d (ascii 'Q')
\r
339 io_out(16#2004#, 16#51#);
\r
340 elsif (spr_step_cnt = 15 * cpu_io_multi) then
\r
341 --set sprite attr=02
\r
342 io_out(16#2004#, 16#02#);
\r
343 elsif (spr_step_cnt = 16 * cpu_io_multi) then
\r
344 --set sprite data: x=45
\r
345 io_out(16#2004#, 45);
\r
349 if (spr_step_cnt > 8 * cpu_io_multi) then
\r
350 global_step_cnt := global_step_cnt + 2;
\r
353 spr_step_cnt := spr_step_cnt + 1;
\r
355 elsif (global_step_cnt = 4) then
\r
357 -- for i in 0 to 64 loop
\r
359 -- if (ch = 16#5b#) then
\r
362 -- ch := 16#41# + i;
\r
365 -- --if (i < 64) then
\r
366 -- if (i < 10) then
\r
367 -- --set dma value on the ram.
\r
368 -- if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
369 -- io_out(16#0200# + j, i);
\r
370 -- elsif (dma_step_cnt = (1 + j) * cpu_io_multi) then
\r
371 -- io_out(16#0201# + j, ch);
\r
372 -- elsif (dma_step_cnt = (2 + j) * cpu_io_multi) then
\r
373 -- io_out(16#0202# + j, 16#01#);
\r
374 -- elsif (dma_step_cnt = (3 + j) * cpu_io_multi) then
\r
375 -- io_out(16#0203# + j, j);
\r
376 -- elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) or
\r
377 -- (dma_step_cnt = (1 + j) * cpu_io_multi + 1) or
\r
378 -- (dma_step_cnt = (2 + j) * cpu_io_multi + 1) or
\r
379 -- (dma_step_cnt = (3 + j) * cpu_io_multi + 1) then
\r
383 -- if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
385 -- io_out(16#4014#, 16#02#);
\r
386 -- elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) then
\r
388 -- elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 2) then
\r
390 global_step_cnt := global_step_cnt + 1;
\r
395 -- dma_step_cnt := dma_step_cnt + 1;
\r
397 elsif (global_step_cnt = 5) then
\r
398 --step4 = scroll test and ppu reg read test.
\r
399 if (scl_step_cnt = 0) then
\r
401 --io_out(16#2005#, 11);
\r
402 io_out(16#2005#, 0);
\r
403 elsif (scl_step_cnt = 1 * cpu_io_multi) then
\r
405 io_out(16#2005#, 0);
\r
407 elsif (scl_step_cnt = 2 * cpu_io_multi) then
\r
411 elsif (scl_step_cnt = 3 * cpu_io_multi) then
\r
413 io_out(16#2006#, 16#20#);
\r
415 elsif (scl_step_cnt = 4 * cpu_io_multi) then
\r
416 io_out(16#2006#, 16#03#);
\r
418 elsif (scl_step_cnt = 5 * cpu_io_multi) then
\r
421 elsif (scl_step_cnt = 6 * cpu_io_multi) then
\r
423 io_out(16#2006#, 16#04#);
\r
425 elsif (scl_step_cnt = 7 * cpu_io_multi) then
\r
426 io_out(16#2006#, 16#d1#);
\r
428 elsif (scl_step_cnt = 8 * cpu_io_multi) then
\r
431 elsif (scl_step_cnt = 9 * cpu_io_multi) then
\r
433 io_out(16#2006#, 16#23#);
\r
435 elsif (scl_step_cnt = 10 * cpu_io_multi) then
\r
436 io_out(16#2006#, 16#c0#);
\r
438 elsif (scl_step_cnt = 11 * cpu_io_multi) then
\r
440 io_out(16#2007#, 16#5a#);
\r
442 elsif (scl_step_cnt = 12 * cpu_io_multi) then
\r
443 io_out(16#2006#, 16#23#);
\r
445 elsif (scl_step_cnt = 13 * cpu_io_multi) then
\r
446 io_out(16#2006#, 16#c0#);
\r
448 elsif (scl_step_cnt = 14 * cpu_io_multi) then
\r
451 elsif (scl_step_cnt = 15 * cpu_io_multi) then
\r
452 --read palette tbl.
\r
453 io_out(16#2006#, 16#3f#);
\r
455 elsif (scl_step_cnt = 16 * cpu_io_multi) then
\r
456 io_out(16#2006#, 16#11#);
\r
458 elsif (scl_step_cnt = 17 * cpu_io_multi) then
\r
463 if (scl_step_cnt > 17 * cpu_io_multi) then
\r
464 global_step_cnt := global_step_cnt + 1;
\r
467 scl_step_cnt := scl_step_cnt + 1;
\r
469 elsif (global_step_cnt = 6) then
\r
470 --final step = enable ppu.
\r
471 if (enable_ppu_step_cnt = 0 * cpu_io_multi) then
\r
473 --PPUMASK=1e (show bg and sprite)
\r
474 --PPUMASK=0e (show bg only)
\r
475 io_out(16#2001#, 16#1e#);
\r
476 elsif (enable_ppu_step_cnt = 1 * cpu_io_multi) then
\r
479 io_out(16#2000#, 16#80#);
\r
482 if (enable_ppu_step_cnt > 1 * cpu_io_multi) then
\r
483 --skip nmi test at this momemnt..
\r
484 global_step_cnt := global_step_cnt + 3;
\r
487 enable_ppu_step_cnt := enable_ppu_step_cnt + 1;
\r
489 elsif (global_step_cnt = 7) then
\r
491 if (pi_nmi_n = '0') then
\r
493 if (nmi_step_cnt = 0 * cpu_io_multi) then
\r
494 --set sprite addr=00 (first sprite)
\r
495 io_out(16#2003#, 16#03#);
\r
496 elsif (nmi_step_cnt = 1 * cpu_io_multi) then
\r
497 --set sprite data: x=100
\r
498 io_out(16#2004#, nmi_oam_x);
\r
499 elsif (nmi_step_cnt = 2 * cpu_io_multi) then
\r
501 -- io_out(16#2005#, nmi_scl_y);
\r
503 elsif (nmi_step_cnt = 3 * cpu_io_multi) then
\r
505 -- io_out(16#2005#, nmi_scl_y);
\r
507 elsif (nmi_step_cnt = 4 * cpu_io_multi) then
\r
508 --set sprite addr=00 (first sprite)
\r
509 io_out(16#2003#, 16#04#);
\r
510 elsif (nmi_step_cnt = 5 * cpu_io_multi) then
\r
511 --set sprite data: x=100
\r
512 io_out(16#2004#, nmi_oam_x);
\r
514 if (ref_cnt = 0) then
\r
515 nmi_oam_x := nmi_oam_x + 1;
\r
517 if (nmi_step_cnt mod 10 = 0) then
\r
518 nmi_scl_y := nmi_scl_y + 1;
\r
521 if (nmi_step_cnt > 5 * cpu_io_multi) then
\r
522 ref_cnt := ref_cnt + 1;
\r
523 global_step_cnt := global_step_cnt + 1;
\r
526 nmi_step_cnt := nmi_step_cnt + 1;
\r
528 elsif (global_step_cnt = 8) then
\r
529 ----back to nmi tests.....
\r
530 if (pi_nmi_n = '1') then
\r
532 global_step_cnt := global_step_cnt - 1;
\r
540 end if;--if (init_done = '0') then
\r
543 reg_addr <= (others => 'Z');
\r
544 reg_d_out <= (others => 'Z');
\r
545 end if;--if (rdy = '1') then
\r
546 end if;--if (pi_cpu_en(0) = '1') then
\r
547 end if; --if (rst_n = '0') then
\r