remove anonymous union members from ppu module

This commit is contained in:
Robert 2023-01-28 02:02:55 +01:00
parent 9bd8d02092
commit 0c6d5f65a5
2 changed files with 48 additions and 53 deletions

View file

@ -170,38 +170,38 @@ void PPU::Tick()
if (x == 257) if (x == 257)
{ {
current.CoarseX = temporary.CoarseX; current.Data.CoarseX = temporary.Data.CoarseX;
current.NametableSel &= 0x2; current.Data.NametableSel &= 0x2;
current.NametableSel |= temporary.NametableSel & 0x1; current.Data.NametableSel |= temporary.Data.NametableSel & 0x1;
} }
if (scanlineType == ScanlineType::PreRender && ppumask.Flag.ShowBackground && x >= 280 && x <= 304) if (scanlineType == ScanlineType::PreRender && ppumask.Flag.ShowBackground && x >= 280 && x <= 304)
{ {
current.FineY = temporary.FineY; current.Data.FineY = temporary.Data.FineY;
current.CoarseY = temporary.CoarseY; current.Data.CoarseY = temporary.Data.CoarseY;
current.NametableSel &= 0x1; current.Data.NametableSel &= 0x1;
current.NametableSel |= temporary.NametableSel & 0x2; current.Data.NametableSel |= temporary.Data.NametableSel & 0x2;
} }
} }
if (x == 256) if (x == 256)
{ {
current.FineY++; current.Data.FineY++;
if (current.FineY == 0) if (current.Data.FineY == 0)
{ {
if (current.CoarseY == 29) if (current.Data.CoarseY == 29)
{ {
current.CoarseY = 0; current.Data.CoarseY = 0;
current.NametableSel ^= 0x2; current.Data.NametableSel ^= 0x2;
} }
else if (current.CoarseY == 31) else if (current.Data.CoarseY == 31)
{ {
current.CoarseY = 0; current.Data.CoarseY = 0;
} }
else else
{ {
current.CoarseY++; current.Data.CoarseY++;
} }
} }
} }
@ -276,7 +276,7 @@ void PPU::WriteRegister(Byte id, Byte val)
{ {
case 0: case 0:
ppuctrl.Raw = val; ppuctrl.Raw = val;
temporary.NametableSel = ppuctrl.Flag.BaseNametableAddr; temporary.Data.NametableSel = ppuctrl.Flag.BaseNametableAddr;
break; break;
case 1: case 1:
@ -302,14 +302,14 @@ void PPU::WriteRegister(Byte id, Byte val)
if (addressLatch == 0) if (addressLatch == 0)
{ {
ppuscroll.x = val; ppuscroll.x = val;
temporary.CoarseX = (val >> 3); temporary.Data.CoarseX = (val >> 3);
fineX = val & 0x7; fineX = val & 0x7;
} }
else else
{ {
ppuscroll.y = val; ppuscroll.y = val;
temporary.CoarseY = (val >> 3); temporary.Data.CoarseY = (val >> 3);
temporary.FineY = val & 0x3; temporary.Data.FineY = val & 0x3;
} }
addressLatch = !addressLatch; addressLatch = !addressLatch;
@ -321,7 +321,7 @@ void PPU::WriteRegister(Byte id, Byte val)
ppuaddr.Bytes.hi = val; ppuaddr.Bytes.hi = val;
temporary.Raw &= 0xFF; temporary.Raw &= 0xFF;
temporary.Raw |= ((Word)(val & 0x3F) << 8); temporary.Raw |= ((Word)(val & 0x3F) << 8);
temporary.FineY &= 0x3; temporary.Data.FineY &= 0x3;
} }
else else
{ {
@ -435,36 +435,36 @@ void PPU::EvaluateBackgroundTiles()
break; break;
case FetchingPhase::AttributeTableByte: case FetchingPhase::AttributeTableByte:
attributeTableByte = Read(0x23C0 | (current.Raw & 0x0C00) | ((current.CoarseY >> 2) << 3) | (current.CoarseX >> 2)); attributeTableByte = Read(0x23C0 | (current.Raw & 0x0C00) | ((current.Data.CoarseY >> 2) << 3) | (current.Data.CoarseX >> 2));
fetchPhase = FetchingPhase::PatternTableLo; fetchPhase = FetchingPhase::PatternTableLo;
break; break;
case FetchingPhase::PatternTableLo: case FetchingPhase::PatternTableLo:
patternTableLo = Read(((Word)ppuctrl.Flag.BackgrPatternTableAddr << 12) | ((Word)nametableByte << 4) + current.FineY); patternTableLo = Read(((Word)ppuctrl.Flag.BackgrPatternTableAddr << 12) | ((Word)nametableByte << 4) + current.Data.FineY);
fetchPhase = FetchingPhase::PatternTableHi; fetchPhase = FetchingPhase::PatternTableHi;
break; break;
case FetchingPhase::PatternTableHi: case FetchingPhase::PatternTableHi:
patternTableHi = Read((((Word)ppuctrl.Flag.BackgrPatternTableAddr << 12) | ((Word)nametableByte << 4)) + 8 + current.FineY); patternTableHi = Read((((Word)ppuctrl.Flag.BackgrPatternTableAddr << 12) | ((Word)nametableByte << 4)) + 8 + current.Data.FineY);
loTile.Lo = patternTableLo; loTile.Bytes.Lo = patternTableLo;
hiTile.Lo = patternTableHi; hiTile.Bytes.Lo = patternTableHi;
Byte attributeHalfNybble = attributeTableByte; Byte attributeHalfNybble = attributeTableByte;
attributeHalfNybble >>= (((current.CoarseX >> 1) % 2) ? 2 : 0); attributeHalfNybble >>= (((current.Data.CoarseX >> 1) % 2) ? 2 : 0);
attributeHalfNybble >>= (((current.CoarseY >> 1) % 2) ? 4 : 0); attributeHalfNybble >>= (((current.Data.CoarseY >> 1) % 2) ? 4 : 0);
loAttribute.Lo = ((attributeHalfNybble & 1) ? 0xFF : 0x00); loAttribute.Bytes.Lo = ((attributeHalfNybble & 1) ? 0xFF : 0x00);
hiAttribute.Lo = ((attributeHalfNybble & 2) ? 0xFF : 0x00); hiAttribute.Bytes.Lo = ((attributeHalfNybble & 2) ? 0xFF : 0x00);
if (current.CoarseX == 0x1F) if (current.Data.CoarseX == 0x1F)
{ {
current.NametableSel ^= 0x1; current.Data.NametableSel ^= 0x1;
current.CoarseX = 0x00; current.Data.CoarseX = 0x00;
} }
else else
{ {
current.CoarseX++; current.Data.CoarseX++;
} }
fetchPhase = FetchingPhase::NametableByte; fetchPhase = FetchingPhase::NametableByte;
@ -552,15 +552,13 @@ void PPU::EvaluateSprites()
case FetchingPhase::NametableByte: // Fetch garbage case FetchingPhase::NametableByte: // Fetch garbage
nametableByte = Read(0x2000 | (current.Raw & 0x0FFF)); nametableByte = Read(0x2000 | (current.Raw & 0x0FFF));
sprites[currentlyEvaluatedSprite].Counter = secondaryOAM[4 * currentlyEvaluatedSprite + 3]; sprites[currentlyEvaluatedSprite].Counter = secondaryOAM[4 * currentlyEvaluatedSprite + 3];
if (sprites[currentlyEvaluatedSprite].Counter == 0x00)
volatile int djf = 3;
sprites[currentlyEvaluatedSprite].FineX = 0; sprites[currentlyEvaluatedSprite].FineX = 0;
fetchPhase = FetchingPhase::AttributeTableByte; fetchPhase = FetchingPhase::AttributeTableByte;
break; break;
case FetchingPhase::AttributeTableByte: // Fetch garbage case FetchingPhase::AttributeTableByte: // Fetch garbage
attributeTableByte = Read(0x23C0 | (current.Raw & 0x0C00) | ((current.CoarseY >> 2) << 3) | (current.CoarseX >> 2)); attributeTableByte = Read(0x23C0 | (current.Raw & 0x0C00) | ((current.Data.CoarseY >> 2) << 3) | (current.Data.CoarseX >> 2));
sprites[currentlyEvaluatedSprite].Latch.Raw = secondaryOAM[4 * currentlyEvaluatedSprite + 2]; sprites[currentlyEvaluatedSprite].Latch.Raw = secondaryOAM[4 * currentlyEvaluatedSprite + 2];
fetchPhase = FetchingPhase::PatternTableLo; fetchPhase = FetchingPhase::PatternTableLo;
@ -569,7 +567,7 @@ void PPU::EvaluateSprites()
case FetchingPhase::PatternTableLo: case FetchingPhase::PatternTableLo:
{ {
Word spriteFineY = y - secondaryOAM[4 * currentlyEvaluatedSprite]; Word spriteFineY = y - secondaryOAM[4 * currentlyEvaluatedSprite];
if (sprites[currentlyEvaluatedSprite].Latch.FlipVertically) if (sprites[currentlyEvaluatedSprite].Latch.Data.FlipVertically)
spriteFineY = 7 - spriteFineY; spriteFineY = 7 - spriteFineY;
Word tileNumber = secondaryOAM[4 * currentlyEvaluatedSprite + 1] + (spriteFineY >> 3); Word tileNumber = secondaryOAM[4 * currentlyEvaluatedSprite + 1] + (spriteFineY >> 3);
@ -582,10 +580,10 @@ void PPU::EvaluateSprites()
case FetchingPhase::PatternTableHi: case FetchingPhase::PatternTableHi:
{ {
Word spriteFineY = y - secondaryOAM[4 * currentlyEvaluatedSprite]; Word spriteFineY = y - secondaryOAM[4 * currentlyEvaluatedSprite];
if (sprites[currentlyEvaluatedSprite].Latch.FlipVertically) if (sprites[currentlyEvaluatedSprite].Latch.Data.FlipVertically)
spriteFineY = 7 - spriteFineY; spriteFineY = 7 - spriteFineY;
Byte tileNumber = secondaryOAM[4 * currentlyEvaluatedSprite + 1] + (spriteFineY >> 3); Byte tileNumber = secondaryOAM[4 * currentlyEvaluatedSprite + 1] + Byte((spriteFineY >> 3) & 0xFF);
sprites[currentlyEvaluatedSprite].Hi = Read((((Word)ppuctrl.Flag.SpritePatternTableAddr << 12) | (tileNumber << 4)) + 8 + spriteFineY); sprites[currentlyEvaluatedSprite].Hi = Read((((Word)ppuctrl.Flag.SpritePatternTableAddr << 12) | (tileNumber << 4)) + 8 + spriteFineY);
@ -615,12 +613,12 @@ Pixel PPU::GetBackgroundPixel()
if (!ppumask.Flag.ShowBackground) if (!ppumask.Flag.ShowBackground)
return returnValue; return returnValue;
Byte shiftAmount = 7 - fineX; Byte shiftAmount = (Byte)(7 - fineX);
Byte loBit = (loTile.Hi >> shiftAmount) & 0x1; Byte loBit = (loTile.Bytes.Hi >> shiftAmount) & 0x1;
Byte hiBit = (hiTile.Hi >> shiftAmount) & 0x1; Byte hiBit = (hiTile.Bytes.Hi >> shiftAmount) & 0x1;
Byte loAttrBit = (loAttribute.Hi >> shiftAmount) & 0x1; Byte loAttrBit = (loAttribute.Bytes.Hi >> shiftAmount) & 0x1;
Byte hiAttrBit = (hiAttribute.Hi >> shiftAmount) & 0x1;; Byte hiAttrBit = (hiAttribute.Bytes.Hi >> shiftAmount) & 0x1;;
returnValue.color = (hiBit << 1) | loBit; returnValue.color = (hiBit << 1) | loBit;
returnValue.palette = (hiAttrBit << 1) | loAttrBit; returnValue.palette = (hiAttrBit << 1) | loAttrBit;
@ -650,7 +648,7 @@ Pixel PPU::GetSpritePixel()
Byte mask = 0x00; Byte mask = 0x00;
if (sprite.Latch.FlipHorizontally) if (sprite.Latch.Data.FlipHorizontally)
mask = 0x1 << sprite.FineX; mask = 0x1 << sprite.FineX;
else else
mask = 0x80 >> sprite.FineX; mask = 0x80 >> sprite.FineX;
@ -662,16 +660,13 @@ Pixel PPU::GetSpritePixel()
if (color == 0x00) if (color == 0x00)
continue; continue;
uint8_t palette = 4 + sprite.Latch.Palette; uint8_t palette = 4 + sprite.Latch.Data.Palette;
if (color == 0x00) if (color == 0x00)
palette = 0x00; palette = 0x00;
if (x < 5)
volatile int djfk = 3;
returnValue.color = color; returnValue.color = color;
returnValue.palette = palette; returnValue.palette = palette;
returnValue.priority = sprite.Latch.Priority; returnValue.priority = sprite.Latch.Data.Priority;
returnValue.isZeroSprite = (sprite.OAMPosition == 0x00); returnValue.isZeroSprite = (sprite.OAMPosition == 0x00);
break; break;

View file

@ -39,7 +39,7 @@ union VRAMAddress
Word CoarseY : 5; Word CoarseY : 5;
Word NametableSel : 2; Word NametableSel : 2;
Word FineY : 3; Word FineY : 3;
}; } Data;
Word Raw; Word Raw;
}; };
@ -50,7 +50,7 @@ union ShiftRegister
{ {
Byte Lo; Byte Lo;
Byte Hi; Byte Hi;
}; } Bytes;
Word Raw; Word Raw;
}; };
@ -68,7 +68,7 @@ struct Sprite
Byte Priority : 1; Byte Priority : 1;
Byte FlipHorizontally : 1; Byte FlipHorizontally : 1;
Byte FlipVertically : 1; Byte FlipVertically : 1;
}; } Data;
Byte Raw; Byte Raw;
} Latch; } Latch;