Browse code

Removed conditional mode

neauoire authored on 26/03/2021 15:35:45
Showing 7 changed files
... ...
@@ -28,7 +28,7 @@ else
28 28
 fi
29 29
 
30 30
 echo "Assembling.."
31
-./bin/assembler projects/software/noodle.usm bin/boot.rom
31
+./bin/assembler projects/software/nasu.usm bin/boot.rom
32 32
 
33 33
 echo "Running.."
34 34
 if [ "${2}" = '--cli' ]; 
... ...
@@ -16,7 +16,8 @@
16 16
 )  
17 17
 
18 18
 %RTN { JMP2r }
19
-%RTN? { JMP2r? }
19
+%RTN? { JNZ2r }
20
+%JMP2? { JNZ2 }
20 21
 
21 22
 %++ { #0001 ADD2 } %-- { #0001 SUB2 }
22 23
 %8/ { #0003 SFT2 } %8* { #0030 SFT2 }
... ...
@@ -8,7 +8,8 @@
8 8
 )
9 9
 
10 10
 %RTN   { JMP2r }
11
-%RTN?  { JMP2r? }
11
+%RTN?  { JNZ2r }
12
+%JMP2? { JNZ2 }
12 13
 %STEP8 { #0033 SFT2 }
13 14
 
14 15
 %++ { #0001 ADD2 }
... ...
@@ -17,9 +17,10 @@
17 17
 		- Zoom drawing with other tools
18 18
 )
19 19
 
20
+%JMP? { JNZ }
21
+%JMP2? { JNZ2 }
20 22
 %RTN   { JMP2r }
21
-%RTN?  { JMP2r? }
22
-%ABS2 { DUP2 #000f SFT2 #ffff SWP2 SWP POP MUL2? } 
23
+%ABS2 { DUP2 #000f SFT2 EQU #04 SWP JNZ #ffff MUL2 }
23 24
 %STEP8 { #0033 SFT2 }
24 25
 %MOD8 { #0007 AND2  }
25 26
 %SFL { #40 SFT SFT }
... ...
@@ -250,7 +251,7 @@ BRK
250 251
 			( move ) ~zoom.x -- =zoom.x $no-left
251 252
 		DUP #07 SFT #01 AND #01 NEQ ,$no-right ROT JMP2?
252 253
 			( move ) ~zoom.x ++ =zoom.x $no-right
253
-	#00 NEQ ,draw-canvas ROT JSR2?
254
+	#00 EQU #04 SWP JMP? ,draw-canvas JSR2
254 255
 	
255 256
 	,$no-keys ~Keys #00 EQU JMP2?
256 257
 		~Keys 
... ...
@@ -555,7 +556,7 @@ RTN
555 556
 	#0000 EQU2 
556 557
 	~Mouse.state 
557 558
 	#00 NEQ 
558
-	#0101 EQU2 RTN? ( Return if unchanged )
559
+	#0101 EQU2 JNZ2r ( Return if unchanged )
559 560
 
560 561
 	,blank_icn =Sprite.addr
561 562
 
... ...
@@ -47,7 +47,7 @@ Program p;
47 47
 char ops[][4] = {
48 48
 	"BRK", "NOP", "LIT", "POP", "DUP", "SWP", "OVR", "ROT",
49 49
 	"EQU", "NEQ", "GTH", "LTH", "GTS", "LTS", "JMP", "JSR",
50
-	"PEK", "POK", "LDR", "STR", "---", "---", "CLN", "STH",
50
+	"PEK", "POK", "LDR", "STR", "JNZ", "---", "CLN", "STH",
51 51
 	"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
52 52
 };
53 53
 
... ...
@@ -156,7 +156,7 @@ findopcode(char *s)
156 156
 		while(s[3 + m]) {
157 157
 			if(s[3 + m] == '2') i |= (1 << 5); /* mode: short */
158 158
 			if(s[3 + m] == 'r') i |= (1 << 6); /* mode: return */
159
-			if(s[3 + m] == '?') i |= (1 << 7); /* mode: conditional */
159
+			if(s[3 + m] == '?') return 0;      /* forget that conditionals exist */
160 160
 			m++;
161 161
 		}
162 162
 		return i;
... ...
@@ -23,9 +23,9 @@ void   mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = (a & 0xff00) == PAG
23 23
 Uint8  mempeek8(Uxn *u, Uint16 a) { return u->ram.dat[a]; }
24 24
 void   mempoke16(Uxn *u, Uint16 a, Uint16 b) { mempoke8(u, a, b >> 8); mempoke8(u, a + 1, b); }
25 25
 Uint16 mempeek16(Uxn *u, Uint16 a) { return (mempeek8(u, a) << 8) + mempeek8(u, a + 1); }
26
-void   push8(Stack *s, Uint8 a) { s->dat[s->ptr++] = a; }
27
-Uint8  pop8(Stack *s) { return s->dat[--s->ptr]; }
28
-Uint8  peek8(Stack *s, Uint8 a) { return s->dat[s->ptr - a - 1]; }
26
+void   push8(Stack *s, Uint8 a) { if (s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
27
+Uint8  pop8(Stack *s) { if (s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
28
+Uint8  peek8(Stack *s, Uint8 a) { if (s->ptr < a + 1) s->error = 1; return s->dat[s->ptr - a - 1]; }
29 29
 void   push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
30 30
 Uint16 pop16(Stack *s) { return pop8(s) + (pop8(s) << 8); }
31 31
 Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1) << 8); }
... ...
@@ -46,6 +46,7 @@ void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b
46 46
 void op_gts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b > (Sint8)a); }
47 47
 void op_lts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b < (Sint8)a); }
48 48
 void op_jmp(Uxn *u) { Uint8 a = pop8(u->src); u->ram.ptr += (Sint8)a; }
49
+void op_jnz(Uxn *u) { Uint8 a = pop8(&u->wst), b; if (getflag(&u->status, FLAG_RETURN) && !a) return; b = pop8(u->src); if (a) u->ram.ptr += (Sint8)b; }
49 50
 void op_jsr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->dst, u->ram.ptr); u->ram.ptr += (Sint8)a; }
50 51
 /* Memory */
51 52
 void op_pek(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u, a)); }
... ...
@@ -79,6 +80,7 @@ void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->sr
79 80
 void op_gts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b > (Sint16)a); }
80 81
 void op_lts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b < (Sint16)a); }
81 82
 void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); }
83
+void op_jnz16(Uxn *u) { Uint8 a = pop8(&u->wst); if (getflag(&u->status, FLAG_RETURN) && !a) return; Uint16 b = pop16(u->src); if (a) u->ram.ptr = b; }
82 84
 void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); }
83 85
 /* Memory(16-bits) */
84 86
 void op_pek16(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u, a)); }
... ...
@@ -100,27 +102,15 @@ void op_sft16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); Uint8 left
100 102
 void (*ops[])(Uxn *u) = {
101 103
 	op_brk, op_nop, op_lit, op_pop, op_dup, op_swp, op_ovr, op_rot,
102 104
 	op_equ, op_neq, op_gth, op_lth, op_gts, op_lts, op_jmp, op_jsr, 
103
-	op_pek, op_pok, op_ldr, op_str, op_nop, op_nop, op_cln, op_sth, 
105
+	op_pek, op_pok, op_ldr, op_str, op_jnz, op_nop, op_cln, op_sth, 
104 106
 	op_add, op_sub, op_mul, op_div, op_and, op_ora, op_eor, op_sft,
105 107
 	/* 16-bit */
106 108
 	op_brk,   op_nop16, op_lit16, op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16,
107 109
 	op_equ16, op_neq16, op_gth16, op_lth16, op_gts16, op_lts16, op_jmp16, op_jsr16, 
108
-	op_pek16, op_pok16, op_ldr16, op_str16, op_nop,   op_nop,   op_cln16, op_sth16, 
110
+	op_pek16, op_pok16, op_ldr16, op_str16, op_jnz16, op_nop,   op_cln16, op_sth16, 
109 111
 	op_add16, op_sub16, op_mul16, op_div16, op_and16, op_ora16, op_eor16, op_sft16
110 112
 };
111 113
 
112
-Uint8 opr[][4] = { /* wstack-/+ rstack-/+ */
113
-	{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {1,0,0,0}, {1,2,0,0}, {2,2,0,0}, {2,3,0,0}, {3,3,0,0},
114
-	{2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {1,0,0,0}, {1,0,0,2},
115
-	{1,1,0,0}, {2,0,0,0}, {1,2,0,0}, {3,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,1,1,1}, {1,0,0,1},
116
-	{2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0},
117
-	/* 16-bit */
118
-	{0,0,0,0}, {2,0,0,0}, {0,0,0,0}, {2,0,0,0}, {2,4,0,0}, {4,4,0,0}, {4,6,0,0}, {6,6,0,0},
119
-	{4,1,0,0}, {4,1,0,0}, {4,1,0,0}, {4,1,0,0}, {4,1,0,0}, {4,1,0,0}, {2,0,0,0}, {2,0,0,2},
120
-	{2,1,0,0}, {3,0,0,0}, {2,2,0,0}, {4,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,2,2,2}, {2,0,0,2},
121
-	{4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}
122
-};
123
-
124 114
 /* clang-format on */
125 115
 
126 116
 #pragma mark - Core
... ...
@@ -132,51 +122,39 @@ haltuxn(Uxn *u, char *name, int id)
132 122
 	return 0;
133 123
 }
134 124
 
135
-int
125
+void
136 126
 lituxn(Uxn *u, Uint8 instr)
137 127
 {
138
-	if(u->wst.ptr >= 255)
139
-		return haltuxn(u, "Stack overflow", instr);
140 128
 	push8(&u->wst, instr);
141 129
 	u->literal--;
142
-	return 1;
143 130
 }
144 131
 
145
-int
132
+void
146 133
 opcuxn(Uxn *u, Uint8 instr)
147 134
 {
148
-	Uint8 op = instr & 0x1f, fcond, freturn;
135
+	Uint8 op = instr & 0x1f, freturn;
149 136
 	setflag(&u->status, FLAG_SHORT, (instr >> 5) & 1);
150 137
 	setflag(&u->status, FLAG_RETURN, (instr >> 6) & 1);
151
-	setflag(&u->status, FLAG_COND, (instr >> 7) & 1);
152
-	fcond = getflag(&u->status, FLAG_COND);
153 138
 	freturn = getflag(&u->status, FLAG_RETURN);
154 139
 	u->src = freturn ? &u->rst : &u->wst;
155 140
 	u->dst = freturn ? &u->wst : &u->rst;
156 141
 	if(getflag(&u->status, FLAG_SHORT))
157 142
 		op += 32;
158
-	if(u->src->ptr < opr[op][0] || (fcond && u->src->ptr < 1))
159
-		return haltuxn(u, "Working-stack underflow", op);
160
-	if(u->src->ptr + opr[op][1] - opr[op][0] >= 255)
161
-		return haltuxn(u, "Working-stack overflow", instr);
162
-	if(u->dst->ptr < opr[op][2])
163
-		return haltuxn(u, "Return-stack underflow", op);
164
-	if(u->dst->ptr + opr[op][3] - opr[op][2] >= 255)
165
-		return haltuxn(u, "Return-stack overflow", instr);
166
-	if(!fcond || (fcond && pop8(&u->wst)))
167
-		(*ops[op])(u);
168
-	else
169
-		u->src->ptr -= opr[op][freturn ? 2 : 0] - opr[op][freturn ? 3 : 1];
170
-	return 1;
143
+	(*ops[op])(u);
171 144
 }
172 145
 
173 146
 int
174 147
 stepuxn(Uxn *u, Uint8 instr)
175 148
 {
176 149
 	if(u->literal > 0)
177
-		return lituxn(u, instr);
150
+		lituxn(u, instr);
178 151
 	else
179
-		return opcuxn(u, instr);
152
+		opcuxn(u, instr);
153
+	if(u->wst.error)
154
+		return haltuxn(u, u->wst.error == 1 ? "Working-stack underflow" : "Working-stack overflow", instr);
155
+	if(u->rst.error)
156
+		return haltuxn(u, u->rst.error == 1 ? "Return-stack underflow" : "Return-stack overflow", instr);
157
+	return 1;
180 158
 }
181 159
 
182 160
 int
... ...
@@ -184,6 +162,8 @@ evaluxn(Uxn *u, Uint16 vec)
184 162
 {
185 163
 	u->literal = 0;
186 164
 	u->ram.ptr = vec;
165
+	u->wst.error = 0;
166
+	u->rst.error = 0;
187 167
 	setflag(&u->status, FLAG_HALT, 0);
188 168
 	while(!(u->status & FLAG_HALT)) {
189 169
 		Uint8 instr = u->ram.dat[u->ram.ptr++];
... ...
@@ -19,11 +19,10 @@ typedef signed short Sint16;
19 19
 #define FLAG_HALT 0x01
20 20
 #define FLAG_SHORT 0x02
21 21
 #define FLAG_RETURN 0x04
22
-#define FLAG_COND 0x08
23 22
 #define PAGE_DEVICE 0x0100
24 23
 
25 24
 typedef struct {
26
-	Uint8 ptr;
25
+	Uint8 ptr, error;
27 26
 	Uint8 dat[256];
28 27
 } Stack;
29 28