From 504f0ffb814ddb0d197f74087b018138f624fef8 Mon Sep 17 00:00:00 2001 From: FeralChild64 Date: Thu, 25 Jul 2019 17:07:33 +0200 Subject: [PATCH] added proper buffer handling to several IEC commands --- c64/kernal/close.s | 6 +----- c64/kernal/iec/iec_tx_command_finalize.s | 2 +- c64/kernal/iec/iec_tx_common.s | 1 + c64/kernal/iec/iec_tx_flush.s | 15 +++++++++++++++ c64/kernal/listen.s | 14 +++++++++++--- c64/kernal/second.s | 9 +++------ c64/kernal/talk.s | 12 +++++++++++- c64/kernal/tksa.s | 13 +++---------- c64/kernal/unlsn.s | 17 +++++++++-------- c64/kernal/untlk.s | 12 ++++++++++++ 10 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 c64/kernal/iec/iec_tx_flush.s diff --git a/c64/kernal/close.s b/c64/kernal/close.s index a4b81d08..d1aed3ac 100644 --- a/c64/kernal/close.s +++ b/c64/kernal/close.s @@ -25,11 +25,7 @@ close: ;; IEC device - ;; First check whether we have something to be sent in the buffer - lda C3PO - beq + - sec ; send it with EOI - jsr iec_tx_byte ; send the command regardless of the status + jsr iec_tx_flush ; make sure no byte is awaiting * lda SAT, y ; get secondary address ora $E0 ; CLOSE command diff --git a/c64/kernal/iec/iec_tx_command_finalize.s b/c64/kernal/iec/iec_tx_command_finalize.s index 91ef8446..e0ad0c35 100644 --- a/c64/kernal/iec/iec_tx_command_finalize.s +++ b/c64/kernal/iec/iec_tx_command_finalize.s @@ -1,5 +1,5 @@ -;; Routine to finalize these IEC commands which do not end in turnaround +;; Routine to finalize some IEC commands - which do not end in turnaround and are not followed by others ;; Implemented based on https://www.pagetable.com/?p=1135, https://github.com/mist64/cbmbus_doc diff --git a/c64/kernal/iec/iec_tx_common.s b/c64/kernal/iec/iec_tx_common.s index 50d5edca..87b0aef1 100644 --- a/c64/kernal/iec/iec_tx_common.s +++ b/c64/kernal/iec/iec_tx_common.s @@ -5,6 +5,7 @@ ;; Expects byte to send in BSOUR; Carry flag set = signal EOI +;; XXX is it OK to use BSOUR for data exchange? Maybe there is a better way? iec_tx_common: diff --git a/c64/kernal/iec/iec_tx_flush.s b/c64/kernal/iec/iec_tx_flush.s new file mode 100644 index 00000000..95395f77 --- /dev/null +++ b/c64/kernal/iec/iec_tx_flush.s @@ -0,0 +1,15 @@ + +;; Flush the IEC output buffer (if not empty) - send byte with EOI +;; +;; Preserves .X, .Y and .A registers + +iec_tx_flush: + + pha + lda C3PO + beq + + sec ; send it with EOI + jsr iec_tx_byte ; send the command regardless of the status +* + pla + rts diff --git a/c64/kernal/listen.s b/c64/kernal/listen.s index 5e2c1bd1..7ba96f11 100644 --- a/c64/kernal/listen.s +++ b/c64/kernal/listen.s @@ -5,6 +5,7 @@ ;; - [RG64] C64 Programmer's Reference Guide - page 285 ;; - [CM64] Compute's Mapping the Commodore 64 - page 223 ;; - https://www.pagetable.com/?p=1031, https://github.com/mist64/cbmbus_doc +;; - http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf ;; ;; CPU registers that has to be preserved (see [RG64]): .X, .Y ;; @@ -12,10 +13,17 @@ listen: - ;; First check whether device number is correct + ;; According to serial-bus.pdf (page 15) this routine flushes the IEC out buffer + jsr iec_tx_flush + + ;; Check whether device number is correct jsr iec_devnum_check bcs kernalerror_DEVICE_NOT_FOUND + ;; Encode and execute the command ora #$20 - sta BSOUR - jmp iec_tx_command + + jmp common_talk_listen + + + diff --git a/c64/kernal/second.s b/c64/kernal/second.s index 819fdc24..27585a4c 100644 --- a/c64/kernal/second.s +++ b/c64/kernal/second.s @@ -18,9 +18,6 @@ second: bcs kernalerror_FILE_NOT_INPUT ora #$60 - sta BSOUR - jsr iec_tx_command - bcs + ; branch if error - jmp iec_tx_command_finalize -* - rts + + jmp common_unlsn_second + diff --git a/c64/kernal/talk.s b/c64/kernal/talk.s index 46c26c35..d4ca5742 100644 --- a/c64/kernal/talk.s +++ b/c64/kernal/talk.s @@ -5,6 +5,7 @@ ;; - [RG64] C64 Programmer's Reference Guide - page 301/302 ;; - [CM64] Compute's Mapping the Commodore 64 - page 223 ;; - https://www.pagetable.com/?p=1031, https://github.com/mist64/cbmbus_doc +;; - http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf ;; ;; CPU registers that has to be preserved (see [RG64]): .X, .Y ;; @@ -12,10 +13,19 @@ talk: - ;; First check whether device number is correct + ;; According to serial-bus.pdf (page 15) this routine flushes the IEC out buffer + jsr iec_tx_flush + + ;; Check whether device number is correct jsr iec_devnum_check bcs kernalerror_DEVICE_NOT_FOUND + ;; Encode and execute the command ora #$40 + +common_talk_listen: ; common part of TALK and LISTEN + sta BSOUR jmp iec_tx_command + + diff --git a/c64/kernal/tksa.s b/c64/kernal/tksa.s index 06953f7d..94d635cd 100644 --- a/c64/kernal/tksa.s +++ b/c64/kernal/tksa.s @@ -18,15 +18,8 @@ tksa: bcs kernalerror_FILE_NOT_INPUT ora #$F0 - sta BSOUR - jsr iec_tx_command - bcs + ; branch if error - ;; XXX - is it really the right place to do a turnaround? I've got some doubts - ;; (it forces us to send the TKSA command 'manually' sometimes), but - ;; Luigi Di Fraia (https://luigidifraia.wordpress.com/2017/06/27/codebase64-on-the-kernal-behaviour/) - ;; claims TKSA Kernal routine actually does the turnaround - jmp iec_turnaround_to_listen -* - rts + + jmp common_untlk_tksa + diff --git a/c64/kernal/unlsn.s b/c64/kernal/unlsn.s index e7ec1955..fd4e4480 100644 --- a/c64/kernal/unlsn.s +++ b/c64/kernal/unlsn.s @@ -5,6 +5,7 @@ ;; - [RG64] C64 Programmer's Reference Guide - page 303 ;; - [CM64] Compute's Mapping the Commodore 64 - page 224 ;; - https://www.pagetable.com/?p=1031, , https://github.com/mist64/cbmbus_doc +;; - http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf ;; ;; CPU registers that has to be preserved (see [RG64]): .X, .Y ;; @@ -12,19 +13,19 @@ unlsn: - ;; This routine is documented in 'Compute's Mapping the Commodore 64', page 224 - - ;; First check whether we have something to be sent in the buffer - lda C3PO - beq + - sec ; send it with EOI - jsr iec_tx_byte ; send the command regardless of the status -* + ;; According to serial-bus.pdf (page 15) this routine flushes the IEC out buffer + jsr iec_tx_flush + ;; Buffer empty, send the command lda #$3F + +common_unlsn_second: ; common part of UNSLS and SECOND + sta BSOUR jsr iec_tx_command bcs + ; branch if error jmp iec_tx_command_finalize * rts + + diff --git a/c64/kernal/untlk.s b/c64/kernal/untlk.s index 4ec846a8..4085698e 100644 --- a/c64/kernal/untlk.s +++ b/c64/kernal/untlk.s @@ -5,6 +5,7 @@ ;; - [RG64] C64 Programmer's Reference Guide - page 304 ;; - [CM64] Compute's Mapping the Commodore 64 - page 224 ;; - https://www.pagetable.com/?p=1031, , https://github.com/mist64/cbmbus_doc +;; - http://www.zimmers.net/anonftp/pub/cbm/programming/serial-bus.pdf ;; ;; CPU registers that has to be preserved (see [RG64]): .X, .Y ;; @@ -12,10 +13,21 @@ untlk: + ;; According to serial-bus.pdf (page 15) this routine flushes the IEC out buffer + jsr iec_tx_flush + + ;; Buffer empty, send the command lda #$5F + +common_untlk_tksa: ; common part of UNTLK and TKSA + sta BSOUR jsr iec_tx_command bcs + ; branch if error + ;; XXX - for TKSA, is it really the right place to do a turnaround? I've got some doubts + ;; (it forces us to send the TKSA command 'manually' sometimes), but + ;; Luigi Di Fraia (https://luigidifraia.wordpress.com/2017/06/27/codebase64-on-the-kernal-behaviour/) + ;; claims TKSA Kernal routine actually does the turnaround jmp iec_turnaround_to_listen * rts