2016-12-05 17:02:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016-2017 StapleButter
|
|
|
|
|
|
|
|
This file is part of melonDS.
|
|
|
|
|
|
|
|
melonDS is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with melonDS. If not, see http://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
2016-12-05 22:17:03 +00:00
|
|
|
#include <stdio.h>
|
2017-01-18 16:57:12 +00:00
|
|
|
#include <string.h>
|
2016-12-05 17:02:29 +00:00
|
|
|
#include "NDS.h"
|
2017-01-18 03:03:19 +00:00
|
|
|
#include "GPU.h"
|
2016-12-05 17:02:29 +00:00
|
|
|
|
|
|
|
|
2017-02-07 21:23:46 +00:00
|
|
|
// notes on color conversion
|
|
|
|
//
|
|
|
|
// * BLDCNT special effects are applied on 18bit colors
|
|
|
|
// -> layers are converted to 18bit before being composited
|
2017-03-04 01:22:58 +00:00
|
|
|
// -> 'brightness up' effect does: x = x + (63-x)*factor
|
2017-02-07 21:23:46 +00:00
|
|
|
// * colors are converted as follows: 18bit = 15bit * 2
|
|
|
|
// -> white comes out as 62,62,62 and not 63,63,63
|
|
|
|
// * VRAM/FIFO display modes convert colors the same way
|
|
|
|
// * 3D engine converts colors differently (18bit = 15bit * 2 + 1, except 0 = 0)
|
|
|
|
// * 'screen disabled' white is 63,63,63
|
2017-04-09 14:12:12 +00:00
|
|
|
// * [Gericom] bit15 is used as bottom green bit for palettes. TODO: check where this applies.
|
|
|
|
// tested on the normal BG palette and applies there
|
2017-02-07 21:23:46 +00:00
|
|
|
//
|
|
|
|
// oh also, changing DISPCNT bit16-17 midframe doesn't work (ignored? applied for next frame?)
|
|
|
|
// TODO, eventually: check whether other DISPCNT bits can be changed midframe
|
|
|
|
//
|
2017-02-27 20:26:11 +00:00
|
|
|
// for VRAM display mode, VRAM must be mapped to LCDC
|
|
|
|
//
|
2017-03-23 16:14:48 +00:00
|
|
|
// FIFO display mode:
|
|
|
|
// * the 'FIFO' is a circular buffer of 32 bytes (16 pixels)
|
|
|
|
// * the buffer doesn't get empty, the display controller keeps reading from it
|
|
|
|
// -> if it isn't updated, the contents will be repeated every 16 pixels
|
|
|
|
// * the write pointer is incremented when writing to the higher 16 bits of the FIFO register (0x04000068)
|
|
|
|
// * the write pointer is reset upon VBlank
|
|
|
|
// * FIFO DMA (mode 4) is triggered every 8 pixels. start bit is cleared upon VBlank.
|
|
|
|
//
|
2017-02-07 21:23:46 +00:00
|
|
|
// sprite blending rules
|
|
|
|
// * destination must be selected as 2nd target
|
|
|
|
// * sprite must be semitransparent or bitmap sprite
|
|
|
|
// * blending is applied instead of the selected color effect, even if it is 'none'.
|
|
|
|
// * for bitmap sprites: EVA = alpha+1, EVB = 16-EVA
|
|
|
|
// * for bitmap sprites: alpha=0 is always transparent, even if blending doesn't apply
|
|
|
|
//
|
|
|
|
// 3D blending rules
|
|
|
|
//
|
|
|
|
// 3D/3D blending seems to follow these equations:
|
|
|
|
// dstColor = srcColor*srcAlpha + dstColor*(1-srcAlpha)
|
|
|
|
// dstAlpha = max(srcAlpha, dstAlpha)
|
|
|
|
// blending isn't applied if dstAlpha is zero.
|
|
|
|
//
|
|
|
|
// 3D/2D blending rules
|
|
|
|
// * if destination selected as 2nd target:
|
2017-03-06 17:13:57 +00:00
|
|
|
// blending is applied instead of the selected color effect, using full 5bit alpha from 3D layer
|
2017-02-07 21:23:46 +00:00
|
|
|
// this even if the selected color effect is 'none'.
|
|
|
|
// apparently this works even if BG0 isn't selected as 1st target
|
|
|
|
// * if BG0 is selected as 1st target, destination not selected as 2nd target:
|
|
|
|
// brightness up/down effect is applied if selected. if blending is selected, it doesn't apply.
|
|
|
|
// * 3D layer pixels with alpha=0 are always transparent.
|
2017-04-09 14:12:12 +00:00
|
|
|
//
|
|
|
|
// mosaic:
|
|
|
|
// * mosaic grid starts at 0,0 regardless of the BG/sprite position
|
2017-02-07 21:23:46 +00:00
|
|
|
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
GPU2D::GPU2D(u32 num)
|
2016-12-05 17:02:29 +00:00
|
|
|
{
|
2017-01-18 03:03:19 +00:00
|
|
|
Num = num;
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
GPU2D::~GPU2D()
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
void GPU2D::Reset()
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 16:57:12 +00:00
|
|
|
DispCnt = 0;
|
|
|
|
memset(BGCnt, 0, 4*2);
|
2017-01-20 14:27:56 +00:00
|
|
|
memset(BGXPos, 0, 4*2);
|
|
|
|
memset(BGYPos, 0, 4*2);
|
2017-03-04 01:22:58 +00:00
|
|
|
memset(BGXRef, 0, 2*4);
|
|
|
|
memset(BGYRef, 0, 2*4);
|
|
|
|
memset(BGXRefInternal, 0, 2*4);
|
|
|
|
memset(BGYRefInternal, 0, 2*4);
|
2017-02-02 00:18:03 +00:00
|
|
|
memset(BGRotA, 0, 2*2);
|
|
|
|
memset(BGRotB, 0, 2*2);
|
|
|
|
memset(BGRotC, 0, 2*2);
|
|
|
|
memset(BGRotD, 0, 2*2);
|
2017-02-27 20:26:11 +00:00
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
memset(Win0Coords, 0, 4);
|
|
|
|
memset(Win1Coords, 0, 4);
|
|
|
|
memset(WinCnt, 0, 4);
|
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
BlendCnt = 0;
|
|
|
|
EVA = 16;
|
|
|
|
EVB = 0;
|
|
|
|
EVY = 0;
|
|
|
|
|
2017-03-20 21:18:35 +00:00
|
|
|
memset(DispFIFOBuffer, 0, 256*2);
|
|
|
|
|
2017-03-01 20:42:06 +00:00
|
|
|
CaptureCnt = 0;
|
|
|
|
|
2017-03-01 19:23:41 +00:00
|
|
|
MasterBrightness = 0;
|
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
BGExtPalStatus[0] = 0;
|
|
|
|
BGExtPalStatus[1] = 0;
|
|
|
|
BGExtPalStatus[2] = 0;
|
|
|
|
BGExtPalStatus[3] = 0;
|
|
|
|
OBJExtPalStatus = 0;
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 20:55:51 +00:00
|
|
|
void GPU2D::SetFramebuffer(u32* buf)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 03:03:19 +00:00
|
|
|
Framebuffer = buf;
|
|
|
|
}
|
2016-12-05 22:17:03 +00:00
|
|
|
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
u8 GPU2D::Read8(u32 addr)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
switch (addr & 0x00000FFF)
|
|
|
|
{
|
|
|
|
case 0x048: return WinCnt[0];
|
|
|
|
case 0x049: return WinCnt[1];
|
|
|
|
case 0x04A: return WinCnt[2];
|
|
|
|
case 0x04B: return WinCnt[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("unknown GPU read8 %08X\n", addr);
|
2017-01-18 16:57:12 +00:00
|
|
|
return 0;
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
u16 GPU2D::Read16(u32 addr)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 03:03:19 +00:00
|
|
|
switch (addr & 0x00000FFF)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 16:57:12 +00:00
|
|
|
case 0x000: return DispCnt&0xFFFF;
|
|
|
|
case 0x002: return DispCnt>>16;
|
|
|
|
|
|
|
|
case 0x008: return BGCnt[0];
|
|
|
|
case 0x00A: return BGCnt[1];
|
|
|
|
case 0x00C: return BGCnt[2];
|
|
|
|
case 0x00E: return BGCnt[3];
|
2017-03-04 13:47:20 +00:00
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
case 0x048: return WinCnt[0] | (WinCnt[1] << 8);
|
|
|
|
case 0x04A: return WinCnt[2] | (WinCnt[3] << 8);
|
|
|
|
|
2017-03-15 23:07:36 +00:00
|
|
|
case 0x050: return BlendCnt;
|
|
|
|
|
2017-03-04 13:47:20 +00:00
|
|
|
case 0x064: return CaptureCnt & 0xFFFF;
|
|
|
|
case 0x066: return CaptureCnt >> 16;
|
2017-04-09 01:47:47 +00:00
|
|
|
|
|
|
|
case 0x06C: return MasterBrightness;
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
2017-01-18 16:57:12 +00:00
|
|
|
|
|
|
|
printf("unknown GPU read16 %08X\n", addr);
|
|
|
|
return 0;
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
u32 GPU2D::Read32(u32 addr)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 03:03:19 +00:00
|
|
|
switch (addr & 0x00000FFF)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 16:57:12 +00:00
|
|
|
case 0x000: return DispCnt;
|
2017-03-04 13:47:20 +00:00
|
|
|
|
|
|
|
case 0x064: return CaptureCnt;
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
return Read16(addr) | (Read16(addr+2) << 16);
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
void GPU2D::Write8(u32 addr, u8 val)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
switch (addr & 0x00000FFF)
|
|
|
|
{
|
|
|
|
case 0x040: Win0Coords[1] = val; return;
|
|
|
|
case 0x041: Win0Coords[0] = val; return;
|
|
|
|
case 0x042: Win1Coords[1] = val; return;
|
|
|
|
case 0x043: Win1Coords[0] = val; return;
|
|
|
|
|
|
|
|
case 0x044: Win0Coords[3] = val; return;
|
|
|
|
case 0x045: Win0Coords[2] = val; return;
|
|
|
|
case 0x046: Win1Coords[3] = val; return;
|
|
|
|
case 0x047: Win1Coords[2] = val; return;
|
|
|
|
|
|
|
|
case 0x048: WinCnt[0] = val; return;
|
|
|
|
case 0x049: WinCnt[1] = val; return;
|
|
|
|
case 0x04A: WinCnt[2] = val; return;
|
|
|
|
case 0x04B: WinCnt[3] = val; return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("unknown GPU write8 %08X %02X\n", addr, val);
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
void GPU2D::Write16(u32 addr, u16 val)
|
2016-12-23 20:22:22 +00:00
|
|
|
{
|
2017-01-18 03:03:19 +00:00
|
|
|
switch (addr & 0x00000FFF)
|
2016-12-23 20:22:22 +00:00
|
|
|
{
|
2017-01-18 16:57:12 +00:00
|
|
|
case 0x000:
|
|
|
|
DispCnt = (DispCnt & 0xFFFF0000) | val;
|
|
|
|
return;
|
|
|
|
case 0x002:
|
|
|
|
DispCnt = (DispCnt & 0x0000FFFF) | (val << 16);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 0x008: BGCnt[0] = val; return;
|
|
|
|
case 0x00A: BGCnt[1] = val; return;
|
|
|
|
case 0x00C: BGCnt[2] = val; return;
|
|
|
|
case 0x00E: BGCnt[3] = val; return;
|
2017-01-20 14:27:56 +00:00
|
|
|
|
|
|
|
case 0x010: BGXPos[0] = val; return;
|
|
|
|
case 0x012: BGYPos[0] = val; return;
|
|
|
|
case 0x014: BGXPos[1] = val; return;
|
|
|
|
case 0x016: BGYPos[1] = val; return;
|
|
|
|
case 0x018: BGXPos[2] = val; return;
|
|
|
|
case 0x01A: BGYPos[2] = val; return;
|
|
|
|
case 0x01C: BGXPos[3] = val; return;
|
|
|
|
case 0x01E: BGYPos[3] = val; return;
|
2017-02-02 00:18:03 +00:00
|
|
|
|
|
|
|
case 0x020: BGRotA[0] = val; return;
|
|
|
|
case 0x022: BGRotB[0] = val; return;
|
|
|
|
case 0x024: BGRotC[0] = val; return;
|
|
|
|
case 0x026: BGRotD[0] = val; return;
|
2017-03-04 01:37:39 +00:00
|
|
|
case 0x028:
|
|
|
|
BGXRef[0] = (BGXRef[0] & 0xFFFF0000) | val;
|
|
|
|
if (GPU::VCount < 192) BGXRefInternal[0] = BGXRef[0];
|
|
|
|
return;
|
|
|
|
case 0x02A:
|
|
|
|
if (val & 0x0800) val |= 0xF000;
|
|
|
|
BGXRef[0] = (BGXRef[0] & 0xFFFF) | (val << 16);
|
|
|
|
if (GPU::VCount < 192) BGXRefInternal[0] = BGXRef[0];
|
|
|
|
return;
|
|
|
|
case 0x02C:
|
|
|
|
BGYRef[0] = (BGYRef[0] & 0xFFFF0000) | val;
|
|
|
|
if (GPU::VCount < 192) BGYRefInternal[0] = BGYRef[0];
|
|
|
|
return;
|
|
|
|
case 0x02E:
|
|
|
|
if (val & 0x0800) val |= 0xF000;
|
|
|
|
BGYRef[0] = (BGYRef[0] & 0xFFFF) | (val << 16);
|
|
|
|
if (GPU::VCount < 192) BGYRefInternal[0] = BGYRef[0];
|
|
|
|
return;
|
2017-02-02 00:18:03 +00:00
|
|
|
|
|
|
|
case 0x030: BGRotA[1] = val; return;
|
|
|
|
case 0x032: BGRotB[1] = val; return;
|
|
|
|
case 0x034: BGRotC[1] = val; return;
|
|
|
|
case 0x036: BGRotD[1] = val; return;
|
2017-03-04 01:37:39 +00:00
|
|
|
case 0x038:
|
|
|
|
BGXRef[1] = (BGXRef[1] & 0xFFFF0000) | val;
|
|
|
|
if (GPU::VCount < 192) BGXRefInternal[1] = BGXRef[1];
|
|
|
|
return;
|
|
|
|
case 0x03A:
|
|
|
|
if (val & 0x0800) val |= 0xF000;
|
|
|
|
BGXRef[1] = (BGXRef[1] & 0xFFFF) | (val << 16);
|
|
|
|
if (GPU::VCount < 192) BGXRefInternal[1] = BGXRef[1];
|
|
|
|
return;
|
|
|
|
case 0x03C:
|
|
|
|
BGYRef[1] = (BGYRef[1] & 0xFFFF0000) | val;
|
|
|
|
if (GPU::VCount < 192) BGYRefInternal[1] = BGYRef[1];
|
|
|
|
return;
|
|
|
|
case 0x03E:
|
|
|
|
if (val & 0x0800) val |= 0xF000;
|
|
|
|
BGYRef[1] = (BGYRef[1] & 0xFFFF) | (val << 16);
|
|
|
|
if (GPU::VCount < 192) BGYRefInternal[1] = BGYRef[1];
|
|
|
|
return;
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
case 0x040:
|
|
|
|
Win0Coords[1] = val & 0xFF;
|
|
|
|
Win0Coords[0] = val >> 8;
|
|
|
|
return;
|
|
|
|
case 0x042:
|
|
|
|
Win1Coords[1] = val & 0xFF;
|
|
|
|
Win1Coords[0] = val >> 8;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 0x044:
|
|
|
|
Win0Coords[3] = val & 0xFF;
|
|
|
|
Win0Coords[2] = val >> 8;
|
|
|
|
return;
|
|
|
|
case 0x046:
|
|
|
|
Win1Coords[3] = val & 0xFF;
|
|
|
|
Win1Coords[2] = val >> 8;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 0x048:
|
|
|
|
WinCnt[0] = val & 0xFF;
|
|
|
|
WinCnt[1] = val >> 8;
|
|
|
|
return;
|
|
|
|
case 0x04A:
|
|
|
|
WinCnt[2] = val & 0xFF;
|
|
|
|
WinCnt[3] = val >> 8;
|
|
|
|
return;
|
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
case 0x050: BlendCnt = val; return;
|
|
|
|
case 0x052:
|
|
|
|
EVA = val & 0x1F;
|
|
|
|
if (EVA > 16) EVA = 16;
|
|
|
|
EVB = (val >> 8) & 0x1F;
|
|
|
|
if (EVB > 16) EVB = 16;
|
|
|
|
return;
|
2017-03-06 17:13:57 +00:00
|
|
|
case 0x054:
|
2017-03-02 18:00:19 +00:00
|
|
|
EVY = val & 0x1F;
|
|
|
|
if (EVY > 16) EVY = 16;
|
|
|
|
return;
|
|
|
|
|
2017-03-01 19:23:41 +00:00
|
|
|
case 0x06C: MasterBrightness = val; return;
|
2016-12-23 20:22:22 +00:00
|
|
|
}
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-01-22 19:34:59 +00:00
|
|
|
//printf("unknown GPU write16 %08X %04X\n", addr, val);
|
2016-12-23 20:22:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
void GPU2D::Write32(u32 addr, u32 val)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 03:03:19 +00:00
|
|
|
switch (addr & 0x00000FFF)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-01-18 16:57:12 +00:00
|
|
|
case 0x000:
|
|
|
|
DispCnt = val;
|
|
|
|
return;
|
2017-02-02 00:18:03 +00:00
|
|
|
|
|
|
|
case 0x028:
|
|
|
|
if (val & 0x08000000) val |= 0xF0000000;
|
2017-03-04 01:22:58 +00:00
|
|
|
BGXRef[0] = val;
|
2017-03-04 01:37:39 +00:00
|
|
|
if (GPU::VCount < 192) BGXRefInternal[0] = BGXRef[0];
|
2017-02-02 00:18:03 +00:00
|
|
|
return;
|
|
|
|
case 0x02C:
|
|
|
|
if (val & 0x08000000) val |= 0xF0000000;
|
2017-03-04 01:22:58 +00:00
|
|
|
BGYRef[0] = val;
|
2017-03-04 01:37:39 +00:00
|
|
|
if (GPU::VCount < 192) BGYRefInternal[0] = BGYRef[0];
|
2017-02-02 00:18:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 0x038:
|
|
|
|
if (val & 0x08000000) val |= 0xF0000000;
|
2017-03-04 01:22:58 +00:00
|
|
|
BGXRef[1] = val;
|
2017-03-04 01:37:39 +00:00
|
|
|
if (GPU::VCount < 192) BGXRefInternal[1] = BGXRef[1];
|
2017-02-02 00:18:03 +00:00
|
|
|
return;
|
|
|
|
case 0x03C:
|
|
|
|
if (val & 0x08000000) val |= 0xF0000000;
|
2017-03-04 01:22:58 +00:00
|
|
|
BGYRef[1] = val;
|
2017-03-04 01:37:39 +00:00
|
|
|
if (GPU::VCount < 192) BGYRefInternal[1] = BGYRef[1];
|
2017-02-02 00:18:03 +00:00
|
|
|
return;
|
2017-03-01 20:42:06 +00:00
|
|
|
|
|
|
|
case 0x064:
|
|
|
|
// TODO: check what happens when writing to it during display
|
|
|
|
// esp. if a capture is happening
|
|
|
|
CaptureCnt = val & 0xEF3F1F1F;
|
|
|
|
return;
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
Write16(addr, val&0xFFFF);
|
|
|
|
Write16(addr+2, val>>16);
|
2016-12-05 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-18 03:03:19 +00:00
|
|
|
void GPU2D::DrawScanline(u32 line)
|
2016-12-05 22:17:03 +00:00
|
|
|
{
|
2017-02-14 20:55:51 +00:00
|
|
|
u32* dst = &Framebuffer[256*line];
|
2017-01-18 16:57:12 +00:00
|
|
|
|
|
|
|
u32 dispmode = DispCnt >> 16;
|
|
|
|
dispmode &= (Num ? 0x1 : 0x3);
|
|
|
|
|
2017-04-10 18:24:41 +00:00
|
|
|
// always render regular graphics
|
|
|
|
DrawScanline_Mode1(line, dst);
|
|
|
|
|
|
|
|
// capture
|
|
|
|
if ((Num == 0) && (CaptureCnt & (1<<31)))
|
|
|
|
{
|
|
|
|
u32 capwidth, capheight;
|
|
|
|
switch ((CaptureCnt >> 20) & 0x3)
|
|
|
|
{
|
|
|
|
case 0: capwidth = 128; capheight = 128; break;
|
|
|
|
case 1: capwidth = 256; capheight = 64; break;
|
|
|
|
case 2: capwidth = 256; capheight = 128; break;
|
|
|
|
case 3: capwidth = 256; capheight = 192; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line < capheight)
|
|
|
|
DoCapture(line, capwidth, dst);
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:57:12 +00:00
|
|
|
switch (dispmode)
|
|
|
|
{
|
|
|
|
case 0: // screen off
|
|
|
|
{
|
2017-02-14 20:55:51 +00:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
dst[i] = 0xFF3F3F3F;
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-10 18:24:41 +00:00
|
|
|
case 1: // regular display, already taken care of
|
2017-01-18 16:57:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // VRAM display
|
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
u32 vrambank = (DispCnt >> 18) & 0x3;
|
|
|
|
if (GPU::VRAMMap_LCDC & (1<<vrambank))
|
2017-02-14 20:55:51 +00:00
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
u16* vram = (u16*)GPU::VRAM[vrambank];
|
|
|
|
vram = &vram[line * 256];
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
u16 color = vram[i];
|
|
|
|
u8 r = (color & 0x001F) << 1;
|
|
|
|
u8 g = (color & 0x03E0) >> 4;
|
|
|
|
u8 b = (color & 0x7C00) >> 9;
|
2017-02-14 20:55:51 +00:00
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
dst[i] = r | (g << 8) | (b << 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
dst[i] = 0;
|
|
|
|
}
|
2017-02-14 20:55:51 +00:00
|
|
|
}
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-03-20 21:18:35 +00:00
|
|
|
case 3: // FIFO display (grossly inaccurate)
|
2017-01-18 16:57:12 +00:00
|
|
|
{
|
2017-03-20 21:18:35 +00:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
u16 color = DispFIFOBuffer[i];
|
|
|
|
u8 r = (color & 0x001F) << 1;
|
|
|
|
u8 g = (color & 0x03E0) >> 4;
|
|
|
|
u8 b = (color & 0x7C00) >> 9;
|
|
|
|
|
|
|
|
dst[i] = r | (g << 8) | (b << 16);
|
|
|
|
}
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-02-14 20:55:51 +00:00
|
|
|
|
2017-03-01 19:23:41 +00:00
|
|
|
// master brightness
|
2017-03-01 19:25:19 +00:00
|
|
|
if (dispmode != 0)
|
2017-03-01 19:23:41 +00:00
|
|
|
{
|
2017-03-01 19:25:19 +00:00
|
|
|
if ((MasterBrightness >> 14) == 1)
|
2017-03-01 19:23:41 +00:00
|
|
|
{
|
2017-03-01 19:25:19 +00:00
|
|
|
// up
|
|
|
|
u32 factor = MasterBrightness & 0x1F;
|
|
|
|
if (factor > 16) factor = 16;
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-03-01 19:25:19 +00:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
u32 val = dst[i];
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-03-01 19:25:19 +00:00
|
|
|
u32 r = val & 0x00003F;
|
|
|
|
u32 g = val & 0x003F00;
|
|
|
|
u32 b = val & 0x3F0000;
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-03-01 19:25:19 +00:00
|
|
|
r += (((0x00003F - r) * factor) >> 4);
|
|
|
|
g += ((((0x003F00 - g) * factor) >> 4) & 0x003F00);
|
|
|
|
b += ((((0x3F0000 - b) * factor) >> 4) & 0x3F0000);
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-03-01 19:25:19 +00:00
|
|
|
dst[i] = r | g | b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((MasterBrightness >> 14) == 2)
|
2017-03-01 19:23:41 +00:00
|
|
|
{
|
2017-03-01 19:25:19 +00:00
|
|
|
// down
|
|
|
|
u32 factor = MasterBrightness & 0x1F;
|
|
|
|
if (factor > 16) factor = 16;
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
u32 val = dst[i];
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-03-01 19:25:19 +00:00
|
|
|
u32 r = val & 0x00003F;
|
|
|
|
u32 g = val & 0x003F00;
|
|
|
|
u32 b = val & 0x3F0000;
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-03-01 19:25:19 +00:00
|
|
|
r -= ((r * factor) >> 4);
|
|
|
|
g -= (((g * factor) >> 4) & 0x003F00);
|
|
|
|
b -= (((b * factor) >> 4) & 0x3F0000);
|
2017-03-01 19:23:41 +00:00
|
|
|
|
2017-03-01 19:25:19 +00:00
|
|
|
dst[i] = r | g | b;
|
|
|
|
}
|
2017-03-01 19:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:55:51 +00:00
|
|
|
// convert to 32-bit RGBA
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
dst[i] = ((dst[i] & 0x003F3F3F) << 2) |
|
|
|
|
((dst[i] & 0x00303030) >> 4) |
|
|
|
|
0xFF000000;
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 14:24:46 +00:00
|
|
|
void GPU2D::VBlank()
|
|
|
|
{
|
2017-03-21 01:11:49 +00:00
|
|
|
CaptureCnt &= ~(1<<31);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU2D::VBlankEnd()
|
|
|
|
{
|
|
|
|
// TODO: find out the exact time this happens
|
2017-03-04 01:22:58 +00:00
|
|
|
BGXRefInternal[0] = BGXRef[0];
|
|
|
|
BGXRefInternal[1] = BGXRef[1];
|
|
|
|
BGYRefInternal[0] = BGYRef[0];
|
|
|
|
BGYRefInternal[1] = BGYRef[1];
|
2017-03-01 20:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GPU2D::DoCapture(u32 line, u32 width, u32* src)
|
|
|
|
{
|
|
|
|
u32 dstvram = (CaptureCnt >> 16) & 0x3;
|
|
|
|
|
|
|
|
// TODO: confirm this
|
|
|
|
// it should work like VRAM display mode, which requires VRAM to be mapped to LCDC
|
|
|
|
if (!(GPU::VRAMMap_LCDC & (1<<dstvram)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
u16* dst = (u16*)GPU::VRAM[dstvram];
|
|
|
|
u32 dstaddr = (((CaptureCnt >> 18) & 0x3) << 14) + (line * width);
|
|
|
|
|
|
|
|
if (CaptureCnt & (1<<24))
|
|
|
|
src = (u32*)GPU3D::GetLine(line);
|
|
|
|
|
|
|
|
u16* srcB = NULL;
|
|
|
|
u32 srcBaddr = line * 256;
|
|
|
|
|
|
|
|
if (CaptureCnt & (1<<25))
|
|
|
|
{
|
2017-03-20 21:18:35 +00:00
|
|
|
srcB = &DispFIFOBuffer[0];
|
|
|
|
srcBaddr = 0;
|
2017-03-01 20:42:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u32 srcvram = (DispCnt >> 18) & 0x3;
|
|
|
|
if (GPU::VRAMMap_LCDC & (1<<srcvram))
|
|
|
|
srcB = (u16*)GPU::VRAM[srcvram];
|
|
|
|
|
|
|
|
if (((DispCnt >> 16) & 0x3) != 2)
|
|
|
|
srcBaddr += ((CaptureCnt >> 26) & 0x3) << 14;
|
|
|
|
}
|
|
|
|
|
|
|
|
dstaddr &= 0xFFFF;
|
|
|
|
srcBaddr &= 0xFFFF;
|
|
|
|
|
2017-04-22 23:05:04 +00:00
|
|
|
switch ((CaptureCnt >> 29) & 0x3)
|
2017-03-01 20:42:06 +00:00
|
|
|
{
|
|
|
|
case 0: // source A
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < width; i++)
|
|
|
|
{
|
|
|
|
u32 val = src[i];
|
|
|
|
|
|
|
|
// TODO: check what happens when alpha=0
|
|
|
|
|
|
|
|
u32 r = (val >> 1) & 0x1F;
|
|
|
|
u32 g = (val >> 9) & 0x1F;
|
|
|
|
u32 b = (val >> 17) & 0x1F;
|
|
|
|
u32 a = ((val >> 24) != 0) ? 0x8000 : 0;
|
|
|
|
|
|
|
|
dst[dstaddr] = r | (g << 5) | (b << 10) | a;
|
|
|
|
dstaddr = (dstaddr + 1) & 0xFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // source B
|
|
|
|
{
|
|
|
|
if (srcB)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < width; i++)
|
|
|
|
{
|
|
|
|
dst[dstaddr] = srcB[srcBaddr];
|
|
|
|
srcBaddr = (srcBaddr + 1) & 0xFFFF;
|
|
|
|
dstaddr = (dstaddr + 1) & 0xFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < width; i++)
|
|
|
|
{
|
|
|
|
dst[dstaddr] = 0;
|
|
|
|
dstaddr = (dstaddr + 1) & 0xFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // sources A+B
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
u32 eva = DispCnt & 0x1F;
|
|
|
|
u32 evb = (DispCnt >> 8) & 0x1F;
|
|
|
|
|
|
|
|
// checkme
|
|
|
|
if (eva > 16) eva = 16;
|
|
|
|
if (evb > 16) evb = 16;
|
|
|
|
|
|
|
|
if (srcB)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < width; i++)
|
|
|
|
{
|
|
|
|
u32 val = src[i];
|
|
|
|
|
|
|
|
// TODO: check what happens when alpha=0
|
|
|
|
|
|
|
|
u32 rA = (val >> 1) & 0x1F;
|
|
|
|
u32 gA = (val >> 9) & 0x1F;
|
|
|
|
u32 bA = (val >> 17) & 0x1F;
|
|
|
|
u32 aA = ((val >> 24) != 0) ? 1 : 0;
|
|
|
|
|
|
|
|
val = srcB[srcBaddr];
|
|
|
|
|
|
|
|
u32 rB = val & 0x1F;
|
|
|
|
u32 gB = (val >> 5) & 0x1F;
|
|
|
|
u32 bB = (val >> 10) & 0x1F;
|
|
|
|
u32 aB = val >> 15;
|
|
|
|
|
|
|
|
u32 rD = ((rA * aA * eva) + (rB * aB * evb)) >> 4;
|
|
|
|
u32 gD = ((gA * aA * eva) + (gB * aB * evb)) >> 4;
|
|
|
|
u32 bD = ((bA * aA * eva) + (bB * aB * evb)) >> 4;
|
|
|
|
u32 aD = (eva>0 ? aA : 0) | (evb>0 ? aB : 0);
|
|
|
|
|
|
|
|
dst[dstaddr] = rD | (gD << 5) | (bD << 10) | (aD << 15);
|
|
|
|
srcBaddr = (srcBaddr + 1) & 0xFFFF;
|
|
|
|
dstaddr = (dstaddr + 1) & 0xFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < width; i++)
|
|
|
|
{
|
|
|
|
u32 val = src[i];
|
|
|
|
|
|
|
|
// TODO: check what happens when alpha=0
|
|
|
|
|
|
|
|
u32 rA = (val >> 1) & 0x1F;
|
|
|
|
u32 gA = (val >> 9) & 0x1F;
|
|
|
|
u32 bA = (val >> 17) & 0x1F;
|
|
|
|
u32 aA = ((val >> 24) != 0) ? 1 : 0;
|
|
|
|
|
|
|
|
u32 rD = (rA * aA * eva) >> 4;
|
|
|
|
u32 gD = (gA * aA * eva) >> 4;
|
|
|
|
u32 bD = (bA * aA * eva) >> 4;
|
|
|
|
u32 aD = (eva>0 ? aA : 0);
|
|
|
|
|
|
|
|
dst[dstaddr] = rD | (gD << 5) | (bD << 10) | (aD << 15);
|
|
|
|
dstaddr = (dstaddr + 1) & 0xFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-02-10 14:24:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 21:18:35 +00:00
|
|
|
void GPU2D::FIFODMA(u32 addr)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 256; i += 2)
|
|
|
|
{
|
|
|
|
u32 val = NDS::ARM9Read32(addr);
|
|
|
|
addr += 4;
|
|
|
|
DispFIFOBuffer[i] = val & 0xFFFF;
|
|
|
|
DispFIFOBuffer[i+1] = val >> 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 14:43:22 +00:00
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
void GPU2D::BGExtPalDirty(u32 base)
|
|
|
|
{
|
|
|
|
BGExtPalStatus[base] = 0;
|
|
|
|
BGExtPalStatus[base+1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU2D::OBJExtPalDirty()
|
|
|
|
{
|
|
|
|
OBJExtPalStatus = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u16* GPU2D::GetBGExtPal(u32 slot, u32 pal)
|
|
|
|
{
|
|
|
|
u16* dst = &BGExtPalCache[slot][pal << 8];
|
|
|
|
|
|
|
|
if (!(BGExtPalStatus[slot] & (1<<pal)))
|
|
|
|
{
|
|
|
|
if (Num)
|
|
|
|
{
|
|
|
|
if (GPU::VRAMMap_BBGExtPal[slot] & (1<<7))
|
|
|
|
memcpy(dst, &GPU::VRAM_H[(slot << 13) + (pal << 9)], 256*2);
|
|
|
|
else
|
|
|
|
memset(dst, 0, 256*2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(dst, 0, 256*2);
|
|
|
|
|
|
|
|
if (GPU::VRAMMap_ABGExtPal[slot] & (1<<4))
|
|
|
|
for (int i = 0; i < 256; i+=2)
|
|
|
|
*(u32*)&dst[i] |= *(u32*)&GPU::VRAM_E[(slot << 13) + (pal << 9) + (i << 1)];
|
|
|
|
|
|
|
|
if (GPU::VRAMMap_ABGExtPal[slot] & (1<<5))
|
|
|
|
for (int i = 0; i < 256; i+=2)
|
|
|
|
*(u32*)&dst[i] |= *(u32*)&GPU::VRAM_F[((slot&1) << 13) + (pal << 9) + (i << 1)];
|
|
|
|
|
|
|
|
if (GPU::VRAMMap_ABGExtPal[slot] & (1<<6))
|
|
|
|
for (int i = 0; i < 256; i+=2)
|
|
|
|
*(u32*)&dst[i] |= *(u32*)&GPU::VRAM_G[((slot&1) << 13) + (pal << 9) + (i << 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
BGExtPalStatus[slot] |= (1<<pal);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16* GPU2D::GetOBJExtPal(u32 pal)
|
|
|
|
{
|
|
|
|
u16* dst = &OBJExtPalCache[pal << 8];
|
|
|
|
|
|
|
|
if (!(OBJExtPalStatus & (1<<pal)))
|
|
|
|
{
|
|
|
|
if (Num)
|
|
|
|
{
|
2017-02-28 11:44:54 +00:00
|
|
|
if (GPU::VRAMMap_BOBJExtPal & (1<<8))
|
2017-02-27 20:26:11 +00:00
|
|
|
memcpy(dst, &GPU::VRAM_I[(pal << 9)], 256*2);
|
|
|
|
else
|
|
|
|
memset(dst, 0, 256*2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(dst, 0, 256*2);
|
|
|
|
|
|
|
|
if (GPU::VRAMMap_AOBJExtPal & (1<<5))
|
|
|
|
for (int i = 0; i < 256; i+=2)
|
|
|
|
*(u32*)&dst[i] |= *(u32*)&GPU::VRAM_F[(pal << 9) + (i << 1)];
|
|
|
|
|
|
|
|
if (GPU::VRAMMap_AOBJExtPal & (1<<6))
|
|
|
|
for (int i = 0; i < 256; i+=2)
|
|
|
|
*(u32*)&dst[i] |= *(u32*)&GPU::VRAM_G[(pal << 9) + (i << 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
OBJExtPalStatus |= (1<<pal);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
void GPU2D::CheckWindows(u32 line)
|
|
|
|
{
|
|
|
|
line &= 0xFF;
|
2017-04-09 01:47:47 +00:00
|
|
|
if (line == Win0Coords[3]) Win0Active = false;
|
|
|
|
else if (line == Win0Coords[2]) Win0Active = true;
|
|
|
|
if (line == Win1Coords[3]) Win1Active = false;
|
|
|
|
else if (line == Win1Coords[2]) Win1Active = true;
|
2017-04-09 01:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU2D::CalculateWindowMask(u32 line, u8* mask)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < 256; i++)
|
|
|
|
mask[i] = WinCnt[2]; // window outside
|
|
|
|
|
|
|
|
if ((DispCnt & (1<<15)) && (DispCnt & (1<<12)))
|
|
|
|
{
|
|
|
|
// OBJ window
|
|
|
|
u8 objwin[256];
|
|
|
|
memset(objwin, 0, 256);
|
|
|
|
DrawSpritesWindow(line, objwin);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
if (objwin[i]) mask[i] = WinCnt[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((DispCnt & (1<<14)) && Win1Active)
|
|
|
|
{
|
|
|
|
// window 1
|
|
|
|
u32 x1 = Win1Coords[0];
|
|
|
|
u32 x2 = Win1Coords[1];
|
|
|
|
if (x2 == 0 && x1 > 0) x2 = 256;
|
|
|
|
if (x1 > x2) x2 = 255; // checkme
|
|
|
|
|
|
|
|
for (u32 i = x1; i < x2; i++)
|
|
|
|
mask[i] = WinCnt[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((DispCnt & (1<<13)) && Win0Active)
|
|
|
|
{
|
|
|
|
// window 0
|
|
|
|
u32 x1 = Win0Coords[0];
|
|
|
|
u32 x2 = Win0Coords[1];
|
|
|
|
if (x2 == 0 && x1 > 0) x2 = 256;
|
|
|
|
if (x1 > x2) x2 = 255; // checkme
|
|
|
|
|
|
|
|
for (u32 i = x1; i < x2; i++)
|
|
|
|
mask[i] = WinCnt[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-01 22:43:02 +00:00
|
|
|
template<u32 bgmode>
|
2017-02-14 20:55:51 +00:00
|
|
|
void GPU2D::DrawScanlineBGMode(u32 line, u32* spritebuf, u32* dst)
|
2017-02-01 22:43:02 +00:00
|
|
|
{
|
|
|
|
for (int i = 3; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if ((BGCnt[3] & 0x3) == i)
|
|
|
|
{
|
|
|
|
if (DispCnt & 0x0800)
|
|
|
|
{
|
|
|
|
if (bgmode >= 3)
|
2017-02-02 00:18:03 +00:00
|
|
|
DrawBG_Extended(line, dst, 3);
|
2017-02-01 22:43:02 +00:00
|
|
|
else if (bgmode >= 1)
|
2017-03-24 19:53:01 +00:00
|
|
|
DrawBG_Affine(line, dst, 3);
|
2017-02-01 22:43:02 +00:00
|
|
|
else
|
|
|
|
DrawBG_Text(line, dst, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((BGCnt[2] & 0x3) == i)
|
|
|
|
{
|
|
|
|
if (DispCnt & 0x0400)
|
|
|
|
{
|
|
|
|
if (bgmode == 5)
|
2017-02-03 21:58:00 +00:00
|
|
|
DrawBG_Extended(line, dst, 2);
|
2017-02-01 22:43:02 +00:00
|
|
|
else if (bgmode == 4 || bgmode == 2)
|
2017-03-24 19:53:01 +00:00
|
|
|
DrawBG_Affine(line, dst, 2);
|
2017-02-01 22:43:02 +00:00
|
|
|
else
|
|
|
|
DrawBG_Text(line, dst, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((BGCnt[1] & 0x3) == i)
|
|
|
|
{
|
|
|
|
if (DispCnt & 0x0200)
|
|
|
|
{
|
|
|
|
DrawBG_Text(line, dst, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((BGCnt[0] & 0x3) == i)
|
|
|
|
{
|
|
|
|
if (DispCnt & 0x0100)
|
|
|
|
{
|
|
|
|
if ((!Num) && (DispCnt & 0x8))
|
2017-02-11 02:54:08 +00:00
|
|
|
DrawBG_3D(line, dst);
|
2017-02-01 22:43:02 +00:00
|
|
|
else
|
|
|
|
DrawBG_Text(line, dst, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DispCnt & 0x1000)
|
|
|
|
InterleaveSprites(spritebuf, 0x8000 | (i<<16), dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:55:51 +00:00
|
|
|
void GPU2D::DrawScanline_Mode1(u32 line, u32* dst)
|
2017-01-18 16:57:12 +00:00
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u32 linebuf[256*2 + 64];
|
|
|
|
u8* windowmask = (u8*)&linebuf[256*2];
|
2017-03-02 18:00:19 +00:00
|
|
|
|
2017-01-20 00:18:30 +00:00
|
|
|
u32 backdrop;
|
|
|
|
if (Num) backdrop = *(u16*)&GPU::Palette[0x400];
|
|
|
|
else backdrop = *(u16*)&GPU::Palette[0];
|
|
|
|
|
2017-02-14 20:55:51 +00:00
|
|
|
{
|
|
|
|
u8 r = (backdrop & 0x001F) << 1;
|
|
|
|
u8 g = (backdrop & 0x03E0) >> 4;
|
|
|
|
u8 b = (backdrop & 0x7C00) >> 9;
|
|
|
|
|
|
|
|
backdrop = r | (g << 8) | (b << 16) | 0x20000000;
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
2017-03-02 18:00:19 +00:00
|
|
|
linebuf[i] = backdrop;
|
2017-02-14 20:55:51 +00:00
|
|
|
}
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
if (DispCnt & 0xE000)
|
|
|
|
CalculateWindowMask(line, windowmask);
|
|
|
|
else
|
|
|
|
memset(windowmask, 0xFF, 256);
|
|
|
|
|
2017-01-21 02:36:14 +00:00
|
|
|
// prerender sprites
|
|
|
|
u32 spritebuf[256];
|
|
|
|
memset(spritebuf, 0, 256*4);
|
|
|
|
if (DispCnt & 0x1000) DrawSprites(line, spritebuf);
|
|
|
|
|
2017-01-18 16:57:12 +00:00
|
|
|
switch (DispCnt & 0x7)
|
|
|
|
{
|
2017-03-02 18:00:19 +00:00
|
|
|
case 0: DrawScanlineBGMode<0>(line, spritebuf, linebuf); break;
|
|
|
|
case 1: DrawScanlineBGMode<1>(line, spritebuf, linebuf); break;
|
|
|
|
case 2: DrawScanlineBGMode<2>(line, spritebuf, linebuf); break;
|
|
|
|
case 3: DrawScanlineBGMode<3>(line, spritebuf, linebuf); break;
|
|
|
|
case 4: DrawScanlineBGMode<4>(line, spritebuf, linebuf); break;
|
|
|
|
case 5: DrawScanlineBGMode<5>(line, spritebuf, linebuf); break;
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
2017-01-31 14:43:22 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
// color special effects
|
|
|
|
// can likely be optimized
|
|
|
|
|
|
|
|
u32 bldcnteffect = (BlendCnt >> 6) & 0x3;
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
u32 val1 = linebuf[i];
|
|
|
|
u32 val2 = linebuf[256+i];
|
|
|
|
|
|
|
|
u32 coloreffect, eva, evb;
|
|
|
|
|
|
|
|
u32 flag1 = val1 >> 24;
|
2017-04-09 01:35:32 +00:00
|
|
|
if (!(windowmask[i] & 0x20))
|
|
|
|
{
|
|
|
|
coloreffect = 0;
|
|
|
|
}
|
|
|
|
else if ((flag1 & 0x80) && (BlendCnt & ((val2 >> 16) & 0xFF00)))
|
2017-03-02 18:00:19 +00:00
|
|
|
{
|
|
|
|
// sprite blending
|
|
|
|
|
|
|
|
coloreffect = 1;
|
|
|
|
|
|
|
|
if (flag1 & 0x40)
|
|
|
|
{
|
|
|
|
eva = flag1 & 0x1F;
|
|
|
|
evb = 16 - eva;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eva = EVA;
|
|
|
|
evb = EVB;
|
|
|
|
}
|
|
|
|
}
|
2017-03-06 22:57:04 +00:00
|
|
|
else if ((flag1 & 0x40) && (BlendCnt & ((val2 >> 16) & 0xFF00)))
|
|
|
|
{
|
|
|
|
// 3D layer blending
|
|
|
|
|
|
|
|
eva = (flag1 & 0x1F) + 1;
|
|
|
|
evb = 32 - eva;
|
|
|
|
|
|
|
|
u32 r = (((val1 & 0x00003F) * eva) + ((val2 & 0x00003F) * evb)) >> 5;
|
|
|
|
u32 g = ((((val1 & 0x003F00) * eva) + ((val2 & 0x003F00) * evb)) >> 5) & 0x007F00;
|
|
|
|
u32 b = ((((val1 & 0x3F0000) * eva) + ((val2 & 0x3F0000) * evb)) >> 5) & 0x7F0000;
|
|
|
|
|
|
|
|
if (eva <= 16)
|
|
|
|
{
|
|
|
|
r += 0x000001;
|
|
|
|
g += 0x000100;
|
|
|
|
b += 0x010000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r > 0x00003F) r = 0x00003F;
|
|
|
|
if (g > 0x003F00) g = 0x003F00;
|
|
|
|
if (b > 0x3F0000) b = 0x3F0000;
|
|
|
|
|
|
|
|
dst[i] = r | g | b | 0xFF000000;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-02 18:00:19 +00:00
|
|
|
else if (BlendCnt & flag1)
|
|
|
|
{
|
|
|
|
if ((bldcnteffect == 1) && (BlendCnt & ((val2 >> 16) & 0xFF00)))
|
|
|
|
{
|
|
|
|
coloreffect = 1;
|
|
|
|
eva = EVA;
|
|
|
|
evb = EVB;
|
|
|
|
}
|
|
|
|
else if (bldcnteffect >= 2)
|
|
|
|
coloreffect = bldcnteffect;
|
|
|
|
else
|
|
|
|
coloreffect = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
coloreffect = 0;
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
switch (coloreffect)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
dst[i] = val1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
u32 r = (((val1 & 0x00003F) * eva) + ((val2 & 0x00003F) * evb)) >> 4;
|
|
|
|
u32 g = ((((val1 & 0x003F00) * eva) + ((val2 & 0x003F00) * evb)) >> 4) & 0x007F00;
|
|
|
|
u32 b = ((((val1 & 0x3F0000) * eva) + ((val2 & 0x3F0000) * evb)) >> 4) & 0x7F0000;
|
|
|
|
|
|
|
|
if (r > 0x00003F) r = 0x00003F;
|
|
|
|
if (g > 0x003F00) g = 0x003F00;
|
|
|
|
if (b > 0x3F0000) b = 0x3F0000;
|
|
|
|
|
|
|
|
dst[i] = r | g | b | 0xFF000000;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
u32 r = val1 & 0x00003F;
|
|
|
|
u32 g = val1 & 0x003F00;
|
|
|
|
u32 b = val1 & 0x3F0000;
|
|
|
|
|
|
|
|
r += ((0x00003F - r) * EVY) >> 4;
|
|
|
|
g += (((0x003F00 - g) * EVY) >> 4) & 0x003F00;
|
|
|
|
b += (((0x3F0000 - b) * EVY) >> 4) & 0x3F0000;
|
|
|
|
|
|
|
|
dst[i] = r | g | b | 0xFF000000;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
u32 r = val1 & 0x00003F;
|
|
|
|
u32 g = val1 & 0x003F00;
|
|
|
|
u32 b = val1 & 0x3F0000;
|
|
|
|
|
|
|
|
r -= (r * EVY) >> 4;
|
|
|
|
g -= ((g * EVY) >> 4) & 0x003F00;
|
|
|
|
b -= ((b * EVY) >> 4) & 0x3F0000;
|
2017-02-01 22:43:02 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
dst[i] = r | g | b | 0xFF000000;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-14 20:55:51 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
|
|
|
|
void GPU2D::DrawPixel(u32* dst, u16 color, u32 flag)
|
2017-02-14 20:55:51 +00:00
|
|
|
{
|
|
|
|
u8 r = (color & 0x001F) << 1;
|
|
|
|
u8 g = (color & 0x03E0) >> 4;
|
|
|
|
u8 b = (color & 0x7C00) >> 9;
|
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
*(dst+256) = *dst;
|
|
|
|
*dst = r | (g << 8) | (b << 16) | flag;
|
2017-02-14 20:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU2D::DrawBG_3D(u32 line, u32* dst)
|
2017-02-11 02:54:08 +00:00
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
// TODO: check if window can prevent blending from happening
|
2017-02-11 02:54:08 +00:00
|
|
|
|
2017-03-06 22:57:04 +00:00
|
|
|
u32* src = GPU3D::GetLine(line);
|
2017-04-09 01:35:32 +00:00
|
|
|
u8* windowmask = (u8*)&dst[256*2];
|
2017-03-07 00:36:16 +00:00
|
|
|
|
|
|
|
u16 xoff = BGXPos[0];
|
|
|
|
int i = 0;
|
|
|
|
int iend = 256;
|
|
|
|
|
|
|
|
if (xoff & 0x100)
|
|
|
|
{
|
|
|
|
i = (0x100 - (xoff & 0xFF));
|
|
|
|
xoff += i;
|
|
|
|
}
|
2017-03-07 20:38:47 +00:00
|
|
|
if ((xoff - i + iend - 1) & 0x100)
|
2017-02-11 02:54:08 +00:00
|
|
|
{
|
2017-03-07 00:36:16 +00:00
|
|
|
iend -= (xoff & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < iend; i++)
|
|
|
|
{
|
|
|
|
u32 c = src[xoff];
|
|
|
|
xoff++;
|
|
|
|
|
2017-03-06 22:57:04 +00:00
|
|
|
if ((c >> 24) == 0) continue;
|
2017-04-09 01:35:32 +00:00
|
|
|
if (!(windowmask[i] & 0x01)) continue;
|
2017-02-14 20:55:51 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
dst[i+256] = dst[i];
|
2017-03-06 22:57:04 +00:00
|
|
|
dst[i] = c | 0x40000000;
|
2017-02-11 02:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:55:51 +00:00
|
|
|
void GPU2D::DrawBG_Text(u32 line, u32* dst, u32 bgnum)
|
2017-01-18 16:57:12 +00:00
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8* windowmask = (u8*)&dst[256*2];
|
2017-01-18 16:57:12 +00:00
|
|
|
u16 bgcnt = BGCnt[bgnum];
|
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
u32 tilesetaddr, tilemapaddr;
|
2017-01-18 16:57:12 +00:00
|
|
|
u16* pal;
|
2017-02-27 20:26:11 +00:00
|
|
|
u32 extpal, extpalslot;
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-01-20 14:27:56 +00:00
|
|
|
u16 xoff = BGXPos[bgnum];
|
|
|
|
u16 yoff = BGYPos[bgnum] + line;
|
2017-01-18 16:57:12 +00:00
|
|
|
|
|
|
|
u32 widexmask = (bgcnt & 0x4000) ? 0x100 : 0;
|
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
extpal = (DispCnt & 0x40000000);
|
|
|
|
if (extpal) extpalslot = ((bgnum<2) && (bgcnt&0x2000)) ? (2+bgnum) : bgnum;
|
2017-02-01 23:09:40 +00:00
|
|
|
|
2017-01-18 16:57:12 +00:00
|
|
|
if (Num)
|
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
tilesetaddr = 0x06200000 + ((bgcnt & 0x003C) << 12);
|
|
|
|
tilemapaddr = 0x06200000 + ((bgcnt & 0x1F00) << 3);
|
2017-02-01 23:09:40 +00:00
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
pal = (u16*)&GPU::Palette[0x400];
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
tilesetaddr = 0x06000000 + ((DispCnt & 0x07000000) >> 8) + ((bgcnt & 0x003C) << 12);
|
|
|
|
tilemapaddr = 0x06000000 + ((DispCnt & 0x38000000) >> 11) + ((bgcnt & 0x1F00) << 3);
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
pal = (u16*)&GPU::Palette[0];
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// adjust Y position in tilemap
|
|
|
|
if (bgcnt & 0x8000)
|
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
tilemapaddr += ((yoff & 0x1F8) << 3);
|
2017-01-18 16:57:12 +00:00
|
|
|
if (bgcnt & 0x4000)
|
2017-02-27 20:26:11 +00:00
|
|
|
tilemapaddr += ((yoff & 0x100) << 3);
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
else
|
2017-02-27 20:26:11 +00:00
|
|
|
tilemapaddr += ((yoff & 0xF8) << 3);
|
2017-01-18 16:57:12 +00:00
|
|
|
|
|
|
|
u16 curtile;
|
|
|
|
u16* curpal;
|
2017-02-27 20:26:11 +00:00
|
|
|
u32 pixelsaddr;
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-02-01 22:43:02 +00:00
|
|
|
if (bgcnt & 0x0080)
|
2017-01-18 16:57:12 +00:00
|
|
|
{
|
2017-02-01 22:43:02 +00:00
|
|
|
// 256-color
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-02-01 22:43:02 +00:00
|
|
|
// preload shit as needed
|
|
|
|
if (xoff & 0x7)
|
2017-01-18 16:57:12 +00:00
|
|
|
{
|
|
|
|
// load a new tile
|
2017-02-27 20:26:11 +00:00
|
|
|
curtile = GPU::ReadVRAM_BG<u16>(tilemapaddr + ((xoff & 0xF8) >> 2) + ((xoff & widexmask) << 3));
|
|
|
|
|
|
|
|
if (extpal) curpal = GetBGExtPal(extpalslot, curtile>>12);
|
|
|
|
else curpal = pal;
|
|
|
|
|
|
|
|
pixelsaddr = tilesetaddr + ((curtile & 0x03FF) << 6)
|
|
|
|
+ (((curtile & 0x0800) ? (7-(yoff&0x7)) : (yoff&0x7)) << 3);
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 22:43:02 +00:00
|
|
|
for (int i = 0; i < 256; i++)
|
2017-01-18 16:57:12 +00:00
|
|
|
{
|
2017-02-01 22:43:02 +00:00
|
|
|
if (!(xoff & 0x7))
|
|
|
|
{
|
|
|
|
// load a new tile
|
2017-02-27 20:26:11 +00:00
|
|
|
curtile = GPU::ReadVRAM_BG<u16>(tilemapaddr + ((xoff & 0xF8) >> 2) + ((xoff & widexmask) << 3));
|
|
|
|
|
|
|
|
if (extpal) curpal = GetBGExtPal(extpalslot, curtile>>12);
|
|
|
|
else curpal = pal;
|
|
|
|
|
|
|
|
pixelsaddr = tilesetaddr + ((curtile & 0x03FF) << 6)
|
|
|
|
+ (((curtile & 0x0800) ? (7-(yoff&0x7)) : (yoff&0x7)) << 3);
|
2017-02-01 22:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw pixel
|
2017-04-09 01:35:32 +00:00
|
|
|
if (windowmask[i] & (1<<bgnum))
|
|
|
|
{
|
|
|
|
u8 color;
|
|
|
|
u32 tilexoff = (curtile & 0x0400) ? (7-(xoff&0x7)) : (xoff&0x7);
|
|
|
|
color = GPU::ReadVRAM_BG<u8>(pixelsaddr + tilexoff);
|
2017-02-01 22:43:02 +00:00
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
if (color)
|
|
|
|
DrawPixel(&dst[i], curpal[color], 0x01000000<<bgnum);
|
|
|
|
}
|
2017-02-01 22:43:02 +00:00
|
|
|
|
|
|
|
xoff++;
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
2017-02-01 22:43:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 16-color
|
|
|
|
|
|
|
|
// preload shit as needed
|
|
|
|
if (xoff & 0x7)
|
2017-01-18 16:57:12 +00:00
|
|
|
{
|
2017-02-01 22:43:02 +00:00
|
|
|
// load a new tile
|
2017-02-27 20:26:11 +00:00
|
|
|
curtile = GPU::ReadVRAM_BG<u16>(tilemapaddr + ((xoff & 0xF8) >> 2) + ((xoff & widexmask) << 3));
|
2017-02-01 22:43:02 +00:00
|
|
|
curpal = pal + ((curtile & 0xF000) >> 8);
|
2017-02-27 20:26:11 +00:00
|
|
|
pixelsaddr = tilesetaddr + ((curtile & 0x03FF) << 5)
|
|
|
|
+ (((curtile & 0x0800) ? (7-(yoff&0x7)) : (yoff&0x7)) << 2);
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
2017-01-31 14:43:22 +00:00
|
|
|
|
2017-02-01 22:43:02 +00:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
if (!(xoff & 0x7))
|
|
|
|
{
|
|
|
|
// load a new tile
|
2017-02-27 20:26:11 +00:00
|
|
|
curtile = GPU::ReadVRAM_BG<u16>(tilemapaddr + ((xoff & 0xF8) >> 2) + ((xoff & widexmask) << 3));
|
2017-02-01 22:43:02 +00:00
|
|
|
curpal = pal + ((curtile & 0xF000) >> 8);
|
2017-02-27 20:26:11 +00:00
|
|
|
pixelsaddr = tilesetaddr + ((curtile & 0x03FF) << 5)
|
|
|
|
+ (((curtile & 0x0800) ? (7-(yoff&0x7)) : (yoff&0x7)) << 2);
|
2017-02-01 22:43:02 +00:00
|
|
|
}
|
2017-01-18 16:57:12 +00:00
|
|
|
|
2017-02-01 22:43:02 +00:00
|
|
|
// draw pixel
|
2017-02-27 20:26:11 +00:00
|
|
|
// TODO: optimize VRAM access
|
2017-04-09 01:35:32 +00:00
|
|
|
if (windowmask[i] & (1<<bgnum))
|
2017-02-01 22:43:02 +00:00
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8 color;
|
|
|
|
u32 tilexoff = (curtile & 0x0400) ? (7-(xoff&0x7)) : (xoff&0x7);
|
|
|
|
if (tilexoff & 0x1)
|
|
|
|
{
|
|
|
|
color = GPU::ReadVRAM_BG<u8>(pixelsaddr + (tilexoff >> 1)) >> 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
color = GPU::ReadVRAM_BG<u8>(pixelsaddr + (tilexoff >> 1)) & 0x0F;
|
|
|
|
}
|
2017-02-01 22:43:02 +00:00
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
if (color)
|
|
|
|
DrawPixel(&dst[i], curpal[color], 0x01000000<<bgnum);
|
|
|
|
}
|
2017-02-01 22:43:02 +00:00
|
|
|
|
|
|
|
xoff++;
|
|
|
|
}
|
2017-01-18 16:57:12 +00:00
|
|
|
}
|
2016-12-05 17:02:29 +00:00
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-24 19:53:01 +00:00
|
|
|
void GPU2D::DrawBG_Affine(u32 line, u32* dst, u32 bgnum)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8* windowmask = (u8*)&dst[256*2];
|
2017-03-24 19:53:01 +00:00
|
|
|
u16 bgcnt = BGCnt[bgnum];
|
|
|
|
|
|
|
|
u32 tilesetaddr, tilemapaddr;
|
|
|
|
u16* pal;
|
|
|
|
|
|
|
|
u32 coordmask;
|
|
|
|
u32 yshift;
|
|
|
|
switch (bgcnt & 0xC000)
|
|
|
|
{
|
|
|
|
case 0x0000: coordmask = 0x07800; yshift = 7; break;
|
|
|
|
case 0x4000: coordmask = 0x0F800; yshift = 8; break;
|
|
|
|
case 0x8000: coordmask = 0x1F800; yshift = 9; break;
|
|
|
|
case 0xC000: coordmask = 0x3F800; yshift = 10; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 overflowmask;
|
|
|
|
if (bgcnt & 0x2000) overflowmask = 0;
|
|
|
|
else overflowmask = ~(coordmask | 0x7FF);
|
|
|
|
|
|
|
|
s16 rotA = BGRotA[bgnum-2];
|
|
|
|
s16 rotB = BGRotB[bgnum-2];
|
|
|
|
s16 rotC = BGRotC[bgnum-2];
|
|
|
|
s16 rotD = BGRotD[bgnum-2];
|
|
|
|
|
|
|
|
s32 rotX = BGXRefInternal[bgnum-2];
|
|
|
|
s32 rotY = BGYRefInternal[bgnum-2];
|
|
|
|
|
|
|
|
if (Num)
|
|
|
|
{
|
|
|
|
tilesetaddr = 0x06200000 + ((bgcnt & 0x003C) << 12);
|
|
|
|
tilemapaddr = 0x06200000 + ((bgcnt & 0x1F00) << 3);
|
|
|
|
|
|
|
|
pal = (u16*)&GPU::Palette[0x400];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tilesetaddr = 0x06000000 + ((DispCnt & 0x07000000) >> 8) + ((bgcnt & 0x003C) << 12);
|
|
|
|
tilemapaddr = 0x06000000 + ((DispCnt & 0x38000000) >> 11) + ((bgcnt & 0x1F00) << 3);
|
|
|
|
|
|
|
|
pal = (u16*)&GPU::Palette[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 curtile;
|
|
|
|
|
|
|
|
yshift -= 3;
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
if ((!((rotX|rotY) & overflowmask)) && (windowmask[i] & (1<<bgnum)))
|
2017-03-24 19:53:01 +00:00
|
|
|
{
|
|
|
|
curtile = GPU::ReadVRAM_BG<u8>(tilemapaddr + ((((rotY & coordmask) >> 11) << yshift) + ((rotX & coordmask) >> 11)));
|
|
|
|
|
|
|
|
// draw pixel
|
|
|
|
u8 color;
|
|
|
|
u32 tilexoff = (rotX >> 8) & 0x7;
|
|
|
|
u32 tileyoff = (rotY >> 8) & 0x7;
|
|
|
|
|
|
|
|
color = GPU::ReadVRAM_BG<u8>(tilesetaddr + (curtile << 6) + (tileyoff << 3) + tilexoff);
|
|
|
|
|
|
|
|
if (color)
|
|
|
|
DrawPixel(&dst[i], pal[color], 0x01000000<<bgnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
rotX += rotA;
|
|
|
|
rotY += rotC;
|
|
|
|
}
|
|
|
|
|
|
|
|
BGXRefInternal[bgnum-2] += rotB;
|
|
|
|
BGYRefInternal[bgnum-2] += rotD;
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:55:51 +00:00
|
|
|
void GPU2D::DrawBG_Extended(u32 line, u32* dst, u32 bgnum)
|
2017-02-02 00:18:03 +00:00
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8* windowmask = (u8*)&dst[256*2];
|
2017-02-02 00:18:03 +00:00
|
|
|
u16 bgcnt = BGCnt[bgnum];
|
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
u32 tilesetaddr, tilemapaddr;
|
2017-02-02 00:18:03 +00:00
|
|
|
u16* pal;
|
|
|
|
u32 extpal;
|
|
|
|
|
2017-02-03 21:58:00 +00:00
|
|
|
u32 coordmask;
|
2017-02-03 22:49:37 +00:00
|
|
|
u32 yshift;
|
2017-02-03 21:58:00 +00:00
|
|
|
switch (bgcnt & 0xC000)
|
|
|
|
{
|
2017-02-03 22:49:37 +00:00
|
|
|
case 0x0000: coordmask = 0x07800; yshift = 7; break;
|
|
|
|
case 0x4000: coordmask = 0x0F800; yshift = 8; break;
|
|
|
|
case 0x8000: coordmask = 0x1F800; yshift = 9; break;
|
|
|
|
case 0xC000: coordmask = 0x3F800; yshift = 10; break;
|
2017-02-03 21:58:00 +00:00
|
|
|
}
|
2017-02-02 00:18:03 +00:00
|
|
|
|
2017-02-03 22:49:37 +00:00
|
|
|
u32 overflowmask;
|
|
|
|
if (bgcnt & 0x2000) overflowmask = 0;
|
|
|
|
else overflowmask = ~(coordmask | 0x7FF);
|
2017-02-02 00:18:03 +00:00
|
|
|
|
2017-02-03 22:49:37 +00:00
|
|
|
extpal = (DispCnt & 0x40000000);
|
2017-02-02 00:18:03 +00:00
|
|
|
|
|
|
|
s16 rotA = BGRotA[bgnum-2];
|
|
|
|
s16 rotB = BGRotB[bgnum-2];
|
|
|
|
s16 rotC = BGRotC[bgnum-2];
|
|
|
|
s16 rotD = BGRotD[bgnum-2];
|
|
|
|
|
2017-03-04 01:22:58 +00:00
|
|
|
s32 rotX = BGXRefInternal[bgnum-2];
|
|
|
|
s32 rotY = BGYRefInternal[bgnum-2];
|
2017-02-02 00:18:03 +00:00
|
|
|
|
|
|
|
if (bgcnt & 0x0080)
|
|
|
|
{
|
|
|
|
// bitmap modes
|
2017-02-03 22:49:37 +00:00
|
|
|
|
2017-03-01 20:42:06 +00:00
|
|
|
if (Num) tilemapaddr = 0x06200000 + ((bgcnt & 0x1F00) << 6);
|
|
|
|
else tilemapaddr = 0x06000000 + ((bgcnt & 0x1F00) << 6);
|
2017-02-03 22:49:37 +00:00
|
|
|
|
|
|
|
coordmask |= 0x7FF;
|
|
|
|
|
|
|
|
if (bgcnt & 0x0004)
|
|
|
|
{
|
|
|
|
// direct color bitmap
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
if ((!((rotX|rotY) & overflowmask)) && (windowmask[i] & (1<<bgnum)))
|
2017-02-03 22:49:37 +00:00
|
|
|
{
|
2017-03-01 20:42:06 +00:00
|
|
|
u16 color = GPU::ReadVRAM_BG<u16>(tilemapaddr + (((((rotY & coordmask) >> 8) << yshift) + ((rotX & coordmask) >> 8)) << 1));
|
2017-02-03 22:49:37 +00:00
|
|
|
|
|
|
|
if (color & 0x8000)
|
2017-03-02 18:00:19 +00:00
|
|
|
DrawPixel(&dst[i], color, 0x01000000<<bgnum);
|
2017-02-03 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rotX += rotA;
|
|
|
|
rotY += rotC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 256-color bitmap
|
|
|
|
|
|
|
|
if (Num) pal = (u16*)&GPU::Palette[0x400];
|
|
|
|
else pal = (u16*)&GPU::Palette[0];
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
if ((!((rotX|rotY) & overflowmask)) && (windowmask[i] & (1<<bgnum)))
|
2017-02-03 22:49:37 +00:00
|
|
|
{
|
2017-03-01 20:42:06 +00:00
|
|
|
u8 color = GPU::ReadVRAM_BG<u8>(tilemapaddr + (((rotY & coordmask) >> 8) << yshift) + ((rotX & coordmask) >> 8));
|
2017-02-03 22:49:37 +00:00
|
|
|
|
|
|
|
if (color)
|
2017-03-02 18:00:19 +00:00
|
|
|
DrawPixel(&dst[i], pal[color], 0x01000000<<bgnum);
|
2017-02-03 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rotX += rotA;
|
|
|
|
rotY += rotC;
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 00:18:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
// mixed affine/text mode
|
2017-02-02 00:18:03 +00:00
|
|
|
|
2017-02-03 22:49:37 +00:00
|
|
|
if (Num)
|
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
tilesetaddr = 0x06200000 + ((bgcnt & 0x003C) << 12);
|
|
|
|
tilemapaddr = 0x06200000 + ((bgcnt & 0x1F00) << 3);
|
2017-02-03 22:49:37 +00:00
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
pal = (u16*)&GPU::Palette[0x400];
|
2017-02-03 22:49:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
tilesetaddr = 0x06000000 + ((DispCnt & 0x07000000) >> 8) + ((bgcnt & 0x003C) << 12);
|
|
|
|
tilemapaddr = 0x06000000 + ((DispCnt & 0x38000000) >> 11) + ((bgcnt & 0x1F00) << 3);
|
2017-02-03 22:49:37 +00:00
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
pal = (u16*)&GPU::Palette[0];
|
2017-02-03 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u16 curtile;
|
|
|
|
u16* curpal;
|
|
|
|
|
|
|
|
yshift -= 3;
|
|
|
|
|
2017-02-02 00:18:03 +00:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
if ((!((rotX|rotY) & overflowmask)) && (windowmask[i] & (1<<bgnum)))
|
2017-02-03 22:49:37 +00:00
|
|
|
{
|
2017-02-27 20:26:11 +00:00
|
|
|
curtile = GPU::ReadVRAM_BG<u16>(tilemapaddr + (((((rotY & coordmask) >> 11) << yshift) + ((rotX & coordmask) >> 11)) << 1));
|
|
|
|
|
|
|
|
if (extpal) curpal = GetBGExtPal(bgnum, curtile>>12);
|
|
|
|
else curpal = pal;
|
2017-02-02 00:18:03 +00:00
|
|
|
|
2017-02-03 22:49:37 +00:00
|
|
|
// draw pixel
|
|
|
|
u8 color;
|
|
|
|
u32 tilexoff = (rotX >> 8) & 0x7;
|
|
|
|
u32 tileyoff = (rotY >> 8) & 0x7;
|
2017-02-02 00:18:03 +00:00
|
|
|
|
2017-02-03 22:49:37 +00:00
|
|
|
if (curtile & 0x0400) tilexoff = 7-tilexoff;
|
|
|
|
if (curtile & 0x0800) tileyoff = 7-tileyoff;
|
2017-02-02 00:18:03 +00:00
|
|
|
|
2017-02-27 20:26:11 +00:00
|
|
|
color = GPU::ReadVRAM_BG<u8>(tilesetaddr + ((curtile & 0x03FF) << 6) + (tileyoff << 3) + tilexoff);
|
2017-02-02 00:18:03 +00:00
|
|
|
|
2017-02-03 22:49:37 +00:00
|
|
|
if (color)
|
2017-03-02 18:00:19 +00:00
|
|
|
DrawPixel(&dst[i], curpal[color], 0x01000000<<bgnum);
|
2017-02-03 22:49:37 +00:00
|
|
|
}
|
2017-02-02 00:18:03 +00:00
|
|
|
|
|
|
|
rotX += rotA;
|
|
|
|
rotY += rotC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 01:22:58 +00:00
|
|
|
BGXRefInternal[bgnum-2] += rotB;
|
|
|
|
BGYRefInternal[bgnum-2] += rotD;
|
2017-02-02 00:18:03 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 20:55:51 +00:00
|
|
|
void GPU2D::InterleaveSprites(u32* buf, u32 prio, u32* dst)
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8* windowmask = (u8*)&dst[256*2];
|
|
|
|
|
2017-01-21 02:36:14 +00:00
|
|
|
for (u32 i = 0; i < 256; i++)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
if (((buf[i] & 0xF8000) == prio) && (windowmask[i] & 0x10))
|
2017-02-14 20:55:51 +00:00
|
|
|
{
|
|
|
|
u32 blendfunc = 0;
|
2017-03-02 18:00:19 +00:00
|
|
|
DrawPixel(&dst[i], buf[i] & 0x7FFF, buf[i] & 0xFF000000);
|
2017-02-14 20:55:51 +00:00
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU2D::DrawSprites(u32 line, u32* dst)
|
|
|
|
{
|
|
|
|
u16* oam = (u16*)&GPU::OAM[Num ? 0x400 : 0];
|
|
|
|
|
|
|
|
const s32 spritewidth[16] =
|
|
|
|
{
|
|
|
|
8, 16, 8, 0,
|
|
|
|
16, 32, 8, 0,
|
|
|
|
32, 32, 16, 0,
|
|
|
|
64, 64, 32, 0
|
|
|
|
};
|
|
|
|
const s32 spriteheight[16] =
|
|
|
|
{
|
|
|
|
8, 8, 16, 0,
|
|
|
|
16, 8, 32, 0,
|
|
|
|
32, 16, 32, 0,
|
|
|
|
64, 32, 64, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int bgnum = 0x0C00; bgnum >= 0x0000; bgnum -= 0x0400)
|
|
|
|
{
|
|
|
|
for (int sprnum = 127; sprnum >= 0; sprnum--)
|
|
|
|
{
|
|
|
|
u16* attrib = &oam[sprnum*4];
|
|
|
|
|
|
|
|
if ((attrib[2] & 0x0C00) != bgnum)
|
|
|
|
continue;
|
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
if (((attrib[0] >> 10) & 0x3) == 2)
|
|
|
|
continue;
|
|
|
|
|
2017-01-21 02:36:14 +00:00
|
|
|
if (attrib[0] & 0x0100)
|
|
|
|
{
|
|
|
|
u32 sizeparam = (attrib[0] >> 14) | ((attrib[1] & 0xC000) >> 12);
|
|
|
|
s32 width = spritewidth[sizeparam];
|
|
|
|
s32 height = spriteheight[sizeparam];
|
|
|
|
s32 boundwidth = width;
|
|
|
|
s32 boundheight = height;
|
|
|
|
|
|
|
|
if (attrib[0] & 0x0200)
|
|
|
|
{
|
|
|
|
boundwidth <<= 1;
|
|
|
|
boundheight <<= 1;
|
|
|
|
}
|
2017-01-21 16:06:54 +00:00
|
|
|
|
2017-01-21 02:36:14 +00:00
|
|
|
u32 ypos = attrib[0] & 0xFF;
|
|
|
|
ypos = (line - ypos) & 0xFF;
|
|
|
|
if (ypos >= (u32)boundheight)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s32 xpos = (s32)(attrib[1] << 23) >> 23;
|
|
|
|
if (xpos <= -boundwidth)
|
|
|
|
continue;
|
|
|
|
|
2017-01-21 16:06:54 +00:00
|
|
|
u32 rotparamgroup = (attrib[1] >> 9) & 0x1F;
|
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
DrawSprite_Rotscale<false>(attrib, &oam[(rotparamgroup*16) + 3], boundwidth, boundheight, width, height, xpos, ypos, dst);
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (attrib[0] & 0x0200)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
u32 sizeparam = (attrib[0] >> 14) | ((attrib[1] & 0xC000) >> 12);
|
|
|
|
s32 width = spritewidth[sizeparam];
|
|
|
|
s32 height = spriteheight[sizeparam];
|
|
|
|
|
|
|
|
u32 ypos = attrib[0] & 0xFF;
|
|
|
|
ypos = (line - ypos) & 0xFF;
|
|
|
|
if (ypos >= (u32)height)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s32 xpos = (s32)(attrib[1] << 23) >> 23;
|
|
|
|
if (xpos <= -width)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// yflip
|
|
|
|
if (attrib[1] & 0x2000)
|
|
|
|
ypos = height-1 - ypos;
|
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
DrawSprite_Normal<false>(attrib, width, xpos, ypos, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU2D::DrawSpritesWindow(u32 line, u8* dst)
|
|
|
|
{
|
|
|
|
u16* oam = (u16*)&GPU::OAM[Num ? 0x400 : 0];
|
|
|
|
|
|
|
|
const s32 spritewidth[16] =
|
|
|
|
{
|
|
|
|
8, 16, 8, 0,
|
|
|
|
16, 32, 8, 0,
|
|
|
|
32, 32, 16, 0,
|
|
|
|
64, 64, 32, 0
|
|
|
|
};
|
|
|
|
const s32 spriteheight[16] =
|
|
|
|
{
|
|
|
|
8, 8, 16, 0,
|
|
|
|
16, 8, 32, 0,
|
|
|
|
32, 16, 32, 0,
|
|
|
|
64, 32, 64, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int sprnum = 127; sprnum >= 0; sprnum--)
|
|
|
|
{
|
|
|
|
u16* attrib = &oam[sprnum*4];
|
|
|
|
|
|
|
|
if (((attrib[0] >> 10) & 0x3) != 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (attrib[0] & 0x0100)
|
|
|
|
{
|
|
|
|
u32 sizeparam = (attrib[0] >> 14) | ((attrib[1] & 0xC000) >> 12);
|
|
|
|
s32 width = spritewidth[sizeparam];
|
|
|
|
s32 height = spriteheight[sizeparam];
|
|
|
|
s32 boundwidth = width;
|
|
|
|
s32 boundheight = height;
|
|
|
|
|
|
|
|
if (attrib[0] & 0x0200)
|
|
|
|
{
|
|
|
|
boundwidth <<= 1;
|
|
|
|
boundheight <<= 1;
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
2017-04-09 01:35:32 +00:00
|
|
|
|
|
|
|
u32 ypos = attrib[0] & 0xFF;
|
|
|
|
ypos = (line - ypos) & 0xFF;
|
|
|
|
if (ypos >= (u32)boundheight)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s32 xpos = (s32)(attrib[1] << 23) >> 23;
|
|
|
|
if (xpos <= -boundwidth)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
u32 rotparamgroup = (attrib[1] >> 9) & 0x1F;
|
|
|
|
|
|
|
|
DrawSprite_Rotscale<true>(attrib, &oam[(rotparamgroup*16) + 3], boundwidth, boundheight, width, height, xpos, ypos, (u32*)dst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (attrib[0] & 0x0200)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
u32 sizeparam = (attrib[0] >> 14) | ((attrib[1] & 0xC000) >> 12);
|
|
|
|
s32 width = spritewidth[sizeparam];
|
|
|
|
s32 height = spriteheight[sizeparam];
|
|
|
|
|
|
|
|
u32 ypos = attrib[0] & 0xFF;
|
|
|
|
ypos = (line - ypos) & 0xFF;
|
|
|
|
if (ypos >= (u32)height)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s32 xpos = (s32)(attrib[1] << 23) >> 23;
|
|
|
|
if (xpos <= -width)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// yflip
|
|
|
|
if (attrib[1] & 0x2000)
|
|
|
|
ypos = height-1 - ypos;
|
|
|
|
|
|
|
|
DrawSprite_Normal<true>(attrib, width, xpos, ypos, (u32*)dst);
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
template<bool window>
|
2017-01-21 16:06:54 +00:00
|
|
|
void GPU2D::DrawSprite_Rotscale(u16* attrib, u16* rotparams, u32 boundwidth, u32 boundheight, u32 width, u32 height, s32 xpos, u32 ypos, u32* dst)
|
|
|
|
{
|
|
|
|
u32 prio = ((attrib[2] & 0x0C00) << 6) | 0x8000;
|
|
|
|
u32 tilenum = attrib[2] & 0x03FF;
|
2017-04-09 01:35:32 +00:00
|
|
|
u32 spritemode = window ? 0 : ((attrib[0] >> 10) & 0x3);
|
2017-03-01 22:02:50 +00:00
|
|
|
|
2017-01-21 16:06:54 +00:00
|
|
|
u32 ytilefactor;
|
|
|
|
|
|
|
|
s32 centerX = boundwidth >> 1;
|
|
|
|
s32 centerY = boundheight >> 1;
|
|
|
|
|
|
|
|
u32 xoff;
|
|
|
|
if (xpos >= 0)
|
|
|
|
{
|
|
|
|
xoff = 0;
|
|
|
|
if ((xpos+boundwidth) > 256)
|
|
|
|
boundwidth = 256-xpos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xoff = -xpos;
|
|
|
|
xpos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s16 rotA = (s16)rotparams[0];
|
|
|
|
s16 rotB = (s16)rotparams[4];
|
|
|
|
s16 rotC = (s16)rotparams[8];
|
|
|
|
s16 rotD = (s16)rotparams[12];
|
|
|
|
|
|
|
|
s32 rotX = ((xoff-centerX) * rotA) + ((ypos-centerY) * rotB) + (width << 7);
|
|
|
|
s32 rotY = ((xoff-centerX) * rotC) + ((ypos-centerY) * rotD) + (height << 7);
|
|
|
|
|
|
|
|
width <<= 8;
|
|
|
|
height <<= 8;
|
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
if (spritemode == 3)
|
2017-01-21 16:06:54 +00:00
|
|
|
{
|
2017-03-02 18:00:19 +00:00
|
|
|
u32 alpha = attrib[2] >> 12;
|
|
|
|
if (!alpha) return;
|
|
|
|
alpha++;
|
2017-02-28 11:44:54 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
prio |= (0xC0000000 | (alpha << 24));
|
2017-02-28 11:44:54 +00:00
|
|
|
|
2017-03-24 19:53:01 +00:00
|
|
|
if (DispCnt & 0x40)
|
|
|
|
{
|
|
|
|
if (DispCnt & 0x20)
|
|
|
|
{
|
|
|
|
// TODO ("reserved")
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tilenum <<= (7 + ((DispCnt >> 22) & 0x1));
|
|
|
|
ytilefactor = ((width >> 8) * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (DispCnt & 0x20)
|
|
|
|
{
|
|
|
|
tilenum = ((tilenum & 0x01F) << 4) + ((tilenum & 0x3E0) << 7);
|
|
|
|
ytilefactor = (256 * 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tilenum = ((tilenum & 0x00F) << 4) + ((tilenum & 0x3F0) << 7);
|
|
|
|
ytilefactor = (128 * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 pixelsaddr = (Num ? 0x06600000 : 0x06400000) + tilenum;
|
|
|
|
|
|
|
|
for (; xoff < boundwidth;)
|
|
|
|
{
|
|
|
|
if ((u32)rotX < width && (u32)rotY < height)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8 color = GPU::ReadVRAM_OBJ<u16>(pixelsaddr + ((rotY >> 8) * ytilefactor) + ((rotX >> 8) << 1));
|
2017-03-24 19:53:01 +00:00
|
|
|
|
|
|
|
if (color & 0x8000)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = color | prio;
|
|
|
|
}
|
2017-03-24 19:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rotX += rotA;
|
|
|
|
rotY += rotC;
|
|
|
|
xoff++;
|
|
|
|
xpos++;
|
|
|
|
}
|
2017-03-02 18:00:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-24 19:53:01 +00:00
|
|
|
if (DispCnt & 0x10)
|
|
|
|
{
|
|
|
|
tilenum <<= ((DispCnt >> 20) & 0x3);
|
|
|
|
ytilefactor = (width >> 11) << ((attrib[0] & 0x2000) ? 1:0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ytilefactor = 0x20;
|
|
|
|
}
|
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
if (spritemode == 1) prio |= 0x80000000;
|
|
|
|
else prio |= 0x10000000;
|
|
|
|
|
|
|
|
if (attrib[0] & 0x2000)
|
2017-02-28 11:44:54 +00:00
|
|
|
{
|
2017-03-02 18:00:19 +00:00
|
|
|
// 256-color
|
|
|
|
tilenum <<= 5;
|
|
|
|
ytilefactor <<= 5;
|
|
|
|
u32 pixelsaddr = (Num ? 0x06600000 : 0x06400000) + tilenum;
|
|
|
|
|
|
|
|
u32 extpal = (DispCnt & 0x80000000);
|
|
|
|
|
|
|
|
u16* pal;
|
2017-04-09 01:35:32 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
if (extpal) pal = GetOBJExtPal(attrib[2] >> 12);
|
|
|
|
else pal = (u16*)&GPU::Palette[Num ? 0x600 : 0x200];
|
|
|
|
}
|
2017-03-02 18:00:19 +00:00
|
|
|
|
|
|
|
for (; xoff < boundwidth;)
|
2017-02-28 11:44:54 +00:00
|
|
|
{
|
2017-03-02 18:00:19 +00:00
|
|
|
if ((u32)rotX < width && (u32)rotY < height)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8 color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr + ((rotY>>11)*ytilefactor) + ((rotY&0x700)>>5) + ((rotX>>11)*64) + ((rotX&0x700)>>8));
|
2017-02-28 11:44:54 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
if (color)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = pal[color] | prio;
|
|
|
|
}
|
2017-03-02 18:00:19 +00:00
|
|
|
}
|
2017-02-28 11:44:54 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
rotX += rotA;
|
|
|
|
rotY += rotC;
|
|
|
|
xoff++;
|
|
|
|
xpos++;
|
|
|
|
}
|
2017-02-28 11:44:54 +00:00
|
|
|
}
|
2017-03-02 18:00:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// 16-color
|
|
|
|
tilenum <<= 5;
|
|
|
|
ytilefactor <<= 5;
|
|
|
|
u32 pixelsaddr = (Num ? 0x06600000 : 0x06400000) + tilenum;
|
2017-01-21 16:06:54 +00:00
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
u16* pal;
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
pal = (u16*)&GPU::Palette[Num ? 0x600 : 0x200];
|
|
|
|
pal += (attrib[2] & 0xF000) >> 8;
|
|
|
|
}
|
2017-01-21 16:06:54 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
for (; xoff < boundwidth;)
|
2017-01-21 16:06:54 +00:00
|
|
|
{
|
2017-03-02 18:00:19 +00:00
|
|
|
if ((u32)rotX < width && (u32)rotY < height)
|
|
|
|
{
|
2017-04-09 01:35:32 +00:00
|
|
|
u8 color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr + ((rotY>>11)*ytilefactor) + ((rotY&0x700)>>6) + ((rotX>>11)*32) + ((rotX&0x700)>>9));
|
2017-01-21 16:06:54 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
if (rotX & 0x100)
|
|
|
|
color >>= 4;
|
|
|
|
else
|
|
|
|
color &= 0x0F;
|
2017-01-21 16:06:54 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
if (color)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = pal[color] | prio;
|
|
|
|
}
|
2017-03-02 18:00:19 +00:00
|
|
|
}
|
2017-01-21 16:06:54 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
rotX += rotA;
|
|
|
|
rotY += rotC;
|
|
|
|
xoff++;
|
|
|
|
xpos++;
|
|
|
|
}
|
2017-01-21 16:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
template<bool window>
|
2017-01-21 02:36:14 +00:00
|
|
|
void GPU2D::DrawSprite_Normal(u16* attrib, u32 width, s32 xpos, u32 ypos, u32* dst)
|
|
|
|
{
|
|
|
|
u32 prio = ((attrib[2] & 0x0C00) << 6) | 0x8000;
|
|
|
|
u32 tilenum = attrib[2] & 0x03FF;
|
2017-04-09 01:35:32 +00:00
|
|
|
u32 spritemode = window ? 0 : ((attrib[0] >> 10) & 0x3);
|
2017-01-21 02:36:14 +00:00
|
|
|
|
|
|
|
u32 wmask = width - 8; // really ((width - 1) & ~0x7)
|
|
|
|
|
|
|
|
u32 xoff;
|
2017-03-02 00:53:08 +00:00
|
|
|
u32 xend = width;
|
2017-01-21 02:36:14 +00:00
|
|
|
if (xpos >= 0)
|
|
|
|
{
|
|
|
|
xoff = 0;
|
2017-03-02 00:53:08 +00:00
|
|
|
if ((xpos+xend) > 256)
|
|
|
|
xend = 256-xpos;
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xoff = -xpos;
|
|
|
|
xpos = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
if (spritemode == 3)
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
// bitmap sprite
|
2017-02-05 17:23:03 +00:00
|
|
|
|
2017-03-24 19:53:01 +00:00
|
|
|
u32 alpha = attrib[2] >> 12;
|
|
|
|
if (!alpha) return;
|
|
|
|
alpha++;
|
|
|
|
|
|
|
|
prio |= (0xC0000000 | (alpha << 24));
|
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
if (DispCnt & 0x40)
|
2017-02-03 23:07:25 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
if (DispCnt & 0x20)
|
2017-02-03 23:07:25 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
// TODO ("reserved")
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tilenum <<= (7 + ((DispCnt >> 22) & 0x1));
|
|
|
|
tilenum += (ypos * width * 2);
|
2017-02-03 23:07:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
if (DispCnt & 0x20)
|
2017-02-03 23:07:25 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
tilenum = ((tilenum & 0x01F) << 4) + ((tilenum & 0x3E0) << 7);
|
|
|
|
tilenum += (ypos * 256 * 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tilenum = ((tilenum & 0x00F) << 4) + ((tilenum & 0x3F0) << 7);
|
|
|
|
tilenum += (ypos * 128 * 2);
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 23:07:25 +00:00
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
u32 pixelsaddr = (Num ? 0x06600000 : 0x06400000) + tilenum;
|
|
|
|
pixelsaddr += (xoff << 1);
|
|
|
|
|
2017-03-02 00:53:08 +00:00
|
|
|
for (; xoff < xend;)
|
2017-03-01 22:02:50 +00:00
|
|
|
{
|
|
|
|
u16 color = GPU::ReadVRAM_OBJ<u16>(pixelsaddr);
|
|
|
|
pixelsaddr += 2;
|
|
|
|
|
|
|
|
if (color & 0x8000)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = color | prio;
|
|
|
|
}
|
2017-03-01 22:02:50 +00:00
|
|
|
|
|
|
|
xoff++;
|
|
|
|
xpos++;
|
2017-02-03 23:07:25 +00:00
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
if (DispCnt & 0x10)
|
|
|
|
{
|
|
|
|
tilenum <<= ((DispCnt >> 20) & 0x3);
|
|
|
|
tilenum += ((ypos >> 3) * (width >> 3)) << ((attrib[0] & 0x2000) ? 1:0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tilenum += ((ypos >> 3) * 0x20);
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-02 18:00:19 +00:00
|
|
|
if (spritemode == 1) prio |= 0x80000000;
|
|
|
|
else prio |= 0x10000000;
|
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
if (attrib[0] & 0x2000)
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
// 256-color
|
|
|
|
tilenum <<= 5;
|
|
|
|
u32 pixelsaddr = (Num ? 0x06600000 : 0x06400000) + tilenum;
|
|
|
|
pixelsaddr += ((ypos & 0x7) << 3);
|
|
|
|
|
|
|
|
u32 extpal = (DispCnt & 0x80000000);
|
|
|
|
|
|
|
|
u16* pal;
|
2017-04-09 01:35:32 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
if (extpal) pal = GetOBJExtPal(attrib[2] >> 12);
|
|
|
|
else pal = (u16*)&GPU::Palette[Num ? 0x600 : 0x200];
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
if (attrib[1] & 0x1000) // xflip. TODO: do better? oh well for now this works
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
pixelsaddr += (((width-1 - xoff) & wmask) << 3);
|
|
|
|
pixelsaddr += ((width-1 - xoff) & 0x7);
|
|
|
|
|
2017-03-02 00:53:08 +00:00
|
|
|
for (; xoff < xend;)
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
u8 color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr);
|
2017-02-27 20:26:11 +00:00
|
|
|
pixelsaddr--;
|
2017-03-01 22:02:50 +00:00
|
|
|
|
|
|
|
if (color)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = pal[color] | prio;
|
|
|
|
}
|
2017-03-01 22:02:50 +00:00
|
|
|
|
|
|
|
xoff++;
|
|
|
|
xpos++;
|
|
|
|
if (!(xoff & 0x7)) pixelsaddr -= 56;
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
2017-03-01 22:02:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixelsaddr += ((xoff & wmask) << 3);
|
|
|
|
pixelsaddr += (xoff & 0x7);
|
|
|
|
|
2017-03-02 00:53:08 +00:00
|
|
|
for (; xoff < xend;)
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
u8 color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr);
|
|
|
|
pixelsaddr++;
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
if (color)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = pal[color] | prio;
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
xoff++;
|
|
|
|
xpos++;
|
|
|
|
if (!(xoff & 0x7)) pixelsaddr += 56;
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
// 16-color
|
|
|
|
tilenum <<= 5;
|
|
|
|
u32 pixelsaddr = (Num ? 0x06600000 : 0x06400000) + tilenum;
|
|
|
|
pixelsaddr += ((ypos & 0x7) << 2);
|
|
|
|
|
2017-04-09 01:35:32 +00:00
|
|
|
u16* pal;
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
pal = (u16*)&GPU::Palette[Num ? 0x600 : 0x200];
|
|
|
|
pal += (attrib[2] & 0xF000) >> 8;
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
if (attrib[1] & 0x1000) // xflip. TODO: do better? oh well for now this works
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
pixelsaddr += (((width-1 - xoff) & wmask) << 2);
|
|
|
|
pixelsaddr += (((width-1 - xoff) & 0x7) >> 1);
|
|
|
|
|
2017-03-02 00:53:08 +00:00
|
|
|
for (; xoff < xend;)
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
u8 color;
|
|
|
|
if (xoff & 0x1)
|
|
|
|
{
|
|
|
|
color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr) & 0x0F;
|
|
|
|
pixelsaddr--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr) >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (color)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = pal[color] | prio;
|
|
|
|
}
|
2017-03-01 22:02:50 +00:00
|
|
|
|
|
|
|
xoff++;
|
|
|
|
xpos++;
|
|
|
|
if (!(xoff & 0x7)) pixelsaddr -= 28;
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
2017-03-01 22:02:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixelsaddr += ((xoff & wmask) << 2);
|
|
|
|
pixelsaddr += ((xoff & 0x7) >> 1);
|
|
|
|
|
2017-03-02 00:53:08 +00:00
|
|
|
for (; xoff < xend;)
|
2017-01-21 02:36:14 +00:00
|
|
|
{
|
2017-03-01 22:02:50 +00:00
|
|
|
u8 color;
|
|
|
|
if (xoff & 0x1)
|
|
|
|
{
|
|
|
|
color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr) >> 4;
|
|
|
|
pixelsaddr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
color = GPU::ReadVRAM_OBJ<u8>(pixelsaddr) & 0x0F;
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
if (color)
|
2017-04-09 01:35:32 +00:00
|
|
|
{
|
|
|
|
if (window) ((u8*)dst)[xpos] = 1;
|
|
|
|
else dst[xpos] = pal[color] | prio;
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
|
2017-03-01 22:02:50 +00:00
|
|
|
xoff++;
|
|
|
|
xpos++;
|
|
|
|
if (!(xoff & 0x7)) pixelsaddr += 28;
|
|
|
|
}
|
2017-01-21 02:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|