uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

uxn-abc-disp.c (3643B)


      1 #include "uxn.h"
      2 
      3 /*
      4 Copyright (u) 2022-2023 Devine Lu Linvega, Andrew Alderwick, Andrew Richards
      5 
      6 Permission to use, copy, modify, and distribute this software for any
      7 purpose with or without fee is hereby granted, provided that the above
      8 copyright notice and this permission notice appear in all copies.
      9 
     10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11 WITH REGARD TO THIS SOFTWARE.
     12 */
     13 
     14 #define FLIP     { s = ins & 0x40 ? &u->wst : &u->rst; }
     15 #define JUMP(x)  { if(m2) pc = (x); else pc += (Sint8)(x); }
     16 #define POP1(o)  { o = s->dat[--*sp]; }
     17 #define POP2(o)  { o = s->dat[--*sp] | (s->dat[--*sp] << 0x8); }
     18 #define POPx(o)  { if(m2) { POP2(o) } else POP1(o) }
     19 #define PUSH1(y) { s->dat[s->ptr++] = (y); }
     20 #define PUSH2(y) { tt = (y); s->dat[s->ptr++] = tt >> 0x8; s->dat[s->ptr++] = tt; }
     21 #define PUSHx(y) { if(m2) { PUSH2(y) } else PUSH1(y) }
     22 #define PEEK(o, x, r) { if(m2) { r = (x); o = ram[r++] << 8 | ram[r]; } else o = ram[(x)]; }
     23 #define POKE(x, y, r) { if(m2) { r = (x); ram[r++] = y >> 8; ram[r] = y; } else ram[(x)] = (y); }
     24 #define DEVR(o, p)    { if(m2) { o = (emu_dei(u, p) << 8) | emu_dei(u, p + 1); } else o = emu_dei(u, p); }
     25 #define DEVW(p, y)    { if(m2) { emu_deo(u, p, y >> 8); emu_deo(u, p + 1, y); } else emu_deo(u, p, y); }
     26 #define next { ins = ram[pc++]; \
     27 	m2 = ins & 0x20; \
     28 	s = ins & 0x40 ? &u->rst : &u->wst; \
     29 	if(ins & 0x80) kp = s->ptr, sp = &kp; else sp = &s->ptr; \
     30 	goto *lut[ins & 0x1f]; }
     31 
     32 int
     33 uxn_eval(Uxn *u, Uint16 pc)
     34 {
     35 	Uint8 t, kp, *sp, ins, m2, *ram = u->ram;
     36 	Uint16 tt, a, b, c;
     37 	Stack *s;
     38 	static void* lut[] = {
     39 		&&_imm, &&_inc, &&_pop, &&_nip, &&_swp, &&_rot, &&_dup, &&_ovr,
     40 		&&_equ, &&_neq, &&_gth, &&_lth, &&_jmp, &&_jcn, &&_jsr, &&_sth,
     41 		&&_ldz, &&_stz, &&_ldr, &&_str, &&_lda, &&_sta, &&_dei, &&_deo,
     42 		&&_add, &&_sub, &&_mul, &&_div, &&_and, &&_ora, &&_eor, &&_sft };
     43 	if(!pc || u->dev[0x0f]) return 0;
     44 	next
     45 	_imm: 
     46 		switch(ins) {
     47 			case 0x00: /* BRK */ return 1;
     48 			case 0x20: /* JCI */ POP1(b) if(!b) { pc += 2; break; }
     49 			case 0x40: /* JMI */ a = ram[pc++] << 8 | ram[pc++]; pc += a; break;
     50 			case 0x60: /* JSI */ PUSH2(pc + 2) a = ram[pc++] << 8 | ram[pc++]; pc += a; break;
     51 			case 0x80: case 0xc0: /* LIT  */ PUSH1(ram[pc++]) break;
     52 			case 0xa0: case 0xe0: /* LIT2 */ PUSH1(ram[pc++]) PUSH1(ram[pc++]) break;
     53 		} next
     54 	_inc: POPx(a) PUSHx(a + 1) next
     55 	_pop: POPx(a) next
     56 	_nip: POPx(a) POPx(b) PUSHx(a) next
     57 	_swp: POPx(a) POPx(b) PUSHx(a) PUSHx(b) next
     58 	_rot: POPx(a) POPx(b) POPx(c) PUSHx(b) PUSHx(a) PUSHx(c) next
     59 	_dup: POPx(a) PUSHx(a) PUSHx(a) next
     60 	_ovr: POPx(a) POPx(b) PUSHx(b) PUSHx(a) PUSHx(b) next
     61 	_equ: POPx(a) POPx(b) PUSH1(b == a) next
     62 	_neq: POPx(a) POPx(b) PUSH1(b != a) next
     63 	_gth: POPx(a) POPx(b) PUSH1(b > a) next
     64 	_lth: POPx(a) POPx(b) PUSH1(b < a) next
     65 	_jmp: POPx(a) JUMP(a) next
     66 	_jcn: POPx(a) POP1(b) if(b) JUMP(a) next
     67 	_jsr: POPx(a) FLIP PUSH2(pc) JUMP(a) next
     68 	_sth: POPx(a) FLIP PUSHx(a) next
     69 	_ldz: POP1(a) PEEK(b, a, t) PUSHx(b) next
     70 	_stz: POP1(a) POPx(b) POKE(a, b, t) next
     71 	_ldr: POP1(a) PEEK(b, pc + (Sint8)a, tt) PUSHx(b) next
     72 	_str: POP1(a) POPx(b) POKE(pc + (Sint8)a, b, tt) next
     73 	_lda: POP2(a) PEEK(b, a, tt) PUSHx(b) next
     74 	_sta: POP2(a) POPx(b) POKE(a, b, tt) next
     75 	_dei: POP1(a) DEVR(b, a) PUSHx(b) next
     76 	_deo: POP1(a) POPx(b) DEVW(a, b) next
     77 	_add: POPx(a) POPx(b) PUSHx(b + a) next
     78 	_sub: POPx(a) POPx(b) PUSHx(b - a) next
     79 	_mul: POPx(a) POPx(b) PUSHx(b * a) next
     80 	_div: POPx(a) POPx(b) PUSHx(a ? b / a : 0) next
     81 	_and: POPx(a) POPx(b) PUSHx(b & a) next
     82 	_ora: POPx(a) POPx(b) PUSHx(b | a) next
     83 	_eor: POPx(a) POPx(b) PUSHx(b ^ a) next
     84 	_sft: POP1(a) POPx(b) PUSHx(b >> (a & 0xf) << (a >> 4)) next
     85 }