2023-05-02 00:15:22 +00:00
|
|
|
; Commander X16 disk drive I/O routines.
|
|
|
|
; Largely compatible with the default C64 ones, but adds more stuff specific to the X16 as well.
|
|
|
|
|
2023-10-21 20:25:38 +00:00
|
|
|
; NOTE: If you experience weird behavior with these routines and you are using them
|
|
|
|
; in the X16 emulator using HostFs, please try again with an SD-card image instead first.
|
|
|
|
; It is possible that there are still small differences between HostFS and actual CBM DOS in the emulator.
|
2023-11-11 13:26:40 +00:00
|
|
|
;
|
|
|
|
; About the secondary addresses:
|
|
|
|
; for writes (new files or overwrites), you can use 1 (without the mode string) or 2-14 (with the mode string)
|
|
|
|
; for reads (existing files) you can use 0 or 2-14 (mode string is optional)
|
|
|
|
; for modify mode (new files* or existing files), you must use 2-14, and the mode string ,s,m is required
|
|
|
|
|
2023-10-21 20:25:38 +00:00
|
|
|
|
2023-05-02 00:15:22 +00:00
|
|
|
%import textio
|
2023-12-17 20:21:55 +00:00
|
|
|
%import conv
|
2024-11-23 14:51:38 +00:00
|
|
|
%import strings
|
2023-05-02 00:15:22 +00:00
|
|
|
%import syslib
|
|
|
|
|
|
|
|
diskio {
|
2023-12-26 22:37:59 +00:00
|
|
|
%option no_symbol_prefixing, ignore_unused
|
2023-05-02 00:15:22 +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-02 00:15:22 +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)
|
|
|
|
}
|
|
|
|
|
2024-04-05 22:04:54 +00:00
|
|
|
sub fastmode(ubyte mode) -> bool {
|
|
|
|
; -- set fast serial mode (0=none, 1=auto_tx, 2=fast writes, 3=both) for the SD card.
|
|
|
|
; Returns success status (fails on emulator host fs for example)
|
2024-04-02 20:17:51 +00:00
|
|
|
list_filename[0] = 'u'
|
|
|
|
list_filename[1] = '0'
|
|
|
|
list_filename[2] = '>'
|
|
|
|
list_filename[3] = 'b'
|
|
|
|
list_filename[4] = mode | $30
|
|
|
|
list_filename[5] = 0
|
|
|
|
send_command(list_filename)
|
2024-04-05 22:04:54 +00:00
|
|
|
return status_code()==0
|
2024-04-02 20:17:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 00:15:22 +00:00
|
|
|
sub directory() -> bool {
|
|
|
|
; -- Prints the directory contents to the screen. Returns success.
|
|
|
|
|
|
|
|
cbm.SETNAM(1, "$")
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0)
|
2023-05-02 00:15:22 +00:00
|
|
|
ubyte status = 1
|
|
|
|
void cbm.OPEN() ; open 12,8,0,"$"
|
|
|
|
if_cs
|
|
|
|
goto io_error
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2023-05-02 00:15:22 +00:00
|
|
|
|
|
|
|
repeat 4 {
|
|
|
|
void cbm.CHRIN() ; skip the 4 prologue bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
; while not stop key pressed / EOF encountered, read data.
|
|
|
|
status = cbm.READST()
|
|
|
|
if status!=0 {
|
|
|
|
status = 1
|
|
|
|
goto io_error
|
|
|
|
}
|
|
|
|
|
|
|
|
while status==0 {
|
|
|
|
ubyte low = cbm.CHRIN()
|
|
|
|
ubyte high = cbm.CHRIN()
|
|
|
|
txt.print_uw(mkword(high, low))
|
|
|
|
txt.spc()
|
2023-08-28 15:35:53 +00:00
|
|
|
ubyte @zp character
|
2023-05-02 00:15:22 +00:00
|
|
|
repeat {
|
2023-08-28 15:35:53 +00:00
|
|
|
character = cbm.CHRIN()
|
|
|
|
if character==0
|
2023-05-02 00:15:22 +00:00
|
|
|
break
|
2023-08-28 15:35:53 +00:00
|
|
|
txt.chrout(character)
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
txt.nl()
|
|
|
|
void cbm.CHRIN() ; skip 2 bytes
|
|
|
|
void cbm.CHRIN()
|
|
|
|
status = cbm.READST()
|
2024-04-03 22:18:53 +00:00
|
|
|
void cbm.STOP()
|
|
|
|
if_z
|
2023-05-02 00:15:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
status = cbm.READST()
|
|
|
|
|
|
|
|
io_error:
|
|
|
|
cbm.CLRCHN() ; restore default i/o devices
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2023-05-02 00:15:22 +00:00
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
if status!=0 and status & $40 == 0 { ; bit 6=end of file
|
2023-05-02 00:15:22 +00:00
|
|
|
txt.print("\ni/o error, status: ")
|
|
|
|
txt.print_ub(status)
|
|
|
|
txt.nl()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
sub diskname() -> uword {
|
2023-06-07 20:31:46 +00:00
|
|
|
; returns disk label name or 0 if error
|
|
|
|
cbm.SETNAM(3, "$")
|
2023-11-10 21:27:07 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0)
|
2023-06-07 20:31:46 +00:00
|
|
|
ubyte status = 1
|
|
|
|
void cbm.OPEN() ; open 12,8,0,"$=c"
|
2023-05-02 00:15:22 +00:00
|
|
|
if_cs
|
|
|
|
goto io_error
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2023-05-02 00:15:22 +00:00
|
|
|
|
2023-06-07 20:31:46 +00:00
|
|
|
while cbm.CHRIN()!='"' {
|
|
|
|
; skip up to entry name
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 21:27:07 +00:00
|
|
|
cx16.r0 = &list_filename
|
2023-05-02 00:15:22 +00:00
|
|
|
repeat {
|
|
|
|
@(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--
|
|
|
|
}
|
2023-05-02 00:15:22 +00:00
|
|
|
break
|
2023-06-07 20:31:46 +00:00
|
|
|
}
|
2023-05-02 00:15:22 +00:00
|
|
|
cx16.r0++
|
|
|
|
}
|
2023-06-07 20:31:46 +00:00
|
|
|
status = cbm.READST()
|
2023-05-02 00:15:22 +00:00
|
|
|
|
|
|
|
io_error:
|
2023-06-07 20:31:46 +00:00
|
|
|
cbm.CLRCHN()
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2024-02-04 22:22:43 +00:00
|
|
|
if status!=0 and status & $40 == 0
|
2023-06-07 20:31:46 +00:00
|
|
|
return 0
|
2023-11-10 21:27:07 +00:00
|
|
|
return list_filename
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
; internal variables for the iterative file lister / loader
|
|
|
|
bool list_skip_disk_name
|
|
|
|
uword list_pattern
|
|
|
|
uword list_blocks
|
|
|
|
bool iteration_in_progress = false
|
2024-10-31 19:08:10 +00:00
|
|
|
bool write_iteration_in_progress = false
|
2023-05-02 00:15:22 +00:00
|
|
|
str list_filetype = "???" ; prg, seq, dir
|
|
|
|
str list_filename = "?" * 50
|
|
|
|
|
|
|
|
; ----- get a list of files (uses iteration functions internally) -----
|
|
|
|
|
|
|
|
sub list_filenames(uword pattern_ptr, uword filenames_buffer, uword filenames_buf_size) -> ubyte {
|
|
|
|
; -- 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.
|
2023-05-02 00:15: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.
|
|
|
|
uword buffer_start = filenames_buffer
|
|
|
|
ubyte files_found = 0
|
2023-12-09 15:54:40 +00:00
|
|
|
filenames_buffer[0]=0
|
2023-05-02 00:15:22 +00:00
|
|
|
if lf_start_list(pattern_ptr) {
|
|
|
|
while lf_next_entry() {
|
|
|
|
if list_filetype!="dir" {
|
2024-11-23 14:51:38 +00:00
|
|
|
filenames_buffer += strings.copy(list_filename, filenames_buffer) + 1
|
2023-05-02 00:15:22 +00:00
|
|
|
files_found++
|
|
|
|
if filenames_buffer - buffer_start > filenames_buf_size-20 {
|
|
|
|
@(filenames_buffer)=0
|
|
|
|
lf_end_list()
|
|
|
|
sys.set_carry()
|
|
|
|
return files_found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lf_end_list()
|
|
|
|
}
|
|
|
|
@(filenames_buffer)=0
|
|
|
|
sys.clear_carry()
|
|
|
|
return files_found
|
|
|
|
}
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
; ----- iterative file lister functions (uses the read io channel) -----
|
2023-05-02 00:15:22 +00:00
|
|
|
|
|
|
|
sub lf_start_list(uword pattern_ptr) -> bool {
|
|
|
|
; -- start an iterative file listing with optional pattern matching.
|
|
|
|
; note: only a single iteration loop can be active at a time!
|
|
|
|
lf_end_list()
|
|
|
|
list_pattern = pattern_ptr
|
|
|
|
list_skip_disk_name = true
|
|
|
|
iteration_in_progress = true
|
|
|
|
|
|
|
|
cbm.SETNAM(1, "$")
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0)
|
2023-05-02 00:15:22 +00:00
|
|
|
void cbm.OPEN() ; open 12,8,0,"$"
|
|
|
|
if_cs
|
|
|
|
goto io_error
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2023-05-02 00:15:22 +00:00
|
|
|
|
|
|
|
repeat 4 {
|
|
|
|
void cbm.CHRIN() ; skip the 4 prologue bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
if cbm.READST()==0
|
|
|
|
return true
|
|
|
|
|
|
|
|
io_error:
|
2023-12-17 20:21:55 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2023-05-02 00:15:22 +00:00
|
|
|
lf_end_list()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lf_next_entry() -> bool {
|
|
|
|
; -- retrieve the next entry from an iterative file listing session.
|
|
|
|
; results will be found in list_blocks, list_filename, and list_filetype.
|
|
|
|
; if it returns false though, there are no more entries (or an error occurred).
|
|
|
|
|
|
|
|
if not iteration_in_progress
|
|
|
|
return false
|
|
|
|
|
|
|
|
repeat {
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel() ; use the input io channel again
|
2023-05-02 00:15:22 +00:00
|
|
|
|
|
|
|
uword nameptr = &list_filename
|
|
|
|
ubyte blocks_lsb = cbm.CHRIN()
|
|
|
|
ubyte blocks_msb = cbm.CHRIN()
|
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
if cbm.READST()!=0
|
2023-05-02 00:15:22 +00:00
|
|
|
goto close_end
|
|
|
|
|
|
|
|
list_blocks = mkword(blocks_msb, blocks_lsb)
|
|
|
|
|
|
|
|
; read until the filename starts after the first "
|
|
|
|
while cbm.CHRIN()!='\"' {
|
2024-02-04 22:22:43 +00:00
|
|
|
if cbm.READST()!=0
|
2023-05-02 00:15:22 +00:00
|
|
|
goto close_end
|
|
|
|
}
|
|
|
|
|
|
|
|
; read the filename
|
|
|
|
repeat {
|
2023-08-28 15:35:53 +00:00
|
|
|
ubyte character = cbm.CHRIN()
|
|
|
|
if character==0
|
2023-05-02 00:15:22 +00:00
|
|
|
break
|
2023-08-28 15:35:53 +00:00
|
|
|
if character=='\"'
|
2023-05-02 00:15:22 +00:00
|
|
|
break
|
2023-08-28 15:35:53 +00:00
|
|
|
@(nameptr) = character
|
2023-05-02 00:15:22 +00:00
|
|
|
nameptr++
|
|
|
|
}
|
|
|
|
|
|
|
|
@(nameptr) = 0
|
|
|
|
|
|
|
|
do {
|
|
|
|
cx16.r15L = cbm.CHRIN()
|
|
|
|
} until cx16.r15L!=' ' ; skip blanks up to 3 chars entry type
|
|
|
|
list_filetype[0] = cx16.r15L
|
|
|
|
list_filetype[1] = cbm.CHRIN()
|
|
|
|
list_filetype[2] = cbm.CHRIN()
|
2024-02-04 22:22:43 +00:00
|
|
|
while cbm.CHRIN()!=0 {
|
2023-05-02 00:15:22 +00:00
|
|
|
; read the rest of the entry until the end
|
|
|
|
}
|
|
|
|
|
|
|
|
void cbm.CHRIN() ; skip 2 bytes
|
|
|
|
void cbm.CHRIN()
|
|
|
|
|
|
|
|
if not list_skip_disk_name {
|
2024-02-04 22:22:43 +00:00
|
|
|
if list_pattern==0
|
2023-05-02 00:15:22 +00:00
|
|
|
return true
|
2024-11-23 14:51:38 +00:00
|
|
|
if strings.pattern_match(list_filename, list_pattern)
|
2023-05-02 00:15:22 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
list_skip_disk_name = false
|
|
|
|
}
|
|
|
|
|
|
|
|
close_end:
|
|
|
|
lf_end_list()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lf_end_list() {
|
|
|
|
; -- end an iterative file listing session (close channels).
|
|
|
|
if iteration_in_progress {
|
|
|
|
cbm.CLRCHN()
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2023-05-02 00:15:22 +00:00
|
|
|
iteration_in_progress = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
; ----- iterative file loader functions (uses the read io channel) -----
|
2023-05-02 00:15:22 +00:00
|
|
|
|
2023-11-12 19:40:17 +00:00
|
|
|
sub f_open(str filenameptr) -> bool {
|
2023-05-02 00:15:22 +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-12 19:40:17 +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()!
|
2023-05-02 00:15:22 +00:00
|
|
|
f_close()
|
|
|
|
|
2024-11-23 14:51:38 +00:00
|
|
|
cbm.SETNAM(strings.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-05-02 00:15:22 +00:00
|
|
|
void cbm.OPEN() ; open 12,8,12,"filename"
|
|
|
|
if_cc {
|
2023-12-17 20:21:55 +00:00
|
|
|
reset_read_channel()
|
2023-05-02 00:15:22 +00:00
|
|
|
if cbm.READST()==0 {
|
|
|
|
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
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f_close()
|
2023-12-17 20:21:55 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2023-05-02 00:15:22 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-02 01:11:09 +00:00
|
|
|
; optimized for Commander X16 to use MACPTR block read kernal call
|
2023-05-02 00:15:22 +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)
|
2023-10-09 20:10:36 +00:00
|
|
|
; NOTE: cannot be used to load into VRAM. Use vload() or call cx16.MACPTR() yourself with the vera data register as address.
|
2024-02-04 22:22:43 +00:00
|
|
|
if not iteration_in_progress or num_bytes==0
|
2023-05-02 00:15:22 +00:00
|
|
|
return 0
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2024-12-02 20:25:38 +00:00
|
|
|
if num_bytes==1 {
|
|
|
|
; optimize for reading just a single byte
|
|
|
|
@(bufferpointer) = cbm.CHRIN()
|
|
|
|
cx16.r0L = cbm.READST()
|
2024-12-03 21:51:56 +00:00
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
2024-12-02 20:25:38 +00:00
|
|
|
if cx16.r0L!=0 {
|
|
|
|
f_close()
|
|
|
|
if cx16.r0L & $40 == 0
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
2023-05-02 00:15:22 +00:00
|
|
|
|
2024-12-02 20:25:38 +00:00
|
|
|
list_blocks = 0 ; we reuse this variable for the total number of bytes read
|
2023-08-28 15:35:53 +00:00
|
|
|
uword readsize
|
2024-02-04 22:22:43 +00:00
|
|
|
while num_bytes!=0 {
|
2023-08-28 15:35:53 +00:00
|
|
|
readsize = 255
|
|
|
|
if num_bytes<readsize
|
|
|
|
readsize = num_bytes
|
2024-04-04 21:47:33 +00:00
|
|
|
void, readsize = cx16.MACPTR(lsb(readsize), bufferpointer, false) ; fast block reads
|
2023-05-02 01:11:09 +00:00
|
|
|
if_cs
|
2023-10-09 20:10:36 +00:00
|
|
|
goto byte_read_loop ; MACPTR block read not supported, do fallback loop
|
2023-08-28 15:35:53 +00:00
|
|
|
list_blocks += readsize
|
|
|
|
bufferpointer += readsize
|
2023-05-02 01:11:09 +00:00
|
|
|
if msb(bufferpointer) == $c0
|
|
|
|
bufferpointer = mkword($a0, lsb(bufferpointer)) ; wrap over bank boundary
|
2023-08-28 15:35:53 +00:00
|
|
|
num_bytes -= readsize
|
2024-02-04 22:22:43 +00:00
|
|
|
if cbm.READST() & $40 !=0 {
|
2023-05-02 01:11:09 +00:00
|
|
|
f_close() ; end of file, close it
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-12-03 21:51:56 +00:00
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
2023-05-02 01:11:09 +00:00
|
|
|
return list_blocks ; number of bytes read
|
|
|
|
|
2023-10-09 20:10:36 +00:00
|
|
|
byte_read_loop: ; fallback if MACPTR isn't supported on the device
|
2023-05-02 00:15:22 +00:00
|
|
|
%asm {{
|
|
|
|
lda bufferpointer
|
|
|
|
sta m_in_buffer+1
|
|
|
|
lda bufferpointer+1
|
|
|
|
sta m_in_buffer+2
|
|
|
|
}}
|
2024-02-04 22:22:43 +00:00
|
|
|
while num_bytes!=0 {
|
2023-05-02 00:15:22 +00:00
|
|
|
%asm {{
|
|
|
|
jsr cbm.CHRIN
|
|
|
|
m_in_buffer sta $ffff
|
|
|
|
inc m_in_buffer+1
|
|
|
|
bne +
|
|
|
|
inc m_in_buffer+2
|
|
|
|
+
|
|
|
|
}}
|
2024-12-02 20:25:38 +00:00
|
|
|
cx16.r0L = cbm.READST()
|
2024-12-03 21:51:56 +00:00
|
|
|
if_nz {
|
2024-12-02 20:25:38 +00:00
|
|
|
f_close()
|
2024-12-03 21:51:56 +00:00
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
2024-12-02 20:25:38 +00:00
|
|
|
if cx16.r0L & $40 !=0 ; eof?
|
|
|
|
return list_blocks ; number of bytes read
|
|
|
|
return 0 ; error.
|
|
|
|
}
|
2023-05-02 00:15:22 +00:00
|
|
|
list_blocks++
|
|
|
|
num_bytes--
|
|
|
|
}
|
2024-12-03 21:51:56 +00:00
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
2023-05-02 00:15:22 +00:00
|
|
|
return list_blocks ; number of bytes read
|
|
|
|
}
|
|
|
|
|
2023-05-02 01:11:09 +00:00
|
|
|
; optimized for Commander X16 to use MACPTR block read kernal call
|
2023-05-02 00:15:22 +00:00
|
|
|
sub f_read_all(uword bufferpointer) -> uword {
|
|
|
|
; -- read the full contents of the file, returns number of bytes read.
|
2024-07-06 20:25:01 +00:00
|
|
|
; It is assumed the file size is less than 64 K.
|
2023-10-09 20:10:36 +00:00
|
|
|
; NOTE: cannot be used to load into VRAM. Use vload() or call cx16.MACPTR() yourself with the vera data register as address.
|
2023-05-02 00:15:22 +00:00
|
|
|
if not iteration_in_progress
|
|
|
|
return 0
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2023-05-02 00:15:22 +00:00
|
|
|
uword total_read = 0
|
2024-02-04 22:22:43 +00:00
|
|
|
while cbm.READST()==0 {
|
2023-05-02 00:15:22 +00:00
|
|
|
cx16.r0 = f_read(bufferpointer, 256)
|
|
|
|
total_read += cx16.r0
|
|
|
|
bufferpointer += cx16.r0
|
|
|
|
}
|
2024-12-03 21:51:56 +00:00
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
2023-05-02 00:15:22 +00:00
|
|
|
return total_read
|
|
|
|
}
|
|
|
|
|
2024-07-06 20:25:01 +00:00
|
|
|
asmsub f_readline(uword bufptr @AY) clobbers(X) -> ubyte @Y, ubyte @A {
|
2023-05-02 00:15:22 +00:00
|
|
|
; Routine to read text lines from a text file. Lines must be less than 255 characters.
|
2024-12-03 21:51:56 +00:00
|
|
|
; Reads characters from the input file UNTIL a newline or return character, or 0 byte (likely EOF).
|
2023-05-02 00:15:22 +00:00
|
|
|
; The line read will be 0-terminated in the buffer (and not contain the end of line character).
|
|
|
|
; The length of the line is returned in Y. Note that an empty line is okay and is length 0!
|
2024-07-06 20:25:01 +00:00
|
|
|
; The I/O error status byte is returned in A.
|
2023-05-02 00:15:22 +00:00
|
|
|
%asm {{
|
|
|
|
sta P8ZP_SCRATCH_W1
|
|
|
|
sty P8ZP_SCRATCH_W1+1
|
2023-09-28 21:29:21 +00:00
|
|
|
jsr reset_read_channel
|
2023-05-02 00:15:22 +00:00
|
|
|
ldy #0
|
|
|
|
_loop jsr cbm.CHRIN
|
|
|
|
sta (P8ZP_SCRATCH_W1),y
|
|
|
|
beq _end
|
|
|
|
iny
|
|
|
|
cmp #$0a
|
|
|
|
beq _line_end
|
|
|
|
cmp #$0d
|
|
|
|
bne _loop
|
|
|
|
_line_end dey ; get rid of the trailing end-of-line char
|
|
|
|
lda #0
|
|
|
|
sta (P8ZP_SCRATCH_W1),y
|
2024-07-06 20:25:01 +00:00
|
|
|
_end jsr cbm.READST
|
2024-12-03 21:51:56 +00:00
|
|
|
pha
|
|
|
|
jsr cbm.CLRCHN
|
|
|
|
pla
|
2024-07-06 20:25:01 +00:00
|
|
|
rts
|
2023-05-02 00:15:22 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub f_close() {
|
|
|
|
; -- end an iterative file loading session (close channels).
|
2024-10-31 19:08:10 +00:00
|
|
|
; it is safe to call this multiple times, or when no file is open for reading.
|
2023-05-02 00:15:22 +00:00
|
|
|
if iteration_in_progress {
|
|
|
|
cbm.CLRCHN()
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2023-05-02 00:15:22 +00:00
|
|
|
iteration_in_progress = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-28 21:29:21 +00:00
|
|
|
; ----- iterative file writing functions (uses write io channel) -----
|
2023-05-02 00:15:22 +00:00
|
|
|
|
2023-11-12 19:40:17 +00:00
|
|
|
sub f_open_w(str filename) -> bool {
|
|
|
|
; -- Open a file for iterative writing with f_write
|
|
|
|
; 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!
|
2024-12-02 21:05:23 +00:00
|
|
|
; NOTE: the default output isn't yet set to this file, you must use reset_write_channel() to do this,
|
|
|
|
; if you're going to write to it yourself instead of using f_write()!
|
2023-11-12 19:40:17 +00:00
|
|
|
return internal_f_open_w(filename, false)
|
|
|
|
}
|
2023-05-02 00:15:22 +00:00
|
|
|
|
2023-11-12 19:40:17 +00:00
|
|
|
sub f_open_w_seek(str filename) -> bool {
|
|
|
|
; -- Open an existing file for iterative writing with f_write, and seeking with f_seek_w.
|
|
|
|
return internal_f_open_w(filename, true)
|
|
|
|
}
|
|
|
|
|
2023-05-02 00:15:22 +00:00
|
|
|
sub f_write(uword bufferpointer, uword num_bytes) -> bool {
|
|
|
|
; -- write the given number of bytes to the currently open file
|
2024-07-06 20:25:01 +00:00
|
|
|
; you can call this multiple times to append more data
|
2023-05-02 00:15:22 +00:00
|
|
|
if num_bytes!=0 {
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_write_channel()
|
2023-11-07 22:39:39 +00:00
|
|
|
do {
|
2024-04-04 21:47:33 +00:00
|
|
|
void, cx16.r0 = cx16.MCIOUT(lsb(num_bytes), bufferpointer, false) ; fast block writes
|
2023-11-07 22:39:39 +00:00
|
|
|
if_cs
|
|
|
|
goto no_mciout
|
|
|
|
num_bytes -= cx16.r0
|
|
|
|
bufferpointer += cx16.r0
|
|
|
|
if msb(bufferpointer) == $c0
|
|
|
|
bufferpointer = mkword($a0, lsb(bufferpointer)) ; wrap over bank boundary
|
|
|
|
if cbm.READST()!=0
|
2024-12-03 21:51:56 +00:00
|
|
|
goto return_status
|
2023-11-07 22:39:39 +00:00
|
|
|
} until num_bytes==0
|
2024-12-03 21:51:56 +00:00
|
|
|
goto return_status
|
2023-11-07 22:39:39 +00:00
|
|
|
|
|
|
|
no_mciout:
|
|
|
|
; the device doesn't support MCIOUT, use a normal per-byte write loop
|
2023-05-02 00:15:22 +00:00
|
|
|
repeat num_bytes {
|
|
|
|
cbm.CHROUT(@(bufferpointer))
|
|
|
|
bufferpointer++
|
|
|
|
}
|
2024-12-03 21:51:56 +00:00
|
|
|
return_status:
|
|
|
|
cx16.r0L = cbm.READST()
|
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
|
|
|
return cx16.r0L==0
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
sub f_close_w() {
|
|
|
|
; -- end an iterative file writing session (close channels).
|
2024-10-31 19:08:10 +00:00
|
|
|
; it is safe to call this multiple times, or when no file is open for reading.
|
|
|
|
if write_iteration_in_progress {
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(WRITE_IO_CHANNEL)
|
|
|
|
write_iteration_in_progress = false
|
|
|
|
}
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
|
2024-12-02 21:05:23 +00:00
|
|
|
sub internal_f_open_w(str filename, bool open_for_seeks) -> bool {
|
|
|
|
f_close_w()
|
|
|
|
list_filename = filename
|
|
|
|
str modifier = ",s,?"
|
|
|
|
modifier[3] = 'w'
|
|
|
|
if open_for_seeks
|
|
|
|
modifier[3] = 'm'
|
|
|
|
cx16.r0L = strings.append(list_filename, modifier) ; secondary 13 requires a mode suffix to signal we're writing/modifying
|
|
|
|
cbm.SETNAM(cx16.r0L, list_filename)
|
|
|
|
cbm.SETLFS(WRITE_IO_CHANNEL, drivenumber, WRITE_IO_CHANNEL)
|
|
|
|
void cbm.OPEN() ; open 13,8,13,"filename"
|
|
|
|
if_cc {
|
|
|
|
if cbm.READST()==0 {
|
|
|
|
write_iteration_in_progress = true
|
|
|
|
cbm.CLRCHN() ; reset default i/o channels
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cbm.CLOSE(WRITE_IO_CHANNEL)
|
|
|
|
f_close_w()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-02 00:15:22 +00:00
|
|
|
|
|
|
|
; ---- other functions ----
|
|
|
|
|
|
|
|
sub status() -> uword {
|
|
|
|
; -- retrieve the disk drive's current status message
|
2024-12-02 22:33:33 +00:00
|
|
|
|
|
|
|
; TODO this doesn't seem to work reliably, sometimes READST returns 128 when the drive is just fine
|
|
|
|
; str device_not_present_error = "device not present #xx"
|
|
|
|
; if cbm.READST()==128 {
|
|
|
|
; device_not_present_error[len(device_not_present_error)-2] = 0
|
|
|
|
; void strings.copy(conv.str_ub(drivenumber), &device_not_present_error+len(device_not_present_error)-2)
|
|
|
|
; return device_not_present_error
|
|
|
|
; }
|
2023-12-17 20:21:55 +00:00
|
|
|
|
2023-05-02 00:15:22 +00:00
|
|
|
uword messageptr = &list_filename
|
|
|
|
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
|
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
while cbm.READST()==0 {
|
2023-05-02 00:15:22 +00:00
|
|
|
cx16.r5L = cbm.CHRIN()
|
|
|
|
if cx16.r5L=='\r' or cx16.r5L=='\n'
|
|
|
|
break
|
|
|
|
@(messageptr) = cx16.r5L
|
|
|
|
messageptr++
|
|
|
|
}
|
|
|
|
@(messageptr) = 0
|
|
|
|
|
|
|
|
done:
|
|
|
|
cbm.CLRCHN() ; restore default i/o devices
|
|
|
|
cbm.CLOSE(15)
|
|
|
|
return list_filename
|
|
|
|
|
|
|
|
io_error:
|
2023-12-17 20:21:55 +00:00
|
|
|
cbm.CLOSE(15)
|
|
|
|
list_filename = "io error"
|
2023-05-02 00:15:22 +00:00
|
|
|
goto done
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:42:47 +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 (CMDR-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 {
|
2024-03-27 22:05:41 +00:00
|
|
|
void cbm.CHRIN()
|
2024-03-27 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-19 20:06:49 +00:00
|
|
|
|
|
|
|
; saves a block of memory to disk, including the default 2 byte prg header.
|
2023-08-28 15:35:53 +00:00
|
|
|
sub save(uword filenameptr, uword startaddress, uword savesize) -> bool {
|
|
|
|
return internal_save_routine(filenameptr, startaddress, savesize, false)
|
2023-05-19 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
; like save() but omits the 2 byte prg header.
|
2023-08-28 15:35:53 +00:00
|
|
|
sub save_raw(uword filenameptr, uword startaddress, uword savesize) -> bool {
|
|
|
|
return internal_save_routine(filenameptr, startaddress, savesize, true)
|
2023-05-19 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 15:35:53 +00:00
|
|
|
sub internal_save_routine(uword filenameptr, uword startaddress, uword savesize, bool headerless) -> bool {
|
2024-11-23 14:51:38 +00:00
|
|
|
cbm.SETNAM(strings.length(filenameptr), filenameptr)
|
2023-05-02 00:15:22 +00:00
|
|
|
cbm.SETLFS(1, drivenumber, 0)
|
2023-08-28 15:35:53 +00:00
|
|
|
uword @shared end_address = startaddress + savesize
|
2023-05-02 00:15:22 +00:00
|
|
|
cx16.r0L = 0
|
|
|
|
|
|
|
|
%asm {{
|
2023-08-28 15:35:53 +00:00
|
|
|
lda startaddress
|
2023-05-02 00:15:22 +00:00
|
|
|
sta P8ZP_SCRATCH_W1
|
2023-08-28 15:35:53 +00:00
|
|
|
lda startaddress+1
|
2023-05-02 00:15:22 +00:00
|
|
|
sta P8ZP_SCRATCH_W1+1
|
|
|
|
ldx end_address
|
|
|
|
ldy end_address+1
|
2023-05-19 20:06:49 +00:00
|
|
|
lda headerless
|
|
|
|
beq +
|
|
|
|
lda #<P8ZP_SCRATCH_W1
|
|
|
|
jsr cx16.BSAVE
|
|
|
|
bra ++
|
|
|
|
+ lda #<P8ZP_SCRATCH_W1
|
2023-05-02 00:15:22 +00:00
|
|
|
jsr cbm.SAVE
|
2023-05-19 20:06:49 +00:00
|
|
|
+ php
|
2023-05-02 00:15:22 +00:00
|
|
|
plp
|
|
|
|
}}
|
|
|
|
|
|
|
|
if_cc
|
2024-02-04 22:22:43 +00:00
|
|
|
cx16.r0L = cbm.READST()==0 as ubyte
|
2023-05-02 00:15:22 +00:00
|
|
|
|
2024-02-04 22:22:43 +00:00
|
|
|
return cx16.r0L as bool
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
; Use kernal LOAD routine to load the given program file in memory.
|
|
|
|
; This is similar to Basic's LOAD "filename",drive / LOAD "filename",drive,1
|
|
|
|
; 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.
|
|
|
|
; Returns the end load address+1 if successful or 0 if a load error occurred.
|
|
|
|
; NOTE: when the load is larger than 64Kb and/or spans multiple RAM banks
|
|
|
|
; (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.
|
2023-05-02 01:11:09 +00:00
|
|
|
; You can use the load_size() function to calcuate the size in this case.
|
|
|
|
; NOTE: data is read into the current Ram bank if you're reading into banked ram.
|
|
|
|
; if you require loading into another ram bank, you have to set that
|
|
|
|
; yourself using cx16.rambank(bank) before calling load().
|
2023-05-02 00:15:22 +00:00
|
|
|
sub load(uword filenameptr, uword address_override) -> uword {
|
|
|
|
return internal_load_routine(filenameptr, address_override, false)
|
|
|
|
}
|
|
|
|
|
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 startaddress) -> uword {
|
|
|
|
return internal_load_routine(filenameptr, startaddress, true)
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub internal_load_routine(uword filenameptr, uword address_override, bool headerless) -> uword {
|
2024-11-23 14:51:38 +00:00
|
|
|
cbm.SETNAM(strings.length(filenameptr), filenameptr)
|
2023-05-02 00:15:22 +00:00
|
|
|
ubyte secondary = 1
|
|
|
|
cx16.r1 = 0
|
2024-02-04 22:22:43 +00:00
|
|
|
if address_override!=0
|
2023-05-02 00:15:22 +00:00
|
|
|
secondary = 0
|
|
|
|
if headerless
|
|
|
|
secondary |= %00000010 ; activate cx16 kernal headerless load support
|
|
|
|
cbm.SETLFS(1, drivenumber, secondary)
|
|
|
|
%asm {{
|
|
|
|
lda #0
|
|
|
|
ldx address_override
|
|
|
|
ldy address_override+1
|
|
|
|
jsr cbm.LOAD
|
|
|
|
bcs +
|
|
|
|
stx cx16.r1
|
|
|
|
sty cx16.r1+1
|
2023-07-15 13:18:26 +00:00
|
|
|
+
|
2023-05-02 00:15:22 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
return cx16.r1
|
|
|
|
}
|
|
|
|
|
|
|
|
sub delete(uword filenameptr) {
|
|
|
|
; -- delete a file on the drive
|
|
|
|
list_filename[0] = 's'
|
|
|
|
list_filename[1] = ':'
|
2024-11-23 14:51:38 +00:00
|
|
|
ubyte flen = strings.copy(filenameptr, &list_filename+2)
|
2023-05-02 00:15:22 +00:00
|
|
|
cbm.SETNAM(flen+2, list_filename)
|
|
|
|
cbm.SETLFS(1, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rename(uword oldfileptr, uword newfileptr) {
|
|
|
|
; -- rename a file on the drive
|
|
|
|
list_filename[0] = 'r'
|
|
|
|
list_filename[1] = ':'
|
2024-11-23 14:51:38 +00:00
|
|
|
ubyte flen_new = strings.copy(newfileptr, &list_filename+2)
|
2023-05-02 00:15:22 +00:00
|
|
|
list_filename[flen_new+2] = '='
|
2024-11-23 14:51:38 +00:00
|
|
|
ubyte flen_old = strings.copy(oldfileptr, &list_filename+3+flen_new)
|
2023-05-02 00:15:22 +00:00
|
|
|
cbm.SETNAM(3+flen_new+flen_old, list_filename)
|
|
|
|
cbm.SETLFS(1, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub send_command(uword commandptr) {
|
2024-05-21 21:14:25 +00:00
|
|
|
; -- send a dos command to the drive (don't read any response)
|
2024-11-23 14:51:38 +00:00
|
|
|
cbm.SETNAM(strings.length(commandptr), commandptr)
|
2023-05-02 00:15:22 +00:00
|
|
|
cbm.SETLFS(15, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLRCHN()
|
|
|
|
cbm.CLOSE(15)
|
|
|
|
}
|
2023-05-02 01:11:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
; CommanderX16 extensions over the basic C64/C128 diskio routines:
|
|
|
|
|
|
|
|
; For use directly after a load or load_raw call (don't mess with the ram bank yet):
|
2024-10-30 18:17:58 +00:00
|
|
|
; Calculates the number of bytes loaded (files > 64Kb are truncated to 16 bits)
|
2023-05-02 01:11:09 +00:00
|
|
|
sub load_size(ubyte startbank, uword startaddress, uword endaddress) -> uword {
|
|
|
|
return $2000 * (cx16.getrambank() - startbank) + endaddress - startaddress
|
|
|
|
}
|
|
|
|
|
2024-02-27 23:34:35 +00:00
|
|
|
asmsub vload(str name @R0, ubyte bank @A, uword startaddress @R1) clobbers(X, Y) -> bool @A {
|
2023-05-02 01:11:09 +00:00
|
|
|
; -- like the basic command VLOAD "filename",drivenumber,bank,address
|
|
|
|
; loads a file into Vera's video memory in the given bank:address, returns success in A
|
|
|
|
; the file has to have the usual 2 byte header (which will be skipped)
|
|
|
|
%asm {{
|
|
|
|
clc
|
|
|
|
internal_vload:
|
|
|
|
pha
|
|
|
|
ldx drivenumber
|
|
|
|
bcc +
|
|
|
|
ldy #%00000010 ; headerless load mode
|
|
|
|
bne ++
|
|
|
|
+ ldy #0 ; normal load mode
|
|
|
|
+ lda #1
|
|
|
|
jsr cbm.SETLFS
|
|
|
|
lda cx16.r0
|
|
|
|
ldy cx16.r0+1
|
|
|
|
jsr prog8_lib.strlen
|
|
|
|
tya
|
|
|
|
ldx cx16.r0
|
|
|
|
ldy cx16.r0+1
|
|
|
|
jsr cbm.SETNAM
|
|
|
|
pla
|
|
|
|
clc
|
|
|
|
adc #2
|
|
|
|
ldx cx16.r1
|
|
|
|
ldy cx16.r1+1
|
|
|
|
stz P8ZP_SCRATCH_B1
|
|
|
|
jsr cbm.LOAD
|
|
|
|
bcs +
|
|
|
|
inc P8ZP_SCRATCH_B1
|
|
|
|
+ jsr cbm.CLRCHN
|
|
|
|
lda #1
|
|
|
|
jsr cbm.CLOSE
|
|
|
|
lda P8ZP_SCRATCH_B1
|
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-02-27 23:34:35 +00:00
|
|
|
asmsub vload_raw(str name @R0, ubyte bank @A, uword startaddress @R1) clobbers(X, Y) -> bool @A {
|
2023-05-02 01:11:09 +00:00
|
|
|
; -- like the basic command BVLOAD "filename",drivenumber,bank,address
|
|
|
|
; loads a file into Vera's video memory in the given bank:address, returns success in A
|
|
|
|
; the file is read fully including the first two bytes.
|
|
|
|
%asm {{
|
|
|
|
sec
|
|
|
|
jmp vload.internal_vload
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-10-10 18:46:18 +00:00
|
|
|
; note: There is no vsave_raw() routine because the Kernal doesn't have a VSAVE routine.
|
|
|
|
; You'll have to write your own loop that reads vram data and use
|
|
|
|
; cbm.CHROUT or cx16.MCIOUT to write it to an open output file.
|
|
|
|
|
|
|
|
|
2023-05-02 01:11:09 +00:00
|
|
|
sub chdir(str path) {
|
|
|
|
; -- change current directory.
|
|
|
|
list_filename[0] = 'c'
|
|
|
|
list_filename[1] = 'd'
|
|
|
|
list_filename[2] = ':'
|
2024-11-23 14:51:38 +00:00
|
|
|
void strings.copy(path, &list_filename+3)
|
2023-05-02 01:11:09 +00:00
|
|
|
send_command(list_filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub mkdir(str name) {
|
|
|
|
; -- make a new subdirectory.
|
|
|
|
list_filename[0] = 'm'
|
|
|
|
list_filename[1] = 'd'
|
|
|
|
list_filename[2] = ':'
|
2024-11-23 14:51:38 +00:00
|
|
|
void strings.copy(name, &list_filename+3)
|
2023-05-02 01:11:09 +00:00
|
|
|
send_command(list_filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rmdir(str name) {
|
|
|
|
; -- remove a subdirectory.
|
2024-11-23 14:51:38 +00:00
|
|
|
void strings.find(name, '*')
|
2023-05-02 01:11:09 +00:00
|
|
|
if_cs
|
|
|
|
return ; refuse to act on a wildcard *
|
|
|
|
list_filename[0] = 'r'
|
|
|
|
list_filename[1] = 'd'
|
|
|
|
list_filename[2] = ':'
|
2024-11-23 14:51:38 +00:00
|
|
|
void strings.copy(name, &list_filename+3)
|
2023-05-02 01:11:09 +00:00
|
|
|
send_command(list_filename)
|
|
|
|
}
|
|
|
|
|
2023-06-07 20:31:46 +00:00
|
|
|
sub curdir() -> uword {
|
|
|
|
; return current directory name or 0 if error
|
|
|
|
; special X16 dos command to only return the current path in the entry list (R42+)
|
|
|
|
const ubyte MAX_PATH_LEN=80
|
|
|
|
uword reversebuffer = memory("curdir_buffer", MAX_PATH_LEN, 0)
|
|
|
|
cx16.r12 = reversebuffer + MAX_PATH_LEN-1
|
|
|
|
@(cx16.r12)=0
|
|
|
|
cbm.SETNAM(3, "$=c")
|
2023-11-10 21:27:07 +00:00
|
|
|
cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0)
|
2023-06-07 20:31:46 +00:00
|
|
|
void cbm.OPEN() ; open 12,8,0,"$=c"
|
|
|
|
if_cs
|
|
|
|
goto io_error
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel()
|
2023-06-07 20:31:46 +00:00
|
|
|
|
|
|
|
repeat 6 {
|
|
|
|
void cbm.CHRIN()
|
|
|
|
}
|
2024-02-04 22:22:43 +00:00
|
|
|
while cbm.CHRIN()!=0 {
|
2023-06-07 20:31:46 +00:00
|
|
|
; skip first line (drive label)
|
|
|
|
}
|
|
|
|
while cbm.CHRIN()!='"' {
|
|
|
|
; skip to first name
|
|
|
|
}
|
|
|
|
ubyte status = cbm.READST()
|
|
|
|
cx16.r10 = &list_filename
|
|
|
|
while status==0 {
|
|
|
|
repeat {
|
|
|
|
@(cx16.r10) = cbm.CHRIN()
|
|
|
|
if @(cx16.r10)==0
|
|
|
|
break
|
|
|
|
cx16.r10++
|
|
|
|
}
|
|
|
|
while @(cx16.r10)!='"' and cx16.r10>=&list_filename {
|
|
|
|
@(cx16.r10)=0
|
|
|
|
cx16.r10--
|
|
|
|
}
|
|
|
|
@(cx16.r10)=0
|
|
|
|
prepend(list_filename)
|
|
|
|
cx16.r10 = &list_filename
|
|
|
|
while cbm.CHRIN()!='"' and status==0 {
|
|
|
|
status = cbm.READST()
|
|
|
|
; skipping up to next entry name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
io_error:
|
|
|
|
cbm.CLRCHN()
|
2023-09-28 21:29:21 +00:00
|
|
|
cbm.CLOSE(READ_IO_CHANNEL)
|
2024-02-04 22:22:43 +00:00
|
|
|
if status!=0 and status & $40 == 0
|
2023-06-07 20:31:46 +00:00
|
|
|
return 0
|
|
|
|
if @(cx16.r12)==0 {
|
|
|
|
cx16.r12--
|
|
|
|
@(cx16.r12)='/'
|
|
|
|
}
|
|
|
|
return cx16.r12
|
|
|
|
|
|
|
|
sub prepend(str dir) {
|
|
|
|
if dir[0]=='/' and dir[1]==0
|
|
|
|
return
|
2024-11-23 14:51:38 +00:00
|
|
|
cx16.r9L = strings.length(dir)
|
2023-06-07 20:31:46 +00:00
|
|
|
cx16.r12 -= cx16.r9L
|
|
|
|
sys.memcopy(dir, cx16.r12, cx16.r9L)
|
|
|
|
cx16.r12--
|
|
|
|
@(cx16.r12)='/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 01:11:09 +00:00
|
|
|
sub relabel(str name) {
|
|
|
|
; -- change the disk label.
|
|
|
|
list_filename[0] = 'r'
|
|
|
|
list_filename[1] = '-'
|
|
|
|
list_filename[2] = 'h'
|
|
|
|
list_filename[3] = ':'
|
2024-11-23 14:51:38 +00:00
|
|
|
void strings.copy(name, &list_filename+4)
|
2023-05-02 01:11:09 +00:00
|
|
|
send_command(list_filename)
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:08:10 +00:00
|
|
|
sub exists(str filename) -> bool {
|
|
|
|
; -- returns true if the given file exists on the disk, otherwise false
|
2024-12-02 21:05:23 +00:00
|
|
|
; DON'T use this if you already have a file open with f_open!
|
2024-10-31 19:08:10 +00:00
|
|
|
if f_open(filename) {
|
|
|
|
f_close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-02 01:11:09 +00:00
|
|
|
sub f_seek(uword pos_hiword, uword pos_loword) {
|
|
|
|
; -- seek in the reading file opened with f_open, to the given 32-bits position
|
2024-07-16 19:28:49 +00:00
|
|
|
; Note: this will not work if you have already read the last byte of the file! Then you must close and reopen the file first.
|
2023-05-02 01:11:09 +00:00
|
|
|
ubyte[6] command = ['p',0,0,0,0,0]
|
2023-11-11 13:26:40 +00:00
|
|
|
command[1] = READ_IO_CHANNEL ; f_open uses this secondary address
|
2023-05-02 01:11:09 +00:00
|
|
|
command[2] = lsb(pos_loword)
|
|
|
|
command[3] = msb(pos_loword)
|
|
|
|
command[4] = lsb(pos_hiword)
|
|
|
|
command[5] = msb(pos_hiword)
|
|
|
|
cbm.SETNAM(sizeof(command), &command)
|
|
|
|
cbm.SETLFS(15, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLOSE(15)
|
2023-09-28 21:29:21 +00:00
|
|
|
reset_read_channel() ; back to the read io channel
|
2023-05-02 01:11:09 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 17:21:01 +00:00
|
|
|
|
2023-11-10 21:27:07 +00:00
|
|
|
sub f_seek_w(uword pos_hiword, uword pos_loword) {
|
2023-11-12 19:40:17 +00:00
|
|
|
; -- seek in the output file opened with f_open_w_seek, to the given 32-bits position
|
2023-11-11 13:26:40 +00:00
|
|
|
diskio.f_seek.command[1] = WRITE_IO_CHANNEL ; f_open_w uses this secondary address
|
2023-11-10 21:27:07 +00:00
|
|
|
diskio.f_seek.command[2] = lsb(pos_loword)
|
|
|
|
diskio.f_seek.command[3] = msb(pos_loword)
|
|
|
|
diskio.f_seek.command[4] = lsb(pos_hiword)
|
|
|
|
diskio.f_seek.command[5] = msb(pos_hiword)
|
2023-11-11 13:26:40 +00:00
|
|
|
cbm.SETNAM(sizeof(diskio.f_seek.command), &diskio.f_seek.command)
|
|
|
|
cbm.SETLFS(15, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
cbm.CLOSE(15)
|
|
|
|
reset_write_channel() ; back to the write io channel
|
2023-11-10 21:27:07 +00:00
|
|
|
}
|
2023-05-02 01:11:09 +00:00
|
|
|
|
2024-05-22 19:47:02 +00:00
|
|
|
asmsub f_tell() -> uword @R0, uword @R1, uword @R2, uword @R3 {
|
|
|
|
; -- Returns the current read position of the opened read file,
|
|
|
|
; in R0 and R1 (low + high words) and the file size in R2 and R3 (low + high words).
|
2024-05-21 21:14:25 +00:00
|
|
|
; Returns 0 as size if the command is not supported by the DOS implementation/version.
|
|
|
|
%asm {{
|
|
|
|
jmp internal_f_tell
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-05-22 19:47:02 +00:00
|
|
|
sub internal_f_tell() {
|
|
|
|
; gets the (32 bits) position + file size of the opened read file channel
|
2024-05-21 21:14:25 +00:00
|
|
|
ubyte[2] command = ['t',0]
|
|
|
|
command[1] = READ_IO_CHANNEL ; f_open uses this secondary address
|
|
|
|
cbm.SETNAM(sizeof(command), &command)
|
|
|
|
cbm.SETLFS(15, drivenumber, 15)
|
|
|
|
void cbm.OPEN()
|
|
|
|
void cbm.CHKIN(15) ; use #15 as input channel
|
|
|
|
bool success=false
|
|
|
|
; valid response starts with "07," followed by hex notations of the position and filesize
|
|
|
|
if cbm.CHRIN()=='0' and cbm.CHRIN()=='7' and cbm.CHRIN()==',' {
|
|
|
|
cx16.r1 = read4hex()
|
|
|
|
cx16.r0 = read4hex() ; position in R1:R0
|
|
|
|
void cbm.CHRIN() ; separator space
|
|
|
|
cx16.r3 = read4hex()
|
|
|
|
cx16.r2 = read4hex() ; filesize in R3:R2
|
|
|
|
success = true
|
|
|
|
}
|
|
|
|
|
|
|
|
while cbm.READST()==0 {
|
|
|
|
cx16.r5L = cbm.CHRIN()
|
|
|
|
if cx16.r5L=='\r' or cx16.r5L=='\n'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
cbm.CLOSE(15)
|
|
|
|
reset_read_channel() ; back to the read io channel
|
|
|
|
if success
|
|
|
|
return
|
|
|
|
|
|
|
|
cx16.r0 = cx16.r1 = cx16.r2 = cx16.r3 = 0
|
|
|
|
}
|
|
|
|
|
2024-09-11 17:51:53 +00:00
|
|
|
sub read4hex() -> uword {
|
2024-09-14 21:17:26 +00:00
|
|
|
str hex = "0000"
|
2024-09-11 17:51:53 +00:00
|
|
|
hex[0] = cbm.CHRIN()
|
|
|
|
hex[1] = cbm.CHRIN()
|
|
|
|
hex[2] = cbm.CHRIN()
|
|
|
|
hex[3] = cbm.CHRIN()
|
|
|
|
return conv.hex2uword(hex)
|
|
|
|
}
|
2023-05-02 00:15:22 +00:00
|
|
|
}
|