Erasing multiple copies of a file on a microdrive

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
jpnz
Manic Miner
Posts: 324
Joined: Tue Nov 14, 2017 4:07 pm
Location: Hamilt[r]on - City Of The Future - NZ

Erasing multiple copies of a file on a microdrive

Post by jpnz »

The subject of speeding up microdrive load times with multiple copies of a file was mentioned a couple of weeks back:
jpnz wrote: Sun Oct 01, 2023 5:17 am You can speed up the seek times by having more than one copy on the cart and this technique applies to other files as well

The system variable 0x5cef (COPIES) supports this, e.g to save two copies of run

Code: Select all

POKE 23791,2: SAVE *"m";1;"run" LINE 0
Saying that, having the two copies of run interspersed on the cartridge would be even better...

One caveat of using COPIES is that erasing a file does exactly that - it's only the copy that turns up first
Andrew Pennell discusses multiple copies of files in his book Master Your ZX Microdrive (Chapter 2 page 24) - the pertinent bit being:
If you make x copies of a file, to completely delete it you must do the ERASE command x times
And that got me thinking, if you don't know how many copies of a file there are (and a CAT won't tell you) how do you get rid of them?...

Well, the ERASE routine - documented here - leaves a breadcrumb

The system variable SECTOR (0x5cc9/23753) is initially set at 0x04fb - that being 1275 sectors (5*255) to visit - or 5 revolutions of the cartridge

The ERASE routine will decrement the SECTOR count while looking for a matching filename - and when it finds the first match it will rewrite the headers, tidy up, then stop and leave SECTOR just where it is

If there's no match, i.e. the file does not exist, the search will continue and SECTOR decremented until hitting 0

And that's the breadcrumb - if SECTOR is not 0 after issuing the ERASE command then try again - for example:

Code: Select all

1 DEF FN P(A)=PEEK A+256*PEEK (A+1)
2 ERASE "M";1;"SOMEFILE": IF FN P(23753)<>0 THEN GOTO 2
3 STOP
This approach can also be implemented in machine code, will post about that later

Warning, code above may also fry any egg(s) inadvertently placed on top of a real microdrive
User avatar
jpnz
Manic Miner
Posts: 324
Joined: Tue Nov 14, 2017 4:07 pm
Location: Hamilt[r]on - City Of The Future - NZ

Re: Erasing multiple copies of a file on a microdrive

Post by jpnz »

Machine code version:

Code: Select all

;
; [ba]star[d] ERASE - oct 2023
; erase multiple copies
; of file on microdrive
; proof of concept - no error handling
; mailto:[email protected]
;
	org &8000
;
d_str1	equ &5cd6	; 2 byte drive number 1 - 8
l_str1	equ &5cd9	; device type "m", "n", "t" or "b"
n_str1	equ &5cda	; length of filename
t_str1	equ &5cdc	; address of filename
sector	equ &5cc9	; 2 byte workspace used by microdrive
;
differ	equ &19dd	; current favourite rom routine
;
main	exx
	push hl		; preserve return to basic
	exx
;
	rst &08
	defb &31	; initialize if1
;
setup	ld hl,&0001	; set drive 1
	ld (d_str1),hl	; and insert
;
	ld a,"m"	; signify microdrive operation
	ld (l_str1),a	; and insert
;
	ld hl,&0008	; filename length
	ld (n_str1),hl	; and insert
;
	ld hl,filenm	; point to filename
	ld (t_str1),hl	; and insert
;
fry_egg	rst &08
	defb &24	; erase hook code
;
	ld hl,(sector)
	ld de,&0000
	call differ	; is sector at 0?
	jr nz,fry_egg	; no, more erasing required
;
leave	exx
	pop hl		; restore return to basic
	exx
;
	ret
;
filenm	defm "SOMEFILE"
;
	end
;
Post Reply