split out files and add build batch file

This commit is contained in:
YamaArashi 2015-10-05 06:03:59 -07:00
parent 7f3c790d09
commit 9024595c93
7 changed files with 236 additions and 206 deletions

133
asm/crt0.s Normal file
View File

@ -0,0 +1,133 @@
.include "rom_header.s"
_080000C0:
.long 0
_080000C4:
.short 0
_080000C6:
.short 0
_080000C8:
.long 0
.incbin "base_emerald.gba", 0xCC, 0x204 - 0xCC
arm_func_start Init
Init:
mov r0, PSR_IRQ_MODE
msr cpsr_cf, r0
ldr sp, sp_irq
mov r0, PSR_SYS_MODE
msr cpsr_cf, r0
ldr sp, sp_sys
ldr r1, =INTR_VECTOR
adr r0, InterruptMain
str r0, [r1]
ldr r1, =Main + 1
mov lr, pc
bx r1
b Init
.align 2, 0
sp_sys: .4byte IWRAM_END - 0x1c0
sp_irq: .4byte IWRAM_END - 0x60
.pool
arm_func_end Init
arm_func_start InterruptMain
InterruptMain:
mov r3, 0x4000000
add r3, r3, 0x200
ldr r2, [r3]
ldrh r1, [r3, 0x8]
mrs r0, spsr
stmdb sp!, {r0-r3,lr}
mov r0, 0
strh r0, [r3, 0x8]
and r1, r2, r2, lsr 16
mov r12, 0
ands r0, r1, 0x4
bne _08000320
add r12, r12, 0x4
mov r0, 0x1
strh r0, [r3, 0x8]
ands r0, r1, 0x80
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x40
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x2
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x1
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x8
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x10
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x20
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x100
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x200
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x400
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x800
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x1000
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x2000
strbne r0, [r3, -0x17C]
_0800031C:
bne _0800031C
_08000320:
strh r0, [r3, 0x2]
bic r2, r2, r0
ldr r0, =0x03007868
ldr r0, [r0]
ldrb r0, [r0, 0xA]
mov r1, 0x8
mov r0, r1, lsl r0
orr r0, r0, 0x2000
orr r1, r0, 0xC6
and r1, r1, r2
strh r1, [r3]
mrs r3, cpsr
bic r3, r3, 0xDF
orr r3, r3, 0x1F
msr cpsr_cf, r3
ldr r1, =0x03002710
add r1, r1, r12
ldr r0, [r1]
stmdb sp!, {lr}
add lr, pc, 0
bx r0
ldmia sp!, {lr}
mrs r3, cpsr
bic r3, r3, 0xDF
orr r3, r3, 0x92
msr cpsr_cf, r3
ldmia sp!, {r0-r3,lr}
strh r2, [r3]
strh r1, [r3, 0x8]
msr spsr_cf, r0
bx lr
.pool
arm_func_end InterruptMain

14
asm/defines.s Normal file
View File

@ -0,0 +1,14 @@
.set PSR_USR_MODE, 0x00000010
.set PSR_FIQ_MODE, 0x00000011
.set PSR_IRQ_MODE, 0x00000012
.set PSR_SVC_MODE, 0x00000013
.set PSR_ABT_MODE, 0x00000017
.set PSR_UND_MODE, 0x0000001b
.set PSR_SYS_MODE, 0x0000001f
.set EWRAM_START, 0x02000000
.set EWRAM_END, EWRAM_START + 0x40000
.set IWRAM_START, 0x03000000
.set IWRAM_END, IWRAM_START + 0x8000
.set INTR_VECTOR, IWRAM_END - 0x4

View File

@ -1,214 +1,20 @@
.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
.include "defines.s"
.include "macros.s"
.syntax unified
.global RomBase
.global Start
.text
.arm
RomBase:
b start_loop
CartridgeHeaderNintendoLogo:
.incbin "base_emerald.gba", 0x4, 0xA0 - 0x4
CartridgeHeaderGameTitle:
.ascii "POKEMON EMER"
CartridgeHeaderGameCode:
.ascii "BPEE"
CartridgeHeaderMakerCode:
.ascii "01"
CartridgeHeaderMagic:
.byte 0x96
CartridgeHeaderMainUnitCode:
.byte 0
CartridgeHeaderDeviceType:
.byte 0
CartridgeHeaderReserved1:
.rept 7
.byte 0
.endr
CartridgeHeaderSoftwareVersion:
.byte 0
CartridgeHeaderChecksum:
.byte 0x72
CartridgeHeaderReserved2:
.rept 2
.byte 0
.endr
_080000C0:
.long 0
_080000C4:
.short 0
_080000C6:
.short 0
_080000C8:
.long 0
.incbin "base_emerald.gba", 0xCC, 0x204 - 0xCC
arm_func_start start_loop
start_loop: @ 8000204
mov r0, 0x12
msr cpsr_cf, r0
ldr sp, _0800023C
mov r0, 0x1F
msr cpsr_cf, r0
ldr sp, _08000238
ldr r1, _08000240
add r0, pc, 0x20
str r0, [r1]
ldr r1, _08000244
mov lr, pc
bx r1
b start_loop
.align 2, 0
_08000238: .4byte 0x03007e40
_0800023C: .4byte 0x03007fa0
_08000240: .4byte 0x03007ffc
_08000244: .4byte main_loop + 1
arm_func_end start_loop
arm_func_start irq_handler
irq_handler: @ 8000248
mov r3, 0x4000000
add r3, r3, 0x200
ldr r2, [r3]
ldrh r1, [r3, 0x8]
mrs r0, spsr
stmdb sp!, {r0-r3,lr}
mov r0, 0
strh r0, [r3, 0x8]
and r1, r2, r2, lsr 16
mov r12, 0
ands r0, r1, 0x4
bne _08000320
add r12, r12, 0x4
mov r0, 0x1
strh r0, [r3, 0x8]
ands r0, r1, 0x80
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x40
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x2
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x1
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x8
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x10
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x20
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x100
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x200
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x400
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x800
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x1000
bne _08000320
add r12, r12, 0x4
ands r0, r1, 0x2000
strbne r0, [r3, -0x17C]
_0800031C:
bne _0800031C
_08000320:
strh r0, [r3, 0x2]
bic r2, r2, r0
ldr r0, _0800039C
ldr r0, [r0]
ldrb r0, [r0, 0xA]
mov r1, 0x8
mov r0, r1, lsl r0
orr r0, r0, 0x2000
orr r1, r0, 0xC6
and r1, r1, r2
strh r1, [r3]
mrs r3, cpsr
bic r3, r3, 0xDF
orr r3, r3, 0x1F
msr cpsr_cf, r3
ldr r1, _080003A0
add r1, r1, r12
ldr r0, [r1]
stmdb sp!, {lr}
add lr, pc, 0
bx r0
ldmia sp!, {lr}
mrs r3, cpsr
bic r3, r3, 0xDF
orr r3, r3, 0x92
msr cpsr_cf, r3
ldmia sp!, {r0-r3,lr}
strh r2, [r3]
strh r1, [r3, 0x8]
msr spsr_cf, r0
bx lr
.align 2, 0
_0800039C: .4byte 0x03007868
_080003A0: .4byte 0x03002710
arm_func_end irq_handler
Start:
.include "crt0.s"
.thumb
thumb_func_start main_loop
main_loop: @ 80003A4
thumb_func_start Main
Main:
push {r4-r7,lr}
mov r7, r8
push {r7}
@ -321,7 +127,7 @@ _080004B2:
b _0800042A
.align 2, 0
_080004C0: .4byte 0x030022b4
thumb_func_end main_loop
thumb_func_end Main
thumb_func_start call_callbacks_wrapper
call_callbacks_wrapper: @ 80004C4
@ -605,7 +411,7 @@ _08000690:
pop {r0}
bx r0
.align 2, 0
_080006D0: .4byte irq_handler
_080006D0: .4byte InterruptMain
_080006D4: .4byte 0x03002750
_080006D8: .4byte 0x082e9548
_080006DC: .4byte 0x03002710
@ -56771,8 +56577,8 @@ _0801B688: .4byte 0x03005d90
_0801B68C: .4byte 0x03005d8c
_0801B690: .4byte 0x00003564
_0801B694: .4byte 0x00002bb0
_0801B698: .4byte CartridgeHeaderGameCode
_0801B69C: .4byte CartridgeHeaderSoftwareVersion
_0801B698: .4byte RomHeaderGameCode
_0801B69C: .4byte RomHeaderSoftwareVersion
thumb_func_end sub_801B580
thumb_func_start sub_801B6A0
@ -972091,7 +971897,7 @@ _082DEE76:
.align 2, 0
_082DEE78: .4byte 0x0000a1c1
_082DEE7C: .4byte 0x6177614b
_082DEE80: .4byte CartridgeHeaderNintendoLogo
_082DEE80: .4byte RomHeaderNintendoLogo
thumb_func_end sub_82DED84
thumb_func_start sub_82DEE84

29
asm/macros.s Normal file
View File

@ -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

38
asm/rom_header.s Normal file
View File

@ -0,0 +1,38 @@
b Init
RomHeaderNintendoLogo:
.incbin "base_emerald.gba", 0x4, 0xA0 - 0x4
RomHeaderGameTitle:
.ascii "POKEMON EMER"
RomHeaderGameCode:
.ascii "BPEE"
RomHeaderMakerCode:
.ascii "01"
RomHeaderMagic:
.byte 0x96
RomHeaderMainUnitCode:
.byte 0
RomHeaderDeviceType:
.byte 0
RomHeaderReserved1:
.rept 7
.byte 0
.endr
RomHeaderSoftwareVersion:
.byte 0
RomHeaderChecksum:
.byte 0x72
RomHeaderReserved2:
.rept 2
.byte 0
.endr

4
build.bat Normal file
View File

@ -0,0 +1,4 @@
as -mcpu=arm7tdmi asm/emerald.s -Iasm
ld -T ld_script.txt -o emerald.o a.out
objcopy -O binary emerald.o emerald.gba
fc /b base_emerald.gba emerald.gba

6
ld_script.txt Normal file
View File

@ -0,0 +1,6 @@
ENTRY(Start)
SECTIONS {
. = 0x8000000;
.text : { *(.text); }
}