Reorganized operators

This commit is contained in:
neauoire 2021-02-07 10:21:41 -08:00
parent 84efbce058
commit b6f742cfaa
4 changed files with 92 additions and 173 deletions

View File

@ -18,14 +18,14 @@ BRK
@strlen @strlen
,0001 ADD^ PEK ,0001 ADD^ PEK
RTS RTU
@incr @incr
,iterator LDR ,iterator LDR
,01 ADD ,01 ADD
,iterator STR ,iterator STR
,iterator LDR ,iterator LDR
RTS RTU
|c000 @FRAME BRK |c000 @FRAME BRK
|d000 @ERROR BRK |d000 @ERROR BRK

View File

@ -18,14 +18,14 @@ BRK
@strlen @strlen
,0001 ADD^ PEK ,0001 ADD^ PEK
RTS RTU
@incr @incr
,iterator LDR ,iterator LDR
,01 ADD ,01 ADD
,iterator STR ,iterator STR
,iterator LDR ,iterator LDR
RTS RTU
|c000 @FRAME BRK |c000 @FRAME BRK
|d000 @ERROR BRK |d000 @ERROR BRK

92
uxn.c
View File

@ -104,78 +104,75 @@ Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.
Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; } Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; } void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; }
/* new flexy pop/push */ /* I/O */
void op_brk() { setflag(FLAG_HALT, 1); } 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_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
void op_nop() { }
void op_drp() { wspop8(); } void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); }
void op_str() { cpu.ram.dat[wspop16()] = wspop8(); }
/* Logic */
void op_jmu() { cpu.rom.ptr = wspop16(); }
void op_jmc() { Uint8 a = wspop8(); if(a) op_jmu(); }
void op_jsu() { rspush16(cpu.rom.ptr); cpu.rom.ptr = wspop16(); }
void op_jsc() { Uint8 a = wspop8(); if(a) op_jsu(); }
void op_rtu() { cpu.rom.ptr = rspop16(); }
void op_rtc() { /* TODO */ }
/* Stack */
void op_pop() { wspop8(); }
void op_dup() { wspush8(wspeek8(1)); } void op_dup() { wspush8(wspeek8(1)); }
void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); } void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); }
void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); } 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_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); }
void op_jmu() { cpu.rom.ptr = wspop16(); }
void op_jsu() { rspush16(cpu.rom.ptr); cpu.rom.ptr = wspop16(); }
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_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); }
void op_ora() { 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_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); }
void op_ror() { Uint8 a = wspop8(), b = wspop8(); wspush8(a >> b); } /* Arithmetic */
void op_add() { 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_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(a - b); }
void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(a * b); } void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(a * b); }
void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(a / b); } void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(a / b); }
void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); } void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(a == b); }
void op_str() { cpu.ram.dat[wspop16()] = wspop8(); } void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(a != b); }
void op_pek() { wspush8(cpu.rom.dat[wspop16()]); } void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a < b); }
void op_pok() { printf("TODO:\n");} void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(a > b); }
/* Stack(16-bits) */
void op_drp16() { wspop16(); } void op_pop16() { wspop16(); }
void op_dup16() { wspush16(wspeek16(2)); } void op_dup16() { wspush16(wspeek16(2)); }
void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); } void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); }
void op_ovr16() { Uint16 a = wspeek16(4); 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_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_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); }
void op_ora16() { 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_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); }
void op_ror16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a >> b); } /* Arithmetic(16-bits) */
void op_add16() { 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_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a - b); }
void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a * b); } void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a * b); }
void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a / b); } void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a / b); }
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); }
/* remove */
void op_pek() { wspush8(cpu.rom.dat[wspop16()]); }
void op_pok() { printf("TODO:\n");}
void (*ops8[])() = { void (*ops[])() = {
op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot, op_brk, op_lit, op_nop, op_nop, op_pek, op_pok, op_ldr, op_str,
op_jmu, op_jsu, op_jmc, op_jsc, op_equ, op_neq, op_gth, op_lth, op_jmu, op_jmc, op_jsu, op_jsc, op_rtu, op_rtc, op_nop, op_nop,
op_and, op_ora, op_rol, op_ror, op_add, op_sub, op_mul, op_div, op_pop, op_dup, op_swp, op_ovr, op_rot, op_and, op_ora, op_rol,
op_ldr, op_str, op_pek, op_pok, op_brk, op_brk, op_brk, op_brk op_add, op_sub, op_mul, op_div, op_equ, op_neq, op_gth, op_lth,
}; op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16, op_and16, op_ora16, op_rol16,
op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16
void (*ops16[])() = {
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_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
}; };
Uint8 opr[][2] = { /* todo: 16 bits mode */ Uint8 opr[][2] = { /* todo: 16 bits mode */
{0,0}, {0,0}, {0,0}, {1,0}, {0,1}, {1,1}, {0,1}, {3,3}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{2,0}, {2,0}, {2,0}, {2,0}, {2,1}, {2,1}, {2,1}, {2,1}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{1,0}, {1,0}, {1,0}, {1,0}, {2,1}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{2,1}, {3,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0} {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
}; };
/* clang-format on */ /* clang-format on */
@ -231,14 +228,11 @@ eval(void)
setflag(FLAG_SIGN, (instr >> 6) & 1); setflag(FLAG_SIGN, (instr >> 6) & 1);
if((instr >> 7) & 1) if((instr >> 7) & 1)
printf("Unused flag: %02x\n", instr); printf("Unused flag: %02x\n", instr);
/* TODO: setflag(FLAG_B, (instr >> 6) & 1); */
/* TODO: setflag(FLAG_C, (instr >> 7) & 1); */
if(cpu.wst.ptr < opr[op][0]) if(cpu.wst.ptr < opr[op][0])
return error("Stack underflow", op); return error("Stack underflow", op);
if(getflag(FLAG_SHORT)) if(getflag(FLAG_SHORT))
(*ops16[op])(); op += 16;
else (*ops[op])();
(*ops8[op])();
cpu.counter++; cpu.counter++;
return 1; return 1;
} }

165
uxnasm.c
View File

@ -26,98 +26,27 @@ typedef struct {
int labelslen; int labelslen;
Label labels[256]; Label labels[256];
Program p;
/* clang-format off */ /* clang-format off */
char ops[][4] = { char ops[][4] = {
"BRK", "RTS", "LIT", "POP", "DUP", "SWP", "OVR", "ROT", "BRK", "LIT", "---", "---", "PEK", "POK", "LDR", "STR",
"JMU", "JSU", "JMC", "JSC", "EQU", "NEQ", "GTH", "LTH", "JMU", "JMC", "JSU", "JSC", "RTU", "RTC", "---", "---",
"AND", "ORA", "ROL", "ROR", "ADD", "SUB", "MUL", "DIV", "POP", "DUP", "SWP", "OVR", "ROT", "AND", "ORA", "ROL",
"LDR", "STR", "PEK", "POK", "---", "---", "---", "---" "ADD", "SUB", "MUL", "DIV", "EQU", "NEQ", "GTH", "LTH"
}; };
/* clang-format on */ int scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */
int slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
Program p; int sihx(char *s) { int i = 0; char c; while((c = s[i++])) if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return 0; return 1; } /* string is hexadecimal */
int shex(char *s) { int n = 0, i = 0; char c; while((c = s[i++])) if(c >= '0' && c <= '9') n = n * 16 + (c - '0'); else if(c >= 'A' && c <= 'F') n = n * 16 + 10 + (c - 'A'); else if(c >= 'a' && c <= 'f') n = n * 16 + 10 + (c - 'a'); return n; } /* string to num */
int cmnt(char *w, int *skip) { if(w[0] == ')') { *skip = 0; return 1; } if(w[0] == '(') *skip = 1; if(*skip) return 1; return 0; } /* comment helper */
char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
#pragma mark - Helpers #pragma mark - Helpers
int /* clang-format on */
scmp(char *a, char *b) /* string compare */
{
int i = 0;
while(a[i] == b[i])
if(!a[i++])
return 1;
return 0;
}
char *
scpy(char *src, char *dst, int len) /* string copy */
{
int i = 0;
while((dst[i] = src[i]) && i < len - 2)
i++;
dst[i + 1] = '\0';
return dst;
}
int
slen(char *s) /* string length */
{
int i = 0;
while(s[i] && s[++i])
;
return i;
}
char *
suca(char *s) /* string to uppercase */
{
int i = 0;
char c;
while((c = s[i]))
s[i++] = c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
return s;
}
int
sihx(char *s) /* string is hexadecimal */
{
int i = 0;
char c;
while((c = s[i++]))
if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F'))
return 0;
return 1;
}
int
shex(char *s) /* string to num */
{
int n = 0, i = 0;
char c;
while((c = s[i++]))
if(c >= '0' && c <= '9')
n = n * 16 + (c - '0');
else if(c >= 'A' && c <= 'F')
n = n * 16 + 10 + (c - 'A');
else if(c >= 'a' && c <= 'f')
n = n * 16 + 10 + (c - 'a');
return n;
}
int
iscomment(char *w, int *skip)
{
if(w[0] == ')') {
*skip = 0;
return 1;
}
if(w[0] == '(') *skip = 1;
if(*skip) return 1;
return 0;
}
#pragma mark - I/O #pragma mark - I/O
@ -125,7 +54,7 @@ void
pushbyte(Uint8 b, int lit) pushbyte(Uint8 b, int lit)
{ {
if(lit) { if(lit) {
pushbyte(0x02, 0); pushbyte(0x01, 0);
pushbyte(0x01, 0); pushbyte(0x01, 0);
} }
p.data[p.ptr++] = b; p.data[p.ptr++] = b;
@ -135,7 +64,7 @@ void
pushshort(Uint16 s, int lit) pushshort(Uint16 s, int lit)
{ {
if(lit) { if(lit) {
pushbyte(0x02, 0); pushbyte(0x01, 0);
pushbyte(0x02, 0); pushbyte(0x02, 0);
} }
pushbyte((s >> 8) & 0xff, 0); pushbyte((s >> 8) & 0xff, 0);
@ -143,10 +72,10 @@ pushshort(Uint16 s, int lit)
} }
void void
pushword(char *w) pushtext(char *w)
{ {
int i = slen(w); int i = slen(w);
pushbyte(0x02, 0); pushbyte(0x01, 0);
pushbyte(slen(w), 0); pushbyte(slen(w), 0);
while(i > 0) while(i > 0)
pushbyte(w[--i], 0); pushbyte(w[--i], 0);
@ -163,19 +92,18 @@ findlabel(char *s)
} }
Uint8 Uint8
findop(char *s) findoperator(char *s)
{ {
int i; int i;
for(i = 0; i < 32; ++i) { for(i = 0; i < 0x20; ++i) {
int m = 0; int m = 0;
if(ops[i][0] != s[0]) continue; char *o = ops[i];
if(ops[i][1] != s[1]) continue; if(o[0] != s[0] || o[1] != s[1] || o[2] != s[2])
if(ops[i][2] != s[2]) continue; continue;
while(s[3 + m]) { while(s[3 + m]) {
char c = s[3 + m]; if(s[3 + m] == '^') i |= (1 << 5); /* mode: 16 bits */
if(c == '^') i |= (1 << 5); /* mode: 16 bits */ if(s[3 + m] == '~') i |= (1 << 6); /* mode: signed */
if(c == '~') i |= (1 << 6); /* mode: signed */ if(s[3 + m] == '&') i |= (1 << 7); /* mode: unused */
if(c == '&') i |= (1 << 7); /* mode: unused */
m++; m++;
} }
return i; return i;
@ -193,14 +121,14 @@ error(char *name, char *id)
} }
int int
makelabel(char *id, Uint16 addr) makelabel(char *name, Uint16 addr)
{ {
Label *l; Label *l;
if(findlabel(id)) if(findlabel(name))
return error("Label duplicate", id); return error("Label duplicate", name);
l = &labels[labelslen++]; l = &labels[labelslen++];
scpy(id, l->name, 64);
l->addr = addr; l->addr = addr;
scpy(name, l->name, 64);
printf("New label: %s[0x%02x]\n", l->name, l->addr); printf("New label: %s[0x%02x]\n", l->name, l->addr);
return 1; return 1;
} }
@ -220,7 +148,8 @@ pass1(FILE *f)
Uint16 addr = 0; Uint16 addr = 0;
char w[64]; char w[64];
while(fscanf(f, "%s", w) == 1) { while(fscanf(f, "%s", w) == 1) {
if(iscomment(w, &skip)) continue; if(cmnt(w, &skip))
continue;
if(w[0] == '@' && !makelabel(w + 1, addr)) if(w[0] == '@' && !makelabel(w + 1, addr))
return error("Pass1 failed", w); return error("Pass1 failed", w);
if(w[0] == ';' && !makelabel(w + 1, vars++)) if(w[0] == ';' && !makelabel(w + 1, vars++))
@ -231,23 +160,19 @@ pass1(FILE *f)
else else
continue; continue;
} }
/* move addr ptr */ if(findoperator(w) || scmp(w, "BRK"))
if(findop(w) || scmp(w, "BRK"))
addr += 1; addr += 1;
else if(w[0] == '|') else {
addr = shex(w + 1); switch(w[0]) {
else if(w[0] == '@') case '|': addr = shex(w + 1); break;
addr += 0; case '@':
else if(w[0] == ';') case ';': break;
addr += 0; case '.': addr += 2; break;
else if(w[0] == '.') case '"': addr += slen(w + 1) + 2; break;
addr += 2; case ',': addr += 2 + (sihx(w + 1) && slen(w + 1) == 2 ? 1 : 2); break;
else if(w[0] == '"') default: return error("Unknown label", w);
addr += slen(w + 1) + 2; }
else if(w[0] == ',') }
addr += 2 + (sihx(w + 1) && slen(w + 1) == 2 ? 1 : 2);
else
return error("Unknown label", w);
} }
rewind(f); rewind(f);
return 1; return 1;
@ -263,16 +188,16 @@ pass2(FILE *f)
Label *l; Label *l;
if(w[0] == '@') continue; if(w[0] == '@') continue;
if(w[0] == ';') continue; if(w[0] == ';') continue;
if(iscomment(w, &skip)) continue; if(cmnt(w, &skip)) continue;
if(w[0] == '|') if(w[0] == '|')
p.ptr = shex(w + 1); p.ptr = shex(w + 1);
else if(w[0] == ':') else if(w[0] == ':')
fscanf(f, "%s", w); fscanf(f, "%s", w);
else if(w[0] == '"') else if(w[0] == '"')
pushword(w + 1); pushtext(w + 1);
else if((l = findlabel(w + 1))) else if((l = findlabel(w + 1)))
pushshort(l->addr, w[0] == ','); pushshort(l->addr, w[0] == ',');
else if((op = findop(w)) || scmp(w, "BRK")) else if((op = findoperator(w)) || scmp(w, "BRK"))
pushbyte(op, 0); pushbyte(op, 0);
else if(sihx(w + 1) && slen(w + 1) == 2) else if(sihx(w + 1) && slen(w + 1) == 2)
pushbyte(shex(w + 1), w[0] == ','); pushbyte(shex(w + 1), w[0] == ',');