OSDN Git Service

ale bug fix..
[motonesfpga/motonesfpga.git] / de1_nes / dummy-mos6502.vhd
1 library ieee;\r
2 use ieee.std_logic_1164.all;\r
3 \r
4 entity mos6502 is \r
5     generic (   dsize : integer := 8;\r
6                 asize : integer :=16\r
7             );\r
8     port (  \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
13 \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
24 \r
25             cpu_clk     : in std_logic; --phi0 input pin.\r
26             dl_cpu_clk  : in std_logic; --phi1 delayed clock.\r
27             rdy         : in std_logic;\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
34     );\r
35 end mos6502;\r
36 \r
37 architecture rtl of mos6502 is\r
38 \r
39 \r
40 begin\r
41 \r
42     --set ppu value...\r
43     set_ppu_p : process (cpu_clk, rst_n)\r
44     use ieee.std_logic_arith.conv_std_logic_vector;\r
45 \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
56 \r
57     variable ref_cnt : integer range 0 to 120;\r
58 \r
59 procedure io_out (ad: in integer; dt : in integer) is\r
60 begin\r
61     r_nw <= '0';\r
62     addr <= conv_std_logic_vector(ad, 16);\r
63     d_io <= conv_std_logic_vector(dt, 8);\r
64 end;\r
65 procedure io_brk is\r
66 begin\r
67     addr <= (others => 'Z');\r
68     d_io <= (others => 'Z');\r
69     r_nw <= 'Z';\r
70 end;\r
71 \r
72     begin\r
73         if (rst_n = '0') then\r
74             \r
75             r_nw <= 'Z';\r
76             addr <= (others => 'Z');\r
77             d_io <= (others => 'Z');\r
78             \r
79             init_done := '0';\r
80             global_step_cnt := 0;\r
81             init_step_cnt := 0;\r
82             plt_step_cnt := 0;\r
83             nt_step_cnt := 0;\r
84             spr_step_cnt := 0;\r
85             dma_step_cnt := 0;\r
86             scl_step_cnt := 0;\r
87             enable_ppu_step_cnt := 0;\r
88             nmi_step_cnt := 0;\r
89             nmi_oam_x := 0;\r
90             nmi_scl_y := 200;\r
91             ref_cnt := 0;\r
92 \r
93         elsif (rising_edge(cpu_clk)) then\r
94 \r
95             if (rdy = '1') 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
103                             --PPUMASK=02\r
104                             io_out(16#2001#, 16#02#);\r
105                         elsif (init_step_cnt = 2 * cpu_io_multi) then\r
106                             --PPUCTRL=00\r
107                             io_out(16#2000#, 16#00#);\r
108                         elsif (init_step_cnt = 3 * cpu_io_multi) then\r
109                             --PPUMASK=00\r
110                             io_out(16#2001#, 16#00#);\r
111                         elsif (init_step_cnt = 4 * cpu_io_multi) then\r
112                             --ppuaddr\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
117                             --scroll\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
121                         else\r
122                             io_brk;\r
123                             if (init_step_cnt > 8 * cpu_io_multi) then\r
124                                 global_step_cnt := global_step_cnt + 1;\r
125                             end if;\r
126                         end if;\r
127                         init_step_cnt := init_step_cnt + 1;\r
128                     elsif (global_step_cnt = 1) then\r
129                         --step0.1 = palette set.\r
130     --palettes:\r
131     --;;;bg palette\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
136     --;;;spr palette\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
141                         \r
142                         \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
148                         \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
158 \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
167      \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
176      \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
185 \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
195 \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
204 \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
213 \r
214                         else\r
215                             io_brk;\r
216                             if (plt_step_cnt > 30 * cpu_io_multi) then\r
217                                 global_step_cnt := global_step_cnt + 1;\r
218                             end if;\r
219                         end if;\r
220                         plt_step_cnt := plt_step_cnt + 1;\r
221                         \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
237 \r
238 \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
245 \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
258                             \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
265 \r
266                         elsif (nt_step_cnt = 17 * cpu_io_multi) then\r
267                             io_out(16#2007#, 16#00#);\r
268 \r
269                         else\r
270                             io_brk;\r
271                             if (nt_step_cnt > 4 * cpu_io_multi) then\r
272                                 global_step_cnt := global_step_cnt + 1;\r
273                             end if;\r
274                         end if;\r
275                         \r
276                         nt_step_cnt := nt_step_cnt + 1;\r
277                         \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
295 \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
308 \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
321 \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
334 \r
335                         else\r
336                             io_brk;\r
337                             if (spr_step_cnt > 8 * cpu_io_multi) then\r
338                                 global_step_cnt := global_step_cnt + 2;\r
339                             end if;\r
340                         end if;\r
341                         spr_step_cnt := spr_step_cnt + 1;\r
342 \r
343                     elsif (global_step_cnt = 4) then\r
344                         --step3 = dma set.\r
345                         for i in 0 to 64 loop\r
346                             j := i * 4;\r
347                             if (ch = 16#5b#) then\r
348                                 ch := 16#41#;\r
349                             else\r
350                                 ch := 16#41# + i;\r
351                             end if;\r
352 \r
353                             --if (i < 64) then\r
354                             if (i < 10) 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
368                                     io_brk;\r
369                                 end if;\r
370                             else\r
371                                 if    (dma_step_cnt = (0 + j) * cpu_io_multi) then\r
372                                     --start dma\r
373                                     io_out(16#4014#, 16#02#);\r
374                                 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) then\r
375                                     io_brk;\r
376                                 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 2) then\r
377                                     io_brk;\r
378                                     global_step_cnt := global_step_cnt + 1;\r
379                                 end if;\r
380                             end if;\r
381                         end loop;\r
382                         \r
383                         dma_step_cnt := dma_step_cnt + 1;\r
384 \r
385                     elsif (global_step_cnt = 5) then\r
386                         --step4 = scroll test.\r
387                         if (scl_step_cnt = 0) then\r
388                             --x scroll pos=123\r
389                             io_out(16#2005#, 0);\r
390                         elsif (scl_step_cnt = 1 * cpu_io_multi) then\r
391                             --y scroll pos=100\r
392                             io_out(16#2005#, 0);\r
393 \r
394                         else\r
395                             io_brk;\r
396                             if (scl_step_cnt > 1 * cpu_io_multi) then\r
397                                 global_step_cnt := global_step_cnt + 1;\r
398                             end if;\r
399                         end if;\r
400                         scl_step_cnt := scl_step_cnt + 1;\r
401 \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
405                             --show bg\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
410                             --enable nmi\r
411                             --PPUCTRL=80\r
412                             io_out(16#2000#, 16#80#);\r
413                         else\r
414                             io_brk;\r
415                             if (enable_ppu_step_cnt > 1 * cpu_io_multi) then\r
416                                 global_step_cnt := global_step_cnt + 1;\r
417                             end if;\r
418                         end if;\r
419                         enable_ppu_step_cnt := enable_ppu_step_cnt + 1;\r
420 \r
421                     elsif (global_step_cnt = 7) then\r
422                         ----nmi tests.....\r
423                         if (nmi_n = '0') then\r
424 \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
432                                 --scroll x=0\r
433 --                                io_out(16#2005#, nmi_scl_y);\r
434                                 io_brk;\r
435                             elsif (nmi_step_cnt = 3 * cpu_io_multi) then\r
436                                 --scroll y++\r
437 --                                io_out(16#2005#, nmi_scl_y);\r
438                                 io_brk;\r
439                             else\r
440                                 if (ref_cnt = 0) then\r
441                                     nmi_oam_x := nmi_oam_x + 1;\r
442                                 end if;\r
443                                 if (nmi_step_cnt mod 10 = 0) then\r
444                                     nmi_scl_y := nmi_scl_y + 1;\r
445                                 end if;\r
446                                 io_brk;\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
450                                 end if;\r
451                             end if;\r
452                             nmi_step_cnt := nmi_step_cnt + 1;\r
453                         end if;\r
454                     elsif (global_step_cnt = 8) then\r
455                         ----back to nmi tests.....\r
456                         if (nmi_n = '1') then\r
457                             nmi_step_cnt := 0;\r
458                             global_step_cnt := global_step_cnt - 1;\r
459                         end if;\r
460                     else\r
461                         io_brk;\r
462                         init_done := '1';\r
463                     end if;\r
464                 end if;--if (init_done = '0') then\r
465             else\r
466                 r_nw <= 'Z';\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
471     end process;\r
472 \r
473 end rtl;\r
474 \r
475 \r
476 \r
477 \r
478 \r
479 \r
480 -----------dummy prg rom\r
481 library ieee;\r
482 use ieee.std_logic_1164.all;\r
483 entity prg_rom is \r
484     generic (abus_size : integer := 15; dbus_size : integer := 8);\r
485     port (\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
490         );\r
491 end prg_rom;\r
492 architecture rtl of prg_rom is\r
493 begin\r
494     data <= (others => 'Z');\r
495 end rtl;\r
496 \r