diff --git a/compiler/res/prog8lib/diskio.p8 b/compiler/res/prog8lib/diskio.p8 index 63cf11bcd..9c376088d 100644 --- a/compiler/res/prog8lib/diskio.p8 +++ b/compiler/res/prog8lib/diskio.p8 @@ -6,8 +6,8 @@ diskio { - sub directory(ubyte drivenumber) -> byte { - ; -- Shows the directory contents of disk drive 8-11 (provide as argument). + sub directory(ubyte drivenumber) -> ubyte { + ; -- Shows the directory contents of disk drive 8-11 (provide as argument). Returns success flag. c64.SETNAM(1, "$") c64.SETLFS(1, drivenumber, 0) @@ -57,6 +57,74 @@ io_error: } + sub listfiles(ubyte drivenumber, uword pattern, ubyte prefixOrSuffix, + uword filenamesbufferptr, uword blocksizesptr, ubyte max_files) -> ubyte { + ; -- returns a list of files in the directory matching the given pattern (optional) + ; their blocksizes will be put into the uword array given by blocksizesptr + ; their names will be concatenated into the filenamesbuffer, separated by a 0-byte + ; The buffer should be at least 17 times max_files and the block sizes array should be big enough too. + if max_files==0 return 0 + if pattern!=0 and strlen(pattern)==0 pattern=0 + +; @(blocksizesptr) = lsb(333) +; @(blocksizesptr+1) = msb(333) +; @(blocksizesptr+2) = lsb(444) +; @(blocksizesptr+3) = msb(444) +; @(blocksizesptr+4) = lsb(555) +; @(blocksizesptr+5) = msb(555) +; str name1 = "filename1.txt" +; str name2 = "filename2.txt" +; str name3 = "filename3.txt" +; memcopy(name1, filenamesbufferptr, len(name1)+1) +; filenamesbufferptr += len(name1) + 1 +; memcopy(name2, filenamesbufferptr, len(name2)+1) +; filenamesbufferptr += len(name2) + 1 +; memcopy(name3, filenamesbufferptr, len(name3)+1) +; filenamesbufferptr += len(name3) + 1 + + ubyte num_files = 0 + + c64.SETNAM(1, "$") + c64.SETLFS(1, drivenumber, 0) + void c64.OPEN() ; open 1,8,0,"$" + if_cs + goto io_error + void c64.CHKIN(1) ; use #1 as input channel + if_cs + goto io_error + + repeat 4 { + void c64.CHRIN() ; skip the 4 prologue bytes + } + + while not c64.READST() { + @(blocksizesptr) = c64.CHRIN() + @(blocksizesptr+1) = c64.CHRIN() + blocksizesptr += 2 + + ; read until the filename starts after the first " + while c64.CHRIN()!='\"' { + if c64.READST() + goto io_error + } + + ubyte char + do { + char = c64.CHRIN() + @(filenamesbufferptr) = char + filenamesbufferptr++ + } until char==0 + num_files++ + void c64.CHRIN() ; skip 2 bytes + void c64.CHRIN() + } + +io_error: + c64.CLRCHN() ; restore default i/o devices + c64.CLOSE(1) + return num_files + } + sub status(ubyte drivenumber) { ; -- display the disk drive's current status message c64.SETNAM(0, $0000) @@ -77,7 +145,7 @@ io_error: } - sub save(ubyte drivenumber, uword filenameptr, uword address, uword size) -> byte { + sub save(ubyte drivenumber, uword filenameptr, uword address, uword size) -> ubyte { c64.SETNAM(strlen(filenameptr), filenameptr) c64.SETLFS(1, drivenumber, 0) uword end_address = address + size diff --git a/examples/test.p8 b/examples/test.p8 index 329869cf9..16be8e7ea 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,63 +1,43 @@ %import textio +%import diskio %import floats %zeropage basicsafe %import test_stack main { sub start() { - uword uw - ubyte ub - ub = 10 - uw = ub * 320 - txt.print_uw(uw) + ubyte result = diskio.directory(8) + + txt.print("result: ") + txt.print_ub(result) txt.chrout('\n') - ub = 100 - uw = ub * 320 - txt.print_uw(uw) + diskio.status(8) txt.chrout('\n') - - ub = 200 - uw = ub * 320 - txt.print_uw(uw) - txt.chrout('\n') - - uw = 10 - uw *= 320 - txt.print_uw(uw) - txt.chrout('\n') - - uw = 100 - uw *= 320 - txt.print_uw(uw) - txt.chrout('\n') - - uw = 200 - uw *= 320 - txt.print_uw(uw) txt.chrout('\n') txt.chrout('\n') - uw = 0 - ub = 10 - uw = uw + 320*ub - txt.print_uw(uw) + const ubyte max_files = 10 + uword[max_files] blocks + str filenames = "?????????????????" * max_files + + ubyte num_files = diskio.listfiles(8, ".bin", true, filenames, blocks, max_files) + txt.print("num files: ") + txt.print_ub(num_files) txt.chrout('\n') - - uw = 0 - ub = 100 - uw = uw + 320*ub - txt.print_uw(uw) - txt.chrout('\n') - - uw = 0 - ub = 200 - uw = uw + 320*ub - txt.print_uw(uw) - txt.chrout('\n') - - txt.print("hello\n") - + if num_files>0 { + ubyte i + uword filenameptr = &filenames + for i in 0 to num_files-1 { + txt.print_ub(i+1) + txt.print(": ") + txt.print(filenameptr) + txt.print(" (") + txt.print_uw(blocks[i]) + txt.print(" blocks)\n") + filenameptr += strlen(filenameptr) + 1 + } + } } }