From 882ae2a693a50cdfa072462be2d57b93078cf916 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 18 Aug 2021 09:42:00 +0200 Subject: Update UXN core with INC/DEC changes --- src/uxn.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 144 insertions(+), 30 deletions(-) (limited to 'src/uxn.c') diff --git a/src/uxn.c b/src/uxn.c index 3a8cdb3..3934e1b 100644 --- a/src/uxn.c +++ b/src/uxn.c @@ -30,35 +30,21 @@ See etc/mkuxn-fast.moon for instructions. IWRAM_CODE int -evaluxn(Uxn *u, u16 vec) +uxn_eval(Uxn *u, u16 vec) { u8 instr; - if(u->dev[0].dat[0xf]) + if(!vec || u->dev[0].dat[0xf]) return 0; u->ram.ptr = vec; if(u->wst.ptr > 0xf8) u->wst.ptr = 0xf8; - while(u->ram.ptr) { - instr = u->ram.dat[u->ram.ptr++]; + while((instr = u->ram.dat[u->ram.ptr++])) { switch(instr) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-value" #pragma GCC diagnostic ignored "-Wunused-variable" - case 0x00: /* BRK */ - case 0x20: /* BRK2 */ - case 0x40: /* BRKr */ - case 0x60: /* BRK2r */ - case 0x80: /* BRKk */ - case 0xa0: /* BRK2k */ - case 0xc0: /* BRKkr */ - case 0xe0: /* BRK2kr */ - __asm__("evaluxn_00_BRK:"); - { - u->ram.ptr = 0; - } - break; - case 0x01: /* LIT */ - case 0x81: /* LITk */ - __asm__("evaluxn_01_LIT:"); + case 0x00: /* LIT */ + case 0x80: /* LITk */ + __asm__("evaluxn_00_LIT:"); { u->wst.dat[u->wst.ptr] = mempeek8(u->ram.dat, u->ram.ptr++); #ifndef NO_STACK_CHECKS @@ -70,6 +56,19 @@ evaluxn(Uxn *u, u16 vec) u->wst.ptr += 1; } break; + case 0x01: /* INC */ + __asm__("evaluxn_01_INC:"); + { + u8 a = u->wst.dat[u->wst.ptr - 1]; + u->wst.dat[u->wst.ptr - 1] = a + 1; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->wst.ptr < 1, 0)) { + u->wst.error = 1; + goto error; + } +#endif + } + break; case 0x02: /* POP */ __asm__("evaluxn_02_POP:"); { @@ -522,9 +521,9 @@ evaluxn(Uxn *u, u16 vec) u->wst.ptr -= 1; } break; - case 0x21: /* LIT2 */ - case 0xa1: /* LIT2k */ - __asm__("evaluxn_21_LIT2:"); + case 0x20: /* LIT2 */ + case 0xa0: /* LIT2k */ + __asm__("evaluxn_20_LIT2:"); { u->wst.dat[u->wst.ptr] = mempeek8(u->ram.dat, u->ram.ptr++); u->wst.dat[u->wst.ptr + 1] = mempeek8(u->ram.dat, u->ram.ptr++); @@ -537,6 +536,20 @@ evaluxn(Uxn *u, u16 vec) u->wst.ptr += 2; } break; + case 0x21: /* INC2 */ + __asm__("evaluxn_21_INC2:"); + { + u16 a = (u->wst.dat[u->wst.ptr - 1] | (u->wst.dat[u->wst.ptr - 2] << 8)); + u->wst.dat[u->wst.ptr - 2] = (a + 1) >> 8; + u->wst.dat[u->wst.ptr - 1] = (a + 1) & 0xff; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->wst.ptr < 2, 0)) { + u->wst.error = 1; + goto error; + } +#endif + } + break; case 0x22: /* POP2 */ __asm__("evaluxn_22_POP2:"); { @@ -1024,9 +1037,9 @@ evaluxn(Uxn *u, u16 vec) u->wst.ptr -= 1; } break; - case 0x41: /* LITr */ - case 0xc1: /* LITkr */ - __asm__("evaluxn_41_LITr:"); + case 0x40: /* LITr */ + case 0xc0: /* LITkr */ + __asm__("evaluxn_40_LITr:"); { u->rst.dat[u->rst.ptr] = mempeek8(u->ram.dat, u->ram.ptr++); #ifndef NO_STACK_CHECKS @@ -1038,6 +1051,19 @@ evaluxn(Uxn *u, u16 vec) u->rst.ptr += 1; } break; + case 0x41: /* INCr */ + __asm__("evaluxn_41_INCr:"); + { + u8 a = u->rst.dat[u->rst.ptr - 1]; + u->rst.dat[u->rst.ptr - 1] = a + 1; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->rst.ptr < 1, 0)) { + u->rst.error = 1; + goto error; + } +#endif + } + break; case 0x42: /* POPr */ __asm__("evaluxn_42_POPr:"); { @@ -1490,9 +1516,9 @@ evaluxn(Uxn *u, u16 vec) u->rst.ptr -= 1; } break; - case 0x61: /* LIT2r */ - case 0xe1: /* LIT2kr */ - __asm__("evaluxn_61_LIT2r:"); + case 0x60: /* LIT2r */ + case 0xe0: /* LIT2kr */ + __asm__("evaluxn_60_LIT2r:"); { u->rst.dat[u->rst.ptr] = mempeek8(u->ram.dat, u->ram.ptr++); u->rst.dat[u->rst.ptr + 1] = mempeek8(u->ram.dat, u->ram.ptr++); @@ -1505,6 +1531,20 @@ evaluxn(Uxn *u, u16 vec) u->rst.ptr += 2; } break; + case 0x61: /* INC2r */ + __asm__("evaluxn_61_INC2r:"); + { + u16 a = (u->rst.dat[u->rst.ptr - 1] | (u->rst.dat[u->rst.ptr - 2] << 8)); + u->rst.dat[u->rst.ptr - 2] = (a + 1) >> 8; + u->rst.dat[u->rst.ptr - 1] = (a + 1) & 0xff; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->rst.ptr < 2, 0)) { + u->rst.error = 1; + goto error; + } +#endif + } + break; case 0x62: /* POP2r */ __asm__("evaluxn_62_POP2r:"); { @@ -1992,6 +2032,24 @@ evaluxn(Uxn *u, u16 vec) u->rst.ptr -= 1; } break; + case 0x81: /* INCk */ + __asm__("evaluxn_81_INCk:"); + { + u8 a = u->wst.dat[u->wst.ptr - 1]; + u->wst.dat[u->wst.ptr] = a + 1; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->wst.ptr < 1, 0)) { + u->wst.error = 1; + goto error; + } + if(__builtin_expect(u->wst.ptr > 254, 0)) { + u->wst.error = 2; + goto error; + } +#endif + u->wst.ptr += 1; + } + break; case 0x82: /* POPk */ __asm__("evaluxn_82_POPk:"); { @@ -2515,6 +2573,25 @@ evaluxn(Uxn *u, u16 vec) u->wst.ptr += 1; } break; + case 0xa1: /* INC2k */ + __asm__("evaluxn_a1_INC2k:"); + { + u16 a = (u->wst.dat[u->wst.ptr - 1] | (u->wst.dat[u->wst.ptr - 2] << 8)); + u->wst.dat[u->wst.ptr] = (a + 1) >> 8; + u->wst.dat[u->wst.ptr + 1] = (a + 1) & 0xff; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->wst.ptr < 2, 0)) { + u->wst.error = 1; + goto error; + } + if(__builtin_expect(u->wst.ptr > 253, 0)) { + u->wst.error = 2; + goto error; + } +#endif + u->wst.ptr += 2; + } + break; case 0xa2: /* POP2k */ __asm__("evaluxn_a2_POP2k:"); { @@ -3062,6 +3139,24 @@ evaluxn(Uxn *u, u16 vec) u->wst.ptr += 2; } break; + case 0xc1: /* INCkr */ + __asm__("evaluxn_c1_INCkr:"); + { + u8 a = u->rst.dat[u->rst.ptr - 1]; + u->rst.dat[u->rst.ptr] = a + 1; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->rst.ptr < 1, 0)) { + u->rst.error = 1; + goto error; + } + if(__builtin_expect(u->rst.ptr > 254, 0)) { + u->rst.error = 2; + goto error; + } +#endif + u->rst.ptr += 1; + } + break; case 0xc2: /* POPkr */ __asm__("evaluxn_c2_POPkr:"); { @@ -3585,6 +3680,25 @@ evaluxn(Uxn *u, u16 vec) u->rst.ptr += 1; } break; + case 0xe1: /* INC2kr */ + __asm__("evaluxn_e1_INC2kr:"); + { + u16 a = (u->rst.dat[u->rst.ptr - 1] | (u->rst.dat[u->rst.ptr - 2] << 8)); + u->rst.dat[u->rst.ptr] = (a + 1) >> 8; + u->rst.dat[u->rst.ptr + 1] = (a + 1) & 0xff; +#ifndef NO_STACK_CHECKS + if(__builtin_expect(u->rst.ptr < 2, 0)) { + u->rst.error = 1; + goto error; + } + if(__builtin_expect(u->rst.ptr > 253, 0)) { + u->rst.error = 2; + goto error; + } +#endif + u->rst.ptr += 2; + } + break; case 0xe2: /* POP2kr */ __asm__("evaluxn_e2_POP2kr:"); { @@ -4143,7 +4257,7 @@ error: } Device * -portuxn(Uxn *u, u8 id, char *name, void (*talkfn)(Device *d, u8 b0, u8 w)) +uxn_port(Uxn *u, u8 id, char *name, void (*talkfn)(Device *d, u8 b0, u8 w)) { Device *d = &u->dev[id]; d->addr = id * 0x10; -- cgit v1.2.1