Skip to content

Commit

Permalink
Fix level reset.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeydumont committed Jul 19, 2019
1 parent 6655a5d commit d65d289
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 21 deletions.
21 changes: 14 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

# Binaries
ARMIPS := armips
CROSS ?= mips64
CROSS ?= mips64-ultra-elf
TOOLS_PREFIX ?= /usr/bin/
AS := $(CROSS)-as
CC := $(CROSS)-gcc
CXX := $(CROSS)-g++
LD := $(CROSS)-g++
Expand All @@ -25,19 +26,19 @@ XDELTA := xdelta3

# Compiler/linker flags.
N64_SYSROOT ?= /usr/$(CROSS)/n64-sysroot/usr/
ASFLAGS = -Wall -mtune=vr4300 -march=vr4300 -mabi=32
CFLAGS = -std=gnu11 -Wall -O1 -mtune=vr4300 -march=vr4300 -mabi=32 \
-mno-check-zero-division -mdivide-breaks \
$(GBI_VERSION) \
-DZ64_VERSION=Z64_OOT10 \
-DSETTINGS_HEADER=../../../src/settings.h \
-I ${N64_SYSROOT}/include/ \
-I $(CURDIR) \
-I $(CURDIR) \
$(SM64_VERSION_FLAG)
CXXFLAGS = -std=gnu++14 -Wall -O1 -mtune=vr4300 -march=vr4300 -mabi=32\
$(GBI_VERSION)
LDSCRIPT = $(N64_SYSROOT)/lib/gl-n64.ld
LDFLAGS = -T $(LDSCRIPT) -nostartfiles -specs=nosys.specs \
LDFLAGS = -T $(LDSCRIPT) -T uss64.ld -nostartfiles -specs=nosys.specs\
-march=vr4300 -mtune=vr4300 -mabi=32 -mdivide-breaks \
-mno-check-zero-division \
-Wl,--gc-sections \
Expand All @@ -55,6 +56,7 @@ DEBUG_SCRIPTS_OUT = debug_scripts_out
EMU_SCRIPTDIR = c/Users/Joey/Documents/VGs/Emulation/Project64d/Scripts

# Source file lists.
ASMFILES = $(ASMDIR)/respawn_object_hook2.s
USS64FILES = $(SRCDIR)/uss64_commands.c $(SRCDIR)/uss64.c \
$(SRCDIR)/sm64.c $(SRCDIR)/gz_api.c \
$(SRCDIR)/settings.c $(SRCDIR)/uss64_settings.c \
Expand All @@ -76,12 +78,13 @@ RESFILES = gz/res/gz/*.png
HEADERS = $(SRCDIR)/sm64.h

# Source files variables.
ASMSRC := $(foreach s, $(ASMFILES), $(wildcard $(s)))
USS64SRC := $(foreach s, $(USS64FILES),$(wildcard $(s)))
STDSRC := $(foreach s, $(STDFILES), $(wildcard $(s)))
GZSRC := $(foreach s, $(GZFILES), $(wildcard $(s)))
RESSRC := $(foreach s, $(RESFILES), $(wildcard $(s)))

OBJECTS = $(USS64_OBJECTS) $(STD_OBJECTS) $(GZ_OBJECTS) $(RES_OBJECTS)
OBJECTS = $(ASM_OBJECTS) $(USS64_OBJECTS) $(STD_OBJECTS) $(GZ_OBJECTS) $(RES_OBJECTS)

# Versions of SM64 to inject into.
USS64_VERSIONS = SM64_U SM64_J SM64_S #SM64_Q SM64_E SM64_D
Expand Down Expand Up @@ -139,19 +142,20 @@ TARGET-NAME-$(1) = $(2)
OUTPUT-DIR-$(1) = $(3)
SRCDIR-$(1) = $(4)
RESDIR-$(1) = $(5)
OBJDIR-$(1) = $(6)
OBJDIR-$(1) = $(6)
BINDIR-$(1) = $(7)

VERSION-$(1) = $$(shell echo $(3) | tail -c 2)

# -- Variables for pattern rules.
ASM_OBJECTS-$(1) := $$(patsubst %, $$(OBJDIR-$(1))/%.o,$$(ASMSRC))
USS64_OBJECTS-$(1) := $$(patsubst %, $$(OBJDIR-$(1))/%.o,$$(USS64SRC))
STD_OBJECTS-$(1) := $$(patsubst %, $$(OBJDIR-$(1))/%.o,$$(STDSRC))
GZ_OBJECTS-$(1) := $$(patsubst %, $$(OBJDIR-$(1))/%.o,$$(GZSRC))
RES_OBJECTS-$(1) := $$(patsubst %, $$(OBJDIR-$(1))/%.o,$$(RESSRC))

# -- Short names for the main C targets.
OBJ-$(1) = $$(USS64_OBJECTS-$(1)) $$(STD_OBJECTS-$(1)) $$(GZ_OBJECTS-$(1)) $$(RES_OBJECTS-$(1))
OBJ-$(1) = $$(ASM_OBJECTS-$(1)) $$(USS64_OBJECTS-$(1)) $$(STD_OBJECTS-$(1)) $$(GZ_OBJECTS-$(1)) $$(RES_OBJECTS-$(1))
BIN-$(1) = $$(BINDIR-$(1))/$$(TARGET-NAME-$(1)).bin
ELF-$(1) = $$(BINDIR-$(1))/$$(TARGET-NAME-$(1)).elf

Expand All @@ -163,7 +167,10 @@ $$(BIN-$(1)) : $$(ELF-$(1)) | $$$$(dir $$$$@)
$$(OBJCOPY) -S -O binary $$< $$@

$$(ELF-$(1)) : $$(OBJ-$(1)) | $$$$(dir $$$$@)
$$(LD) $$(LDFLAGS) $$^ -o $$@
$$(LD) $$(LDFLAGS) -Xlinker -Map=uss64_$$(VERSION-$(1)).map $$^ -o $$@

$$(ASM_OBJECTS-$(1)): $$(OBJDIR-$(1))/%.o : % src/sm64.h | $$$$(dir $$$$@)
$$(AS) $$(ASFLAGS) $$< -o $$@

$$(USS64_OBJECTS-$(1)): $$(OBJDIR-$(1))/%.o : % src/sm64.h | $$$$(dir $$$$@)
$$(CC) $$(CFLAGS) -c $$< -o $$@
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Ideas for a future release:
- [ ] Double savestates (asm)
- [ ] Something similar to memfiles in gz, with SD card writes
- [X] Level Select (similar to warps in gz)
- [ ] Not super stable. And some warps won't work depending on the current level. Should work out warp nodes.
- [ ] Not super stable. And some warps won't work depending on the current
level. Should work out warp nodes.
- [ ] Pre-made practice files (customizable)
- [ ] 120 Star
- [ ] Up RTA 74 star
Expand Down
22 changes: 22 additions & 0 deletions asm/hook.asm
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ SM64_osInvalDCache equ {osInvalDCache}
USS64_DisplayAddr equ {USS64_DisplayHook}
SM64_interaction_star_hook1 equ {SM64_interaction_star_hook1}
SM64_interaction_star_hook2 equ {SM64_interaction_star_hook2}
SM64_skip_intro_hook equ {SM64_skip_intro_hook}
SM64_init_level_hook equ {SM64_init_level_hook}
SM64_respawn_objects_hook1 equ {SM64_respawn_objects_hook1}
SM64_respawn_objects_hook2 equ {SM64_respawn_objects_hook2}
USS64_interaction_star_hook1 equ {USS64_Interaction_Star_Hook_1}
USS64_interaction_star_hook2 equ {USS64_Interaction_Star_Hook_2}
USS64_respawn_objects_hook equ {USS64_respawn_objects_hook}
USS64_skip_intro_hook equ {USS64_skip_intro_hook}
USS64_init_level_hook equ {USS64_init_level_hook}

.open SM64_ROM, USS64_ROM, SM64_RAMEntryPoint

Expand Down Expand Up @@ -93,6 +100,21 @@ jal USS64_interaction_star_hook1
.org SM64_interaction_star_hook2
jal USS64_interaction_star_hook2

// Hook into intro to skip it.
.org SM64_skip_intro_hook
j USS64_skip_intro_hook

//// Hook into init_level.
.org SM64_init_level_hook
jal USS64_init_level_hook

// Level Reset
.org SM64_respawn_objects_hook1
nop

.org SM64_respawn_objects_hook2
nop

// Import the payload at the end of the ROM.
.orga SM64_ROMPadding
.headersize 0
Expand Down
53 changes: 53 additions & 0 deletions asm/respawn_object_hook2.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.set noat
.set noreorder
.set gp=64

.text

# -- In US/JP, $a2 contains the obj->respawnInfoType. In Shindou, it's v0.
# -- Cannot modify a1,a2 (JP/US).
# -- Cannot modify a1,v0,t6 (S).
.global respawn_objects_hook
respawn_objects_hook:
lui $t2, %hi(uss64)
addiu $t2, $t2, %lo(uss64)
lbu $t3, 105($t2)
li $at, 1

# -- Skip this function the function if command_reset was called, and reset the state.
#beq $t3, $at, .jumpBackToMainCode
b .jumpBackToMainCode
nop
#sb $0 , 105($t2)

# -- Otherwise, re-implement the function logic.
.checkType32:
bne $a2, $at, .checkType16
nop

lui $t5, %hi(0x8029C704)
addiu $t5, $t5, %lo(0x8029C704)
jr $t5
nop

.checkType16:
li $at, 2
bne $a2, $at, .jumpBackToMainCode
nop

lui $t5, %hi(0x8029C728)
addiu $t5, $t5, %lo(0x8029C728)
jr $t5
nop

.jumpBackToMainCode:
lui $t5, %hi(0x8029C754)
addiu $t5, %lo(0x8029C754)
jr $t5
nop

# How to use C structs in GAS
# https://stackoverflow.com/questions/22185848/how-to-re-use-c-structs-in-arm-assembly-in-a-maintainable-and-readable-way

# Make sure that ld does not discard the section with the asm function
# https://stackoverflow.com/questions/31521326/gc-sections-discards-used-data
111 changes: 109 additions & 2 deletions sm64_hooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ Variables:
D: ,
}

SM64_gHudDisplay:
VariableType: struct HudDisplay
Addresses: {
J: 0x80339ef0,
U: 0x8033b260,
S: 0x8031DA88,
E: ,
Q: ,
D: ,
}

SM64_gSnowParticleCount:
VariableType: int16_t
Addresses: {
J: 0x803600a4,
U: 0x80361414,
S: 0x80343C54,
E: ,
Q: ,
D: ,
}


SM64_sNumVblanks:
VariableType: uint32_t
Addresses: {
Expand Down Expand Up @@ -301,6 +324,20 @@ Functions:
D: ,
}

save_file_set_flags:
ReturnType: void
Arguments:
- int32_t
Addresses: {
J: 0x80279af8,
U: 0x8027a0a8,
S: 0x8027314C,
E: ,
Q: ,
D: ,
}


Hooks:
SM64_RAMEntryPoint:
Addresses: {
Expand Down Expand Up @@ -394,8 +431,8 @@ Hooks:

SM64_FuncCalledAtSoundInitHook: # N/A to J/U.
Addresses: {
J: None,
U: None,
J: ,
U: ,
S: 0x802FD6B0,
E: ,
Q: ,
Expand All @@ -422,6 +459,76 @@ Hooks:
D: ,
}

SM64_init_level_hook:
Addresses: {
J: 0x8024B8F8,
U: 0x8024BA98,
S: 0x8024EB78,
E: ,
Q: ,
D: ,
}

SM64_skip_intro_hook:
Addresses: {
J: 0x8024BA34,
U: 0x8024BBD4,
S: 0x8024ECA4,
E: ,
Q: ,
D: ,
}

SM64_skip_intro_branch:
Addresses: {
J: 0x8024BA5C,
U: 0x8024BBFC,
S: 0x8024ECCC,
E: ,
Q: ,
D: ,
}

SM64_update_objects_hook:
Addresses: {
J: 0x8029D0E4,
U: ,
S: ,
E: ,
Q: ,
D: ,
}

SM64_respawn_objects_hook1:
Addresses: {
J: 0x8029C71C,
U: 0x8029CE9C,
S: 0x8028EC28,
E: ,
Q: ,
D: ,
}

SM64_respawn_objects_hook2:
Addresses: {
J: 0x8029C740,
U: 0x8029CEC0,
S: 0x8028EC3C,
E: ,
Q: ,
D: ,
}

SM64_lvl_reset_camera_fix_hook:
Addresses: {
J: 0x80286D2A,
U: 0x802872DA,
S: 0x8027D946,
E: ,
Q: ,
D: ,
}

Constants:
ACT_FALL_AFTER_STAR_GRAB: 0x00001904
ACT_STAR_DANCE_EXIT: 0x00001302
Expand Down
21 changes: 21 additions & 0 deletions src/offsets.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdint.h>
#include <stddef.h>

#include "uss64.h"

#define DEFINE(sym, val) \
__asm__ volatile("__AS_DEFINE__" #sym "\t%0" : : "n" ((unsigned long) val))

#define OFFSET(sym, str, mem) \
DEFINE(sym, offsetof(struct str, mem))

int main(void) {

OFFSET(USS64_CMD_STARSELECT_CALLED, uss64, command_starselect_was_called);
OFFSET(USS64_CMD_RELOAD_CALLED, uss64, command_reset_was_called);

if (uss64.command_reset_was_called == 1)
{
OFFSET(USS64_CURRENT_LVL_NUM, uss64, current_level_num);
}
}
1 change: 1 addition & 0 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void settings_load_default(void)
d->bits.timer = 1;
d->bits.non_stop = 0;
d->bits.special_triple_jump = 0;
d->bits.skip_intro = 1;
d->menu_x = 16;
d->menu_y = 64;
d->line_spacing = 1;
Expand Down
1 change: 1 addition & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct settings_bits
uint32_t timer : 1;
uint32_t non_stop : 1;
uint32_t special_triple_jump : 1;
uint32_t skip_intro : 1;
};

struct settings_data
Expand Down
10 changes: 10 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ struct MarioState
/*0xC4*/ float unkC4;
};

struct HudDisplay {
/*0x00*/ int16_t lives;
/*0x02*/ int16_t coins;
/*0x04*/ int16_t stars;
/*0x06*/ int16_t wedges;
/*0x08*/ int16_t keys;
/*0x0A*/ int16_t flags;
/*0x0C*/ uint16_t timer;
};

#endif
Loading

0 comments on commit d65d289

Please sign in to comment.