From 991b7f1aa1b4b31c939ae5139504e597727ade52 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 13 Jul 2020 13:49:07 -0600 Subject: [PATCH 01/15] Add gc handler and etc. --- src/fileioc/fileioc.asm | 96 +++++++++++++++++++++++++++++++++++++++-- src/fileioc/fileioc.h | 8 ++++ 2 files changed, 100 insertions(+), 4 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 589f1c360..9e20a1b91 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -59,6 +59,13 @@ library 'FILEIOC', 5 ;------------------------------------------------------------------------------- export ti_ArchiveHasRoom +;------------------------------------------------------------------------------- +; v6 functions +;------------------------------------------------------------------------------- + export ti_SetPostGCHandler + + + ;------------------------------------------------------------------------------- vat_ptr0 := $d0244e vat_ptr1 := $d0257b @@ -461,7 +468,7 @@ ti_SetArchiveStatus: .set_archived: push bc pop af - call z, _Arc_Unarc + call z, util_Arc_Unarc jr .relocate_var .set_not_archived: push bc @@ -1206,7 +1213,7 @@ ti_Rename: inc de call _Mov8b call _PushOP1 ; save old name - ld hl, _Arc_Unarc + ld hl, util_Arc_Unarc ld (.smc_archive), hl pop hl ; new name ld de, OP1 + 1 @@ -1224,7 +1231,7 @@ ti_Rename: ld hl, $f8 ; $f8 = ret ld (.smc_archive), hl call _PushOP1 - call _Arc_Unarc + call util_Arc_Unarc call _PopOP1 jr .locate_program .in_archive: @@ -1257,7 +1264,7 @@ ti_Rename: ldir .is_zero: call _PopOP1 - call _Arc_Unarc + call util_Arc_Unarc .smc_archive := $-3 call _PopOP1 call _ChkFindSym @@ -1393,6 +1400,26 @@ ti_ArchiveHasRoom: dec a ret +;------------------------------------------------------------------------------- +ti_SetPostGCHandler: +;Set handler for setting up the screen after a garbage collect +; args: +; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette) +; return: +; None + pop de + ex (sp),hl + push de + add hl,de + or a,a + sbc hl,de + jr nz,.notdefault + ld hl,util_gfx_restore_default_handler +.notdefault: + ld (util_gfx_restore_handler),hl + ex hl,de + jp (hl) + ;------------------------------------------------------------------------------- ; internal library routines ;------------------------------------------------------------------------------- @@ -1573,6 +1600,67 @@ util_set_offset: ld (hl), bc ret +util_Arc_Unarc: ;properly handle garbage collects :P + call _ChkFindSym + push hl + call _ChkInRAM + pop hl + jr nz,.arc_unarc ;if the file is already in archive, we won't trigger a gc + call _LoadDEInd_s + ld hl,12 + add hl,de + call _FindFreeArcSpot ;check if we will trigger a gc + jr nz,.arc_unarc ;gc will not be triggered + ld a,(mpLcdCtrl) + cp a,lcdBpp16 + jr z,.arc_unarc ;already in OS 16bpp graphics mode + push af ;save lcd mode + call _ClrLCDFull + call _DrawStatusBar + ld a,lcdBpp16 + ld (mpLcdCtrl),a + call _Arc_Unarc + pop af ;restore lcd mode + ld (mpLcdCtrl),a + jp util_gfx_restore_default_handler +util_gfx_restore_handler:=$-3 +.arc_unarc: + jp _Arc_Unarc + + +util_gfx_restore_default_handler: + call _RunIndicOff + di ; turn off indicator + ld hl,$D40000 + ld (hl),l + ld bc,320*240 + push hl + pop de + inc de + ldir +.setup: + ld a,lcdBpp8 + ld (mpLcdCtrl),a ; operate in 8bpp + ld hl,mpLcdPalette + ld b,0 +.loop: + ld d,b + ld a,b + and a,192 + srl d + rra + ld e,a + ld a,31 + and a,b + or a,e + ld (hl),a + inc hl + ld (hl),d + inc hl + inc b + jr nz,.loop + ret + ;------------------------------------------------------------------------------- ; Internal library data ;------------------------------------------------------------------------------- diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index 3be1493ea..8096101c6 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -382,6 +382,14 @@ uint8_t ti_RclVar(const uint8_t var_type, const char *var_name, void **data_stru */ bool ti_ArchiveHasRoom(uint24_t num_bytes); + +/** + * Set routine to run after a garbage collect. + * @param routine Routine to run following a garbage collect. If this is 0, the default handler will be used. + * @note Useful for setting up the graphics palette. + * */ +void ti_SetPostGCHandler((void)(*routine)(void)); + /** * Allocates space for a real variable * @returns Pointer to variable From 364c0e34bc2b9f32b4442218f13130586e276bc5 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 14 Jul 2020 14:46:56 -0600 Subject: [PATCH 02/15] Add pre-gc handler and remove default gfx handlers --- src/fileioc/fileioc.asm | 77 ++++++++++++++++------------------------- src/fileioc/fileioc.h | 12 +++++-- 2 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 9e20a1b91..4d3c37561 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -63,7 +63,7 @@ library 'FILEIOC', 5 ; v6 functions ;------------------------------------------------------------------------------- export ti_SetPostGCHandler - + export ti_SetPreGCHandler ;------------------------------------------------------------------------------- @@ -1414,11 +1414,34 @@ ti_SetPostGCHandler: or a,a sbc hl,de jr nz,.notdefault - ld hl,util_gfx_restore_default_handler + ld hl,util_post_gc_default_handler +.notdefault: + ld (util_post_gc_handler),hl + ex hl,de + jp (hl) +util_post_gc_default_handler:=$F8 + +;------------------------------------------------------------------------------- +ti_SetPreGCHandler: +;Set handler for setting up the screen after a garbage collect +; args: +; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette) +; return: +; None + pop de + ex (sp),hl + push de + add hl,de + or a,a + sbc hl,de + jr nz,.notdefault + ld hl,util_pre_gc_default_handler .notdefault: - ld (util_gfx_restore_handler),hl + ld (util_pre_gc_handler),hl ex hl,de jp (hl) +util_pre_gc_default_handler:=$F8 + ;------------------------------------------------------------------------------- ; internal library routines @@ -1611,55 +1634,15 @@ util_Arc_Unarc: ;properly handle garbage collects :P add hl,de call _FindFreeArcSpot ;check if we will trigger a gc jr nz,.arc_unarc ;gc will not be triggered - ld a,(mpLcdCtrl) - cp a,lcdBpp16 - jr z,.arc_unarc ;already in OS 16bpp graphics mode - push af ;save lcd mode - call _ClrLCDFull - call _DrawStatusBar - ld a,lcdBpp16 - ld (mpLcdCtrl),a + call util_pre_gc_default_handler +util_pre_gc_handler:=$-3 call _Arc_Unarc - pop af ;restore lcd mode - ld (mpLcdCtrl),a - jp util_gfx_restore_default_handler -util_gfx_restore_handler:=$-3 + jp util_post_gc_default_handler +util_post_gc_handler:=$-3 .arc_unarc: jp _Arc_Unarc -util_gfx_restore_default_handler: - call _RunIndicOff - di ; turn off indicator - ld hl,$D40000 - ld (hl),l - ld bc,320*240 - push hl - pop de - inc de - ldir -.setup: - ld a,lcdBpp8 - ld (mpLcdCtrl),a ; operate in 8bpp - ld hl,mpLcdPalette - ld b,0 -.loop: - ld d,b - ld a,b - and a,192 - srl d - rra - ld e,a - ld a,31 - and a,b - or a,e - ld (hl),a - inc hl - ld (hl),d - inc hl - inc b - jr nz,.loop - ret ;------------------------------------------------------------------------------- ; Internal library data diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index 8096101c6..a3600b4c8 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -385,10 +385,18 @@ bool ti_ArchiveHasRoom(uint24_t num_bytes); /** * Set routine to run after a garbage collect. - * @param routine Routine to run following a garbage collect. If this is 0, the default handler will be used. + * @param routine Routine to run following a garbage collect. NULL sets it to do nothing. * @note Useful for setting up the graphics palette. * */ -void ti_SetPostGCHandler((void)(*routine)(void)); +void ti_SetPostGCHandler(void (*routine)(void)); + +/** + * Set routine to run before a garbage collect. + * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. + * @note Useful for cleanup. Make sure to set OS graphics mode during. + * */ +void ti_SetPreGCHandler(void (*routine)(void)); + /** * Allocates space for a real variable From ab3edfc0318788ec3f87452c05c29f7abafef15e Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 14 Jul 2020 14:57:17 -0600 Subject: [PATCH 03/15] Update fileioc.h documentation --- src/fileioc/fileioc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index a3600b4c8..9095864aa 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -386,14 +386,14 @@ bool ti_ArchiveHasRoom(uint24_t num_bytes); /** * Set routine to run after a garbage collect. * @param routine Routine to run following a garbage collect. NULL sets it to do nothing. - * @note Useful for setting up the graphics palette. + * @note If your program uses graphx, pass gfx_Begin to setup graphics after the garbage collect finishes. * */ void ti_SetPostGCHandler(void (*routine)(void)); /** * Set routine to run before a garbage collect. * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. - * @note Useful for cleanup. Make sure to set OS graphics mode during. + * @note Useful for cleanup. If your program uses graphx, pass gfx_End to set OS graphics mode to avoid corrupted graphics during the garbage collect. * */ void ti_SetPreGCHandler(void (*routine)(void)); From f4a51e88728f3d106b8f8cb1605ca365d85e0323 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 15 Jul 2020 20:36:25 -0600 Subject: [PATCH 04/15] Modify pre-gc handler return type so archival may be canceled. --- src/fileioc/fileioc.asm | 6 +++++- src/fileioc/fileioc.h | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 4d3c37561..581e944b9 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1440,7 +1440,9 @@ ti_SetPreGCHandler: ld (util_pre_gc_handler),hl ex hl,de jp (hl) -util_pre_gc_default_handler:=$F8 +util_pre_gc_default_handler: + xor a,a + ret ;------------------------------------------------------------------------------- @@ -1636,6 +1638,8 @@ util_Arc_Unarc: ;properly handle garbage collects :P jr nz,.arc_unarc ;gc will not be triggered call util_pre_gc_default_handler util_pre_gc_handler:=$-3 + or a,a + ret nz ;exit if the handler returns a non-zero value call _Arc_Unarc jp util_post_gc_default_handler util_post_gc_handler:=$-3 diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index 9095864aa..9ecb957eb 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -392,10 +392,10 @@ void ti_SetPostGCHandler(void (*routine)(void)); /** * Set routine to run before a garbage collect. - * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. + * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns a non-zero value, the variable will not be archived. * @note Useful for cleanup. If your program uses graphx, pass gfx_End to set OS graphics mode to avoid corrupted graphics during the garbage collect. * */ -void ti_SetPreGCHandler(void (*routine)(void)); +void ti_SetPreGCHandler(uint8_t (*routine)(void)); /** From 173bdd407f1ec1345cf8aae491cfb9186bd80242 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 15 Jul 2020 20:59:37 -0600 Subject: [PATCH 05/15] Update documentation for ti_SetPreGCHandler --- src/fileioc/fileioc.h | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index 9ecb957eb..0361b2e96 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -392,10 +392,26 @@ void ti_SetPostGCHandler(void (*routine)(void)); /** * Set routine to run before a garbage collect. - * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns a non-zero value, the variable will not be archived. - * @note Useful for cleanup. If your program uses graphx, pass gfx_End to set OS graphics mode to avoid corrupted graphics during the garbage collect. + * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns true, the variable will not be archived. + * @note Useful for cleanup. If your program uses graphx, run gfx_End inside the passed function to set OS graphics mode to avoid corrupted graphics during the garbage collect. + * @code + * bool pre_gc_handler(void){ + * gfx_End(); + * return 0; + * } + * int main(void){ + * gfx_Begin(); + * ti_CloseAll(); + * ti_SetPostGCHandler(gfx_Begin); + * ti_SetPreGCHandler(pre_gc_handler); + * //any garbage collect triggered in this program will now be preceeded by pre_gc_handler, and afterwards by gfx_Begin. + * //... + * ti_CloseAll(); + * gfx_End(); + * } + * @endcode * */ -void ti_SetPreGCHandler(uint8_t (*routine)(void)); +void ti_SetPreGCHandler(bool (*routine)(void)); /** From 9fc94154f21d1245ed39b687841abc7a2067136c Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 18 Jul 2020 16:16:43 -0600 Subject: [PATCH 06/15] Resolve 2/3 of comments --- src/fileioc/fileioc.asm | 5 +++-- src/fileioc/fileioc.h | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 581e944b9..26998537a 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1228,7 +1228,7 @@ ti_Rename: jr c, .return_2 call _ChkInRam jr nz, .in_archive - ld hl, $f8 ; $f8 = ret + ld hl, util_no_op ; no-op routine instead of assuming $F8 points to a ret instruction lol ld (.smc_archive), hl call _PushOP1 call util_Arc_Unarc @@ -1419,7 +1419,7 @@ ti_SetPostGCHandler: ld (util_post_gc_handler),hl ex hl,de jp (hl) -util_post_gc_default_handler:=$F8 +util_post_gc_default_handler:=util_no_op ;------------------------------------------------------------------------------- ti_SetPreGCHandler: @@ -1442,6 +1442,7 @@ ti_SetPreGCHandler: jp (hl) util_pre_gc_default_handler: xor a,a +util_no_op: ret diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index 0361b2e96..baa2e08a2 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -392,12 +392,12 @@ void ti_SetPostGCHandler(void (*routine)(void)); /** * Set routine to run before a garbage collect. - * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns true, the variable will not be archived. + * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns true, the GC will not occur, and the post-GC handler will not be executed. * @note Useful for cleanup. If your program uses graphx, run gfx_End inside the passed function to set OS graphics mode to avoid corrupted graphics during the garbage collect. * @code * bool pre_gc_handler(void){ * gfx_End(); - * return 0; + * return false; * } * int main(void){ * gfx_Begin(); From 35d1ad46444f5b77a892bc7eebee6a7d0eef5fc5 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 18 Jul 2020 22:08:31 -0600 Subject: [PATCH 07/15] Fix spacing and split hairs. --- src/fileioc/fileioc.asm | 78 ++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 26998537a..cd851aa1b 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1407,18 +1407,18 @@ ti_SetPostGCHandler: ; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette) ; return: ; None - pop de - ex (sp),hl - push de - add hl,de - or a,a - sbc hl,de - jr nz,.notdefault - ld hl,util_post_gc_default_handler + pop de + ex (sp),hl + push de + add hl,de + or a,a + sbc hl,de + jr nz,.notdefault + ld hl,util_post_gc_default_handler .notdefault: - ld (util_post_gc_handler),hl - ex hl,de - jp (hl) + ld (util_post_gc_handler),hl + ex hl,de + jp (hl) util_post_gc_default_handler:=util_no_op ;------------------------------------------------------------------------------- @@ -1428,20 +1428,20 @@ ti_SetPreGCHandler: ; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette) ; return: ; None - pop de - ex (sp),hl - push de - add hl,de - or a,a - sbc hl,de - jr nz,.notdefault - ld hl,util_pre_gc_default_handler + pop de + ex (sp),hl + push de + add hl,de + or a,a + sbc hl,de + jr nz,.notdefault + ld hl,util_pre_gc_default_handler .notdefault: - ld (util_pre_gc_handler),hl - ex hl,de - jp (hl) + ld (util_pre_gc_handler),hl + ex hl,de + jp (hl) util_pre_gc_default_handler: - xor a,a + xor a,a util_no_op: ret @@ -1627,25 +1627,25 @@ util_set_offset: ret util_Arc_Unarc: ;properly handle garbage collects :P - call _ChkFindSym - push hl - call _ChkInRAM - pop hl - jr nz,.arc_unarc ;if the file is already in archive, we won't trigger a gc - call _LoadDEInd_s - ld hl,12 - add hl,de - call _FindFreeArcSpot ;check if we will trigger a gc - jr nz,.arc_unarc ;gc will not be triggered - call util_pre_gc_default_handler + call _ChkFindSym + push hl + call _ChkInRAM + pop hl + jr nz,.arc_unarc ;if the file is already in archive, we won't trigger a gc + call _LoadDEInd_s + ld hl,12 + add hl,de + call _FindFreeArcSpot ;check if we will trigger a gc + jr nz,.arc_unarc ;gc will not be triggered + call util_pre_gc_default_handler util_pre_gc_handler:=$-3 - or a,a - ret nz ;exit if the handler returns a non-zero value - call _Arc_Unarc - jp util_post_gc_default_handler + or a,a + ret nz ;exit if the handler returns a non-zero value + call _Arc_Unarc + jp util_post_gc_default_handler util_post_gc_handler:=$-3 .arc_unarc: - jp _Arc_Unarc + jp _Arc_Unarc From a93c194c1802034a596eccb6f73e3feb39f900b7 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 20 Jul 2020 09:27:11 -0600 Subject: [PATCH 08/15] update ti_SetPreGCHandler --- src/fileioc/fileioc.asm | 2 +- src/fileioc/fileioc.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index cd851aa1b..ba1030402 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1640,7 +1640,7 @@ util_Arc_Unarc: ;properly handle garbage collects :P call util_pre_gc_default_handler util_pre_gc_handler:=$-3 or a,a - ret nz ;exit if the handler returns a non-zero value + ret z ;exit if the handler returns false call _Arc_Unarc jp util_post_gc_default_handler util_post_gc_handler:=$-3 diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index baa2e08a2..81d86e107 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -392,12 +392,12 @@ void ti_SetPostGCHandler(void (*routine)(void)); /** * Set routine to run before a garbage collect. - * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns true, the GC will not occur, and the post-GC handler will not be executed. + * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns false, the GC will not occur, and the post-GC handler will not be executed. * @note Useful for cleanup. If your program uses graphx, run gfx_End inside the passed function to set OS graphics mode to avoid corrupted graphics during the garbage collect. * @code * bool pre_gc_handler(void){ * gfx_End(); - * return false; + * return true; * } * int main(void){ * gfx_Begin(); From 2d10c1d6a906187e9eab6cc34847c0749df4ef64 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 20 Jul 2020 14:54:36 -0600 Subject: [PATCH 09/15] ti_SetGCBehavior instead of setting pre/post seperately --- src/fileioc/fileioc.asm | 63 +++++++++++++++-------------------------- src/fileioc/fileioc.h | 29 ++----------------- 2 files changed, 26 insertions(+), 66 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index ba1030402..2ebcb8cd5 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -2,7 +2,7 @@ include '../include/library.inc' ;------------------------------------------------------------------------------- -library 'FILEIOC', 5 +library 'FILEIOC', 6 ;------------------------------------------------------------------------------- ; no dependencies @@ -62,8 +62,7 @@ library 'FILEIOC', 5 ;------------------------------------------------------------------------------- ; v6 functions ;------------------------------------------------------------------------------- - export ti_SetPostGCHandler - export ti_SetPreGCHandler + export ti_SetGCBehavior ;------------------------------------------------------------------------------- @@ -1401,49 +1400,36 @@ ti_ArchiveHasRoom: ret ;------------------------------------------------------------------------------- -ti_SetPostGCHandler: -;Set handler for setting up the screen after a garbage collect +ti_SetGCBehavior: +;Set routines to run before and after a garbage collect would be triggered. ; args: -; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette) -; return: -; None - pop de - ex (sp),hl - push de - add hl,de - or a,a - sbc hl,de - jr nz,.notdefault - ld hl,util_post_gc_default_handler -.notdefault: - ld (util_post_gc_handler),hl - ex hl,de - jp (hl) -util_post_gc_default_handler:=util_no_op - -;------------------------------------------------------------------------------- -ti_SetPreGCHandler: -;Set handler for setting up the screen after a garbage collect -; args: -; sp + 3 : pointer to handler. Set to 0 to use default handler (xlibc palette) +; sp + 3 : pointer to routine to be run before. Set to 0 to use default handler. +; sp + 6 : pointer to routine to be run after. Set to 0 to use default handler. ; return: ; None + pop bc pop de ex (sp),hl push de + push bc add hl,de or a,a sbc hl,de - jr nz,.notdefault + jr nz,.notdefault1 ld hl,util_pre_gc_default_handler -.notdefault: +.notdefault1: ld (util_pre_gc_handler),hl - ex hl,de - jp (hl) -util_pre_gc_default_handler: - xor a,a -util_no_op: + ex hl,de + add hl,bc + or a,a + sbc hl,bc + jr nz,.notdefault2 + ld hl,util_post_gc_default_handler +.notdefault2: + ld (util_post_gc_handler),hl ret +util_post_gc_default_handler:=util_no_op +util_pre_gc_default_handler:=util_no_op ;------------------------------------------------------------------------------- @@ -1463,6 +1449,7 @@ util_skip_archive_header: inc hl add hl, bc ex de, hl +util_no_op: ret ;------------------------------------------------------------------------------- @@ -1631,21 +1618,17 @@ util_Arc_Unarc: ;properly handle garbage collects :P push hl call _ChkInRAM pop hl - jr nz,.arc_unarc ;if the file is already in archive, we won't trigger a gc + jp nz,_Arc_Unarc ;if the file is already in archive, we won't trigger a gc call _LoadDEInd_s ld hl,12 add hl,de call _FindFreeArcSpot ;check if we will trigger a gc - jr nz,.arc_unarc ;gc will not be triggered + jp nz,_Arc_Unarc ;gc will not be triggered call util_pre_gc_default_handler util_pre_gc_handler:=$-3 - or a,a - ret z ;exit if the handler returns false call _Arc_Unarc jp util_post_gc_default_handler util_post_gc_handler:=$-3 -.arc_unarc: - jp _Arc_Unarc diff --git a/src/fileioc/fileioc.h b/src/fileioc/fileioc.h index 81d86e107..4a2d77cdd 100644 --- a/src/fileioc/fileioc.h +++ b/src/fileioc/fileioc.h @@ -384,34 +384,11 @@ bool ti_ArchiveHasRoom(uint24_t num_bytes); /** - * Set routine to run after a garbage collect. + * Set routines to run before and after a garbage collect would be triggered. * @param routine Routine to run following a garbage collect. NULL sets it to do nothing. - * @note If your program uses graphx, pass gfx_Begin to setup graphics after the garbage collect finishes. + * @note If your program uses graphx, use gfx_End and gfx_Begin to reset graphics before, and setup graphics after the garbage collect. * */ -void ti_SetPostGCHandler(void (*routine)(void)); - -/** - * Set routine to run before a garbage collect. - * @param routine Routine to run preceeding a garbage collect. NULL sets it to do nothing. If this routine returns false, the GC will not occur, and the post-GC handler will not be executed. - * @note Useful for cleanup. If your program uses graphx, run gfx_End inside the passed function to set OS graphics mode to avoid corrupted graphics during the garbage collect. - * @code - * bool pre_gc_handler(void){ - * gfx_End(); - * return true; - * } - * int main(void){ - * gfx_Begin(); - * ti_CloseAll(); - * ti_SetPostGCHandler(gfx_Begin); - * ti_SetPreGCHandler(pre_gc_handler); - * //any garbage collect triggered in this program will now be preceeded by pre_gc_handler, and afterwards by gfx_Begin. - * //... - * ti_CloseAll(); - * gfx_End(); - * } - * @endcode - * */ -void ti_SetPreGCHandler(bool (*routine)(void)); +void ti_SetGCBehavior(void (*before)(void), void (*after)(void)); /** From 781aab59f7403cfaec836a58a6d46e7b6ddfbfdc Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 20 Jul 2020 14:57:57 -0600 Subject: [PATCH 10/15] Optimize util_Arc_Unarc --- src/fileioc/fileioc.asm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 2ebcb8cd5..90da58486 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1391,6 +1391,7 @@ ti_ArchiveHasRoom: pop de ex (sp),hl push de +util_ArchiveHasRoom: ld bc,12 add hl,bc call _FindFreeArcSpot @@ -1620,9 +1621,8 @@ util_Arc_Unarc: ;properly handle garbage collects :P pop hl jp nz,_Arc_Unarc ;if the file is already in archive, we won't trigger a gc call _LoadDEInd_s - ld hl,12 - add hl,de - call _FindFreeArcSpot ;check if we will trigger a gc + ex hl,de + call util_ArchiveHasRoom jp nz,_Arc_Unarc ;gc will not be triggered call util_pre_gc_default_handler util_pre_gc_handler:=$-3 From ffde1fccd0dfecdd6951733699b8d41745af6bba Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 20 Jul 2020 21:55:35 -0600 Subject: [PATCH 11/15] Optimize util_Arc_Unarc again --- src/fileioc/fileioc.asm | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 90da58486..93f3b2e81 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1615,11 +1615,9 @@ util_set_offset: ret util_Arc_Unarc: ;properly handle garbage collects :P - call _ChkFindSym - push hl call _ChkInRAM - pop hl jp nz,_Arc_Unarc ;if the file is already in archive, we won't trigger a gc + ex hl,de call _LoadDEInd_s ex hl,de call util_ArchiveHasRoom From dc76a7a0b354e7e1813edcc36cd99d96037dbed8 Mon Sep 17 00:00:00 2001 From: Matt Waltz Date: Sun, 9 Aug 2020 09:05:16 -0600 Subject: [PATCH 12/15] minor optimization and spacing fixes --- src/fileioc/fileioc.asm | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 93f3b2e81..0a1778749 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1408,11 +1408,11 @@ ti_SetGCBehavior: ; sp + 6 : pointer to routine to be run after. Set to 0 to use default handler. ; return: ; None - pop bc pop de + pop bc ex (sp),hl - push de push bc + push de add hl,de or a,a sbc hl,de @@ -1420,18 +1420,15 @@ ti_SetGCBehavior: ld hl,util_pre_gc_default_handler .notdefault1: ld (util_pre_gc_handler),hl - ex hl,de - add hl,bc - or a,a - sbc hl,bc + sbc hl,hl + adc hl,bc jr nz,.notdefault2 ld hl,util_post_gc_default_handler .notdefault2: ld (util_post_gc_handler),hl ret -util_post_gc_default_handler:=util_no_op -util_pre_gc_default_handler:=util_no_op - +util_post_gc_default_handler := util_no_op +util_pre_gc_default_handler := util_no_op ;------------------------------------------------------------------------------- ; internal library routines @@ -1617,10 +1614,10 @@ util_set_offset: util_Arc_Unarc: ;properly handle garbage collects :P call _ChkInRAM jp nz,_Arc_Unarc ;if the file is already in archive, we won't trigger a gc - ex hl,de + ex hl,de call _LoadDEInd_s - ex hl,de - call util_ArchiveHasRoom + ex hl,de + call util_ArchiveHasRoom jp nz,_Arc_Unarc ;gc will not be triggered call util_pre_gc_default_handler util_pre_gc_handler:=$-3 From 20861c92b60dc3d700fbeeba91c43d2b4994c971 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 9 Aug 2020 09:23:36 -0600 Subject: [PATCH 13/15] why did this change during the pull lol --- src/fileioc/fileioc.asm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 0a1778749..b81029dee 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1408,11 +1408,11 @@ ti_SetGCBehavior: ; sp + 6 : pointer to routine to be run after. Set to 0 to use default handler. ; return: ; None - pop de pop bc + pop de ex (sp),hl - push bc push de + push bc add hl,de or a,a sbc hl,de From a6118329b37020a9790a18bffaf8058e630b3f42 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 9 Aug 2020 09:27:56 -0600 Subject: [PATCH 14/15] Revert my 'fix' lol --- src/fileioc/fileioc.asm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index b81029dee..0a1778749 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1408,11 +1408,11 @@ ti_SetGCBehavior: ; sp + 6 : pointer to routine to be run after. Set to 0 to use default handler. ; return: ; None - pop bc pop de + pop bc ex (sp),hl - push de push bc + push de add hl,de or a,a sbc hl,de From cd701cd695302473282d237d2e78f83e813d85ad Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 9 Aug 2020 09:51:33 -0600 Subject: [PATCH 15/15] Match code to documentation --- src/fileioc/fileioc.asm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fileioc/fileioc.asm b/src/fileioc/fileioc.asm index 0a1778749..9abee8387 100644 --- a/src/fileioc/fileioc.asm +++ b/src/fileioc/fileioc.asm @@ -1417,15 +1417,15 @@ ti_SetGCBehavior: or a,a sbc hl,de jr nz,.notdefault1 - ld hl,util_pre_gc_default_handler + ld hl,util_post_gc_default_handler .notdefault1: - ld (util_pre_gc_handler),hl + ld (util_post_gc_handler),hl sbc hl,hl adc hl,bc jr nz,.notdefault2 - ld hl,util_post_gc_default_handler + ld hl,util_pre_gc_default_handler .notdefault2: - ld (util_post_gc_handler),hl + ld (util_pre_gc_handler),hl ret util_post_gc_default_handler := util_no_op util_pre_gc_default_handler := util_no_op