Browse code

Progress on 16b mode

neauoire authored on 07/02/2021 04:18:49
Showing 2 changed files
... ...
@@ -65,6 +65,8 @@ BRK ( RESET )
65 65
 
66 66
 ### Assembler
67 67
 
68
+- Complete implementing short mode
69
+- Implement shorthand operators
68 70
 - Catch overflow/underflow
69 71
 - Jumps should be relative
70 72
 
... ...
@@ -103,6 +103,9 @@ Uint8 wspeek8(void) { return cpu.wst.dat[cpu.wst.ptr - 1]; }
103 103
 Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
104 104
 void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; }
105 105
 
106
+/* new flexy pop/push */
107
+
108
+
106 109
 void op_brk() { setflag(FLAG_HALT, 1); }
107 110
 void op_rts() {	cpu.rom.ptr = rspop16(); }
108 111
 void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
... ...
@@ -123,28 +126,35 @@ void op_and() { wspush8(wspop8() & wspop8()); }
123 126
 void op_ora() { wspush8(wspop8() | wspop8()); }
124 127
 void op_rol() { wspush8(wspop8() << 1); }
125 128
 void op_ror() { wspush8(wspop8() >> 1); }
126
-void op_add() { 
127
-	if(getflag(FLAG_SHORT))
128
-		wspush16(wspop16() + wspop16()); 
129
-	else
130
-		wspush8(wspop8() + wspop8()); 
131
-}
132
-void op_sub() { wspush8(wspop8() - wspop8()); }
133
-void op_mul() { wspush8(wspop8() * wspop8()); }
134
-void op_div() { wspush8(wspop8() / wspop8()); }
129
+void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(a + b); }
130
+void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(a - b); }
131
+void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(a * b); }
132
+void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(a / b); }
135 133
 void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); }
136 134
 void op_str() { cpu.ram.dat[wspop16()] = wspop8(); }
137 135
 void op_pek() { wspush8(cpu.rom.dat[wspop16()]); }
138 136
 void op_pok() { printf("TODO:\n");}
139 137
 
140
-void (*ops[])() = {
138
+void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a + b); }
139
+void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a - b); }
140
+void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a * b); }
141
+void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a / b); }
142
+
143
+void (*ops8[])() = {
141 144
 	op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, 
142 145
 	op_jmu, op_jsu, op_jmc, op_jsc, op_equ, op_neq, op_gth, op_lth, 
143 146
 	op_and, op_ora, op_rol, op_ror, op_add, op_sub, op_mul, op_div,
144 147
 	op_ldr, op_str, op_pek, op_pok, op_brk, op_brk, op_brk, op_brk
145 148
 };
146 149
 
147
-Uint8 opr[][2] = {
150
+void (*ops16[])() = {
151
+	op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, 
152
+	op_jmu, op_jsu, op_jmc, op_jsc, op_equ, op_neq, op_gth, op_lth, 
153
+	op_and, op_ora, op_rol, op_ror, op_add16, op_sub16, op_mul16, op_div16,
154
+	op_ldr, op_str, op_pek, op_pok, op_brk, op_brk, op_brk, op_brk
155
+};
156
+
157
+Uint8 opr[][2] = { /* todo: 16 bits mode */
148 158
 	{0,0}, {0,0}, {0,0}, {1,0}, {0,1}, {1,1}, {0,1}, {3,3},
149 159
 	{2,0}, {2,0}, {2,0}, {2,0}, {2,1}, {2,1}, {2,1}, {2,1},
150 160
 	{1,0}, {1,0}, {1,0}, {1,0}, {2,1}, {0,0}, {0,0}, {0,0},
... ...
@@ -156,17 +166,10 @@ Uint8 opr[][2] = {
156 166
 void
157 167
 reset(void)
158 168
 {
159
-	int i;
160
-	cpu.status = 0x00;
161
-	cpu.counter = 0x00;
162
-	cpu.literal = 0x00;
163
-	cpu.rom.ptr = 0x00;
164
-	cpu.wst.ptr = 0x00;
165
-	cpu.rst.ptr = 0x00;
166
-	for(i = 0; i < 256; i++) {
167
-		cpu.wst.dat[i] = 0x00;
168
-		cpu.rst.dat[i] = 0x00;
169
-	}
169
+	size_t i;
170
+	char *cptr = (char *)&cpu;
171
+	for(i = 0; i < sizeof cpu; i++)
172
+		cptr[i] = 0;
170 173
 }
171 174
 
172 175
 int
... ...
@@ -186,24 +189,19 @@ device1(Uint8 *read, Uint8 *write)
186 189
 }
187 190
 
188 191
 void
189
-opc(Uint8 src, Uint8 *op, Uint8 *mode)
192
+opc(Uint8 src, Uint8 *op)
190 193
 {
191 194
 	*op = src;
192 195
 	*op &= ~(1 << 5);
193 196
 	*op &= ~(1 << 6);
194 197
 	*op &= ~(1 << 7);
195
-	*mode = src;
196
-	*mode &= ~(1 << 0);
197
-	*mode &= ~(1 << 1);
198
-	*mode &= ~(1 << 2);
199
-	*mode &= ~(1 << 3);
200 198
 }
201 199
 
202 200
 int
203 201
 eval(void)
204 202
 {
205 203
 	Uint8 instr = cpu.rom.dat[cpu.rom.ptr++];
206
-	Uint8 op, opmode;
204
+	Uint8 op;
207 205
 	/* when literal */
208 206
 	if(cpu.literal > 0) {
209 207
 		wspush8(instr);
... ...
@@ -211,7 +209,7 @@ eval(void)
211 209
 		return 1;
212 210
 	}
213 211
 	/* when opcode */
214
-	opc(instr, &op, &opmode);
212
+	opc(instr, &op);
215 213
 	setflag(FLAG_SHORT, (instr >> 5) & 1);
216 214
 	if((instr >> 6) & 1)
217 215
 		printf("Unused flag: %02x\n", instr);
... ...
@@ -221,7 +219,10 @@ eval(void)
221 219
 	/* TODO: setflag(FLAG_C, (instr >> 7) & 1); */
222 220
 	if(cpu.wst.ptr < opr[op][0])
223 221
 		return error("Stack underflow", op);
224
-	(*ops[op])();
222
+	if(getflag(FLAG_SHORT))
223
+		(*ops16[op])();
224
+	else
225
+		(*ops8[op])();
225 226
 	cpu.counter++;
226 227
 	return 1;
227 228
 }