2021-01-07 00:56:31 +00:00
|
|
|
; C64 and Cx16 disk drive I/O routines.
|
|
|
|
;
|
|
|
|
; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
|
|
|
|
|
2020-10-13 23:17:18 +00:00
|
|
|
%import textio
|
2021-01-07 19:01:11 +00:00
|
|
|
%import string
|
2020-10-13 23:17:18 +00:00
|
|
|
%import syslib
|
|
|
|
|
|
|
|
diskio {
|
|
|
|
|
2020-12-07 22:29:34 +00:00
|
|
|
sub directory(ubyte drivenumber) -> ubyte {
|
2020-12-09 23:02:21 +00:00
|
|
|
; -- Prints the directory contents of disk drive 8-11 to the screen. Returns success.
|
2020-10-13 23:17:18 +00:00
|
|
|
|
|
|
|
c64.SETNAM(1, "$")
|
2020-12-11 21:36:14 +00:00
|
|
|
c64.SETLFS(13, drivenumber, 0)
|
|
|
|
void c64.OPEN() ; open 13,8,0,"$"
|
2020-10-13 23:17:18 +00:00
|
|
|
if_cs
|
|
|
|
goto io_error
|
2020-12-11 21:36:14 +00:00
|
|
|
void c64.CHKIN(13) ; use #13 as input channel
|
2020-10-13 23:17:18 +00:00
|
|
|
if_cs
|
|
|
|
goto io_error
|
|
|
|
|
|
|
|
repeat 4 {
|
|
|
|
void c64.CHRIN() ; skip the 4 prologue bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
; while not key pressed / EOF encountered, read data.
|
|
|
|
ubyte status = c64.READST()
|
|
|
|
while not status {
|
2020-12-08 00:02:38 +00:00
|
|
|
ubyte low = c64.CHRIN()
|
|
|
|
ubyte high = c64.CHRIN()
|
|
|
|
txt.print_uw(mkword(high, low))
|
2021-01-14 21:51:09 +00:00
|
|
|
txt.spc()
|
2020-10-13 23:17:18 +00:00
|
|
|
ubyte @zp char
|
2020-12-22 02:35:00 +00:00
|
|
|
repeat {
|
2020-10-13 23:17:18 +00:00
|
|
|
char = c64.CHRIN()
|
2020-12-22 04:54:28 +00:00
|
|
|
if char==0
|
2020-12-22 02:35:00 +00:00
|
|
|
break
|
2020-10-13 23:17:18 +00:00
|
|
|
txt.chrout(char)
|
2020-12-22 02:35:00 +00:00
|
|
|
}
|
2021-01-08 15:56:17 +00:00
|
|
|
txt.nl()
|
2020-10-13 23:17:18 +00:00
|
|
|
void c64.CHRIN() ; skip 2 bytes
|
|
|
|
void c64.CHRIN()
|
|
|
|
status = c64.READST()
|
2020-12-22 02:35:00 +00:00
|
|
|
if c64.STOP2()
|
2020-10-13 23:17:18 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
io_error:
|
|
|
|
status = c64.READST()
|
|
|
|
c64.CLRCHN() ; restore default i/o devices
|
2020-12-11 21:36:14 +00:00
|
|
|
c64.CLOSE(13)
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2021-01-10 13:29:51 +00:00
|
|
|
if status and status & $40 == 0 { ; bit 6=end of file
|
2020-10-13 23:17:18 +00:00
|
|
|
txt.print("\ni/o error, status: ")
|
|
|
|
txt.print_ub(status)
|
2021-01-08 15:56:17 +00:00
|
|
|
txt.nl()
|
2020-10-13 23:17:18 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-09 23:58:59 +00:00
|
|
|
; internal variables for the iterative file lister / loader
|
2020-12-08 02:28:29 +00:00
|
|
|
ubyte list_skip_disk_name
|
|
|
|
uword list_pattern
|
|
|
|
uword list_blocks
|
2020-12-09 23:02:21 +00:00
|
|
|
ubyte iteration_in_progress = false
|
2020-12-31 18:27:34 +00:00
|
|
|
ubyte @zp first_byte
|
|
|
|
ubyte have_first_byte
|
2020-12-11 21:36:14 +00:00
|
|
|
str list_filename = "?" * 32
|
|
|
|
|
|
|
|
|
2021-01-18 00:38:33 +00:00
|
|
|
; ----- get a list of files (uses iteration functions internally) -----
|
2020-12-11 21:36:14 +00:00
|
|
|
|
2020-12-30 22:34:00 +00:00
|
|
|
sub list_files(ubyte drivenumber, uword pattern_ptr, uword name_ptrs, ubyte max_names) -> ubyte {
|
2020-12-11 21:36:14 +00:00
|
|
|
; -- fill the array 'name_ptrs' with (pointers to) the names of the files requested.
|
2022-01-26 02:17:33 +00:00
|
|
|
const uword names_buf_size = 800
|
|
|
|
uword names_buffer = memory("filenames", names_buf_size, 0)
|
2020-12-27 02:35:56 +00:00
|
|
|
uword buffer_start = names_buffer
|
2020-12-11 21:36:14 +00:00
|
|
|
ubyte files_found = 0
|
2020-12-30 22:34:00 +00:00
|
|
|
if lf_start_list(drivenumber, pattern_ptr) {
|
2020-12-11 21:36:14 +00:00
|
|
|
while lf_next_entry() {
|
2020-12-27 02:35:56 +00:00
|
|
|
@(name_ptrs) = lsb(names_buffer)
|
2020-12-11 21:36:14 +00:00
|
|
|
name_ptrs++
|
2020-12-27 02:35:56 +00:00
|
|
|
@(name_ptrs) = msb(names_buffer)
|
2020-12-11 21:36:14 +00:00
|
|
|
name_ptrs++
|
2021-01-07 19:01:11 +00:00
|
|
|
names_buffer += string.copy(diskio.list_filename, names_buffer) + 1
|
2020-12-11 21:36:14 +00:00
|
|
|
files_found++
|
2022-01-26 02:17:33 +00:00
|
|
|
if names_buffer - buffer_start > names_buf_size-18
|
2020-12-11 21:36:14 +00:00
|
|
|
break
|
|
|
|
if files_found == max_names
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lf_end_list()
|
|
|
|
}
|
|
|
|
return files_found
|
|
|
|
}
|
2020-12-09 23:58:59 +00:00
|
|
|
|
2021-01-10 04:04:56 +00:00
|
|
|
; ----- iterative file lister functions (uses io channel 12) -----
|
2020-12-09 23:58:59 +00:00
|
|
|
|
2020-12-30 22:34:00 +00:00
|
|
|
sub lf_start_list(ubyte drivenumber, uword pattern_ptr) -> ubyte {
|
|
|
|
; -- start an iterative file listing with optional pattern matching.
|
2020-12-11 21:36:14 +00:00
|
|
|
; note: only a single iteration loop can be active at a time!
|
2020-12-08 02:28:29 +00:00
|
|
|
lf_end_list()
|
2020-12-30 22:34:00 +00:00
|
|
|
list_pattern = pattern_ptr
|
2020-12-08 02:28:29 +00:00
|
|
|
list_skip_disk_name = true
|
2020-12-09 23:02:21 +00:00
|
|
|
iteration_in_progress = true
|
2020-12-07 22:29:34 +00:00
|
|
|
|
|
|
|
c64.SETNAM(1, "$")
|
2020-12-11 21:36:14 +00:00
|
|
|
c64.SETLFS(12, drivenumber, 0)
|
|
|
|
void c64.OPEN() ; open 12,8,0,"$"
|
2020-12-07 22:29:34 +00:00
|
|
|
if_cs
|
2020-12-08 02:28:29 +00:00
|
|
|
goto io_error
|
2020-12-11 21:36:14 +00:00
|
|
|
void c64.CHKIN(12) ; use #12 as input channel
|
2020-12-07 22:29:34 +00:00
|
|
|
if_cs
|
2020-12-08 02:28:29 +00:00
|
|
|
goto io_error
|
2020-12-07 22:29:34 +00:00
|
|
|
|
|
|
|
repeat 4 {
|
|
|
|
void c64.CHRIN() ; skip the 4 prologue bytes
|
|
|
|
}
|
|
|
|
|
2020-12-08 02:28:29 +00:00
|
|
|
if c64.READST()==0
|
|
|
|
return true
|
|
|
|
|
|
|
|
io_error:
|
|
|
|
lf_end_list()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lf_next_entry() -> ubyte {
|
|
|
|
; -- retrieve the next entry from an iterative file listing session.
|
|
|
|
; results will be found in list_blocks and list_filename.
|
|
|
|
; if it returns false though, there are no more entries (or an error occurred).
|
|
|
|
|
2020-12-09 23:02:21 +00:00
|
|
|
if not iteration_in_progress
|
2020-12-08 02:28:29 +00:00
|
|
|
return false
|
2020-12-08 00:02:38 +00:00
|
|
|
|
2020-12-11 21:36:14 +00:00
|
|
|
repeat {
|
|
|
|
void c64.CHKIN(12) ; use #12 as input channel again
|
|
|
|
|
2020-12-08 02:28:29 +00:00
|
|
|
uword nameptr = &list_filename
|
|
|
|
ubyte blocks_lsb = c64.CHRIN()
|
|
|
|
ubyte blocks_msb = c64.CHRIN()
|
2020-12-11 21:36:14 +00:00
|
|
|
|
|
|
|
if c64.READST()
|
|
|
|
goto close_end
|
|
|
|
|
2020-12-08 02:28:29 +00:00
|
|
|
list_blocks = mkword(blocks_msb, blocks_lsb)
|
2020-12-07 22:29:34 +00:00
|
|
|
|
|
|
|
; read until the filename starts after the first "
|
|
|
|
while c64.CHRIN()!='\"' {
|
|
|
|
if c64.READST()
|
2020-12-08 00:34:08 +00:00
|
|
|
goto close_end
|
2020-12-07 22:29:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 02:28:29 +00:00
|
|
|
; read the filename
|
2020-12-08 00:02:38 +00:00
|
|
|
repeat {
|
|
|
|
ubyte char = c64.CHRIN()
|
2020-12-22 02:35:00 +00:00
|
|
|
if char==0
|
2020-12-08 00:34:08 +00:00
|
|
|
break
|
2020-12-08 00:02:38 +00:00
|
|
|
if char=='\"'
|
|
|
|
break
|
2020-12-08 02:28:29 +00:00
|
|
|
@(nameptr) = char
|
|
|
|
nameptr++
|
2020-12-08 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 02:28:29 +00:00
|
|
|
@(nameptr) = 0
|
2020-12-08 00:02:38 +00:00
|
|
|
|
2020-12-08 21:10:12 +00:00
|
|
|
while c64.CHRIN() {
|
|
|
|
; read the rest of the entry until the end
|
|
|
|
}
|
2020-12-08 00:02:38 +00:00
|
|
|
|
2020-12-07 22:29:34 +00:00
|
|
|
void c64.CHRIN() ; skip 2 bytes
|
|
|
|
void c64.CHRIN()
|
2020-12-08 02:28:29 +00:00
|
|
|
|
|
|
|
if not list_skip_disk_name {
|
2020-12-30 22:34:00 +00:00
|
|
|
if not list_pattern
|
|
|
|
return true
|
|
|
|
if prog8_lib.pattern_match(list_filename, list_pattern)
|
2020-12-08 02:28:29 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
list_skip_disk_name = false
|
2020-12-07 22:29:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:34:08 +00:00
|
|
|
close_end:
|
2020-12-08 02:28:29 +00:00
|
|
|
lf_end_list()
|
|
|
|
return false
|
2020-12-07 22:29:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 02:28:29 +00:00
|
|
|
sub lf_end_list() {
|
|
|
|
; -- end an iterative file listing session (close channels).
|
2020-12-09 23:02:21 +00:00
|
|
|
if iteration_in_progress {
|
2020-12-08 02:28:29 +00:00
|
|
|
c64.CLRCHN()
|
2020-12-11 21:36:14 +00:00
|
|
|
c64.CLOSE(12)
|
2020-12-09 23:02:21 +00:00
|
|
|
iteration_in_progress = false
|
2020-12-08 02:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-10 04:04:56 +00:00
|
|
|
; ----- iterative file loader functions (uses io channel 11) -----
|
2020-12-09 23:58:59 +00:00
|
|
|
|
|
|
|
sub f_open(ubyte drivenumber, uword filenameptr) -> ubyte {
|
2020-12-11 21:36:14 +00:00
|
|
|
; -- open a file for iterative reading with f_read
|
|
|
|
; note: only a single iteration loop can be active at a time!
|
2020-12-09 23:58:59 +00:00
|
|
|
f_close()
|
|
|
|
|
2021-01-07 19:01:11 +00:00
|
|
|
c64.SETNAM(string.length(filenameptr), filenameptr)
|
2021-01-13 21:11:51 +00:00
|
|
|
c64.SETLFS(11, drivenumber, 0)
|
2020-12-09 23:58:59 +00:00
|
|
|
void c64.OPEN() ; open 11,8,0,"filename"
|
|
|
|
if_cc {
|
|
|
|
iteration_in_progress = true
|
2020-12-31 18:27:34 +00:00
|
|
|
have_first_byte = false
|
2020-12-11 21:36:14 +00:00
|
|
|
void c64.CHKIN(11) ; use #11 as input channel
|
2020-12-31 18:27:34 +00:00
|
|
|
if_cc {
|
|
|
|
first_byte = c64.CHRIN() ; read first byte to test for file not found
|
|
|
|
if not c64.READST() {
|
|
|
|
have_first_byte = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 23:58:59 +00:00
|
|
|
}
|
|
|
|
f_close()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-24 05:24:52 +00:00
|
|
|
sub f_read(uword bufferpointer, uword num_bytes) -> uword {
|
|
|
|
; -- read from the currently open file, up to the given number of bytes.
|
|
|
|
; returns the actual number of bytes read. (checks for End-of-file and error conditions)
|
2020-12-31 18:27:34 +00:00
|
|
|
if not iteration_in_progress or not num_bytes
|
2020-12-09 23:58:59 +00:00
|
|
|
return 0
|
|
|
|
|
2020-12-31 18:27:34 +00:00
|
|
|
list_blocks = 0 ; we reuse this variable for the total number of bytes read
|
|
|
|
if have_first_byte {
|
|
|
|
have_first_byte=false
|
|
|
|
@(bufferpointer) = first_byte
|
|
|
|
bufferpointer++
|
|
|
|
list_blocks++
|
|
|
|
num_bytes--
|
|
|
|
}
|
|
|
|
|
2020-12-11 21:36:14 +00:00
|
|
|
void c64.CHKIN(11) ; use #11 as input channel again
|
2020-12-31 15:46:24 +00:00
|
|
|
%asm {{
|
|
|
|
lda bufferpointer
|
2022-01-17 21:03:53 +00:00
|
|
|
sta m_in_buffer+1
|
2020-12-31 15:46:24 +00:00
|
|
|
lda bufferpointer+1
|
2022-01-17 21:03:53 +00:00
|
|
|
sta m_in_buffer+2
|
2020-12-31 15:46:24 +00:00
|
|
|
}}
|
2020-12-24 05:24:52 +00:00
|
|
|
repeat num_bytes {
|
2020-12-31 15:46:24 +00:00
|
|
|
%asm {{
|
|
|
|
jsr c64.CHRIN
|
2021-01-10 13:29:51 +00:00
|
|
|
sta cx16.r5
|
2022-01-17 21:03:53 +00:00
|
|
|
m_in_buffer sta $ffff
|
|
|
|
inc m_in_buffer+1
|
2020-12-31 15:46:24 +00:00
|
|
|
bne +
|
2022-01-17 21:03:53 +00:00
|
|
|
inc m_in_buffer+2
|
2020-12-31 18:27:34 +00:00
|
|
|
+ inc list_blocks
|
2020-12-31 15:46:24 +00:00
|
|
|
bne +
|
2020-12-31 18:27:34 +00:00
|
|
|
inc list_blocks+1
|
2020-12-31 15:46:24 +00:00
|
|
|
+
|
|
|
|
}}
|
2020-12-24 05:24:52 +00:00
|
|
|
|
2021-01-10 13:29:51 +00:00
|
|
|
if cx16.r5==$0d { ; chance on I/o error status?
|
|
|
|
first_byte = c64.READST()
|
|
|
|
if first_byte & $40
|
|
|
|
f_close() ; end of file, close it
|
|
|
|
if first_byte
|
|
|
|
return list_blocks
|
|
|
|
}
|
2020-12-31 18:27:34 +00:00
|
|
|
}
|
2021-01-10 13:29:51 +00:00
|
|
|
return list_blocks
|
2020-12-31 15:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub f_read_all(uword bufferpointer) -> uword {
|
|
|
|
; -- read the full contents of the file, returns number of bytes read.
|
|
|
|
if not iteration_in_progress
|
|
|
|
return 0
|
|
|
|
|
2020-12-31 18:27:34 +00:00
|
|
|
list_blocks = 0 ; we reuse this variable for the total number of bytes read
|
|
|
|
if have_first_byte {
|
|
|
|
have_first_byte=false
|
|
|
|
@(bufferpointer) = first_byte
|
|
|
|
bufferpointer++
|
|
|
|
list_blocks++
|
|
|
|
}
|
|
|
|
|
2020-12-31 15:46:24 +00:00
|
|
|
while not c64.READST() {
|
2020-12-31 18:27:34 +00:00
|
|
|
list_blocks += f_read(bufferpointer, 256)
|
2020-12-31 15:46:24 +00:00
|
|
|
bufferpointer += 256
|
2020-12-24 05:24:52 +00:00
|
|
|
}
|
2020-12-31 18:27:34 +00:00
|
|
|
return list_blocks
|
2020-12-24 05:24:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 04:04:56 +00:00
|
|
|
asmsub f_readline(uword bufptr @AY) clobbers(X) -> ubyte @Y {
|
|
|
|
; Routine to read text lines from a text file. Lines must be less than 255 characters.
|
2021-01-10 15:00:22 +00:00
|
|
|
; Reads characters from the input file UNTIL a newline or return character (or EOF).
|
|
|
|
; The line read will be 0-terminated in the buffer (and not contain the end of line character).
|
2021-01-14 21:51:09 +00:00
|
|
|
; The length of the line is returned in Y. Note that an empty line is okay and is length 0!
|
|
|
|
; I/O error status should be checked by the caller itself via READST() routine.
|
2021-01-10 04:04:56 +00:00
|
|
|
%asm {{
|
|
|
|
sta P8ZP_SCRATCH_W1
|
|
|
|
sty P8ZP_SCRATCH_W1+1
|
|
|
|
ldx #11
|
|
|
|
jsr c64.CHKIN ; use channel 11 again for input
|
|
|
|
ldy #0
|
2021-01-10 15:00:22 +00:00
|
|
|
lda have_first_byte
|
|
|
|
beq _loop
|
|
|
|
lda #0
|
|
|
|
sta have_first_byte
|
|
|
|
lda first_byte
|
|
|
|
sta (P8ZP_SCRATCH_W1),y
|
|
|
|
iny
|
2021-01-10 04:04:56 +00:00
|
|
|
_loop jsr c64.CHRIN
|
|
|
|
sta (P8ZP_SCRATCH_W1),y
|
|
|
|
beq _end
|
|
|
|
iny
|
|
|
|
cmp #$0a
|
2021-01-10 15:00:22 +00:00
|
|
|
beq _line_end
|
2021-01-10 04:04:56 +00:00
|
|
|
cmp #$0d
|
|
|
|
bne _loop
|
2021-01-10 15:00:22 +00:00
|
|
|
_line_end dey ; get rid of the trailing end-of-line char
|
|
|
|
lda #0
|
2021-01-10 04:04:56 +00:00
|
|
|
sta (P8ZP_SCRATCH_W1),y
|
|
|
|
_end rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2020-12-31 15:46:24 +00:00
|
|
|
|
2020-12-09 23:58:59 +00:00
|
|
|
sub f_close() {
|
|
|
|
; -- end an iterative file loading session (close channels).
|
|
|
|
if iteration_in_progress {
|
|
|
|
c64.CLRCHN()
|
|
|
|
c64.CLOSE(11)
|
|
|
|
iteration_in_progress = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-01 17:13:56 +00:00
|
|
|
; ----- iterative file saver functions (uses io channel 14) -----
|
|
|
|
|
|
|
|
sub f_open_w(ubyte drivenumber, uword filenameptr) -> ubyte {
|
|
|
|
; -- open a file for iterative writing with f_write
|
|
|
|
f_close_w()
|
|
|
|
|
|
|
|
c64.SETNAM(string.length(filenameptr), filenameptr)
|
|
|
|
c64.SETLFS(14, drivenumber, 1)
|
|
|
|
void c64.OPEN() ; open 14,8,1,"filename"
|
|
|
|
if_cc {
|
|
|
|
void c64.CHKOUT(14) ; use #14 as input channel
|
|
|
|
return not c64.READST()
|
|
|
|
}
|
|
|
|
f_close_w()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sub f_write(uword bufferpointer, uword num_bytes) -> ubyte {
|
|
|
|
; -- write the given umber of bytes to the currently open file
|
|
|
|
if num_bytes!=0 {
|
|
|
|
void c64.CHKOUT(14) ; use #14 as input channel again
|
|
|
|
repeat num_bytes {
|
|
|
|
c64.CHROUT(@(bufferpointer))
|
|
|
|
bufferpointer++
|
|
|
|
}
|
|
|
|
return not c64.READST()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
sub f_close_w() {
|
|
|
|
; -- end an iterative file writing session (close channels).
|
|
|
|
c64.CLRCHN()
|
|
|
|
c64.CLOSE(14)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
; ---- other functions ----
|
|
|
|
|
2020-12-09 23:02:21 +00:00
|
|
|
sub status(ubyte drivenumber) -> uword {
|
|
|
|
; -- retrieve the disk drive's current status message
|
|
|
|
uword messageptr = &filename
|
2020-12-08 00:02:38 +00:00
|
|
|
c64.SETNAM(0, filename)
|
2020-10-13 23:17:18 +00:00
|
|
|
c64.SETLFS(15, drivenumber, 15)
|
|
|
|
void c64.OPEN() ; open 15,8,15
|
|
|
|
if_cs
|
|
|
|
goto io_error
|
|
|
|
void c64.CHKIN(15) ; use #15 as input channel
|
|
|
|
if_cs
|
|
|
|
goto io_error
|
|
|
|
|
2020-12-09 23:02:21 +00:00
|
|
|
while not c64.READST() {
|
|
|
|
@(messageptr) = c64.CHRIN()
|
|
|
|
messageptr++
|
|
|
|
}
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2020-12-09 23:02:21 +00:00
|
|
|
@(messageptr) = 0
|
2021-05-01 13:20:11 +00:00
|
|
|
done:
|
2020-10-13 23:17:18 +00:00
|
|
|
c64.CLRCHN() ; restore default i/o devices
|
|
|
|
c64.CLOSE(15)
|
2020-12-09 23:02:21 +00:00
|
|
|
return filename
|
2021-05-01 13:20:11 +00:00
|
|
|
|
|
|
|
io_error:
|
|
|
|
filename = "?disk error"
|
|
|
|
goto done
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 22:29:34 +00:00
|
|
|
sub save(ubyte drivenumber, uword filenameptr, uword address, uword size) -> ubyte {
|
2021-01-07 19:01:11 +00:00
|
|
|
c64.SETNAM(string.length(filenameptr), filenameptr)
|
2020-10-14 20:33:49 +00:00
|
|
|
c64.SETLFS(1, drivenumber, 0)
|
|
|
|
uword end_address = address + size
|
2021-04-06 21:55:20 +00:00
|
|
|
first_byte = 0 ; result var reuse
|
2020-10-14 20:33:49 +00:00
|
|
|
|
|
|
|
%asm {{
|
|
|
|
lda address
|
|
|
|
sta P8ZP_SCRATCH_W1
|
|
|
|
lda address+1
|
|
|
|
sta P8ZP_SCRATCH_W1+1
|
|
|
|
stx P8ZP_SCRATCH_REG
|
|
|
|
lda #<P8ZP_SCRATCH_W1
|
|
|
|
ldx end_address
|
|
|
|
ldy end_address+1
|
|
|
|
jsr c64.SAVE
|
|
|
|
php
|
|
|
|
ldx P8ZP_SCRATCH_REG
|
|
|
|
plp
|
|
|
|
}}
|
|
|
|
|
|
|
|
if_cc
|
2020-12-31 18:27:34 +00:00
|
|
|
first_byte = c64.READST()==0
|
2020-10-14 20:33:49 +00:00
|
|
|
|
2020-12-03 15:01:58 +00:00
|
|
|
c64.CLRCHN()
|
|
|
|
c64.CLOSE(1)
|
|
|
|
|
2020-12-31 18:27:34 +00:00
|
|
|
return first_byte
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 23:07:21 +00:00
|
|
|
; Use kernal LOAD routine to load the given program file in memory.
|
2022-01-03 21:44:27 +00:00
|
|
|
; This is similar to Basic's LOAD "filename",drive / LOAD "filename",drive,1
|
2021-12-03 23:07:21 +00:00
|
|
|
; If you don't give an address_override, the location in memory is taken from the 2-byte file header.
|
|
|
|
; If you specify a custom address_override, the first 2 bytes in the file are ignored
|
|
|
|
; and the rest is loaded at the given location in memory.
|
2021-12-14 22:54:42 +00:00
|
|
|
; Returns the end load address+1 if successful or 0 if a load error occurred.
|
2021-12-03 23:07:21 +00:00
|
|
|
; NOTE: when the load is larger than 64Kb and/or spans multiple RAM banks
|
2021-12-04 22:23:10 +00:00
|
|
|
; (which is possible on the Commander X16), the returned size is not correct,
|
|
|
|
; because it doesn't take the number of ram banks into account.
|
|
|
|
; Consider using cx16diskio.load() instead.
|
2020-10-14 20:33:49 +00:00
|
|
|
sub load(ubyte drivenumber, uword filenameptr, uword address_override) -> uword {
|
2021-01-07 19:01:11 +00:00
|
|
|
c64.SETNAM(string.length(filenameptr), filenameptr)
|
2020-10-14 20:33:49 +00:00
|
|
|
ubyte secondary = 1
|
2021-12-14 22:54:42 +00:00
|
|
|
cx16.r1 = 0
|
2020-10-14 20:33:49 +00:00
|
|
|
if address_override
|
|
|
|
secondary = 0
|
|
|
|
c64.SETLFS(1, drivenumber, secondary)
|
|
|
|
%asm {{
|
|
|
|
stx P8ZP_SCRATCH_REG
|
|
|
|
lda #0
|
|
|
|
ldx address_override
|
|
|
|
ldy address_override+1
|
|
|
|
jsr c64.LOAD
|
|
|
|
bcs +
|
2021-12-14 22:54:42 +00:00
|
|
|
stx cx16.r1
|
|
|
|
sty cx16.r1+1
|
2020-10-14 20:33:49 +00:00
|
|
|
+ ldx P8ZP_SCRATCH_REG
|
|
|
|
}}
|
|
|
|
|
2020-12-03 15:01:58 +00:00
|
|
|
c64.CLRCHN()
|
|
|
|
c64.CLOSE(1)
|
2021-12-14 22:54:42 +00:00
|
|
|
return cx16.r1
|
2020-10-14 20:33:49 +00:00
|
|
|
}
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2021-12-03 23:07:21 +00:00
|
|
|
; Use kernal LOAD routine to load the given file in memory.
|
|
|
|
; INCLUDING the first 2 bytes in the file: no program header is assumed in the file.
|
|
|
|
; This is different from Basic's LOAD instruction which always skips the first two bytes.
|
2021-12-14 22:54:42 +00:00
|
|
|
; The load address is mandatory.
|
|
|
|
; Returns the end load address+1 if successful or 0 if a load error occurred.
|
2021-12-03 23:07:21 +00:00
|
|
|
; NOTE: when the load is larger than 64Kb and/or spans multiple RAM banks
|
2021-12-04 22:23:10 +00:00
|
|
|
; (which is possible on the Commander X16), the returned size is not correct,
|
|
|
|
; because it doesn't take the number of ram banks into account.
|
|
|
|
; Consider using cx16diskio.load_raw() instead.
|
2021-12-03 23:07:21 +00:00
|
|
|
sub load_raw(ubyte drivenumber, uword filenameptr, uword address) -> uword {
|
|
|
|
if not f_open(drivenumber, filenameptr)
|
|
|
|
return 0
|
2021-12-14 22:54:42 +00:00
|
|
|
cx16.r1 = f_read(address, 2)
|
2021-12-03 23:07:21 +00:00
|
|
|
f_close()
|
2021-12-14 22:54:42 +00:00
|
|
|
if cx16.r1!=2
|
2021-12-03 23:07:21 +00:00
|
|
|
return 0
|
|
|
|
address += 2
|
2021-12-14 22:54:42 +00:00
|
|
|
return load(drivenumber, filenameptr, address)
|
2021-12-03 23:07:21 +00:00
|
|
|
}
|
2020-10-13 23:17:18 +00:00
|
|
|
|
|
|
|
sub delete(ubyte drivenumber, uword filenameptr) {
|
|
|
|
; -- delete a file on the drive
|
|
|
|
filename[0] = 's'
|
|
|
|
filename[1] = ':'
|
2021-01-23 21:46:46 +00:00
|
|
|
ubyte flen = string.copy(filenameptr, &filename+2)
|
2020-10-13 23:17:18 +00:00
|
|
|
c64.SETNAM(flen+2, filename)
|
|
|
|
c64.SETLFS(1, drivenumber, 15)
|
|
|
|
void c64.OPEN()
|
|
|
|
c64.CLRCHN()
|
|
|
|
c64.CLOSE(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rename(ubyte drivenumber, uword oldfileptr, uword newfileptr) {
|
|
|
|
; -- rename a file on the drive
|
|
|
|
filename[0] = 'r'
|
|
|
|
filename[1] = ':'
|
2021-01-23 21:46:46 +00:00
|
|
|
ubyte flen_new = string.copy(newfileptr, &filename+2)
|
2020-10-22 21:41:16 +00:00
|
|
|
filename[flen_new+2] = '='
|
2021-01-23 21:46:46 +00:00
|
|
|
ubyte flen_old = string.copy(oldfileptr, &filename+3+flen_new)
|
2020-10-13 23:17:18 +00:00
|
|
|
c64.SETNAM(3+flen_new+flen_old, filename)
|
|
|
|
c64.SETLFS(1, drivenumber, 15)
|
|
|
|
void c64.OPEN()
|
|
|
|
c64.CLRCHN()
|
|
|
|
c64.CLOSE(1)
|
|
|
|
}
|
2021-12-03 23:07:21 +00:00
|
|
|
|
|
|
|
str filename = "0:??????????????????????????????????????"
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|