diff --git a/aip.lds b/aip.lds new file mode 100644 index 0000000..d24d93c --- /dev/null +++ b/aip.lds @@ -0,0 +1,28 @@ +OUTPUT_FORMAT("elf32-shl", "elf32-shl", "elf32-shl") + +MEMORY +{ + systemid (arx) : ORIGIN = 0x8c008000, LENGTH = 0x100 + toc (arx) : ORIGIN = 0x8c008100, LENGTH = 0x200 + sg_sec (arx) : ORIGIN = 0x8c008300, LENGTH = 0x3400 + sg_are (arx) : ORIGIN = 0x8c00b700, LENGTH = 0x100 + sg_ini (arx) : ORIGIN = 0x8c00b800, LENGTH = 0x2800 + aip (arx) : ORIGIN = 0x8c00e000, LENGTH = 0x2000 +} + +SECTIONS +{ + .text.aip : + { + KEEP(*(.text.*aip)) + . = ALIGN(8192); + } > aip + + /DISCARD/ : + { + *(.text) + *(.data) + *(.bss) + *(.comment) + } +} diff --git a/aip.s b/aip.s new file mode 100644 index 0000000..a64e679 --- /dev/null +++ b/aip.s @@ -0,0 +1,207 @@ + .section .text.aip + .global _text_aip_start + +_text_aip_start: + bra label_10 + nop + +label_4: + .long 0xac010000 +label_8: + .long 0x00000000 +label_c: + .long 0xac00e100 + +label_10: + mov.l label_1c,r0 /* ac00e020 */ + jsr @r0 + nop +label_16: + bra label_16 + nop + nop +label_1c: + .long 0xac00e020 + +/* called from label_10 */ +label_20: + mov.l label_40,r2 /* ac00e0dc */ + mov.l label_3c,r3 /* a0000000 */ + or r3,r2 + jsr @r2 /* jump to label_dc */ + nop + stc sr,r0 + mov.w label_38,r1 /* ff0f */ + and r1,r0 + or #0xf0,r0 /* set IMASK ; disables interrupts */ + ldc r0,sr + bra label_44 + nop +label_38: + .word 0xff0f +label_3a: + .word 0x0000 +label_3c: + .long 0xa0000000 +label_40: + .long 0xac00e0dc + +label_44: + mov.l label_4c,r0 /* ac00f400 → new stack address */ + mov r0,r15 + bra label_50 + nop + +label_4c: + .long 0xac00f400 + +label_50: + mov.l label_70,r2 /* ac00e0d0 */ + mov.l label_6c,r3 /* a0000000 */ + or r3,r2 + jsr @r2 /* jump to label_d0 */ + nop +label_5a: + mov.l label_78,r3 /* ac00e13c */ + mov.l label_74,r4 /* ac00fc00 */ + mov.w label_68,r6 /* 400 */ + jsr @r3 /* jump to label_13c */ + mov #0,r5 + bra label_7c + nop + +label_68: + .word 0x0400 +label_6a: + .word 0x0000 +label_6c: + .long 0xa0000000 +label_70: + .long 0xac00e0d0 +label_74: + .long 0xac00fc00 +label_78: + .long 0xac00e13c + +label_7c: + mov.l label_c4,r0 /* 700000f0 */ + ldc r0,sr + sub r0,r0 + mov r0,r1 + mov r0,r2 + mov r0,r3 + mov r0,r4 + mov r0,r5 + mov r0,r6 + mov r0,r7 + mov r0,r8 + mov r0,r9 + mov r0,r10 + mov r0,r11 + mov r0,r12 + mov r0,r13 + mov r0,r14 + mov.l label_bc,r0 /* 8c00f400 */ + mov r0,r15 + mov.l label_c8,r0 /* 8c00f400 */ + ldc r0,vbr + mov.l label_c0,r0 /* 40000 */ + lds r0,fpscr + mov.l label_b8,r0 /* ac00e004 */ + mov.l @r0,r0 /* ac010000 */ + jsr @r0 + mov r1,r0 +label_b2: + bra label_b2 + nop + nop + +label_b8: + .long 0xac00e004 +label_bc: + .long 0x8c00f400 +label_c0: + .long 0x00040000 +label_c4: + .long 0x700000f0 +label_c8: + .long 0x8c00f400 + + /* unreachable label_cc ? */ +label_cc: + bra label_cc + nop + /* called from label_50 */ +label_d0: + mov.l label_d8,r2 /* ff00001c (CCN.CCR) */ + mov #0,r3 /* disable all caches */ + rts + mov.l r3,@r2 + +label_d8: + .long 0xff00001c + + /* called from label_20 */ +label_dc: + mov.l label_f4,r0 /* ff00001c (CCN.CCR) */ + mov.l @r0,r1 + mov.l label_f8,r2 /* 89af → mask off undefined bits */ + and r2,r1 + mov.w label_f0,r2 /* 800 → ICI , instruction cache invalidation */ + or r2,r1 + mov.l r1,@r0 + bra label_fc + nop + nop + +label_f0: + .word 0x0800 + nop /* unreachable nop? */ +label_f4: + .long 0xff00001c +label_f8: + .word 0x89af +label_fa: + .word 0x0000 + +label_fc: + rts + nop + + mov.l label_108,r0 /* 8c0000e0 */ + mov.l @r0,r1 + jmp @r1 + mov #1,r4 +label_108: + .long 0x8c0000e0 +label_10c: + .long 0xac00e110 + + .ascii "\nAIP Ver 1.07 Build:Apr 27 1999 14:00:30\n" + .byte 0x00 + .byte 0x00 + .byte 0x00 + /* called from label_5a */ + /* + memset(char * dest, char value, unsigned int length) +dest : r4 +value : r5 +length : r6 +[return]: original value of `dest` as r0 + */ +label_13c: + mov #0,r7 + mov r7,r3 + cmp/hs r6,r3 /* r3 ≥ r6 (unsigned) */ + /* 0 ≥ 0x400 */ + bt/s label_150 + mov r4,r0 /* ac00fc00 → r0 */ +label_146: + add #1,r7 + mov.b r5,@r0 /* 0 → @(ac00fc00) */ + cmp/hs r6,r7 /* r7 ≥ 0x400 */ + bf/s label_146 + add #1,r0 +label_150: + rts + mov r4,r0 diff --git a/sg_ini.lds b/sg_ini.lds new file mode 100644 index 0000000..4782220 --- /dev/null +++ b/sg_ini.lds @@ -0,0 +1,28 @@ +OUTPUT_FORMAT("elf32-shl", "elf32-shl", "elf32-shl") + +MEMORY +{ + systemid (arx) : ORIGIN = 0x8c008000, LENGTH = 0x100 + toc (arx) : ORIGIN = 0x8c008100, LENGTH = 0x200 + sg_sec (arx) : ORIGIN = 0x8c008300, LENGTH = 0x3400 + sg_are (arx) : ORIGIN = 0x8c00b700, LENGTH = 0x100 + sg_ini (arx) : ORIGIN = 0x8c00b800, LENGTH = 0x2800 + aip (arx) : ORIGIN = 0x8c00e000, LENGTH = 0x2000 +} + +SECTIONS +{ + .text.sg_ini : + { + KEEP(*(.text.*sg_ini)) + . = 0x2800; + } > sg_ini + + /DISCARD/ : + { + *(.text) + *(.data) + *(.bss) + *(.comment) + } +} diff --git a/sg_ini.s b/sg_ini.s new file mode 100644 index 0000000..afc84c4 --- /dev/null +++ b/sg_ini.s @@ -0,0 +1,576 @@ + .section .text.sg_ini + .global _text_sg_ini_start + +_text_sg_ini_start: + mov.l label_18,r0 /* 8d000000 → beginning of system ram, image area */ + mov r0,r15 + nop + nop + mov.l label_1c,r0 /* 8c00d820 → label_2020 */ + jmp @r0 + nop + nop + nop + nop + nop + nop + +label_18: + .long 0x8d000000 +label_1c: + .long 0x8c00d820 +label_20: + .fill 0x2000,1,0x00 + +label_2020: + mov.l label_204c,r0 /* 8c00d940 → label_2140 */ + mov #0,r1 + jsr @r0 + nop + mov.l label_2050,r0 /* 8c00d900 → label_2100 */ + jsr @r0 + nop + mov.l label_2054,r0 /* 8c00d888 → label_2088 */ + jsr @r0 + nop + mov.l label_2058,r0 /* 8c00dae0 → label_22e0 */ + jsr @r0 + nop + mov.l label_205c,r0 /* 8c00db40 → label_2340 */ + jsr @r0 + nop + nop + mov.l label_2060,r0 /* 8c00d86c → label_206c */ + jmp @r0 + nop + nop + nop + +label_204c: + .long 0x8c00d940 +label_2050: + .long 0x8c00d900 +label_2054: + .long 0x8c00d888 +label_2058: + .long 0x8c00dae0 +label_205c: + .long 0x8c00db40 +label_2060: + .long 0x8c00d86c + + .ascii "INI90405" + +label_206c: + nop + nop + nop + nop + nop + nop + nop + nop + nop + mov.l label_2084,r0 /* 8c00e000 */ + jmp @r0 + nop +label_2084: + .long 0x8c00e000 /* aip */ + +label_2088: + sts.l pr,@-r15 + mov.l label_20a4,r0 /* 8c000048 */ + mov.l @r0,r0 + cmp/eq #6,r0 + bt label_209e + cmp/eq #7,r0 + bt label_209e + cmp/eq #9,r0 + bt label_209e + rts + lds.l @r15+,pr +label_209e: + mov.l label_20a8,r1 /* 8c00d8ac → label_20ac */ + jmp @r1 + nop +label_20a4: + .long 0x8c000048 +label_20a8: + .long 0x8c00d8ac + +label_20ac: + mov.l label_20b4,r0 /* 8c0000e0 */ + mov.l @r0,r0 + jmp @r0 + mov #1,r4 +label_20b4: + .long 0x8c0000e0 + +label_20b8: + exts.b r4,r0 + mov #57,r1 + cmp/gt r1,r0 + bf label_20c2 + add #-7,r0 +label_20c2: + rts + add #-48,r0 +label_20c6: + xor r2,r2 + sts.l pr,@-r15 + mov.b @r4,r4 + mov r4,r0 + cmp/eq #32,r0 + bt label_20e0 + bsr label_20b8 + nop + mov #1,r1 + tst r1,r0 + bt.s label_20e0 + xor r2,r2 + mov #1,r2 +label_20e0: + lds.l @r15+,pr + rts + mov r2,r0 + .word 0xffff +label_20e8: + mov.l label_20fc,r1 ! 8c008024 + mov.l label_20f8,r3 ! 4c494d20 + mov.l @r1,r2 + cmp/eq r3,r2 + movt r0 + rts + nop + nop +label_20f8: + .long 0x4c494d20 +label_20fc: + .long 0x8c008024 +label_2100: + sts.l pr,@-r15 + mov.l label_212c,r3 /* 8c00d8c6 → label_20c6 */ + mov.l label_213c,r4 ! 8c008000 + jsr @r3 + add #62,r4 + tst r0,r0 + bf label_2126 + mov.l label_2130,r3 /* 8c00d8e8 → label_20e8 */ + jsr @r3 + nop + tst r0,r0 + bf label_2126 + mov.l label_2138,r3 ! 8c010000 + mov.w @r3,r0 + cmp/eq #34,r0 + bf label_2126 + mov.l label_2134,r1 /* 8c00d8ac → label_20ac */ + jmp @r1 + lds.l @r15+,pr +label_2126: + lds.l @r15+,pr + rts + nop +label_212c: + .long 0x8c00d8c6 +label_2130: + .long 0x8c00d8e8 +label_2134: + .long 0x8c00d8ac +label_2138: + .long 0x8c010000 +label_213c: + .long 0x8c008000 + +label_2140: + mov.l r2,@-r15 + mov.l r3,@-r15 + mov.l r4,@-r15 + mov.l r5,@-r15 + sts.l pr,@-r15 + mova label_2284,r0 + mov.w @(r0,r1),r1 + add r1,r0 +label_2150: + add #3,r0 + shlr2 r0 + shll2 r0 + mov.w @r0+,r4 + tst r4,r4 + bf label_2172 + mov.w @r0+,r4 + tst r4,r4 + bf.s label_2150 + add r4,r0 + lds.l @r15+,pr + mov.l @r15+,r5 + mov.l @r15+,r4 + mov.l @r15+,r3 + mov.l @r15+,r2 + rts + sett +label_2172: + mov.w @r0+,r3 + mov.l label_2278,r1 ! ffff8000 + and r3,r1 + tst r1,r1 + bt label_2180 + xor r1,r3 + bf label_2182 +label_2180: + mov.l @r0+,r2 +label_2182: + mov.l label_227c,r1 ! 6000 + and r3,r1 + tst r1,r1 + bt label_21a0 + xor r1,r3 + add #12,r3 + mov.l @r0+,r1 + mov r0,r5 + add r1,r0 +label_2194: + bsrf r3 + dt r4 + bf.s label_2194 + add #4,r2 + bra label_2150 + mov r5,r0 +label_21a0: + bsrf r3 + dt r4 + bf.s label_21a0 + add #4,r2 + bt label_2150 + mov #0,r1 + rts + mov.l r1,@r2 + mov #0,r1 + rts + mov.w r1,@r2 + mov #0,r1 + rts + mov.b r1,@r2 + mov.l @r0+,r1 + rts + mov.l r1,@r2 + mov.w @r0+,r1 + rts + mov.l r1,@r2 + mov.l @r0+,r1 + rts + mov.b r1,@r2 + mov #-1,r1 + rts + mov.l r1,@r2 + mov.w @r0+,r1 + rts + mov.w r1,@r2 + mov.l @r0+,r1 + rts + mov.w r1,@r2 + nop + rts + mov.l @r2,r1 + bra label_2228 + nop + nop + xor r1,r1 + sts.l pr,@-r15 + mov.l r3,@-r15 +label_21f2: + mov #8,r3 +label_21f4: + mov.l r1,@r2 + dt r3 + bf.s label_21f4 + add #4,r2 + bsr label_2252 + nop + dt r4 + bf label_21f2 + add #1,r4 + add #-4,r2 + mov.l @r15+,r3 + lds.l @r15+,pr + rts + sett + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop +label_2228: + sts.l pr,@-r15 + mov.l r0,@-r15 + mov.l r1,@-r15 + mov.l r2,@-r15 + mov.l r3,@-r15 + mov.l r4,@-r15 + mov.l r5,@-r15 + mov.l r6,@-r15 + jsr @r2 + mov.l r7,@-r15 + mov.l @r15+,r7 + mov.l @r15+,r6 + mov.l @r15+,r5 + mov.l @r15+,r4 + mov.l @r15+,r3 + mov.l @r15+,r2 + mov.l @r15+,r1 + mov.l @r15+,r0 + lds.l @r15+,pr + rts + sett +label_2252: + mov.l r0,@-r15 + mov.l r1,@-r15 + mov.l r2,@-r15 + mov.l label_2270,r1 ! a05f688c + mov.l label_2274,r2 ! 1800 +label_225c: + dt r2 + bt label_2266 + mov.l @r1,r0 + tst #1,r0 + bf label_225c +label_2266: + mov.l @r15+,r2 + mov.l @r15+,r1 + rts + mov.l @r15+,r0 + nop +label_2270: + .long 0xa05f688c +label_2274: + .long 0x00001800 + +label_2278: + .long 0xffff8000 +label_227c: + .long 0x00006000 + + /* unreachable? */ + .word 0x8040 + .word 0xa05f +label_2284: + .word 0x0002 + .word 0x0009 + .word 0x0002 + .word 0x0006 + .word 0x0038 + .word 0xff00 + .word 0x0001 + .word 0x0018 + .word 0x002c + .word 0xffa0 + .word 0x12c0 + .word 0x0000 + .word 0x0004 + .word 0x000c + .word 0x0000 + .word 0xffd0 + .word 0x0001 + .word 0x0018 + .word 0x0024 + .word 0xff00 + .word 0x0020 + .word 0x0000 + .word 0x0002 + .word 0x001e + .word 0x7490 + .word 0xa05f + .word 0x0222 + .word 0x0222 + .word 0x0002 + .word 0x001e + .word 0x74a0 + .word 0xa05f + .word 0x2001 + .word 0x2001 + .word 0x0002 + .word 0x001e + .word 0x7890 + .word 0xa05f + .word 0x001b + .word 0x0271 + .word 0x0000 + .word 0x0000 + nop + nop + nop + nop + +label_22e0: + sts.l pr,@-r15 + mov.l label_2330,r4 ! 8c008000 + mov.l label_2328,r3 ! 8c00d8c6 + jsr @r3 + add #62,r4 + tst r0,r0 + bt label_2320 + mov.l label_2334,r1 ! 8ce01010 + mov.l @r1,r0 + tst r0,r0 + bt label_231a + mov.l label_233c,r2 ! a8 + cmp/hi r2,r0 + bt label_231a + dt r0 + mov r0,r2 + shll r2 + add r0,r2 + shll2 r2 + add r2,r1 + add #12,r1 + mov.l @r1,r2 + add #4,r1 + mov.l @r1,r3 + add r3,r2 + mov.l label_2338,r0 ! a05f74f4 + mov.l @r0,r3 + cmp/eq r3,r2 + bt label_2320 +label_231a: + mov.l label_232c,r1 ! 8c00d8ac + jmp @r1 + lds.l @r15+,pr +label_2320: + lds.l @r15+,pr + rts + nop + nop +label_2328: + .long 0x8c00d8c6 +label_232c: + .long 0x8c00d8ac +label_2330: + .long 0x8c008000 +label_2334: + .long 0x8ce01010 +label_2338: + .long 0xa05f74f4 +label_233c: + .long 0x000000a8 + +label_2340: + mov.l r14,@-r15 + mov #-1,r14 + sts.l pr,@-r15 + add #-20,r15 + mov r15,r3 + mov r15,r5 + mov.l r3,@(16,r15) + add #16,r5 + bsr label_23e0 + mov #30,r4 + mov r0,r4 + tst r4,r4 + bt label_2384 + bsr label_2396 + nop + tst r0,r0 + bf label_2384 + mov.l @(4,r15),r4 + mov.l label_23d4,r3 ! ffff + cmp/hi r3,r4 + bt label_2384 + mov r15,r5 + mov.w label_23d0,r1 ! e10 + mov.l r1,@(4,r15) + bsr label_23e0 + mov #31,r4 + mov r0,r4 + tst r4,r4 + bt label_2384 + bsr label_2396 + nop + tst r0,r0 + bf label_2384 + mov #0,r14 +label_2384: + tst r14,r14 + bt label_238e + mov.l label_23d8,r2 ! 8c00d8ac + jsr @r2 + nop +label_238e: + add #20,r15 + lds.l @r15+,pr + rts + mov.l @r15+,r14 +label_2396: + mov.l r14,@-r15 + mov.l r13,@-r15 + sts.l pr,@-r15 + mov #0,r14 + add #-16,r15 + mov r4,r13 +label_23a2: + bsr label_23f8 + nop + mov r15,r5 + bsr label_23ec + mov r13,r4 + cmp/eq #-1,r0 + bt label_23c4 + cmp/eq #1,r0 + bt label_23bc + cmp/eq #2,r0 + bf label_23c4 + bra label_23c6 + mov #0,r0 +label_23bc: + mov.l label_23dc,r4 ! 10000 + add #1,r14 + cmp/ge r4,r14 + bf label_23a2 +label_23c4: + mov #-1,r0 +label_23c6: + add #16,r15 + lds.l @r15+,pr + mov.l @r15+,r13 + rts + mov.l @r15+,r14 +label_23d0: + .word 0x0e10 + .word 0xffff +label_23d4: + .long 0x0000ffff +label_23d8: + .long 0x8c00d8ac +label_23dc: + .long 0x00010000 + +label_23e0: + mov #0,r6 + mov #0,r7 + mov.l label_2404,r0 ! 8c0000bc + mov.l @r0,r0 +label_23e8: + jmp @r0 + nop +label_23ec: + mov #0,r6 + mov #1,r7 + mov.l label_2404,r0 ! 8c0000bc + mov.l @r0,r0 + jmp @r0 + nop +label_23f8: + mov #0,r6 + mov #2,r7 + mov.l label_2404,r0 ! 8c0000bc + mov.l @r0,r0 + jmp @r0 + nop +label_2404: + .long 0x8c0000bc diff --git a/test_compile_ip_fragments.sh b/test_compile_ip_fragments.sh new file mode 100644 index 0000000..e425f1a --- /dev/null +++ b/test_compile_ip_fragments.sh @@ -0,0 +1,15 @@ +set -eux + +sh4-none-elf-as -g --isa=sh4 --little sg_ini.s -o sg_ini.o +sh4-none-elf-ld -T sg_ini.lds sg_ini.o -o sg_ini.elf +sh4-none-elf-objcopy -O binary sg_ini.elf sg_ini.bin +xxd sg_ini.bin sg_ini.bin.txt +sh4-none-elf-objcopy -O binary sg/sg_ini.obj sg/sg_ini.bin +xxd sg/sg_ini.bin sg/sg_ini.bin.txt + +sh4-none-elf-as -g --isa=sh4 --little aip.s -o aip.o +sh4-none-elf-ld -T aip.lds aip.o -o aip.elf +sh4-none-elf-objcopy -O binary aip.elf aip.bin +xxd aip.bin aip.bin.txt +sh4-none-elf-objcopy -O binary sg/aip.obj sg/aip.bin +xxd sg/aip.bin sg/aip.bin.txt