From 3f3b6123b9b5242f581085d3932ed5fb63ddbd04 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Wed, 30 Aug 2023 21:28:09 +0200 Subject: [PATCH 1/5] Serial: Optimize TryToSend We don't need to reload SendFreeCnt each time we loop to wait for the ACIA to be ready. --- libsrc/apple2/ser/a2.ssc.s | 7 ++++--- libsrc/atmos/ser/atmos-acia.s | 7 ++++--- libsrc/c128/ser/c128-swlink.s | 14 +++++++------- libsrc/c64/ser/c64-swlink.s | 14 +++++++------- libsrc/cbm510/ser/cbm510-std.s | 14 +++++++------- libsrc/cbm610/ser/cbm610-std.s | 14 +++++++------- libsrc/plus4/ser/plus4-stdser.s | 14 +++++++------- 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/libsrc/apple2/ser/a2.ssc.s b/libsrc/apple2/ser/a2.ssc.s index 6ad9c3825..c2b0bf321 100644 --- a/libsrc/apple2/ser/a2.ssc.s +++ b/libsrc/apple2/ser/a2.ssc.s @@ -427,12 +427,13 @@ Done: rts TryToSend: sta tmp1 ; Remember tryHard flag -Again: lda SendFreeCnt +NextByte: + lda SendFreeCnt cmp #$FF beq Quit ; Bail out ; Check for flow stopped - lda Stopped +Again: lda Stopped bne Quit ; Bail out ; Check that ACIA is ready to send @@ -449,4 +450,4 @@ Send: ldy SendHead sta ACIA_DATA,x inc SendHead inc SendFreeCnt - jmp Again + jmp NextByte diff --git a/libsrc/atmos/ser/atmos-acia.s b/libsrc/atmos/ser/atmos-acia.s index f679125d1..ed5386471 100644 --- a/libsrc/atmos/ser/atmos-acia.s +++ b/libsrc/atmos/ser/atmos-acia.s @@ -352,12 +352,13 @@ Done: rts TryToSend: sta tmp1 ; Remember tryHard flag -Again: lda SendFreeCnt +NextByte: + lda SendFreeCnt cmp #$FF beq Quit ; Bail out ; Check for flow stopped - lda Stopped +Again: lda Stopped bne Quit ; Bail out ; Check that ACIA is ready to send @@ -374,4 +375,4 @@ Send: ldy SendHead sta ACIA::DATA inc SendHead inc SendFreeCnt - jmp Again + jmp NextByte diff --git a/libsrc/c128/ser/c128-swlink.s b/libsrc/c128/ser/c128-swlink.s index 7d36eb5bc..57b5ea3b2 100644 --- a/libsrc/c128/ser/c128-swlink.s +++ b/libsrc/c128/ser/c128-swlink.s @@ -466,25 +466,25 @@ NmiHandler: sta tmp1 ; Remember tryHard flag @L0: lda SendFreeCnt cmp #$ff - beq @L3 ; Bail out + beq @L2 ; Bail out ; Check for flow stopped @L1: lda Stopped - bne @L3 ; Bail out + bne @L2 ; Bail out ; Check that swiftlink is ready to send -@L2: lda ACIA_STATUS + lda ACIA_STATUS and #$10 - bne @L4 + bne @L3 bit tmp1 ;keep trying if must try hard - bmi @L0 -@L3: rts + bmi @L1 +@L2: rts ; Send byte and try again -@L4: ldx SendHead +@L3: ldx SendHead lda SendBuf,x sta ACIA_DATA inc SendHead diff --git a/libsrc/c64/ser/c64-swlink.s b/libsrc/c64/ser/c64-swlink.s index 81c9916a6..8c8b8bf39 100644 --- a/libsrc/c64/ser/c64-swlink.s +++ b/libsrc/c64/ser/c64-swlink.s @@ -443,25 +443,25 @@ NmiHandler: sta tmp1 ; Remember tryHard flag @L0: lda SendFreeCnt cmp #$ff - beq @L3 ; Bail out + beq @L2 ; Bail out ; Check for flow stopped @L1: lda Stopped - bne @L3 ; Bail out + bne @L2 ; Bail out ; Check that swiftlink is ready to send -@L2: lda ACIA_STATUS + lda ACIA_STATUS and #$10 - bne @L4 + bne @L3 bit tmp1 ;keep trying if must try hard - bmi @L0 -@L3: rts + bmi @L1 +@L2: rts ; Send byte and try again -@L4: ldx SendHead +@L3: ldx SendHead lda SendBuf,x sta ACIA_DATA inc SendHead diff --git a/libsrc/cbm510/ser/cbm510-std.s b/libsrc/cbm510/ser/cbm510-std.s index cc58c1233..34fe02d0f 100644 --- a/libsrc/cbm510/ser/cbm510-std.s +++ b/libsrc/cbm510/ser/cbm510-std.s @@ -395,31 +395,31 @@ SER_IRQ: sta IndReg ; Switch to the system bank @L0: lda SendFreeCnt cmp #$ff - beq @L3 ; Bail out + beq @L2 ; Bail out ; Check for flow stopped @L1: lda Stopped - bne @L3 ; Bail out + bne @L2 ; Bail out ; Check that swiftlink is ready to send -@L2: ldy #ACIA::STATUS + ldy #ACIA::STATUS lda (acia),y and #$10 - bne @L4 + bne @L3 bit tmp1 ; Keep trying if must try hard - bmi @L0 + bmi @L1 ; Switch back the bank and return -@L3: lda ExecReg +@L2: lda ExecReg sta IndReg rts ; Send byte and try again -@L4: ldx SendHead +@L3: ldx SendHead lda SendBuf,x ldy #ACIA::DATA sta (acia),y diff --git a/libsrc/cbm610/ser/cbm610-std.s b/libsrc/cbm610/ser/cbm610-std.s index f7ddde935..c241d5908 100644 --- a/libsrc/cbm610/ser/cbm610-std.s +++ b/libsrc/cbm610/ser/cbm610-std.s @@ -395,31 +395,31 @@ SER_IRQ: sta IndReg ; Switch to the system bank @L0: lda SendFreeCnt cmp #$ff - beq @L3 ; Bail out + beq @L2 ; Bail out ; Check for flow stopped @L1: lda Stopped - bne @L3 ; Bail out + bne @L2 ; Bail out ; Check that swiftlink is ready to send -@L2: ldy #ACIA::STATUS + ldy #ACIA::STATUS lda (acia),y and #$10 - bne @L4 + bne @L3 bit tmp1 ; Keep trying if must try hard - bmi @L0 + bmi @L1 ; Switch back the bank and return -@L3: lda ExecReg +@L2: lda ExecReg sta IndReg rts ; Send byte and try again -@L4: ldx SendHead +@L3: ldx SendHead lda SendBuf,x ldy #ACIA::DATA sta (acia),y diff --git a/libsrc/plus4/ser/plus4-stdser.s b/libsrc/plus4/ser/plus4-stdser.s index fbdc61b2e..5c4c550e6 100644 --- a/libsrc/plus4/ser/plus4-stdser.s +++ b/libsrc/plus4/ser/plus4-stdser.s @@ -387,25 +387,25 @@ SER_IRQ: sta tmp1 ; Remember tryHard flag @L0: lda SendFreeCnt cmp #$ff - beq @L3 ; Bail out + beq @L2 ; Bail out ; Check for flow stopped @L1: lda Stopped - bne @L3 ; Bail out + bne @L2 ; Bail out ; Check that swiftlink is ready to send -@L2: lda ACIA_STATUS + lda ACIA_STATUS and #$10 - bne @L4 + bne @L3 bit tmp1 ;keep trying if must try hard - bmi @L0 -@L3: rts + bmi @L1 +@L2: rts ; Send byte and try again -@L4: ldx SendHead +@L3: ldx SendHead lda SendBuf,x sta ACIA_DATA inc SendHead From 89af42e5cbcb98f43fc2269fc5ca5bb0e43f6bf1 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Wed, 30 Aug 2023 21:14:07 +0200 Subject: [PATCH 2/5] Serial: Optimize SER_IRQ Apple2 and Atmos have Index in X, but can still use it for the best-case path as long as we reload it in the worst-case part (when we assert flow control). Also, standardize the free space to trigger flow control to 32 characters left (compare with RecvFreeCnt before decrement) --- libsrc/apple2/ser/a2.ssc.s | 8 ++++---- libsrc/atmos/ser/atmos-acia.s | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libsrc/apple2/ser/a2.ssc.s b/libsrc/apple2/ser/a2.ssc.s index c2b0bf321..1dbf5f3a4 100644 --- a/libsrc/apple2/ser/a2.ssc.s +++ b/libsrc/apple2/ser/a2.ssc.s @@ -404,19 +404,19 @@ SER_IRQ: and #$08 beq Done ; Jump if no ACIA interrupt lda ACIA_DATA,x ; Get byte from ACIA - ldy RecvFreeCnt ; Check if we have free space left + ldx RecvFreeCnt ; Check if we have free space left beq Flow ; Jump if no space in receive buffer ldy RecvTail ; Load buffer pointer sta RecvBuf,y ; Store received byte in buffer inc RecvTail ; Increment buffer pointer dec RecvFreeCnt ; Decrement free space counter - ldy RecvFreeCnt ; Check for buffer space low - cpy #33 + cpx #33 ; Check for buffer space low bcc Flow ; Assert flow control if buffer space low rts ; Interrupt handled (carry already set) ; Assert flow control if buffer space too low -Flow: lda RtsOff +Flow: ldx Index +lda RtsOff sta ACIA_CMD,x sta Stopped sec ; Interrupt handled diff --git a/libsrc/atmos/ser/atmos-acia.s b/libsrc/atmos/ser/atmos-acia.s index ed5386471..774a337bd 100644 --- a/libsrc/atmos/ser/atmos-acia.s +++ b/libsrc/atmos/ser/atmos-acia.s @@ -329,19 +329,19 @@ SER_IRQ: and #$08 beq Done ; Jump if no ACIA interrupt lda ACIA::DATA,x ; Get byte from ACIA - ldy RecvFreeCnt ; Check if we have free space left + ldx RecvFreeCnt ; Check if we have free space left beq Flow ; Jump if no space in receive buffer ldy RecvTail ; Load buffer pointer sta RecvBuf,y ; Store received byte in buffer inc RecvTail ; Increment buffer pointer dec RecvFreeCnt ; Decrement free space counter - ldy RecvFreeCnt ; Check for buffer space low - cpy #33 + cpx #33 bcc Flow ; Assert flow control if buffer space low rts ; Interrupt handled (carry already set) ; Assert flow control if buffer space too low -Flow: lda RtsOff +Flow: ldx Index ; Reload port + lda RtsOff sta ACIA::CMD,x sta Stopped sec ; Interrupt handled From e8f5ad24712f9ebad8b308ab57b673aebacd9c0a Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Wed, 30 Aug 2023 21:02:47 +0200 Subject: [PATCH 3/5] Serial: Optimize SER_PUT We don't need to reload SendFreeCnt if we don't jsr TryToSend. --- libsrc/apple2/ser/a2.ssc.s | 7 ++++--- libsrc/atmos/ser/atmos-acia.s | 7 ++++--- libsrc/c128/ser/c128-swlink.s | 12 +++++++----- libsrc/c64/ser/c64-swlink.s | 12 +++++++----- libsrc/cbm510/ser/cbm510-std.s | 12 +++++++----- libsrc/cbm610/ser/cbm610-std.s | 12 +++++++----- libsrc/plus4/ser/plus4-stdser.s | 12 +++++++----- 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/libsrc/apple2/ser/a2.ssc.s b/libsrc/apple2/ser/a2.ssc.s index 1dbf5f3a4..7705f0933 100644 --- a/libsrc/apple2/ser/a2.ssc.s +++ b/libsrc/apple2/ser/a2.ssc.s @@ -328,20 +328,21 @@ SER_PUT: ; Try to send ldy SendFreeCnt - iny ; Y = $FF? + cpy #$FF ; Nothing to flush beq :+ pha lda #$00 ; TryHard = false jsr TryToSend pla - ; Put byte into send buffer & send -: ldy SendFreeCnt + ; Reload SendFreeCnt after TryToSend + ldy SendFreeCnt bne :+ lda #SER_ERR_OVERFLOW ldx #0 ; return value is char rts + ; Put byte into send buffer & send : ldy SendTail sta SendBuf,y inc SendTail diff --git a/libsrc/atmos/ser/atmos-acia.s b/libsrc/atmos/ser/atmos-acia.s index 774a337bd..c918fe995 100644 --- a/libsrc/atmos/ser/atmos-acia.s +++ b/libsrc/atmos/ser/atmos-acia.s @@ -269,20 +269,21 @@ SER_GET: SER_PUT: ; Try to send ldy SendFreeCnt - iny ; Y = $FF? + cpy #$FF ; Nothing to flush beq :+ pha lda #$00 ; TryHard = false jsr TryToSend pla - ; Put byte into send buffer & send -: ldy SendFreeCnt + ; Reload SendFreeCnt after TryToSend + ldy SendFreeCnt bne :+ lda #SER_ERR_OVERFLOW ldx #0 ; return value is char rts + ; Put byte into send buffer & send : ldy SendTail sta SendBuf,y inc SendTail diff --git a/libsrc/c128/ser/c128-swlink.s b/libsrc/c128/ser/c128-swlink.s index 57b5ea3b2..a0fc43050 100644 --- a/libsrc/c128/ser/c128-swlink.s +++ b/libsrc/c128/ser/c128-swlink.s @@ -362,21 +362,23 @@ SER_PUT: ; Try to send ldx SendFreeCnt - inx ; X = $ff? + cpx #$FF ; Nothing to flush beq @L2 pha lda #$00 jsr TryToSend pla -; Put byte into send buffer & send +; Reload SendFreeCnt after TryToSend -@L2: ldx SendFreeCnt - bne @L3 + ldx SendFreeCnt + bne @L2 lda #SER_ERR_OVERFLOW ; X is already zero rts -@L3: ldx SendTail +; Put byte into send buffer & send + +@L2: ldx SendTail sta SendBuf,x inc SendTail dec SendFreeCnt diff --git a/libsrc/c64/ser/c64-swlink.s b/libsrc/c64/ser/c64-swlink.s index 8c8b8bf39..c2934e669 100644 --- a/libsrc/c64/ser/c64-swlink.s +++ b/libsrc/c64/ser/c64-swlink.s @@ -336,21 +336,23 @@ SER_PUT: ; Try to send ldx SendFreeCnt - inx ; X = $ff? + cpx #$FF ; Nothing to flush beq @L2 pha lda #$00 jsr TryToSend pla -; Put byte into send buffer & send +; Reload SendFreeCnt after TryToSend -@L2: ldx SendFreeCnt - bne @L3 + ldx SendFreeCnt + bne @L2 lda #SER_ERR_OVERFLOW ; X is already zero rts -@L3: ldx SendTail +; Put byte into send buffer & send + +@L2: ldx SendTail sta SendBuf,x inc SendTail dec SendFreeCnt diff --git a/libsrc/cbm510/ser/cbm510-std.s b/libsrc/cbm510/ser/cbm510-std.s index 34fe02d0f..633a392c7 100644 --- a/libsrc/cbm510/ser/cbm510-std.s +++ b/libsrc/cbm510/ser/cbm510-std.s @@ -292,21 +292,23 @@ SER_PUT: ; Try to send ldx SendFreeCnt - inx ; X = $ff? + cpx #$FF ; Nothing to flush beq @L2 pha lda #$00 jsr TryToSend pla -; Put byte into send buffer & send +; Reload SendFreeCnt after TryToSend -@L2: ldx SendFreeCnt - bne @L3 + ldx SendFreeCnt + bne @L2 lda #SER_ERR_OVERFLOW ; X is already zero rts -@L3: ldx SendTail +; Put byte into send buffer & send + +@L2: ldx SendTail sta SendBuf,x inc SendTail dec SendFreeCnt diff --git a/libsrc/cbm610/ser/cbm610-std.s b/libsrc/cbm610/ser/cbm610-std.s index c241d5908..1216e3794 100644 --- a/libsrc/cbm610/ser/cbm610-std.s +++ b/libsrc/cbm610/ser/cbm610-std.s @@ -293,21 +293,23 @@ SER_PUT: ; Try to send ldx SendFreeCnt - inx ; X = $ff? + cpx #$ff ; Nothing to flush beq @L2 pha lda #$00 jsr TryToSend pla -; Put byte into send buffer & send +; Reload SendFreeCnt after TryToSend -@L2: ldx SendFreeCnt - bne @L3 + ldx SendFreeCnt + bne @L2 lda #SER_ERR_OVERFLOW ; X is already zero rts -@L3: ldx SendTail +; Put byte into send buffer & send + +@L2: ldx SendTail sta SendBuf,x inc SendTail dec SendFreeCnt diff --git a/libsrc/plus4/ser/plus4-stdser.s b/libsrc/plus4/ser/plus4-stdser.s index 5c4c550e6..d1d84267f 100644 --- a/libsrc/plus4/ser/plus4-stdser.s +++ b/libsrc/plus4/ser/plus4-stdser.s @@ -300,21 +300,23 @@ SER_PUT: ; Try to send ldx SendFreeCnt - inx ; X = $ff? + cpx #$ff ; Nothing to flush beq @L2 pha lda #$00 jsr TryToSend pla -; Put byte into send buffer & send +; Reload SendFreeCnt after TryToSend -@L2: ldx SendFreeCnt - bne @L3 + ldx SendFreeCnt + bne @L2 lda #SER_ERR_OVERFLOW ; X is already zero rts -@L3: ldx SendTail +; Put byte into send buffer & send + +@L2: ldx SendTail sta SendBuf,x inc SendTail dec SendFreeCnt From a89a311abfda385b1c1a9a0bb654180681c23d74 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Wed, 30 Aug 2023 20:51:48 +0200 Subject: [PATCH 4/5] Serial: Micro-optimisation for apple2enh On 65C02 we can spare one cycle on sta (abs); we also not have to care about the 6502 false reads --- libsrc/apple2/ser/a2.ssc.s | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libsrc/apple2/ser/a2.ssc.s b/libsrc/apple2/ser/a2.ssc.s index 7705f0933..12e975965 100644 --- a/libsrc/apple2/ser/a2.ssc.s +++ b/libsrc/apple2/ser/a2.ssc.s @@ -26,6 +26,7 @@ .include "ser-error.inc" .macpack module + .macpack cpu ; ------------------------------------------------------------------------ ; Header. Includes jump table @@ -57,9 +58,13 @@ ;---------------------------------------------------------------------------- ; I/O definitions +.if (.cpu .bitand CPU_ISET_65C02) +ACIA = $C088 +.else Offset = $8F ; Move 6502 false read out of I/O to page $BF - ACIA = $C088-Offset +.endif + ACIA_DATA = ACIA+0 ; Data register ACIA_STATUS = ACIA+1 ; Status register ACIA_CMD = ACIA+2 ; Command register @@ -200,7 +205,9 @@ SER_OPEN: asl asl asl +.if .not (.cpu .bitand CPU_ISET_65C02) adc #Offset ; Assume carry to be clear +.endif tax ; Check if the handshake setting is valid @@ -315,7 +322,11 @@ SER_GET: inc RecvHead inc RecvFreeCnt ldx #$00 ; (59) +.if (.cpu .bitand CPU_ISET_65C02) + sta (ptr1) +.else sta (ptr1,x) +.endif txa ; Return code = 0 rts From 734541ee05000bad9e9dd2f903a2db754e8c36f3 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Wed, 30 Aug 2023 20:49:39 +0200 Subject: [PATCH 5/5] Serial: Optimize SER_GET There is no need to TryToSend before getting the character. We send bytes during SER_PUT, and if interrupted during sending, we still try to do it at the beginning of the next SER_PUT. --- libsrc/apple2/ser/a2.ssc.s | 7 +------ libsrc/atmos/ser/atmos-acia.s | 8 +------- libsrc/c128/ser/c128-swlink.s | 7 +------ libsrc/c64/ser/c64-swlink.s | 7 +------ libsrc/cbm510/ser/cbm510-std.s | 7 +------ libsrc/cbm610/ser/cbm610-std.s | 7 +------ libsrc/plus4/ser/plus4-stdser.s | 7 +------ 7 files changed, 7 insertions(+), 43 deletions(-) diff --git a/libsrc/apple2/ser/a2.ssc.s b/libsrc/apple2/ser/a2.ssc.s index 12e975965..5942d43e9 100644 --- a/libsrc/apple2/ser/a2.ssc.s +++ b/libsrc/apple2/ser/a2.ssc.s @@ -291,14 +291,9 @@ InvBaud:lda #SER_ERR_BAUD_UNAVAIL SER_GET: ldx Index - ldy SendFreeCnt ; Send data if necessary - iny ; Y == $FF? - beq :+ - lda #$00 ; TryHard = false - jsr TryToSend ; Check for buffer empty -: lda RecvFreeCnt ; (25) + lda RecvFreeCnt ; (25) cmp #$FF bne :+ lda #SER_ERR_NO_DATA diff --git a/libsrc/atmos/ser/atmos-acia.s b/libsrc/atmos/ser/atmos-acia.s index c918fe995..1509803d2 100644 --- a/libsrc/atmos/ser/atmos-acia.s +++ b/libsrc/atmos/ser/atmos-acia.s @@ -227,14 +227,8 @@ InvBaud:lda #