Skip to content

Commit

Permalink
VIC-II: apply color change (background mainly) one pixel earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
gyurco committed Dec 3, 2023
1 parent 9ee8594 commit fe99275
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions rtl/video_vicII_656x_a.vhd
Expand Up @@ -40,7 +40,7 @@ architecture rtl of video_vicii_656x is
type spriteColorsDef is array(7 downto 0) of unsigned(3 downto 0);
type pixelColorStoreDef is array(7 downto 0) of unsigned(3 downto 0);

constant PIX_DELAY : integer := 5;
constant PIX_DELAY : integer := 6;

-- State machine
signal lastLineFlag : boolean; -- True for on last line of the frame.
Expand Down Expand Up @@ -187,7 +187,8 @@ architecture rtl of video_vicii_656x is
signal MYE_ff : unsigned(7 downto 0); -- Sprite Y expansion flipflop
signal MYE_ff_next : unsigned(7 downto 0); -- Sprite Y expansion flipflop combinatorial
signal MC_ff : unsigned(7 downto 0); -- controls sprite shift-register in multicolor
signal MShift : MFlags; -- Sprite is shifting
signal MShift_stop : MFlags; -- Stop sprite shifting flag
signal MCurrentPixel_r : MCurrentPixelDef;
signal MCurrentPixel : MCurrentPixelDef;

-- Current colors and pixels
Expand Down Expand Up @@ -232,10 +233,10 @@ begin
end process;

myWr_a <= cs and phi and we;
myWr_b <= '1' when we_r = '1' and enaPixel = '1' and rasterX(2 downto 0) = "011" else '0';
myWr_c <= '1' when we_r = '1' and enaPixel = '1' and rasterX(2 downto 0) = "100" else '0';
myWr_b <= '1' when we_r = '1' and enaPixel = '1' and rasterX(2 downto 0) = "100" else '0';
myWr_c <= '1' when we_r = '1' and enaPixel = '1' and rasterX(2 downto 0) = "101" else '0';
-- timing of the read is only important for the collision register reads
myRd <= '1' when cs = '1' and phi = '1' and we = '0' and enaPixel = '1' and rasterX(1 downto 0) = "00" else '0';
myRd <= '1' when cs = '1' and phi = '1' and we = '0' and enaPixel = '1' and rasterX(1 downto 0) = "01" else '0';

-- -----------------------------------------------------------------------
-- debug signals
Expand Down Expand Up @@ -889,7 +890,7 @@ calcBorders: process(clk)
newTBBorder := TBBorder;
-- 1. If the X coordinate reaches the right comparison value, the main border
-- flip flop is set (comparison values are from VIC II datasheet).
if (rasterX = 339 + 2 and CSEL = '0') or (rasterX = 348 + 2 and CSEL = '1') then
if (rasterX = 339 + 3 and CSEL = '0') or (rasterX = 348 + 3 and CSEL = '1') then
MainBorder <= '1';
end if;
-- 2. If the Y coordinate reaches the bottom comparison value in cycle 63, the
Expand All @@ -909,7 +910,7 @@ calcBorders: process(clk)
setTBBorder <= false;
end if;
end if;
if (rasterX = 35 + 2 and CSEL = '0') or (rasterX = 28 + 2 and CSEL = '1') then
if (rasterX = 35 + 3 and CSEL = '0') or (rasterX = 28 + 3 and CSEL = '1') then
-- 4. If the X coordinate reaches the left comparison value and the Y
-- coordinate reaches the bottom one, the vertical border flip flop is set.
-- FIX: act on the already triggered condition
Expand Down Expand Up @@ -1189,18 +1190,23 @@ calcBitmap: process(clk)
-- Sprite pixel Shift register
-- -----------------------------------------------------------------------
process(clk)
variable MShift : MFlags; -- Sprite is shifting
begin

if rising_edge(clk) then
if enaPixel = '1' then
for i in 0 to 7 loop
-- Enable sprites on the correct X position
if MActive(i) and rasterXDelay = MX(i) then
MShift(i) <= true;
MShift(i) := true;
end if;
-- Stop shifting in the third s cycle
MShift_stop(i) <= false;
if sprite = i and phi = '1' and vicCycle = cycleSpriteB then
MShift(i) <= false;
MShift_stop(i) <= true;
end if;
if MShift_stop(i) then
MShift(i) := false;
end if;
end loop;

Expand All @@ -1211,9 +1217,9 @@ calcBitmap: process(clk)
if MXE_ff(i) = '0' then
MC_ff(i) <= (not MC_ff(i)) and MC(i);
if MC_ff(i) = '0' then
MCurrentPixel(i) <= MPixels(i)(23 downto 22);
MCurrentPixel_r(i) <= MPixels(i)(23 downto 22);
if MPixels(i) = 0 then
MShift(i) <= false;
MShift(i) := false;
end if;
end if;
-- Don't shift in the s cycles (sprite0move.prg, demusinterruptus.prg)
Expand All @@ -1224,8 +1230,9 @@ calcBitmap: process(clk)
else
MXE_ff(i) <= '0';
MC_ff(i) <= '0';
MCurrentPixel(i) <= "00";
MCurrentPixel_r(i) <= "00";
end if;
MCurrentPixel(i) <= MCurrentPixel_r(i);
end loop;

-- Changing the MC register directly affects the MC flip-flop
Expand Down Expand Up @@ -1347,10 +1354,10 @@ collisionClearFlag: process(clk)
begin
if rising_edge(clk) then
-- spritevssprite.prg
if phi = '1' and cs = '1' and we = '0' and aRegisters = "011110" then
if myRd = '1' and aRegisters = "011110" then
M2MClr <= '1';
end if;
if phi = '0' and not (addr_r = "011110" and rd_r = '1') then
if phi = '0' and enaPixel = '1' and not (addr_r = "011110" and rd_r = '1') then
M2MClr <= '0';
end if;
end if;
Expand Down

0 comments on commit fe99275

Please sign in to comment.