Instantly share code, notes, and snippets.

# charlesdaniels/dcpu16_demo.md

Last active February 24, 2018 20:05
Show Gist options
• Save charlesdaniels/272372aceed6a138e1f52204e7639cd0 to your computer and use it in GitHub Desktop.
UofSC ACM eWeek 2018 DCPU-16 Demo

# Sample Programs

``````; Author: Charles Daniels
; Date: 2018-02-24
; Purpose: Calculate A + B, store to C

SET A,   2  ; Set A to 2
SET B,   3  ; Set B to 3
SET C,   0  ; Clear contents of C
ADD C,   A  ; C = C + A
ADD C,   B  ; C = C + B = A  +B
``````

## Fibonacci Sequence

``````; Author: Charles Daniels
; Date: 2018-02-24
; Purpose: Calculate the Fibonacci sequence one
;          digit at a time and sent to X. Note
;          F(0) is omitted from the output, so
;          the actual output is 1, 2, 3, 5 ...
;
;          Remember the Fibonacci sequence is
;          defined as F(n) = F(n-1) + F(n-2)
;          F(0) = 1, F(1) = 1

set A,  0   ; Current n
set B,  0   ; Previous n
set C,  0   ; Second-to-last n

set B,  1   ; n - 1
set C,  1   ; n - 2

:fibo
; increment accumulator

; shift numbers backwards
set C,  B
set B,  A

; send to output
set X,  A

; loop
set PC, fibo
``````

## "Hello World"

source

``````		; Hello world for DCPU-16
; By Metapyziks

SET I, 0				; Initialise loop
:nextchar
IFE [data+I], 0				; If we have reached the end of the string
SET PC, end			; Goto the end

SET A, [data+I]				; Load the next character
BOR A, 0xf000				; Set the colour to white
SET [0x8000+I], A	         	; Set the value in video memory
ADD I, 1				; Increment the loop
SET PC, nextchar		        ; Loop back to do the next character
:data
DAT "Hello world!", 0			; Null terminated string to print
:end
SET PC, end				; Hang forever
``````

## Matrix-Style Digital Rain

source

``````set z, 0x1234 ; rand_seed
set y, z ; cur_rand
set pc, main_loop

:next_rand
mul y, 10061
set pc, pop

:main_loop
set i, 0
set x, y
set y, z
set a, 0x8000

:next_row1
set j, 0
:next_char1
jsr next_rand
ife [a], 0
set pc, skip1
ifb y, 0x7000
set pc, skip1

; mutate char at [a] (j, i)
set push, y
set y, x
jsr next_rand
and [a], 0xff00 ; reset char
and y, 0x003f
bor [a], [code+y] ; set letter
set x, y
set y, pop

:skip1
ifg 32, j
set pc, next_char1
ifg 12, i
set pc, next_row1
set y, x

; step 2: move all columns down
set i, 12
set a, 0x817f

:next_row2
set j, 32
:next_char2
ife [a], 0
set pc, empty_char
ifb [a+32], 0xffff
set pc, move_down

; add new char at [a+32] (j, i+1)
jsr next_rand
set [a+32], [a]
and [a+32], 0xff00
set x, y
and x, 0x003f
bor [a+32], [code+x]
and [a], 0x7fff
set pc, skip2

:empty_char
set [a+32], 0

:move_down
and [a+32], 0x7fff

:skip2
sub j, 1
sub a, 1
ifg j, 0
set pc, next_char2
sub i, 1
ifg i, 0
set pc, next_row2
set y, x

; step 3: update top layer
set a, 0x8000
set j, 0
:next_char3
jsr next_rand
ifb y, 0x0700
set pc, skip3
ifb [a], 0xffff
set pc, empty_char2

set [a], 0x2000
ifb y, 0x0800
set [a], 0xa000
set x, y
and x, 0x003f
bor [a], [code+x]
set pc, skip3

:empty_char2
set [a], 0

:skip3