ch01v2.s
Original bytes preserved. This is a browser-readable view.
; ======================================================================
; CS617 chapter01-u32calc-v2 | shared VT22 r022 candidate
; Alex Barragan and ChatGPT (Solcito), 2026-10-24.
;
; FIRST REAL OWNER OBSERVATION: r001 cold menu boots in the emulator.
; OWNER FEEDBACK: use the accepted Menu service and its default
; digits 1,2,3,4 as shortcuts, even for diagnostic menus.
;
; Retains r014 tape/recall; adds ROM-backed read-only [info] viewer;
; protected zero division, checked U32 and first-char error cursor.
; FK1 now routes to historical r003 classical UI; FK3 seeds examples.
; r022 shared-terminal migration candidate; native runtime not yet tested.
; Only new application code/data at and above ORG 0E80.
; ======================================================================
V2_ACTIVE: EQU &H2B00 ; bank UA=14, 0 chooser, 1..4 menu pages
V2_FMT: EQU &H2B10 ; 11 bytes output incl NUL
V2_MERR: EQU &H2B20 ; menu init failure 1..3
V2_LEN: EQU &H2B30 ; calcv2 editor content length
V2_BUF: EQU &H2B40 ; 120-byte text field (119 + terminal NUL)
V2_STAT: EQU &H2BC0 ; 0/1 normal, 2 overflow, 3 syntax, 4 empty, 5 under, 6 zero, 7 depth
V2_ANS: EQU &H2BC4 ; four-byte last-successful-U32 value
; 2BC8..2C47: retired r008 one-operator scratch, left untouched
V2_TLEN: EQU &H2C48 ; decimal formatter return length
V2_EROW: EQU &H2C49 ; dynamic editor line start 40h or 60h
V2_SCROLL: EQU &H2C50 ; 3x32 visible rows 2C50..2CAF
; LCD-only cache retained; the independent VT20 canvas holds tape.
ORG &H0E80
V2_COLD:
PRE SS,&H0100
LD $30,1
LD $31,0
PSR SZ,0
PSR SY,30
PSR SX,31
PST UA,&H14
PRE IX,RCS_TOP
PRE IY,RCS_END-1
PRE IZ,RCS
PST UA,&H44
BUP
PST UA,&H14
PRE SS,SSPTP
PRE US,USPTP
CAL SYS_INIT
CAL SCREEN_INIT
PRE IX,V2_ACTIVE
LD $0,0
ST $0,(IX+$SX)
; Only a genuine cold entry clears the editable text.
PRE IX,V2_LEN
ST $0,(IX+$SX)
PRE IX,V2_BUF
ST $0,(IX+$SX)
PRE IX,V2_STAT
ST $0,(IX+$SX)
LDW $0,0
PRE IX,V2_ANS
STW $0,(IX+$SX)
PRE IX,V2_ANS+2
STW $0,(IX+$SX)
CAL V2_LCD_INIT
; Genuine cold start clears the VT20 tape and its row metadata ONCE.
CAL T12_COLD
; Genuine cold start initializes all 26 register values to zero.
CAL V9_REG_COLD
; The historical v1 owns independent, relocated warm state.
CAL C1_COLD
LDW $10,V2_SPLASH
LD $12,3
CAL SPL_SHOW
JP V2_HOME
V2_WARM:
; Rebuild processor/card environment. Preserve active page across BRK.
PRE SS,&H0100
LD $30,1
LD $31,0
PSR SZ,0
PSR SY,30
PSR SX,31
PST UA,&H14
PRE IX,RCS_TOP
PRE IY,RCS_END-1
PRE IZ,RCS
PST UA,&H44
BUP
PST UA,&H14
PRE SS,SSPTP
PRE US,USPTP
CAL SYS_INIT
CAL SCREEN_INIT
CAL KBD_INIT
PRE IX,V2_ACTIVE
LD $0,(IX+$SX)
SBC $0,1
JP Z,V2_SHOW1
SBC $0,2
JP Z,V2_SHOW2
SBC $0,3
JP Z,V2_LIB_VIEW
SBC $0,4
JP Z,V2_SHOW4
JP V2_HOME
V2_HOME:
; Use lib-set-01 Menu, NOT an ad-hoc FK scanner. One 68-byte group.
; Explicit aliases all 0: default ASCII 1,2,3,4 and physical FK1..4.
CAL KBD_INIT
LDW $10,V2_MENU_GROUP
LD $12,1
CAL MNU_INIT
AN $0,$0
JP NZ,V2_BAD_MENU
V2_MENU:
CAL MNU_SHOW
AN $0,$0
JP NZ,V2_BAD_MENU
V2_MENU_WAIT:
CAL MNU_WAIT
SBC $0,1
JP Z,V2_MENU
SBC $0,2
JP Z,V2_WARM
AN $0,$0
JP NZ,V2_BAD_MENU
; Menu's selected ordinal in $9 is valid only on success.
SBC $9,1
JP Z,V2_SELECT1
SBC $9,2
JP Z,V2_SELECT2
SBC $9,3
JP Z,V2_SELECT3
SBC $9,4
JP Z,V2_SELECT4
JP V2_BAD_MENU
V2_BAD_MENU:
; A visible diagnostic if the reusable service rejects our table.
PRE IX,V2_MERR
ST $0,(IX+$SX)
CAL LCD_CLEAR
LD $6,0
PRE IZ,V2_MERR_MSG
CAL LCD_PUTZ
PRE IX,V2_MERR
LD $16,(IX+$SX)
AD $16,&H30
LD $6,&H24
CAL PUTC
V2_MERR_LOOP:
CAL KBD_EVENT
SBC $17,K540_EV_BRK
JP Z,V2_WARM
SBC $17,K540_EV_MENU
JP Z,V2_HOME
JP V2_MERR_LOOP
V2_SELECT1:
LD $0,1
JP V2_STORE_SHOW1
V2_SELECT2:
LD $0,2
JP V2_STORE_SHOW2
V2_SELECT3:
LD $0,3
JP V2_STORE_SHOW3
V2_SELECT4:
LD $0,4
JP V2_STORE_SHOW4
V2_STORE_SHOW1:
PRE IX,V2_ACTIVE
ST $0,(IX+$SX)
JP V2_SHOW1
V2_STORE_SHOW2:
PRE IX,V2_ACTIVE
ST $0,(IX+$SX)
JP V2_SHOW2
V2_STORE_SHOW3:
PRE IX,V2_ACTIVE
ST $0,(IX+$SX)
JP V2_SHOW3
V2_STORE_SHOW4:
PRE IX,V2_ACTIVE
ST $0,(IX+$SX)
JP V2_SHOW4
V2_SHOW1:
; Historical r003 calculator, not a probe or modernized v2 editor.
JP C1_ENTER
V2_SHOW2:
; One reusable editor, app-owned rolling three-row LCD cache.
JP V2_EDITOR_PAGE
V2_EDITOR_PAGE:
CAL LCD_CLEAR
PST UA,&H14
PRE IX,V2_STAT
LD $0,(IX+$SX)
SBC $0,2
JP Z,V2_LCD_ERROR
SBC $0,3
JP Z,V2_LCD_ERROR
SBC $0,4
JP Z,V2_LCD_ERROR
SBC $0,5
JP Z,V2_LCD_ERROR
SBC $0,6
JP Z,V2_LCD_ERROR
SBC $0,7
JP Z,V2_LCD_ERROR
; Success: history on rows 1..3, fresh empty input on row 4.
CAL V2_DRAW3
LD $0,&H60
PRE IX,V2_EROW
ST $0,(IX+$SX)
JP V2_EDIT_OPEN
V2_LCD_ERROR:
; Keep editor on row 3 and diagnosis immediately underneath row 4.
; Neither the error nor the expression is added to LCD history here.
CAL V2_DRAW2
PST UA,&H14
PRE IX,V2_EROW
LD $0,&H40
ST $0,(IX+$SX)
PRE IX,V2_STAT
LD $0,(IX+$SX)
SBC $0,2
JP Z,V2_SHOW_OVERFLOW
SBC $0,3
JP Z,V2_SHOW_SYNTAX
SBC $0,4
JP Z,V2_SHOW_EMPTY
SBC $0,5
JP Z,V2_SHOW_UFLOW
SBC $0,6
JP Z,V2_SHOW_DIVZERO
PRE IZ,V2_ERR_DEPTH
JP V2_SHOW_DIAG
V2_SHOW_OVERFLOW:
PRE IZ,V2_ERR_OVERFLOW
JP V2_SHOW_DIAG
V2_SHOW_SYNTAX:
PRE IZ,V2_ERR_SYNTAX
JP V2_SHOW_DIAG
V2_SHOW_EMPTY:
PRE IZ,V2_ERR_EMPTY
JP V2_SHOW_DIAG
V2_SHOW_UFLOW:
PRE IZ,V2_ERR_UNDERFLOW
JP V2_SHOW_DIAG
V2_SHOW_DIVZERO:
PRE IZ,V2_ERR_DIVZERO
JP V2_SHOW_DIAG
V2_SHOW_DIAG:
LD $6,&H60
CAL LCD_PUTZ
JP V2_EDIT_OPEN
V2_EDIT_OPEN:
; The visible '>' is at column 1, INP_EDIT starts at column 2.
; Error editing uses row 3; success/normal editing uses row 4.
; Retain the 119-char RAM field with the original Input service.
PST UA,&H14
PRE IX,V2_EROW
LD $6,(IX+$SX)
LD $16,&H3E ; '>'
CAL PUTC
LDW $10,V2_BUF
LD $12,120
LD $13,31 ; LCD columns 2..32
PST UA,&H14
PRE IX,V2_EROW
LD $14,(IX+$SX)
AD $14,$SY ; starts just after '>'
LD $15,0 ; printable ASCII, expression-ready
LD $16,0 ; UP/DOWN ignored by this editor
PST UA,&H14
PRE IX,V2_LEN
LD $17,(IX+$SX)
LD $18,$17 ; default warm reopen at end of field
; A viewer Q has priority: preserve unfinished input AND actual cursor.
; Otherwise an evaluation failure begins the editor at source offset zero.
PRE IX,T12_RESUME
LD $0,(IX+$SX)
AN $0,$0
JR Z,V2_ERROR_CUR
LD $0,0
ST $0,(IX+$SX)
PRE IX,T12_CURSOR
LD $18,(IX+$SX)
JR V2_CURS_READY
V2_ERROR_CUR:
PRE IX,V2_STAT
LD $0,(IX+$SX)
AN $0,$0
JR Z,V2_CURS_READY
LD $18,0
V2_CURS_READY:
LD $19,1 ; overwrite default; INS toggles insert
CAL INP_EDIT
; Source-inspected Input adapter: INP_CUR survives return from INP_EDIT.
; Preserve public outputs $0 status, $8 key and $9 length exactly.
PST UA,&H14
PRE IX,INP_CUR
LD $1,(IX+$SX)
PRE IX,T12_CURSOR
ST $1,(IX+$SX)
; Preserve input length regardless of EXE/MENU/BRK. INP_EDIT has
; already terminated the field and released its keyboard session.
PRE IX,V2_LEN
ST $9,(IX+$SX)
SBC $0,1
JP Z,V2_HOME
SBC $0,2
JP Z,V2_WARM
AN $0,$0
JP NZ,V2_EDITOR_BAD
; EXE dispatches decimal/ans or one checked U32 infix operation.
; Physical FK1 is the calcv2 paper-tape key (PK1). Input returns it.
SBC $8,KEYF1
JP Z,T12_BROWSE
SBC $8,KEYEXE
JP Z,V2_EVAL_DISPATCH
JP V2_EDITOR_PAGE
V2_EVAL_DISPATCH:
; One bounded expression engine replaces r008's one-operator scanner.
; $4=0 success; 2 overflow,3 syntax,4 empty,5 underflow,6 zero,7 depth.
CAL V9_EVAL
AN $4,$4
JP Z,V2_COMMIT_RESULT
LD $0,$4
JP V2_SAVE_STATUS
V2_COMMIT_RESULT:
; Transactional ans: commit ONLY a successful, in-range U32 value.
PST UA,&H14
PRE IX,V2_ANS
STW $15,(IX+$SX)
PRE IX,V2_ANS+2
STW $17,(IX+$SX)
LD $0,1
JP V2_SAVE_STATUS
V2_SAVE_STATUS:
PST UA,&H14
PRE IX,V2_STAT
ST $0,(IX+$SX)
SBC $0,1
JP Z,V2_RECORD_OK
; A failed EXE is an actual paper-tape event too. Preserve editable
; offending input and its cursor-at-beginning error recovery.
CAL T12_RECORD
JP V2_EDITOR_PAGE
V2_RECORD_OK:
; Only a valid evaluation advances the visible LCD transcript.
; ans was committed by V2_COMMIT_RESULT and the source is intact.
PST UA,&H14
PRE IX,V2_ANS
LDW $15,(IX+$SX)
PRE IX,V2_ANS+2
LDW $17,(IX+$SX)
PRE IX,V2_FMT
CAL UFMT32DEC
LD $13,$14
AN $13,$13
JP Z,V2_EDITOR_BAD
SBC $13,11
JP NC,V2_EDITOR_BAD
PRE IX,V2_TLEN
ST $14,(IX+$SX)
CAL V2_APPEND_EXPR
CAL V2_APPEND_VAL
; Source and formatted value are still intact; append an actual tape
; record before clearing the successful input buffer.
CAL T12_RECORD
; Successful EXE starts a fresh, genuinely EMPTY edit buffer.
; Errors instead keep the buffer and cursor for correction.
PST UA,&H14
LD $0,0
PRE IX,V2_STAT
ST $0,(IX+$SX)
PRE IX,V2_LEN
ST $0,(IX+$SX)
PRE IX,V2_BUF
ST $0,(IX+$SX)
JP V2_EDITOR_PAGE
; ------------------ application-only visible LCD flow ---------------
V2_LCD_INIT:
; Clear 96 bytes. This is LCD scrollback only, not historical tape.
PST UA,&H14
PRE IX,V2_SCROLL
LD $13,0
LD $0,&H20
V2_INIT_LOOP:
ST $0,(IX+$13)
AD $13,$SY
SBC $13,96
JP C,V2_INIT_LOOP
RTN
V2_SHIFT_ROW:
; Shift three 32-cell rows up, then blank the last row.
; All offsets within 0..95, so 8-bit IX+register indexing suffices.
PST UA,&H14
PRE IX,V2_SCROLL
LD $13,32
LD $14,0
V2_SHIFT_LOOP:
LD $0,(IX+$13)
ST $0,(IX+$14)
AD $13,$SY
AD $14,$SY
SBC $14,64
JP C,V2_SHIFT_LOOP
LD $13,64
LD $0,&H20
V2_BLANK_LOOP:
ST $0,(IX+$13)
AD $13,$SY
SBC $13,96
JP C,V2_BLANK_LOOP
RTN
V2_APPEND_EXPR:
; Tape-like LCD summary: '>' then up to 31 expression characters.
; Over 31, display 28 characters + '...' after the prompt.
; Full 119-char source remains in V2_BUF until success is committed.
CAL V2_SHIFT_ROW
PST UA,&H14
PRE IX,V2_SCROLL
LD $0,&H3E
ST $0,(IX+64)
PRE IX,V2_LEN
LD $11,(IX+$SX)
SBC $11,32
JP C,V2_EXPR_COPY
LD $11,28
V2_EXPR_COPY:
LD $12,0
LD $14,65
V2_EXPR_LOOP:
SBC $12,$11
JP NC,V2_EXPR_DOTS
PRE IX,V2_BUF
LD $0,(IX+$12)
PRE IX,V2_SCROLL
ST $0,(IX+$14)
AD $12,$SY
AD $14,$SY
JP V2_EXPR_LOOP
V2_EXPR_DOTS:
PRE IX,V2_LEN
LD $0,(IX+$SX)
SBC $0,32
RTN C
PRE IX,V2_SCROLL
LD $0,&H2E
ST $0,(IX+93)
ST $0,(IX+94)
ST $0,(IX+95)
RTN
V2_APPEND_VAL:
; Result starts in column 1 without '='; the '>' identifies input.
CAL V2_SHIFT_ROW
PST UA,&H14
PRE IX,V2_TLEN
LD $11,(IX+$SX)
LD $12,0
LD $14,64
V2_VAL_LOOP:
SBC $12,$11
RTN NC
PRE IX,V2_FMT
LD $0,(IX+$12)
PRE IX,V2_SCROLL
ST $0,(IX+$14)
AD $12,$SY
AD $14,$SY
JP V2_VAL_LOOP
V2_DRAW3:
; Render all three cached lines into the upper three LCD rows.
PST UA,&H14
LDW $10,V2_SCROLL
LD $13,32
LD $6,0
CAL LCD_RAMN
PST UA,&H14
LDW $10,V2_SCROLL+32
LD $13,32
LD $6,&H20
CAL LCD_RAMN
PST UA,&H14
LDW $10,V2_SCROLL+64
LD $13,32
LD $6,&H40
CAL LCD_RAMN
RTN
V2_DRAW2:
; On error, two recent rows above the editable offending text.
PST UA,&H14
LDW $10,V2_SCROLL+32
LD $13,32
LD $6,0
CAL LCD_RAMN
PST UA,&H14
LDW $10,V2_SCROLL+64
LD $13,32
LD $6,&H20
CAL LCD_RAMN
RTN
V2_EDITOR_BAD:
CAL LCD_CLEAR
LD $6,0
PRE IZ,V2_EDIT_ERROR
CAL LCD_PUTZ
JP V2_PAGE_WAIT
V2_SHOW3:
; [lib] launches a read-only browse of the SAME RAM tape used by v2.
; An empty tape receives the examples immediately. A populated tape
; is NEVER overwritten without explicit Y/EXE confirmation.
PST UA,&H14
PRE IX,T12_USED
LD $0,(IX+$SX)
AN $0,$0
JP Z,V2_LIB_YES
CAL LCD_CLEAR
LD $6,0
PRE IZ,V2_LIB_ASK
CAL LCD_PUTZ
LD $6,&H20
PRE IZ,V2_LIB_ASK2
CAL LCD_PUTZ
V2_LIB_WAIT:
CAL KEY_WAIT
SBC $17,2
JP Z,V2_WARM
SBC $0,KEYMENU
JP Z,V2_HOME
SBC $0,KEYEXE
JP Z,V2_LIB_YES
SBC $0,&H59
JP Z,V2_LIB_YES
SBC $0,&H79
JP Z,V2_LIB_YES
SBC $0,&H4E
JP Z,V2_HOME
SBC $0,&H6E
JP Z,V2_HOME
SBC $0,KEYQ
JP Z,V2_HOME
SBC $0,KEYQLC
JP Z,V2_HOME
JP V2_LIB_WAIT
V2_LIB_YES:
; Both storage and logical-row markers must be reset together.
; This operation NEVER touches ans, a..z, or unfinished v2 input.
CAL T12_ERASE
CAL T14_LOAD_EX
V2_LIB_VIEW:
; A third binding policy: RAM document, read-only to this viewer.
; Q and MENU return to chooser, BRK resumes the library viewer.
LDW $10,T22_BASE
CAL VT22_BIND_RO
CAL KBD_INIT
CAL VT22_VIEW
; The generic viewer returns semantic-free events. Chapter01 handles
; EXE/:c recall and :h help even for RAM_RO, but never :e erasure.
JP T13_EVENT
V2_SHOW4:
; FK4: shared VT22 viewer over a caller-owned ROM document.
; Zero canvas copies: tape RAM 2000h..277Fh is NEVER modified.
; In ROM mode VT22 refuses writes/clear and calculator extensions.
LDW $10,V2_INFO_CANVAS
CAL VT22_BIND_ROM
CAL VT22_VIEW
V2_INFO_EVENT:
; Only the application sends bytes; the viewer returns event 8.
SBC $0,8
JP Z,T22_SEND
SBC $0,9
JP Z,V2_INFO_REOPEN
; BRK retains active [info] on warm return; Q/MENU go home.
SBC $0,2
JP Z,V2_WARM
JP V2_HOME
V2_INFO_REOPEN:
; Resume the original ROM document without losing VT22's viewport.
CAL VT22_OPEN
JP V2_INFO_EVENT
V2_DEMO:
; Retained diagnostic probe, no longer a calculator menu route.
; LCD has been cleared by their SHOW routine, preventing stale cells.
LD $6,&H20
PRE IZ,V2_CORE
CAL LCD_PUTZ
LD $6,&H40
PRE IZ,V2_EQ
CAL LCD_PUTZ
LDW $15,10
LDW $17,0
LDW $5,20
LDW $7,0
CAL UADD32
PST UA,&H14
PRE IX,V2_FMT
CAL UFMT32DEC
; The formatter is allowed to return only 1..10 characters.
; Do not read beyond the buffer or print unbounded garbage on failure.
LD $13,$14
AN $13,$13
JP Z,V2_FMT_FAIL
SBC $13,11
JP NC,V2_FMT_FAIL
LDW $10,V2_FMT
LD $6,&H4A
CAL LCD_RAMN
JP V2_PAGE_WAIT
V2_FMT_FAIL:
LD $6,&H40
PRE IZ,V2_FMT_ERROR
CAL LCD_PUTZ
; MENU and BRK are persistent controls: no on-screen legend.
V2_PAGE_WAIT:
CAL KBD_EVENT
SBC $17,K540_EV_MENU
JP Z,V2_HOME
SBC $17,K540_EV_BRK
JP Z,V2_WARM
JP V2_PAGE_WAIT
; Splash: four NUL-terminated strings for SPL_SHOW, all <=32 chars.
V2_SPLASH:
DB "Xandrian Labs",0
DB "CS617 - Chapter 01 v2b",0
DB "Two calculators one U32",0
DB "VT paper tape",0
; 68-byte group, EXACT: 32 title + 4*8 captions + 4 alias bytes.
; Zero aliases are the documented numeric defaults, 1,2,3,4.
V2_MENU_GROUP:
DB "CS617 Chapter 01 v2b "
DB "[v1] ","[v2] ","[lib] ","[info] "
DB 0,0,0,0
V2_ERR_OVERFLOW:
DB "MA error: U32 overflow.",0
V2_ERR_SYNTAX:
DB "MA error: syntax error.",0
V2_ERR_EMPTY:
DB "MA error: empty input.",0
V2_ERR_UNDERFLOW:
DB "MA error: U32 underflow.",0
V2_ERR_DIVZERO:
DB "MA error: divide by zero.",0
V2_ERR_DEPTH:
DB "MA error: nesting too deep.",0
V2_EDIT_ERROR:
DB "MA error: editor config error.",0
V2_LIB_ASK: DB "Load examples? Y/n",0
V2_LIB_ASK2:DB "Replaces current tape; N cancels",0
V2_CORE:
DB "Shared U32 core",0
V2_EQ:
DB "10 + 20 = ",0
V2_FMT_ERROR:
DB "MA error: format length error.",0
V2_MERR_MSG:
DB "MA error: menu config error.",0