0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-23 22:35:11 +00:00

Completed new uxn core

This commit is contained in:
neauoire 2021-08-29 18:18:52 -07:00
parent 51313988cf
commit 91df7ab470
2 changed files with 63 additions and 78 deletions

140
src/uxn.c
View file

@ -30,71 +30,25 @@ static void (*devw)(Device *d, Uint8 a, Uint16 b);
static Uint16 (*devr)(Device *d, Uint8 a);
static void (*warp)(Uxn *u, Uint16 a);
static void (*pull)(Uxn *u);
/* byte mode */
static void push8(Stack *s, Uint16 a) { if(s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
static Uint16 pop8_keep(Stack *s) { if(s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; }
static Uint16 pop8_nokeep(Stack *s) { if(s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
static Uint16 pop8k(Stack *s) { if(s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; }
static Uint16 pop8d(Stack *s) { if(s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
static void poke8(Uint8 *m, Uint16 a, Uint16 b) { m[a] = b; }
static Uint16 peek8(Uint8 *m, Uint16 a) { return m[a]; }
static void devw8(Device *d, Uint8 a, Uint16 b) { d->dat[a & 0xf] = b; d->talk(d, a & 0x0f, 1); }
static Uint16 devr8(Device *d, Uint8 a) { d->talk(d, a & 0x0f, 0); return d->dat[a & 0xf]; }
static void warp8(Uxn *u, Uint16 a){ u->ram.ptr += (Sint8)a; }
static void pull8(Uxn *u){ push8(u->src, peek8(u->ram.dat, u->ram.ptr++)); }
void poke16(Uint8 *m, Uint16 a, Uint16 b) { poke8(m, a, b >> 8); poke8(m, a + 1, b); }
Uint16 peek16(Uint8 *m, Uint16 a) { return (peek8(m, a) << 8) + peek8(m, a + 1); }
static void warp8(Uxn *u, Uint16 a){ u->ram.ptr += (Sint8)a; }
static void pull8(Uxn *u){ push8(u->src, peek8(u->ram.dat, u->ram.ptr++)); }
/* short mode */
void poke16(Uint8 *m, Uint16 a, Uint16 b) { poke8(m, a, b >> 8); poke8(m, a + 1, b); }
Uint16 peek16(Uint8 *m, Uint16 a) { return (peek8(m, a) << 8) + peek8(m, a + 1); }
static void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
static Uint16 pop16(Stack *s) { Uint8 a = pop8(s), b = pop8(s); return a + (b << 8); }
static void devw16(Device *d, Uint8 a, Uint16 b) { devw8(d, a, b >> 8); devw8(d, a + 1, b); }
static Uint16 devr16(Device *d, Uint8 a) { return (devr8(d, a) << 8) + devr8(d, a + 1); }
static void warp16(Uxn *u, Uint16 a){ u->ram.ptr = a; }
static void pull16(Uxn *u){ push16(u->src, peek16(u->ram.dat, u->ram.ptr++)); u->ram.ptr++; }
/* Stack */
static void op_lit(Uxn *u) { pull(u); }
static void op_inc(Uxn *u) { Uint16 a = pop(u->src); push(u->src, a + 1); }
static void op_pop(Uxn *u) { pop(u->src); }
static void op_dup(Uxn *u) { Uint16 a = pop(u->src); push(u->src, a); push(u->src, a); }
static void op_nip(Uxn *u) { Uint16 a = pop(u->src); pop(u->src); push(u->src, a); }
static void op_swp(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, a); push(u->src, b); }
static void op_ovr(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, b); }
static void op_rot(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src), c = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, c); }
/* Logic */
static void op_equ(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b == a); }
static void op_neq(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b != a); }
static void op_gth(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b > a); }
static void op_lth(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push8(u->src, b < a); }
static void op_jmp(Uxn *u) { Uint16 a = pop(u->src); warp(u, a); }
static void op_jnz(Uxn *u) { Uint16 a = pop(u->src); if(pop8(u->src)) warp(u, a); }
static void op_jsr(Uxn *u) { Uint16 a = pop(u->src); push16(u->dst, u->ram.ptr); warp(u, a); }
static void op_sth(Uxn *u) { Uint16 a = pop(u->src); push(u->dst, a); }
/* Memory */
static void op_ldz(Uxn *u) { Uint8 a = pop8(u->src); push(u->src, peek(u->ram.dat, a)); }
static void op_stz(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop(u->src); poke(u->ram.dat, a, b); }
static void op_ldr(Uxn *u) { Uint8 a = pop8(u->src); push(u->src, peek(u->ram.dat, u->ram.ptr + (Sint8)a)); }
static void op_str(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop(u->src); poke(u->ram.dat, u->ram.ptr + (Sint8)a, b); }
static void op_lda(Uxn *u) { Uint16 a = pop16(u->src); push(u->src, peek(u->ram.dat, a)); }
static void op_sta(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop(u->src); poke(u->ram.dat, a, b); }
static void op_dei(Uxn *u) { Uint8 a = pop8(u->src); push(u->src, devr(&u->dev[a >> 4], a)); }
static void op_deo(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop(u->src); devw(&u->dev[a >> 4], a, b); }
/* Arithmetic */
static void op_add(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b + a); }
static void op_sub(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b - a); }
static void op_mul(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b * a); }
static void op_div(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); if(a == 0) { u->src->error = 3; a = 1; } push(u->src, b / a); }
static void op_and(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b & a); }
static void op_ora(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b | a); }
static void op_eor(Uxn *u) { Uint16 a = pop(u->src), b = pop(u->src); push(u->src, b ^ a); }
static void op_sft(Uxn *u) { Uint16 a = pop8(u->src), b = pop(u->src); push(u->src, b >> (a & 0x07) << ((a & 0x70) >> 4)); }
static void (*ops[])(Uxn *u) = {
op_lit, op_inc, op_pop, op_dup, op_nip, op_swp, op_ovr, op_rot,
op_equ, op_neq, op_gth, op_lth, op_jmp, op_jnz, op_jsr, op_sth,
op_ldz, op_stz, op_ldr, op_str, op_lda, op_sta, op_dei, op_deo,
op_add, op_sub, op_mul, op_div, op_and, op_ora, op_eor, op_sft,
};
/* clang-format on */
static void warp16(Uxn *u, Uint16 a){ u->ram.ptr = a; }
static void pull16(Uxn *u){ push16(u->src, peek16(u->ram.dat, u->ram.ptr++)); u->ram.ptr++; }
#pragma mark - Core
@ -102,6 +56,7 @@ int
uxn_eval(Uxn *u, Uint16 vec)
{
Uint8 instr;
Uint16 a,b,c;
if(!vec || u->dev[0].dat[0xf])
return 0;
u->ram.ptr = vec;
@ -109,40 +64,69 @@ uxn_eval(Uxn *u, Uint16 vec)
while((instr = u->ram.dat[u->ram.ptr++])) {
/* Return Mode */
if(instr & MODE_RETURN) {
u->src = &u->rst;
u->src = &u->rst;
u->dst = &u->wst;
} else {
u->src = &u->wst;
u->src = &u->wst;
u->dst = &u->rst;
}
/* Keep Mode */
if(instr & MODE_KEEP) {
pop8 = pop8_keep;
pop8 = pop8k;
u->src->kptr = u->src->ptr;
} else {
pop8 = pop8_nokeep;
pop8 = pop8d;
}
/* Short Mode */
if(instr & MODE_SHORT) {
pop = pop16;
push = push16;
peek = peek16;
poke = poke16;
devr = devr16;
devw = devw16;
warp = warp16;
pull = pull16;
push = push16; pop = pop16;
poke = poke16; peek = peek16;
devw = devw16; devr = devr16;
warp = warp16; pull = pull16;
} else {
pop = pop8;
push = push8;
peek = peek8;
poke = poke8;
devr = devr8;
devw = devw8;
warp = warp8;
pull = pull8;
push = push8; pop = pop8;
poke = poke8; peek = peek8;
devw = devw8; devr = devr8;
warp = warp8; pull = pull8;
}
switch(instr & 0x1f){
/* Stack */
case 0x00: /* LIT */ pull(u); break;
case 0x01: /* INC */ a = pop(u->src); push(u->src, a + 1); break;
case 0x02: /* POP */ pop(u->src); break;
case 0x03: /* DUP */ a = pop(u->src); push(u->src, a); push(u->src, a); break;
case 0x04: /* NIP */ a = pop(u->src); pop(u->src); push(u->src, a); break;
case 0x05: /* SWP */ a = pop(u->src), b = pop(u->src); push(u->src, a); push(u->src, b); break;
case 0x06: /* OVR */ a = pop(u->src), b = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, b); break;
case 0x07: /* ROT */ a = pop(u->src), b = pop(u->src), c = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, c); break;
/* Logic */
case 0x08: /* EQU */ a = pop(u->src), b = pop(u->src); push8(u->src, b == a); break;
case 0x09: /* NEQ */ a = pop(u->src), b = pop(u->src); push8(u->src, b != a); break;
case 0x0a: /* GTH */ a = pop(u->src), b = pop(u->src); push8(u->src, b > a); break;
case 0x0b: /* LTH */ a = pop(u->src), b = pop(u->src); push8(u->src, b < a); break;
case 0x0c: /* JMP */ a = pop(u->src); warp(u, a); break;
case 0x0d: /* JNZ */ a = pop(u->src); if(pop8(u->src)) warp(u, a); break;
case 0x0e: /* JSR */ a = pop(u->src); push16(u->dst, u->ram.ptr); warp(u, a); break;
case 0x0f: /* STH */ a = pop(u->src); push(u->dst, a); break;
/* Memory */
case 0x10: /* LDZ */ a = pop8(u->src); push(u->src, peek(u->ram.dat, a)); break;
case 0x11: /* STZ */ a = pop8(u->src); b = pop(u->src); poke(u->ram.dat, a, b); break;
case 0x12: /* LDR */ a = pop8(u->src); push(u->src, peek(u->ram.dat, u->ram.ptr + (Sint8)a)); break;
case 0x13: /* STR */ a = pop8(u->src); b = pop(u->src); poke(u->ram.dat, u->ram.ptr + (Sint8)a, b); break;
case 0x14: /* LDA */ a = pop16(u->src); push(u->src, peek(u->ram.dat, a)); break;
case 0x15: /* STA */ a = pop16(u->src); b = pop(u->src); poke(u->ram.dat, a, b); break;
case 0x16: /* DEI */ a = pop8(u->src); push(u->src, devr(&u->dev[a >> 4], a)); break;
case 0x17: /* DEO */ a = pop8(u->src); b = pop(u->src); devw(&u->dev[a >> 4], a, b); break;
/* Arithmetic */
case 0x18: /* ADD */ a = pop(u->src), b = pop(u->src); push(u->src, b + a); break;
case 0x19: /* SUB */ a = pop(u->src), b = pop(u->src); push(u->src, b - a); break;
case 0x1a: /* MUL */ a = pop(u->src), b = pop(u->src); push(u->src, b * a); break;
case 0x1b: /* DIV */ a = pop(u->src), b = pop(u->src); if(a == 0) { u->src->error = 3; a = 1; } push(u->src, b / a); break;
case 0x1c: /* AND */ a = pop(u->src), b = pop(u->src); push(u->src, b & a); break;
case 0x1d: /* ORA */ a = pop(u->src), b = pop(u->src); push(u->src, b | a); break;
case 0x1e: /* EOR */ a = pop(u->src), b = pop(u->src); push(u->src, b ^ a); break;
case 0x1f: /* SFT */ a = pop8(u->src), b = pop(u->src); push(u->src, b >> (a & 0x07) << ((a & 0x70) >> 4)); break;
}
(*ops[instr & 0x1f])(u);
if(u->wst.error)
return uxn_halt(u, u->wst.error, "Working-stack", instr);
if(u->rst.error)
@ -151,6 +135,8 @@ uxn_eval(Uxn *u, Uint16 vec)
return 1;
}
/* clang-format on */
int
uxn_boot(Uxn *u)
{

View file

@ -257,7 +257,6 @@ doctrl(Uxn *u, SDL_Event *event, int z)
case SDLK_F2: if(z) toggledebug(u); break;
case SDLK_F3: if(z) screencapture(); break;
}
/* clang-format on */
if(z) {
devctrl->dat[2] |= flag;