diff --git a/README.md b/README.md index daecb10..31a4aa4 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ BRK ( RESET ) - Implement shorthand operators - Catch overflow/underflow - Jumps should be relative +- Load program in RAM +- Signed operations ### CPU diff --git a/uxn.c b/uxn.c index a3827ba..1e09c34 100644 --- a/uxn.c +++ b/uxn.c @@ -13,7 +13,7 @@ WITH REGARD TO THIS SOFTWARE. #define FLAG_HALT 0x01 #define FLAG_SHORT 0x02 -#define FLAG_CARRY 0x04 +#define FLAG_SIGN 0x04 #define FLAG_TRAPS 0x08 typedef unsigned char Uint8; @@ -99,33 +99,36 @@ void wspush8(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; } void wspush16(Uint16 s) { wspush8(s >> 8); wspush8(s & 0xff); } Uint8 wspop8(void) { return cpu.wst.dat[--cpu.wst.ptr]; } Uint16 wspop16(void) { return wspop8() + (wspop8() << 8); } -Uint8 wspeek8(void) { return cpu.wst.dat[cpu.wst.ptr - 1]; } +Uint8 wspeek8(Uint8 o) { return cpu.wst.dat[cpu.wst.ptr - o]; } +Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.wst.dat[cpu.wst.ptr - o + 1]); } Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; } void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; } /* new flexy pop/push */ - void op_brk() { setflag(FLAG_HALT, 1); } void op_rts() { cpu.rom.ptr = rspop16(); } void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; } + void op_drp() { wspop8(); } -void op_dup() { wspush8(wspeek8()); } +void op_dup() { wspush8(wspeek8(1)); } void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); } -void op_ovr() { wspush8(cpu.wst.dat[cpu.wst.ptr - 2]); } +void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); } void op_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); } -void op_jmu() { cpu.rom.ptr = wspop8(); } + +void op_jmu() { cpu.rom.ptr = wspop16(); } void op_jsu() { rspush16(cpu.rom.ptr); cpu.rom.ptr = wspop16(); } -void op_jmc() { if(wspop8()) op_jmu(); } -void op_jsc() { if(wspop8()) op_jsu(); } -void op_equ() { wspush8(wspop8() == wspop8()); } -void op_neq() { wspush8(wspop8() != wspop8()); } -void op_gth() { wspush8(wspop8() < wspop8()); } -void op_lth() { wspush8(wspop8() > wspop8()); } -void op_and() { wspush8(wspop8() & wspop8()); } -void op_ora() { wspush8(wspop8() | wspop8()); } -void op_rol() { wspush8(wspop8() << 1); } -void op_ror() { wspush8(wspop8() >> 1); } +void op_jmc() { Uint8 a = wspop8(); if(a) op_jmu(); } +void op_jsc() { Uint8 a = wspop8(); if(a) op_jsu(); } + +void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(a == b); } +void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(a != b); } +void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a < b); } +void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a > b); } +void op_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); } +void op_ora() { Uint8 a = wspop8(), b = wspop8(); wspush8(a | b); } +void op_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); } +void op_ror() { Uint8 a = wspop8(), b = wspop8(); wspush8(a >> b); } void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(a + b); } void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(a - b); } void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(a * b); } @@ -135,6 +138,20 @@ void op_str() { cpu.ram.dat[wspop16()] = wspop8(); } void op_pek() { wspush8(cpu.rom.dat[wspop16()]); } void op_pok() { printf("TODO:\n");} +void op_drp16() { wspop16(); } +void op_dup16() { wspush16(wspeek16(2)); } +void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); } +void op_ovr16() { Uint16 a = wspeek16(4); wspush16(a); } +void op_rot16() { Uint16 c = wspop16(), b = wspop16(), a = wspop16(); wspush16(b); wspush16(c); wspush16(a); } + +void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a == b); } +void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a != b); } +void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a < b); } +void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a > b); } +void op_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); } +void op_ora16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a | b); } +void op_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); } +void op_ror16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a >> b); } void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a + b); } void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a - b); } void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a * b); } @@ -148,9 +165,9 @@ void (*ops8[])() = { }; void (*ops16[])() = { - op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, + op_brk, op_rts, op_lit, op_drp16, op_dup16, op_swp16, op_ovr16, op_rot16, op_jmu, op_jsu, op_jmc, op_jsc, op_equ, op_neq, op_gth, op_lth, - op_and, op_ora, op_rol, op_ror, op_add16, op_sub16, op_mul16, op_div16, + op_and16, op_ora16, op_rol16, op_ror16, op_add16, op_sub16, op_mul16, op_div16, op_ldr, op_str, op_pek, op_pok, op_brk, op_brk, op_brk, op_brk }; @@ -211,8 +228,7 @@ eval(void) /* when opcode */ opc(instr, &op); setflag(FLAG_SHORT, (instr >> 5) & 1); - if((instr >> 6) & 1) - printf("Unused flag: %02x\n", instr); + setflag(FLAG_SIGN, (instr >> 6) & 1); if((instr >> 7) & 1) printf("Unused flag: %02x\n", instr); /* TODO: setflag(FLAG_B, (instr >> 6) & 1); */ @@ -256,7 +272,7 @@ start(FILE *f) printf("hf: %x zf: %x cf: %x tf: %x\n", getflag(FLAG_HALT) != 0, getflag(FLAG_SHORT) != 0, - getflag(FLAG_CARRY) != 0, + getflag(FLAG_SIGN) != 0, getflag(FLAG_TRAPS) != 0); printf("\n"); return 1; diff --git a/uxnasm.c b/uxnasm.c index a95331e..b21b021 100644 --- a/uxnasm.c +++ b/uxnasm.c @@ -174,8 +174,8 @@ findop(char *s) while(s[3 + m]) { char c = s[3 + m]; if(c == '^') i |= (1 << 5); /* mode: 16 bits */ - if(c == '&') i |= (1 << 6); /* mode: unused */ - if(c == '~') i |= (1 << 7); /* mode: unused */ + if(c == '~') i |= (1 << 6); /* mode: signed */ + if(c == '&') i |= (1 << 7); /* mode: unused */ m++; } return i;