Last active
October 2, 2023 04:58
-
-
Save AXKuhta/5969ae3181d4d9921fab065dbd56cd93 to your computer and use it in GitHub Desktop.
Game of Life for R216K2A
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Game of Life for R216K2A with RT2 | |
; v1.02 | |
; There is no figures included in the source (Too painful to add them with dw) | |
; So you'll have to add them yourself with the PROP tool after compiling the code | |
; Base position: cell 384 (First cell of line 4 of RAM) | |
; R0 = Terminal port | |
; R1 = Current cell position | |
; R2 = Target cell position | |
; R3 = Current line | |
; R4 = Used for counting nearby cells | |
; R5 = Total frame count | |
; R6 = PrintText and PrintHex argument | |
; R7 = Used by PrintText and PrintHex | |
mov sp, 0 | |
mov r0, 0 | |
mov r5, 0 | |
bump r0 | |
send r0, 0x200F ; Set colors | |
send r0, 0x10B0 ; Write to the last line of the screen | |
CalculateFrame: | |
mov r6, TEXT.FRAMES | |
call PrintText | |
mov r6, r5 | |
add r5, 0x0001 | |
call PrintHex | |
send r0, 0x1000 | |
mov r1, 384 | |
mov r2, 400 ; Base position (384) + Line length (16) | |
mov r3, 0 | |
.CalculateLine: | |
.LineLoop: | |
xor r4, r4 | |
add r4, [r1 - 129] | |
add r4, [r1 - 128] | |
add r4, [r1 - 127] | |
add r4, [r1 - 1] | |
add r4, [r1 + 1] | |
add r4, [r1 + 127] | |
add r4, [r1 + 128] | |
add r4, [r1 + 129] | |
cmp r4, 2 | |
jl .CellIsDead | |
jg .NotTwoCells | |
cmp [r1], 1 | |
jne .CellIsDead | |
jmp .CellIsAlive | |
.NotTwoCells: | |
cmp r4, 3 | |
jne .CellIsDead | |
.CellIsAlive: | |
mov [r1 + 64], 1 | |
send r0, '*' | |
jmp .EvaluateOut | |
.CellIsDead: | |
mov [r1 + 64], 0 | |
send r0, ' ' | |
.EvaluateOut: | |
add r1, 1 | |
cmp r1, r2 | |
jne .LineLoop | |
add r1, 112 ; Set base position to the next line | |
add r2, 128 ; And set the target position to the (Next line + line length) | |
add r3, 1 ; Add 1 to line count | |
cmp r3, 11 ; Check if current line is 12 (Counting from 0 so 11) | |
jne .CalculateLine | |
; WriteBack | |
; R1 = First cell of the line position | |
; R2 = Unused (Since V1.02) | |
; R3 = Current line | |
; R4 = ""Copying buffer"" | |
mov r1, 448 | |
mov r3, 0 | |
; Manually unrolled to copy 16 cells (An entire line) per iteration | |
WriteBackLine: | |
mov r4, [r1] ; 0 | |
mov [r1 - 64], r4 ; 0 | |
mov r4, [r1 + 1] ; 1 | |
mov [r1 - 63], r4 ; 1 | |
mov r4, [r1 + 2] ; 2 | |
mov [r1 - 62], r4 ; 2 | |
mov r4, [r1 + 3] ; 3 | |
mov [r1 - 61], r4 ; 3 | |
mov r4, [r1 + 4] ; 4 | |
mov [r1 - 60], r4 ; 4 | |
mov r4, [r1 + 5] ; 5 | |
mov [r1 - 59], r4 ; 5 | |
mov r4, [r1 + 6] ; 6 | |
mov [r1 - 58], r4 ; 6 | |
mov r4, [r1 + 7] ; 7 | |
mov [r1 - 57], r4 ; 7 | |
mov r4, [r1 + 8] ; 8 | |
mov [r1 - 56], r4 ; 8 | |
mov r4, [r1 + 9] ; 9 | |
mov [r1 - 55], r4 ; 9 | |
mov r4, [r1 + 10] ; 10 | |
mov [r1 - 54], r4 ; 10 | |
mov r4, [r1 + 11] ; 11 | |
mov [r1 - 53], r4 ; 11 | |
mov r4, [r1 + 12] ; 12 | |
mov [r1 - 52], r4 ; 12 | |
mov r4, [r1 + 13] ; 13 | |
mov [r1 - 51], r4 ; 13 | |
mov r4, [r1 + 14] ; 14 | |
mov [r1 - 50], r4 ; 14 | |
mov r4, [r1 + 15] ; 15 | |
mov [r1 - 49], r4 ; 15 | |
add r1, 128 ; Set target position to the next line | |
add r3, 1 ; Add 1 to line count | |
cmp r3, 11 ; Check if current line is 12 (Counting from 0 so 11) | |
jne WriteBackLine | |
jmp CalculateFrame | |
PrintText: | |
mov r7, [r6] | |
.PrintChar: | |
send r0, r7 | |
add r6, 1 | |
mov r7, [r6] | |
jnz .PrintChar | |
ret | |
PrintHex: | |
mov r7, r6 | |
and r7, 0xF000 | |
shr r7, 12 | |
send r0, [.TABLE + r7] | |
mov r7, r6 | |
and r7, 0x0F00 | |
shr r7, 8 | |
send r0, [.TABLE + r7] | |
mov r7, r6 | |
and r7, 0x00F0 | |
shr r7, 4 | |
send r0, [.TABLE + r7] | |
mov r7, r6 | |
and r7, 0x000F | |
send r0, [.TABLE + r7] | |
ret | |
.TABLE: dw "0123456789ABCDEF" | |
TEXT: | |
.FRAMES: dw "Frame: 0x", 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment