keystone.s
Original bytes preserved. This is a browser-readable view.
; CS617 CH08 Keystone r019 CALIBRATED HALF-TM BLINK from r018.
; Alex Barragan & ChatGPT (Sol), October 15, 2026.
; No language ROM initialization or keyv540 changes. Runtime untested.
; r018 is frozen as dual-machine human-observed one-second blink control.
; Original Keystone logic began at 0E80. This integrated module is NEW
; adapter code at 6000+, not byte-identical; preservation must be re-tested.
; keystoneROM.s r0.9 in keystone-10.zip remains frozen and untouched.
ORG &H6000
K8_ENTRY:
PRE SS,SSPTP
PRE US,USPTP
; r011 AI-1000 emulator-only entry probe:
; DELIBERATELY OMIT the PB-2000C C-ROM display preamble inherited
; from r004 (SYSCLR/SETLCD/SET4LN through SYSCAL).
; The shared CH08_INIT below was already used by the launcher,
; Tiny and Life on both models. This tests whether those PB-only
; calls block the AI-1000 before Keystone initializes.
; NO claim is made that cursor initialization/blink works on AI-1000.
; Fresh app contract: initialize display with portable ROM0 services.
CAL CH08_INIT
CAL CH08_ARM_BRK
CAL CH08_CLEAR
LD $6,$SX
PRE IZ,K8_L1
CAL CH08_PRINT
LD $6,&H20
PRE IZ,K8_L2
CAL CH08_PRINT
LD $6,&H60
PRE IZ,K8_INPUT
CAL CH08_PRINT
; Restore blinking row-four insertion cursor as in keystone 0.9.
LD $6,&H61
CAL K8_SETCUR
CAL &H0304
CAL &H0306
; Cursor experiment r001: set ONLY LCDST bit 02h once after refresh.
; No keyboard-library change, no blink-timing assumption.
CAL K8_CURON
; EXPERIMENT r003: prime cursor flash-rate and flash counter.
; The rest of r002 idle service remains unchanged.
CAL K8_BLINKINIT
; EXPERIMENTAL v0.2: keyv540 borrows native KEYMD (RAM1:09E4h).
; K540_GET reloads CAPS from RAM each time; register $13 is disposable.
; Host C-ROM keyboard services are NOT called while driver is active.
; Application chooses the return policy for MENU and BRK events.
; Red S and KANA remain deferred; the compiled driver has NO C-ROM calls.
LD $11,1
CAL K540_INIT ; initialize library-owned native mode byte
CAL SYS540_INIT ; shared timer/overlay state; one init per entry
LD $6,&H61
LD $16,&H20
CAL SYS540_PLACE ; public API: initial blank insertion cell
K8_LOOP:
; r007 STATIC CANDIDATE: card-owned nonblocking keyboard readiness.
; Service ROM0 cursor only during idle, not at every raw-key scan.
; No host CRTKY while KEYMD belongs to keyv540. Caller is PB-only here
; because the inherited r004 display preamble uses PB C-ROM addresses.
K8_IDLE_POLL:
CAL K540_RAW
AN $17,$17
JP NZ,K8_DISPATCH
CAL SYS540_POLL ; shared due predicate; NO graphics here
AN $18,$18
JP Z,K8_IDLE_POLL
CAL SYS540_TOGGLE ; shared renderer AFTER checker returns
JP K8_IDLE_POLL
K8_DISPATCH:
; TEST ONLY: poison scratch $13. Library MUST reload CAPS from RAM.
LD $13,&H5A
CAL K540_GET ; $17 event, $0 code, $13 CAPS mode
LD $15,$17
SBC $15,K540_EV_CAPS
JP Z,K8_LOOP ; modifier event: no character inserted
LD $15,$17
SBC $15,K540_EV_MENU
JP Z,K8_QUIT ; MENU -> caller-selected warm exit
LD $15,$17
SBC $15,K540_EV_BRK
JP Z,K8_QUIT ; BRK -> caller-selected warm exit
LD $14,$0 ; printable/nonprintable source key
K8_CASE_DONE:
LD $6,&H20
PRE IZ,K8_TKEY
CAL CH08_PRINT
LD $12,$14
CAL K8_SHOWHEX
PRE IZ,K8_TMODE
CAL CH08_PRINT
CAL K540_MODE ; refresh mode AFTER ROM0 LCD calls
LD $12,$13
CAL K8_SHOWHEX
; Preserve original 20h..7Eh inclusive printable acceptance.
LD $16,$14
SBC $16,&H20
JP C,K8_LOOP
LD $16,$14
SBC $16,&H7F
JP NC,K8_LOOP
; Stop drawing before modifying the cell beneath the cursor.
CAL SYS540_HIDE
; 30th printable stays; 31st clears before starting anew.
LD $16,$11
SBC $16,&H1F
JP NZ,K8_CHAR
LD $6,&H60
PRE IZ,K8_INPUT
CAL CH08_PRINT
LD $11,1
K8_CHAR:
LD $6,$11
AD $6,&H60
LD $16,$14
CAL K8_PUTC
AD $11,$30
LD $6,$11
AD $6,&H60
CAL K8_SETCUR
LD $6,$11
SBC $6,&H1F
JP Z,K8_NO_INS_CUR ; full field: protect right bracket
LD $6,$11
AD $6,&H60
LD $16,&H20
CAL SYS540_PLACE ; shared position & repaint
K8_NO_INS_CUR:
JP K8_LOOP
; Exit policy belongs to Keystone, not the keyboard driver.
K8_QUIT:
CAL SYS540_STOP ; public API: restore underlying cell
CAL K540_RESET ; leave native KEYMD in neutral state
JP CH08_WARM
; Keystone 0.9 functional routines with names scoped to guest.
K8_SETCUR:
PST UA,&H54
PRE IX,EDCSR
ST $6,(IX+$SX)
PST UA,&H14
RTN
; EXPERIMENT r001: RAM1 LCDST=08CAh, cursor-enable bit=02h.
; Change only this display bit; preserve all other LCDST bits.
; Entry/exit UA=14h; clobbers $1 and IX. 02A3h helper
; deliberately NOT called until its full ABI is verified.
K8_CURON:
PST UA,&H54
PRE IX,&H08CA
LD $1,(IX+$SX)
OR $1,&H02
ST $1,(IX+$SX)
PST UA,&H14
RTN
; EXPERIMENT r003: isolated native blink counter initialization.
; RAM1:08D8h = flash-rate reload; 08D7h = live counter.
; 5Fh is the preserved C-ROM SYSCLR default, not a measured
; current value for the new card application.
; Entry/exit UA=14h. Clobbers $1 and IX.
K8_BLINKINIT:
PST UA,&H54
LD $1,&H5F
PRE IX,&H08D8
ST $1,(IX+$SX)
PRE IX,&H08D7
ST $1,(IX+$SX)
PST UA,&H14
RTN
; r010 ADAPTIVE HALF-TM cursor cadence. PB-only experiment.
; r009 toggled once per GST TM transition; DIV=01h is already the
; minimum possible divisor. r010 preserves that proven edge toggle,
; measures one TM interval in idle raw scans (counting every second
; scan), then schedules ONE midpoint toggle in the next interval.
; Expected after startup calibration: approx. 2 toggles per TM interval.
; No changes to keyv540 or hardware keyboard decoding.
; The baseline r009 service remains active even if midpoint times out.
;
; RAM1 2F00h..2F07h are temporary Keystone-only candidate storage:
; 2F00 last TM; 2F01 unused by r010; 2F02..03 half-scan count;
; 2F04..05 countdown; 2F06 midpoint armed; 2F07 alternate-scan bit.
; Neither hardware ownership nor 0368h ABI has been validated on AI-1000.
K8_TICK_LAST: EQU &H2F00
K8_TICK_MEAS: EQU &H2F02
K8_TICK_HALF: EQU &H2F04
K8_TICK_ARM: EQU &H2F06
K8_TICK_ODD: EQU &H2F07
K8_TICK_INIT:
PHS $11 ; preserve Keystone insertion index
GST TM,$8
GST TM,$8
PST UA,&H54
PRE IX,K8_TICK_LAST
ST $8,(IX+$SX)
LDW $10,0
PRE IX,K8_TICK_MEAS
STW $10,(IX+$SX)
PRE IX,K8_TICK_HALF
STW $10,(IX+$SX)
PRE IX,K8_TICK_ARM
ST $31,(IX+$SX)
PRE IX,K8_TICK_ODD
ST $31,(IX+$SX)
PST UA,&H14
PPS $11
RTN
K8_TICK_IDLE:
PHS $11 ; 16-bit count uses $10/$11
; Edge sampling stays byte-for-byte in spirit with the proven r009.
GST TM,$8
GST TM,$8
PST UA,&H54
PRE IX,K8_TICK_LAST
LD $9,(IX+$SX)
SBC $9,$8
JP NZ,K8_TICK_EDGE
; No TM change. Measure the current interval in half-scan units.
; The midpoint countdown is decremented on EVERY idle scan, hence
; a half-scan measurement yields a half-TM-period countdown.
PRE IX,K8_TICK_ODD
LD $9,(IX+$SX)
XR $9,$30
ST $9,(IX+$SX)
AN $9,$9
JP Z,K8_TICK_NOCOUNT
PRE IX,K8_TICK_MEAS
LDW $10,(IX+$SX)
ADW $10,$SY
STW $10,(IX+$SX)
K8_TICK_NOCOUNT:
PRE IX,K8_TICK_ARM
LD $9,(IX+$SX)
AN $9,$9
JP Z,K8_TICK_RETURN
PRE IX,K8_TICK_HALF
LDW $10,(IX+$SX)
SBW $10,$SY
JP Z,K8_TICK_MIDPOINT
STW $10,(IX+$SX)
JP K8_TICK_RETURN
K8_TICK_MIDPOINT:
STW $10,(IX+$SX)
PRE IX,K8_TICK_ARM
ST $31,(IX+$SX) ; one midpoint per TM interval
JP K8_TICK_TOGGLE
K8_TICK_EDGE:
; TM just changed: r009 baseline phase toggle always happens.
; Use half of the LAST measured interval for the NEXT midpoint.
ST $8,(IX+$SX) ; K8_TICK_LAST
PRE IX,K8_TICK_MEAS
LDW $10,(IX+$SX)
LDW $12,0
STW $12,(IX+$SX) ; start fresh interval measurement
PRE IX,K8_TICK_HALF
STW $10,(IX+$SX)
PRE IX,K8_TICK_ODD
ST $31,(IX+$SX) ; reset alternating-scan phase
PRE IX,K8_TICK_ARM
LD $9,$10
OR $9,$11 ; measured half-count nonzero?
AN $9,$9
JP Z,K8_TICK_DISARM
LD $9,$30 ; arm when calibration is nonzero
K8_TICK_DISARM:
ST $9,(IX+$SX)
JP K8_TICK_TOGGLE
K8_TICK_RETURN:
PST UA,&H14
PPS $11
RTN
K8_TICK_TOGGLE:
; Same native service path as r009. Entry already saved $11.
PST UA,&H14
CAL &H02A3
CAL &H0368
PST UA,&H14
PPS $11
RTN
; Shared blink timing and rendering now live ONLY in sysv540.s.
; r020 originals remain archived and immutable.
K8_SHOWHEX:
LD $16,$12
DID $16
CAL K8_HEXASC
CAL K8_PUTC
LD $16,$12
CAL K8_HEXASC
CAL K8_PUTC
RTN
K8_HEXASC:
AN $16,&H0F
AD $16,&H30
SBC $16,&H3A
RTN C
AD $16,&H07
RTN
K8_PUTC:
LD $7,$16
LD $0,$7
DIU $7
DID $0
OR $7,$SZ
CAL &H0197
RTN
K8_L1:
DB "Press Key Xandrian Labs",0
K8_L2:
DB "Key=-- Mode=-- keystone .9",0
K8_INPUT:
DB "[ ]",0
K8_TKEY:
DB "Key=",0
K8_TMODE:
DB " Mode=",0