Browse code

(mandelbrot.tal) Migrated to immediate opcodes

Devine Lu Linvega authored on 03/03/2023 04:45:19
Showing 3 changed files
... ...
@@ -1,80 +1,77 @@
1 1
 ( mandelbrot )
2 2
 
3
-%GTS2 { #8000 ADD2 SWP2 #8000 ADD2 LTH2 }
4
-%AUTO-X    { #01 .Screen/auto DEO }
5
-%NEXT-LINE { #0000 .Screen/x DEO2 .Screen/y DEI2k INC2 ROT DEO2 }
6
-
7
-%WIDTH { #0280 }
8
-%HEIGHT { #01e0 }
9
-
3
+%WIDTH { #02a0 }
4
+%HEIGHT { #0200 }
10 5
 %XMIN { #de69 } ( -8601 )
11 6
 %XMAX { #0b33 } ( 2867 )
12 7
 %YMIN { #ecc7 } ( -4915 )
13 8
 %YMAX { #1333 } ( 4915 )
14
-%MAXI { #20 } ( 32 )
15
-%DX { XMAX XMIN SUB2 WIDTH DIV2 } ( (XMAX-XMIN)/W )
16
-%DY { YMAX YMIN SUB2 HEIGHT DIV2 } ( (YMAX-YMIN)/H )
17
-%X  { .x LDZ2  } %Y  { .y LDZ2  }
18
-%X2 { .x2 LDZ2 } %Y2 { .y2 LDZ2 }
19
-
20
-|00 @System     &vector $2 &wst      $1 &rst    $1 &eaddr  $2 &ecode  $1 &pad     $1 &r       $2 &g      $2 &b     $2 &debug  $1 &halt $1
21
-|20 @Screen     &vector $2 &width    $2 &height $2 &auto   $1 &pad    $1 &x       $2 &y       $2 &addr   $2 &pixel $1 &sprite $1
22 9
 
23
-|0000 ( zero-page )
24
-
25
-@x  $2 @y  $2
26
-@x2 $2 @y2 $2
10
+|00 @System &vector $2 &wst $1 &rst $1 &eaddr $2 &ecode $1 &pad $1 &r $2 &g $2 &b $2 &debug $1 &halt $1
11
+|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
27 12
 
28 13
 |0100 ( -> )
29 14
 
30
-	( theme ) 
31
-	#0f0f .System/r DEO2 
32
-	#0ff0 .System/g DEO2 
15
+	( theme )
16
+	#0f0f .System/r DEO2
17
+	#0ff0 .System/g DEO2
33 18
 	#00ff .System/b DEO2
34 19
 
35 20
 	WIDTH .Screen/width DEO2 ( 640 )
36 21
 	HEIGHT .Screen/height DEO2 ( 480 )
37 22
 
38
-	AUTO-X
39
-
40
-	;draw-mandel JSR2
23
+	draw-mandel
41 24
 
42 25
 BRK
43 26
 
44 27
 @draw-mandel ( -- )
45 28
 
29
+	XMAX XMIN SUB2 WIDTH DIV2 ;&dx STA2
30
+	YMAX YMIN SUB2 HEIGHT DIV2 ;&dy STA2
31
+	[ LIT2 01 -Screen/auto ] DEO
46 32
 	YMAX YMIN
47 33
 	&ver
48 34
 		DUP2 ,&y STR2
49 35
 		XMAX XMIN
50 36
 		&hor
51 37
 			DUP2 ,&x STR2
52
-			#0000 DUP2k DUP2 .x STZ2 .y STZ2 .x2 STZ2 .y2 STZ2
53
-			MAXI #00
38
+			#0000
39
+				DUP2 ,&x1 STR2
40
+				DUP2 ,&y1 STR2
41
+				DUP2 ,&x2 STR2
42
+				,&y2 STR2
43
+			( pixel )
44
+			#2000
54 45
 			&loop
55
-				X Y ;smul2 JSR2 DUP2 ADD2 [ LIT2 &y $2 ] ADD2 .y STZ2
56
-				X2 Y2 SUB2 [ LIT2 &x $2 ] ADD2 .x STZ2
57
-				X DUP2 ;smul2 JSR2 .x2 STZ2
58
-				Y DUP2 ;smul2 JSR2 .y2 STZ2
59
-				X2 Y2 ADD2 #4000 GTH2 ,&end JCN
60
-				INC GTHk ,&loop JCN
46
+				[ LIT2 &x1 $2 ] [ LIT2 &y1 $2 ] smul2 DUP2 ADD2
47
+					[ LIT2 &y $2 ] ADD2 ,&y1 STR2
48
+				[ LIT2 &x2 $2 ] [ LIT2 &y2 $2 ] SUB2
49
+					[ LIT2 &x $2 ] ADD2 ,&x1 STR2
50
+				,&x1 LDR2 DUP2 smul2
51
+					DUP2 ,&x2 STR2
52
+				,&y1 LDR2 DUP2 smul2
53
+					DUP2 ,&y2 STR2
54
+				ADD2 #4000 GTH2 ?&end
55
+				INC GTHk ?&loop
61 56
 				&end
62 57
 			NIP .Screen/pixel DEO
63
-			DX ADD2 OVR2 OVR2 GTS2 ;&hor JCN2
58
+			( done. )
59
+			[ LIT2 &dx $2 ] ADD2 OVR2 #8000 ADD2 OVR2 #8000 ADD2 SWP2 LTH2 ?&hor
64 60
 		POP2 POP2
65
-		NEXT-LINE
66
-		DY ADD2 OVR2 OVR2 GTS2 ;&ver JCN2
61
+		#0000 .Screen/x DEO2
62
+		.Screen/y DEI2k INC2 ROT DEO2
63
+		[ LIT2 &dy $2 ] ADD2 OVR2 #8000 ADD2 OVR2 #8000 ADD2 SWP2 LTH2 ?&ver
67 64
 	POP2 POP2
68 65
 
69 66
 JMP2r
70 67
 
71 68
 @smul2 ( a* b* -- c* )
72 69
 	LITr 00
73
-	DUP2 #8000 LTH2 ,&b-positive JCN
70
+	DUP2 #8000 LTH2 ?&b-positive
74 71
 	INCr DUP2k EOR2 SWP2 SUB2
75 72
 	&b-positive
76 73
 	SWP2
77
-	DUP2 #8000 LTH2 ,&a-positive JCN
74
+	DUP2 #8000 LTH2 ?&a-positive
78 75
 	INCr DUP2k EOR2 SWP2 SUB2
79 76
 	&a-positive
80 77
 	( ahi alo bhi blo )
... ...
@@ -97,8 +94,9 @@ JMP2r
97 94
 	( The result we want is bits 27-12 due to the fixed point representation we use. )
98 95
 	#04 SFT2 SWP2 #40 SFT2 ADD2
99 96
 	( saturate to +/-7.fff )
100
-	#7fff GTH2k JMP SWP2 NIP2
101
-	STHr #01 NEQ ,&result-positive JCN
97
+	#7fff GTH2k [ JMP SWP2 NIP2 ]
98
+	STHr #01 NEQ ?&result-positive
102 99
 	DUP2k EOR2 SWP2 SUB2
103 100
 	&result-positive
104 101
 JMP2r
102
+
... ...
@@ -29,7 +29,7 @@ WITH REGARD TO THIS SOFTWARE.
29 29
 #define N2 PEEK16(s->dat+s->ptr-4)
30 30
 #define L2 PEEK16(s->dat+s->ptr-6)
31 31
 
32
-#define HALT(c) { return uxn_halt(u, instr, (c), pc - 1); }
32
+#define HALT(c) { return uxn_halt(u, ins, (c), pc - 1); }
33 33
 #define INC(mul, add) { if(mul > s->ptr) HALT(1) s->ptr += k * mul + add; if(s->ptr > 255) HALT(2) }
34 34
 #define DEC(mul, sub) { if(mul > s->ptr) HALT(1) s->ptr -= !k * mul - sub; if(s->ptr > 255) HALT(2) }
35 35
 #define PUT(o, v) { s->dat[s->ptr - o - 1] = (v); }
... ...
@@ -40,17 +40,15 @@ WITH REGARD TO THIS SOFTWARE.
40 40
 int
41 41
 uxn_eval(Uxn *u, Uint16 pc)
42 42
 {
43
-	Uint8 instr, opc, k;
43
+	Uint8 ins, opc, k;
44 44
 	Uint16 t, n, l, tmp;
45 45
 	Stack *s;
46 46
 	if(!pc || u->dev[0x0f]) return 0;
47 47
 	for(;;) {
48
-		instr = u->ram[pc++];
49
-		k = !!(instr & 0x80);
50
-		s = instr & 0x40 ? u->rst : u->wst;
51
-		opc = instr & 0x3f;
52
-		if(!(instr & 0x1f))
53
-			opc = 0 - (instr >> 5);
48
+		ins = u->ram[pc++];
49
+		k = !!(ins & 0x80);
50
+		s = ins & 0x40 ? u->rst : u->wst;
51
+		opc = !(ins & 0x1f) ? 0 - (ins >> 5) : ins & 0x3f;
54 52
 		switch(opc) {
55 53
 			/* IMM */
56 54
 			case 0x00: /* BRK   */ return 1;
... ...
@@ -90,8 +88,8 @@ uxn_eval(Uxn *u, Uint16 pc)
90 88
 			case 0x0d: /* JCN  */ t=T;n=N;        DEC(2, 0) pc += !!n * (Sint8)t; break; 
91 89
 			case 0x2e: /* JSR2 */ t=T2;           DEC(2, 0) PUSH2(u->rst, pc) pc = t; break;                 
92 90
 			case 0x0e: /* JSR  */ t=T;            DEC(1, 0) PUSH2(u->rst, pc) pc += (Sint8)t; break;
93
-			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; 
94
-			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; 
91
+			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; 
92
+			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; 
95 93
 			case 0x30: /* LDZ2 */ t=T;            INC(1, 1) PUT2(0, PEEK16(u->ram + t)) break;                       
96 94
 			case 0x10: /* LDZ  */ t=T;            INC(1, 0) PUT(0, u->ram[t]) break; 
97 95
 			case 0x31: /* STZ2 */ t=T;n=H2;       DEC(3, 0) POKE16(u->ram + t, n) break;                     
98 96
new file mode 100644
... ...
@@ -0,0 +1,9 @@
1
+
2
+|0100
3
+
4
+
5
+	
6
+
7
+	#010f DEO
8
+
9
+BRK
0 10
\ No newline at end of file