Files
SplitBit-Emulator/Programs/Examples/gameOfLife/16x16Life.asm
T
AnachronautandClaude Opus 5 ccf4b384e1 Give Programs/ one rule: a directory per kind, nothing loose
Five .asm files sat at the top of Programs/ beside six directories, with
nothing to say which a new file should join - and hello.asm, which is the
native assembler's first target and named in sixteen places, looked like a
stray.

    Programs/
      Examples/     what you read to learn: hello, printHello, inputTest,
                    replCalculator, and Fibonacci, primeSieve and gameOfLife
                    as sets of their own
      Libraries/    included by name, no entry point of their own
      Loader/       loader.asm, and the loadable program it reads
      CosmOS/       the system, its applications and its assembler
      testPrograms/ what 'make test' drives

Loader/ is the one worth explaining. loader.asm is not a demonstration: it
reads a program off a disk, puts the two pieces where the header asks, and
jumps to the entry. CosmOS grew out of it and does the same thing as one of
its commands. It is kept because backward compatibility with the simplest
version of the system is a standing goal, and it was sitting loose next to
the demos as though it were one.

Programs/loadable/ was a directory holding one file called hello.asm - a
third thing of that name, and the name said nothing about why it was there.
It is Loader/loadable.asm now, beside the loader that reads it.

Every reference moved with them: the makefile's program list, twelve
manifest lines, makedisks.sh, native.sh, and four paths across the README
and both manuals. Verified by deleting both build directories and running
the whole suite from nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E2JrLzFvuFX9fgi1LDRjrW
2026-08-21 13:38:47 -04:00

327 lines
3.8 KiB
NASM

; Conway's Game of Life for SplitBit.
;
; The visible field is 16x16, surrounded by a permanently dead one-cell border.
; Each of the 18x18 allocated cells is stored as two adjacent bytes:
; current state, next state
; This makes double buffering possible with SplitBit's single Data Pointer.
;
; The initial pattern is a glider. ANSI terminal control codes redraw the field
; in place. Press Ctrl-C to stop the emulator.
#Include print.asm
#Program
start:
CALL seedGlider
SETD ClearScreen
CALL printString
generationLoop:
CALL renderBoard
CALL evolveBoard
CALL commitBoard
CALL frameDelay
BRI generationLoop
seedGlider:
; Coordinates in the padded field:
; .#.
; ..#
; ###
SETD Board
DPUP 0d42
INIA 0x01
STA
SETD Board
DPUP 0d80
STA
SETD Board
DPUP 0d112
STA
SETD Board
DPUP 0d114
STA
SETD Board
DPUP 0d116
STA
RET
renderBoard:
SETD CursorHome
CALL printString
SETD Board
DPUP 0d38
INIA 0d16
SETD RowCount
STA
SETD Board
DPUP 0d38
renderRow:
PSHD
INIA 0d16
SETD ColCount
STA
POPD
renderCell:
LDA
BRA renderDead
INIB 0x23
OUTB 0x00
BRI renderCellDone
renderDead:
INIB 0x20
OUTB 0x00
renderCellDone:
DPUP 0d02
PSHD
SETD ColCount
LDA
DECA
STA
BRA renderRowDone
POPD
BRI renderCell
renderRowDone:
POPD
CALL lineFeed
DPUP 0d04
PSHD
SETD RowCount
LDA
DECA
STA
BRA renderDone
POPD
BRI renderRow
renderDone:
POPD
RET
evolveBoard:
SETD Board
DPUP 0d38
INIA 0d16
SETD RowCount
STA
SETD Board
DPUP 0d38
evolveRow:
PSHD
INIA 0d16
SETD ColCount
STA
POPD
evolveCellLoop:
CALL evolveCell
DPUP 0d02
PSHD
SETD ColCount
LDA
DECA
STA
BRA evolveRowDone
POPD
BRI evolveCellLoop
evolveRowDone:
POPD
DPUP 0d04
PSHD
SETD RowCount
LDA
DECA
STA
BRA evolveDone
POPD
BRI evolveRow
evolveDone:
POPD
RET
; DP points to the current-state byte of a visible cell.
evolveCell:
CALL countNeighbors
PSHQ
LDA
POPB
; Three neighbors always produces a live cell.
INIA 0d03
CCF
SUB
BRQ makeAlive
; Two neighbors preserve the current state.
INIA 0d02
CCF
SUB
BRQ preserveCell
makeDead:
RSTA
INCD
STA
DECD
RET
preserveCell:
LDA
BRA makeDead
makeAlive:
INIA 0x01
INCD
STA
DECD
RET
; Return the sum of the eight neighboring current-state bytes in Q.
; With interleaved cells and an 18-cell row, the relative offsets are:
; -38, -36, -34, -2, +2, +34, +36, +38.
countNeighbors:
RSTA
DPDN 0d38
LDB
CCF
ADD
PSHQ
POPA
DPUP 0d02
LDB
CCF
ADD
PSHQ
POPA
DPUP 0d02
LDB
CCF
ADD
PSHQ
POPA
DPUP 0d32
LDB
CCF
ADD
PSHQ
POPA
DPUP 0d04
LDB
CCF
ADD
PSHQ
POPA
DPUP 0d32
LDB
CCF
ADD
PSHQ
POPA
DPUP 0d02
LDB
CCF
ADD
PSHQ
POPA
DPUP 0d02
LDB
CCF
ADD
RET
; Copy every next-state byte to its adjacent current-state byte.
; The border's next bytes remain zero, so it stays permanently dead.
commitBoard:
SETD Board
INIA 0d18
SETD RowCount
STA
SETD Board
commitRow:
PSHD
INIA 0d18
SETD ColCount
STA
POPD
commitCell:
INCD
LDA
DECD
STA
DPUP 0d02
PSHD
SETD ColCount
LDA
DECA
STA
BRA commitRowDone
POPD
BRI commitCell
commitRowDone:
POPD
PSHD
SETD RowCount
LDA
DECA
STA
BRA commitDone
POPD
BRI commitRow
commitDone:
POPD
RET
; Approximately 0.2 seconds at the emulator's nominal 1 MHz rate.
frameDelay:
INIA 0xFF
delayOuter:
INIB 0xFF
delayInner:
DECB
BRB delayInnerDone
BRI delayInner
delayInnerDone:
DECA
BRA delayDone
BRI delayOuter
delayDone:
RET
#Data
RowCount:
0x00
ColCount:
0x00
ClearScreen:
0x1B
"[2J"
CursorHome:
0x1B
"[H"
; The emulator zero-fills the remainder of Data Memory. Board names the first
; byte of a 648-byte logical allocation (18 * 18 * 2).
Board:
0x00