Skip to content

Commit 62e51b6

Browse files
committed
fix BGTile overflow
1 parent e25a0b1 commit 62e51b6

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

ePceCD/Core/PPU.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public unsafe void tick()
191191
if (m_RenderLine >= 262)
192192
{
193193
m_RenderLine = 0;
194+
//ConvertColor();
194195
Marshal.Copy(_screenBufPtr, _screenBuf, 0, _screenBuf.Length);
195196
FrameReady = true;
196197
host.RenderFrame(_screenBuf, SCREEN_WIDTH, m_VDC_VDW);
@@ -320,10 +321,10 @@ private unsafe void DrawScanLine()
320321
int YOverFlow = (RealBYR + m_RenderLine) & 0x7;
321322
int* tileMap = ScanLinePtr;
322323
tileMap -= m_VDC_BXR & 7;
323-
for (i = 0; i < m_VDC_HDR + 1; i++)
324+
for (i = 0; i < m_VDC_HDR + 2; i++)
324325
{
325326
int tile = m_VRAM[BATAddress | BATLine];
326-
DrawBGTile(ref tileMap, (tile & 0xF000) >> 8, (tile & 0xFFF) << 4 | YOverFlow);
327+
DrawBGTile(ScanLinePtr, ref tileMap, (tile & 0xF000) >> 8, (tile & 0xFFF) << 4 | YOverFlow);
327328
BATAddress = (BATAddress + 1) & BATMask;
328329
}
329330
}
@@ -339,6 +340,20 @@ private unsafe void DrawScanLine()
339340
}
340341
}
341342

343+
public unsafe void ConvertColor()
344+
{
345+
int color = 0;
346+
for (int y = 0; y < m_VDC_VDW; y++)
347+
{
348+
int* LineWritePtr = (int*)_screenBufPtr.ToPointer() + SCREEN_WIDTH * y;
349+
for (int i = 0; i < SCREEN_WIDTH; i++, LineWritePtr++)
350+
{
351+
color = PALETTE[m_VCE[*LineWritePtr & 0x1FF]];
352+
*(LineWritePtr) = color;
353+
}
354+
}
355+
}
356+
342357
public unsafe void DrawSPRTile(int* ScanLinePtr, ref int* px, int palette, int tile, bool priority, bool flip)
343358
{
344359
int p1 = m_VRAM[tile];
@@ -352,22 +367,22 @@ public unsafe void DrawSPRTile(int* ScanLinePtr, ref int* px, int palette, int t
352367
if (flip)
353368
for (int x = 0; x < 16; x++, px++)
354369
{
370+
if (px < ScanLinePtr) continue;
355371
color = ((p1 >> x) & 1) | ((p2 >> x) & 2) | ((p3 >> x) & 4) | ((p4 >> x) & 8);
356372
if (color == 0) continue;
357-
if (px < ScanLinePtr) continue;
358373
*px = palette | color;
359374
}
360375
else
361376
for (int x = 15; x >= 0; x--, px++)
362377
{
378+
if (px < ScanLinePtr) continue;
363379
color = ((p1 >> x) & 1) | ((p2 >> x) & 2) | ((p3 >> x) & 4) | ((p4 >> x) & 8);
364380
if (color == 0) continue;
365-
if (px < ScanLinePtr) continue;
366381
*px = palette | color;
367382
}
368383
}
369384

370-
public unsafe void DrawBGTile(ref int* px, int palette, int tile)
385+
public unsafe void DrawBGTile(int* ScanLinePtr, ref int* px, int palette, int tile)
371386
{
372387
int p1 = m_VRAM[tile];
373388
int p2 = p1 >> 7;
@@ -377,6 +392,7 @@ public unsafe void DrawBGTile(ref int* px, int palette, int tile)
377392

378393
for (int x = 7; x >= 0; x--, px++)
379394
{
395+
if (px < ScanLinePtr) continue;
380396
if ((*px & 0x1000) != 0) continue;
381397
color = ((p1 >> x) & 1) | ((p2 >> x) & 2) | ((p3 >> x) & 4) | ((p4 >> x) & 8);
382398
if (color != 0) *px = palette | color;

0 commit comments

Comments
 (0)