Browse code

Removed global

neauoire authored on 08/02/2021 21:56:56
Showing 3 changed files
... ...
@@ -18,23 +18,23 @@ WITH REGARD TO THIS SOFTWARE.
18 18
 #define FLAG_SIGN 0x04
19 19
 #define FLAG_COND 0x08
20 20
 
21
-Computer cpu;
21
+Cpu cpu;
22 22
 
23 23
 #pragma mark - Helpers
24 24
 
25 25
 void
26
-setflag(char flag, int b)
26
+setflag(Cpu *c, char flag, int b)
27 27
 {
28 28
 	if(b)
29
-		cpu.status |= flag;
29
+		c->status |= flag;
30 30
 	else
31
-		cpu.status &= (~flag);
31
+		c->status &= (~flag);
32 32
 }
33 33
 
34 34
 int
35
-getflag(char flag)
35
+getflag(Cpu *c, char flag)
36 36
 {
37
-	return cpu.status & flag;
37
+	return c->status & flag;
38 38
 }
39 39
 
40 40
 void
... ...
@@ -68,64 +68,64 @@ echom(Memory *m, Uint8 len, char *name)
68 68
 /* clang-format off */
69 69
 
70 70
 Uint16 bytes2short(Uint8 a, Uint8 b) { return (a << 8) + b; }
71
-Uint16 mempeek16(Uint16 s) { return (cpu.ram.dat[s] << 8) + (cpu.ram.dat[s+1] & 0xff); }
72
-void wspush8(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; }
73
-void wspush16(Uint16 s) { wspush8(s >> 8); wspush8(s & 0xff); }
74
-Uint8 wspop8(void) { return cpu.wst.dat[--cpu.wst.ptr]; }
75
-Uint16 wspop16(void) { return wspop8() + (wspop8() << 8); }
76
-Uint8 wspeek8(Uint8 o) { return cpu.wst.dat[cpu.wst.ptr - o]; }
77
-Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.wst.dat[cpu.wst.ptr - o + 1]); }
78
-Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
79
-void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; }
71
+Uint16 mempeek16(Cpu *c, Uint16 s) { return (c->ram.dat[s] << 8) + (c->ram.dat[s+1] & 0xff); }
72
+void wspush8(Cpu *c, Uint8 b) { c->wst.dat[c->wst.ptr++] = b; }
73
+void wspush16(Cpu *c, Uint16 s) { wspush8(c,s >> 8); wspush8(c,s & 0xff); }
74
+Uint8 wspop8(Cpu *c) { return c->wst.dat[--c->wst.ptr]; }
75
+Uint16 wspop16(Cpu *c) { return wspop8(c) + (wspop8(c) << 8); }
76
+Uint8 wspeek8(Cpu *c, Uint8 o) { return c->wst.dat[c->wst.ptr - o]; }
77
+Uint16 wspeek16(Cpu *c, Uint8 o) { return bytes2short(c->wst.dat[c->wst.ptr - o], c->wst.dat[c->wst.ptr - o + 1]); }
78
+Uint16 rspop16(Cpu *c) { return c->rst.dat[--c->rst.ptr]; }
79
+void rspush16(Cpu *c, Uint16 a) { c->rst.dat[c->rst.ptr++] = a; }
80 80
 
81 81
 /* I/O */
82
-void op_brk() { setflag(FLAG_HALT, 1); }
83
-void op_lit() { cpu.literal += cpu.ram.dat[cpu.ram.ptr++]; }
84
-void op_nop() { printf("NOP");}
85
-void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); }
86
-void op_str() { cpu.ram.dat[wspop16()] = wspop8(); }
82
+void op_brk(Cpu *c) { setflag(c,FLAG_HALT, 1); }
83
+void op_lit(Cpu *c) { c->literal += c->ram.dat[c->ram.ptr++]; }
84
+void op_nop(Cpu *c) { printf("NOP");}
85
+void op_ldr(Cpu *c) { wspush8(c,c->ram.dat[wspop16(c)]); }
86
+void op_str(Cpu *c) { c->ram.dat[wspop16(c)] = wspop8(c); }
87 87
 /* Logic */
88
-void op_jmp() { cpu.ram.ptr = wspop16(); }
89
-void op_jsr() { rspush16(cpu.ram.ptr); cpu.ram.ptr = wspop16(); }
90
-void op_rts() {	cpu.ram.ptr = rspop16(); }
88
+void op_jmp(Cpu *c) { c->ram.ptr = wspop16(c); }
89
+void op_jsr(Cpu *c) { rspush16(c,c->ram.ptr); c->ram.ptr = wspop16(c); }
90
+void op_rts(Cpu *c) {	c->ram.ptr = rspop16(c); }
91 91
 /* Stack */
92
-void op_pop() { wspop8(); }
93
-void op_dup() { wspush8(wspeek8(1)); }
94
-void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); }
95
-void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); }
96
-void op_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); }
97
-void op_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); }
98
-void op_ora() { Uint8 a = wspop8(), b = wspop8(); wspush8(a | b); }
99
-void op_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); }
92
+void op_pop(Cpu *c) { wspop8(c); }
93
+void op_dup(Cpu *c) { wspush8(c,wspeek8(c,1)); }
94
+void op_swp(Cpu *c) { Uint8 b = wspop8(c), a = wspop8(c); wspush8(c,b); wspush8(c,a); }
95
+void op_ovr(Cpu *c) { Uint8 a = wspeek8(c,2); wspush8(c,a); }
96
+void op_rot(Cpu *c) { Uint8 c1 = wspop8(c),b = wspop8(c),a = wspop8(c); wspush8(c,b); wspush8(c,c1); wspush8(c,a); }
97
+void op_and(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a & b); }
98
+void op_ora(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a | b); }
99
+void op_rol(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,a << b); }
100 100
 /* Arithmetic */
101
-void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(b + a); }
102
-void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(b - a); }
103
-void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(b * a); }
104
-void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(b / a); }
105
-void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(b == a); }
106
-void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(b != a); }
107
-void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b > a); }
108
-void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b < a); }
101
+void op_add(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b + a); }
102
+void op_sub(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b - a); }
103
+void op_mul(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b * a); }
104
+void op_div(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b / a); }
105
+void op_equ(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b == a); }
106
+void op_neq(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b != a); }
107
+void op_gth(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b > a); }
108
+void op_lth(Cpu *c) { Uint8 a = wspop8(c), b = wspop8(c); wspush8(c,b < a); }
109 109
 /* Stack(16-bits) */
110
-void op_pop16() { wspop16(); }
111
-void op_dup16() { wspush16(wspeek16(2)); }
112
-void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); }
113
-void op_ovr16() { Uint16 a = wspeek16(4); wspush16(a); }
114
-void op_rot16() { Uint16 c = wspop16(), b = wspop16(), a = wspop16(); wspush16(b); wspush16(c); wspush16(a); }
115
-void op_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); }
116
-void op_ora16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a | b); }
117
-void op_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); }
110
+void op_pop16(Cpu *c) { wspop16(c); }
111
+void op_dup16(Cpu *c) { wspush16(c,wspeek16(c,2)); }
112
+void op_swp16(Cpu *c) { Uint16 b = wspop16(c), a = wspop16(c); wspush16(c,b); wspush16(c,a); }
113
+void op_ovr16(Cpu *c) { Uint16 a = wspeek16(c, 4); wspush16(c,a); }
114
+void op_rot16(Cpu *c) { Uint16 c1 = wspop16(c), b = wspop16(c), a = wspop16(c); wspush16(c,b); wspush16(c,c1); wspush16(c,a); }
115
+void op_and16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a & b); }
116
+void op_ora16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a | b); }
117
+void op_rol16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,a << b); }
118 118
 /* Arithmetic(16-bits) */
119
-void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b + a); }
120
-void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b - a); }
121
-void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b * a); }
122
-void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b / a); }
123
-void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b == a); }
124
-void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b != a); }
125
-void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b > a); }
126
-void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b < a); }
127
-
128
-void (*ops[])() = {
119
+void op_add16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b + a); }
120
+void op_sub16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b - a); }
121
+void op_mul16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b * a); }
122
+void op_div16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush16(c,b / a); }
123
+void op_equ16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b == a); }
124
+void op_neq16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b != a); }
125
+void op_gth16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b > a); }
126
+void op_lth16(Cpu *c) { Uint16 a = wspop16(c), b = wspop16(c); wspush8(c,b < a); }
127
+
128
+void (*ops[])(Cpu *c) = {
129 129
 	op_brk, op_lit, op_nop, op_nop, op_nop, op_nop, op_ldr, op_str, 
130 130
 	op_jmp, op_jsr, op_nop, op_rts, op_nop, op_nop, op_nop, op_nop, 
131 131
 	op_pop, op_dup, op_swp, op_ovr, op_rot, op_and, op_ora, op_rol,
... ...
@@ -146,95 +146,105 @@ Uint8 opr[][2] = {
146 146
 /* clang-format on */
147 147
 
148 148
 int
149
-error(char *name, int id)
149
+error(Cpu *c, char *name, int id)
150 150
 {
151
-	printf("Error: %s[%04x], at 0x%04x\n", name, id, cpu.counter);
151
+	printf("Error: %s[%04x], at 0x%04x\n", name, id, c->counter);
152 152
 	return 0;
153 153
 }
154 154
 
155 155
 int
156
-doliteral(Uint8 instr)
156
+doliteral(Cpu *c, Uint8 instr)
157 157
 {
158
-	if(cpu.wst.ptr >= 255)
159
-		return error("Stack overflow", instr);
160
-	wspush8(instr);
161
-	cpu.literal--;
158
+	if(c->wst.ptr >= 255)
159
+		return error(c, "Stack overflow", instr);
160
+	wspush8(c, instr);
161
+	c->literal--;
162 162
 	return 1;
163 163
 }
164 164
 
165 165
 int
166
-dodevices(void) /* experimental */
166
+dodevices(Cpu *c) /* experimental */
167 167
 {
168
-	if(cpu.ram.dat[0xfff1]) {
169
-		printf("%c", cpu.ram.dat[0xfff1]);
170
-		cpu.ram.dat[0xfff1] = 0x00;
168
+	if(c->ram.dat[0xfff1]) {
169
+		printf("%c", c->ram.dat[0xfff1]);
170
+		c->ram.dat[0xfff1] = 0x00;
171 171
 	}
172 172
 	return 1;
173 173
 }
174 174
 
175 175
 int
176
-doopcode(Uint8 instr)
176
+doopcode(Cpu *c, Uint8 instr)
177 177
 {
178 178
 	Uint8 op = instr & 0x1f;
179
-	setflag(FLAG_SHORT, (instr >> 5) & 1);
180
-	setflag(FLAG_SIGN, (instr >> 6) & 1); /* usused */
181
-	setflag(FLAG_COND, (instr >> 7) & 1);
182
-	if(getflag(FLAG_SHORT))
179
+	setflag(c, FLAG_SHORT, (instr >> 5) & 1);
180
+	setflag(c, FLAG_SIGN, (instr >> 6) & 1); /* usused */
181
+	setflag(c, FLAG_COND, (instr >> 7) & 1);
182
+	if(getflag(c, FLAG_SHORT))
183 183
 		op += 16;
184
-	if(cpu.wst.ptr < opr[op][0])
185
-		return error("Stack underflow", op);
186
-	if(cpu.wst.ptr + opr[op][1] - opr[op][0] >= 255)
187
-		return error("Stack overflow", instr);
188
-	if(!getflag(FLAG_COND) || (getflag(FLAG_COND) && wspop8()))
189
-		(*ops[op])();
190
-	dodevices();
184
+	if(c->wst.ptr < opr[op][0])
185
+		return error(c, "Stack underflow", op);
186
+	if(c->wst.ptr + opr[op][1] - opr[op][0] >= 255)
187
+		return error(c, "Stack overflow", instr);
188
+	if(!getflag(c, FLAG_COND) || (getflag(c, FLAG_COND) && wspop8(c)))
189
+		(*ops[op])(c);
190
+	dodevices(c);
191 191
 	return 1;
192 192
 }
193 193
 
194 194
 int
195
-eval(void)
195
+eval(Cpu *c)
196 196
 {
197
-	Uint8 instr = cpu.ram.dat[cpu.ram.ptr++];
198
-	if(cpu.literal > 0)
199
-		return doliteral(instr);
197
+	Uint8 instr = c->ram.dat[c->ram.ptr++];
198
+	if(c->literal > 0)
199
+		return doliteral(c, instr);
200 200
 	else
201
-		return doopcode(instr);
201
+		return doopcode(c, instr);
202 202
 	return 1;
203 203
 }
204 204
 
205 205
 int
206
-load(FILE *f)
206
+load(Cpu *c, FILE *f)
207 207
 {
208
-	fread(cpu.ram.dat, sizeof(cpu.ram.dat), 1, f);
208
+	fread(c->ram.dat, sizeof(c->ram.dat), 1, f);
209 209
 	return 1;
210 210
 }
211 211
 
212 212
 void
213
-echof(void)
213
+echof(Cpu *c)
214 214
 {
215 215
 	printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n",
216
-		cpu.counter,
217
-		getflag(FLAG_HALT) != 0,
218
-		getflag(FLAG_SHORT) != 0,
219
-		getflag(FLAG_SIGN) != 0,
220
-		getflag(FLAG_COND) != 0);
216
+		c->counter,
217
+		getflag(c, FLAG_HALT) != 0,
218
+		getflag(c, FLAG_SHORT) != 0,
219
+		getflag(c, FLAG_SIGN) != 0,
220
+		getflag(c, FLAG_COND) != 0);
221
+}
222
+
223
+void
224
+reset(Cpu *c)
225
+{
226
+	size_t i;
227
+	char *cptr = (char *)c;
228
+	for(i = 0; i < sizeof c; i++)
229
+		cptr[i] = 0;
221 230
 }
222 231
 
223 232
 int
224
-boot(void)
233
+boot(Cpu *c)
225 234
 {
226
-	cpu.vreset = mempeek16(0xfffa);
227
-	cpu.vframe = mempeek16(0xfffc);
228
-	cpu.verror = mempeek16(0xfffe);
235
+	reset(c);
236
+	c->vreset = mempeek16(c, 0xfffa);
237
+	c->vframe = mempeek16(c, 0xfffc);
238
+	c->verror = mempeek16(c, 0xfffe);
229 239
 	/* eval reset */
230
-	cpu.ram.ptr = cpu.vreset;
231
-	setflag(FLAG_HALT, 0);
232
-	while(!(cpu.status & FLAG_HALT) && eval())
233
-		cpu.counter++;
240
+	c->ram.ptr = c->vreset;
241
+	setflag(c, FLAG_HALT, 0);
242
+	while(!(c->status & FLAG_HALT) && eval(c))
243
+		c->counter++;
234 244
 	/*eval frame */
235
-	cpu.ram.ptr = cpu.vframe;
236
-	setflag(FLAG_HALT, 0);
237
-	while(!(cpu.status & FLAG_HALT) && eval())
238
-		cpu.counter++;
245
+	c->ram.ptr = c->vframe;
246
+	setflag(c, FLAG_HALT, 0);
247
+	while(!(c->status & FLAG_HALT) && eval(c))
248
+		c->counter++;
239 249
 	return 1;
240 250
 }
... ...
@@ -35,11 +35,11 @@ typedef struct {
35 35
 	Stack8 wst;
36 36
 	Stack16 rst;
37 37
 	Memory ram;
38
-} Computer;
38
+} Cpu;
39 39
 
40
-int error(char *name, int id);
41
-int load(FILE *f);
42
-int boot(void);
43
-void echof(void);
40
+int error(Cpu *c, char *name, int id);
41
+int load(Cpu *c, FILE *f);
42
+int boot(Cpu *c);
43
+void echof(Cpu *c);
44 44
 void echom(Memory *m, Uint8 len, char *name);
45 45
 void echos(Stack8 *s, Uint8 len, char *name);
... ...
@@ -16,18 +16,18 @@ int
16 16
 main(int argc, char *argv[])
17 17
 {
18 18
 	FILE *f;
19
-	Computer cpu;
19
+	Cpu cpu;
20 20
 	if(argc < 2)
21
-		return error("No input.", 0);
21
+		return error(&cpu, "No input.", 0);
22 22
 	if(!(f = fopen(argv[1], "rb")))
23
-		return error("Missing input.", 0);
24
-	if(!load(f))
25
-		return error("Load error", 0);
26
-	if(!boot())
27
-		return error("Boot error", 0);
23
+		return error(&cpu, "Missing input.", 0);
24
+	if(!load(&cpu, f))
25
+		return error(&cpu, "Load error", 0);
26
+	if(!boot(&cpu))
27
+		return error(&cpu, "Boot error", 0);
28 28
 	/* print result */
29 29
 	echos(&cpu.wst, 0x40, "stack");
30 30
 	echom(&cpu.ram, 0x40, "ram");
31
-	echof();
31
+	echof(&cpu);
32 32
 	return 0;
33 33
 }