Last active
December 16, 2022 21:36
-
-
Save mattdesl/008587f2b07d0ca155ecaa51fde7d781 to your computer and use it in GitHub Desktop.
License: MIT
This file contains hidden or 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
( artwork ) | |
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 &debug $1 &halt $1 ] | |
|10 @Console [ &vector $2 &read $1 &pad $5 &write $1 &error $1 ] | |
|20 @Screen [ &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ] | |
|c0 @DateTime [ &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1 ] | |
|0000 | |
@framecount $1 | |
%MOD { DUP2 DIV MUL SUB } ( a b -- a%b ) | |
%MOD2 { OVR2 OVR2 DIV2 MUL2 SUB2 } ( a b -- a%b ) | |
%TOSHORT { #00 SWP } ( byte -- short ) | |
|0100 ( -> ) | |
#00ff .Screen/width DEO2 | |
#00ff .Screen/height DEO2 | |
( set system colors ) | |
#c0bc .System/r DEO2 | |
#c71a .System/g DEO2 | |
#c210 .System/b DEO2 | |
( get random seed based on second + day + minute ) | |
.DateTime/second DEI .DateTime/day DEI .DateTime/minute DEI ADD ADD | |
( seed randomizer ) | |
;rand/a STA | |
( setup screen ) | |
;on-frame .Screen/vector DEO2 | |
BRK | |
@on-frame ( -> ) | |
( slow down framerate a little ) | |
.framecount LDZ INC .framecount STZ | |
.framecount LDZ #03 MOD #00 EQU #01 JCN BRK | |
( choose random color ) | |
#00 #04 ;rand-between JSR2 | |
( set padding ) | |
#20 ,&pad STR | |
#10 ,&rsize STR | |
( store w h ) | |
#01 ,&rsize LDR ;rand-between JSR2 ,&rw STR | |
#01 ,&rsize LDR ;rand-between JSR2 ,&rh STR | |
( min x ) | |
,&pad LDR | |
( max x ) | |
.Screen/width DEI2 NIP ,&pad LDR SUB | |
( random x ) | |
;rand-between JSR2 TOSHORT | |
( offset by half w ) | |
,&rw LDR #02 DIV SUB | |
( min y ) | |
,&pad LDR | |
( max y ) | |
.Screen/height DEI2 NIP ,&pad LDR SUB | |
( random y ) | |
;rand-between JSR2 TOSHORT | |
( offset by half h ) | |
,&rh LDR #02 DIV SUB | |
( apply w h ) | |
,&rw LDR TOSHORT | |
,&rh LDR TOSHORT | |
( draw ) | |
;fill-rect JSR2 | |
BRK | |
[ &pad $1 &rsize $1 &rw $1 &rh $1 ] | |
@rand-between ( min max -- number ) | |
( random between two bytes ) | |
( max - min -- dist ) | |
OVR SUB | |
( rand ) | |
;rand JSR2 | |
( rand mod dist ) | |
SWP MOD | |
( add min ) | |
ADD | |
JMP2r | |
@rand ( -- number ) | |
( 8-bit PRNG https://github.com/edrosten/8bit_rng ) | |
( https://github.com/keijiro/uxn-sketches/blob/main/prng.tal ) | |
( t = x ^ (x << 4) ) | |
,&x LDR DUP #40 SFT EOR | |
( x = y ) | |
,&y LDR ,&x STR | |
( y = z ) | |
,&z LDR ,&y STR | |
( z = a ) | |
,&a LDR DUP ,&z STR | |
( a = z ^ t ^ (z >> 1) ^ (t << 1) ) | |
DUP #10 SFT EOR SWP DUP #01 SFT EOR EOR | |
DUP ,&a STR | |
JMP2r | |
[ &x $1 &y $1 &z $1 &a $1 ] | |
@fill-rect ( color x* y* w* h* -- ) | |
MUL2k | |
,&k STR2 | |
,&h STR2 | |
,&w STR2 | |
( dup x y ) | |
,&y STR2 | |
,&x STR2 | |
,&y LDR2 .Screen/y DEO2 | |
,&x LDR2 .Screen/x DEO2 | |
( reset counter ) | |
#0000 ,&i STR2 | |
&for-pixel | |
( draw pixel with color ) | |
DUP #40 ADD | |
.Screen/pixel DEO | |
( increment counter ) | |
,&i LDR2 INC2 ,&i STR2 | |
( get local x position by doing i % w ) | |
,&i LDR2 ,&w LDR2 MOD2 | |
( transform by absolute x ) | |
,&x LDR2 ADD2 | |
( put screen x ) | |
.Screen/x DEO2 | |
( get y position by doing i / w ) | |
,&i LDR2 ,&w LDR2 DIV2 | |
( transform by absolute y ) | |
,&y LDR2 ADD2 | |
( put screen y ) | |
.Screen/y DEO2 | |
( while i < k ) | |
,&i LDR2 | |
,&k LDR2 | |
LTH2 ,&for-pixel JCN | |
POP | |
JMP2r | |
[ &x $2 &y $2 &w $2 &h $2 &i $2 &k $2 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of storing all your literals outside of the
@fill-rect
routine, uxntal is a self-modifying programming language, so you can write literals directly into place :)For example:
You don't have to LDR2
var1
, it's already there waiting for the PC to run over it, in tight loops it's much faster to write the value directly into the LIT address where it's needed instead of juggling with the stack, you can also do a similar gymnastics to write specific opcodes in the program instead of branching.Your MOD/MOD2 macros can make use of the Keep mode to be slightly faster:
Neat little random number generator btw, you can find a bunch of helpers here.