Browse code

Removed RTN

neauoire authored on 16/03/2021 04:29:44
Showing 13 changed files
... ...
@@ -45,7 +45,7 @@ Program p;
45 45
 /* clang-format off */
46 46
 
47 47
 char ops[][4] = {
48
-	"BRK", "NOP", "LIT", "LDR", "STR", "JMP", "JSR", "RTN", 
48
+	"BRK", "NOP", "LIT", "LDR", "STR", "JMP", "JSR", "---", 
49 49
 	"EQU", "NEQ", "GTH", "LTH", "AND", "XOR", "ROL", "ROR",
50 50
 	"POP", "DUP", "SWP", "OVR", "ROT", "CLN", "WSR", "RSW",
51 51
 	"ADD", "SUB", "MUL", "DIV", "---", "---", "---", "---"
... ...
@@ -155,7 +155,7 @@ findopcode(char *s)
155 155
 			continue;
156 156
 		while(s[3 + m]) {
157 157
 			if(s[3 + m] == '2') i |= (1 << 5); /* mode: short */
158
-			if(s[3 + m] == 'S') i |= (1 << 6); /* mode: signed */
158
+			if(s[3 + m] == 'r') i |= (1 << 6); /* mode: return */
159 159
 			if(s[3 + m] == '?') i |= (1 << 7); /* mode: conditional */
160 160
 			m++;
161 161
 		}
... ...
@@ -20,5 +20,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr
20 20
 # cc uxn.c emulator.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -L/usr/local/lib -lSDL2 -o bin/emulator
21 21
 
22 22
 # run
23
-./bin/assembler projects/software/nasu.usm bin/boot.rom
23
+./bin/assembler projects/software/left.usm bin/boot.rom
24 24
 ./bin/emulator bin/boot.rom
... ...
@@ -1,5 +1,7 @@
1 1
 ( hello world )
2 2
 
3
+%RTN { JMP2r }
4
+
3 5
 @RESET 
4 6
 	
5 7
 	,text1 ,print-label JSR2
... ...
@@ -1,5 +1,7 @@
1 1
 ( Mouse )
2 2
 
3
+%RTN { JMP2r }
4
+
3 5
 ;label { x 2 y 2 color 1 addr 2 }
4 6
 ;cat { x 2 y 2 } 
5 7
 ;pointer { x 2 y 2 }
... ...
@@ -1,5 +1,7 @@
1 1
 ( app/hex )      
2 2
 
3
+%RTN { JMP2r }
4
+
3 5
 ;label { x 2 y 2 color 1 addr 2 }
4 6
 ;pixel { x 1 y 1 }
5 7
 ;window { x1 2 y1 2 x2 2 y2 2 }
... ...
@@ -7,7 +9,6 @@
7 9
 ;editor { x1 2 y1 2 addr 2 }
8 10
 ;rect { x1 2 y1 2 x2 2 y2 2 }
9 11
 ;color { byte 1 }
10
-;addr { short 2 }
11 12
 
12 13
 |0100 @RESET
13 14
 	
... ...
@@ -1,5 +1,7 @@
1 1
 ( draw routines )
2 2
 
3
+%RTN { JMP2r }
4
+
3 5
 ;color { byte 1 }
4 6
 ;pointer { x 2 y 2 sprite 2 } 
5 7
 ;rect { x1 2 y1 2 x2 2 y2 2 }
... ...
@@ -1,5 +1,7 @@
1 1
 ( draw routines )
2 2
 
3
+%RTN { JMP2r }
4
+
3 5
 ;label { x 2 y 2 color 1 addr 2 }
4 6
 ;rect { x1 2 y1 2 x2 2 y2 2 }
5 7
 ;center { x 2 y 2 }
... ...
@@ -1,5 +1,7 @@
1 1
 ( gui/picture )
2 2
 
3
+%RTN { JMP2r }
4
+
3 5
 ;pict { x 2 y 2 width 2 height 2 color 1 addr 2 }
4 6
 
5 7
 |0100 @RESET
... ...
@@ -1,5 +1,7 @@
1 1
 ( draw routines )
2 2
 
3
+%RTN { JMP2r }
4
+
3 5
 ;label { x 2 y 2 color 1 addr 2 }
4 6
 ;pict { x 2 y 2 width 2 height 2 color 1 addr 2 }
5 7
 ;rect { x1 2 y1 2 x2 2 y2 2 }
... ...
@@ -15,6 +15,9 @@
15 15
 			- Real scrolling distance
16 16
 )  
17 17
 
18
+%RTN { JMP2r }
19
+%RTN? { JMP2r? }
20
+
18 21
 ;lock { byte 1 }
19 22
 ;k { byte 1 } 
20 23
 ;l { byte 1 } 
... ...
@@ -6,6 +6,9 @@
6 6
 	1 2 3  - Select brush
7 7
 )
8 8
 
9
+%RTN { JMP2r }
10
+%RTN? { JMP2r? }
11
+
9 12
 ;bankview { x 2 y 2 mode 1 addr 2 }
10 13
 ;tileview { x 2 y 2 addr 2 }
11 14
 ;rect { x1 2 y1 2 x2 2 y2 2 }
... ...
@@ -33,63 +33,63 @@ Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1)
33 33
 void op_brk(Uxn *u) { setflag(&u->status, FLAG_HALT, 1); }
34 34
 void op_lit(Uxn *u) { u->literal += 1; }
35 35
 void op_nop(Uxn *u) { (void)u; }
36
-void op_jmp(Uxn *u) { Uint8 a = pop8(&u->wst); u->ram.ptr += (Sint8)a; }
37
-void op_jsr(Uxn *u) { Uint8 a = pop8(&u->wst); push16(&u->rst, u->ram.ptr); u->ram.ptr += (Sint8)a; }
38
-void op_rtn(Uxn *u) { u->ram.ptr = pop16(&u->rst); }
39
-void op_ldr(Uxn *u) { Uint16 a = pop16(&u->wst); push8(&u->wst, mempeek8(u, a)); }
40
-void op_str(Uxn *u) { Uint16 a = pop16(&u->wst); Uint8 b = pop8(&u->wst); mempoke8(u, a, b); }
36
+void op_jmp(Uxn *u) { Uint8 a = pop8(u->src); u->ram.ptr += (Sint8)a; }
37
+void op_jsr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->dst, u->ram.ptr); u->ram.ptr += (Sint8)a; }
38
+void op_rtn(Uxn *u) { u->ram.ptr = pop16(u->dst); }
39
+void op_ldr(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u, a)); }
40
+void op_str(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); }
41 41
 /* Logic */
42
-void op_and(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b & a); }
43
-void op_xor(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b | a); }
44
-void op_rol(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b << (a % 8)); }
45
-void op_ror(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, b >> (a % 8)); }
42
+void op_and(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b & a); }
43
+void op_xor(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b | a); }
44
+void op_rol(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b << (a % 8)); }
45
+void op_ror(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b >> (a % 8)); }
46 46
 /* Stack */
47
-void op_pop(Uxn *u) { pop8(&u->wst); }
48
-void op_dup(Uxn *u) { push8(&u->wst, peek8(&u->wst, 0)); }
49
-void op_swp(Uxn *u) { Uint8 b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, a); }
50
-void op_ovr(Uxn *u) { push8(&u->wst, peek8(&u->wst, 1)); }
51
-void op_rot(Uxn *u) { Uint8 c = pop8(&u->wst), b = pop8(&u->wst), a = pop8(&u->wst); push8(&u->wst, b); push8(&u->wst, c); push8(&u->wst, a); }
52
-void op_cln(Uxn *u) { push8(&u->wst, peek8(&u->rst, 0)); }
53
-void op_wsr(Uxn *u) { Uint8 a = pop8(&u->wst); push8(&u->rst, a); }
54
-void op_rsw(Uxn *u) { Uint8 a = pop8(&u->rst); push8(&u->wst, a); }
47
+void op_pop(Uxn *u) { pop8(u->src); }
48
+void op_dup(Uxn *u) { push8(u->src, peek8(u->src, 0)); }
49
+void op_swp(Uxn *u) { Uint8 b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, a); }
50
+void op_ovr(Uxn *u) { push8(u->src, peek8(u->src, 1)); }
51
+void op_rot(Uxn *u) { Uint8 c = pop8(u->src), b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, c); push8(u->src, a); }
52
+void op_cln(Uxn *u) { push8(u->src, peek8(u->dst, 0)); }
53
+void op_wsr(Uxn *u) { Uint8 a = pop8(u->src); push8(u->dst, a); }
54
+void op_rsw(Uxn *u) { Uint8 a = pop8(u->dst); push8(u->src, a); }
55 55
 /* Arithmetic */
56
-void op_add(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b + (Sint8)a : b + a); }
57
-void op_sub(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b - (Sint8)a : b - a); }
58
-void op_mul(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b * (Sint8)a : b * a); }
59
-void op_div(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b / (Sint8)a : b / a); }
60
-void op_equ(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b == (Sint8)a : b == a); }
61
-void op_neq(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b != (Sint8)a : b != a); }
62
-void op_gth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b > (Sint8)a : b > a); }
63
-void op_lth(Uxn *u) { Uint8 a = pop8(&u->wst), b = pop8(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint8)b < (Sint8)a : b < a); }
56
+void op_add(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b + a); }
57
+void op_sub(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b - a); }
58
+void op_mul(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b * a); }
59
+void op_div(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b / a); }
60
+void op_equ(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b == a); }
61
+void op_neq(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b != a); }
62
+void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b > a); }
63
+void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); }
64 64
 /* --- */
65 65
 void op_lit16(Uxn *u) { u->literal += 2; }
66
-void op_nop16(Uxn *u) { printf("%04x\n", pop16(&u->wst)); }
67
-void op_jmp16(Uxn *u) { u->ram.ptr = pop16(&u->wst); }
68
-void op_jsr16(Uxn *u) { push16(&u->rst, u->ram.ptr); u->ram.ptr = pop16(&u->wst); }
69
-void op_ldr16(Uxn *u) { Uint16 a = pop16(&u->wst); push16(&u->wst, mempeek16(u, a)); }
70
-void op_str16(Uxn *u) { Uint16 a = pop16(&u->wst); Uint16 b = pop16(&u->wst); mempoke16(u, a, b); }
71
-void op_and16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b & a); }
72
-void op_xor16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b ^ a); }
73
-void op_rol16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b << (a % 16)); }
74
-void op_ror16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, b >> (a % 16)); }
66
+void op_nop16(Uxn *u) { printf("%04x\n", pop16(u->src)); }
67
+void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); }
68
+void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); }
69
+void op_ldr16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, mempeek16(u, a)); }
70
+void op_str16(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop16(u->src); mempoke16(u, a, b); }
71
+void op_and16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b & a); }
72
+void op_xor16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b ^ a); }
73
+void op_rol16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b << (a % 16)); }
74
+void op_ror16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b >> (a % 16)); }
75 75
 /* Stack(16-bits) */
76
-void op_pop16(Uxn *u) { pop16(&u->wst); }
77
-void op_dup16(Uxn *u) { push16(&u->wst, peek16(&u->wst, 0)); }
78
-void op_swp16(Uxn *u) { Uint16 b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, a); }
79
-void op_ovr16(Uxn *u) { push16(&u->wst, peek16(&u->wst, 1)); }
80
-void op_rot16(Uxn *u) { Uint16 c = pop16(&u->wst), b = pop16(&u->wst), a = pop16(&u->wst); push16(&u->wst, b); push16(&u->wst, c); push16(&u->wst, a); }
81
-void op_cln16(Uxn *u) { push16(&u->wst, peek16(&u->rst, 0)); }
82
-void op_wsr16(Uxn *u) { Uint16 a = pop16(&u->wst); push16(&u->rst, a); }
83
-void op_rsw16(Uxn *u) { Uint16 a = pop16(&u->rst); push16(&u->wst, a); }
76
+void op_pop16(Uxn *u) { pop16(u->src); }
77
+void op_dup16(Uxn *u) { push16(u->src, peek16(u->src, 0)); }
78
+void op_swp16(Uxn *u) { Uint16 b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, a); }
79
+void op_ovr16(Uxn *u) { push16(u->src, peek16(u->src, 1)); }
80
+void op_rot16(Uxn *u) { Uint16 c = pop16(u->src), b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, c); push16(u->src, a); }
81
+void op_cln16(Uxn *u) { push16(u->src, peek16(u->dst, 0)); }
82
+void op_wsr16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->dst, a); }
83
+void op_rsw16(Uxn *u) { Uint16 a = pop16(u->dst); push16(u->src, a); }
84 84
 /* Arithmetic(16-bits) */
85
-void op_add16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b + (Sint16)a : b + a); }
86
-void op_sub16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b - (Sint16)a : b - a); }
87
-void op_mul16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b * (Sint16)a : b * a); }
88
-void op_div16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push16(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b / (Sint16)a : b / a); }
89
-void op_equ16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b == (Sint16)a : b == a); }
90
-void op_neq16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b != (Sint16)a : b != a); }
91
-void op_gth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b > (Sint16)a : b > a); }
92
-void op_lth16(Uxn *u) { Uint16 a = pop16(&u->wst), b = pop16(&u->wst); push8(&u->wst, getflag(&u->status, FLAG_SIGN) ? (Sint16)b < (Sint16)a : b < a); }
85
+void op_add16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b + a); }
86
+void op_sub16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b - a); }
87
+void op_mul16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b * a); }
88
+void op_div16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b / a); }
89
+void op_equ16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b == a); }
90
+void op_neq16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b != a); }
91
+void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b > a); }
92
+void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); }
93 93
 
94 94
 void (*ops[])(Uxn *u) = {
95 95
 	op_brk, op_nop, op_lit, op_ldr, op_str, op_jmp, op_jsr, op_rtn, 
... ...
@@ -139,25 +139,32 @@ lituxn(Uxn *u, Uint8 instr)
139 139
 int
140 140
 opcuxn(Uxn *u, Uint8 instr)
141 141
 {
142
-	Uint8 op = instr & 0x1f, fcond;
142
+	Uint8 op = instr & 0x1f, fcond, freturn;
143 143
 	setflag(&u->status, FLAG_SHORT, (instr >> 5) & 1);
144
-	setflag(&u->status, FLAG_SIGN, (instr >> 6) & 1);
144
+	setflag(&u->status, FLAG_RETURN, (instr >> 6) & 1);
145 145
 	setflag(&u->status, FLAG_COND, (instr >> 7) & 1);
146 146
 	fcond = getflag(&u->status, FLAG_COND);
147
+	freturn = getflag(&u->status, FLAG_RETURN);
148
+	u->src = freturn ? &u->rst : &u->wst;
149
+	u->dst = freturn ? &u->wst : &u->rst;
147 150
 	if(getflag(&u->status, FLAG_SHORT))
148 151
 		op += 32;
149
-	if(u->wst.ptr < opr[op][0] || (fcond && u->wst.ptr < 1))
152
+	if(u->src->ptr < opr[op][0] || (fcond && u->src->ptr < 1))
150 153
 		return haltuxn(u, "Working-stack underflow", op);
151
-	if(u->wst.ptr + opr[op][1] - opr[op][0] >= 255)
154
+	if(u->src->ptr + opr[op][1] - opr[op][0] >= 255)
152 155
 		return haltuxn(u, "Working-stack overflow", instr);
153
-	if(u->rst.ptr < opr[op][2])
156
+	if(u->dst->ptr < opr[op][2])
154 157
 		return haltuxn(u, "Return-stack underflow", op);
155
-	if(u->rst.ptr + opr[op][3] - opr[op][2] >= 255)
158
+	if(u->dst->ptr + opr[op][3] - opr[op][2] >= 255)
156 159
 		return haltuxn(u, "Return-stack overflow", instr);
157 160
 	if(!fcond || (fcond && pop8(&u->wst)))
158 161
 		(*ops[op])(u);
159
-	else
160
-		u->wst.ptr -= opr[op][0] - opr[op][1];
162
+	else {
163
+		if(freturn)
164
+			u->src->ptr -= opr[op][2] - opr[op][3];
165
+		else
166
+			u->src->ptr -= opr[op][0] - opr[op][1];
167
+	}
161 168
 	return 1;
162 169
 }
163 170
 
... ...
@@ -18,7 +18,7 @@ typedef signed short Sint16;
18 18
 
19 19
 #define FLAG_HALT 0x01
20 20
 #define FLAG_SHORT 0x02
21
-#define FLAG_SIGN 0x04
21
+#define FLAG_RETURN 0x04
22 22
 #define FLAG_COND 0x08
23 23
 
24 24
 typedef struct {
... ...
@@ -39,7 +39,7 @@ typedef struct Device {
39 39
 typedef struct {
40 40
 	Uint8 literal, status, devices;
41 41
 	Uint16 counter, vreset, vframe, verror;
42
-	Stack wst, rst;
42
+	Stack wst, rst, *src, *dst;
43 43
 	Memory ram;
44 44
 	Device dev[16];
45 45
 } Uxn;