From ed9adf1388d64172cae25dfd4a7d80c9942d9960 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Mon, 31 Oct 2022 05:17:09 +0000 Subject: [PATCH] Figured out most of `start`. --- asm/dw2003/exe/f2.s | 2 +- asm/dw2003/exe/fns.s | 66 ++++++++------------------------ asm/dw2003/exe/start.s | 61 +++++++++++++++++++++++++++++ elf/dw2003/exe.yaml | 5 ++- rust/dw2003_exe_data/src/data.rs | 7 ++-- 5 files changed, 86 insertions(+), 55 deletions(-) create mode 100644 asm/dw2003/exe/start.s diff --git a/asm/dw2003/exe/f2.s b/asm/dw2003/exe/f2.s index 4e366850f..fc8b77017 100644 --- a/asm/dw2003/exe/f2.s +++ b/asm/dw2003/exe/f2.s @@ -28,4 +28,4 @@ var_b=$s1 lw $s1, 20($sp) lw $s0, 16($sp) jr $ra - addiu $sp, 32 \ No newline at end of file + addiu $sp, 32 diff --git a/asm/dw2003/exe/fns.s b/asm/dw2003/exe/fns.s index ea2887f54..5565566a4 100644 --- a/asm/dw2003/exe/fns.s +++ b/asm/dw2003/exe/fns.s @@ -1,6 +1,6 @@ .include "macros.s" -.section "fns0" +.section "fns00" .L80010000: .word .L80011134 .L80010004: .word .L8001114c .L80010008: .word .L80011224 @@ -50,7 +50,7 @@ .L800100b8: .word .L80013258 .L800100bc: .word .L800132e0 .L800100c0: .word .L80013350 -.L800100c4: .word D0x80082cb0 +.L800100c4: .word HEAP .L800100c8: .word D0x800a5de0 .L800100cc: .word 0x4c534942 .L800100d0: .word 0x302d5350 @@ -1033,50 +1033,8 @@ D0x80010c06: .L80010e84: .word 0x0000000a .L80010e88: jr $ra .L80010e8c: nop -.global start -start: -.L80010e90: la_ $v0, D0x8005cce8 -.L80010e98: la_ $v1, D0x80082cb0 -.L80010ea0: sw $zr, 0($v0) -.L80010ea4: addiu $v0, 4 -.L80010ea8: sltu $at, $v0, $v1 -.L80010eac: bnez $at, .L80010ea0 -.L80010eb0: nop -.L80010eb4: li $v0, 4 -.L80010eb8: nop -.L80010ebc: nop -.L80010ec0: nop -.L80010ec4: nop -.L80010ec8: la_ $a0, .L80010f3c -.L80010ed0: addu $a0, $v0 -.L80010ed4: lw $v0, 0($a0) -.L80010ed8: lui $t0, 0x8000 -.L80010edc: or $sp, $v0, $t0 -.L80010ee0: la_ $a0, D0x80082cb0 -.L80010ee8: sll $a0, 0x3 -.L80010eec: srl $a0, 0x3 -.L80010ef0: lui $v1, 0x8006 -.L80010ef4: lw $v1, -16280($v1) -.L80010ef8: nop -.L80010efc: subu $a1, $v0, $v1 -.L80010f00: subu $a1, $a0 -.L80010f04: or $a0, $t0 -.L80010f08: lui $at, 0x8006 -.L80010f0c: sw $ra, -13080($at) -.L80010f10: la_ $gp, D0x8005cb50 -.L80010f18: move_ $s8, $sp -.L80010f1c: jal .L8002507c -.L80010f20: addi $a0, 4 -.L80010f24: lui $ra, 0x8006 -.L80010f28: lw $ra, -13080($ra) -.L80010f2c: nop -.L80010f30: jal .L80014524 -.L80010f34: nop -.L80010f38: break 0x0, 0x1 -.L80010f3c: .word 0x200000 -.L80010f40: .word 0x200000 -.L80010f44: .word 0x200000 -.L80010f48: .word 0x200000 + +.section "fns01" .L80010f4c: addiu $sp, -24 .L80010f50: sw $s0, 16($sp) .L80010f54: move_ $s0, $a0 @@ -4503,6 +4461,8 @@ F0x80014238: .L80014518: nop .L8001451c: jr $ra .L80014520: addiu $sp, 24 + +.global .L80014524 .L80014524: addiu $sp, -88 .L80014528: sw $ra, 84($sp) .L8001452c: sw $s2, 80($sp) @@ -21719,10 +21679,16 @@ F0x80024a80: .L80025070: addiu $sp, 48 .L80025074: nop .L80025078: nop -.L8002507c: li $t2, 160 -.L80025080: jr $t2 -.L80025084: li $t1, 57 -.L80025088: nop + +# fn(addr: *u32, size: u32) +# Calls `A(0x39)` +.global InitHeap +InitHeap: + li $t2, 0xa0 + jr $t2 + li $t1, 0x39 + nop + .L8002508c: li $a0, 1 .L80025090: syscall .L80025094: jr $ra diff --git a/asm/dw2003/exe/start.s b/asm/dw2003/exe/start.s new file mode 100644 index 000000000..af4b4bc99 --- /dev/null +++ b/asm/dw2003/exe/start.s @@ -0,0 +1,61 @@ +.include "macros.s" +.set at + +.section "section_start" +.global start +start: + +# Zero out `D0x8005cce8..HEAP` +cur_ptr=$v0 +end_ptr=$v1 + la_ cur_ptr, D0x8005cce8 + la_ end_ptr, HEAP +.Lzero_loop: + sw $zr, (cur_ptr) + addiu cur_ptr, 4 + bltu cur_ptr, end_ptr, .Lzero_loop + nop + +# Setup the stack pointer from `data[1]` adjusted to `KSEG0` + li $v0, 4 + nop + nop + nop + nop + la_ $a0, .Ldata + addu $a0, $v0 +stack_ptr_kuseg=$v0 + lw stack_ptr_kuseg, ($a0) + lui $t0, 0x8000 + or $sp, stack_ptr_kuseg, $t0 + +# Initialize the heap at `HEAP` until `$sp - HEAP_END_RESERVED` + la_ $a0, HEAP + sll $a0, 0x3 + srl $a0, 0x3 + lw $v1, HEAP_END_RESERVED + nop + subu $a1, stack_ptr_kuseg, $v1 + subu $a1, $a0 + or $a0, $t0 + sw $ra, 0x8005cce8 + la_ $gp, D0x8005cb50 + move_ $s8, $sp + jal InitHeap + addi $a0, 4 + lw $ra, 0x8005cce8 + nop + +# Enter the main loop + jal .L80014524 + nop + +# Then break if we get back from it + break 0x0, 0x1 + +# [u32; 4] +.Ldata: + .word 0x200000 + .word 0x200000 # Stack pointer (in KUSEG) + .word 0x200000 + .word 0x200000 diff --git a/elf/dw2003/exe.yaml b/elf/dw2003/exe.yaml index 08b5060e6..bf4deec54 100644 --- a/elf/dw2003/exe.yaml +++ b/elf/dw2003/exe.yaml @@ -1,6 +1,7 @@ --- entry: start objs: + - /build/asm/dw2003/exe/start.o - /build/asm/dw2003/exe/fns.o - /build/asm/dw2003/exe/f0.o - /build/asm/dw2003/exe/f1.o @@ -9,7 +10,9 @@ objs: - /build/rust/dw2003_exe_data.a - /build/rust/dw2003_exe_digimon_profiles.a sections: - - fns0 + - fns00 + - section_start + - fns01 - section_f2 - section_f1 - section_f0 diff --git a/rust/dw2003_exe_data/src/data.rs b/rust/dw2003_exe_data/src/data.rs index 37bde2963..75e3097cb 100644 --- a/rust/dw2003_exe_data/src/data.rs +++ b/rust/dw2003_exe_data/src/data.rs @@ -27812,7 +27812,7 @@ util::decl_static! { "dw2003_exe_data1", pub static mut D0x8005c05c: u32 = 0x00000000; pub static mut D0x8005c060: u32 = 0x00000000; pub static mut D0x8005c064: u32 = 0x00000000; - pub static mut D0x8005c068: u32 = 0x00008000; + pub static mut HEAP_END_RESERVED: u32 = 0x00008000; pub static mut D0x8005c06c: u32 = 0x00800000; pub static mut D0x8005c070: u32 = 0x00000000; pub static mut D0x8005c074: u32 = 0x00107350; @@ -67718,8 +67718,9 @@ util::decl_static! { "dw2003_exe_data1", pub static mut D0x80082ca4: u32 = 0x00000000; pub static mut D0x80082ca8: u32 = 0x00000000; pub static mut D0x80082cac: u32 = 0x00000000; - // - pub static mut D0x80082cb0: u32 = 0x00000000; + + // Heap + pub static mut HEAP: u32 = 0x00000000; pub static mut D0x80082cb4: u32 = 0x00000000; pub static mut D0x80082cb8: u32 = 0x00000000; pub static mut D0x80082cbc: u32 = 0x00000000;