| ... | ... |
@@ -13,7 +13,7 @@ WITH REGARD TO THIS SOFTWARE. |
| 13 | 13 |
|
| 14 | 14 |
#define FLAG_HALT 0x01 |
| 15 | 15 |
#define FLAG_SHORT 0x02 |
| 16 |
-#define FLAG_CARRY 0x04 |
|
| 16 |
+#define FLAG_SIGN 0x04 |
|
| 17 | 17 |
#define FLAG_TRAPS 0x08 |
| 18 | 18 |
|
| 19 | 19 |
typedef unsigned char Uint8; |
| ... | ... |
@@ -99,33 +99,36 @@ void wspush8(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; }
|
| 99 | 99 |
void wspush16(Uint16 s) { wspush8(s >> 8); wspush8(s & 0xff); }
|
| 100 | 100 |
Uint8 wspop8(void) { return cpu.wst.dat[--cpu.wst.ptr]; }
|
| 101 | 101 |
Uint16 wspop16(void) { return wspop8() + (wspop8() << 8); }
|
| 102 |
-Uint8 wspeek8(void) { return cpu.wst.dat[cpu.wst.ptr - 1]; }
|
|
| 102 |
+Uint8 wspeek8(Uint8 o) { return cpu.wst.dat[cpu.wst.ptr - o]; }
|
|
| 103 |
+Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.wst.dat[cpu.wst.ptr - o + 1]); }
|
|
| 103 | 104 |
Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
|
| 104 | 105 |
void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; }
|
| 105 | 106 |
|
| 106 | 107 |
/* new flexy pop/push */ |
| 107 | 108 |
|
| 108 |
- |
|
| 109 | 109 |
void op_brk() { setflag(FLAG_HALT, 1); }
|
| 110 | 110 |
void op_rts() { cpu.rom.ptr = rspop16(); }
|
| 111 | 111 |
void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
|
| 112 |
+ |
|
| 112 | 113 |
void op_drp() { wspop8(); }
|
| 113 |
-void op_dup() { wspush8(wspeek8()); }
|
|
| 114 |
+void op_dup() { wspush8(wspeek8(1)); }
|
|
| 114 | 115 |
void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); }
|
| 115 |
-void op_ovr() { wspush8(cpu.wst.dat[cpu.wst.ptr - 2]); }
|
|
| 116 |
+void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); }
|
|
| 116 | 117 |
void op_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); }
|
| 117 |
-void op_jmu() { cpu.rom.ptr = wspop8(); }
|
|
| 118 |
+ |
|
| 119 |
+void op_jmu() { cpu.rom.ptr = wspop16(); }
|
|
| 118 | 120 |
void op_jsu() { rspush16(cpu.rom.ptr); cpu.rom.ptr = wspop16(); }
|
| 119 |
-void op_jmc() { if(wspop8()) op_jmu(); }
|
|
| 120 |
-void op_jsc() { if(wspop8()) op_jsu(); }
|
|
| 121 |
-void op_equ() { wspush8(wspop8() == wspop8()); }
|
|
| 122 |
-void op_neq() { wspush8(wspop8() != wspop8()); }
|
|
| 123 |
-void op_gth() { wspush8(wspop8() < wspop8()); }
|
|
| 124 |
-void op_lth() { wspush8(wspop8() > wspop8()); }
|
|
| 125 |
-void op_and() { wspush8(wspop8() & wspop8()); }
|
|
| 126 |
-void op_ora() { wspush8(wspop8() | wspop8()); }
|
|
| 127 |
-void op_rol() { wspush8(wspop8() << 1); }
|
|
| 128 |
-void op_ror() { wspush8(wspop8() >> 1); }
|
|
| 121 |
+void op_jmc() { Uint8 a = wspop8(); if(a) op_jmu(); }
|
|
| 122 |
+void op_jsc() { Uint8 a = wspop8(); if(a) op_jsu(); }
|
|
| 123 |
+ |
|
| 124 |
+void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(a == b); }
|
|
| 125 |
+void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(a != b); }
|
|
| 126 |
+void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a < b); }
|
|
| 127 |
+void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a > b); }
|
|
| 128 |
+void op_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); }
|
|
| 129 |
+void op_ora() { Uint8 a = wspop8(), b = wspop8(); wspush8(a | b); }
|
|
| 130 |
+void op_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); }
|
|
| 131 |
+void op_ror() { Uint8 a = wspop8(), b = wspop8(); wspush8(a >> b); }
|
|
| 129 | 132 |
void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(a + b); }
|
| 130 | 133 |
void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(a - b); }
|
| 131 | 134 |
void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(a * b); }
|
| ... | ... |
@@ -135,6 +138,20 @@ void op_str() { cpu.ram.dat[wspop16()] = wspop8(); }
|
| 135 | 138 |
void op_pek() { wspush8(cpu.rom.dat[wspop16()]); }
|
| 136 | 139 |
void op_pok() { printf("TODO:\n");}
|
| 137 | 140 |
|
| 141 |
+void op_drp16() { wspop16(); }
|
|
| 142 |
+void op_dup16() { wspush16(wspeek16(2)); }
|
|
| 143 |
+void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); }
|
|
| 144 |
+void op_ovr16() { Uint16 a = wspeek16(4); wspush16(a); }
|
|
| 145 |
+void op_rot16() { Uint16 c = wspop16(), b = wspop16(), a = wspop16(); wspush16(b); wspush16(c); wspush16(a); }
|
|
| 146 |
+ |
|
| 147 |
+void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a == b); }
|
|
| 148 |
+void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a != b); }
|
|
| 149 |
+void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a < b); }
|
|
| 150 |
+void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a > b); }
|
|
| 151 |
+void op_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); }
|
|
| 152 |
+void op_ora16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a | b); }
|
|
| 153 |
+void op_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); }
|
|
| 154 |
+void op_ror16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a >> b); }
|
|
| 138 | 155 |
void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a + b); }
|
| 139 | 156 |
void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a - b); }
|
| 140 | 157 |
void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a * b); }
|
| ... | ... |
@@ -148,9 +165,9 @@ void (*ops8[])() = {
|
| 148 | 165 |
}; |
| 149 | 166 |
|
| 150 | 167 |
void (*ops16[])() = {
|
| 151 |
- op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, |
|
| 168 |
+ op_brk, op_rts, op_lit, op_drp16, op_dup16, op_swp16, op_ovr16, op_rot16, |
|
| 152 | 169 |
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, |
|
| 170 |
+ op_and16, op_ora16, op_rol16, op_ror16, op_add16, op_sub16, op_mul16, op_div16, |
|
| 154 | 171 |
op_ldr, op_str, op_pek, op_pok, op_brk, op_brk, op_brk, op_brk |
| 155 | 172 |
}; |
| 156 | 173 |
|
| ... | ... |
@@ -211,8 +228,7 @@ eval(void) |
| 211 | 228 |
/* when opcode */ |
| 212 | 229 |
opc(instr, &op); |
| 213 | 230 |
setflag(FLAG_SHORT, (instr >> 5) & 1); |
| 214 |
- if((instr >> 6) & 1) |
|
| 215 |
- printf("Unused flag: %02x\n", instr);
|
|
| 231 |
+ setflag(FLAG_SIGN, (instr >> 6) & 1); |
|
| 216 | 232 |
if((instr >> 7) & 1) |
| 217 | 233 |
printf("Unused flag: %02x\n", instr);
|
| 218 | 234 |
/* TODO: setflag(FLAG_B, (instr >> 6) & 1); */ |
| ... | ... |
@@ -256,7 +272,7 @@ start(FILE *f) |
| 256 | 272 |
printf("hf: %x zf: %x cf: %x tf: %x\n",
|
| 257 | 273 |
getflag(FLAG_HALT) != 0, |
| 258 | 274 |
getflag(FLAG_SHORT) != 0, |
| 259 |
- getflag(FLAG_CARRY) != 0, |
|
| 275 |
+ getflag(FLAG_SIGN) != 0, |
|
| 260 | 276 |
getflag(FLAG_TRAPS) != 0); |
| 261 | 277 |
printf("\n");
|
| 262 | 278 |
return 1; |
| ... | ... |
@@ -174,8 +174,8 @@ findop(char *s) |
| 174 | 174 |
while(s[3 + m]) {
|
| 175 | 175 |
char c = s[3 + m]; |
| 176 | 176 |
if(c == '^') i |= (1 << 5); /* mode: 16 bits */ |
| 177 |
- if(c == '&') i |= (1 << 6); /* mode: unused */ |
|
| 178 |
- if(c == '~') i |= (1 << 7); /* mode: unused */ |
|
| 177 |
+ if(c == '~') i |= (1 << 6); /* mode: signed */ |
|
| 178 |
+ if(c == '&') i |= (1 << 7); /* mode: unused */ |
|
| 179 | 179 |
m++; |
| 180 | 180 |
} |
| 181 | 181 |
return i; |