-
Notifications
You must be signed in to change notification settings - Fork 9
/
_main.cpp
356 lines (303 loc) · 9.3 KB
/
_main.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <tonc.h>
#include "Benchmark.h"
#include "BuildConfig.h"
#include "Palette.h"
#include "Protocol.h"
#include "RuntimeConfig.h"
#include "SPISlave.h"
#include "Utils.h"
#include "_state.h"
extern "C" {
#include "gsmplayer/player.h"
}
#define TRY(ACTION) \
if (!(ACTION)) \
goto reset;
// ------------
// DECLARATIONS
// ------------
void wipeScreen();
void init();
void mainLoop();
void syncReset();
bool sendKeysAndReceiveMetadata();
bool receiveAudio();
bool receivePixels();
void render(bool withRLE, u32 width, u32 scaleX, u32 scaleY, u32 totalPixels);
bool needsToRunAudio();
void runAudio();
u32 transfer(u32 packetToSend, bool withRecovery = true);
bool sync(u32 command);
u32 x(u32 cursor, u32 width, u32 scaleX);
u32 y(u32 cursor, u32 width, u32 scaleY);
void optimizedRender();
SPISlave* spiSlave = new SPISlave();
// -----------
// DEFINITIONS
// -----------
CODE_EWRAM int main() {
RuntimeConfig::initialize();
while (true) {
RuntimeConfig::show();
wipeScreen();
if (config.isBenchmark()) {
syncReset();
Benchmark::init();
Benchmark::mainLoop();
}
init();
mainLoop();
#ifdef WITH_AUDIO
player_stop();
#endif
}
return 0;
}
ALWAYS_INLINE void wipeScreen() {
tte_erase_screen();
for (u32 i = 0; i < TOTAL_SCREEN_PIXELS; i++)
m4Draw(i, 0);
setMosaic(1, 1);
}
ALWAYS_INLINE void init() {
enableMode4AndBackground2();
setMosaic(RENDER_MODE_SCALEX[config.renderMode],
config.scanlines ? 1 : RENDER_MODE_SCALEY[config.renderMode]);
dma3_cpy(pal_bg_mem, MAIN_PALETTE, sizeof(COLOR) * PALETTE_COLORS);
#ifdef WITH_AUDIO
player_init();
#endif
}
CODE_IWRAM void mainLoop() {
state.hasAudio = false;
state.isVBlank = false;
state.isAudioReady = false;
reset:
syncReset();
while (true) {
if ((config.exitWithStart && needsRestartSTART()) || needsRestartABLR())
return;
TRY(sync(CMD_FRAME_START))
TRY(sendKeysAndReceiveMetadata())
if (state.hasAudio) {
TRY(sync(CMD_AUDIO))
TRY(receiveAudio())
}
TRY(sync(CMD_PIXELS))
TRY(receivePixels())
TRY(sync(CMD_FRAME_END))
optimizedRender();
}
}
ALWAYS_INLINE void syncReset() {
u32 resetPacket =
CMD_RESET + (config.renderMode |
(config.controls << CONTROLS_BIT_OFFSET) |
(config.compression << COMPRESSION_BIT_OFFSET) |
(config.cpuOverclock << CPU_OVERCLOCK_BIT_OFFSET));
while (transfer(resetPacket, false) != resetPacket)
;
}
ALWAYS_INLINE bool sendKeysAndReceiveMetadata() {
u16 keys = pressedKeys();
u32 metadata = spiSlave->transfer(keys);
if (spiSlave->transfer(metadata) != keys)
return false;
state.expectedPackets = (metadata >> PACKS_BIT_OFFSET) & PACKS_BIT_MASK;
state.startPixel = metadata & START_BIT_MASK;
state.isRLE = (metadata & COMPR_BIT_MASK) != 0;
state.hasAudio = (metadata & AUDIO_BIT_MASK) != 0;
u32 diffMaxPackets =
TEMPORAL_DIFF_MAX_PACKETS(RENDER_MODE_PIXELS[config.renderMode]);
u32 diffStart = (state.startPixel / 8) / PACKET_SIZE;
u32 diffEndPacket = min(spiSlave->transfer(0), diffMaxPackets);
for (u32 i = diffStart; i < diffEndPacket; i++)
((u32*)state.temporalDiffs)[i] = transfer(i);
for (u32 i = diffEndPacket; i < diffMaxPackets; i++)
((u32*)state.temporalDiffs)[i] = 0;
return true;
}
ALWAYS_INLINE bool receiveAudio() {
for (u32 i = 0; i < AUDIO_SIZE_PACKETS; i++)
((u32*)state.audioChunks)[i] = transfer(i);
state.isAudioReady = true;
return true;
}
ALWAYS_INLINE bool receivePixels() {
for (u32 i = 0; i < state.expectedPackets; i++)
((u32*)compressedPixels)[i] = transfer(i);
return true;
}
ALWAYS_INLINE void render(bool withRLE,
u32 width,
u32 scaleX,
u32 scaleY,
u32 totalPixels) {
u32 cursor = state.startPixel;
u32 rleRepeats = compressedPixels[0];
u32 decompressedBytes = withRLE;
#define RUN_AUDIO_IF_NEEDED() \
if (withRLE) { \
if (needsToRunAudio()) \
runAudio(); \
} else { \
if (!(cursor % 8) && needsToRunAudio()) \
runAudio(); \
}
#define DRAW_PIXEL(PIXEL) \
m4Draw(y(cursor, width, scaleY) * DRAW_WIDTH + x(cursor, width, scaleX), \
PIXEL);
#define DRAW_NEXT() \
if (withRLE) { \
u8 pixel = compressedPixels[decompressedBytes]; \
if (--rleRepeats == 0) { \
rleRepeats = compressedPixels[decompressedBytes + 1]; \
decompressedBytes += 2; \
} \
DRAW_PIXEL(pixel) \
cursor++; \
} else { \
u8 pixel = compressedPixels[decompressedBytes]; \
DRAW_PIXEL(pixel); \
decompressedBytes++; \
cursor++; \
}
#define DRAW_BATCH(TIMES) \
u32 target = min(cursor + TIMES, totalPixels); \
while (cursor < target) { \
RUN_AUDIO_IF_NEEDED() \
DRAW_NEXT() \
}
while (cursor < totalPixels) {
RUN_AUDIO_IF_NEEDED()
u32 diffCursor = cursor / 8;
u32 diffCursorBit = cursor % 8;
if (diffCursorBit == 0) {
u32 diffWord, diffHalfWord, diffByte;
if ((diffWord = ((u32*)state.temporalDiffs)[diffCursor / 4]) == 0) {
// (none of the next 32 pixels changed)
cursor += 32;
continue;
} else if (diffWord == 0xffffffff) {
// (all next 32 pixels changed)
DRAW_BATCH(32)
continue;
} else if ((diffHalfWord = ((u16*)state.temporalDiffs)[diffCursor / 2]) ==
0) {
// (none of the next 16 pixels changed)
cursor += 16;
continue;
} else if (diffHalfWord == 0xffff) {
// (all next 16 pixels changed)
DRAW_BATCH(16)
continue;
} else if ((diffByte = state.temporalDiffs[diffCursor]) == 0) {
// (none of the 8 pixels changed)
cursor += 8;
continue;
} else if (diffByte == 0xff) {
// (all next 8 pixels changed)
DRAW_BATCH(8)
continue;
}
}
u8 diffByte = state.temporalDiffs[diffCursor];
if (BIT_IS_HIGH(diffByte, diffCursorBit)) {
// (a pixel changed)
DRAW_NEXT()
} else
cursor++;
}
}
ALWAYS_INLINE bool needsToRunAudio() {
#ifndef WITH_AUDIO
return false;
#endif
if (!state.isVBlank && IS_VBLANK) {
state.isVBlank = true;
return true;
} else if (state.isVBlank && !IS_VBLANK)
state.isVBlank = false;
return false;
}
CODE_IWRAM void runAudio() {
if (player_needsData() && state.isAudioReady) {
player_play((const unsigned char*)state.audioChunks, AUDIO_CHUNK_SIZE);
state.isAudioReady = false;
}
spiSlave->stop();
player_run();
spiSlave->start();
}
ALWAYS_INLINE u32 transfer(u32 packetToSend, bool withRecovery) {
bool breakFlag = false;
u32 receivedPacket =
spiSlave->transfer(packetToSend, needsToRunAudio, &breakFlag);
if (breakFlag) {
runAudio();
if (withRecovery) {
sync(CMD_RECOVERY);
spiSlave->transfer(packetToSend);
receivedPacket = spiSlave->transfer(packetToSend);
}
}
return receivedPacket;
}
CODE_IWRAM bool sync(u32 command) {
u32 local = command + CMD_GBA_OFFSET;
u32 remote = command + CMD_RPI_OFFSET;
bool wasVBlank = IS_VBLANK;
while (true) {
bool breakFlag = false;
bool isOnSync =
spiSlave->transfer(local, needsToRunAudio, &breakFlag) == remote;
if (breakFlag) {
runAudio();
continue;
}
if (isOnSync)
return true;
else {
bool isVBlank = IS_VBLANK;
if (!wasVBlank && isVBlank)
wasVBlank = true;
else if (wasVBlank && !isVBlank)
return false;
}
}
}
ALWAYS_INLINE u32 x(u32 cursor, u32 width, u32 scaleX) {
return (cursor % width) * scaleX;
}
ALWAYS_INLINE u32 y(u32 cursor, u32 width, u32 scaleY) {
return (cursor / width) * scaleY;
}
ALWAYS_INLINE void optimizedRender() {
#define RENDER(N, WITH_RLE) \
render(WITH_RLE, RENDER_MODE_WIDTH[N], RENDER_MODE_SCALEX[N], \
RENDER_MODE_SCALEY[N], RENDER_MODE_PIXELS[N]);
#define HANDLE_RENDER_MODE(N) \
case N: { \
if (!RENDER_MODE_IS_INVALID(N)) { \
if (state.isRLE) \
RENDER(N, true) \
else \
RENDER(N, false) \
} \
break; \
}
// (this creates multiple copies of render(...)'s code)
switch (config.renderMode) {
HANDLE_RENDER_MODE(0)
HANDLE_RENDER_MODE(1)
HANDLE_RENDER_MODE(2)
HANDLE_RENDER_MODE(3)
HANDLE_RENDER_MODE(4)
HANDLE_RENDER_MODE(5)
HANDLE_RENDER_MODE(6)
HANDLE_RENDER_MODE(7)
HANDLE_RENDER_MODE(8)
default:
break;
}
}