~rabbits/uxn

895bfc86758e9284fdf4fa9e3e4b8d8ed3cc90b5 — Devine Lu Linvega 11 days ago 617e3d1
(mandelbrot.tal) Migrated to immediate opcodes
3 files changed, 55 insertions(+), 50 deletions(-)

M projects/examples/demos/mandelbrot.tal
M src/uxn-fast.c
A test.tal
M projects/examples/demos/mandelbrot.tal => projects/examples/demos/mandelbrot.tal +38 -40
@@ 1,80 1,77 @@
( mandelbrot )

%GTS2 { #8000 ADD2 SWP2 #8000 ADD2 LTH2 }
%AUTO-X    { #01 .Screen/auto DEO }
%NEXT-LINE { #0000 .Screen/x DEO2 .Screen/y DEI2k INC2 ROT DEO2 }

%WIDTH { #0280 }
%HEIGHT { #01e0 }

%WIDTH { #02a0 }
%HEIGHT { #0200 }
%XMIN { #de69 } ( -8601 )
%XMAX { #0b33 } ( 2867 )
%YMIN { #ecc7 } ( -4915 )
%YMAX { #1333 } ( 4915 )
%MAXI { #20 } ( 32 )
%DX { XMAX XMIN SUB2 WIDTH DIV2 } ( (XMAX-XMIN)/W )
%DY { YMAX YMIN SUB2 HEIGHT DIV2 } ( (YMAX-YMIN)/H )
%X  { .x LDZ2  } %Y  { .y LDZ2  }
%X2 { .x2 LDZ2 } %Y2 { .y2 LDZ2 }

|00 @System     &vector $2 &wst      $1 &rst    $1 &eaddr  $2 &ecode  $1 &pad     $1 &r       $2 &g      $2 &b     $2 &debug  $1 &halt $1
|20 @Screen     &vector $2 &width    $2 &height $2 &auto   $1 &pad    $1 &x       $2 &y       $2 &addr   $2 &pixel $1 &sprite $1

|0000 ( zero-page )

@x  $2 @y  $2
@x2 $2 @y2 $2
|00 @System &vector $2 &wst $1 &rst $1 &eaddr $2 &ecode $1 &pad $1 &r $2 &g $2 &b $2 &debug $1 &halt $1
|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1

|0100 ( -> )

	( theme ) 
	#0f0f .System/r DEO2 
	#0ff0 .System/g DEO2 
	( theme )
	#0f0f .System/r DEO2
	#0ff0 .System/g DEO2
	#00ff .System/b DEO2

	WIDTH .Screen/width DEO2 ( 640 )
	HEIGHT .Screen/height DEO2 ( 480 )

	AUTO-X

	;draw-mandel JSR2
	draw-mandel

BRK

@draw-mandel ( -- )

	XMAX XMIN SUB2 WIDTH DIV2 ;&dx STA2
	YMAX YMIN SUB2 HEIGHT DIV2 ;&dy STA2
	[ LIT2 01 -Screen/auto ] DEO
	YMAX YMIN
	&ver
		DUP2 ,&y STR2
		XMAX XMIN
		&hor
			DUP2 ,&x STR2
			#0000 DUP2k DUP2 .x STZ2 .y STZ2 .x2 STZ2 .y2 STZ2
			MAXI #00
			#0000
				DUP2 ,&x1 STR2
				DUP2 ,&y1 STR2
				DUP2 ,&x2 STR2
				,&y2 STR2
			( pixel )
			#2000
			&loop
				X Y ;smul2 JSR2 DUP2 ADD2 [ LIT2 &y $2 ] ADD2 .y STZ2
				X2 Y2 SUB2 [ LIT2 &x $2 ] ADD2 .x STZ2
				X DUP2 ;smul2 JSR2 .x2 STZ2
				Y DUP2 ;smul2 JSR2 .y2 STZ2
				X2 Y2 ADD2 #4000 GTH2 ,&end JCN
				INC GTHk ,&loop JCN
				[ LIT2 &x1 $2 ] [ LIT2 &y1 $2 ] smul2 DUP2 ADD2
					[ LIT2 &y $2 ] ADD2 ,&y1 STR2
				[ LIT2 &x2 $2 ] [ LIT2 &y2 $2 ] SUB2
					[ LIT2 &x $2 ] ADD2 ,&x1 STR2
				,&x1 LDR2 DUP2 smul2
					DUP2 ,&x2 STR2
				,&y1 LDR2 DUP2 smul2
					DUP2 ,&y2 STR2
				ADD2 #4000 GTH2 ?&end
				INC GTHk ?&loop
				&end
			NIP .Screen/pixel DEO
			DX ADD2 OVR2 OVR2 GTS2 ;&hor JCN2
			( done. )
			[ LIT2 &dx $2 ] ADD2 OVR2 #8000 ADD2 OVR2 #8000 ADD2 SWP2 LTH2 ?&hor
		POP2 POP2
		NEXT-LINE
		DY ADD2 OVR2 OVR2 GTS2 ;&ver JCN2
		#0000 .Screen/x DEO2
		.Screen/y DEI2k INC2 ROT DEO2
		[ LIT2 &dy $2 ] ADD2 OVR2 #8000 ADD2 OVR2 #8000 ADD2 SWP2 LTH2 ?&ver
	POP2 POP2

JMP2r

@smul2 ( a* b* -- c* )
	LITr 00
	DUP2 #8000 LTH2 ,&b-positive JCN
	DUP2 #8000 LTH2 ?&b-positive
	INCr DUP2k EOR2 SWP2 SUB2
	&b-positive
	SWP2
	DUP2 #8000 LTH2 ,&a-positive JCN
	DUP2 #8000 LTH2 ?&a-positive
	INCr DUP2k EOR2 SWP2 SUB2
	&a-positive
	( ahi alo bhi blo )


@@ 97,8 94,9 @@ JMP2r
	( The result we want is bits 27-12 due to the fixed point representation we use. )
	#04 SFT2 SWP2 #40 SFT2 ADD2
	( saturate to +/-7.fff )
	#7fff GTH2k JMP SWP2 NIP2
	STHr #01 NEQ ,&result-positive JCN
	#7fff GTH2k [ JMP SWP2 NIP2 ]
	STHr #01 NEQ ?&result-positive
	DUP2k EOR2 SWP2 SUB2
	&result-positive
JMP2r


M src/uxn-fast.c => src/uxn-fast.c +8 -10
@@ 29,7 29,7 @@ WITH REGARD TO THIS SOFTWARE.
#define N2 PEEK16(s->dat+s->ptr-4)
#define L2 PEEK16(s->dat+s->ptr-6)

#define HALT(c) { return uxn_halt(u, instr, (c), pc - 1); }
#define HALT(c) { return uxn_halt(u, ins, (c), pc - 1); }
#define INC(mul, add) { if(mul > s->ptr) HALT(1) s->ptr += k * mul + add; if(s->ptr > 255) HALT(2) }
#define DEC(mul, sub) { if(mul > s->ptr) HALT(1) s->ptr -= !k * mul - sub; if(s->ptr > 255) HALT(2) }
#define PUT(o, v) { s->dat[s->ptr - o - 1] = (v); }


@@ 40,17 40,15 @@ WITH REGARD TO THIS SOFTWARE.
int
uxn_eval(Uxn *u, Uint16 pc)
{
	Uint8 instr, opc, k;
	Uint8 ins, opc, k;
	Uint16 t, n, l, tmp;
	Stack *s;
	if(!pc || u->dev[0x0f]) return 0;
	for(;;) {
		instr = u->ram[pc++];
		k = !!(instr & 0x80);
		s = instr & 0x40 ? u->rst : u->wst;
		opc = instr & 0x3f;
		if(!(instr & 0x1f))
			opc = 0 - (instr >> 5);
		ins = u->ram[pc++];
		k = !!(ins & 0x80);
		s = ins & 0x40 ? u->rst : u->wst;
		opc = !(ins & 0x1f) ? 0 - (ins >> 5) : ins & 0x3f;
		switch(opc) {
			/* IMM */
			case 0x00: /* BRK   */ return 1;


@@ 90,8 88,8 @@ uxn_eval(Uxn *u, Uint16 pc)
			case 0x0d: /* JCN  */ t=T;n=N;        DEC(2, 0) pc += !!n * (Sint8)t; break; 
			case 0x2e: /* JSR2 */ t=T2;           DEC(2, 0) PUSH2(u->rst, pc) pc = t; break;                 
			case 0x0e: /* JSR  */ t=T;            DEC(1, 0) PUSH2(u->rst, pc) pc += (Sint8)t; break;
			case 0x2f: /* STH2 */ t=T2; if(instr & 0x40){ u->rst->ptr -= !k * 2; PUSH2(u->wst, t); } else{ u->wst->ptr -= !k * 2; PUSH2(u->rst, t); } break; 
			case 0x0f: /* STH  */ t=T;  if(instr & 0x40){ u->rst->ptr -= !k; PUSH(u->wst, t); } else{ u->wst->ptr -= !k; PUSH(u->rst, t); } break; 
			case 0x2f: /* STH2 */ t=T2; if(ins & 0x40) { u->rst->ptr -= !k * 2; PUSH2(u->wst, t); } else{ u->wst->ptr -= !k * 2; PUSH2(u->rst, t); } break; 
			case 0x0f: /* STH  */ t=T;  if(ins & 0x40) { u->rst->ptr -= !k; PUSH(u->wst, t); } else{ u->wst->ptr -= !k; PUSH(u->rst, t); } break; 
			case 0x30: /* LDZ2 */ t=T;            INC(1, 1) PUT2(0, PEEK16(u->ram + t)) break;                       
			case 0x10: /* LDZ  */ t=T;            INC(1, 0) PUT(0, u->ram[t]) break; 
			case 0x31: /* STZ2 */ t=T;n=H2;       DEC(3, 0) POKE16(u->ram + t, n) break;                     

A test.tal => test.tal +9 -0
@@ 0,0 1,9 @@

|0100


	

	#010f DEO

BRK
\ No newline at end of file