Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileioc: improve garbage collect handling #270

Merged
merged 15 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions src/fileioc/fileioc.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
include '../include/library.inc'
;-------------------------------------------------------------------------------

library 'FILEIOC', 5
library 'FILEIOC', 6

;-------------------------------------------------------------------------------
; no dependencies
Expand Down Expand Up @@ -59,6 +59,12 @@ library 'FILEIOC', 5
;-------------------------------------------------------------------------------
export ti_ArchiveHasRoom

;-------------------------------------------------------------------------------
; v6 functions
;-------------------------------------------------------------------------------
export ti_SetGCBehavior


;-------------------------------------------------------------------------------
vat_ptr0 := $d0244e
vat_ptr1 := $d0257b
Expand Down Expand Up @@ -461,7 +467,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
Expand Down Expand Up @@ -1206,7 +1212,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
Expand All @@ -1221,10 +1227,10 @@ 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 _Arc_Unarc
call util_Arc_Unarc
call _PopOP1
jr .locate_program
.in_archive:
Expand Down Expand Up @@ -1257,7 +1263,7 @@ ti_Rename:
ldir
.is_zero:
call _PopOP1
call _Arc_Unarc
call util_Arc_Unarc
.smc_archive := $-3
call _PopOP1
call _ChkFindSym
Expand Down Expand Up @@ -1385,6 +1391,7 @@ ti_ArchiveHasRoom:
pop de
ex (sp),hl
push de
util_ArchiveHasRoom:
ld bc,12
add hl,bc
call _FindFreeArcSpot
Expand All @@ -1393,6 +1400,36 @@ ti_ArchiveHasRoom:
dec a
ret

;-------------------------------------------------------------------------------
ti_SetGCBehavior:
;Set routines to run before and after a garbage collect would be triggered.
; args:
; 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 de
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can someone else just double check that this pop order is correct? it seems that it sets up the pointers backwards but maybe I'm just tired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that apparently changed during my last pull lol. Fixed it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, no. Just realised that was an intentional optimization

Copy link
Member

@mateoconlechuga mateoconlechuga Aug 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware. I don't think it was ever correct; which is why I am asking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dude. I am asking you to look at if you are popping the arguments in the correct order. I know how to write code.

pop bc
ex (sp),hl
push bc
push de
add hl,de
or a,a
sbc hl,de
jr nz,.notdefault1
ld hl,util_pre_gc_default_handler
.notdefault1:
ld (util_pre_gc_handler),hl
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

;-------------------------------------------------------------------------------
; internal library routines
;-------------------------------------------------------------------------------
Expand All @@ -1410,6 +1447,7 @@ util_skip_archive_header:
inc hl
add hl, bc
ex de, hl
util_no_op:
ret

;-------------------------------------------------------------------------------
Expand Down Expand Up @@ -1573,6 +1611,22 @@ util_set_offset:
ld (hl), bc
ret

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
call _LoadDEInd_s
beckadamtheinventor marked this conversation as resolved.
Show resolved Hide resolved
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
call _Arc_Unarc
jp util_post_gc_default_handler
util_post_gc_handler:=$-3



;-------------------------------------------------------------------------------
; Internal library data
;-------------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions src/fileioc/fileioc.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,15 @@ uint8_t ti_RclVar(const uint8_t var_type, const char *var_name, void **data_stru
*/
bool ti_ArchiveHasRoom(uint24_t num_bytes);


/**
* 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, use gfx_End and gfx_Begin to reset graphics before, and setup graphics after the garbage collect.
* */
void ti_SetGCBehavior(void (*before)(void), void (*after)(void));


/**
* Allocates space for a real variable
* @returns Pointer to variable
Expand Down