Twenty four places moved Q into A or B by pushing it and popping it back. That is four bus cycles and two bytes to do what MVQA does in one of each, and several of them are inside loops - Life, the calculator, int8. Nineteen more loaded zero with INIA 0d0 where RSTA says the same thing in one byte. Both are equivalent at the CPU rather than by assertion: RSTA and INIA both leave Status alone, and PSHQ followed by POPA nets to A = Q with the Stack Pointer where it started. The one difference is that the pair leaves a copy of Q in memory just below the Stack Pointer and MVQA does not, which nothing here reads. Five recorded outputs moved and every one of them says the change worked: - 16x16Life fits five more generations into the same cycle budget, the first 457 lines identical, because the loop got cheaper. - Life.sbx is 1409 bytes rather than 1411, in three tests that list it. - Edit.sbx is 1995 rather than 1996. That last one broke a check I added this morning, and the hole is worth recording: the CosmOS README's claim about Edit's size did not have the word "Edit" on the same line as the number, because the subject was in the sentence before, so the check that measures quoted sizes skipped it silently. The sentence now names what it is talking about, which makes it both checkable and clearer, and the check fails on a wrong number there. Comments on either half of a replaced pair are carried onto the instruction that replaces them, so nothing anybody wrote was lost.
320 lines
3.8 KiB
NASM
320 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
|
|
MVQA
|
|
DPUP 0d02
|
|
|
|
LDB
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
DPUP 0d02
|
|
|
|
LDB
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
DPUP 0d32
|
|
|
|
LDB
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
DPUP 0d04
|
|
|
|
LDB
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
DPUP 0d32
|
|
|
|
LDB
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
DPUP 0d02
|
|
|
|
LDB
|
|
CCF
|
|
ADD
|
|
MVQA
|
|
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
|