-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPU.cpp
More file actions
199 lines (156 loc) · 6.89 KB
/
PPU.cpp
File metadata and controls
199 lines (156 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "PPU.h"
const uint16_t Colors [4] = {0xffff, 0xaaaa, 0x5555, 0x0000};
uint8_t BGPalette [4];
uint8_t SpritePalette0 [4];
uint8_t SpritePalette1 [4];
using namespace Utils;
PPU::PPU (const char* Title, const uint16_t _PixelSize) {
PixelSize = _PixelSize;
MainWindow = SDL_CreateWindow (Title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width * PixelSize, Height * PixelSize, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
MainRenderer = SDL_CreateRenderer(MainWindow, -1, SDL_RENDERER_ACCELERATED);
MainTexture = SDL_CreateTexture (MainRenderer, SDL_PIXELFORMAT_ARGB4444, SDL_TEXTUREACCESS_STREAMING, Width, Height);
SDL_SetRenderDrawColor(MainRenderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(MainRenderer);
}
PPU::~PPU () {
SDL_DestroyTexture (MainTexture);
SDL_DestroyRenderer (MainRenderer);
SDL_DestroyWindow (MainWindow);
}
inline void PPU::SetPixel (uint32_t CoordX, uint32_t CoordY, uint32_t Color) {
uint32_t PixelNo = CoordY * Width + CoordX;
Pixels [PixelNo] = Color;
}
void PPU::OAMSearch (uint8_t* Memory, uint8_t* IOMap) {
uint8_t SpriteSize = 8 + (GetBit (IOMap [0x40], 2) << 3); // 8x8 or 8x16
uint8_t QueueNumber = 0;
for (int i = 0xFE00; i <= 0xFE9F; i += 4) {
if (CurrentY + 16 >= Memory[i] && CurrentY + 16 < Memory[i] + SpriteSize) { // Y Position
//printf ("%d: Load sprite at %d\n", QueueNumber, CurrentY);
memcpy (OAMQueue + (QueueNumber << 2), Memory + i, 4);
QueueNumber++;
if (QueueNumber == 10) // Max 10 sprites per line
break;
}
}
SpriteCount = QueueNumber;
}
void PPU::Render () {
SDL_UpdateTexture (MainTexture, NULL, PixelsReady, 2 * Width);
SDL_RenderCopy (MainRenderer, MainTexture, NULL, NULL);
SDL_RenderPresent (MainRenderer);
}
void PPU::Update (uint8_t* Memory, uint8_t* IOMap) {
uint16_t BGTable = 0x9800;
uint16_t WindowTable = 0x9800;
if (GetBit (IOMap [0x40], 3))
BGTable = 0x9C00;
if (GetBit (IOMap [0x40], 6))
WindowTable = 0x9C00;
uint8_t BGAddressingMode = GetBit (IOMap [0x40], 4); // 0 - Signed (0x8000), 1 - Unsigned (0x9000)
if (GetBit (IOMap [0x40], 0)) { // BG Enabled
// Set BG Palette
BGPalette [0] = GetBit (IOMap [0x47], 0) | (GetBit (IOMap [0x47], 1) << 1);
BGPalette [1] = GetBit (IOMap [0x47], 2) | (GetBit (IOMap [0x47], 3) << 1);
BGPalette [2] = GetBit (IOMap [0x47], 4) | (GetBit (IOMap [0x47], 5) << 1);
BGPalette [3] = GetBit (IOMap [0x47], 6) | (GetBit (IOMap [0x47], 7) << 1);
}
if (GetBit (IOMap [0x40], 1)) { // Sprites Enabled
// Set Sprite Palettes
SpritePalette0 [1] = GetBit (IOMap [0x48], 2) | (GetBit (IOMap [0x48], 3) << 1);
SpritePalette0 [2] = GetBit (IOMap [0x48], 4) | (GetBit (IOMap [0x48], 5) << 1);
SpritePalette0 [3] = GetBit (IOMap [0x48], 6) | (GetBit (IOMap [0x48], 7) << 1);
SpritePalette1 [1] = GetBit (IOMap [0x49], 2) | (GetBit (IOMap [0x49], 3) << 1);
SpritePalette1 [2] = GetBit (IOMap [0x49], 4) | (GetBit (IOMap [0x49], 5) << 1);
SpritePalette1 [3] = GetBit (IOMap [0x49], 6) | (GetBit (IOMap [0x49], 7) << 1);
}
if (CurrentY < Height) {
for (int CurrentX = 0; CurrentX < Width; CurrentX++) {
uint32_t ColorToDraw = Colors [BGPalette [0]];
uint8_t BGColor = 0;
if (GetBit (IOMap [0x40], 0)) { // BG Display + Window Display (DMG Only)
uint8_t BGX = CurrentX + IOMap[0x43];
uint8_t BGY = CurrentY + IOMap[0x42];
uint8_t BGTile = Memory [BGTable + ((BGY >> 3) << 5) + (BGX >> 3)];
uint8_t PixelX = BGX & 0x7;
uint8_t PixelY = BGY & 0x7;
uint8_t* BGTileData;
if (BGAddressingMode)
BGTileData = Memory + (0x8000 + (BGTile << 4));
else
BGTileData = Memory + (0x9000 + (int8_t) BGTile * 16);
// First Byte: LSB of Color for Pixel (PixelX, PixelY)
// Second Byte: MSB ~~~
uint8_t Color = (GetBit (BGTileData [PixelY * 2 + 1], 7 - PixelX) << 1) | GetBit (BGTileData [PixelY * 2], 7 - PixelX);
BGColor = Color;
ColorToDraw = Colors [BGPalette [Color]];
}
if (GetBit (IOMap [0x40], 0) && GetBit (IOMap [0x40], 5)) { // Window Display
uint8_t CoordX = IOMap[0x4B];
uint8_t CoordY = IOMap[0x4A];
if (CoordX - 7 <= CurrentX && CoordY <= CurrentY) {
uint8_t WindowX = CurrentX + 7 - CoordX;
uint8_t WindowY = CurrentY - CoordY;
uint8_t WindowTile = Memory [WindowTable + ((WindowY >> 3) << 5) + (WindowX >> 3)];
uint8_t PixelX = WindowX & 0x7;
uint8_t PixelY = WindowY & 0x7;
uint8_t* WindowTileData;
if (BGAddressingMode)
WindowTileData = Memory + (0x8000 + (WindowTile << 4));
else
WindowTileData = Memory + (0x9000 + (int8_t) WindowTile * 16);
// First Byte: LSB of Color for Pixel (PixelX, PixelY)
// Second Byte: MSB ~~~
uint8_t Color = (GetBit (WindowTileData [PixelY * 2 + 1], 7 - PixelX) << 1) | GetBit (WindowTileData [PixelY * 2], 7 - PixelX);
ColorToDraw = Colors [BGPalette [Color]]; // Shared with BG
}
}
if (GetBit (IOMap [0x40], 1)) { // Sprite Display
uint16_t MinX = 256; // Just greater than max (SpriteX)
for (int i = 0; i < (SpriteCount << 2); i += 4) {
uint8_t CoordY = OAMQueue [i];
uint8_t CoordX = OAMQueue [i + 1];
if (CurrentX + 8 >= CoordX && CurrentX < CoordX) { // Sprite in Current X
uint8_t PixelX = 7 - ((CurrentX + 8) - CoordX); // True X coordinate is 7 - X, due to the order of Bits
uint8_t PixelY = (CurrentY + 16) - CoordY;
uint8_t SpriteTile = OAMQueue [i + 2];
if (GetBit (IOMap [0x40], 2)) // Ignore bit 0 if 8x16
SetBit (SpriteTile, 0, 0);
uint8_t* SpriteTileData = Memory + (0x8000 + (SpriteTile << 4));
uint8_t Color = 0;
if (GetBit (OAMQueue [i + 3], 5)) // Flip X
PixelX = 7 - PixelX;
// Y flipping is done differently for 8x16
if (GetBit (IOMap [0x40], 2)) { // 0 - 8x8, 1 - 8x16
if (GetBit (OAMQueue [i + 3], 6)) // Flip Y
PixelY = 15 - PixelY;
} else {
if (GetBit (OAMQueue [i + 3], 6)) // Flip Y
PixelY = 7 - PixelY;
}
Color = (GetBit (SpriteTileData [PixelY * 2 + 1], PixelX) << 1) | GetBit (SpriteTileData [PixelY * 2], PixelX);
if (Color != 0) { // Not Transparent
if (GetBit (OAMQueue [i + 3], 4)) // Choose sprite palette
Color = SpritePalette1 [Color];
else
Color = SpritePalette0 [Color];
if (CoordX < MinX) { // Only draw over the sprite if our X coord is <
MinX = CoordX;
if (GetBit (OAMQueue [i + 3], 7)) { // Above only if BG Color is 0
if (BGColor == 0)
ColorToDraw = Colors [Color];
} else
ColorToDraw = Colors [Color]; // Above BG
}
}
}
}
}
SetPixel (CurrentX, CurrentY, ColorToDraw);
}
}
CurrentY = (CurrentY + 1) % 154;
IOMap [0x44] = CurrentY; // Update current line that's being scanned
if (CurrentY == 0) // End of Frame, Save the good pixels to be drawn at 60 Hz afterwards
memcpy (PixelsReady, Pixels, sizeof (Pixels));
}