Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit b729dbb

Browse files
committed
Align core.c with SA3
1 parent c7fa7b5 commit b729dbb

1 file changed

Lines changed: 89 additions & 42 deletions

File tree

src/core.c

Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ static void UpdateScreenDma(void);
172172
static void UpdateScreenCpuSet(void);
173173
static void ClearOamBufferCpuSet(void);
174174
static void ClearOamBufferDma(void);
175-
static void GetInput(void);
176-
static bool32 ProcessVramGraphicsCopyQueue(void);
175+
void GetInput(void);
176+
bool32 ProcessVramGraphicsCopyQueue(void);
177177

178178
static void VBlankIntr(void);
179179
static void HBlankIntr(void);
@@ -184,6 +184,9 @@ static void Timer2Intr(void);
184184
static void Dma0Intr(void);
185185
static void Dma1Intr(void);
186186
static void Dma2Intr(void);
187+
#if (GAME == GAME_SA3)
188+
void sub_80C66DC(void);
189+
#endif
187190
static void Dma3Intr(void);
188191
static void KeypadIntr(void);
189192
static void GamepakIntr(void);
@@ -204,19 +207,24 @@ IntrFunc const gIntrTableTemplate[] = {
204207
Timer0Intr,
205208
Timer1Intr,
206209
Timer2Intr,
210+
#if (GAME == GAME_SA3)
211+
sub_80C66DC,
212+
#endif
207213
Dma0Intr,
208214
Dma1Intr,
209215
Dma2Intr,
210216
Dma3Intr,
211217
KeypadIntr,
212218
GamepakIntr,
219+
#if (GAME != GAME_SA3)
213220
NULL,
221+
#endif
214222
};
215223

216224
// Result of these:
217225
// FALSE: Not currently in vblank
218226
// TRUE: Currently in VBlank /
219-
static VBlankProcessFunc const sVblankFuncs[] = {
227+
VBlankProcessFunc const sVblankFuncs[] = {
220228
ProcessVramGraphicsCopyQueue,
221229
SA2_LABEL(sub_8004010),
222230
#ifndef COLLECT_RINGS_ROM
@@ -229,6 +237,8 @@ static VBlankProcessFunc const sVblankFuncs[] = {
229237
// Multiboot area?
230238
extern u8 gUnknown_02035000[0xA000];
231239
extern void sub_80C4B48(void);
240+
extern void sub_80C6908(void);
241+
void sub_80BCB84(void); // inside core.c in SA3
232242

233243
// (99.97%) https://decomp.me/scratch/hVNLQ
234244
NONMATCH("asm/non_matching/engine/EngineInit_sa3.inc", void EngineInit(void))
@@ -254,11 +264,11 @@ void EngineInit(void)
254264
if ((REG_RCNT & 0xC000) != 0x8000) {
255265
gFlags = FLAGS_200;
256266

257-
#if (ENGINE == ENGINE_3)
267+
#if (ENGINE != ENGINE_3)
268+
DmaCopy16(3, (void *)OBJ_VRAM0, EWRAM_START + 0x3B000, 0x5000);
269+
#else
258270
DmaCopy16(3, (void *)BG_SCREEN_ADDR(24), gUnknown_02035000, sizeof(gUnknown_02035000));
259271
DmaWait(3);
260-
#else
261-
DmaCopy16(3, (void *)OBJ_VRAM0, EWRAM_START + 0x3B000, 0x5000);
262272
#endif
263273
}
264274
#endif
@@ -268,10 +278,10 @@ void EngineInit(void)
268278
if (gInput == (START_BUTTON | SELECT_BUTTON | B_BUTTON | A_BUTTON)) {
269279
gFlags |= FLAGS_SKIP_INTRO;
270280
} else {
271-
#if (ENGINE == ENGINE_3)
272-
gFlags = 0;
273-
#else
281+
#if (ENGINE != ENGINE_3)
274282
gFlags &= ~FLAGS_SKIP_INTRO;
283+
#else
284+
gFlags = 0;
275285
#endif
276286
}
277287

@@ -292,20 +302,20 @@ void EngineInit(void)
292302
DmaWait(3);
293303
#endif
294304

295-
#if (ENGINE == ENGINE_3)
305+
#if (ENGINE != ENGINE_3)
296306
sLastCalledVblankFuncId = VBLANK_FUNC_ID_NONE;
297-
298-
gBackgroundsCopyQueueIndex = gBackgroundsCopyQueueCursor = 0;
307+
gBackgroundsCopyQueueCursor = 0;
308+
gBackgroundsCopyQueueIndex = 0;
299309
gBgSpritesCount = 0;
300310
gVramGraphicsCopyCursor = 0;
301-
gVramGraphicsCopyQueueIndex = gVramGraphicsCopyCursor = 0;
311+
gVramGraphicsCopyQueueIndex = 0;
302312
#else
303313
sLastCalledVblankFuncId = VBLANK_FUNC_ID_NONE;
304-
gBackgroundsCopyQueueCursor = 0;
305-
gBackgroundsCopyQueueIndex = 0;
314+
315+
gBackgroundsCopyQueueIndex = gBackgroundsCopyQueueCursor = 0;
306316
gBgSpritesCount = 0;
307317
gVramGraphicsCopyCursor = 0;
308-
gVramGraphicsCopyQueueIndex = 0;
318+
gVramGraphicsCopyQueueIndex = gVramGraphicsCopyCursor = 0;
309319
#endif
310320
DmaFill32(3, 0, gBgSprites_Unknown2, sizeof(gBgSprites_Unknown2));
311321
#if (ENGINE == ENGINE_3)
@@ -499,14 +509,14 @@ void EngineInit(void)
499509
INTR_VECTOR = IntrMain;
500510
#endif
501511

502-
#if (ENGINE == ENGINE_3)
512+
#if (ENGINE != ENGINE_3)
513+
REG_IME = INTR_FLAG_VBLANK;
503514
REG_IE = INTR_FLAG_VBLANK;
504515
REG_DISPSTAT = DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR;
505-
REG_IME = INTR_FLAG_VBLANK;
506516
#else
507-
REG_IME = INTR_FLAG_VBLANK;
508517
REG_IE = INTR_FLAG_VBLANK;
509518
REG_DISPSTAT = DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR;
519+
REG_IME = INTR_FLAG_VBLANK;
510520
#endif
511521

512522
// Setup multi sio
@@ -540,14 +550,14 @@ void EngineMainLoop(void)
540550
#endif
541551
{
542552
gExecSoundMain = FALSE;
543-
#if (ENGINE == ENGINE_3)
544-
if (gFlags & FLAGS_40000) {
545-
sub_80BCB84();
546-
}
547-
#else
553+
#if (ENGINE != ENGINE_3)
548554
if (!(gFlags & FLAGS_4000)) {
549555
m4aSoundMain();
550556
}
557+
#else
558+
if (gFlags & FLAGS_40000) {
559+
sub_80BCB84();
560+
}
551561
#endif
552562

553563
if (sLastCalledVblankFuncId == VBLANK_FUNC_ID_NONE) {
@@ -605,7 +615,7 @@ void EngineMainLoop(void)
605615
};
606616
}
607617

608-
static void UpdateScreenDma(void)
618+
void UpdateScreenDma(void)
609619
{
610620
u8 i, j = 0;
611621
REG_DISPCNT = gDispCnt;
@@ -709,13 +719,13 @@ static void UpdateScreenDma(void)
709719
}
710720
}
711721

712-
static void ClearOamBufferDma(void)
722+
void ClearOamBufferDma(void)
713723
{
714724
gNumHBlankCallbacks = 0;
715725

716726
gFlags &= ~FLAGS_EXECUTE_HBLANK_CALLBACKS;
717727
if (!(gFlags & FLAGS_20)) {
718-
#if (GAME == GAME_SA1)
728+
#if ((GAME == GAME_SA1) || (GAME == GAME_SA3))
719729
if (gBgOffsetsHBlankPrimary == gBgOffsetsBuffer[0]) {
720730
gBgOffsetsHBlankPrimary = gBgOffsetsBuffer[1];
721731
gBgOffsetsHBlankSecondary = gBgOffsetsBuffer[0];
@@ -744,7 +754,7 @@ static void ClearOamBufferDma(void)
744754
}
745755

746756
#ifndef COLLECT_RINGS_ROM
747-
static void UpdateScreenCpuSet(void)
757+
void UpdateScreenCpuSet(void)
748758
{
749759
u8 i, j = 0;
750760
REG_DISPCNT = gDispCnt;
@@ -820,7 +830,7 @@ static void UpdateScreenCpuSet(void)
820830
}
821831
#endif
822832

823-
static void VBlankIntr(void)
833+
void VBlankIntr(void)
824834
{
825835
u16 keys;
826836
DmaStop(0);
@@ -862,8 +872,11 @@ static void VBlankIntr(void)
862872
REG_IE = 0;
863873
REG_IME = 0;
864874
REG_DISPSTAT = DISPCNT_MODE_0;
875+
#if (ENGINE != ENGINE_3)
876+
// TODO: Maybe BUG_FIX?
865877
m4aMPlayAllStop();
866878
m4aSoundVSyncOff();
879+
#endif
867880
gFlags &= ~FLAGS_EXECUTE_HBLANK_COPY;
868881
DmaStop(0);
869882
DmaStop(1);
@@ -888,7 +901,7 @@ struct GraphicsData_Hack {
888901

889902
#define COPY_CHUNK_SIZE 1024
890903

891-
static bool32 ProcessVramGraphicsCopyQueue(void)
904+
bool32 ProcessVramGraphicsCopyQueue(void)
892905
{
893906
u32 offset;
894907
#ifndef NON_MATCHING
@@ -898,7 +911,11 @@ static bool32 ProcessVramGraphicsCopyQueue(void)
898911
#endif
899912

900913
while (gVramGraphicsCopyCursor != gVramGraphicsCopyQueueIndex) {
914+
#if (ENGINE != ENGINE_3)
901915
graphics = (void *)gVramGraphicsCopyQueue[gVramGraphicsCopyCursor];
916+
#else
917+
graphics = (void *)&gVramGraphicsCopyQueue[gVramGraphicsCopyCursor];
918+
#endif
902919

903920
if (graphics->size != 0) {
904921
for (offset = 0; graphics->size > 0; offset += COPY_CHUNK_SIZE) {
@@ -1002,6 +1019,43 @@ void GetInput(void)
10021019
}
10031020
}
10041021

1022+
#if (ENGINE == ENGINE_3)
1023+
void sub_80BCB84(void)
1024+
{
1025+
volatile u16 sp0;
1026+
volatile u16 sp2;
1027+
volatile u32 sp4;
1028+
volatile u16 sp8;
1029+
1030+
m4aMPlayAllStop();
1031+
m4aSoundVSyncOff();
1032+
sp4 = gFlagsPreVBlank;
1033+
gFlagsPreVBlank |= 0x8000;
1034+
sp0 = REG_DISPCNT;
1035+
sp8 = REG_DISPSTAT;
1036+
REG_DISPCNT = 0x80U;
1037+
REG_KEYCNT = 0x8304;
1038+
REG_IME = 0;
1039+
REG_DISPSTAT = 0;
1040+
sp2 = REG_IE;
1041+
REG_IE = 0x1000;
1042+
REG_IE |= 0x2000;
1043+
REG_IME = 1;
1044+
SoundBiasReset();
1045+
Stop();
1046+
SoundBiasSet();
1047+
REG_IME = 0;
1048+
REG_IE = sp2;
1049+
REG_IME = 1;
1050+
REG_DISPSTAT = sp8;
1051+
VBlankIntrWait();
1052+
REG_DISPCNT = sp0;
1053+
gFlagsPreVBlank = sp4;
1054+
gFlags &= ~FLAGS_40000;
1055+
m4aSoundVSyncOn();
1056+
}
1057+
#endif // (ENGINE == ENGINE_3)
1058+
10051059
static void HBlankIntr(void)
10061060
{
10071061
u8 i;
@@ -1017,28 +1071,21 @@ static void HBlankIntr(void)
10171071
}
10181072

10191073
static void VCountIntr(void) { REG_IF = INTR_FLAG_VCOUNT; }
1020-
10211074
static void Dma0Intr(void) { REG_IF = INTR_FLAG_DMA0; }
1022-
10231075
static void Dma1Intr(void) { REG_IF = INTR_FLAG_DMA1; }
1024-
10251076
static void Dma2Intr(void) { REG_IF = INTR_FLAG_DMA2; }
1026-
10271077
static void Dma3Intr(void) { REG_IF = INTR_FLAG_DMA3; }
1028-
10291078
static void Timer0Intr(void) { REG_IF = INTR_FLAG_TIMER0; }
1030-
10311079
static void Timer1Intr(void) { REG_IF = INTR_FLAG_TIMER1; }
1032-
10331080
static void Timer2Intr(void) { REG_IF = INTR_FLAG_TIMER2; }
1034-
1081+
#if (ENGINE == ENGINE_3)
1082+
static void Timer3Intr(void) { REG_IF = INTR_FLAG_TIMER3; }
1083+
#endif
10351084
static void KeypadIntr(void) { REG_IF = INTR_FLAG_KEYPAD; }
1036-
10371085
static void GamepakIntr(void) { REG_IF = INTR_FLAG_GAMEPAK; }
1086+
static void DummyFunc(void) { }
10381087

1039-
void DummyFunc(void) { }
1040-
1041-
#if (GAME == GAME_SA1)
1088+
#if ((GAME == GAME_SA1) || (GAME == GAME_SA3))
10421089
static void ClearOamBufferCpuSet(void)
10431090
{
10441091
gNumHBlankCallbacks = 0;

0 commit comments

Comments
 (0)