sotn-decomp/tools/sotn-debugmodule/inject.c
Luciano Ciccariello 9b9b3f649f
Custom debugging module (#108)
# What

Adds a custom debug menu. It is written in C and it is meant to replace
the Bat familar `SERVANT/TT_000.BIN`. Once loaded you can long-press
SELECT+START to soft-reset the game and keep using the debug menu
everywhere, including when playing with Richter or during the credits.

One key requirement to run this is to have an emulator that emulates the
8MB of RAM. This is a key requirement to have the debug module surviving
soft-resets or accessing to the in-game menu. I personally used [PCSX
Redux](https://github.com/grumpycoders/pcsx-redux) to build this module.
I am not sure about the compatibility with other emulators. This does
**NOT** work on real hardware and it is a choice by design. The debug
module is intended to test different areas of the game and help
decompiling. It is not intended to be used in normal gameplay.

# Build

Simply invoke `make disk_debug` to create a disk image of the game in
`build/` with the debug module replacing the Bat familiar.

# Usage


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/15a040b6-6191-41c4-b2b8-a4a906ed59eb)


On PCSX Redux go to Configuration, Emulation and tick the box `8MB`.

## Loading the module


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/65b7ccb3-800e-4b66-84f5-5703fc91babe)

You need to enable the Bat Card from the menu. This will load the debug
module from the disk. If you want to re-load the module you need to
select another Familar Card, un-pause, pause again and select the Bat
Card once more.

## The main screen


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/b528425a-ea6c-4c10-9c19-522612d0ad2a)

You will know you have loaded the debug module when you see the blue
rectangle on the top right. You can press R2 to cycle between the menus.
Some menus will temporarily freeze the game, some not. To quickly return
in-game you can either press TRIANGLE or START. To bring back the paused
debug menu just press R2 once again.

## Debug Mode


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/12cac1ee-725b-484f-8564-89c03b6a755b)

### Stage

Teleports the player to a different stage. It is not stable and it can
crash often.

### Player

Switches between Alucard and Richter. Currently switching from Richter
to Alucard consistently crashes the game.

### No Clip

Allows to freely move the player within the room and without the
collisions on. Once the flag is enabled from the debug menu, pressing L2
while in-game will temporarily freeze the player movement and make it
immune to collision checking. You can then press the directional buttons
to slowly move the player or you can hold CROSS to move it faster. You
can also use SQUARE or CIRCLE to cycle between the player frames. Press
L2 again to deactivate the NoClip mode.

### Frame by frame

Freezes the game outside the debug module. Press L1 to advance by 1
frame. Hold L2 to put the game in slow-motion.

### Show hitboxes


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/ea8dd918-cf37-4be1-bb87-541a66ac7f16)

As shown in the image

### Show debug messages


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/5c9bc7b2-6bc7-4831-949a-bf73cdf910e8)

When the debug menu is un-paused, prints on the top left all the debug
messages from the game itself.

### Show collision layer


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/959ad1ab-dfe0-42fe-a8d0-65c619469280)

This prints the internal collision value for every 16x16 tile on the
screen. Look the CheckCollision function for more information on how
each printed value is used.

### Show draw calls


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/a52970fc-bf30-430d-98e3-5fca33c9f6f7)

Shows the maximum GPU resource usage since the game started. Currently
only the `max` option works. The `current` option will not show
anything.

### Show HBlank


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/81759019-6bf9-4cf2-b88c-aa60eaa5ddcb)

Prints the current horizontal blank interrupt count.

## Entity Spawn


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/f0fdc024-4537-4c07-a80d-816d605a3583)

Allows to immediately spawn new entities in the current map

### Mode

There are three list of entities, each one with their own ID: DRA, Stage
and RIC. The option RIC is hidden if Richter is not the current playing
character. As the list of entities per stage is maintained manually,
stage entities might not be available for all stages. The `Alloc` shows
how many entities are reserved or actually used. Pressing the SQUARE
button here will destroy all the entities within that range.

### ID

Press Left or Right to cycle between the different IDs available. Some
of them might crash the game immediately once spawned. Press CROSS to
immediately spawn the entity.

### Params

Each entity might have its own parameters. Sometimes the flag 0x8000 is
used, which can be toggled with the SQUARE button. Press CROSS to
immediately spawn the entity.

### Entity preview

Shows the entity before spawning it. This is turned off by default as it
can immediately crash by cycling through the available entity IDs.

### Place entity

Pressing CROSS will allow to move the entity across the screen before
placing it. Press CROSS again to place the entity and return to the
previous screen. Press SQUARE to quickly place multiple entities of the
same type.

## Sound player


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/9d8ad3a5-bc78-4674-abd0-94537bbfae98)

There are three macro categories the sound player is split into. For
what is currently known only the sounds within the Kind 3 changes based
on the loaded stage.

### Stop all sounds

This will also disable the SPU IRQ, effectively unlocking the frame
rate.

### Load Stage

Loads a different sound font than the current loaded stage. This can
help to quickly preview and test SFXs from other stages without
necessarily moving the player there.

### Load Servant

Loads the sound font of a specific servant without necessarily equipping
the Familiar Card.

## Castle flags

Preview all the flags used to modify the behaviour of different parts of
the two castles.

### Edit mode


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/b03ada2f-0fb4-4345-ad50-81209702032c)

You can move the cursor with the directional buttons and flip the flag
with CROSS. Press L1 or R1 to cycle between the pages. The cursor warps
when reaching the border of the flag grid, allowing a faster navigation.

### View mode

Allows to move between the flags more flexibly.

### Listen mode


![image](https://github.com/Xeeynamo/sotn-decomp/assets/6128729/4f9ed118-d09d-4658-ba25-5865dbebbf3c)

Listens for the modified flags while playing. Every time a flag is
modified the offset and its value is registered on the top left up to 4
rows. When all the rows are occupied, new values will just remove the
oldest one. The last modified flag will always be displayed at the
bottom.

---------

Co-authored-by: Alejandro Asenjo Nitti <sonicdcer@users.noreply.github.com>
Co-authored-by: sozud <sozud@users.noreply.github.com>
2023-08-10 18:55:24 +01:00

62 lines
1.5 KiB
C

#include <game.h>
typedef struct {
u32 Init;
u32 Update;
u32 Unk08;
u32 Unk0C;
u32 Unk10;
u32 Unk14;
u32 Unk18;
u32 Unk1C;
u32 Unk20;
u32 Unk24;
u32 Unk28;
u32 Unk2C;
u32 Unk30;
u32 Unk34;
u32 Unk38;
u32 Unk3C;
} ServantDesc;
const u32 INJECT_MAIN_ADDR = 0x80280040; // sotn-debugmodule.map
const u32 MODULE_ADDR = 0x80280000; // sotn-debugmodule.ld
const u32 LOAD_ADDR = 0x80170000; // Familiar overlay address
const u32 INJECT = INJECT_MAIN_ADDR - (MODULE_ADDR - LOAD_ADDR);
void Dummy(Entity* e);
ServantDesc g_ServantDesc __attribute__((section(".inject-head"))) = {
INJECT, Dummy, Dummy, Dummy, Dummy, Dummy, Dummy, Dummy,
Dummy, Dummy, Dummy, Dummy, Dummy, Dummy, Dummy, Dummy,
};
#define JAL(addr) ((((u32)(addr)&0x3FFFFFFU) >> 2) | 0x0C000000U)
int MainLoop();
void __attribute__((section(".inject-func"))) InjectMain(void) {
u32* const InjectPoint = 0x800E3D00U;
int i;
// Enable 8MB of ram
*(unsigned int*)0x1F801060 |= 0xE00;
// The game loads this overlay in 0x80170000, but as this module is compiled
// to use 0x80280000 as a base, we copy the overlay to there.
__builtin_memcpy((void*)MODULE_ADDR, (void*)LOAD_ADDR, 40960U);
// Now we say to 'entrypoint_sotn' to call our main every frame
*InjectPoint = JAL(MainLoop);
}
void Dummy(Entity* e) {}
bool g_Init = false;
void Init(void);
bool Update(void);
int MainLoop() {
if (!g_Init) {
Init();
g_Init = true;
}
return Update();
}