OSDN Git Service

branch page crossing bug fixed.
[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             input_clk   : in std_logic; --phi0 input pin.\r
26             rdy         : in std_logic;\r
27             rst_n       : in std_logic;\r
28             irq_n       : in std_logic;\r
29             nmi_n       : in std_logic;\r
30             dbe         : 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
36     );\r
37 end mos6502;\r
38 \r
39 architecture rtl of mos6502 is\r
40 \r
41 \r
42 begin\r
43     phi1 <= input_clk;\r
44     phi2 <= not input_clk;\r
45 \r
46     --set ppu value...\r
47     set_ppu_p : process (input_clk, rst_n)\r
48     use ieee.std_logic_arith.conv_std_logic_vector;\r
49 \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
60 \r
61     variable ref_cnt : integer range 0 to 120;\r
62 \r
63 procedure io_out (ad: in integer; dt : in integer) is\r
64 begin\r
65     r_nw <= '0';\r
66     addr <= conv_std_logic_vector(ad, 16);\r
67     d_io <= conv_std_logic_vector(dt, 8);\r
68 end;\r
69 procedure io_brk is\r
70 begin\r
71     addr <= (others => 'Z');\r
72     d_io <= (others => 'Z');\r
73     r_nw <= 'Z';\r
74 end;\r
75 \r
76     begin\r
77         if (rst_n = '0') then\r
78             \r
79             r_nw <= 'Z';\r
80             addr <= (others => 'Z');\r
81             d_io <= (others => 'Z');\r
82             \r
83             init_done := '0';\r
84             global_step_cnt := 0;\r
85             init_step_cnt := 0;\r
86             plt_step_cnt := 0;\r
87             nt_step_cnt := 0;\r
88             spr_step_cnt := 0;\r
89             dma_step_cnt := 0;\r
90             scl_step_cnt := 0;\r
91             enable_ppu_step_cnt := 0;\r
92             nmi_step_cnt := 0;\r
93             nmi_oam_x := 0;\r
94             nmi_scl_y := 200;\r
95             ref_cnt := 0;\r
96 \r
97         elsif (rising_edge(input_clk)) then\r
98 \r
99             if (rdy = '1') 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
104                             --PPUCTRL=00\r
105                             io_out(16#2000#, 16#00#);\r
106                         elsif (init_step_cnt = 1 * cpu_io_multi) then\r
107                             --PPUMASK=00\r
108                             io_out(16#2001#, 16#00#);\r
109                         else\r
110                             io_brk;\r
111                             if (init_step_cnt > 2 * cpu_io_multi) then\r
112                                 global_step_cnt := global_step_cnt + 1;\r
113                             end if;\r
114                         end if;\r
115                         init_step_cnt := init_step_cnt + 1;\r
116                     elsif (global_step_cnt = 1) then\r
117                         --step0.1 = palette set.\r
118     --palettes:\r
119     --;;;bg palette\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
124     --;;;spr palette\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
129                         \r
130                         \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
136                         \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
146 \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
155      \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
164      \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
173 \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
183 \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
192 \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
201 \r
202                         else\r
203                             io_brk;\r
204                             if (plt_step_cnt > 30 * cpu_io_multi) then\r
205                                 global_step_cnt := global_step_cnt + 1;\r
206                             end if;\r
207                         end if;\r
208                         plt_step_cnt := plt_step_cnt + 1;\r
209                         \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
225 \r
226 \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
233 \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
246                             \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
253 \r
254                         elsif (nt_step_cnt = 17 * cpu_io_multi) then\r
255                             io_out(16#2007#, 16#00#);\r
256 \r
257                         else\r
258                             io_brk;\r
259                             if (nt_step_cnt > 4 * cpu_io_multi) then\r
260                                 global_step_cnt := global_step_cnt + 1;\r
261                             end if;\r
262                         end if;\r
263                         \r
264                         nt_step_cnt := nt_step_cnt + 1;\r
265                         \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
283 \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
296 \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
309 \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
322 \r
323                         else\r
324                             io_brk;\r
325                             if (spr_step_cnt > 8 * cpu_io_multi) then\r
326                                 global_step_cnt := global_step_cnt + 2;\r
327                             end if;\r
328                         end if;\r
329                         spr_step_cnt := spr_step_cnt + 1;\r
330 \r
331                     elsif (global_step_cnt = 4) then\r
332                         --step3 = dma set.\r
333                         for i in 0 to 64 loop\r
334                             j := i * 4;\r
335                             if (ch = 16#5b#) then\r
336                                 ch := 16#41#;\r
337                             else\r
338                                 ch := 16#41# + i;\r
339                             end if;\r
340 \r
341                             --if (i < 64) then\r
342                             if (i < 10) 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
356                                     io_brk;\r
357                                 end if;\r
358                             else\r
359                                 if    (dma_step_cnt = (0 + j) * cpu_io_multi) then\r
360                                     --start dma\r
361                                     io_out(16#4014#, 16#02#);\r
362                                 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 1) then\r
363                                     io_brk;\r
364                                 elsif (dma_step_cnt = (0 + j) * cpu_io_multi + 2) then\r
365                                     io_brk;\r
366                                     global_step_cnt := global_step_cnt + 1;\r
367                                 end if;\r
368                             end if;\r
369                         end loop;\r
370                         \r
371                         dma_step_cnt := dma_step_cnt + 1;\r
372 \r
373                     elsif (global_step_cnt = 5) then\r
374                         --step4 = scroll test.\r
375                         if (scl_step_cnt = 0) then\r
376                             --x scroll pos=123\r
377                             io_out(16#2005#, 0);\r
378                         elsif (scl_step_cnt = 1 * cpu_io_multi) then\r
379                             --y scroll pos=100\r
380                             io_out(16#2005#, 0);\r
381 \r
382                         else\r
383                             io_brk;\r
384                             if (scl_step_cnt > 1 * cpu_io_multi) then\r
385                                 global_step_cnt := global_step_cnt + 1;\r
386                             end if;\r
387                         end if;\r
388                         scl_step_cnt := scl_step_cnt + 1;\r
389 \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
393                             --show bg\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
398                             --enable nmi\r
399                             --PPUCTRL=80\r
400                             io_out(16#2000#, 16#80#);\r
401                         else\r
402                             io_brk;\r
403                             if (enable_ppu_step_cnt > 1 * cpu_io_multi) then\r
404                                 global_step_cnt := global_step_cnt + 1;\r
405                             end if;\r
406                         end if;\r
407                         enable_ppu_step_cnt := enable_ppu_step_cnt + 1;\r
408 \r
409                     elsif (global_step_cnt = 7) then\r
410                         ----nmi tests.....\r
411                         if (nmi_n = '0') then\r
412 \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
420                                 --scroll x=0\r
421 --                                io_out(16#2005#, nmi_scl_y);\r
422                                 io_brk;\r
423                             elsif (nmi_step_cnt = 3 * cpu_io_multi) then\r
424                                 --scroll y++\r
425 --                                io_out(16#2005#, nmi_scl_y);\r
426                                 io_brk;\r
427                             else\r
428                                 if (ref_cnt = 0) then\r
429                                     nmi_oam_x := nmi_oam_x + 1;\r
430                                 end if;\r
431                                 if (nmi_step_cnt mod 10 = 0) then\r
432                                     nmi_scl_y := nmi_scl_y + 1;\r
433                                 end if;\r
434                                 io_brk;\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
438                                 end if;\r
439                             end if;\r
440                             nmi_step_cnt := nmi_step_cnt + 1;\r
441                         end if;\r
442                     elsif (global_step_cnt = 8) then\r
443                         ----back to nmi tests.....\r
444                         if (nmi_n = '1') then\r
445                             nmi_step_cnt := 0;\r
446                             global_step_cnt := global_step_cnt - 1;\r
447                         end if;\r
448                     else\r
449                         io_brk;\r
450                         init_done := '1';\r
451                     end if;\r
452                 end if;--if (init_done = '0') then\r
453             else\r
454                 r_nw <= 'Z';\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
459     end process;\r
460 \r
461 end rtl;\r
462 \r
463 \r
464 \r
465 \r
466 \r
467 \r
468 -----------dummy prg rom\r
469 library ieee;\r
470 use ieee.std_logic_1164.all;\r
471 entity prg_rom is \r
472     generic (abus_size : integer := 15; dbus_size : integer := 8);\r
473     port (\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
478         );\r
479 end prg_rom;\r
480 architecture rtl of prg_rom is\r
481 begin\r
482     data <= (others => 'Z');\r
483 end rtl;\r
484 \r