SOURCE VIEWER

life.s

Original bytes preserved. This is a browser-readable view.

DOWNLOAD ORIGINAL ↓

; CS617 Chapter 08 r014 -- Life 4 ROM WHOLE-IMAGE SAME-ADDRESS MIRROR
; Alex Barragan and ChatGPT (Sol) 2026-10-03
; Original source: life.zip/life/artifacts/life4ROM.s, SHA256:
; 17a5ce4a11e634a908166e93f299beec9ce55b78256f4f89aa159148be067b4d
; Source-only integration candidate, NOT HD61 built or emulator verified.
; Chapter08 owns the 0..0DFF scaffold and native BRK fallback.
; This module owns ROM2:0E00..2Cxx and 3000..307D.
; LF_LOADER copies ROM2:LF_MAIN..LF_LOADER_END-1 into
; RAM1:the SAME 16-bit addresses, including LF_MAIN / LF_RESTART.
; RAM1:0EFE RCS is intentionally overwritten; CH08_WARM restores it.
; NO SYSCAL may be called from Life's copied RAM while RCS is absent.
; The original stand-alone image remains untouched in donor capsule.

LF_SPLASH0POS: EQU     16
LF_SPLASH1POS: EQU     48
LF_SPLASH2POS: EQU     80
LF_SPLASH3POS: EQU     112


        ORG     &H0E00


LF_MAIN:
        PRE     SS,&H0100
        PST     UA,&H14

        CAL     &H04EF
        PPO     &HC0

        LDW     $0,&H12B5

LF_INIT_DELAY:
        SBW     $0,$SY
        JR      NZ,LF_INIT_DELAY

        CAL     &H02D4

        PPO     &HC3
        LD      $0,&H0C
        LDW     $1,&H3000
        STLM    $0,3

        CAL     &H0304
        CAL     &H0306

; ============================================================
; STARTUP SPLASH
;
; Normal/small firmware-character path.
; SPLASH is a contiguous 32 x 4 text area (128 characters)
; followed by a zero terminator.  Customize only the four
; DB strings below; keep each row exactly 32 characters.
; ============================================================

        CAL     LF_SHOW_SPLASH

        ; Chapter 08 owns ACJMP / BRK. Do not arm standalone Life
        ; BRK_ENTRY (would restart firmware, not return to launcher).
        ; CH08_RAWREADY is called cooperatively in RAM during Life.

        JP      LF_LOADER


; ============================================================
; WARM LF_RESTART
;
; Used after the benchmark result is acknowledged with MENU.
; Repeats the normal ROM/display initialization, but deliberately
; skips the ROM-to-RAM mirror because the Life image is already
; resident in RAM.
; ============================================================

LF_RESTART:
        PRE     SS,&H0100
        PST     UA,&H14

        CAL     &H04EF
        PPO     &HC0

        LDW     $0,&H12B5

LF_RDELAY:
        SBW     $0,$SY
        JR      NZ,LF_RDELAY

        CAL     &H02D4

        PPO     &HC3
        LD      $0,&H0C
        LDW     $1,&H3000
        STLM    $0,3

        CAL     &H0304
        CAL     &H0306

        ; Return to the already-copied RAM image.
        PST     UA,&H55
        JP      LF_RAM_ENTRY


; ============================================================
; LIFE IMAGE START
;
; Verified architecture:
; Everything from here through LF_LIFE_COPY_END-1 is copied from
; ROM bank 0 to RAM bank 1 at identical 16-bit addresses.
; The copied Life image is then executed from RAM.
; ============================================================

LF_COPY_START:


; ============================================================
; RAM ENTRY
; ============================================================

LF_RAM_ENTRY:
        ; Proven byte-for-byte reconstruction of the DL-Pascal
        ; prelude.  In RAM this writes the LF_LIFECALC address into
        ; the writable procedure-entry area.
        LDW     $0,LF_LIFECALC
        STW     LF_LIFECALC,($SZ)

        JP      LF_LIFE_BODY


; ============================================================
; PASCAL PROCEDURE ENTRY WORD
; ============================================================

LF_LIFECALC:
        DW      0


; ============================================================
; GAME OF LIFE
; ============================================================

LF_LIFE_BODY:
        PST     IE,0

        PRE     IZ,LF_T

        ST      $30,(IZ+&H03)
        ST      $30,(IZ+&H13)
        ST      $30,(IZ+&H14)

        PRE     IZ,&H171C
        GRE     KY,$0
        GST     TM,$0
        STW     $0,(IZ+$SX)

        ; Start timing the first Life run once.
        CAL     LF_STRTM


LF_L00:
        CAL     LF_BRK_POLL
        PRE     IX,LF_B
        PRE     IY,LF_B+1022
        PRE     IZ,LF_B+1

        STD     $31,(IX+$SX)
        BUP

        PRE     IZ,&H171A
        STIW    $30,(IZ+$SX)
        LDW     $16,(IZ+$SX)

        PRE     IZ,LF_A+33
        LD      $19,30


LF_L01:
        LD      $18,30


LF_L02:
        ORC     $16,$17
        JR      NZ,LF_L03

        LDW     $16,$SY


LF_L03:
        LD      $15,$17
        LD      $14,$17

        BIU     $14
        BIU     $14
        BIU     $14

        XR      $15,$14

        LD      $14,$15

        BIU     $14
        BIU     $14

        XR      $15,$14

        BIU     $15

        ROUW    $16

        LD      $15,$16
        AN      $15,$SY

        STI     $15,(IZ+$SX)

        SB      $18,$SY
        JR      NZ,LF_L02

        LDD     $14,(IZ+2)

        SB      $19,$SY
        JR      NZ,LF_L01

        PRE     IZ,&H171C
        STW     $16,(IZ+$SX)

        PPO     &HFF

        STL     &H38


; ============================================================
; DISPLAY CONVERSION
; ============================================================

LF_L04:
        PRE     IZ,LF_A
        PRE     IX,LF_D

        LD      $17,24
        LD      $19,4


LF_L05:
        LD      $16,4


LF_L06:
        LD      $18,7

        LDIM    $0,(IZ+$SX),8


LF_L07:
        BIUW    $0

        ROUW    $2
        ROUW    $4
        ROUW    $6

        LDIM    $8,(IZ+$17),8
        ORM     $0,$8,8

        SB      $18,$SY
        JR      NZ,LF_L07

        STIM    $0,(IX+$SX),8

        SB      $16,$SY
        JR      Z,LF_L08

        LDD     $0,(IZ-224)

        JR      LF_L06


LF_L08:
        SB      $19,$SY
        JR      NZ,LF_L05


; ============================================================
; LCD OUTPUT
; ============================================================

        PRE     IX,LF_D

        LDW     $17,&H1082
        LD      $19,$SX


LF_L09:
        PPO     &HFF

        STLM    $17,3

        PPO     &HFE

        LD      $0,4


LF_L10:
        LDIM    $1,(IX+$SX),8

        STLM    $1,8

        SB      $0,$SY
        JR      NZ,LF_L10

        AD      $19,$SY

        XRC     $19,4
        JR      NZ,LF_L09

        LD      $0,&H83
        LDW     $1,&H009E

        PPO     &HFF

        STLM    $0,3

        PPO     &HFE


; ============================================================
; GENERATION COUNTER
; ============================================================

        PRE     IZ,&H171A

        LDW     $0,(IZ+$SX)
        DIUW    $0

        LD      $3,3


LF_L11:
        DIUM    $0,3

        DIU     $2

        AD      $2,&H0F

        STL     $2

        SB      $3,$SY
        JR      NZ,LF_L11

        LDW     $0,(IZ+$SX)

        ADBW    $0,$SY
        JR      UZ,LF_L12

        JP      LF_RUNDN


LF_L12:
        STW     $0,(IZ+$SX)


; ============================================================
; COPY GRIDS
; ============================================================

        PRE     IX,LF_B
        PRE     IY,LF_B+1023
        PRE     IZ,LF_C

        BUP

        PRE     IX,LF_A
        PRE     IY,LF_A+1023
        PRE     IZ,LF_B

        BUP


; ============================================================
; CALCULATE NEXT GENERATION
; ============================================================

        PRE     IX,LF_A+33
        PRE     IZ,LF_B+33

        LD      $19,30
        LD      $16,33
        LD      $17,31


LF_L13:
        LD      $18,5


LF_L14:
        LDM     $8,(IZ-$16),8
        LDM     $0,(IZ+$17),8

        ADBM    $8,$0,8

        LDM     $0,(IZ-$SY),8

        ADBM    $8,$0,8

        DIUM    $1,6

        ADBM    $1,$8,6
        ADBM    $1,$9,6
        ADBM    $1,$10,6

        LDD     $0,(IZ+6)

        GRE     IZ,$14

        PRE     IZ,LF_T

        LD      $1,(IZ+$1)
        LD      $2,(IZ+$2)
        LD      $3,(IZ+$3)
        LD      $4,(IZ+$4)
        LD      $5,(IZ+$5)
        LD      $6,(IZ+$6)

        PRE     IZ,$14

        STIM    $1,(IX+$SX),6

        SB      $18,$SY
        JR      NZ,LF_L14

        LDD     $0,(IX+2)
        LDD     $0,(IZ+2)

        SB      $19,$SY
        JR      NZ,LF_L13


; ============================================================
; KEYBOARD
; ============================================================

        ; Japanese ROM practice: check OFF before keyboard scanning.
        ; LF_LIFE_OFFCHK safely crosses from the RAM execution bank to
        ; machine-ROM bank 0 and back.
        ; Before delegating the OFF path to firmware, give the
        ; card-side BRK scanner first refusal.  Earlier Life BRK
        ; experiments show the native path can enter badlands while
        ; executing from RAM1.  Probe only; the precise cause
        ; remains UNVERIFIED until an emulator observation.
        CAL     LF_BRK_POLL
        CAL     LF_LIFE_OFFCHK

        PST     IA,9

        XRCM    $0,$0,8

        GRE     KY,$0

        PST     IA,&H40
        ANC     $0,&H10

        JP      NZ,LF_L00


; ============================================================
; COMPARE GENERATIONS
; ============================================================

        PRE     IX,LF_A+32
        PRE     IZ,LF_C+32

        LD      $16,120


LF_L15:
        LDIM    $0,(IX+$SX),8
        LDIM    $8,(IZ+$SX),8

        XRCM    $8,$SZ,8

        JR      NZ,LF_L16

        SB      $16,$SY
        JR      NZ,LF_L15

;
; Stop when successive generations are identical.
;
        JP      LF_RUNDN


; ============================================================
; CONTINUE / EXIT
; ============================================================

LF_L16:
        PST     IA,$30

        XRCM    $0,$0,8

        GRE     KY,$0

        PST     IA,&H40
        ANC     $0,&H80

        JR      NZ,LF_L17

        JP      LF_L04


LF_L17:
        PPO     &HFF

        STL     &H28

        PRE     IX,$28
        PST     IE,&H18

        JP      &H0177


; ============================================================
; LIFE RUN BENCHMARK
;
; TM is the complete 8-bit four-minute timer:
;   bits 7..6 = minute within the four-minute cycle, 0..3
;   bits 5..0 = seconds, 0..59
;
; Capture the complete TM byte at start and end. Convert each to
; seconds 0..239, then measure forward modulo 240.
; Valid for benchmark runs shorter than four minutes.
; ============================================================

LF_GETTM:
        GST     TM,$8
LF_GT1:
        LD      $9,$8
        GST     TM,$8
        SBC     $8,$9
        JR      NZ,LF_GT1
        LD      $9,0
        RTN

LF_STRTM:
        CAL     LF_GETTM
        PRE     IZ,LF_STM
        ST      $8,(IZ+$SX)
        RTN

LF_ENDTM:
        CAL     LF_GETTM
        PRE     IZ,LF_ETM
        ST      $8,(IZ+$SX)
        RTN

; Convert complete TM byte in $8 to binary seconds 0..239 in $10.
LF_TMSEC:
        LD      $10,$8
        AN      $10,&H3F

        LD      $12,$8
        AN      $12,&HC0

        SBC     $12,&H00
        JR      Z,LF_TS4
        SBC     $12,&H40
        JR      Z,LF_TS1
        SBC     $12,&H80
        JR      Z,LF_TS2

        LDW     $14,180
        ADW     $10,$14
        JR      LF_TS4
LF_TS1:
        LDW     $14,60
        ADW     $10,$14
        JR      LF_TS4
LF_TS2:
        LDW     $14,120
        ADW     $10,$14
LF_TS4:
        RTN

; Elapsed = (end-start) modulo 240.
; Implemented by walking forward around the 240-second ring.
LF_ELAPS:
        PRE     IZ,LF_STM
        LD      $8,(IZ+$SX)
        CAL     LF_TMSEC
        LDW     $16,$10

        PRE     IZ,LF_ETM
        LD      $8,(IZ+$SX)
        CAL     LF_TMSEC
        LDW     $18,$10

        LDW     $14,0
        LDW     $20,1
        LDW     $22,240
        LDW     $10,$16

LF_ET1:
        SBCW    $10,$18
        JR      Z,LF_ET5
        ADW     $10,$20
        ADW     $14,$20
        SBCW    $10,$22
        JR      NZ,LF_ET1
        LDW     $10,0
        JR      LF_ET1

LF_ET5:
        ; Convert binary elapsed seconds to decimal-coded form for LF_PRN3.
        LDW     $8,0
LF_ET6:
        ORC     $14,$15
        JR      Z,LF_ET7
        ADBW    $8,$SY
        SBW     $14,$SY
        JR      LF_ET6
LF_ET7:
        PRE     IZ,LF_LTIME
        STW     $8,(IZ+$SX)
        RTN


; Print unsigned packed-decimal Word in $0 as three digits.
LF_PRN3:
        DIUW    $0
        LD      $3,3

LF_P3:
        DIUM    $0,3
        DIU     $2
        AD      $2,&H0F
        STL     $2
        SB      $3,$SY
        JR      NZ,LF_P3

        RTN


; Display the completed-run summary:
;   &H009E = live/current generation counter
;   &H029E = final generation count minus one
;   &H039E = elapsed seconds
LF_SHOW:
        ; Final generation count minus one.
        ; The Life counter is BCD, so use BCD word subtraction.
        PRE     IZ,&H171A
        LDW     $0,(IZ+$SX)
        SBBW    $0,$SY

        LD      $4,&H83
        LDW     $5,&H029E
        PPO     &HFF
        STLM    $4,3
        PPO     &HFE

        CAL     LF_PRN3

        ; Elapsed seconds.
        LD      $4,&H83
        LDW     $5,&H039E
        PPO     &HFF
        STLM    $4,3
        PPO     &HFE

        PRE     IZ,LF_LTIME
        LDW     $0,(IZ+$SX)
        CAL     LF_PRN3

        RTN


; LF_A Life run ended either because the counter reached its limit
; or because the original state-comparison path caused an early
; restart.  The precise semantic condition of that comparison is
; not asserted here.
LF_RUNDN:
        ; The existing live generation counter is already the final
        ; count and remains visible while we wait for MENU.
        CAL     LF_ENDTM
        CAL     LF_ELAPS
        CAL     LF_SHOW

; Immediately begin the next run.
; Results remain on their separate display lines until overwritten
; by the following iteration.
        PST     UA,&H14
        JP      LF_MAIN


; ============================================================
; POWER-OFF CHECK FROM RAM
;
; Life normally executes with UA=&H55:
;   PC = bank 1, IX/IY/IZ/SS/US = bank 1.
;
; &H54 changes only the PC bank to bank 0.  CAL &H04EF therefore
; enters the machine-ROM OFF check while keeping Life's RAM data
; and stacks selected.  OFFCHK returns to the ROM mirror of this
; helper; PST UA,&H55 followed by RTN returns to the RAM caller.
; ============================================================

LF_LIFE_OFFCHK:
        PST     UA,&H54
        CAL     &H04EF
        PST     UA,&H55
        RTN


; ============================================================
; BREAK TRAMPOLINE
;
; ACJMP points here.  No Life state is preserved: BREAK abandons
; the running program.  Force PC bank 0 and jump to the Japanese
; ROM's power-on restart address.
; ============================================================

LF_BRK_ENTRY:
        PST     UA,&H54
        JP      &H0001


; ============================================================
; LIFE WORKSPACE IMAGE
;
; These are now REAL locations in the ROM image, not EQU
; aliases. The entire blocks therefore have corresponding
; bytes that can be mirrored into RAM.
; ============================================================

; ---------------------------------------------------------------
; Chapter 08 Life-specific, NONBLOCKING cooperative BRK check.
; Life executes from RAM1 (UA55) and has an identical ROM2 shadow.
; The switch to UA54 makes the next instruction execute from the
; shadow ROM. Return switches to the RAM image at the same address.
; CH08_RAWREADY calls CH08_WARM only after actual BRK/release.
; Scanner changes $0,$1,$2,$17; caller LF_L00 resets its state.
; ---------------------------------------------------------------
LF_BRK_POLL:
        PST     UA,&H54
        CAL     CH08_RAWREADY
        PST     UA,&H55
        RTN

        ORG     &H2000

LF_A:
        DS      1024


        ORG     &H2400

LF_B:
        DS      1024


        ORG     &H2800

LF_C:
        DS      1024


        ORG     &H2C00

LF_D:
        DS      128


        ORG     &H2C80

LF_T:
        DS      26


; ============================================================
; BENCHMARK STORAGE
;
; LF_STM   - complete 8-bit TM value at run start
; LF_ETM   - complete 8-bit TM value at run end
; LF_LTIME - elapsed seconds in decimal-coded form expected by LF_PRN3
; ============================================================

LF_STM:
        DB      0
LF_ETM:
        DB      0
LF_LTIME:
        DW      0


; ============================================================
; END OF COMPLETE MIRROR IMAGE
; ============================================================

LF_LIFE_COPY_END:


; ============================================================
; ROM -> RAM MIRROR LF_LOADER
;
; Verified working arrangement:
;   source      = bank 0, LF_COPY_START ... LF_LIFE_COPY_END-1
;   destination = bank 1, identical 16-bit addresses
;
; The loader is deliberately outside the copied image so the
; Life code retains stable addresses for byte-by-byte auditing.
; ============================================================

LF_LOADER:
        PST     UA,&H44

        PRE     IX,LF_MAIN
        PRE     IY,LF_LOADER_END-1
        PRE     IZ,LF_MAIN

        BUP


; ============================================================
; ENTER RAM ENVIRONMENT
; ============================================================

        PST     UA,&H55

        JP      LF_RAM_ENTRY

LF_LOADER_END: