From 1efbed236effa31fb2cc8e7dfdb18bac5d565428 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Wed, 4 Mar 2026 13:58:19 -0700 Subject: [PATCH 1/5] format unconditional jumps in crt0.S --- src/crt/crt0.S | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/crt/crt0.S b/src/crt/crt0.S index 3778ab511..8b3893342 100644 --- a/src/crt/crt0.S +++ b/src/crt/crt0.S @@ -96,6 +96,7 @@ __start: ldir ld a, ti.kClear jp ti.JForceCmd + .local ___app_start ___app_start: #else @@ -133,6 +134,7 @@ __start: #if HAS_LIBLOAD call ti.PushRealO1 ; save running program name jr .L.tryfind + .L.inram: call ti.Arc_Unarc .L.tryfind: @@ -151,6 +153,7 @@ __start: pop hl ; start of loader (required to be in hl) ld de, ___libload_libs ; start of relocation data jp (hl) ; jump to the loader -- it should take care of everything else + .L.notfound: call ti.PopRealO1 ; restore running program name call ti.ClrScrn @@ -218,6 +221,7 @@ ___libload_libs_ret: inc hl ld b, (hl) jr .L.parse_ans_next + .L.parse_ans_loop: inc hl dec bc @@ -296,6 +300,7 @@ ___libload_libs_ret: #if HAS_INIT_ARRAY ld hl, __init_array_start jr .L.init_array_start + .L.init_array_loop: push hl ld hl, (hl) @@ -345,6 +350,7 @@ _exit: #if HAS_ATEXIT ; input: (sp + 0) = exit status jr .L.exit_function_start + .L.exit_function_loop: ld hl, (ix + 1 + 0 * 3) ld (__atexit_functions), hl @@ -374,6 +380,7 @@ _exit: #if HAS_FINI_ARRAY ld hl, __fini_array_end jr .L.fini_array_start + .L.fini_array_loop: dec hl dec hl From 41fa42d92cb66ac6e45fd1bc6febb042334a1dbc Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Wed, 4 Mar 2026 14:30:54 -0700 Subject: [PATCH 2/5] optimized clock zeroize --- src/crt/crt0.S | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/crt/crt0.S b/src/crt/crt0.S index 8b3893342..e3e08fb23 100644 --- a/src/crt/crt0.S +++ b/src/crt/crt0.S @@ -288,11 +288,16 @@ ___libload_libs_ret: res ti.bTmr1Enable, (hl) set ti.bTmr1Crystal, (hl) res ti.bTmr1Overflow, (hl) - ld l, (ti.mpTmr1Counter + 1) & 0xFF - ld de, 0 ; Set the counter to zero - ld (hl), de - dec hl - ld (hl), e + + ; Set the counter to zero + .errif (ti.mpTmr1Counter & 0xFFFF) != 0 + ld l, h ; ld hl, ti.mpTmr1Counter + ld b, 4 +.L.zeroize_counter: + ld (hl), h + inc hl + djnz .L.zeroize_counter + ld l, ti.mpTmrCtrl & 0xFF set ti.bTmr1Enable, (hl) ; Enable the timer #endif From 00089569191ec496a56caa71051a26c5edd9bcbc Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Wed, 4 Mar 2026 15:23:58 -0700 Subject: [PATCH 3/5] use the pointer as the loop counter --- src/crt/crt0.S | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/crt/crt0.S b/src/crt/crt0.S index e3e08fb23..1664a385f 100644 --- a/src/crt/crt0.S +++ b/src/crt/crt0.S @@ -291,12 +291,11 @@ ___libload_libs_ret: ; Set the counter to zero .errif (ti.mpTmr1Counter & 0xFFFF) != 0 - ld l, h ; ld hl, ti.mpTmr1Counter - ld b, 4 + ld l, (ti.mpTmr1Counter + 4) & 0xFF .L.zeroize_counter: + dec l ld (hl), h - inc hl - djnz .L.zeroize_counter + jr nz, .L.zeroize_counter ld l, ti.mpTmrCtrl & 0xFF set ti.bTmr1Enable, (hl) ; Enable the timer From 66a45bcb713264a7e67f05c4daeed27165bd4348 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Thu, 5 Mar 2026 16:26:14 -0700 Subject: [PATCH 4/5] size opt exit_function_loop --- src/crt/crt0.S | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/crt/crt0.S b/src/crt/crt0.S index 1664a385f..8bc664cbb 100644 --- a/src/crt/crt0.S +++ b/src/crt/crt0.S @@ -351,21 +351,24 @@ _exit: ; exit status is currently at (sp + 3), so we need to fix that pop bc ; destroy return address #endif + #if HAS_ATEXIT ; input: (sp + 0) = exit status jr .L.exit_function_start .L.exit_function_loop: - ld hl, (ix + 1 + 0 * 3) + push hl + pop ix + ld hl, (hl) ; ld hl, (ix + 0 * 3) ld (__atexit_functions), hl pop hl ; exit status push hl ; exit status - ld de, (ix + 1 + 2 * 3) ; arg + ld de, (ix + 2 * 3) ; arg push de ; arg push hl ; exit status - ld hl, (ix + 1 + 1 * 3) ; func + ld hl, (ix + 1 * 3) ; func push hl ; func - pea ix + 1 + push ix call _free pop bc ; reset SP pop hl ; func @@ -375,12 +378,16 @@ _exit: pop bc ; reset SP pop bc ; reset SP .L.exit_function_start: - ld ix, (__atexit_functions) + ld hl, (__atexit_functions) ; NULL indicates no more atexit functions - ld bc, -1 - add ix, bc - jr c, .L.exit_function_loop + add hl, bc + or a, a + sbc hl, bc + jr nz, .L.exit_function_loop + + .extern __atexit_functions #endif + #if HAS_FINI_ARRAY ld hl, __fini_array_end jr .L.fini_array_start From fae00b4684310920f3613b4381d7b6a6fadb7d2c Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Thu, 5 Mar 2026 16:40:06 -0700 Subject: [PATCH 5/5] swapped order of atexit and free --- src/crt/crt0.S | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/crt/crt0.S b/src/crt/crt0.S index 8bc664cbb..a0feccd78 100644 --- a/src/crt/crt0.S +++ b/src/crt/crt0.S @@ -367,14 +367,11 @@ _exit: push de ; arg push hl ; exit status ld hl, (ix + 1 * 3) ; func - push hl ; func - push ix - call _free - pop bc ; reset SP - pop hl ; func ; atexit : void (*func)(void) ; on_exit : void (*func)(int status, void *arg) call __indcallhl + ex (sp), ix + call _free pop bc ; reset SP pop bc ; reset SP .L.exit_function_start: