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, scl_step_cnt,
\r
52 enable_ppu_step_cnt, nmi_step_cnt : integer;
\r
53 variable init_done : std_logic;
\r
54 variable global_step_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
58 variable nmi_oam_x : integer range 0 to 255;
\r
59 variable nmi_scl_y : integer range 0 to 255;
\r
61 variable ref_cnt : integer range 0 to 120;
\r
63 procedure io_out (ad: in integer; dt : in integer) is
\r
66 addr <= conv_std_logic_vector(ad, 16);
\r
67 d_io <= conv_std_logic_vector(dt, 8);
\r
71 addr <= (others => 'Z');
\r
72 d_io <= (others => 'Z');
\r
77 if (rst_n = '0') then
\r
80 addr <= (others => 'Z');
\r
81 d_io <= (others => 'Z');
\r
84 global_step_cnt := 0;
\r
91 enable_ppu_step_cnt := 0;
\r
97 elsif (rising_edge(input_clk)) then
\r
100 if (init_done = '0') then
\r
101 if (global_step_cnt = 0) then
\r
102 --step0.0 = init ppu.
\r
103 if (init_step_cnt = 0 * cpu_io_multi) then
\r
105 io_out(16#2000#, 16#00#);
\r
106 elsif (init_step_cnt = 1 * cpu_io_multi) then
\r
108 io_out(16#2001#, 16#00#);
\r
111 if (init_step_cnt > 2 * cpu_io_multi) then
\r
112 global_step_cnt := global_step_cnt + 1;
\r
115 init_step_cnt := init_step_cnt + 1;
\r
116 elsif (global_step_cnt = 1) then
\r
117 --step0.1 = palette set.
\r
120 -- .byte $0f, $00, $10, $20
\r
121 -- .byte $0f, $04, $14, $24
\r
122 -- .byte $0f, $08, $18, $28
\r
123 -- .byte $0f, $0c, $1c, $2c
\r
125 -- .byte $0f, $00, $10, $20
\r
126 -- .byte $0f, $06, $16, $26
\r
127 -- .byte $0f, $08, $18, $28
\r
128 -- .byte $0f, $0a, $1a, $2a
\r
131 if (plt_step_cnt = 0 * cpu_io_multi) then
\r
132 --set vram addr 3f00
\r
133 io_out(16#2006#, 16#3f#);
\r
134 elsif (plt_step_cnt = 1 * cpu_io_multi) then
\r
135 io_out(16#2006#, 16#00#);
\r
137 elsif (plt_step_cnt = 2 * cpu_io_multi) then
\r
138 --set palette bg data
\r
139 io_out(16#2007#, 16#11#);
\r
140 elsif (plt_step_cnt = 3 * cpu_io_multi) then
\r
141 io_out(16#2007#, 16#01#);
\r
142 elsif (plt_step_cnt = 4 * cpu_io_multi) then
\r
143 io_out(16#2007#, 16#03#);
\r
144 elsif (plt_step_cnt = 5 * cpu_io_multi) then
\r
145 io_out(16#2007#, 16#13#);
\r
147 elsif (plt_step_cnt = 6 * cpu_io_multi) then
\r
148 io_out(16#2007#, 16#0f#);
\r
149 elsif (plt_step_cnt = 7 * cpu_io_multi) then
\r
150 io_out(16#2007#, 16#04#);
\r
151 elsif (plt_step_cnt = 8 * cpu_io_multi) then
\r
152 io_out(16#2007#, 16#14#);
\r
153 elsif (plt_step_cnt = 9 * cpu_io_multi) then
\r
154 io_out(16#2007#, 16#24#);
\r
156 elsif (plt_step_cnt = 10 * cpu_io_multi) then
\r
157 io_out(16#2007#, 16#0f#);
\r
158 elsif (plt_step_cnt = 11 * cpu_io_multi) then
\r
159 io_out(16#2007#, 16#08#);
\r
160 elsif (plt_step_cnt = 12 * cpu_io_multi) then
\r
161 io_out(16#2007#, 16#18#);
\r
162 elsif (plt_step_cnt = 13 * cpu_io_multi) then
\r
163 io_out(16#2007#, 16#28#);
\r
165 elsif (plt_step_cnt = 14 * cpu_io_multi) then
\r
166 io_out(16#2007#, 16#05#);
\r
167 elsif (plt_step_cnt = 15 * cpu_io_multi) then
\r
168 io_out(16#2007#, 16#0c#);
\r
169 elsif (plt_step_cnt = 16 * cpu_io_multi) then
\r
170 io_out(16#2007#, 16#1c#);
\r
171 elsif (plt_step_cnt = 17 * cpu_io_multi) then
\r
172 io_out(16#2007#, 16#2c#);
\r
174 elsif (plt_step_cnt = 18 * cpu_io_multi) then
\r
175 --below is sprite pallete
\r
176 io_out(16#2007#, 16#00#);
\r
177 elsif (plt_step_cnt = 19 * cpu_io_multi) then
\r
178 io_out(16#2007#, 16#24#);
\r
179 elsif (plt_step_cnt = 20 * cpu_io_multi) then
\r
180 io_out(16#2007#, 16#1b#);
\r
181 elsif (plt_step_cnt = 21 * cpu_io_multi) then
\r
182 io_out(16#2007#, 16#11#);
\r
184 elsif (plt_step_cnt = 22 * cpu_io_multi) then
\r
185 io_out(16#2007#, 16#00#);
\r
186 elsif (plt_step_cnt = 23 * cpu_io_multi) then
\r
187 io_out(16#2007#, 16#32#);
\r
188 elsif (plt_step_cnt = 24 * cpu_io_multi) then
\r
189 io_out(16#2007#, 16#16#);
\r
190 elsif (plt_step_cnt = 25 * cpu_io_multi) then
\r
191 io_out(16#2007#, 16#20#);
\r
193 elsif (plt_step_cnt = 26 * cpu_io_multi) then
\r
194 io_out(16#2007#, 16#00#);
\r
195 elsif (plt_step_cnt = 27 * cpu_io_multi) then
\r
196 io_out(16#2007#, 16#26#);
\r
197 elsif (plt_step_cnt = 28 * cpu_io_multi) then
\r
198 io_out(16#2007#, 16#01#);
\r
199 elsif (plt_step_cnt = 29 * cpu_io_multi) then
\r
200 io_out(16#2007#, 16#31#);
\r
204 if (plt_step_cnt > 30 * cpu_io_multi) then
\r
205 global_step_cnt := global_step_cnt + 1;
\r
208 plt_step_cnt := plt_step_cnt + 1;
\r
210 elsif (global_step_cnt = 2) then
\r
211 --step1 = name table set.
\r
212 if (nt_step_cnt = 0 * cpu_io_multi) then
\r
213 --set vram addr 2005 (first row, 6th col)
\r
214 io_out(16#2006#, 16#20#);
\r
215 elsif (nt_step_cnt = 1 * cpu_io_multi) then
\r
216 io_out(16#2006#, 16#03#);
\r
217 elsif (nt_step_cnt = 2 * cpu_io_multi) then
\r
218 --set name tbl data
\r
219 --0x44, 45, 45 = DEE
\r
220 io_out(16#2007#, 16#44#);
\r
221 elsif (nt_step_cnt = 3 * cpu_io_multi) then
\r
222 io_out(16#2007#, 16#45#);
\r
223 elsif (nt_step_cnt = 4 * cpu_io_multi) then
\r
224 io_out(16#2007#, 16#45#);
\r
227 elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
228 io_out(16#2006#, 16#20#);
\r
229 elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
230 io_out(16#2006#, 16#2a#);
\r
231 elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
232 io_out(16#2007#, 16#44#);
\r
234 elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
235 io_out(16#2006#, 16#24#);
\r
236 elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
237 io_out(16#2006#, 16#43#);
\r
238 elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
239 io_out(16#2007#, 16#6d#);
\r
240 elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
241 io_out(16#2007#, 16#6f#);
\r
242 elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
243 io_out(16#2007#, 16#74#);
\r
244 elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
245 io_out(16#2007#, 16#6f#);
\r
247 elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
248 io_out(16#2006#, 16#2e#);
\r
249 elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
250 io_out(16#2006#, 16#93#);
\r
251 elsif (nt_step_cnt = 16 * cpu_io_multi) then
\r
252 io_out(16#2007#, 16#59#);
\r
254 elsif (nt_step_cnt = 17 * cpu_io_multi) then
\r
255 io_out(16#2007#, 16#00#);
\r
259 if (nt_step_cnt > 4 * cpu_io_multi) then
\r
260 global_step_cnt := global_step_cnt + 1;
\r
264 nt_step_cnt := nt_step_cnt + 1;
\r
266 elsif (global_step_cnt = 3) then
\r
267 --step2 = sprite set.
\r
268 if (spr_step_cnt = 0) then
\r
269 --set sprite addr=00 (first sprite)
\r
270 io_out(16#2003#, 16#00#);
\r
271 elsif (spr_step_cnt = 1 * cpu_io_multi) then
\r
272 --set sprite data: y=02
\r
273 io_out(16#2004#, 16#01#);
\r
274 elsif (spr_step_cnt = 2 * cpu_io_multi) then
\r
275 --tile=0x4d (ascii 'M')
\r
276 io_out(16#2004#, 16#4d#);
\r
277 elsif (spr_step_cnt = 3 * cpu_io_multi) then
\r
278 --set sprite attr=03 (palette 03)
\r
279 io_out(16#2004#, 16#01#);
\r
280 elsif (spr_step_cnt = 4 * cpu_io_multi) then
\r
281 --set sprite data: x=100
\r
282 io_out(16#2004#, 16#64#);
\r
284 elsif (spr_step_cnt = 5 * cpu_io_multi) then
\r
285 --set sprite data: y=50
\r
286 io_out(16#2004#, 8);
\r
287 elsif (spr_step_cnt = 6 * cpu_io_multi) then
\r
288 --tile=0x4d (ascii 'O')
\r
289 io_out(16#2004#, 16#4f#);
\r
290 elsif (spr_step_cnt = 7 * cpu_io_multi) then
\r
291 --set sprite attr=01
\r
292 io_out(16#2004#, 16#01#);
\r
293 elsif (spr_step_cnt = 8 * cpu_io_multi) then
\r
294 --set sprite data: x=30
\r
295 io_out(16#2004#, 16#1e#);
\r
297 elsif (spr_step_cnt = 9 * cpu_io_multi) then
\r
298 --set sprite data: y=60
\r
299 io_out(16#2004#, 60);
\r
300 elsif (spr_step_cnt = 10 * cpu_io_multi) then
\r
301 --tile=0x4d (ascii 'P')
\r
302 io_out(16#2004#, 16#50#);
\r
303 elsif (spr_step_cnt = 11 * cpu_io_multi) then
\r
304 --set sprite attr=01
\r
305 io_out(16#2004#, 16#01#);
\r
306 elsif (spr_step_cnt = 12 * cpu_io_multi) then
\r
307 --set sprite data: x=33
\r
308 io_out(16#2004#, 16#21#);
\r
310 elsif (spr_step_cnt = 13 * cpu_io_multi) then
\r
311 --set sprite data: y=61
\r
312 io_out(16#2004#, 16#3d#);
\r
313 elsif (spr_step_cnt = 14 * cpu_io_multi) then
\r
314 --tile=0x4d (ascii 'Q')
\r
315 io_out(16#2004#, 16#51#);
\r
316 elsif (spr_step_cnt = 15 * cpu_io_multi) then
\r
317 --set sprite attr=02
\r
318 io_out(16#2004#, 16#02#);
\r
319 elsif (spr_step_cnt = 16 * cpu_io_multi) then
\r
320 --set sprite data: x=45
\r
321 io_out(16#2004#, 45);
\r
325 if (spr_step_cnt > 8 * cpu_io_multi) then
\r
326 global_step_cnt := global_step_cnt + 2;
\r
329 spr_step_cnt := spr_step_cnt + 1;
\r
331 elsif (global_step_cnt = 4) then
\r
333 for i in 0 to 64 loop
\r
335 if (ch = 16#5b#) then
\r
343 --set dma value on the ram.
\r
344 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
345 io_out(16#0200# + j, i);
\r
346 elsif (dma_step_cnt = (1 + j) * cpu_io_multi) then
\r
347 io_out(16#0201# + j, ch);
\r
348 elsif (dma_step_cnt = (2 + j) * cpu_io_multi) then
\r
349 io_out(16#0202# + j, 16#01#);
\r
350 elsif (dma_step_cnt = (3 + j) * cpu_io_multi) then
\r
351 io_out(16#0203# + j, j);
\r
352 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) or
\r
353 (dma_step_cnt = (1 + j) * cpu_io_multi + 1) or
\r
354 (dma_step_cnt = (2 + j) * cpu_io_multi + 1) or
\r
355 (dma_step_cnt = (3 + j) * cpu_io_multi + 1) then
\r
359 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
361 io_out(16#4014#, 16#02#);
\r
362 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) then
\r
364 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 2) then
\r
366 global_step_cnt := global_step_cnt + 1;
\r
371 dma_step_cnt := dma_step_cnt + 1;
\r
373 elsif (global_step_cnt = 5) then
\r
374 --step4 = scroll test.
\r
375 if (scl_step_cnt = 0) then
\r
377 io_out(16#2005#, 0);
\r
378 elsif (scl_step_cnt = 1 * cpu_io_multi) then
\r
380 io_out(16#2005#, 0);
\r
384 if (scl_step_cnt > 1 * cpu_io_multi) then
\r
385 global_step_cnt := global_step_cnt + 1;
\r
388 scl_step_cnt := scl_step_cnt + 1;
\r
390 elsif (global_step_cnt = 6) then
\r
391 --final step = enable ppu.
\r
392 if (enable_ppu_step_cnt = 0 * cpu_io_multi) then
\r
394 --PPUMASK=1e (show bg and sprite)
\r
395 --PPUMASK=0e (show bg only)
\r
396 io_out(16#2001#, 16#1e#);
\r
397 elsif (enable_ppu_step_cnt = 1 * cpu_io_multi) then
\r
400 io_out(16#2000#, 16#80#);
\r
403 if (enable_ppu_step_cnt > 1 * cpu_io_multi) then
\r
404 global_step_cnt := global_step_cnt + 1;
\r
407 enable_ppu_step_cnt := enable_ppu_step_cnt + 1;
\r
409 elsif (global_step_cnt = 7) then
\r
411 if (nmi_n = '0') then
\r
413 if (nmi_step_cnt = 0 * cpu_io_multi) then
\r
414 --set sprite addr=00 (first sprite)
\r
415 io_out(16#2003#, 16#03#);
\r
416 elsif (nmi_step_cnt = 1 * cpu_io_multi) then
\r
417 --set sprite data: x=100
\r
418 io_out(16#2004#, nmi_oam_x);
\r
419 elsif (nmi_step_cnt = 2 * cpu_io_multi) then
\r
421 -- io_out(16#2005#, nmi_scl_y);
\r
423 elsif (nmi_step_cnt = 3 * cpu_io_multi) then
\r
425 -- io_out(16#2005#, nmi_scl_y);
\r
428 if (ref_cnt = 0) then
\r
429 nmi_oam_x := nmi_oam_x + 1;
\r
431 if (nmi_step_cnt mod 10 = 0) then
\r
432 nmi_scl_y := nmi_scl_y + 1;
\r
435 if (nmi_step_cnt > 3 * cpu_io_multi) then
\r
436 ref_cnt := ref_cnt + 1;
\r
437 global_step_cnt := global_step_cnt + 1;
\r
440 nmi_step_cnt := nmi_step_cnt + 1;
\r
442 elsif (global_step_cnt = 8) then
\r
443 ----back to nmi tests.....
\r
444 if (nmi_n = '1') then
\r
446 global_step_cnt := global_step_cnt - 1;
\r
452 end if;--if (init_done = '0') then
\r
455 addr <= (others => 'Z');
\r
456 d_io <= (others => 'Z');
\r
457 end if;--if (rdy = '1') then
\r
458 end if; --if (rst_n = '0') then
\r
468 -----------dummy prg rom
\r
470 use ieee.std_logic_1164.all;
\r
472 generic (abus_size : integer := 15; dbus_size : integer := 8);
\r
474 clk : in std_logic;
\r
475 ce_n : in std_logic; --active low.
\r
476 addr : in std_logic_vector (abus_size - 1 downto 0);
\r
477 data : out std_logic_vector (dbus_size - 1 downto 0)
\r
480 architecture rtl of prg_rom is
\r
482 data <= (others => 'Z');
\r