Skip to content

Commit

Permalink
Port over berry fix program
Browse files Browse the repository at this point in the history
  • Loading branch information
PikalaxALT committed Dec 17, 2018
1 parent cc84f66 commit f91b71d
Show file tree
Hide file tree
Showing 47 changed files with 7,189 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ clean: tidy
rm -f sound/direct_sound_samples/*.bin
rm -f $(SONG_OBJS)
find . \( -iname '*.1bpp' -o -iname '*.4bpp' -o -iname '*.8bpp' -o -iname '*.gbapal' -o -iname '*.lz' -o -iname '*.latfont' -o -iname '*.hwjpnfont' -o -iname '*.fwjpnfont' \) -exec rm {} +
@$(MAKE) -C berry_fix clean

tidy:
rm -f $(ROM) $(ELF) $(MAP)
rm -r build/*
@$(MAKE) -C berry_fix tidy

include graphics_file_rules.mk

Expand Down Expand Up @@ -169,3 +171,6 @@ $(ELF): $(OBJ_DIR)/ld_script.ld $(OBJS)
$(ROM): $(ELF)
$(OBJCOPY) -O binary $< $@
$(FIX) $@ -p -t"$(TITLE)" -c$(GAME_CODE) -m$(MAKER_CODE) -r$(REVISION) --silent

berry_fix/berry_fix.gba:
@$(MAKE) -C berry_fix
160 changes: 160 additions & 0 deletions berry_fix/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
include $(DEVKITARM)/base_tools
export CPP := $(PREFIX)cpp
export LD := $(PREFIX)ld

ifeq ($(OS),Windows_NT)
EXE := .exe
else
EXE :=
endif

GAME_CODE := AGBJ
MAKER_CODE := 01
REVISION := 0

SHELL := /bin/bash -o pipefail

CPPFLAGS := -I ../tools/agbcc/include -I ../tools/agbcc -iquote include -nostdinc -undef

ROM := berry_fix.gba
OBJ_DIR := build
CC1 := ../tools/agbcc/bin/agbcc$(EXE)
override CFLAGS += -mthumb-interwork -Wimplicit -Wparentheses -Werror -O2 -fhex-asm


ELF = $(ROM:.gba=.elf)
MAP = $(ROM:.gba=.map)

C_SUBDIR = src
ASM_SUBDIR = asm
DATA_ASM_SUBDIR = data

C_BUILDDIR = $(OBJ_DIR)/$(C_SUBDIR)
ASM_BUILDDIR = $(OBJ_DIR)/$(ASM_SUBDIR)
DATA_ASM_BUILDDIR = $(OBJ_DIR)/$(DATA_ASM_SUBDIR)

ASFLAGS := -mcpu=arm7tdmi

LDFLAGS = -Map ../$(MAP)

SHA1 := $(shell { command -v sha1sum || command -v shasum; } 2>/dev/null) -c
GFX := ../tools/gbagfx/gbagfx$(EXE)
AIF := ../tools/aif2pcm/aif2pcm$(EXE)
MID := ../tools/mid2agb/mid2agb$(EXE)
SCANINC := ../tools/scaninc/scaninc$(EXE)
PREPROC := ../tools/preproc/preproc$(EXE)
RAMSCRGEN := ../tools/ramscrgen/ramscrgen$(EXE)
FIX := ../tools/gbafix/gbafix$(EXE)

# Clear the default suffixes
.SUFFIXES:
# Don't delete intermediate files
.SECONDARY:
# Delete files that weren't built properly
.DELETE_ON_ERROR:

# Secondary expansion is required for dependency variables in object rules.
.SECONDEXPANSION:

.PHONY: rom clean compare tidy

C_SRCS := $(wildcard $(C_SUBDIR)/*.c $(C_SUBDIR)/*/*.c $(C_SUBDIR)/*/*/*.c)
C_OBJS := $(patsubst $(C_SUBDIR)/%.c,$(C_BUILDDIR)/%.o,$(C_SRCS))

ASM_SRCS := $(wildcard $(ASM_SUBDIR)/*.s)
ASM_OBJS := $(patsubst $(ASM_SUBDIR)/%.s,$(ASM_BUILDDIR)/%.o,$(ASM_SRCS))

DATA_ASM_SRCS := $(wildcard $(DATA_ASM_SUBDIR)/*.s)
DATA_ASM_OBJS := $(patsubst $(DATA_ASM_SUBDIR)/%.s,$(DATA_ASM_BUILDDIR)/%.o,$(DATA_ASM_SRCS))

SONG_SRCS := $(wildcard $(SONG_SUBDIR)/*.s)
SONG_OBJS := $(patsubst $(SONG_SUBDIR)/%.s,$(SONG_BUILDDIR)/%.o,$(SONG_SRCS))

MID_SRCS := $(wildcard $(MID_SUBDIR)/*.mid)
MID_OBJS := $(patsubst $(MID_SUBDIR)/%.mid,$(MID_BUILDDIR)/%.o,$(MID_SRCS))

OBJS := $(C_OBJS) $(ASM_OBJS) $(DATA_ASM_OBJS) $(SONG_OBJS) $(MID_OBJS)
# OBJS_REL := $(patsubst $(OBJ_DIR)/%,%,$(OBJS))

SUBDIRS := $(sort $(dir $(OBJS)))

$(shell mkdir -p $(SUBDIRS))

rom: $(ROM)

# For contributors to make sure a change didn't affect the contents of the ROM.
compare: $(ROM)
@$(SHA1) rom.sha1

clean: tidy
rm -f sound/direct_sound_samples/*.bin
rm -f $(SONG_OBJS) $(MID_OBJS) $(MID_SUBDIR)/*.s
find . \( -iname '*.1bpp' -o -iname '*.4bpp' -o -iname '*.8bpp' -o -iname '*.gbapal' -o -iname '*.lz' -o -iname '*.latfont' -o -iname '*.hwjpnfont' -o -iname '*.fwjpnfont' \) -exec rm {} +
make -C payload clean

tidy:
rm -f $(ROM) $(ELF) $(MAP)
rm -r build/*
make -C payload tidy

%.s: ;
%.png: ;
%.pal: ;
%.aif: ;

%.1bpp: %.png ; $(GFX) $< $@
%.4bpp: %.png ; $(GFX) $< $@
%.8bpp: %.png ; $(GFX) $< $@
%.gbapal: %.pal ; $(GFX) $< $@
%.gbapal: %.png ; $(GFX) $< $@
%.lz: % ; $(GFX) $< $@
%.rl: % ; $(GFX) $< $@


ifeq ($(NODEP),1)
$(C_BUILDDIR)/%.o: c_dep :=
else
$(C_BUILDDIR)/%.o: c_dep = $(shell $(SCANINC) -I include $(C_SUBDIR)/$*.c)
endif

$(C_BUILDDIR)/%.o : $(C_SUBDIR)/%.c $$(c_dep)
@$(CPP) $(CPPFLAGS) $< -o $(C_BUILDDIR)/$*.i
@$(PREPROC) $(C_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(C_BUILDDIR)/$*.s
$(AS) $(ASFLAGS) -o $@ $(C_BUILDDIR)/$*.s

ifeq ($(NODEP),1)
$(ASM_BUILDDIR)/%.o: asm_dep :=
else
$(ASM_BUILDDIR)/%.o: asm_dep = $(shell $(SCANINC) $(ASM_SUBDIR)/$*.s)
endif

$(ASM_BUILDDIR)/%.o: $(ASM_SUBDIR)/%.s $$(asm_dep)
$(AS) $(ASFLAGS) -o $@ $<

ifeq ($(NODEP),1)
$(DATA_ASM_BUILDDIR)/%.o: data_dep :=
else
$(DATA_ASM_BUILDDIR)/%.o: data_dep = $(shell $(SCANINC) $(DATA_ASM_SUBDIR)/$*.s)
endif

payload: data/payload.gba.lz

payload/payload.gba:
$(MAKE) -C payload/

data/payload.gba.lz: payload/payload.gba
$(GFX) $< $@ -search 1

$(DATA_ASM_BUILDDIR)/%.o: $(DATA_ASM_SUBDIR)/%.s $$(data_dep)
$(PREPROC) $< charmap.txt | $(CPP) -I include | $(AS) $(ASFLAGS) -o $@

$(SONG_BUILDDIR)/%.o: $(SONG_SUBDIR)/%.s
$(AS) $(ASFLAGS) -I sound -o $@ $<

$(ELF): ld_script.txt $(OBJS)
cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ../ld_script.txt -o ../$@

$(ROM): $(ELF)
$(OBJCOPY) -O binary $< $@
$(FIX) $@ -c$(GAME_CODE) -m$(MAKER_CODE) -r$(REVISION) --silent

35 changes: 35 additions & 0 deletions berry_fix/asm/berry_fix_header.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.global BerryFixMBHeaderNintendoLogo
BerryFixMBHeaderNintendoLogo:
.space 156

BerryFixMBHeaderGameTitle:
.space 12

.global BerryFixMBHeaderGameCode
BerryFixMBHeaderGameCode:
.space 4

BerryFixMBHeaderMakerCode:
.space 2

BerryFixMBHeaderMagic:
.byte 0

BerryFixMBHeaderMainUnitCode:
.byte 0

BerryFixMBHeaderDeviceType:
.byte 0

BerryFixMBHeaderReserved1:
.space 7

.global BerryFixMBHeaderSoftwareVersion
BerryFixMBHeaderSoftwareVersion:
.byte 0

BerryFixMBHeaderChecksum:
.byte 0

BerryFixMBHeaderReserved2:
.space 2
119 changes: 119 additions & 0 deletions berry_fix/asm/loader.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
.include "asm/macros/asm.inc"
.include "asm/macros/function.inc"
.include "constants/gba_constants.inc"

.set SIO_ERROR, 0x0040
.set SIO_MULTI_BUSY, 0x0080

.set EWRAM_ORIG, 0x02000000
.set gCode, 0x02010000
.set PROG_ORIG, 0x00008000

.syntax unified

.text

arm_func_start _start
_start: @ 0
b _entry
arm_func_end _start

.include "asm/berry_fix_header.inc"

@ C0
.word 0

.global _GPIOPortData
_GPIOPortData: @ C4
.2byte 0

.global _GPIOPortDirection
_GPIOPortDirection: @ C6
.2byte 0

.global _GPIOPortReadEnable
_GPIOPortReadEnable: @ C8
.2byte 0

@ CA
.2byte 0

@ CC
.space 0x34

arm_func_start _entry
_entry: @ 100
b _send
arm_func_end _entry

.space 0x1C

arm_func_start _recv
_recv:
@ Waits until link cable is no longer busy.
@ Returns nz if an error has occurred
@ Otherwise, returns the received short in r1.
@ Preserves r0
_120:
ldrh r1, [r0, 0x8] @ SIOCNT
tst r1, SIO_MULTI_BUSY
beq _120
_12c:
ldrh r1, [r0, 0x8] @ SIOCNT
tst r1, SIO_MULTI_BUSY
bne _12c
ldrh r1, [r0, 0x8] @ SIOCNT
tst r1, SIO_ERROR
bxne lr
ldrh r1, [r0] @ SIOMULTI0
bx lr
arm_func_end _recv

arm_func_start _send
_send: @ 14c
ldr r0, =REG_SIOMULTI0
_150:
bl _recv
bne _150
mov r2, 0
strh r2, [r0, 0xa] @ SIOMLT_SEND
cmp r1, 0
bne _150
mov r2, 0x8000
_16c:
mov r1, 0
_170:
strh r1, [r0, 0xa] @ SIOMLT_SEND
bl _recv
bne _150
cmp r1, r2
bne _16c
lsr r2, 5
cmp r1, 0
bne _170
ldr r3, =BerryFixMBHeaderGameCode
ldrh r2, [r3]
strh r2, [r0, 0xa] @ SIOMLT_SEND
bl _recv
_1a0:
bne _1a0
cmp r1, r2
bne _1a0
ldrh r2, [r3, 0x2]
strh r2, [r0, 0xa] @ SIOMLT_SEND
bl _recv
bne _1a0
cmp r1, r2
bne _1a0
mov r1, 0
strh r1, [r0, 0xa] @ SIOMLT_SEND
ldr r0, =_data_2f0
ldr r1, =gCode
swi 0x11 << 16
ldr lr, =gCode
bx lr
.pool
arm_func_end _send
@ 1f0

.align 2, 0 @ don't pad with nop
12 changes: 12 additions & 0 deletions berry_fix/asm/macros/asm.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.macro inc x
.set \x, \x + 1
.endm

.macro enum_start x=0
.set __enum__, \x
.endm

.macro enum constant
.equiv \constant, __enum__
inc __enum__
.endm
29 changes: 29 additions & 0 deletions berry_fix/asm/macros/function.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.macro arm_func_start name
.align 2, 0
.global \name
.arm
.type \name, %function
.endm

.macro arm_func_end name
.size \name, .-\name
.endm

.macro thumb_func_start name
.align 2, 0
.global \name
.thumb
.thumb_func
.type \name, %function
.endm

.macro non_word_aligned_thumb_func_start name
.global \name
.thumb
.thumb_func
.type \name, %function
.endm

.macro thumb_func_end name
.size \name, .-\name
.endm
7 changes: 7 additions & 0 deletions berry_fix/asmdiff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

OBJDUMP="$DEVKITARM/bin/arm-none-eabi-objdump -D -bbinary -marmv4t -Mforce-thumb"
OPTIONS="--start-address=$(($1)) --stop-address=$(($1 + $2))"
$OBJDUMP $OPTIONS baserom.gba > baserom.dump
$OBJDUMP $OPTIONS berry_fix.gba > berry_fix.dump
diff -c baserom.dump berry_fix.dump
10 changes: 10 additions & 0 deletions berry_fix/build_tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
make -C tools/gbagfx CXX=${1:-g++}
make -C tools/scaninc CXX=${1:-g++}
make -C tools/preproc CXX=${1:-g++}
make -C tools/bin2c CXX=${1:-g++}
make -C tools/rsfont CXX=${1:-g++}
make -C tools/aif2pcm CXX=${1:-g++}
make -C tools/ramscrgen CXX=${1:-g++}
make -C tools/gbafix CXX=${1:-g++}
make -C tools/mid2agb CXX=${1:-g++}
Loading

0 comments on commit f91b71d

Please sign in to comment.