--bg prefetch position (scroll + 16 cycle ahead of current pos)\r
--511 x 239 (or 255 x 479)\r
signal prf_x : std_logic_vector(X_SIZE - 1 downto 0);\r
+signal prf_x_cnt : std_logic_vector(X_SIZE - 1 downto 0);\r
signal prf_y : std_logic_vector(X_SIZE - 1 downto 0);\r
+signal vram_addr_in : std_logic_vector (13 downto 0);\r
\r
signal nt_we_n : std_logic;\r
signal disp_nt : std_logic_vector (dsize - 1 downto 0);\r
io_cnt_inst : counter_register generic map (1, 1)\r
port map (emu_ppu_clk, io_cnt_rst_n, '0', '1', (others => '0'), io_cnt);\r
\r
+ io_scr_cnt_inst : d_flip_flop generic map(X_SIZE)\r
+ port map (emu_ppu_clk, rst_n, '1', '0', prf_x, prf_x_cnt);\r
+\r
ale_n <= \r
+ not prf_x_cnt(0) when (\r
+ ppu_mask(PPUSBG) = '1' and\r
+ (nes_x <= conv_std_logic_vector(HSCAN, X_SIZE) or\r
+ nes_x >= conv_std_logic_vector(HSCAN_NEXT_START, X_SIZE)) and\r
+ (nes_y < conv_std_logic_vector(VSCAN, X_SIZE) or \r
+ nes_y = conv_std_logic_vector(VSCAN_NEXT_START, X_SIZE))) else\r
not io_cnt(0) when (\r
- ((ppu_mask(PPUSBG) = '1' or ppu_mask(PPUSSP) = '1') and\r
+ ppu_mask(PPUSSP) = '1' and\r
+ (nes_x > conv_std_logic_vector(HSCAN, X_SIZE) and \r
+ nes_x <= conv_std_logic_vector(HSCAN_SPR_MAX, X_SIZE)) and\r
(nes_y < conv_std_logic_vector(VSCAN, X_SIZE) or \r
- nes_y = conv_std_logic_vector(VSCAN_NEXT_START, X_SIZE)))) else\r
+ nes_y = conv_std_logic_vector(VSCAN_NEXT_START, X_SIZE))) else\r
'1'; --else no latch, keep addr val.\r
rd_n <= \r
+ prf_x_cnt(0) when (\r
+ ppu_mask(PPUSBG) = '1' and\r
+ (nes_x <= conv_std_logic_vector(HSCAN, X_SIZE) or\r
+ nes_x >= conv_std_logic_vector(HSCAN_NEXT_START, X_SIZE)) and\r
+ (nes_y < conv_std_logic_vector(VSCAN, X_SIZE) or \r
+ nes_y = conv_std_logic_vector(VSCAN_NEXT_START, X_SIZE))) else\r
io_cnt(0) when (\r
- ((ppu_mask(PPUSBG) = '1' or ppu_mask(PPUSSP) = '1') and\r
+ ppu_mask(PPUSSP) = '1' and\r
+ (nes_x > conv_std_logic_vector(HSCAN, X_SIZE) and \r
+ nes_x <= conv_std_logic_vector(HSCAN_SPR_MAX, X_SIZE)) and\r
(nes_y < conv_std_logic_vector(VSCAN, X_SIZE) or \r
- nes_y = conv_std_logic_vector(VSCAN_NEXT_START, X_SIZE)))) else\r
+ nes_y = conv_std_logic_vector(VSCAN_NEXT_START, X_SIZE))) else\r
'1';\r
wr_n <= \r
'1' when (\r
d_print("*");\r
d_print("nes_x: " & conv_hex16(conv_integer(nes_x)));\r
d_print("nes_y: " & conv_hex16(conv_integer(nes_y)));\r
+ --all cycls is delayed by two clocks.\r
\r
----fetch next tile byte.\r
- if (nes_x (2 downto 0) = "010") then\r
+ if (prf_x (2 downto 0) = "100") then\r
nt_we_n <= '0';\r
else\r
nt_we_n <= '1';\r
end if;\r
\r
----fetch attr table byte.\r
- if (nes_x (4 downto 0) = "00100") then\r
+ if (prf_x (4 downto 0) = "00110") then\r
attr_we_n <= '0';\r
else\r
attr_we_n <= '1';\r
end if;\r
- if (nes_x (4 downto 0) = "10000") then\r
+ if (prf_x (4 downto 0) = "10010") then\r
disp_attr_we_n <= '0';\r
else\r
disp_attr_we_n <= '1';\r
end if;\r
---attribute is shifted every 16 bit.\r
- if (nes_x (3 downto 0) = "0000") then\r
+ if (prf_x (3 downto 0) = "0010") then\r
attr_ce_n <= '0';\r
else\r
attr_ce_n <= '1';\r
end if;\r
\r
----fetch pattern table low byte.\r
- if (nes_x (2 downto 0) = "110") then\r
+ if (prf_x (2 downto 0) = "000") then\r
ptn_l_we_n <= '0';\r
else\r
ptn_l_we_n <= '1';\r
end if;\r
\r
----fetch pattern table high byte.\r
- if (nes_x (2 downto 0) = "000") then\r
+ if (prf_x (2 downto 0) = "010") then\r
ptn_h_we_n <= '0';\r
else\r
ptn_h_we_n <= '1';\r
-----------------------------------------\r
---vram addr access process\r
-----------------------------------------\r
+ vaddr_inst : d_flip_flop generic map(asize)\r
+ port map (emu_ppu_clk, rst_n, '1', '0', vram_addr_in, vram_addr);\r
+\r
vaddr_p : process (rst_n, emu_ppu_clk)\r
begin\r
if (rst_n = '0') then\r
- vram_addr <= (others => 'Z');\r
+ vram_addr_in <= (others => 'Z');\r
else\r
if (rising_edge(emu_ppu_clk)) then\r
\r
--bg pattern fetch.\r
--------------------------\r
if (ppu_mask(PPUSBG) = '1' and (\r
- (nes_x > conv_std_logic_vector(0, X_SIZE) and \r
- nes_x <= conv_std_logic_vector(HSCAN, X_SIZE))\r
- or\r
- (nes_x >= conv_std_logic_vector(HSCAN_NEXT_START, X_SIZE))\r
+ (nes_x <= conv_std_logic_vector(HSCAN, X_SIZE) or\r
+ nes_x >= conv_std_logic_vector(HSCAN_NEXT_START, X_SIZE))\r
)) then\r
\r
----fetch next tile byte.\r
- if (nes_x (2 downto 0) = "001") then\r
+ if (prf_x (2 downto 0) = "011") then\r
--vram addr is incremented every 8 cycle.\r
--name table at 0x2000\r
- vram_addr(9 downto 0) \r
+ vram_addr_in(9 downto 0) \r
<= prf_y(dsize - 1 downto 3) \r
& prf_x(dsize - 1 downto 3);\r
- vram_addr(asize - 1 downto 10) <= "10" & ppu_ctrl(PPUBNA downto 0) \r
+ vram_addr_in(asize - 1 downto 10) <= "10" & ppu_ctrl(PPUBNA downto 0) \r
+ ("000" & prf_x(dsize));\r
----fetch attr table byte.\r
- elsif (nes_x (2 downto 0) = "011") then\r
+ elsif (prf_x (2 downto 0) = "101") then\r
--attr table at 0x23c0\r
- vram_addr(dsize - 1 downto 0) <= "11000000" +\r
+ vram_addr_in(dsize - 1 downto 0) <= "11000000" +\r
("00" & prf_y(7 downto 5) & prf_x(7 downto 5));\r
- vram_addr(asize - 1 downto dsize) <= "10" &\r
+ vram_addr_in(asize - 1 downto dsize) <= "10" &\r
ppu_ctrl(PPUBNA downto 0) & "11"\r
+ ("000" & prf_x(dsize) & "00");\r
----fetch pattern table low byte.\r
- elsif (nes_x (2 downto 0) = "101") then\r
+ elsif (prf_x (2 downto 0) = "111") then\r
--vram addr is incremented every 8 cycle.\r
- vram_addr <= "0" & ppu_ctrl(PPUBPA) & \r
+ vram_addr_in <= "0" & ppu_ctrl(PPUBPA) & \r
disp_nt(dsize - 1 downto 0) \r
& "0" & prf_y(2 downto 0);\r
----fetch pattern table high byte.\r
- elsif (nes_x (2 downto 0) = "111") then\r
+ elsif (prf_x (2 downto 0) = "001") then\r
--vram addr is incremented every 8 cycle.\r
- vram_addr <= "0" & ppu_ctrl(PPUBPA) & \r
+ vram_addr_in <= "0" & ppu_ctrl(PPUBPA) & \r
disp_nt(dsize - 1 downto 0) \r
& "0" & prf_y(2 downto 0) + "00000000001000";\r
end if;\r
--------------------------\r
--sprite pattern fetch.\r
--------------------------\r
- elsif (ppu_mask(PPUSSP) = '1' and (nes_x > conv_std_logic_vector(HSCAN, X_SIZE) and \r
- nes_x <= conv_std_logic_vector(HSCAN_SPR_MAX, X_SIZE))) then\r
+ elsif (ppu_mask(PPUSSP) = '1' and (\r
+ nes_x > conv_std_logic_vector(HSCAN, X_SIZE) and \r
+ nes_x <= conv_std_logic_vector(HSCAN_SPR_MAX, X_SIZE))\r
+ ) then\r
\r
----fetch pattern table low byte.\r
- if (nes_x (2 downto 0) = "101" ) then\r
+ if (nes_x (2 downto 0) = "111" ) then\r
if (spr_attr(conv_integer(s_oam_addr_cpy(4 downto 2)))(SPRVFL) = '0') then\r
- vram_addr <= "0" & ppu_ctrl(PPUSPA) & \r
+ vram_addr_in <= "0" & ppu_ctrl(PPUSPA) & \r
spr_tile_tmp(dsize - 1 downto 0) & "0" & \r
(nes_y(2 downto 0) + "001" - spr_y_tmp(2 downto 0));\r
else\r
--flip sprite vertically.\r
- vram_addr <= "0" & ppu_ctrl(PPUSPA) & \r
+ vram_addr_in <= "0" & ppu_ctrl(PPUSPA) & \r
spr_tile_tmp(dsize - 1 downto 0) & "0" & \r
(spr_y_tmp(2 downto 0) - nes_y(2 downto 0) - "010");\r
end if;\r
\r
----fetch pattern table high byte.\r
- elsif (nes_x (2 downto 0) = "111" ) then\r
+ elsif (nes_x (2 downto 0) = "001" ) then\r
if (spr_attr(conv_integer(s_oam_addr_cpy(4 downto 2)))(SPRVFL) = '0') then\r
- vram_addr <= "0" & ppu_ctrl(PPUSPA) & \r
+ vram_addr_in <= "0" & ppu_ctrl(PPUSPA) & \r
spr_tile_tmp(dsize - 1 downto 0) & "0" & \r
(nes_y(2 downto 0) + "001" - spr_y_tmp(2 downto 0))\r
+ "00000000001000";\r
else\r
--flip sprite vertically.\r
- vram_addr <= "0" & ppu_ctrl(PPUSPA) & \r
+ vram_addr_in <= "0" & ppu_ctrl(PPUSPA) & \r
spr_tile_tmp(dsize - 1 downto 0) & "0" & \r
(spr_y_tmp(2 downto 0) - nes_y(2 downto 0) - "010")\r
+ "00000000001000";\r
end if;\r
end if;\r
else\r
- vram_addr <= (others => 'Z');\r
+ vram_addr_in <= (others => 'Z');\r
end if; --if (nes_x > conv_std_logic_vector(HSCAN, X_SIZE) and \r
--nes_x <= conv_std_logic_vector(HSCAN_SPR_MAX, X_SIZE)) then\r
else\r
- vram_addr <= (others => 'Z');\r
+ vram_addr_in <= (others => 'Z');\r
end if; --(nes_y < conv_std_logic_vector(VSCAN, X_SIZE) or \r
--nes_y = conv_std_logic_vector(VSCAN_NEXT_START, X_SIZE))) then\r
end if; --if (rising_edge(emu_ppu_clk)) then\r