From e444f4c40e919a5ada2c52fdea8420a9d6dff74c Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Tue, 27 May 2025 22:42:07 +0200 Subject: [PATCH] Apple2: Safely check for 80-columns card Check the Pascal ID bytes for an 80-columns card to avoid blindly jumping into a $C300 that could be totally unrelated. --- libsrc/apple2/detect80cols.s | 56 ++++++++++++++++++++++++++++++++++++ libsrc/apple2/exec.s | 7 +++-- libsrc/apple2/videomode.s | 15 ++-------- 3 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 libsrc/apple2/detect80cols.s diff --git a/libsrc/apple2/detect80cols.s b/libsrc/apple2/detect80cols.s new file mode 100644 index 000000000..19e6fda74 --- /dev/null +++ b/libsrc/apple2/detect80cols.s @@ -0,0 +1,56 @@ +; +; Colin Leroy-Mira, 27/05/2025 +; +; Verify the presence of a 80 columns card in slot 3, +; and publish a flag accordingly. +; + .export aux80col + + .ifndef __APPLE2ENH__ + .import machinetype + .endif + + .constructor detect80cols + + .include "apple2.inc" + + .data + +aux80col: .byte 0 + + .segment "ONCE" + +IdOfsTable: ; Table of bytes positions, used to check four + ; specific bytes on the slot's firmware to make + ; sure this is a serial card. + .byte $05 ; Pascal 1.0 ID byte + .byte $07 ; Pascal 1.0 ID byte + .byte $0B ; Pascal 1.1 generic signature byte + .byte $0C ; Device signature byte + +IdValTable: ; Table of expected values for the four checked + ; bytes + .byte $38 ; ID Byte 0 (from Pascal 1.0), fixed + .byte $18 ; ID Byte 1 (from Pascal 1.0), fixed + .byte $01 ; Generic signature for Pascal 1.1, fixed + .byte $88 ; Device signature byte (80 columns card) + +IdTableLen = * - IdValTable + +detect80cols: + .ifndef __APPLE2ENH__ + lda machinetype ; Check we're on a //e at least, otherwise we + bpl NoDev ; handle no 80cols hardware (like Videx) + .endif + + ldx #IdTableLen-1 +: ldy IdOfsTable,x ; Check Pascal 1.1 Firmware Protocol ID bytes + lda IdValTable,x + cmp $C300,y + bne NoDev + dex + bpl :- + + dec aux80col ; We have an 80-columns card! Set flag to $FF + +NoDev: rts diff --git a/libsrc/apple2/exec.s b/libsrc/apple2/exec.s index ec90f19bb..4e5e77a6e 100644 --- a/libsrc/apple2/exec.s +++ b/libsrc/apple2/exec.s @@ -6,6 +6,7 @@ .export _exec .import mli_file_info_direct + .import aux80col .import pushname, popname, popax, done, _exit .include "zeropage.inc" @@ -115,7 +116,8 @@ setbuf: lda #$00 ; Low byte sta io_buffer stx io_buffer+1 - .ifdef __APPLE2ENH__ + bit aux80col + bpl :+ ; Calling the 80 column firmware needs the ROM switched ; in, otherwise it copies the F8 ROM to the LC (@ $CEF4) bit $C082 @@ -128,9 +130,8 @@ setbuf: lda #$00 ; Low byte ; Switch in LC bank 2 for R/O bit $C080 - .endif - ; Reset stack as we already passed +: ; Reset stack as we already passed ; the point of no return anyway ldx #$FF txs diff --git a/libsrc/apple2/videomode.s b/libsrc/apple2/videomode.s index d8310427f..ea9eb28df 100644 --- a/libsrc/apple2/videomode.s +++ b/libsrc/apple2/videomode.s @@ -5,14 +5,10 @@ ; .export _videomode - .ifndef __APPLE2ENH__ - .import machinetype - .endif - + .import aux80col .import returnFFFF .include "apple2.inc" - .include "mli.inc" VIDEOMODE_40x24 = $15 @@ -21,19 +17,14 @@ VIDEOMODE_80x24 = $00 .segment "LOWCODE" _videomode: - ; Functionally equivalent to previous assumption that - ; __APPLE2ENH__ == 80 columns hardware present. Will be - ; correctly checked in the very near future. - .ifndef __APPLE2ENH__ - bit machinetype - bvs set_mode + bit aux80col + bmi set_mode ; No 80 column card, return error if requested mode is 80cols cmp #VIDEOMODE_40x24 beq out jmp returnFFFF set_mode: - .endif ; Get and save current videomode flag bit RD80VID