Uxn
A collection of code snippets in the uxntal language
Here are some simple, unfinished little uxn sketches I wrote in November of 2021 while working through the 1 week uxn tutorial.
dotted-line⌗
This program draws a dotted line from the top left of the screen, diagonally down towards the bottom right, then promptly crashes!
With a little work we could make the line bounce around the screen, but for now this will have to do.
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|80 @Controller [ &vector $2 &button $1 &key $1 ]
( main )
|0100
( set system colors )
#2ce9 .System/r DEO2
#01c0 .System/g DEO2
#2ce5 .System/b DEO2
( set x/y )
#0008 .Screen/x DEO2
#0008 .Screen/y DEO2
( set screen vector )
;on-frame .Screen/vector DEO2
BRK
@on-frame
.Screen/x DEI2 INC2 DUP2 DUP2 ( increment x and dup )
.Screen/x DEO2 ( write to x )
.Screen/y DEO2 ( write to y )
#0001 AND2 JCN2 ,&end ( if x is odd, jump to end )
( draw a pixel )
#01 .Screen/pixel DEO
&end
BRK
day4⌗
This creatively named program allows you to move a box left and right across the screen using the left and right arrow keys.
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|80 @Controller [ &vector $2 &button $1 &key $1 ]
( macros )
%HALF2 { #01 SFT2 } ( divide a short by 2 )
%color-clear { #40 } ( clear 1bpp sprite from fg )
%color-2 { #4a } ( draw 1bpp sprite w/ color 2 and transparent )
%DUP4 { OVR2 OVR2 } ( duplicate 4 bytes (2 shorts) )
%MOD { DUP2 DIV MUL SUB }
%MOD2 { DUP4 DIV2 MUL2 SUB2 }
( zero page )
|0000
@sprite [ &pos-x $2 &pos-y $2 ]
( main )
|0100
( set system colors )
#2ce9 .System/r DEO2
#01c0 .System/g DEO2
#2ce5 .System/b DEO2
( center sprite y pos )
.Screen/height DEI2 HALF2 #0004 SUB2 .Screen/y DEO2
( set sprite addr )
;square .Screen/addr DEO2
( set screen vector )
;on-frame .Screen/vector DEO2
BRK
@on-frame
( clear sprite )
color-clear .Screen/sprite DEO
( arrow key control flow )
.Controller/button DEI
#40 AND ( isolate "left key" bit )
,&left JCN
.Controller/button DEI
#80 AND ( isolate "right key" bit )
,&right JCN
,&draw JMP ( else, jump to draw )
&left
.sprite/pos-x LDZ2 #0004 SUB2 .sprite/pos-x STZ2
,&draw JMP
&right
.sprite/pos-x LDZ2 #0004 ADD2 .sprite/pos-x STZ2
&draw
( update Screen/x % width )
.sprite/pos-x LDZ2 .Screen/width DEI2 MOD2 .Screen/x DEO2
( draw sprite )
color-2 .Screen/sprite DEO
BRK
@square ff81 8181 8181 81ff
tictactoe⌗
The beginnings of a simple tictactoe game.
Use the arrow keys to move the cursor around.
Press CTRL
to swap the cursor between X mode and O mode.
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|80 @Controller [ &vector $2 &button $1 &key $1 ]
( macros )
%PIXEL { #01 .Screen/pixel DEO }
%X1 { #00 SWP .Screen/x DEO2 } ( inc -> short and set x )
%Y1 { #00 SWP .Screen/y DEO2 } ( set y with offset )
%SET-X { .Screen/x DEO2 }
%SET-Y { .Screen/y DEO2 }
%TO-SHT { #00 SWP }
( zero page )
|0000
@pos [ &xpos $2 &ypos $2 ]
@turn $1 ( player 0 or or player 1 )
( main )
|0100
( set system colors )
#2ce9 .System/r DEO2
#01c0 .System/g DEO2
#1ce5 .System/b DEO2
( assign controller vector )
;on-controller .Controller/vector DEO2
( set sprite addr )
;ocursor .Screen/addr DEO2
( horizontal lines )
#f000 ( set up counter and conditional )
&whilex
INC DUP ( inc and dup counter )
#0050 SET-Y X1 PIXEL ( set x/y and draw pixel )
DUP #00a0 SET-Y X1 PIXEL ( set x/y and draw pixel )
DUP2 ( copy cond and counter )
GTH ,&whilex JCN ( compare and jump )
POP2 ( clear the stack )
( vertical lines )
#f000 ( set up counter and conditional )
&whiley
INC DUP ( inc and dup counter )
#0050 SET-X Y1 PIXEL ( set x/y and draw pixel )
DUP #00a0 SET-X Y1 PIXEL ( set x/y and draw pixel )
DUP2 ( copy cond and counter )
GTH ,&whiley JCN ( compare and jump )
POP2 ( clear the stack )
BRK
( end of main )
( run on keypress )
@on-controller
( clear sprite )
#40 .Screen/sprite DEO
.Controller/button DEI #10 EQU ,&up JCN
.Controller/button DEI #20 EQU ,&down JCN
.Controller/button DEI #40 EQU ,&left JCN
.Controller/button DEI #80 EQU ,&right JCN
.Controller/button DEI #01 EQU ,&control JCN
,&draw JMP
&up
.pos/ypos LDZ2 DUP2 #0000 NEQ2 TO-SHT SUB2 .pos/ypos STZ2 ( y -= y != O )
,&draw JMP
&down
.pos/ypos LDZ2 DUP2 #0002 LTH2 TO-SHT ADD2 .pos/ypos STZ2 ( y += y < 2 )
,&draw JMP
&left
.pos/xpos LDZ2 DUP2 #0000 NEQ2 TO-SHT SUB2 .pos/xpos STZ2 ( x -= x != 0 )
,&draw JMP
&right
.pos/xpos LDZ2 DUP2 #0002 LTH2 TO-SHT ADD2 .pos/xpos STZ2 ( x += x < 0 )
,&draw JMP
&control
.turn LDZ #01 EOR .turn STZ ( turn = !turn )
.turn LDZ TO-SHT #30 SFT2 ;ocursor ADD2 .Screen/addr DEO2 ( Screen/address = ocurs + turn << 3 )
,&draw JMP
&draw
.pos/xpos LDZ2 #0050 MUL2 #0024 ADD2 SET-X ( set x pos (mult and offset) )
.pos/ypos LDZ2 #0050 MUL2 #0024 ADD2 SET-Y ( set y pos (mult and offset) )
#41 .turn LDZ ADD .Screen/sprite DEO ( draw sprite )
BRK
( sprite )
@ocursor 3c42 8181 8181 423c
@xcursor 8142 2418 1824 4281