Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanded N64split to allow larger splits without segfaults #12

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Object files
*.o
*.obj
*.d

# Precompiled Headers
*.gch
Expand All @@ -18,3 +19,4 @@

# Executables
*.exe
bin
81 changes: 50 additions & 31 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,28 @@ GEO_SRC_FILES := sm64geo.c \
GRAPHICS_SRC_FILES := n64graphics.c \
utils.c

MI0_SRC_FILES := libmio0.c \
libmio0.h

SPLIT_SRC_FILES := blast.c \
libmio0.c \
libsfx.c \
mipsdisasm.c \
n64graphics.c \
n64split.c \
n64split/n64split.c \
n64split/n64split.sm64.geo.c \
n64split/n64split.sm64.behavior.c \
n64split/n64split.sm64.collision.c \
n64split/n64split.sound.c \
strutils.c \
utils.c \
yamlconfig.c

WALK_SRC_FILES := sm64walk.c

OBJ_DIR = ./obj
BIN_DIR = ./bin
SPLIT_DIR = $(OBJ_DIR)/n64split

##################### Compiler Options #######################

Expand All @@ -62,7 +73,7 @@ CC = $(CROSS)gcc
LD = $(CC)
AR = $(CROSS)ar

INCLUDES = -I./ext
INCLUDES = -I. -I./ext
DEFS =
# Release flags
CFLAGS = -Wall -Wextra -Wno-format-overflow -O2 -ffunction-sections -fdata-sections $(INCLUDES) $(DEFS) -MMD
Expand All @@ -80,12 +91,16 @@ EXTEND_OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(EXTEND_SRC_FILES:.c=.o))
F3D_OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(F3D_SRC_FILES:.c=.o))
F3D2OBJ_OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(F3D2OBJ_SRC_FILES:.c=.o))
GEO_OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(GEO_SRC_FILES:.c=.o))
MI0_OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(MI0_SRC_FILES:.c=.o))
SPLIT_OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(SPLIT_SRC_FILES:.c=.o))
OBJ_FILES = $(LIB_OBJ_FILES) $(EXTEND_OBJ_FILES) $(COMPRESS_OBJ_FILES) \
$(SPLIT_OBJ_FILES) $(CKSUM_OBJ_FILES) $(F3D_OBJ_FILES) \
$(F3D2OBJ_OBJ_FILES) $(GEO_OBJ_FILES)
WALK_OBJ_FILES = $(addprefix $(OBJ_DIR)/,$(WALK_SRC_FILES:.c=.o))
OBJ_FILES = $(LIB_OBJ_FILES) $(CKSUM_OBJ_FILES) $(COMPRESS_OBJ_FILES) \
$(EXTEND_OBJ_FILES) $(F3D_OBJ_FILES) $(F3D2OBJ_OBJ_FILES) \
$(GEO_OBJ_FILES) $(MI0_OBJ_FILES) $(SPLIT_OBJ_FILES) \
$(WALK_OBJ_FILES)
DEP_FILES = $(OBJ_FILES:.o=.d)


######################## Targets #############################

default: all
Expand All @@ -96,62 +111,66 @@ all: $(EXTEND_TARGET) $(COMPRESS_TARGET) $(MIO0_TARGET) $(CKSUM_TARGET) \

$(OBJ_DIR)/%.o: %.c
@[ -d $(OBJ_DIR) ] || mkdir -p $(OBJ_DIR)
@[ -d $(BIN_DIR) ] || mkdir -p $(BIN_DIR)
@[ -d $(SPLIT_DIR) ] || mkdir -p $(SPLIT_DIR)
$(CC) $(CFLAGS) -o $@ -c $<

$(SM64_LIB): $(LIB_OBJ_FILES)
rm -f $@
$(AR) rcs $@ $^

$(CKSUM_TARGET): $(CKSUM_OBJ_FILES) $(SM64_LIB)
$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
$(LD) $(LDFLAGS) -o $(BIN_DIR)/$@ $^ $(LIBS)

$(COMPRESS_TARGET): $(COMPRESS_OBJ_FILES) $(SM64_LIB)
$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
$(LD) $(LDFLAGS) -o $(BIN_DIR)/$@ $^ $(LIBS)

$(EXTEND_TARGET): $(EXTEND_OBJ_FILES) $(SM64_LIB)
$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
$(LD) $(LDFLAGS) -o $(BIN_DIR)/$@ $^ $(LIBS)

$(F3D_TARGET): $(F3D_OBJ_FILES)
$(LD) $(LDFLAGS) -o $@ $^
$(LD) $(LDFLAGS) -o $(BIN_DIR)/$@ $^

$(F3D2OBJ_TARGET): $(F3D2OBJ_OBJ_FILES)
$(LD) $(LDFLAGS) -o $@ $^
$(LD) $(LDFLAGS) -o $(BIN_DIR)/$@ $^

$(GEO_TARGET): $(GEO_OBJ_FILES)
$(LD) $(LDFLAGS) -o $@ $^
$(LD) $(LDFLAGS) -o $(BIN_DIR)/$@ $^

$(GRAPHICS_TARGET): $(GRAPHICS_SRC_FILES)
$(CC) $(CFLAGS) -DN64GRAPHICS_STANDALONE $^ $(LDFLAGS) -o $@
$(CC) $(CFLAGS) -DN64GRAPHICS_STANDALONE $^ $(LDFLAGS) -o $(BIN_DIR)/$@

$(MIO0_TARGET): libmio0.c libmio0.h
$(CC) $(CFLAGS) -DMIO0_STANDALONE $(LDFLAGS) -o $@ $<
$(MIO0_TARGET): $(MI0_SRC_FILES)
$(CC) $(CFLAGS) -DMIO0_STANDALONE $(LDFLAGS) -o $(BIN_DIR)/$@ $<

$(DISASM_TARGET): $(DISASM_SRC_FILES)
$(CC) $(CFLAGS) -DMIPSDISASM_STANDALONE $^ $(LDFLAGS) -o $@ -lcapstone
$(CC) $(CFLAGS) -DMIPSDISASM_STANDALONE $^ $(LDFLAGS) -o $(BIN_DIR)/$@ -lcapstone

$(SPLIT_TARGET): $(SPLIT_OBJ_FILES)
$(LD) $(LDFLAGS) -o $@ $^ $(SPLIT_LIBS)
$(LD) $(LDFLAGS) -o $(BIN_DIR)/$@ $^ $(SPLIT_LIBS)

$(WALK_TARGET): sm64walk.c $(SM64_LIB)
$(CC) $(CFLAGS) -o $@ $^
$(WALK_TARGET): $(WALK_SRC_FILES) $(SM64_LIB)
$(CC) $(CFLAGS) -o $(BIN_DIR)/$@ $^

rawmips: rawmips.c utils.c
$(CC) $(CFLAGS) -o $@ $^ -lcapstone
$(CC) $(CFLAGS) -o $(BIN_DIR)/$@ $^ -lcapstone

clean:
rm -f $(OBJ_FILES) $(DEP_FILES) $(SM64_LIB) $(MIO0_TARGET)
rm -f $(CKSUM_TARGET) $(CKSUM_TARGET).exe
rm -f $(COMPRESS_TARGET) $(COMPRESS_TARGET).exe
rm -f $(DISASM_TARGET) $(DISASM_TARGET).exe
rm -f $(EXTEND_TARGET) $(EXTEND_TARGET).exe
rm -f $(F3D_TARGET) $(F3D_TARGET).exe
rm -f $(F3D2OBJ_TARGET) $(F3D2OBJ_TARGET).exe
rm -f $(GEO_TARGET) $(GEO_TARGET).exe
rm -f $(MIO0_TARGET) $(MIO0_TARGET).exe
rm -f $(GRAPHICS_TARGET) $(GRAPHICS_TARGET).exe
rm -f $(SPLIT_TARGET) $(SPLIT_TARGET).exe
rm -f $(WALK_TARGET) $(WALK_TARGET).exe
rm -f $(OBJ_FILES) $(DEP_FILES) $(SM64_LIB) $(MIO0_TARGET) $(BIN_DIR)/*.d
rm -f $(BIN_DIR)/$(CKSUM_TARGET) $(BIN_DIR)/$(CKSUM_TARGET).exe
rm -f $(BIN_DIR)/$(COMPRESS_TARGET) $(BIN_DIR)/$(COMPRESS_TARGET).exe
rm -f $(BIN_DIR)/$(DISASM_TARGET) $(BIN_DIR)/$(DISASM_TARGET).exe
rm -f $(BIN_DIR)/$(EXTEND_TARGET) $(BIN_DIR)/$(EXTEND_TARGET).exe
rm -f $(BIN_DIR)/$(F3D_TARGET) $(BIN_DIR)/$(F3D_TARGET).exe
rm -f $(BIN_DIR)/$(F3D2OBJ_TARGET) $(BIN_DIR)/$(F3D2OBJ_TARGET).exe
rm -f $(BIN_DIR)/$(GEO_TARGET) $(BIN_DIR)/$(GEO_TARGET).exe
rm -f $(BIN_DIR)/$(MIO0_TARGET) $(BIN_DIR)/$(MIO0_TARGET).exe
rm -f $(BIN_DIR)/$(GRAPHICS_TARGET) $(BIN_DIR)/$(GRAPHICS_TARGET).exe
rm -f $(BIN_DIR)/$(SPLIT_TARGET) $(BIN_DIR)/$(SPLIT_TARGET).exe
rm -f $(BIN_DIR)/$(WALK_TARGET) $(BIN_DIR)/$(WALK_TARGET).exe
-@[ -d $(SPLIT_DIR) ] && rmdir --ignore-fail-on-non-empty $(SPLIT_DIR)
-@[ -d $(OBJ_DIR) ] && rmdir --ignore-fail-on-non-empty $(OBJ_DIR)
-@[ -d $(BIN_DIR) ] && rmdir --ignore-fail-on-non-empty $(BIN_DIR)

.PHONY: all clean default

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,13 @@ There are many other smaller tools included to help with SM64 hacking. They are
## License

MIT License. Copyright 2015 queueRAM.

## Building

### MacOSX
```
git pull --recurse-submodules
brew install capstone
brew install libyaml
export C_INCLUDE_PATH=../sm64tools:/usr/local/Cellar/capstone/4.0.1/include/:/usr/local/Cellar/libyaml/0.2.1/include && export LIBRARY_PATH=/usr/local/opt/capstone/lib/:/usr/local/Cellar/libyaml/0.2.1/lib:/usr/local/Cellar/libpng/1.6.36/lib && make
```
5 changes: 3 additions & 2 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef enum
typedef struct _label
{
unsigned int ram_addr;
char name[128];
char name[2048];
} label;

typedef struct _texture
Expand All @@ -50,11 +50,12 @@ typedef struct _texture

typedef struct _split_section
{
char label[128];
char label[512];
unsigned int start;
unsigned int end;
unsigned int vaddr;
section_type type;
char section_name[512];

int subtype;

Expand Down
31 changes: 29 additions & 2 deletions mipsdisasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct
{
char name[60];
unsigned int vaddr;
char prefix[FILENAME_MAX];
} asm_label;

typedef struct
Expand Down Expand Up @@ -231,7 +232,7 @@ static void disassemble_block(unsigned char *data, unsigned int length, unsigned
unsigned int jal_target = (unsigned int)insn[i].operands[0].imm;
// create label if one does not exist
if (labels_find(&state->globals, jal_target) < 0) {
char label_name[32];
char label_name[FILENAME_MAX];
sprintf(label_name, "func_%08X", jal_target);
labels_add(&state->globals, label_name, jal_target);
}
Expand Down Expand Up @@ -449,6 +450,7 @@ void mipsdisasm_pass2(FILE *out, disasm_state *state, unsigned int offset)
while ( (local_idx < block->locals.count) && (vaddr > block->locals.labels[local_idx].vaddr) ) {
local_idx++;
}
char previous_instruction[16];
for (int i = 0; i < block->instruction_count; i++) {
disasm_data *insn = &block->instructions[i];
// newline between functions
Expand All @@ -465,13 +467,37 @@ void mipsdisasm_pass2(FILE *out, disasm_state *state, unsigned int offset)
fprintf(out, "%s:\n", block->locals.labels[local_idx].name);
local_idx++;
}
// write out bytes as comment
fprintf(out, "/* %06X %08X %02X%02X%02X%02X */ ", offset, vaddr, insn->bytes[0], insn->bytes[1], insn->bytes[2], insn->bytes[3]);
// indent the lines after a jump or branch
if (indent) {
indent = 0;
fputc(' ', out);
}
if (insn->is_jump) {
if (strncmp(insn->mnemonic,"movf",4) == 0 || strncmp(insn->mnemonic,"lsa",4) == 0 || strncmp(insn->mnemonic,"dlsa",4) == 0
|| strncmp(insn->mnemonic,"movn",4) == 0 || strncmp(insn->mnemonic,"ext",4) == 0 || strncmp(insn->mnemonic,"movt",4) == 0
|| strncmp(insn->mnemonic,"movz",4) == 0 || strncmp(insn->mnemonic,"bbit",4) == 0 || strncmp(insn->mnemonic,"pref",4) == 0
|| strncmp(insn->mnemonic,"synci",5) == 0 || strncmp(insn->mnemonic,"ld.b",4) == 0 || strncmp(insn->mnemonic,"ori.b",4) == 0
|| strncmp(insn->mnemonic,"pause",4) == 0 || strncmp(insn->mnemonic,"rotr",4) == 0 || strncmp(insn->mnemonic,"madd",4) == 0
|| strncmp(insn->mnemonic,"nmsub",5) == 0 || strncmp(insn->mnemonic,"mz.",3) == 0
|| strncmp(insn->mnemonic,"bc0",3) == 0 || strncmp(insn->mnemonic,"dmtc",4) == 0 || strncmp(insn->mnemonic,"sync",4) == 0
|| strncmp(insn->mnemonic,"bseli",5) == 0 || strncmp(insn->mnemonic,"bnz.",4) == 0 || strncmp(insn->mnemonic,"snei",4) == 0
|| strncmp(insn->mnemonic,"cle_s.",6) == 0 || strncmp(insn->mnemonic,"bz.",3) == 0 || strncmp(insn->mnemonic,"msub.",5) == 0
|| strncmp(insn->mnemonic,"shrav.",5) == 0 || strncmp(insn->mnemonic,"din",3) == 0 || strncmp(insn->mnemonic,"cins",4) == 0
|| strncmp(insn->mnemonic,"st.",3) == 0 || strncmp(insn->mnemonic,"shra",4) == 0 || strncmp(insn->mnemonic,"dextm",5) == 0
|| strncmp(insn->mnemonic,"srl.",4) == 0 || strncmp(insn->mnemonic,"bc1",3) == 0 || strncmp(insn->mnemonic,"sra.",4) == 0
|| strncmp(insn->mnemonic,"fmul.",4) == 0 || strncmp(insn->mnemonic,"dextu",5) == 0
)
{
// These instructions aren't supported on the N64 but capstone didn't know that
fprintf(out, ".byte 0x%02X,0x%02X,0x%02X,0x%02X /* Because of invalid n64 opcode %s */\n", insn->bytes[0], insn->bytes[1], insn->bytes[2], insn->bytes[3], insn->mnemonic);
strcpy(insn->mnemonic, ".byte");
}
else if (strncmp(previous_instruction,".byte",4) == 0 ) {
// fprintf(out, ".byte 0x%02X,0x%02X,0x%02X,0x%02X /* Because previous was .byte */\n", insn->bytes[0], insn->bytes[1], insn->bytes[2], insn->bytes[3]);
// strcpy(insn->mnemonic, ".byte");
}
else if (insn->is_jump) {
indent = 1;
fprintf(out, "%-5s ", insn->mnemonic);
if (insn->id == MIPS_INS_JAL || insn->id == MIPS_INS_BAL || insn->id == MIPS_INS_J) {
Expand Down Expand Up @@ -638,6 +664,7 @@ void mipsdisasm_pass2(FILE *out, disasm_state *state, unsigned int offset)
}
vaddr += 4;
offset += 4;
strcpy(previous_instruction, insn->mnemonic);
}
}

Expand Down
Loading