2023-05-02 00:15:22 +00:00
|
|
|
; C64/C128 disk drive I/O routines.
|
2021-01-07 00:56:31 +00:00
|
|
|
|
2020-10-13 23:17:18 +00:00
|
|
|
%import textio
|
2023-12-17 20:21:55 +00:00
|
|
|
%import conv
|
2021-01-07 19:01:11 +00:00
|
|
|
%import string
|
2020-10-13 23:17:18 +00:00
|
|
|
%import syslib
|
|
|
|
|
|
|
|
diskio {
|
2023-12-26 22:37:59 +00:00
|
|
|
%option no_symbol_prefixing, ignore_unused
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
const ubyte READ_IO_CHANNEL=12
|
|
|
|
const ubyte WRITE_IO_CHANNEL=13
|
|
|
|
|
2023-12-28 04:17:15 +00:00
|
|
|
ubyte @shared drivenumber = 8 ; user programs can set this to the drive number they want to load/save to!
|
2023-05-01 23:48:56 +00:00
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
sub reset_read_channel() {
|
|
|
|
void cbm.CHKIN(READ_IO_CHANNEL)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub reset_write_channel() {
|
|
|
|
cbm.CHKOUT(WRITE_IO_CHANNEL)
|
|
|
|
}
|
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub directory() -> bool {
|
|
|
|
; -- Prints the directory contents to the screen. Returns success.
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(1, "$")
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0)
|
2022-07-30 11:17:20 +00:00
|
|
|
ubyte status = 1
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.OPEN() ; open 12,8,0,"$"
|
2020-10-13 23:17:18 +00:00
|
|
|
if_cs
|
|
|
|
goto io_error
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2020-10-13 23:17:18 +00:00
|
|
|
|
|
|
|
repeat 4 {
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.CHRIN() ; skip the 4 prologue bytes
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 11:07:29 +00:00
|
|
|
; while not stop key pressed / EOF encountered, read data.
|
2023-04-28 21:13:03 +00:00
|
|
|
status = cbm.READST()
|
2022-07-30 11:17:20 +00:00
|
|
|
if status!=0 {
|
|
|
|
status = 1
|
|
|
|
goto io_error
|
|
|
|
}
|
|
|
|
|
|
|
|
while status==0 {
|
2023-04-28 21:13:03 +00:00
|
|
|
ubyte low = cbm.CHRIN()
|
|
|
|
ubyte high = cbm.CHRIN()
|
2020-12-08 00:02:38 +00:00
|
|
|
txt.print_uw(mkword(high, low))
|
2021-01-14 21:51:09 +00:00
|
|
|
txt.spc()
|
2023-08-28 15:35:53 +00:00
|
|
|
ubyte @zp character
|
2020-12-22 02:35:00 +00:00
|
|
|
repeat {
|
2023-08-28 15:35:53 +00:00
|
|
|
character = cbm.CHRIN()
|
|
|
|
if character==0
|
2020-12-22 02:35:00 +00:00
|
|
|
break
|
2023-08-28 15:35:53 +00:00
|
|
|
txt.chrout(character)
|
2020-12-22 02:35:00 +00:00
|
|
|
}
|
2021-01-08 15:56:17 +00:00
|
|
|
txt.nl()
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.CHRIN() ; skip 2 bytes
|
|
|
|
void cbm.CHRIN()
|
|
|
|
status = cbm.READST()
|
|
|
|
if cbm.STOP2()
|
2020-10-13 23:17:18 +00:00
|
|
|
break
|
|
|
|
}
|
2023-04-28 21:13:03 +00:00
|
|
|
status = cbm.READST()
|
2020-10-13 23:17:18 +00:00
|
|
|
|
|
|
|
io_error:
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.CLRCHN() ; restore default i/o devices
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
if status!=0 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
|
|
|
|
}
|
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub diskname() -> uword {
|
2022-07-30 11:17:20 +00:00
|
|
|
; -- Returns pointer to disk name string or 0 if failure.
|
|
|
|
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(1, "$")
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0)
|
2024-02-04 22:22:43 +00:00
|
|
|
bool okay = false
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.OPEN() ; open 12,8,0,"$"
|
2022-07-30 11:17:20 +00:00
|
|
|
if_cs
|
|
|
|
goto io_error
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2022-07-30 11:17:20 +00:00
|
|
|
|
2023-06-07 20:31:46 +00:00
|
|
|
while cbm.CHRIN()!='"' {
|
|
|
|
; skip up to entry name
|
2022-07-30 11:17:20 +00:00
|
|
|
}
|
2023-04-28 21:13:03 +00:00
|
|
|
if cbm.READST()!=0
|
2022-07-30 11:17:20 +00:00
|
|
|
goto io_error
|
|
|
|
|
|
|
|
cx16.r0 = &list_filename
|
|
|
|
repeat {
|
2023-04-28 21:13:03 +00:00
|
|
|
@(cx16.r0) = cbm.CHRIN()
|
2023-06-07 20:31:46 +00:00
|
|
|
if @(cx16.r0)=='"' {
|
|
|
|
@(cx16.r0) = ' '
|
2023-11-10 21:27:07 +00:00
|
|
|
while @(cx16.r0)==' ' and cx16.r0>=&list_filename {
|
2023-06-07 20:31:46 +00:00
|
|
|
@(cx16.r0) = 0
|
|
|
|
cx16.r0--
|
|
|
|
}
|
2022-07-30 11:17:20 +00:00
|
|
|
break
|
2023-06-07 20:31:46 +00:00
|
|
|
}
|
2022-07-30 11:17:20 +00:00
|
|
|
cx16.r0++
|
|
|
|
}
|
|
|
|
okay = true
|
|
|
|
|
|
|
|
io_error:
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.CLRCHN() ; restore default i/o devices
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2022-07-30 11:17:20 +00:00
|
|
|
if okay
|
|
|
|
return &list_filename
|
|
|
|
return 0
|
|
|
|
}
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2020-12-09 23:58:59 +00:00
|
|
|
; internal variables for the iterative file lister / loader
|
2022-07-08 19:50:32 +00:00
|
|
|
bool list_skip_disk_name
|
2020-12-08 02:28:29 +00:00
|
|
|
uword list_pattern
|
|
|
|
uword list_blocks
|
2022-07-08 19:50:32 +00:00
|
|
|
bool iteration_in_progress = false
|
2022-11-20 19:22:09 +00:00
|
|
|
str list_filetype = "???" ; prg, seq, dir
|
2022-08-01 22:58:32 +00:00
|
|
|
str list_filename = "?" * 50
|
2020-12-11 21:36:14 +00:00
|
|
|
|
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
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub list_filenames(uword pattern_ptr, uword filenames_buffer, uword filenames_buf_size) -> ubyte {
|
2022-11-20 22:27:22 +00:00
|
|
|
; -- fill the provided buffer with the names of the files on the disk (until buffer is full).
|
2023-10-12 20:29:01 +00:00
|
|
|
; Files in the buffer are separated by a 0 byte. You can provide an optional pattern to match against.
|
2022-11-20 22:27:22 +00:00
|
|
|
; After the last filename one additional 0 byte is placed to indicate the end of the list.
|
|
|
|
; Returns number of files (it skips 'dir' entries i.e. subdirectories).
|
|
|
|
; Also sets carry on exit: Carry clear = all files returned, Carry set = directory has more files that didn't fit in the buffer.
|
2022-08-01 20:58:03 +00:00
|
|
|
uword buffer_start = filenames_buffer
|
2020-12-11 21:36:14 +00:00
|
|
|
ubyte files_found = 0
|
2023-05-01 23:48:56 +00:00
|
|
|
if lf_start_list(pattern_ptr) {
|
2020-12-11 21:36:14 +00:00
|
|
|
while lf_next_entry() {
|
2022-11-20 19:22:09 +00:00
|
|
|
if list_filetype!="dir" {
|
2023-05-02 01:11:09 +00:00
|
|
|
filenames_buffer += string.copy(list_filename, filenames_buffer) + 1
|
2022-11-20 19:22:09 +00:00
|
|
|
files_found++
|
2022-11-20 22:27:22 +00:00
|
|
|
if filenames_buffer - buffer_start > filenames_buf_size-20 {
|
|
|
|
@(filenames_buffer)=0
|
|
|
|
lf_end_list()
|
|
|
|
sys.set_carry()
|
|
|
|
return files_found
|
|
|
|
}
|
2022-11-20 19:22:09 +00:00
|
|
|
}
|
2020-12-11 21:36:14 +00:00
|
|
|
}
|
|
|
|
lf_end_list()
|
|
|
|
}
|
2022-11-20 22:27:22 +00:00
|
|
|
@(filenames_buffer)=0
|
|
|
|
sys.clear_carry()
|
2020-12-11 21:36:14 +00:00
|
|
|
return files_found
|
|
|
|
}
|
2020-12-09 23:58:59 +00:00
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
; ----- iterative file lister functions (uses the read io channel) -----
|
2020-12-09 23:58:59 +00:00
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub lf_start_list(uword pattern_ptr) -> bool {
|
2020-12-30 22:34:00 +00:00
|
|
|
; -- 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
|
|
|
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(1, "$")
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0)
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.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
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2020-12-07 22:29:34 +00:00
|
|
|
|
|
|
|
repeat 4 {
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.CHRIN() ; skip the 4 prologue bytes
|
2020-12-07 22:29:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 21:13:03 +00:00
|
|
|
if cbm.READST()==0
|
2020-12-08 02:28:29 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
io_error:
|
2023-12-17 20:21:55 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2020-12-08 02:28:29 +00:00
|
|
|
lf_end_list()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:50:32 +00:00
|
|
|
sub lf_next_entry() -> bool {
|
2020-12-08 02:28:29 +00:00
|
|
|
; -- retrieve the next entry from an iterative file listing session.
|
2022-11-20 19:22:09 +00:00
|
|
|
; results will be found in list_blocks, list_filename, and list_filetype.
|
2020-12-08 02:28:29 +00:00
|
|
|
; 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 {
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel() ; use the input io channel again
|
2020-12-11 21:36:14 +00:00
|
|
|
|
2020-12-08 02:28:29 +00:00
|
|
|
uword nameptr = &list_filename
|
2023-04-28 21:13:03 +00:00
|
|
|
ubyte blocks_lsb = cbm.CHRIN()
|
|
|
|
ubyte blocks_msb = cbm.CHRIN()
|
2020-12-11 21:36:14 +00:00
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
if cbm.READST()!=0
|
2020-12-11 21:36:14 +00:00
|
|
|
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 "
|
2023-04-28 21:13:03 +00:00
|
|
|
while cbm.CHRIN()!='\"' {
|
2024-02-04 22:22:43 +00:00
|
|
|
if cbm.READST()!=0
|
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 {
|
2023-08-28 15:35:53 +00:00
|
|
|
ubyte character = cbm.CHRIN()
|
|
|
|
if character==0
|
2020-12-08 00:34:08 +00:00
|
|
|
break
|
2023-08-28 15:35:53 +00:00
|
|
|
if character=='\"'
|
2020-12-08 00:02:38 +00:00
|
|
|
break
|
2023-08-28 15:35:53 +00:00
|
|
|
@(nameptr) = character
|
2020-12-08 02:28:29 +00:00
|
|
|
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
|
|
|
|
2022-11-20 19:22:09 +00:00
|
|
|
do {
|
2023-04-28 21:13:03 +00:00
|
|
|
cx16.r15L = cbm.CHRIN()
|
2022-11-20 19:22:09 +00:00
|
|
|
} until cx16.r15L!=' ' ; skip blanks up to 3 chars entry type
|
|
|
|
list_filetype[0] = cx16.r15L
|
2023-04-28 21:13:03 +00:00
|
|
|
list_filetype[1] = cbm.CHRIN()
|
|
|
|
list_filetype[2] = cbm.CHRIN()
|
2024-02-04 22:22:43 +00:00
|
|
|
while cbm.CHRIN()!=0 {
|
2020-12-08 21:10:12 +00:00
|
|
|
; read the rest of the entry until the end
|
|
|
|
}
|
2020-12-08 00:02:38 +00:00
|
|
|
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.CHRIN() ; skip 2 bytes
|
|
|
|
void cbm.CHRIN()
|
2020-12-08 02:28:29 +00:00
|
|
|
|
|
|
|
if not list_skip_disk_name {
|
2024-02-04 22:22:43 +00:00
|
|
|
if list_pattern==0
|
2020-12-30 22:34:00 +00:00
|
|
|
return true
|
2022-03-21 20:19:32 +00:00
|
|
|
if string.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 {
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.CLRCHN()
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2020-12-09 23:02:21 +00:00
|
|
|
iteration_in_progress = false
|
2020-12-08 02:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
; ----- iterative file loader functions (uses the input io channel) -----
|
2020-12-09 23:58:59 +00:00
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub f_open(uword filenameptr) -> bool {
|
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!
|
2023-11-17 23:27:45 +00:00
|
|
|
; Returns true if the file is successfully opened and readable.
|
|
|
|
; No need to check status(), unlike f_open_w() !
|
2023-11-29 20:04:31 +00:00
|
|
|
; NOTE: the default input isn't yet set to this logical file, you must use reset_read_channel() to do this,
|
|
|
|
; if you're going to read from it yourself instead of using f_read()!
|
2020-12-09 23:58:59 +00:00
|
|
|
f_close()
|
|
|
|
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(string.length(filenameptr), filenameptr)
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, READ_IO_CHANNEL) ; note: has to be Channel,x,Channel because otherwise f_seek doesn't work
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.OPEN() ; open 12,8,12,"filename"
|
2020-12-09 23:58:59 +00:00
|
|
|
if_cc {
|
2023-12-17 20:21:55 +00:00
|
|
|
reset_read_channel()
|
2023-04-28 21:13:03 +00:00
|
|
|
if cbm.READST()==0 {
|
2022-07-15 20:21:34 +00:00
|
|
|
iteration_in_progress = true
|
2023-12-17 20:21:55 +00:00
|
|
|
void cbm.CHRIN() ; read first byte to test for file not found
|
|
|
|
if cbm.READST()==0 {
|
|
|
|
cbm.CLOSE(READ_IO_CHANNEL) ; close file because we already consumed first byte
|
|
|
|
void cbm.OPEN() ; re-open the file
|
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
|
|
|
return true
|
2020-12-31 18:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-09 23:58:59 +00:00
|
|
|
}
|
|
|
|
f_close()
|
2023-12-17 20:21:55 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2020-12-09 23:58:59 +00:00
|
|
|
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)
|
2024-02-04 22:22:43 +00:00
|
|
|
if not iteration_in_progress or num_bytes==0
|
2020-12-09 23:58:59 +00:00
|
|
|
return 0
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2020-12-31 18:27:34 +00:00
|
|
|
list_blocks = 0 ; we reuse this variable for the total number of bytes read
|
2022-06-01 22:37:18 +00:00
|
|
|
|
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
|
|
|
}}
|
2024-02-04 22:22:43 +00:00
|
|
|
while num_bytes!=0 {
|
|
|
|
if cbm.READST()!=0 {
|
2022-11-23 22:20:24 +00:00
|
|
|
f_close()
|
2024-02-04 22:22:43 +00:00
|
|
|
if cbm.READST() & $40 !=0 ; eof?
|
2022-11-23 22:20:24 +00:00
|
|
|
return list_blocks ; number of bytes read
|
|
|
|
return 0 ; error.
|
|
|
|
}
|
2020-12-31 15:46:24 +00:00
|
|
|
%asm {{
|
2023-04-28 21:13:03 +00:00
|
|
|
jsr cbm.CHRIN
|
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 15:46:24 +00:00
|
|
|
+
|
|
|
|
}}
|
2022-11-23 22:20:24 +00:00
|
|
|
list_blocks++
|
|
|
|
num_bytes--
|
2020-12-31 18:27:34 +00:00
|
|
|
}
|
2022-05-31 23:09:37 +00:00
|
|
|
return list_blocks ; number of bytes read
|
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
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2022-05-31 23:09:37 +00:00
|
|
|
uword total_read = 0
|
2024-02-04 22:22:43 +00:00
|
|
|
while cbm.READST()==0 {
|
2022-11-23 22:20:24 +00:00
|
|
|
cx16.r0 = f_read(bufferpointer, 256)
|
|
|
|
total_read += cx16.r0
|
|
|
|
bufferpointer += cx16.r0
|
2020-12-24 05:24:52 +00:00
|
|
|
}
|
2022-05-31 23:09:37 +00:00
|
|
|
return total_read
|
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
|
2023-09-28 21:29:21 +00:00
|
|
|
jsr reset_read_channel
|
2021-01-10 04:04:56 +00:00
|
|
|
ldy #0
|
2023-04-28 21:13:03 +00:00
|
|
|
_loop jsr cbm.CHRIN
|
2021-01-10 04:04:56 +00:00
|
|
|
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-09 23:58:59 +00:00
|
|
|
sub f_close() {
|
|
|
|
; -- end an iterative file loading session (close channels).
|
|
|
|
if iteration_in_progress {
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.CLRCHN()
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2020-12-09 23:58:59 +00:00
|
|
|
iteration_in_progress = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
; ----- iterative file writing functions (uses write io channel) -----
|
2021-05-01 17:13:56 +00:00
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub f_open_w(uword filenameptr) -> bool {
|
2021-05-01 17:13:56 +00:00
|
|
|
; -- open a file for iterative writing with f_write
|
2023-11-17 23:27:45 +00:00
|
|
|
; WARNING: returns true if the open command was received by the device,
|
|
|
|
; but this can still mean the file wasn't successfully opened for writing!
|
|
|
|
; (for example, if it already exists). This is different than f_open()!
|
|
|
|
; To be 100% sure if this call was successful, you have to use status()
|
|
|
|
; and check the drive's status message!
|
2021-05-01 17:13:56 +00:00
|
|
|
f_close_w()
|
|
|
|
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(string.length(filenameptr), filenameptr)
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.SETLFS(WRITE_IO_CHANNEL, drivenumber, 1)
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.OPEN() ; open 13,8,1,"filename"
|
2023-09-28 21:29:21 +00:00
|
|
|
if_cc
|
2024-02-04 22:22:43 +00:00
|
|
|
return cbm.READST()==0
|
2023-12-17 20:21:55 +00:00
|
|
|
cbm.CLOSE(WRITE_IO_CHANNEL)
|
2021-05-01 17:13:56 +00:00
|
|
|
f_close_w()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:50:32 +00:00
|
|
|
sub f_write(uword bufferpointer, uword num_bytes) -> bool {
|
2022-11-23 00:14:43 +00:00
|
|
|
; -- write the given number of bytes to the currently open file
|
2021-05-01 17:13:56 +00:00
|
|
|
if num_bytes!=0 {
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_write_channel()
|
2021-05-01 17:13:56 +00:00
|
|
|
repeat num_bytes {
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.CHROUT(@(bufferpointer))
|
2021-05-01 17:13:56 +00:00
|
|
|
bufferpointer++
|
|
|
|
}
|
2024-02-04 22:22:43 +00:00
|
|
|
return cbm.READST()==0
|
2021-05-01 17:13:56 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
sub f_close_w() {
|
|
|
|
; -- end an iterative file writing session (close channels).
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.CLRCHN()
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(WRITE_IO_CHANNEL)
|
2021-05-01 17:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
; ---- other functions ----
|
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub status() -> uword {
|
2020-12-09 23:02:21 +00:00
|
|
|
; -- retrieve the disk drive's current status message
|
2023-12-17 20:21:55 +00:00
|
|
|
str device_not_present_error = "device not present #xx"
|
|
|
|
if cbm.READST()==128 {
|
|
|
|
device_not_present_error[len(device_not_present_error)-2] = 0
|
2024-03-08 00:56:35 +00:00
|
|
|
void string.copy(conv.str_ub(drivenumber), &device_not_present_error+len(device_not_present_error)-2)
|
2023-12-17 20:21:55 +00:00
|
|
|
return device_not_present_error
|
|
|
|
}
|
2022-08-01 22:58:32 +00:00
|
|
|
uword messageptr = &list_filename
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(0, list_filename)
|
|
|
|
cbm.SETLFS(15, drivenumber, 15)
|
|
|
|
void cbm.OPEN() ; open 15,8,15
|
2020-10-13 23:17:18 +00:00
|
|
|
if_cs
|
|
|
|
goto io_error
|
2023-04-28 21:13:03 +00:00
|
|
|
void cbm.CHKIN(15) ; use #15 as input channel
|
2020-10-13 23:17:18 +00:00
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
while cbm.READST()==0 {
|
2023-04-28 21:13:03 +00:00
|
|
|
cx16.r5L = cbm.CHRIN()
|
2022-11-23 01:43:22 +00:00
|
|
|
if cx16.r5L=='\r' or cx16.r5L=='\n'
|
2022-07-15 20:21:34 +00:00
|
|
|
break
|
2022-11-23 01:43:22 +00:00
|
|
|
@(messageptr) = cx16.r5L
|
2020-12-09 23:02:21 +00:00
|
|
|
messageptr++
|
|
|
|
}
|
|
|
|
@(messageptr) = 0
|
2022-07-15 20:21:34 +00:00
|
|
|
|
2021-05-01 13:20:11 +00:00
|
|
|
done:
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.CLRCHN() ; restore default i/o devices
|
|
|
|
cbm.CLOSE(15)
|
2022-08-01 22:58:32 +00:00
|
|
|
return list_filename
|
2021-05-01 13:20:11 +00:00
|
|
|
|
|
|
|
io_error:
|
2023-12-17 20:21:55 +00:00
|
|
|
cbm.CLOSE(15)
|
|
|
|
list_filename = "io error"
|
2021-05-01 13:20:11 +00:00
|
|
|
goto done
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 19:05:39 +00:00
|
|
|
; similar to above, but instead of fetching the entire string, it only fetches the status code and returns it as ubyte
|
|
|
|
; in case of IO error, returns 255 (CBM-DOS itself is physically unable to return such a value)
|
|
|
|
sub status_code() -> ubyte {
|
|
|
|
if cbm.READST()==128 {
|
|
|
|
return 255
|
|
|
|
}
|
|
|
|
|
|
|
|
cbm.SETNAM(0, list_filename)
|
|
|
|
cbm.SETLFS(15, drivenumber, 15)
|
|
|
|
void cbm.OPEN() ; open 15,8,15
|
|
|
|
if_cs
|
|
|
|
goto io_error
|
|
|
|
void cbm.CHKIN(15) ; use #15 as input channel
|
|
|
|
|
|
|
|
list_filename[0] = cbm.CHRIN()
|
|
|
|
list_filename[1] = cbm.CHRIN()
|
|
|
|
list_filename[2] = 0
|
|
|
|
|
|
|
|
while cbm.READST()==0 {
|
|
|
|
cbm.CHRIN()
|
|
|
|
}
|
|
|
|
|
|
|
|
cbm.CLRCHN() ; restore default i/o devices
|
|
|
|
cbm.CLOSE(15)
|
|
|
|
return conv.str2ubyte(list_filename)
|
|
|
|
|
|
|
|
io_error:
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(15)
|
|
|
|
return 255
|
|
|
|
}
|
|
|
|
|
2023-08-28 15:35:53 +00:00
|
|
|
sub save(uword filenameptr, uword start_address, uword savesize) -> bool {
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(string.length(filenameptr), filenameptr)
|
|
|
|
cbm.SETLFS(1, drivenumber, 0)
|
2023-08-28 15:35:53 +00:00
|
|
|
uword @shared end_address = start_address + savesize
|
2022-11-23 01:43:22 +00:00
|
|
|
cx16.r0L = 0
|
2020-10-14 20:33:49 +00:00
|
|
|
|
|
|
|
%asm {{
|
2023-08-28 15:35:53 +00:00
|
|
|
lda start_address
|
2020-10-14 20:33:49 +00:00
|
|
|
sta P8ZP_SCRATCH_W1
|
2023-08-28 15:35:53 +00:00
|
|
|
lda start_address+1
|
2020-10-14 20:33:49 +00:00
|
|
|
sta P8ZP_SCRATCH_W1+1
|
|
|
|
lda #<P8ZP_SCRATCH_W1
|
|
|
|
ldx end_address
|
|
|
|
ldy end_address+1
|
2023-04-28 21:13:03 +00:00
|
|
|
jsr cbm.SAVE
|
2020-10-14 20:33:49 +00:00
|
|
|
php
|
|
|
|
plp
|
|
|
|
}}
|
|
|
|
|
|
|
|
if_cc
|
2024-02-04 22:22:43 +00:00
|
|
|
cx16.r0L = cbm.READST()==0 as ubyte
|
2020-10-14 20:33:49 +00:00
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
return cx16.r0L as bool
|
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.
|
2023-05-01 23:48:56 +00:00
|
|
|
sub load(uword filenameptr, uword address_override) -> uword {
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.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
|
2024-02-04 22:22:43 +00:00
|
|
|
if address_override!=0
|
2020-10-14 20:33:49 +00:00
|
|
|
secondary = 0
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETLFS(1, drivenumber, secondary)
|
2020-10-14 20:33:49 +00:00
|
|
|
%asm {{
|
|
|
|
lda #0
|
|
|
|
ldx address_override
|
|
|
|
ldy address_override+1
|
2023-04-28 21:13:03 +00:00
|
|
|
jsr cbm.LOAD
|
2020-10-14 20:33:49 +00:00
|
|
|
bcs +
|
2021-12-14 22:54:42 +00:00
|
|
|
stx cx16.r1
|
|
|
|
sty cx16.r1+1
|
2023-07-15 13:18:26 +00:00
|
|
|
+
|
2020-10-14 20:33:49 +00:00
|
|
|
}}
|
|
|
|
|
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
|
|
|
|
2023-05-02 01:11:09 +00:00
|
|
|
; Identical to load(), but DOES INCLUDE the first 2 bytes in the file.
|
|
|
|
; No program header is assumed in the file. Everything is loaded.
|
|
|
|
; See comments on load() for more details.
|
2023-08-28 15:35:53 +00:00
|
|
|
sub load_raw(uword filenameptr, uword start_address) -> uword {
|
2023-05-02 00:15:22 +00:00
|
|
|
; read the 2 header bytes separately to skip them
|
|
|
|
if not f_open(filenameptr)
|
|
|
|
return 0
|
2023-08-28 15:35:53 +00:00
|
|
|
cx16.r1 = f_read(start_address, 2)
|
2023-05-02 00:15:22 +00:00
|
|
|
f_close()
|
|
|
|
if cx16.r1!=2
|
|
|
|
return 0
|
2023-08-28 15:35:53 +00:00
|
|
|
start_address += 2
|
|
|
|
return load(filenameptr, start_address)
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub delete(uword filenameptr) {
|
2020-10-13 23:17:18 +00:00
|
|
|
; -- delete a file on the drive
|
2022-08-01 22:58:32 +00:00
|
|
|
list_filename[0] = 's'
|
|
|
|
list_filename[1] = ':'
|
|
|
|
ubyte flen = string.copy(filenameptr, &list_filename+2)
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(flen+2, list_filename)
|
|
|
|
cbm.SETLFS(1, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(1)
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub rename(uword oldfileptr, uword newfileptr) {
|
2020-10-13 23:17:18 +00:00
|
|
|
; -- rename a file on the drive
|
2022-08-01 22:58:32 +00:00
|
|
|
list_filename[0] = 'r'
|
|
|
|
list_filename[1] = ':'
|
|
|
|
ubyte flen_new = string.copy(newfileptr, &list_filename+2)
|
|
|
|
list_filename[flen_new+2] = '='
|
|
|
|
ubyte flen_old = string.copy(oldfileptr, &list_filename+3+flen_new)
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(3+flen_new+flen_old, list_filename)
|
|
|
|
cbm.SETLFS(1, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(1)
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|
2021-12-03 23:07:21 +00:00
|
|
|
|
2023-05-01 23:48:56 +00:00
|
|
|
sub send_command(uword commandptr) {
|
2022-08-01 20:58:03 +00:00
|
|
|
; -- send a dos command to the drive
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.SETNAM(string.length(commandptr), commandptr)
|
|
|
|
cbm.SETLFS(15, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(15)
|
2022-08-01 20:58:03 +00:00
|
|
|
}
|
2020-10-13 23:17:18 +00:00
|
|
|
}
|