OSDN Git Service

snapshot restore bug fixed.
[motonesfpga/motonesfpga.git] / de1_nes / dummy-mos6502.vhd
index b35fd17..955540e 100644 (file)
@@ -22,15 +22,13 @@ entity mos6502 is
     signal dbg_stat_we_n    : out std_logic;\r
     signal dbg_idl_h, dbg_idl_l, dbg_dbb_r, dbg_dbb_w    : out std_logic_vector (7 downto 0);\r
 \r
-            input_clk   : in std_logic; --phi0 input pin.\r
+            cpu_clk     : in std_logic; --phi0 input pin.\r
+            dl_cpu_clk  : in std_logic; --phi1 delayed clock.\r
             rdy         : in std_logic;\r
             rst_n       : in std_logic;\r
             irq_n       : in std_logic;\r
             nmi_n       : in std_logic;\r
-            dbe         : in std_logic;\r
             r_nw        : out std_logic;\r
-            phi1        : out std_logic;\r
-            phi2        : out std_logic;\r
             addr        : out std_logic_vector ( asize - 1 downto 0);\r
             d_io        : inout std_logic_vector ( dsize - 1 downto 0)\r
     );\r
@@ -40,21 +38,23 @@ architecture rtl of mos6502 is
 \r
 \r
 begin\r
-    phi1 <= input_clk;\r
-    phi2 <= not input_clk;\r
 \r
     --set ppu value...\r
-    set_ppu_p : process (input_clk, rst_n)\r
+    set_ppu_p : process (cpu_clk, rst_n)\r
     use ieee.std_logic_arith.conv_std_logic_vector;\r
 \r
     variable init_step_cnt, plt_step_cnt, \r
             nt_step_cnt, spr_step_cnt, dma_step_cnt, scl_step_cnt, \r
-            enable_ppu_step_cnt : integer;\r
+            enable_ppu_step_cnt, nmi_step_cnt : integer;\r
     variable init_done : std_logic;\r
     variable global_step_cnt : integer;\r
     constant cpu_io_multi : integer := 3; --io happens every 4 cpu cycle.\r
     variable i, j : integer;\r
     variable ch : integer := 16#41# ;\r
+    variable nmi_oam_x : integer range 0 to 255;\r
+    variable nmi_scl_y : integer range 0 to 255;\r
+\r
+    variable ref_cnt : integer range 0 to 120;\r
 \r
 procedure io_out (ad: in integer; dt : in integer) is\r
 begin\r
@@ -62,6 +62,7 @@ begin
     addr <= conv_std_logic_vector(ad, 16);\r
     d_io <= conv_std_logic_vector(dt, 8);\r
 end;\r
+\r
 procedure io_brk is\r
 begin\r
     addr <= (others => 'Z');\r
@@ -69,6 +70,13 @@ begin
     r_nw <= 'Z';\r
 end;\r
 \r
+procedure io_read (ad: in integer) is\r
+begin\r
+    r_nw <= '1';\r
+    addr <= conv_std_logic_vector(ad, 16);\r
+    d_io <= (others => 'Z');\r
+end;\r
+\r
     begin\r
         if (rst_n = '0') then\r
             \r
@@ -85,22 +93,42 @@ end;
             dma_step_cnt := 0;\r
             scl_step_cnt := 0;\r
             enable_ppu_step_cnt := 0;\r
+            nmi_step_cnt := 0;\r
+            nmi_oam_x := 0;\r
+            nmi_scl_y := 200;\r
+            ref_cnt := 0;\r
 \r
-        elsif (rising_edge(input_clk)) then\r
+        elsif (rising_edge(cpu_clk)) then\r
 \r
             if (rdy = '1') then\r
                 if (init_done = '0') then\r
                     if (global_step_cnt = 0) then\r
                         --step0.0 = init ppu.\r
                         if (init_step_cnt = 0 * cpu_io_multi) then\r
+                            --PPUCTRL=01 for test...\r
+                            io_out(16#2000#, 16#01#);\r
+                        elsif (init_step_cnt = 1 * cpu_io_multi) then\r
+                            --PPUMASK=02\r
+                            io_out(16#2001#, 16#02#);\r
+                        elsif (init_step_cnt = 2 * cpu_io_multi) then\r
                             --PPUCTRL=00\r
                             io_out(16#2000#, 16#00#);\r
-                        elsif (init_step_cnt = 1 * cpu_io_multi) then\r
+                        elsif (init_step_cnt = 3 * cpu_io_multi) then\r
                             --PPUMASK=00\r
                             io_out(16#2001#, 16#00#);\r
+                        elsif (init_step_cnt = 4 * cpu_io_multi) then\r
+                            --ppuaddr\r
+                            io_out(16#2006#, 16#02#);\r
+                        elsif (init_step_cnt = 5 * cpu_io_multi) then\r
+                            io_out(16#2006#, 16#40#);\r
+                        elsif (init_step_cnt = 6 * cpu_io_multi) then\r
+                            --scroll\r
+                            io_out(16#2005#, 16#21#);\r
+                        elsif (init_step_cnt = 7 * cpu_io_multi) then\r
+                            io_out(16#2005#, 16#5#);\r
                         else\r
                             io_brk;\r
-                            if (init_step_cnt > 2 * cpu_io_multi) then\r
+                            if (init_step_cnt > 8 * cpu_io_multi) then\r
                                 global_step_cnt := global_step_cnt + 1;\r
                             end if;\r
                         end if;\r
@@ -205,7 +233,7 @@ end;
                             --set vram addr 2005 (first row, 6th col)\r
                             io_out(16#2006#, 16#20#);\r
                         elsif (nt_step_cnt = 1 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#06#);\r
+                            io_out(16#2006#, 16#03#);\r
                         elsif (nt_step_cnt = 2 * cpu_io_multi) then\r
                             --set name tbl data\r
                             --0x44, 45, 45 = DEE\r
@@ -217,138 +245,34 @@ end;
 \r
 \r
                         elsif (nt_step_cnt = 5 * cpu_io_multi) then\r
-                            --set vram addr 23c1 (attribute)\r
-                            io_out(16#2006#, 16#23#);\r
+                            io_out(16#2006#, 16#20#);\r
                         elsif (nt_step_cnt = 6 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#c1#);\r
+                            io_out(16#2006#, 16#2a#);\r
                         elsif (nt_step_cnt = 7 * cpu_io_multi) then\r
-                                    --attr=11011000\r
-                            io_out(16#2007#, 16#d8#);\r
-\r
+                            io_out(16#2007#, 16#44#);\r
 \r
                         elsif (nt_step_cnt = 8 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#20#);\r
+                            io_out(16#2006#, 16#24#);\r
                         elsif (nt_step_cnt = 9 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#60#);\r
-\r
+                            io_out(16#2006#, 16#43#);\r
                         elsif (nt_step_cnt = 10 * cpu_io_multi) then\r
-                            io_out(16#2007#, 48);\r
+                            io_out(16#2007#, 16#6d#);\r
                         elsif (nt_step_cnt = 11 * cpu_io_multi) then\r
-                            io_out(16#2007#, 49);\r
+                            io_out(16#2007#, 16#6f#);\r
                         elsif (nt_step_cnt = 12 * cpu_io_multi) then\r
-                            io_out(16#2007#, 50);\r
+                            io_out(16#2007#, 16#74#);\r
                         elsif (nt_step_cnt = 13 * cpu_io_multi) then\r
-                            io_out(16#2007#, 51);\r
+                            io_out(16#2007#, 16#6f#);\r
+                            \r
                         elsif (nt_step_cnt = 14 * cpu_io_multi) then\r
-                            io_out(16#2007#, 52);\r
+                            io_out(16#2006#, 16#2e#);\r
                         elsif (nt_step_cnt = 15 * cpu_io_multi) then\r
-                            io_out(16#2007#, 53);\r
+                            io_out(16#2006#, 16#93#);\r
                         elsif (nt_step_cnt = 16 * cpu_io_multi) then\r
-                            io_out(16#2007#, 54);\r
-                        elsif (nt_step_cnt = 17 * cpu_io_multi) then\r
-                            io_out(16#2007#, 55);\r
-                        elsif (nt_step_cnt = 18 * cpu_io_multi) then\r
-                            io_out(16#2007#, 56);\r
-\r
-    --                    elsif (nt_step_cnt = 5 * cpu_io_multi) then\r
-    --                        --set vram addr 21d1\r
-    --                        io_out(16#2006#, 16#21#);\r
-    --                    elsif (nt_step_cnt = 6 * cpu_io_multi) then\r
-    --                        io_out(16#2006#, 16#E6#);\r
-    --                    elsif (nt_step_cnt = 7 * cpu_io_multi) then\r
-    --                        --msg=DEE TEST !!!\r
-    --                        io_out(16#2007#, 16#44#);\r
-    --                    elsif (nt_step_cnt = 8 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#45#);\r
-    --                    elsif (nt_step_cnt = 9 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#45#);\r
-    --                    elsif (nt_step_cnt = 10 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#00#);\r
-    --                    elsif (nt_step_cnt = 11 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#54#);\r
-    --                    elsif (nt_step_cnt = 12 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#45#);\r
-    --                    elsif (nt_step_cnt = 13 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#53#);\r
-    --                    elsif (nt_step_cnt = 14 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#54#);\r
-    --                    elsif (nt_step_cnt = 15 * cpu_io_multi) then\r
-    --                        io_out(16#2007#, 16#21#);\r
-\r
-                        --display test pattern\r
-                        elsif (nt_step_cnt = 19 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#20#);\r
-                        elsif (nt_step_cnt = 20 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#20#);\r
-                        \r
-                        elsif (nt_step_cnt = 21 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#01#);\r
-                        elsif (nt_step_cnt = 22 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#02#);\r
-                        elsif (nt_step_cnt = 23 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#03#);\r
-                        elsif (nt_step_cnt = 24 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#04#);\r
-                        elsif (nt_step_cnt = 25 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#05#);\r
-                        elsif (nt_step_cnt = 26 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#06#);\r
-                        elsif (nt_step_cnt = 27 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#07#);\r
-                        elsif (nt_step_cnt = 28 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#08#);\r
-                        elsif (nt_step_cnt = 29 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#09#);\r
-                        elsif (nt_step_cnt = 30 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#0a#);\r
-                        elsif (nt_step_cnt = 31 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#0b#);\r
-                        elsif (nt_step_cnt = 32 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#0c#);\r
-                        elsif (nt_step_cnt = 33 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#0d#);\r
-                        elsif (nt_step_cnt = 34 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#0e#);\r
-                        elsif (nt_step_cnt = 35 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#0f#);\r
+                            io_out(16#2007#, 16#59#);\r
 \r
-                        elsif (nt_step_cnt = 36 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#20#);\r
-                        elsif (nt_step_cnt = 37 * cpu_io_multi) then\r
-                            io_out(16#2006#, 16#40#);\r
-                        \r
-                        elsif (nt_step_cnt = 38 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#10#);\r
-                        elsif (nt_step_cnt = 39 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#11#);\r
-                        elsif (nt_step_cnt = 40 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#12#);\r
-                        elsif (nt_step_cnt = 41 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#13#);\r
-                        elsif (nt_step_cnt = 42 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#14#);\r
-                        elsif (nt_step_cnt = 43 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#15#);\r
-                        elsif (nt_step_cnt = 44 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#16#);\r
-                        elsif (nt_step_cnt = 45 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#17#);\r
-                        elsif (nt_step_cnt = 46 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#18#);\r
-                        elsif (nt_step_cnt = 47 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#19#);\r
-                        elsif (nt_step_cnt = 48 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#1a#);\r
-                        elsif (nt_step_cnt = 49 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#1b#);\r
-                        elsif (nt_step_cnt = 50 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#1c#);\r
-                        elsif (nt_step_cnt = 51 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#1d#);\r
-                        elsif (nt_step_cnt = 52 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#1e#);\r
-                        elsif (nt_step_cnt = 53 * cpu_io_multi) then\r
-                            io_out(16#2007#, 16#1f#);\r
+                        elsif (nt_step_cnt = 17 * cpu_io_multi) then\r
+                            io_out(16#2007#, 16#00#);\r
 \r
                         else\r
                             io_brk;\r
@@ -364,31 +288,32 @@ end;
                         if (spr_step_cnt = 0) then\r
                             --set sprite addr=00 (first sprite)\r
                             io_out(16#2003#, 16#00#);\r
+                        \r
                         elsif (spr_step_cnt = 1 * cpu_io_multi) then\r
                             --set sprite data: y=02\r
-                            io_out(16#2004#, 16#13#);\r
+                            io_out(16#2004#, 16#05#);\r
                         elsif (spr_step_cnt = 2 * cpu_io_multi) then\r
-                            --tile=0x4d (ascii 'M')\r
-                            io_out(16#2004#, 16#4d#);\r
+                            --tile=0x4d (ascii 'O')\r
+                            io_out(16#2004#, 16#4f#);\r
                         elsif (spr_step_cnt = 3 * cpu_io_multi) then\r
-                            --set sprite attr=03 (palette 03)\r
+                            --set sprite attr=01 (palette 01)\r
                             io_out(16#2004#, 16#01#);\r
                         elsif (spr_step_cnt = 4 * cpu_io_multi) then\r
-                            --set sprite data: x=100\r
-                            io_out(16#2004#, 16#64#);\r
+                            --set sprite data: x=60\r
+                            io_out(16#2004#, 16#3c#);\r
 \r
                         elsif (spr_step_cnt = 5 * cpu_io_multi) then\r
                             --set sprite data: y=50\r
-                            io_out(16#2004#, 16#32#);\r
+                            io_out(16#2004#, 1);\r
                         elsif (spr_step_cnt = 6 * cpu_io_multi) then\r
-                            --tile=0x4d (ascii 'O')\r
-                            io_out(16#2004#, 16#4f#);\r
+                            --tile=0x4f (ascii 'M')\r
+                            io_out(16#2004#, 16#4d#);\r
                         elsif (spr_step_cnt = 7 * cpu_io_multi) then\r
-                            --set sprite attr=01\r
-                            io_out(16#2004#, 16#01#);\r
+                            --set sprite attr=02\r
+                            io_out(16#2004#, 16#02#);\r
                         elsif (spr_step_cnt = 8 * cpu_io_multi) then\r
-                            --set sprite data: x=30\r
-                            io_out(16#2004#, 16#1e#);\r
+                            --set sprite data: x=100\r
+                            io_out(16#2004#, 16#64#);\r
 \r
                         elsif (spr_step_cnt = 9 * cpu_io_multi) then\r
                             --set sprite data: y=60\r
@@ -418,7 +343,7 @@ end;
 \r
                         else\r
                             io_brk;\r
-                            if (spr_step_cnt > 4 * cpu_io_multi) then\r
+                            if (spr_step_cnt > 8 * cpu_io_multi) then\r
                                 global_step_cnt := global_step_cnt + 2;\r
                             end if;\r
                         end if;\r
@@ -434,7 +359,8 @@ end;
                                 ch := 16#41# + i;\r
                             end if;\r
 \r
-                            if (i < 64) then\r
+                            --if (i < 64) then\r
+                            if (i < 10) then\r
                                 --set dma value on the ram.\r
                                 if    (dma_step_cnt = (0 + j) * cpu_io_multi) then\r
                                     io_out(16#0200# + j, i);\r
@@ -466,17 +392,71 @@ end;
                         dma_step_cnt := dma_step_cnt + 1;\r
 \r
                     elsif (global_step_cnt = 5) then\r
-                        --step4 = scroll test.\r
+                        --step4 = scroll test and ppu reg read test.\r
                         if (scl_step_cnt = 0) then\r
-                            --x scroll pos=40\r
-                            io_out(16#2005#, 40);\r
+                            --x scroll pos=123\r
+                            io_out(16#2005#, 11);\r
                         elsif (scl_step_cnt = 1 * cpu_io_multi) then\r
-                            --y scroll pos=3\r
-                            io_out(16#2005#, 3);\r
+                            --y scroll pos=100\r
+                            io_out(16#2005#, 0);\r
+\r
+                        elsif (scl_step_cnt = 2 * cpu_io_multi) then\r
+                            --read status reg.\r
+                            io_read(16#2002#);\r
+\r
+                        elsif (scl_step_cnt = 3 * cpu_io_multi) then\r
+                            --read vram nt0.\r
+                            io_out(16#2006#, 16#20#);\r
+\r
+                        elsif (scl_step_cnt = 4 * cpu_io_multi) then\r
+                            io_out(16#2006#, 16#03#);\r
+\r
+                        elsif (scl_step_cnt = 5 * cpu_io_multi) then\r
+                            io_read(16#2007#);\r
+\r
+                        elsif (scl_step_cnt = 6 * cpu_io_multi) then\r
+                            --pattern tbl.\r
+                            io_out(16#2006#, 16#04#);\r
+\r
+                        elsif (scl_step_cnt = 7 * cpu_io_multi) then\r
+                            io_out(16#2006#, 16#d1#);\r
+\r
+                        elsif (scl_step_cnt = 8 * cpu_io_multi) then\r
+                            io_read(16#2007#);\r
+\r
+                        elsif (scl_step_cnt = 9 * cpu_io_multi) then\r
+                            --attr tbl.\r
+                            io_out(16#2006#, 16#23#);\r
+\r
+                        elsif (scl_step_cnt = 10 * cpu_io_multi) then\r
+                            io_out(16#2006#, 16#c0#);\r
+\r
+                        elsif (scl_step_cnt = 11 * cpu_io_multi) then\r
+                            --set attr first.\r
+                            io_out(16#2007#, 16#5a#);\r
+\r
+                        elsif (scl_step_cnt = 12 * cpu_io_multi) then\r
+                            io_out(16#2006#, 16#23#);\r
+\r
+                        elsif (scl_step_cnt = 13 * cpu_io_multi) then\r
+                            io_out(16#2006#, 16#c0#);\r
+\r
+                        elsif (scl_step_cnt = 14 * cpu_io_multi) then\r
+                            io_read(16#2007#);\r
+\r
+                        elsif (scl_step_cnt = 15 * cpu_io_multi) then\r
+                            --read palette tbl.\r
+                            io_out(16#2006#, 16#3f#);\r
+\r
+                        elsif (scl_step_cnt = 16 * cpu_io_multi) then\r
+                            io_out(16#2006#, 16#11#);\r
+\r
+                        elsif (scl_step_cnt = 17 * cpu_io_multi) then\r
+                            io_read(16#2007#);\r
 \r
                         else\r
                             io_brk;\r
-                            if (scl_step_cnt > 1 * cpu_io_multi) then\r
+                            if (scl_step_cnt > 17 * cpu_io_multi) then\r
                                 global_step_cnt := global_step_cnt + 1;\r
                             end if;\r
                         end if;\r
@@ -501,6 +481,51 @@ end;
                         end if;\r
                         enable_ppu_step_cnt := enable_ppu_step_cnt + 1;\r
 \r
+                    elsif (global_step_cnt = 7) then\r
+                        ----nmi tests.....\r
+                        if (nmi_n = '0') then\r
+\r
+                            if (nmi_step_cnt = 0 * cpu_io_multi) then\r
+                                --set sprite addr=00 (first sprite)\r
+                                io_out(16#2003#, 16#03#);\r
+                            elsif (nmi_step_cnt = 1 * cpu_io_multi) then\r
+                                --set sprite data: x=100\r
+                                io_out(16#2004#, nmi_oam_x);\r
+                            elsif (nmi_step_cnt = 2 * cpu_io_multi) then\r
+                                --scroll x=0\r
+--                                io_out(16#2005#, nmi_scl_y);\r
+                                io_brk;\r
+                            elsif (nmi_step_cnt = 3 * cpu_io_multi) then\r
+                                --scroll y++\r
+--                                io_out(16#2005#, nmi_scl_y);\r
+                                io_brk;\r
+                            elsif (nmi_step_cnt = 4 * cpu_io_multi) then\r
+                                --set sprite addr=00 (first sprite)\r
+                                io_out(16#2003#, 16#04#);\r
+                            elsif (nmi_step_cnt = 5 * cpu_io_multi) then\r
+                                --set sprite data: x=100\r
+                                io_out(16#2004#, nmi_oam_x);\r
+                            else\r
+                                if (ref_cnt = 0) then\r
+                                    nmi_oam_x := nmi_oam_x + 1;\r
+                                end if;\r
+                                if (nmi_step_cnt mod 10 = 0) then\r
+                                    nmi_scl_y := nmi_scl_y + 1;\r
+                                end if;\r
+                                io_brk;\r
+                                if (nmi_step_cnt > 5 * cpu_io_multi) then\r
+                                    ref_cnt := ref_cnt + 1;\r
+                                    global_step_cnt := global_step_cnt + 1;\r
+                                end if;\r
+                            end if;\r
+                            nmi_step_cnt := nmi_step_cnt + 1;\r
+                        end if;\r
+                    elsif (global_step_cnt = 8) then\r
+                        ----back to nmi tests.....\r
+                        if (nmi_n = '1') then\r
+                            nmi_step_cnt := 0;\r
+                            global_step_cnt := global_step_cnt - 1;\r
+                        end if;\r
                     else\r
                         io_brk;\r
                         init_done := '1';\r