From b6fa361bcca17de9c01a129b0cebed0ba7ec5bc5 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 31 Dec 2020 16:46:24 +0100 Subject: [PATCH] exit() now also resets the io channels. Optimized diskio data read subroutines. added diskio.f_read_all() --- compiler/res/prog8lib/diskio.p8 | 82 +++++++++++++++++++++++---- compiler/res/prog8lib/prog8_funcs.asm | 1 + examples/test.p8 | 78 +++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 12 deletions(-) diff --git a/compiler/res/prog8lib/diskio.p8 b/compiler/res/prog8lib/diskio.p8 index 250ba2fd1..67df15ab0 100644 --- a/compiler/res/prog8lib/diskio.p8 +++ b/compiler/res/prog8lib/diskio.p8 @@ -201,7 +201,7 @@ close_end: f_close() c64.SETNAM(strlen(filenameptr), filenameptr) - c64.SETLFS(11, drivenumber, 0) + c64.SETLFS(11, drivenumber, 3) void c64.OPEN() ; open 11,8,0,"filename" if_cc { iteration_in_progress = true @@ -221,15 +221,28 @@ close_end: uword actual = 0 void c64.CHKIN(11) ; use #11 as input channel again + %asm {{ + lda bufferpointer + sta _in_buffer+1 + lda bufferpointer+1 + sta _in_buffer+2 + }} repeat num_bytes { - ubyte data = c64.CHRIN() - @(bufferpointer) = data - bufferpointer++ - actual++ - ubyte status = c64.READST() - if status==64 + %asm {{ + jsr c64.CHRIN +_in_buffer sta $ffff + inc _in_buffer+1 + bne + + inc _in_buffer+2 ++ inc actual + bne + + inc actual+1 ++ + }} + ubyte data = c64.READST() + if data==64 f_close() ; end of file, close it - if status + if data return actual } return actual @@ -242,12 +255,57 @@ close_end: return void c64.CHKIN(11) ; use #11 as input channel again - repeat num_bytes { - @(bufferpointer) = c64.CHRIN() - bufferpointer++ - } + ; repeat num_bytes { + ; @(bufferpointer) = c64.CHRIN() + ; bufferpointer++ + ; } + %asm {{ + lda bufferpointer + sta P8ZP_SCRATCH_W1 + lda bufferpointer+1 + sta P8ZP_SCRATCH_W1+1 + lda #0 + sta P8ZP_SCRATCH_B1 + lda num_bytes+1 + sta P8ZP_SCRATCH_W2 + beq _no_msb +- jsr c64.CHRIN + ldy P8ZP_SCRATCH_B1 + sta (P8ZP_SCRATCH_W1),y + inc P8ZP_SCRATCH_B1 + bne - + inc P8ZP_SCRATCH_W1+1 + dec P8ZP_SCRATCH_W2 + bne - +_no_msb + lda num_bytes + beq _done +- jsr c64.CHRIN + ldy P8ZP_SCRATCH_B1 + sta (P8ZP_SCRATCH_W1),y + iny + sty P8ZP_SCRATCH_B1 + cpy num_bytes + bne - +_done + }} } + 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 + + uword total = 0 + while not c64.READST() { + total += f_read(bufferpointer, 256) + txt.chrout('.') + bufferpointer += 256 + } + return total + } + + sub f_close() { ; -- end an iterative file loading session (close channels). if iteration_in_progress { diff --git a/compiler/res/prog8lib/prog8_funcs.asm b/compiler/res/prog8lib/prog8_funcs.asm index 71bda5377..23dd25d6f 100644 --- a/compiler/res/prog8lib/prog8_funcs.asm +++ b/compiler/res/prog8lib/prog8_funcs.asm @@ -1080,6 +1080,7 @@ _loop_hi ldy _index_first func_exit .proc ; -- immediately exit the program with a return code in the A register + jsr c64.CLRCHN ; reset i/o channels ldx orig_stackpointer txs rts ; return to original caller diff --git a/examples/test.p8 b/examples/test.p8 index 473f85b6c..99f2a92f4 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,5 +1,6 @@ %import test_stack %import textio +%import diskio %zeropage basicsafe %option no_sysinit @@ -7,7 +8,84 @@ main { sub start () { + uword large_buffer_ptr = progend() + ubyte[256] buffer + uword size + + if diskio.f_open(8, "rom.asm2") { + txt.print("read-exact\n") + c64.SETTIM(0,0,0) + size = 0 + while not c64.READST() { + diskio.f_read_exact(&buffer, len(buffer)) + size += 256 + } + + diskio.f_close() + txt.print_uw(size) + txt.chrout('\n') + print_time() + txt.chrout('\n') + } else + txt.print("can't open file!\n") + txt.print(diskio.status(8)) + txt.chrout('\n') + + + if diskio.f_open(8, "rom.asm2") { + txt.print("read-all\n") + c64.SETTIM(0,0,0) + size = 0 + size = diskio.f_read_all(large_buffer_ptr) + diskio.f_close() + txt.print_uw(size) + txt.chrout('\n') + print_time() + txt.chrout('\n') + } else + txt.print("can't open file!\n") + txt.print(diskio.status(8)) + txt.chrout('\n') + +; if diskio.f_open(8, "rom.asm") { +; txt.print("read\n") +; c64.SETTIM(0,0,0) +; size = 0 +; while not c64.READST() { +; size += diskio.f_read(&buffer, len(buffer)) +; } +; +; diskio.f_close() +; txt.print_uw(size) +; txt.chrout('\n') +; print_time() +; txt.chrout('\n') +; } + test_stack.test() } + + + sub print_time() { + ubyte time_lo + ubyte time_mid + ubyte time_hi + + %asm {{ + stx P8ZP_SCRATCH_REG + jsr c64.RDTIM ; A/X/Y + sta time_lo + stx time_mid + sty time_hi + ldx P8ZP_SCRATCH_REG + }} + + txt.print_ub(time_hi) + txt.chrout(':') + txt.print_ub(time_mid) + txt.chrout(':') + txt.print_ub(time_lo) + txt.chrout('\n') + } }