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 cpu_clk : in std_logic; --phi0 input pin.
\r
26 dl_cpu_clk : in std_logic; --phi1 delayed clock.
\r
28 rst_n : in std_logic;
\r
29 irq_n : in std_logic;
\r
30 nmi_n : in std_logic;
\r
31 r_nw : out std_logic;
\r
32 addr : out std_logic_vector ( asize - 1 downto 0);
\r
33 d_io : inout std_logic_vector ( dsize - 1 downto 0)
\r
37 architecture rtl of mos6502 is
\r
43 set_ppu_p : process (cpu_clk, rst_n)
\r
44 use ieee.std_logic_arith.conv_std_logic_vector;
\r
46 variable init_step_cnt, plt_step_cnt,
\r
47 nt_step_cnt, spr_step_cnt, dma_step_cnt, scl_step_cnt,
\r
48 enable_ppu_step_cnt, nmi_step_cnt : integer;
\r
49 variable init_done : std_logic;
\r
50 variable global_step_cnt : integer;
\r
51 constant cpu_io_multi : integer := 3; --io happens every 4 cpu cycle.
\r
52 variable i, j : integer;
\r
53 variable ch : integer := 16#41# ;
\r
54 variable nmi_oam_x : integer range 0 to 255;
\r
55 variable nmi_scl_y : integer range 0 to 255;
\r
57 variable ref_cnt : integer range 0 to 120;
\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
87 enable_ppu_step_cnt := 0;
\r
93 elsif (rising_edge(cpu_clk)) then
\r
96 if (init_done = '0') then
\r
97 if (global_step_cnt = 0) then
\r
98 --step0.0 = init ppu.
\r
99 if (init_step_cnt = 0 * cpu_io_multi) then
\r
100 --PPUCTRL=01 for test...
\r
101 io_out(16#2000#, 16#01#);
\r
102 elsif (init_step_cnt = 1 * cpu_io_multi) then
\r
104 io_out(16#2001#, 16#02#);
\r
105 elsif (init_step_cnt = 2 * cpu_io_multi) then
\r
107 io_out(16#2000#, 16#00#);
\r
108 elsif (init_step_cnt = 3 * cpu_io_multi) then
\r
110 io_out(16#2001#, 16#00#);
\r
111 elsif (init_step_cnt = 4 * cpu_io_multi) then
\r
113 io_out(16#2006#, 16#02#);
\r
114 elsif (init_step_cnt = 5 * cpu_io_multi) then
\r
115 io_out(16#2006#, 16#40#);
\r
116 elsif (init_step_cnt = 6 * cpu_io_multi) then
\r
118 io_out(16#2005#, 16#21#);
\r
119 elsif (init_step_cnt = 7 * cpu_io_multi) then
\r
120 io_out(16#2005#, 16#5#);
\r
123 if (init_step_cnt > 8 * cpu_io_multi) then
\r
124 global_step_cnt := global_step_cnt + 1;
\r
127 init_step_cnt := init_step_cnt + 1;
\r
128 elsif (global_step_cnt = 1) then
\r
129 --step0.1 = palette set.
\r
132 -- .byte $0f, $00, $10, $20
\r
133 -- .byte $0f, $04, $14, $24
\r
134 -- .byte $0f, $08, $18, $28
\r
135 -- .byte $0f, $0c, $1c, $2c
\r
137 -- .byte $0f, $00, $10, $20
\r
138 -- .byte $0f, $06, $16, $26
\r
139 -- .byte $0f, $08, $18, $28
\r
140 -- .byte $0f, $0a, $1a, $2a
\r
143 if (plt_step_cnt = 0 * cpu_io_multi) then
\r
144 --set vram addr 3f00
\r
145 io_out(16#2006#, 16#3f#);
\r
146 elsif (plt_step_cnt = 1 * cpu_io_multi) then
\r
147 io_out(16#2006#, 16#00#);
\r
149 elsif (plt_step_cnt = 2 * cpu_io_multi) then
\r
150 --set palette bg data
\r
151 io_out(16#2007#, 16#11#);
\r
152 elsif (plt_step_cnt = 3 * cpu_io_multi) then
\r
153 io_out(16#2007#, 16#01#);
\r
154 elsif (plt_step_cnt = 4 * cpu_io_multi) then
\r
155 io_out(16#2007#, 16#03#);
\r
156 elsif (plt_step_cnt = 5 * cpu_io_multi) then
\r
157 io_out(16#2007#, 16#13#);
\r
159 elsif (plt_step_cnt = 6 * cpu_io_multi) then
\r
160 io_out(16#2007#, 16#0f#);
\r
161 elsif (plt_step_cnt = 7 * cpu_io_multi) then
\r
162 io_out(16#2007#, 16#04#);
\r
163 elsif (plt_step_cnt = 8 * cpu_io_multi) then
\r
164 io_out(16#2007#, 16#14#);
\r
165 elsif (plt_step_cnt = 9 * cpu_io_multi) then
\r
166 io_out(16#2007#, 16#24#);
\r
168 elsif (plt_step_cnt = 10 * cpu_io_multi) then
\r
169 io_out(16#2007#, 16#0f#);
\r
170 elsif (plt_step_cnt = 11 * cpu_io_multi) then
\r
171 io_out(16#2007#, 16#08#);
\r
172 elsif (plt_step_cnt = 12 * cpu_io_multi) then
\r
173 io_out(16#2007#, 16#18#);
\r
174 elsif (plt_step_cnt = 13 * cpu_io_multi) then
\r
175 io_out(16#2007#, 16#28#);
\r
177 elsif (plt_step_cnt = 14 * cpu_io_multi) then
\r
178 io_out(16#2007#, 16#05#);
\r
179 elsif (plt_step_cnt = 15 * cpu_io_multi) then
\r
180 io_out(16#2007#, 16#0c#);
\r
181 elsif (plt_step_cnt = 16 * cpu_io_multi) then
\r
182 io_out(16#2007#, 16#1c#);
\r
183 elsif (plt_step_cnt = 17 * cpu_io_multi) then
\r
184 io_out(16#2007#, 16#2c#);
\r
186 elsif (plt_step_cnt = 18 * cpu_io_multi) then
\r
187 --below is sprite pallete
\r
188 io_out(16#2007#, 16#00#);
\r
189 elsif (plt_step_cnt = 19 * cpu_io_multi) then
\r
190 io_out(16#2007#, 16#24#);
\r
191 elsif (plt_step_cnt = 20 * cpu_io_multi) then
\r
192 io_out(16#2007#, 16#1b#);
\r
193 elsif (plt_step_cnt = 21 * cpu_io_multi) then
\r
194 io_out(16#2007#, 16#11#);
\r
196 elsif (plt_step_cnt = 22 * cpu_io_multi) then
\r
197 io_out(16#2007#, 16#00#);
\r
198 elsif (plt_step_cnt = 23 * cpu_io_multi) then
\r
199 io_out(16#2007#, 16#32#);
\r
200 elsif (plt_step_cnt = 24 * cpu_io_multi) then
\r
201 io_out(16#2007#, 16#16#);
\r
202 elsif (plt_step_cnt = 25 * cpu_io_multi) then
\r
203 io_out(16#2007#, 16#20#);
\r
205 elsif (plt_step_cnt = 26 * cpu_io_multi) then
\r
206 io_out(16#2007#, 16#00#);
\r
207 elsif (plt_step_cnt = 27 * cpu_io_multi) then
\r
208 io_out(16#2007#, 16#26#);
\r
209 elsif (plt_step_cnt = 28 * cpu_io_multi) then
\r
210 io_out(16#2007#, 16#01#);
\r
211 elsif (plt_step_cnt = 29 * cpu_io_multi) then
\r
212 io_out(16#2007#, 16#31#);
\r
216 if (plt_step_cnt > 30 * cpu_io_multi) then
\r
217 global_step_cnt := global_step_cnt + 1;
\r
220 plt_step_cnt := plt_step_cnt + 1;
\r
222 elsif (global_step_cnt = 2) then
\r
223 --step1 = name table set.
\r
224 if (nt_step_cnt = 0 * cpu_io_multi) then
\r
225 --set vram addr 2005 (first row, 6th col)
\r
226 io_out(16#2006#, 16#20#);
\r
227 elsif (nt_step_cnt = 1 * cpu_io_multi) then
\r
228 io_out(16#2006#, 16#03#);
\r
229 elsif (nt_step_cnt = 2 * cpu_io_multi) then
\r
230 --set name tbl data
\r
231 --0x44, 45, 45 = DEE
\r
232 io_out(16#2007#, 16#44#);
\r
233 elsif (nt_step_cnt = 3 * cpu_io_multi) then
\r
234 io_out(16#2007#, 16#45#);
\r
235 elsif (nt_step_cnt = 4 * cpu_io_multi) then
\r
236 io_out(16#2007#, 16#45#);
\r
239 elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
240 io_out(16#2006#, 16#20#);
\r
241 elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
242 io_out(16#2006#, 16#2a#);
\r
243 elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
244 io_out(16#2007#, 16#44#);
\r
246 elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
247 io_out(16#2006#, 16#24#);
\r
248 elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
249 io_out(16#2006#, 16#43#);
\r
250 elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
251 io_out(16#2007#, 16#6d#);
\r
252 elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
253 io_out(16#2007#, 16#6f#);
\r
254 elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
255 io_out(16#2007#, 16#74#);
\r
256 elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
257 io_out(16#2007#, 16#6f#);
\r
259 elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
260 io_out(16#2006#, 16#2e#);
\r
261 elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
262 io_out(16#2006#, 16#93#);
\r
263 elsif (nt_step_cnt = 16 * cpu_io_multi) then
\r
264 io_out(16#2007#, 16#59#);
\r
266 elsif (nt_step_cnt = 17 * cpu_io_multi) then
\r
267 io_out(16#2007#, 16#00#);
\r
271 if (nt_step_cnt > 4 * cpu_io_multi) then
\r
272 global_step_cnt := global_step_cnt + 1;
\r
276 nt_step_cnt := nt_step_cnt + 1;
\r
278 elsif (global_step_cnt = 3) then
\r
279 --step2 = sprite set.
\r
280 if (spr_step_cnt = 0) then
\r
281 --set sprite addr=00 (first sprite)
\r
282 io_out(16#2003#, 16#00#);
\r
283 elsif (spr_step_cnt = 1 * cpu_io_multi) then
\r
284 --set sprite data: y=02
\r
285 io_out(16#2004#, 16#01#);
\r
286 elsif (spr_step_cnt = 2 * cpu_io_multi) then
\r
287 --tile=0x4d (ascii 'M')
\r
288 io_out(16#2004#, 16#4d#);
\r
289 elsif (spr_step_cnt = 3 * cpu_io_multi) then
\r
290 --set sprite attr=03 (palette 03)
\r
291 io_out(16#2004#, 16#01#);
\r
292 elsif (spr_step_cnt = 4 * cpu_io_multi) then
\r
293 --set sprite data: x=100
\r
294 io_out(16#2004#, 16#64#);
\r
296 elsif (spr_step_cnt = 5 * cpu_io_multi) then
\r
297 --set sprite data: y=50
\r
298 io_out(16#2004#, 8);
\r
299 elsif (spr_step_cnt = 6 * cpu_io_multi) then
\r
300 --tile=0x4d (ascii 'O')
\r
301 io_out(16#2004#, 16#4f#);
\r
302 elsif (spr_step_cnt = 7 * cpu_io_multi) then
\r
303 --set sprite attr=01
\r
304 io_out(16#2004#, 16#01#);
\r
305 elsif (spr_step_cnt = 8 * cpu_io_multi) then
\r
306 --set sprite data: x=30
\r
307 io_out(16#2004#, 16#1e#);
\r
309 elsif (spr_step_cnt = 9 * cpu_io_multi) then
\r
310 --set sprite data: y=60
\r
311 io_out(16#2004#, 60);
\r
312 elsif (spr_step_cnt = 10 * cpu_io_multi) then
\r
313 --tile=0x4d (ascii 'P')
\r
314 io_out(16#2004#, 16#50#);
\r
315 elsif (spr_step_cnt = 11 * cpu_io_multi) then
\r
316 --set sprite attr=01
\r
317 io_out(16#2004#, 16#01#);
\r
318 elsif (spr_step_cnt = 12 * cpu_io_multi) then
\r
319 --set sprite data: x=33
\r
320 io_out(16#2004#, 16#21#);
\r
322 elsif (spr_step_cnt = 13 * cpu_io_multi) then
\r
323 --set sprite data: y=61
\r
324 io_out(16#2004#, 16#3d#);
\r
325 elsif (spr_step_cnt = 14 * cpu_io_multi) then
\r
326 --tile=0x4d (ascii 'Q')
\r
327 io_out(16#2004#, 16#51#);
\r
328 elsif (spr_step_cnt = 15 * cpu_io_multi) then
\r
329 --set sprite attr=02
\r
330 io_out(16#2004#, 16#02#);
\r
331 elsif (spr_step_cnt = 16 * cpu_io_multi) then
\r
332 --set sprite data: x=45
\r
333 io_out(16#2004#, 45);
\r
337 if (spr_step_cnt > 8 * cpu_io_multi) then
\r
338 global_step_cnt := global_step_cnt + 2;
\r
341 spr_step_cnt := spr_step_cnt + 1;
\r
343 elsif (global_step_cnt = 4) then
\r
345 for i in 0 to 64 loop
\r
347 if (ch = 16#5b#) then
\r
355 --set dma value on the ram.
\r
356 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
357 io_out(16#0200# + j, i);
\r
358 elsif (dma_step_cnt = (1 + j) * cpu_io_multi) then
\r
359 io_out(16#0201# + j, ch);
\r
360 elsif (dma_step_cnt = (2 + j) * cpu_io_multi) then
\r
361 io_out(16#0202# + j, 16#01#);
\r
362 elsif (dma_step_cnt = (3 + j) * cpu_io_multi) then
\r
363 io_out(16#0203# + j, j);
\r
364 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) or
\r
365 (dma_step_cnt = (1 + j) * cpu_io_multi + 1) or
\r
366 (dma_step_cnt = (2 + j) * cpu_io_multi + 1) or
\r
367 (dma_step_cnt = (3 + j) * cpu_io_multi + 1) then
\r
371 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
373 io_out(16#4014#, 16#02#);
\r
374 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) then
\r
376 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 2) then
\r
378 global_step_cnt := global_step_cnt + 1;
\r
383 dma_step_cnt := dma_step_cnt + 1;
\r
385 elsif (global_step_cnt = 5) then
\r
386 --step4 = scroll test.
\r
387 if (scl_step_cnt = 0) then
\r
389 io_out(16#2005#, 0);
\r
390 elsif (scl_step_cnt = 1 * cpu_io_multi) then
\r
392 io_out(16#2005#, 0);
\r
396 if (scl_step_cnt > 1 * cpu_io_multi) then
\r
397 global_step_cnt := global_step_cnt + 1;
\r
400 scl_step_cnt := scl_step_cnt + 1;
\r
402 elsif (global_step_cnt = 6) then
\r
403 --final step = enable ppu.
\r
404 if (enable_ppu_step_cnt = 0 * cpu_io_multi) then
\r
406 --PPUMASK=1e (show bg and sprite)
\r
407 --PPUMASK=0e (show bg only)
\r
408 io_out(16#2001#, 16#1e#);
\r
409 elsif (enable_ppu_step_cnt = 1 * cpu_io_multi) then
\r
412 io_out(16#2000#, 16#80#);
\r
415 if (enable_ppu_step_cnt > 1 * cpu_io_multi) then
\r
416 global_step_cnt := global_step_cnt + 1;
\r
419 enable_ppu_step_cnt := enable_ppu_step_cnt + 1;
\r
421 elsif (global_step_cnt = 7) then
\r
423 if (nmi_n = '0') then
\r
425 if (nmi_step_cnt = 0 * cpu_io_multi) then
\r
426 --set sprite addr=00 (first sprite)
\r
427 io_out(16#2003#, 16#03#);
\r
428 elsif (nmi_step_cnt = 1 * cpu_io_multi) then
\r
429 --set sprite data: x=100
\r
430 io_out(16#2004#, nmi_oam_x);
\r
431 elsif (nmi_step_cnt = 2 * cpu_io_multi) then
\r
433 -- io_out(16#2005#, nmi_scl_y);
\r
435 elsif (nmi_step_cnt = 3 * cpu_io_multi) then
\r
437 -- io_out(16#2005#, nmi_scl_y);
\r
440 if (ref_cnt = 0) then
\r
441 nmi_oam_x := nmi_oam_x + 1;
\r
443 if (nmi_step_cnt mod 10 = 0) then
\r
444 nmi_scl_y := nmi_scl_y + 1;
\r
447 if (nmi_step_cnt > 3 * cpu_io_multi) then
\r
448 ref_cnt := ref_cnt + 1;
\r
449 global_step_cnt := global_step_cnt + 1;
\r
452 nmi_step_cnt := nmi_step_cnt + 1;
\r
454 elsif (global_step_cnt = 8) then
\r
455 ----back to nmi tests.....
\r
456 if (nmi_n = '1') then
\r
458 global_step_cnt := global_step_cnt - 1;
\r
464 end if;--if (init_done = '0') then
\r
467 addr <= (others => 'Z');
\r
468 d_io <= (others => 'Z');
\r
469 end if;--if (rdy = '1') then
\r
470 end if; --if (rst_n = '0') then
\r
480 -----------dummy prg rom
\r
482 use ieee.std_logic_1164.all;
\r
484 generic (abus_size : integer := 15; dbus_size : integer := 8);
\r
486 clk : in std_logic;
\r
487 ce_n : in std_logic; --active low.
\r
488 addr : in std_logic_vector (abus_size - 1 downto 0);
\r
489 data : out std_logic_vector (dbus_size - 1 downto 0)
\r
492 architecture rtl of prg_rom is
\r
494 data <= (others => 'Z');
\r