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
86 enable_ppu_step_cnt := 0;
\r
89 elsif (rising_edge(input_clk)) then
\r
90 cpu_cnt := cpu_cnt + 1;
\r
92 if (init_done = '0') then
\r
93 if (global_step_cnt = 0) then
\r
94 --step0.0 = init ppu.
\r
95 if (init_step_cnt = 0 * cpu_io_multi) then
\r
97 io_out(16#2000#, 16#00#);
\r
98 elsif (init_step_cnt = 1 * cpu_io_multi) then
\r
100 io_out(16#2001#, 16#00#);
\r
103 if (init_step_cnt > 2 * cpu_io_multi) then
\r
104 global_step_cnt := global_step_cnt + 1;
\r
107 init_step_cnt := init_step_cnt + 1;
\r
108 elsif (global_step_cnt = 1) then
\r
109 --step0.1 = palette set.
\r
112 -- .byte $0f, $00, $10, $20
\r
113 -- .byte $0f, $04, $14, $24
\r
114 -- .byte $0f, $08, $18, $28
\r
115 -- .byte $0f, $0c, $1c, $2c
\r
117 -- .byte $0f, $00, $10, $20
\r
118 -- .byte $0f, $06, $16, $26
\r
119 -- .byte $0f, $08, $18, $28
\r
120 -- .byte $0f, $0a, $1a, $2a
\r
123 if (plt_step_cnt = 0 * cpu_io_multi) then
\r
124 --set vram addr 3f00
\r
125 io_out(16#2006#, 16#3f#);
\r
126 elsif (plt_step_cnt = 1 * cpu_io_multi) then
\r
127 io_out(16#2006#, 16#00#);
\r
129 elsif (plt_step_cnt = 2 * cpu_io_multi) then
\r
130 --set palette bg data
\r
131 io_out(16#2007#, 16#11#);
\r
132 elsif (plt_step_cnt = 3 * cpu_io_multi) then
\r
133 io_out(16#2007#, 16#01#);
\r
134 elsif (plt_step_cnt = 4 * cpu_io_multi) then
\r
135 io_out(16#2007#, 16#03#);
\r
136 elsif (plt_step_cnt = 5 * cpu_io_multi) then
\r
137 io_out(16#2007#, 16#13#);
\r
139 elsif (plt_step_cnt = 6 * cpu_io_multi) then
\r
140 io_out(16#2007#, 16#0f#);
\r
141 elsif (plt_step_cnt = 7 * cpu_io_multi) then
\r
142 io_out(16#2007#, 16#04#);
\r
143 elsif (plt_step_cnt = 8 * cpu_io_multi) then
\r
144 io_out(16#2007#, 16#14#);
\r
145 elsif (plt_step_cnt = 9 * cpu_io_multi) then
\r
146 io_out(16#2007#, 16#24#);
\r
148 elsif (plt_step_cnt = 10 * cpu_io_multi) then
\r
149 io_out(16#2007#, 16#0f#);
\r
150 elsif (plt_step_cnt = 11 * cpu_io_multi) then
\r
151 io_out(16#2007#, 16#08#);
\r
152 elsif (plt_step_cnt = 12 * cpu_io_multi) then
\r
153 io_out(16#2007#, 16#18#);
\r
154 elsif (plt_step_cnt = 13 * cpu_io_multi) then
\r
155 io_out(16#2007#, 16#28#);
\r
157 elsif (plt_step_cnt = 14 * cpu_io_multi) then
\r
158 io_out(16#2007#, 16#05#);
\r
159 elsif (plt_step_cnt = 15 * cpu_io_multi) then
\r
160 io_out(16#2007#, 16#0c#);
\r
161 elsif (plt_step_cnt = 16 * cpu_io_multi) then
\r
162 io_out(16#2007#, 16#1c#);
\r
163 elsif (plt_step_cnt = 17 * cpu_io_multi) then
\r
164 io_out(16#2007#, 16#2c#);
\r
166 elsif (plt_step_cnt = 18 * cpu_io_multi) then
\r
167 --below is sprite pallete
\r
168 io_out(16#2007#, 16#00#);
\r
169 elsif (plt_step_cnt = 19 * cpu_io_multi) then
\r
170 io_out(16#2007#, 16#24#);
\r
171 elsif (plt_step_cnt = 20 * cpu_io_multi) then
\r
172 io_out(16#2007#, 16#1b#);
\r
173 elsif (plt_step_cnt = 21 * cpu_io_multi) then
\r
174 io_out(16#2007#, 16#11#);
\r
176 elsif (plt_step_cnt = 22 * cpu_io_multi) then
\r
177 io_out(16#2007#, 16#00#);
\r
178 elsif (plt_step_cnt = 23 * cpu_io_multi) then
\r
179 io_out(16#2007#, 16#32#);
\r
180 elsif (plt_step_cnt = 24 * cpu_io_multi) then
\r
181 io_out(16#2007#, 16#16#);
\r
182 elsif (plt_step_cnt = 25 * cpu_io_multi) then
\r
183 io_out(16#2007#, 16#20#);
\r
185 elsif (plt_step_cnt = 26 * cpu_io_multi) then
\r
186 io_out(16#2007#, 16#00#);
\r
187 elsif (plt_step_cnt = 27 * cpu_io_multi) then
\r
188 io_out(16#2007#, 16#26#);
\r
189 elsif (plt_step_cnt = 28 * cpu_io_multi) then
\r
190 io_out(16#2007#, 16#01#);
\r
191 elsif (plt_step_cnt = 29 * cpu_io_multi) then
\r
192 io_out(16#2007#, 16#31#);
\r
196 if (plt_step_cnt > 30 * cpu_io_multi) then
\r
197 global_step_cnt := global_step_cnt + 1;
\r
200 plt_step_cnt := plt_step_cnt + 1;
\r
202 elsif (global_step_cnt = 2) then
\r
203 --step1 = name table set.
\r
204 if (nt_step_cnt = 0 * cpu_io_multi) then
\r
205 --set vram addr 2005 (first row, 6th col)
\r
206 io_out(16#2006#, 16#20#);
\r
207 elsif (nt_step_cnt = 1 * cpu_io_multi) then
\r
208 io_out(16#2006#, 16#06#);
\r
209 elsif (nt_step_cnt = 2 * cpu_io_multi) then
\r
210 --set name tbl data
\r
211 --0x44, 45, 45 = DEE
\r
212 io_out(16#2007#, 16#44#);
\r
213 elsif (nt_step_cnt = 3 * cpu_io_multi) then
\r
214 io_out(16#2007#, 16#45#);
\r
215 elsif (nt_step_cnt = 4 * cpu_io_multi) then
\r
216 io_out(16#2007#, 16#45#);
\r
219 elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
220 --set vram addr 23c1 (attribute)
\r
221 io_out(16#2006#, 16#23#);
\r
222 elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
223 io_out(16#2006#, 16#c1#);
\r
224 elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
226 io_out(16#2007#, 16#d8#);
\r
229 elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
230 io_out(16#2006#, 16#20#);
\r
231 elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
232 io_out(16#2006#, 16#60#);
\r
234 elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
235 io_out(16#2007#, 48);
\r
236 elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
237 io_out(16#2007#, 49);
\r
238 elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
239 io_out(16#2007#, 50);
\r
240 elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
241 io_out(16#2007#, 51);
\r
242 elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
243 io_out(16#2007#, 52);
\r
244 elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
245 io_out(16#2007#, 53);
\r
246 elsif (nt_step_cnt = 16 * cpu_io_multi) then
\r
247 io_out(16#2007#, 54);
\r
248 elsif (nt_step_cnt = 17 * cpu_io_multi) then
\r
249 io_out(16#2007#, 55);
\r
250 elsif (nt_step_cnt = 18 * cpu_io_multi) then
\r
251 io_out(16#2007#, 56);
\r
253 -- elsif (nt_step_cnt = 5 * cpu_io_multi) then
\r
254 -- --set vram addr 21d1
\r
255 -- io_out(16#2006#, 16#21#);
\r
256 -- elsif (nt_step_cnt = 6 * cpu_io_multi) then
\r
257 -- io_out(16#2006#, 16#E6#);
\r
258 -- elsif (nt_step_cnt = 7 * cpu_io_multi) then
\r
259 -- --msg=DEE TEST !!!
\r
260 -- io_out(16#2007#, 16#44#);
\r
261 -- elsif (nt_step_cnt = 8 * cpu_io_multi) then
\r
262 -- io_out(16#2007#, 16#45#);
\r
263 -- elsif (nt_step_cnt = 9 * cpu_io_multi) then
\r
264 -- io_out(16#2007#, 16#45#);
\r
265 -- elsif (nt_step_cnt = 10 * cpu_io_multi) then
\r
266 -- io_out(16#2007#, 16#00#);
\r
267 -- elsif (nt_step_cnt = 11 * cpu_io_multi) then
\r
268 -- io_out(16#2007#, 16#54#);
\r
269 -- elsif (nt_step_cnt = 12 * cpu_io_multi) then
\r
270 -- io_out(16#2007#, 16#45#);
\r
271 -- elsif (nt_step_cnt = 13 * cpu_io_multi) then
\r
272 -- io_out(16#2007#, 16#53#);
\r
273 -- elsif (nt_step_cnt = 14 * cpu_io_multi) then
\r
274 -- io_out(16#2007#, 16#54#);
\r
275 -- elsif (nt_step_cnt = 15 * cpu_io_multi) then
\r
276 -- io_out(16#2007#, 16#21#);
\r
278 --display test pattern
\r
279 elsif (nt_step_cnt = 19 * cpu_io_multi) then
\r
280 io_out(16#2006#, 16#20#);
\r
281 elsif (nt_step_cnt = 20 * cpu_io_multi) then
\r
282 io_out(16#2006#, 16#20#);
\r
284 elsif (nt_step_cnt = 21 * cpu_io_multi) then
\r
285 io_out(16#2007#, 16#01#);
\r
286 elsif (nt_step_cnt = 22 * cpu_io_multi) then
\r
287 io_out(16#2007#, 16#02#);
\r
288 elsif (nt_step_cnt = 23 * cpu_io_multi) then
\r
289 io_out(16#2007#, 16#03#);
\r
290 elsif (nt_step_cnt = 24 * cpu_io_multi) then
\r
291 io_out(16#2007#, 16#04#);
\r
292 elsif (nt_step_cnt = 25 * cpu_io_multi) then
\r
293 io_out(16#2007#, 16#05#);
\r
294 elsif (nt_step_cnt = 26 * cpu_io_multi) then
\r
295 io_out(16#2007#, 16#06#);
\r
296 elsif (nt_step_cnt = 27 * cpu_io_multi) then
\r
297 io_out(16#2007#, 16#07#);
\r
298 elsif (nt_step_cnt = 28 * cpu_io_multi) then
\r
299 io_out(16#2007#, 16#08#);
\r
300 elsif (nt_step_cnt = 29 * cpu_io_multi) then
\r
301 io_out(16#2007#, 16#09#);
\r
302 elsif (nt_step_cnt = 30 * cpu_io_multi) then
\r
303 io_out(16#2007#, 16#0a#);
\r
304 elsif (nt_step_cnt = 31 * cpu_io_multi) then
\r
305 io_out(16#2007#, 16#0b#);
\r
306 elsif (nt_step_cnt = 32 * cpu_io_multi) then
\r
307 io_out(16#2007#, 16#0c#);
\r
308 elsif (nt_step_cnt = 33 * cpu_io_multi) then
\r
309 io_out(16#2007#, 16#0d#);
\r
310 elsif (nt_step_cnt = 34 * cpu_io_multi) then
\r
311 io_out(16#2007#, 16#0e#);
\r
312 elsif (nt_step_cnt = 35 * cpu_io_multi) then
\r
313 io_out(16#2007#, 16#0f#);
\r
315 elsif (nt_step_cnt = 36 * cpu_io_multi) then
\r
316 io_out(16#2006#, 16#20#);
\r
317 elsif (nt_step_cnt = 37 * cpu_io_multi) then
\r
318 io_out(16#2006#, 16#40#);
\r
320 elsif (nt_step_cnt = 38 * cpu_io_multi) then
\r
321 io_out(16#2007#, 16#10#);
\r
322 elsif (nt_step_cnt = 39 * cpu_io_multi) then
\r
323 io_out(16#2007#, 16#11#);
\r
324 elsif (nt_step_cnt = 40 * cpu_io_multi) then
\r
325 io_out(16#2007#, 16#12#);
\r
326 elsif (nt_step_cnt = 41 * cpu_io_multi) then
\r
327 io_out(16#2007#, 16#13#);
\r
328 elsif (nt_step_cnt = 42 * cpu_io_multi) then
\r
329 io_out(16#2007#, 16#14#);
\r
330 elsif (nt_step_cnt = 43 * cpu_io_multi) then
\r
331 io_out(16#2007#, 16#15#);
\r
332 elsif (nt_step_cnt = 44 * cpu_io_multi) then
\r
333 io_out(16#2007#, 16#16#);
\r
334 elsif (nt_step_cnt = 45 * cpu_io_multi) then
\r
335 io_out(16#2007#, 16#17#);
\r
336 elsif (nt_step_cnt = 46 * cpu_io_multi) then
\r
337 io_out(16#2007#, 16#18#);
\r
338 elsif (nt_step_cnt = 47 * cpu_io_multi) then
\r
339 io_out(16#2007#, 16#19#);
\r
340 elsif (nt_step_cnt = 48 * cpu_io_multi) then
\r
341 io_out(16#2007#, 16#1a#);
\r
342 elsif (nt_step_cnt = 49 * cpu_io_multi) then
\r
343 io_out(16#2007#, 16#1b#);
\r
344 elsif (nt_step_cnt = 50 * cpu_io_multi) then
\r
345 io_out(16#2007#, 16#1c#);
\r
346 elsif (nt_step_cnt = 51 * cpu_io_multi) then
\r
347 io_out(16#2007#, 16#1d#);
\r
348 elsif (nt_step_cnt = 52 * cpu_io_multi) then
\r
349 io_out(16#2007#, 16#1e#);
\r
350 elsif (nt_step_cnt = 53 * cpu_io_multi) then
\r
351 io_out(16#2007#, 16#1f#);
\r
355 if (nt_step_cnt > 4 * cpu_io_multi) then
\r
356 global_step_cnt := global_step_cnt + 1;
\r
360 nt_step_cnt := nt_step_cnt + 1;
\r
362 elsif (global_step_cnt = 3) then
\r
363 --step2 = sprite set.
\r
364 if (spr_step_cnt = 0) then
\r
365 --set sprite addr=00 (first sprite)
\r
366 io_out(16#2003#, 16#00#);
\r
367 elsif (spr_step_cnt = 1 * cpu_io_multi) then
\r
368 --set sprite data: y=02
\r
369 io_out(16#2004#, 16#13#);
\r
370 elsif (spr_step_cnt = 2 * cpu_io_multi) then
\r
371 --tile=0x4d (ascii 'M')
\r
372 io_out(16#2004#, 16#4d#);
\r
373 elsif (spr_step_cnt = 3 * cpu_io_multi) then
\r
374 --set sprite attr=03 (palette 03)
\r
375 io_out(16#2004#, 16#01#);
\r
376 elsif (spr_step_cnt = 4 * cpu_io_multi) then
\r
377 --set sprite data: x=100
\r
378 io_out(16#2004#, 16#64#);
\r
380 elsif (spr_step_cnt = 5 * cpu_io_multi) then
\r
381 --set sprite data: y=50
\r
382 io_out(16#2004#, 16#32#);
\r
383 elsif (spr_step_cnt = 6 * cpu_io_multi) then
\r
384 --tile=0x4d (ascii 'O')
\r
385 io_out(16#2004#, 16#4f#);
\r
386 elsif (spr_step_cnt = 7 * cpu_io_multi) then
\r
387 --set sprite attr=01
\r
388 io_out(16#2004#, 16#01#);
\r
389 elsif (spr_step_cnt = 8 * cpu_io_multi) then
\r
390 --set sprite data: x=30
\r
391 io_out(16#2004#, 16#1e#);
\r
393 elsif (spr_step_cnt = 9 * cpu_io_multi) then
\r
394 --set sprite data: y=60
\r
395 io_out(16#2004#, 60);
\r
396 elsif (spr_step_cnt = 10 * cpu_io_multi) then
\r
397 --tile=0x4d (ascii 'P')
\r
398 io_out(16#2004#, 16#50#);
\r
399 elsif (spr_step_cnt = 11 * cpu_io_multi) then
\r
400 --set sprite attr=01
\r
401 io_out(16#2004#, 16#01#);
\r
402 elsif (spr_step_cnt = 12 * cpu_io_multi) then
\r
403 --set sprite data: x=33
\r
404 io_out(16#2004#, 16#21#);
\r
406 elsif (spr_step_cnt = 13 * cpu_io_multi) then
\r
407 --set sprite data: y=61
\r
408 io_out(16#2004#, 16#3d#);
\r
409 elsif (spr_step_cnt = 14 * cpu_io_multi) then
\r
410 --tile=0x4d (ascii 'Q')
\r
411 io_out(16#2004#, 16#51#);
\r
412 elsif (spr_step_cnt = 15 * cpu_io_multi) then
\r
413 --set sprite attr=02
\r
414 io_out(16#2004#, 16#02#);
\r
415 elsif (spr_step_cnt = 16 * cpu_io_multi) then
\r
416 --set sprite data: x=45
\r
417 io_out(16#2004#, 45);
\r
421 if (spr_step_cnt > 4 * cpu_io_multi) then
\r
422 global_step_cnt := global_step_cnt + 1;
\r
425 spr_step_cnt := spr_step_cnt + 1;
\r
427 elsif (global_step_cnt = 4) then
\r
429 for i in 0 to 64 loop
\r
431 if (ch = 16#5b#) then
\r
438 --set dma value on the ram.
\r
439 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
440 io_out(16#0200# + j, i);
\r
441 elsif (dma_step_cnt = (1 + j) * cpu_io_multi) then
\r
442 io_out(16#0201# + j, ch);
\r
443 elsif (dma_step_cnt = (2 + j) * cpu_io_multi) then
\r
444 io_out(16#0202# + j, 16#01#);
\r
445 elsif (dma_step_cnt = (3 + j) * cpu_io_multi) then
\r
446 io_out(16#0203# + j, j);
\r
447 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) or
\r
448 (dma_step_cnt = (1 + j) * cpu_io_multi + 1) or
\r
449 (dma_step_cnt = (2 + j) * cpu_io_multi + 1) or
\r
450 (dma_step_cnt = (3 + j) * cpu_io_multi + 1) then
\r
454 if (dma_step_cnt = (0 + j) * cpu_io_multi) then
\r
456 io_out(16#4014#, 16#02#);
\r
457 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) then
\r
459 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 2) then
\r
460 global_step_cnt := global_step_cnt + 1;
\r
465 dma_step_cnt := dma_step_cnt + 1;
\r
467 elsif (global_step_cnt = 5) then
\r
468 --final step = enable ppu.
\r
469 if (enable_ppu_step_cnt = 0 * cpu_io_multi) then
\r
470 --scroll reg set x.
\r
471 io_out(16#2005#, 0);
\r
472 elsif (enable_ppu_step_cnt = 1 * cpu_io_multi) then
\r
473 --scroll reg set y.
\r
474 io_out(16#2005#, 0);
\r
475 elsif (enable_ppu_step_cnt = 2 * cpu_io_multi) then
\r
477 --PPUMASK=1e (show bg and sprite)
\r
478 --PPUMASK=0e (show bg only)
\r
479 io_out(16#2001#, 16#1e#);
\r
480 elsif (enable_ppu_step_cnt = 3 * cpu_io_multi) then
\r
483 io_out(16#2000#, 16#80#);
\r
486 if (enable_ppu_step_cnt > 4 * cpu_io_multi) then
\r
487 global_step_cnt := global_step_cnt + 1;
\r
490 enable_ppu_step_cnt := enable_ppu_step_cnt + 1;
\r
496 end if;--if (init_done = '0') then
\r
497 end if; --if (rst_n = '0') then
\r
507 -----------dummy prg rom
\r
509 use ieee.std_logic_1164.all;
\r
511 generic (abus_size : integer := 15; dbus_size : integer := 8);
\r
513 clk : in std_logic;
\r
514 ce_n : in std_logic; --active low.
\r
515 addr : in std_logic_vector (abus_size - 1 downto 0);
\r
516 data : out std_logic_vector (dbus_size - 1 downto 0)
\r
519 architecture rtl of prg_rom is
\r
521 data <= (others => 'Z');
\r