uxn/uxnasm.c

251 lines
3.8 KiB
C
Raw Normal View History

2021-01-29 15:14:37 -05:00
#include <stdio.h>
/*
Copyright (c) 2021 Devine Lu Linvega
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
2021-01-29 16:59:16 -05:00
#define PRGLEN 256
2021-01-31 00:31:49 -05:00
#define LABELIDLEN 32
2021-01-29 15:14:37 -05:00
typedef unsigned char Uint8;
2021-01-29 16:59:16 -05:00
typedef struct {
2021-01-30 17:25:48 -05:00
int len;
2021-01-29 20:49:10 -05:00
Uint8 data[PRGLEN];
2021-01-29 16:59:16 -05:00
} Program;
2021-01-31 00:31:49 -05:00
typedef struct {
Uint8 addr;
char name[LABELIDLEN];
} Label;
int labelslen;
Label labels[256];
2021-01-30 17:25:48 -05:00
2021-01-30 00:56:19 -05:00
char opcodes[][4] = {
"BRK",
2021-02-01 14:58:47 -05:00
"RTS",
2021-01-30 00:56:19 -05:00
"LIT",
2021-01-30 17:25:48 -05:00
"POP",
2021-01-30 00:56:19 -05:00
"DUP",
"SWP",
2021-01-30 17:25:48 -05:00
"OVR",
"ROT",
/* */
2021-01-30 00:56:19 -05:00
"JMP",
"JSR",
2021-02-01 14:58:47 -05:00
"JMQ",
"JSQ",
2021-01-30 00:56:19 -05:00
"EQU",
"NEQ",
"LTH",
2021-01-30 17:25:48 -05:00
"GTH",
2021-01-31 00:31:49 -05:00
"---",
"---",
"---",
"---",
"ADD",
"SUB",
"MUL",
"DIV"
2021-01-30 17:25:48 -05:00
/* */};
2021-01-29 20:49:10 -05:00
2021-01-29 16:59:16 -05:00
Program p;
#pragma mark - Helpers
int
scmp(char *a, char *b) /* string compare */
{
int i = 0;
while(a[i] == b[i])
if(!a[i++])
return 1;
return 0;
}
2021-01-31 00:31:49 -05:00
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;
}
2021-01-29 20:49:10 -05:00
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;
}
2021-01-30 17:25:48 -05:00
int
sihx(char *s)
{
int i = 0;
char c;
while((c = s[i++]))
2021-01-31 00:31:49 -05:00
if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F'))
2021-01-30 17:25:48 -05:00
return 0;
return 1;
}
2021-01-29 16:59:16 -05:00
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');
2021-01-30 17:25:48 -05:00
else if(c >= 'A' && c <= 'F')
n = n * 16 + 10 + (c - 'A');
2021-01-31 00:31:49 -05:00
else if(c >= 'a' && c <= 'f')
n = n * 16 + 10 + (c - 'f');
2021-01-29 16:59:16 -05:00
return n;
}
2021-01-30 17:25:48 -05:00
#pragma mark - Parser
void
addprg(Uint8 hex)
{
p.data[p.len++] = hex;
}
void
addlabel(char *id, Uint8 addr)
{
2021-01-31 00:31:49 -05:00
Label *l = &labels[labelslen++];
scpy(suca(id), l->name, LABELIDLEN);
l->addr = addr;
printf("new label: %s=%02x\n", l->name, l->addr);
2021-01-30 17:25:48 -05:00
}
void
addconst(char *id, Uint8 value)
{
printf("new const: %s=%02x\n", id, value);
}
2021-01-29 16:59:16 -05:00
2021-01-31 00:31:49 -05:00
Label *
findlabel(char *s)
{
int i;
for(i = 0; i < labelslen; ++i)
if(scmp(labels[i].name, s))
return &labels[i];
return NULL;
}
2021-01-29 16:59:16 -05:00
Uint8
2021-01-30 17:25:48 -05:00
findop(char *s)
2021-01-29 16:59:16 -05:00
{
2021-01-29 20:49:10 -05:00
int i;
2021-01-31 00:31:49 -05:00
for(i = 0; i < 24; ++i)
2021-01-30 17:25:48 -05:00
if(scmp(opcodes[i], s))
2021-01-29 20:49:10 -05:00
return i;
2021-01-30 17:25:48 -05:00
return 0;
}
2021-01-31 00:31:49 -05:00
int
getlength(char *w)
{
if(findop(w) || scmp(w, "BRK")) return 1;
2021-02-01 14:58:47 -05:00
if(w[0] == '.') return 2;
2021-01-31 00:31:49 -05:00
if(w[0] == ':') return 0;
2021-02-01 14:58:47 -05:00
if(w[0] == '+') return 2;
if(w[0] == '-') return 2;
2021-01-31 00:31:49 -05:00
printf("Unknown length %s\n", w);
return 0;
}
2021-01-30 17:25:48 -05:00
int
comment(char *w, int *skip)
{
if(w[0] == '>') {
*skip = 0;
return 1;
}
if(w[0] == '<') *skip = 1;
if(*skip) return 1;
return 0;
2021-01-29 20:49:10 -05:00
}
2021-01-29 15:14:37 -05:00
void
pass1(FILE *f)
{
2021-01-30 17:25:48 -05:00
int skip = 0;
2021-01-31 00:31:49 -05:00
int addr = 0;
2021-01-29 16:59:16 -05:00
char word[64];
while(fscanf(f, "%s", word) == 1) {
2021-01-31 00:31:49 -05:00
if(comment(word, &skip)) continue;
if(word[0] == ':') addlabel(word + 1, addr);
addr += getlength(word);
2021-01-30 17:25:48 -05:00
}
rewind(f);
}
void
pass2(FILE *f)
{
int skip = 0;
char word[64];
while(fscanf(f, "%s", word) == 1) {
2021-02-01 14:58:47 -05:00
Uint8 op = 0;
2021-01-31 00:31:49 -05:00
Label *l;
if(word[0] == ':') continue;
2021-01-30 17:25:48 -05:00
suca(word);
if(comment(word, &skip)) continue;
2021-02-01 14:58:47 -05:00
/* literals */
if(word[0] == '+' || word[0] == '-')
addprg(0x02);
if(word[0] == '+')
2021-01-30 17:25:48 -05:00
addprg(shex(word + 1));
2021-02-01 14:58:47 -05:00
else if(word[0] == '-')
addprg((Uint8)(-1 * shex(word + 1)));
/* opcodes */
else if((op = findop(word)) || scmp(word, "BRK"))
2021-01-30 17:25:48 -05:00
addprg(op);
2021-01-31 00:31:49 -05:00
else if((l = findlabel(word + 1))) {
2021-02-01 14:58:47 -05:00
addprg(0x02);
2021-01-31 00:31:49 -05:00
addprg(l->addr);
} else
2021-01-30 17:25:48 -05:00
printf("unknown: %s\n", word);
2021-01-29 15:14:37 -05:00
}
}
int
error(char *name)
{
printf("Error: %s\n", name);
return 0;
}
int
main(int argc, char *argv[])
{
FILE *f;
if(argc < 3)
return error("No input.");
if(!(f = fopen(argv[1], "r")))
return error("Missing input.");
pass1(f);
2021-01-30 17:25:48 -05:00
pass2(f);
2021-01-29 16:59:16 -05:00
fwrite(p.data, sizeof(p.data), 1, fopen(argv[2], "wb"));
fclose(f);
2021-01-29 15:14:37 -05:00
return 0;
}