Browse code

Building a loop example

neauoire authored on 02/02/2021 20:22:20
Showing 4 changed files
... ...
@@ -14,5 +14,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr
14 14
 cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c -o uxn
15 15
 
16 16
 # run
17
-./uxnasm examples/core.usm boot.rom
17
+./uxnasm examples/loop.usm boot.rom
18 18
 ./uxn boot.rom
... ...
@@ -1,4 +1,4 @@
1 1
 < core >
2 2
 
3
-01 02 ADD
3
+0102
4 4
 
5 5
new file mode 100644
... ...
@@ -0,0 +1,10 @@
1
+< loop >
2
+
3
+01 .loop JSR BRK
4
+
5
+:loop
6
+	01 ADD
7
+	0f NEQ .loop JMQ
8
+	RTS
9
+
10
+:end ff BRK
0 11
\ No newline at end of file
... ...
@@ -56,26 +56,26 @@ getflag(char flag)
56 56
 }
57 57
 
58 58
 void
59
-echo(Uint8 *s, Uint8 len, char *name)
59
+echo(Stack *s, Uint8 len, char *name)
60 60
 {
61 61
 	int i;
62 62
 	printf("%s\n", name);
63 63
 	for(i = 0; i < len; ++i) {
64 64
 		if(i % 16 == 0)
65 65
 			printf("\n");
66
-		printf("%02x ", s[i]);
66
+		printf("%02x%c", s->dat[i], s->ptr == i ? '<' : ' ');
67 67
 	}
68 68
 	printf("\n\n");
69 69
 }
70 70
 
71 71
 void
72
-spush(Uint8 v)
72
+wspush(Uint8 v)
73 73
 {
74 74
 	cpu.wst.dat[cpu.wst.ptr++] = v;
75 75
 }
76 76
 
77 77
 Uint8
78
-spop(void)
78
+wspop(void)
79 79
 {
80 80
 	return cpu.wst.dat[--cpu.wst.ptr];
81 81
 }
... ...
@@ -87,7 +87,7 @@ rspush(Uint8 v)
87 87
 }
88 88
 
89 89
 Uint8
90
-rspop(void)
90
+rwspop(void)
91 91
 {
92 92
 	return cpu.rst.dat[--cpu.rst.ptr];
93 93
 }
... ...
@@ -97,29 +97,29 @@ rspop(void)
97 97
 /* clang-format off */
98 98
 
99 99
 void op_brk() { setflag(FLAG_HALT, 1); }
100
-void op_rts() {	cpu.rom.ptr = rspop(); }
100
+void op_rts() {	cpu.rom.ptr = wspop(); }
101 101
 void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
102
-void op_drp() { spop(); }
103
-void op_dup() { spush(cpu.wst.dat[cpu.wst.ptr - 1]); }
104
-void op_swp() { Uint8 b = spop(), a = spop(); spush(b); spush(a); }
105
-void op_ovr() { spush(cpu.wst.dat[cpu.wst.ptr - 2]); }
106
-void op_rot() { Uint8 c = spop(),b = spop(),a = spop(); spush(b); spush(c); spush(a); }
107
-void op_jmp() { cpu.rom.ptr = spop(); }
108
-void op_jsr() { rspush(cpu.rom.ptr); cpu.rom.ptr = spop(); }
109
-void op_jmq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
110
-void op_jsq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ rspush(cpu.rom.ptr); cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
111
-void op_equ() { setflag(FLAG_ZERO, spop() == spop()); }
112
-void op_neq() { setflag(FLAG_ZERO, spop() != spop()); }
113
-void op_lth() {	setflag(FLAG_ZERO, spop() < spop()); }
114
-void op_gth() {	setflag(FLAG_ZERO, spop() > spop()); }
115
-void op_and() {	spush(spop() & spop()); }
116
-void op_ora() {	spush(spop() | spop()); }
117
-void op_rol() { spush(spop() << 1); }
118
-void op_ror() { spush(spop() >> 1); }
119
-void op_add() { spush(spop() + spop()); }
120
-void op_sub() { spush(spop() - spop()); }
121
-void op_mul() { spush(spop() * spop()); }
122
-void op_div() { spush(spop() / spop()); }
102
+void op_drp() { wspop(); }
103
+void op_dup() { wspush(cpu.wst.dat[cpu.wst.ptr - 1]); }
104
+void op_swp() { Uint8 b = wspop(), a = wspop(); wspush(b); wspush(a); }
105
+void op_ovr() { wspush(cpu.wst.dat[cpu.wst.ptr - 2]); }
106
+void op_rot() { Uint8 c = wspop(),b = wspop(),a = wspop(); wspush(b); wspush(c); wspush(a); }
107
+void op_jmp() { cpu.rom.ptr = wspop(); }
108
+void op_jsr() { rspush(cpu.rom.ptr); cpu.rom.ptr = wspop(); }
109
+void op_jmq() { Uint8 a = wspop(); if(getflag(FLAG_ZERO)){ cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
110
+void op_jsq() { Uint8 a = wspop(); if(getflag(FLAG_ZERO)){ rspush(cpu.rom.ptr); cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
111
+void op_equ() { setflag(FLAG_ZERO, wspop() == cpu.wst.dat[cpu.wst.ptr]); }
112
+void op_neq() { setflag(FLAG_ZERO, wspop() != cpu.wst.dat[cpu.wst.ptr]); }
113
+void op_lth() {	setflag(FLAG_ZERO, wspop() < cpu.wst.dat[cpu.wst.ptr]); }
114
+void op_gth() {	setflag(FLAG_ZERO, wspop() > cpu.wst.dat[cpu.wst.ptr]); }
115
+void op_and() {	wspush(wspop() & wspop()); }
116
+void op_ora() {	wspush(wspop() | wspop()); }
117
+void op_rol() { wspush(wspop() << 1); }
118
+void op_ror() { wspush(wspop() >> 1); }
119
+void op_add() { wspush(wspop() + wspop()); }
120
+void op_sub() { wspush(wspop() - wspop()); }
121
+void op_mul() { wspush(wspop() * wspop()); }
122
+void op_div() { wspush(wspop() / wspop()); }
123 123
 
124 124
 void (*ops[])(void) = {
125 125
 	op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, 
... ...
@@ -167,7 +167,7 @@ eval()
167 167
 {
168 168
 	Uint8 instr = cpu.rom.dat[cpu.rom.ptr++];
169 169
 	if(cpu.literal > 0) {
170
-		spush(instr);
170
+		wspush(instr);
171 171
 		cpu.literal--;
172 172
 		return 1;
173 173
 	}
... ...
@@ -179,6 +179,10 @@ eval()
179 179
 	}
180 180
 	if(instr > 0x10)
181 181
 		setflag(FLAG_ZERO, 0);
182
+	if(cpu.counter == 64) {
183
+		printf("REACHED COUNTER\n");
184
+		return 0;
185
+	}
182 186
 	cpu.counter++;
183 187
 	return 1;
184 188
 }
... ...
@@ -210,6 +214,6 @@ main(int argc, char *argv[])
210 214
 	load(f);
211 215
 	run();
212 216
 	/* print result */
213
-	echo(cpu.wst.dat, 0x40, "stack");
217
+	echo(&cpu.wst, 0x40, "stack");
214 218
 	return 0;
215 219
 }