Skip to content

Commit

Permalink
Integrated Normmatts open source bios, such that for most games an of…
Browse files Browse the repository at this point in the history
…ficial bios is not needed anymore
  • Loading branch information
Gericom committed Aug 9, 2020
1 parent c417fd0 commit c77a37c
Show file tree
Hide file tree
Showing 14 changed files with 1,879 additions and 24 deletions.
2 changes: 1 addition & 1 deletion arm9/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include $(DEVKITARM)/ds_rules
# all directories are relative to this makefile
#---------------------------------------------------------------------------------
BUILD := build
SOURCES := source source/emu source/gui source/gui/core source/fat source/save
SOURCES := source source/emu source/gui source/gui/core source/fat source/save source/bios
INCLUDES := include source
DATA := data

Expand Down
7 changes: 6 additions & 1 deletion arm9/ds_arm9_gbarunner.ld
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ __secure_area__ = .;

.vram __vram_start : AT (__vram_lma)
{
bios.o
__bios_start = .;
bios.o(.vram)
bios_divide.o(.vram)
bios_reset.o(.vram)
bios_swi.vram.o(.text)
. = . + 0x4000 - (. - __bios_start);
*(.vram)
*vram.*(.text)
*vram.*(.text.*)
Expand Down
13 changes: 0 additions & 13 deletions arm9/source/bios.s

This file was deleted.

File renamed without changes.
140 changes: 140 additions & 0 deletions arm9/source/bios/bios.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
.section .vram

.global gGbaBios
gGbaBios:

@ SWI calling convention:
@ Parameters are passed in via r0 - r3
@ Called SWI can modify r0 - r3 (and return things here), r12, and r14.
@ They can't modify anything else.
@---------------------------------------------------------------------------------
swi_vector:
@---------------------------------------------------------------------------------
@ Save these as temporaries
stmdb sp!, { r11, r12, lr }

@ Load comment from SWI instruction, which indicates which SWI
@ to use.
ldrb r12, [lr,#-2]
adr r11, swi_branch_table
ldr r12, [r11,r12,lsl#2]

@ get SPSR and enter system mode, interrupts on
MRS R11, SPSR
@ This must be stacked and not just saved, because otherwise SWI won't
@ be reentrant, which can happen if you're waiting for interrupts and the
@ interrupt handler triggers the SWI.
stmfd sp!, {r11}

@ Set up new CPSR value
and r11, r11, #0x80
orr r11, r11, #0x1f
msr cpsr_cf, r11

@ We have to now save system-mode lr register as well
stmfd sp!, {r2, lr}

@ Set return address
adr lr, swi_complete
@ Branch to SWI handler
bx r12

swi_complete:
@ Restore system mode lr
ldmfd sp!, {r2, lr}

@ Go back to supervisor mode to get back to that stack
mov r12, #0xD3
msr cpsr_cf, r12

@ SPSR has to be restored because the transition to system mode broke it
ldmfd sp!, {r11}
msr spsr_cf, r11

//set the right protected bios opcode
ldr r11,= gBiosOp
ldr r12,= 0xE3A02004
str r12, [r11]

@ Restore stuff we saved
ldmfd sp!, {r11,r12,lr}

@ Return from exception handler
movs pc, lr

@---------------------------------------------------------------------------------
swi_branch_table:
@---------------------------------------------------------------------------------
.word swi_SoftReset @ 0x00_SoftReset
.word swi_RegisterRamReset @ 0x01_RegisterRAMReset
.word swi_Halt @ 0x02_Halt
.word swi_Stop @ 0x03_Stop
.word swi_IntrWait @ 0x04_IntrWait
.word swi_VBlankIntrWait @ 0x05_VBlankIntrWait
.word swi_Div @ 0x06_Div
.word swi_DivARM @ 0x07_DivARM
.word swi_Sqrt @ 0x08_Sqrt
.word swi_ArcTan @ 0x09_ArcTan
.word swi_ArcTan2 @ 0x0A_ArcTan2
.word swi_CpuSet @ 0x0B_CPUSet
.word swi_CpuFastSet @ 0x0C_CPUFastSet
.word swi_GetBiosChecksum @ 0x0D_GetBiosChecksum
.word swi_BgAffineSet @ 0x0E_BgAffineSet
.word swi_ObjAffineSet @ 0x0F_ObjAffineSet
.word swi_BitUnPack @ 0x10_BitUnPack
.word swi_LZ77UnCompWram @ 0x11_LZ77UnCompWram
.word swi_LZ77UnCompVram @ 0x12_LZ77UnCompVram
.word swi_HuffUnComp @ 0x13_HuffUnComp
.word swi_RLUnCompWram @ 0x14_RLUnCompWram
.word swi_RLUnCompVram @ 0x15_RLUnCompVram
.word swi_Diff8bitUnFilterWram @ 0x16_Diff8bitUnFilterWram
.word swi_Diff8bitUnFilterVram @ 0x17_Diff8bitUnFilterVram
.word swi_Diff16bitUnFilter @ 0x18_Diff16bitUnFilter
.word swi_Invalid @ 0x19_SoundBiasChange
.word swi_Invalid @ 0x1A_SoundDriverInit
.word swi_Invalid @ 0x1B_SoundDriverMode
.word swi_Invalid @ 0x1C_SoundDriverMain
@ .word swi_SoundDriverMain @ 0x1C_SoundDriverMain
.word swi_Invalid @ 0x1D_SoundDriverVSync
.word swi_Invalid @ 0x1E_SoundChannelClear
.word swi_MidiKey2Freq @ 0x1F_MidiKey2Freq
.word swi_Invalid @ 0x20_MusicPlayerOpen
.word swi_Invalid @ 0x21_MusicPlayerStart
.word swi_Invalid @ 0x22_MusicPlayerStop
.word swi_MusicPlayerContinue @ 0x23_MusicPlayerContinue
.word swi_MusicPlayerFadeOut @ 0x24_MusicPlayerFadeOut
.word swi_Invalid @ 0x25_MultiBoot
.word reset_vector @ 0x26_HardReset
.word swi_CustomHalt @ 0x27_CustomHalt
.word swi_Invalid @ 0x28_SoundDriverVSyncOff
.word swi_Invalid @ 0x29_SoundDriverVSyncOn
.word swi_SoundGetJumpList @ 0x2A_SoundGetJumpList

@---------------------------------------------------------------------------------
.global swi_Invalid
.type swi_Invalid STT_FUNC
swi_Invalid:
@---------------------------------------------------------------------------------
bx lr
@---------------------------------------------------------------------------------

@---------------------------------------------------------------------------------
reset_vector: @This isn't required if not booting from bios
@---------------------------------------------------------------------------------
mov r0, #0xDF
msr cpsr_cf, r0

@Disable Interrupts IME=0
mov r3, #0x04000000
strb r3, [r3,#0x208]

@Setup stacks
bl bios_init

mov r2, #1
strb r2, [r3,#0x208]

b swi_SoftReset
@ ldr r0, =DrawLogo
@ ldr lr, =swi_SoftReset
@ bx r0
8 changes: 7 additions & 1 deletion arm9/source/bios.vram.cpp → arm9/source/bios/bios.vram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ static void applyRelocation()
//*(vu32*)0x01000000 = 0x00000000;// 0xEAFFFFFE; //b .
//*(vu32*)0x01000008 = 0xEAFFFFFE;
*(vu32*)0x01000008 = 0xE3A0F51A; //mov pc, #bios_swiVeneer
*(vu32*)0x06800000 = pcu_makeArmBranch(0x06800000, (u32)&gGbaBios[0x140 >> 2]);
}

/**
Expand Down Expand Up @@ -129,8 +130,13 @@ BiosLoadResult bios_load()
result = f_open(&vram_cd->fil, "0:/gba/bios.bin", FA_OPEN_EXISTING | FA_READ);
if (result != FR_OK)
result = f_open(&vram_cd->fil, "0:/_gba/bios.bin", FA_OPEN_EXISTING | FA_READ);
// if (result != FR_OK)
// return BIOS_LOAD_RESULT_NOT_FOUND;
if (result != FR_OK)
return BIOS_LOAD_RESULT_NOT_FOUND;
{
*(vu32*)0x01000008 = 0xE3A0F51A; //mov pc, #bios_swiVeneer
return BIOS_LOAD_RESULT_OK;
}
if (vram_cd->fil.obj.objsize != BIOS_SIZE)
{
f_close(&vram_cd->fil);
Expand Down
77 changes: 77 additions & 0 deletions arm9/source/bios/bios_divide.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.section .vram

@---------------------------------------------------------------------------------
@ Setup some nicer names
@---------------------------------------------------------------------------------
numerator .req r0
denominator .req r1
accumulator .req r2
current_bit .req r3

numerator_signed .req r12
denominator_signed .req r3

sign_flip .req r12

result .req r0
remainder .req r1
result_abs .req r3

temp .req r3

@---------------------------------------------------------------------------------
.global swi_DivARM
.type swi_DivARM STT_FUNC
swi_DivARM:
@---------------------------------------------------------------------------------
mov temp, numerator
mov numerator, denominator
mov denominator, temp
b swi_Div

@---------------------------------------------------------------------------------
.global swi_Div
.type swi_Div STT_FUNC
swi_Div:
@ See http://www.tofla.iconbar.com/tofla/arm/arm02/index.htm for more information
@---------------------------------------------------------------------------------
@ Set if numerator is signed, and abs numerator
ands numerator_signed, numerator, #0x80000000
rsbmi numerator, numerator, #0

// Same with denominator
ands denominator_signed, denominator, #0x80000000
rsbmi denominator, denominator, #0

// Gets set if sign(numerator) != sign(denominator)
eor sign_flip, numerator_signed, denominator_signed

mov accumulator, #0
mov current_bit, #1

// This moves out the current bit to the MSB of the denominator,
// and aligns the denominator up to the same bit-length as the
// numerator
0:
cmp denominator, numerator
movls denominator, denominator, lsl #1
movls current_bit, current_bit, lsl #1
bls 0b

// Basically the grade-school algorithm, for unsigned integers in binary
1:
cmp numerator, denominator
subcs numerator, numerator, denominator
orrcs accumulator, accumulator, current_bit
movs current_bit, current_bit, lsr #1
movcc denominator, denominator, lsr #1
bcc 1b

mov remainder, numerator
mov result_abs, accumulator
mov result, accumulator

tst sign_flip, #0x80000000
rsbmi result, result, #0

bx lr
File renamed without changes.
70 changes: 70 additions & 0 deletions arm9/source/bios/bios_reset.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
.section .vram

.arm
@---------------------------------------------------------------------------------
.global swi_SoftReset
.type swi_SoftReset STT_FUNC
swi_SoftReset:
@---------------------------------------------------------------------------------
mov r3, #0x04000000
@ Read flag from 0x3007FFA
@ 00h=8000000h (ROM), or 01h-FFh=2000000h (RAM)
@ This must be done before init because it gets cleared in init
ldrb r2, [r3,#-6]
bl bios_init
cmp r2, #0
ldmdb r3, {r0-r12}
movne lr, #0x02000000
moveq lr, #0x08000000
mov r0, #0x1F
msr cpsr_cf, r0

//set the right protected bios opcode
ldr r0,= gBiosOp
ldr r1,= 0xE129F000
str r1, [r0]
mov r0, #0
mov r1, #0

bx lr

@---------------------------------------------------------------------------------
.global bios_init
bios_init:
@ Requires r3 to be set to 0x04000000 already.
@---------------------------------------------------------------------------------
@ Reset the stack locations
mov r0, #0xD3
msr cpsr_cf, r0
ldr sp, Cpu_Stack_SVC
mov lr, #0
msr spsr_cf, lr
mov r0, #0xD2
msr cpsr_cf, r0
ldr sp, Cpu_Stack_IRQ
mov lr, #0
msr spsr_cf, lr
mov r0, #0x5F
msr cpsr_cf, r0
ldr sp, Cpu_Stack_USR

movs r0, #0
subs r1, r0, #0x200

@ Clear top 0x200 bytes of IWRAM 03007E00 -> 03008000

init_loop:
str r0, [r3,r1]
adds r1, r1, #4
blt init_loop

bx lr

@---------------------------------------------------------------------------------
@---------------------------------------------------------------------------------

Cpu_Stack_USR: .word 0x03007F00
Cpu_Stack_IRQ: .word 0x03007FA0
Cpu_Stack_SVC: .word 0x03007FE0

@---------------------------------------------------------------------------------
Loading

0 comments on commit c77a37c

Please sign in to comment.