2021-01-29 20:14:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/*
|
2024-03-26 18:02:11 +00:00
|
|
|
Copyright (c) 2021-2024 Devine Lu Linvega, Andrew Alderwick
|
2021-01-29 20:14:37 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2024-03-27 18:41:48 +00:00
|
|
|
#define PAGE 0x0100
|
2021-03-28 18:06:36 +00:00
|
|
|
|
2021-01-29 20:14:37 +00:00
|
|
|
typedef unsigned char Uint8;
|
2021-02-13 00:18:52 +00:00
|
|
|
typedef signed char Sint8;
|
2021-02-04 20:22:08 +00:00
|
|
|
typedef unsigned short Uint16;
|
2021-01-29 20:14:37 +00:00
|
|
|
|
2021-03-14 20:30:17 +00:00
|
|
|
typedef struct {
|
2024-03-27 17:37:03 +00:00
|
|
|
char *name, rune, *content;
|
2021-06-18 04:20:19 +00:00
|
|
|
Uint16 addr, refs;
|
2024-03-27 17:39:34 +00:00
|
|
|
} Item;
|
2021-01-31 05:31:49 +00:00
|
|
|
|
2024-03-27 20:11:17 +00:00
|
|
|
static int ptr, length;
|
2024-03-27 18:33:07 +00:00
|
|
|
static char source[0x40], token[0x40], scope[0x40], sublabel[0x80], lambda[0x05];
|
|
|
|
static char dict[0x10000], *dictnext = dict;
|
|
|
|
static Uint8 data[0x10000], lambda_stack[0x100], lambda_ptr, lambda_len;
|
2024-03-27 18:01:58 +00:00
|
|
|
static Uint16 label_len, refs_len, macro_len;
|
|
|
|
static Item labels[0x400], refs[0x1000], macros[0x100];
|
2021-01-30 22:25:48 +00:00
|
|
|
|
2021-02-02 04:21:27 +00:00
|
|
|
/* clang-format off */
|
|
|
|
|
2024-03-27 04:46:41 +00:00
|
|
|
static char *runes = "|$@&,_.-;=!?#\"%~";
|
|
|
|
static char *hexad = "0123456789abcdef";
|
2021-06-28 21:42:36 +00:00
|
|
|
static char ops[][4] = {
|
2022-04-18 08:45:33 +00:00
|
|
|
"LIT", "INC", "POP", "NIP", "SWP", "ROT", "DUP", "OVR",
|
2021-05-11 18:12:07 +00:00
|
|
|
"EQU", "NEQ", "GTH", "LTH", "JMP", "JCN", "JSR", "STH",
|
2021-05-11 18:14:52 +00:00
|
|
|
"LDZ", "STZ", "LDR", "STR", "LDA", "STA", "DEI", "DEO",
|
2021-03-21 17:24:44 +00:00
|
|
|
"ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
|
2021-02-04 20:22:08 +00:00
|
|
|
};
|
2021-02-02 04:21:27 +00:00
|
|
|
|
2024-03-26 17:34:20 +00:00
|
|
|
static int cndx(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; } /* chr in str */
|
|
|
|
static int sihx(char *s) { char c; while((c = *s++)) if(cndx(hexad, c) < 0) return 0; return 1; } /* str is hex */
|
|
|
|
static int shex(char *s) { int n = 0; char c; while((c = *s++)) { n = n << 4, n |= cndx(hexad, c); } return n; } /* str to num */
|
2024-03-26 17:01:54 +00:00
|
|
|
static int scmp(char *a, char *b, int len) { int i = 0; while(a[i] == b[i]) if(!a[i] || ++i >= len) return 1; return 0; } /* str compare */
|
|
|
|
static int slen(char *s) { int i = 0; while(s[i]) i++; return i; } /* str length */
|
|
|
|
static 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; } /* str copy */
|
2024-03-27 18:41:48 +00:00
|
|
|
static char *scat(char *dst, char *src) { char *o = dst + slen(dst); while(*src) *o++ = *src++; *o = '\0'; return dst; } /* str concat */
|
2024-03-27 18:01:58 +00:00
|
|
|
static char *push(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; } /* save str */
|
2021-01-29 21:59:16 +00:00
|
|
|
|
2024-03-27 04:46:41 +00:00
|
|
|
#define isopcode(x) (findopcode(x) || scmp(x, "BRK", 4))
|
2024-03-27 20:11:17 +00:00
|
|
|
#define writeshort(x) (writebyte(x >> 8, ln) && writebyte(x & 0xff, ln))
|
2024-03-27 17:27:17 +00:00
|
|
|
#define makesublabel(x) push(scat(scat(scpy(scope, sublabel, 0x40), "/"), x), 0)
|
2024-03-27 18:01:58 +00:00
|
|
|
#define findlabel(x) finditem(x, labels, label_len)
|
|
|
|
#define findmacro(x) finditem(x, macros, macro_len)
|
2024-03-27 20:18:42 +00:00
|
|
|
#define error_top(name, msg) !fprintf(stderr, "%s: %s\n", name, msg)
|
2024-03-27 20:23:16 +00:00
|
|
|
#define error_asm(name) !fprintf(stderr, "%s: %s in @%s, %s:%d.\n", name, token, scope, source, *ln)
|
2024-03-27 04:46:41 +00:00
|
|
|
|
2021-02-07 18:21:41 +00:00
|
|
|
/* clang-format on */
|
2021-01-30 22:25:48 +00:00
|
|
|
|
2024-03-27 20:11:17 +00:00
|
|
|
static int parse(char *w, FILE *f, int *ln);
|
2021-11-27 19:33:22 +00:00
|
|
|
|
2024-03-27 17:39:34 +00:00
|
|
|
static Item *
|
2024-03-27 18:01:58 +00:00
|
|
|
finditem(char *name, Item *list, int len)
|
2021-01-31 05:31:49 +00:00
|
|
|
{
|
2021-04-20 04:00:14 +00:00
|
|
|
int i;
|
2024-03-26 03:41:02 +00:00
|
|
|
if(name[0] == '&')
|
2024-03-27 16:37:19 +00:00
|
|
|
name = makesublabel(name + 1);
|
2024-03-27 18:01:58 +00:00
|
|
|
for(i = 0; i < len; i++)
|
2024-03-27 17:47:12 +00:00
|
|
|
if(scmp(list[i].name, name, 0x40))
|
|
|
|
return &list[i];
|
2021-01-31 05:31:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static Uint8
|
2021-02-13 21:21:05 +00:00
|
|
|
findopcode(char *s)
|
2021-02-06 18:39:13 +00:00
|
|
|
{
|
|
|
|
int i;
|
2022-01-04 02:04:09 +00:00
|
|
|
for(i = 0; i < 0x20; i++) {
|
2024-03-25 21:48:37 +00:00
|
|
|
int m = 3;
|
2021-08-29 18:43:00 +00:00
|
|
|
if(!scmp(ops[i], s, 3))
|
2021-02-07 18:21:41 +00:00
|
|
|
continue;
|
2024-03-25 21:48:37 +00:00
|
|
|
if(!i)
|
|
|
|
i |= (1 << 7);
|
|
|
|
while(s[m]) {
|
|
|
|
if(s[m] == '2')
|
|
|
|
i |= (1 << 5);
|
|
|
|
else if(s[m] == 'r')
|
|
|
|
i |= (1 << 6);
|
|
|
|
else if(s[m] == 'k')
|
|
|
|
i |= (1 << 7);
|
2022-04-14 17:24:48 +00:00
|
|
|
else
|
2024-03-25 21:48:37 +00:00
|
|
|
return 0;
|
2022-04-14 17:24:48 +00:00
|
|
|
m++;
|
|
|
|
}
|
|
|
|
return i;
|
2022-04-11 23:07:15 +00:00
|
|
|
}
|
2021-02-06 18:39:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-26 20:53:23 +00:00
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
walkcomment(FILE *f, int *ln)
|
2024-03-26 20:53:23 +00:00
|
|
|
{
|
|
|
|
char c;
|
2024-03-27 18:49:05 +00:00
|
|
|
int depth = 1;
|
2024-03-27 19:11:51 +00:00
|
|
|
while(f && fread(&c, 1, 1, f)) {
|
2024-03-27 20:34:44 +00:00
|
|
|
if(c == 0xa) *ln += 1;
|
2024-03-27 18:49:05 +00:00
|
|
|
if(c == '(') depth++;
|
|
|
|
if(c == ')' && --depth < 1) return 1;
|
2024-03-27 18:33:07 +00:00
|
|
|
}
|
2024-03-26 20:53:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-27 19:05:19 +00:00
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
walkmacro(Item *m, int *ln)
|
2024-03-27 19:05:19 +00:00
|
|
|
{
|
|
|
|
char c, *contentptr = m->content, *cptr = token;
|
|
|
|
while((c = *contentptr++)) {
|
|
|
|
if(c < 0x21) {
|
|
|
|
*cptr++ = 0x00;
|
2024-03-27 20:11:17 +00:00
|
|
|
if(token[0] && !parse(token, NULL, ln)) return 0;
|
2024-03-27 19:05:19 +00:00
|
|
|
cptr = token;
|
|
|
|
} else
|
|
|
|
*cptr++ = c;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
walkfile(FILE *f, int *ln)
|
2024-03-27 19:05:19 +00:00
|
|
|
{
|
|
|
|
char c, *cptr = token;
|
2024-03-27 19:11:51 +00:00
|
|
|
while(f && fread(&c, 1, 1, f)) {
|
2024-03-27 20:34:44 +00:00
|
|
|
if(c == 0xa) *ln += 1;
|
2024-03-27 19:05:19 +00:00
|
|
|
if(c < 0x21) {
|
|
|
|
*cptr++ = 0x00;
|
2024-03-27 20:11:17 +00:00
|
|
|
if(token[0] && !parse(token, f, ln))
|
2024-03-27 19:05:19 +00:00
|
|
|
return 0;
|
|
|
|
cptr = token;
|
|
|
|
} else if(cptr - token < 0x3f)
|
|
|
|
*cptr++ = c;
|
|
|
|
else
|
|
|
|
return error_asm("Token too long");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
makemacro(char *name, FILE *f, int *ln)
|
2021-03-14 20:30:17 +00:00
|
|
|
{
|
2024-03-27 19:05:19 +00:00
|
|
|
char c;
|
2024-03-27 19:11:51 +00:00
|
|
|
Item *m;
|
2024-03-25 22:20:43 +00:00
|
|
|
if(!slen(name)) return error_asm("Macro is empty");
|
|
|
|
if(findmacro(name)) return error_asm("Macro is duplicate");
|
|
|
|
if(sihx(name)) return error_asm("Macro is hex number");
|
|
|
|
if(isopcode(name)) return error_asm("Macro is opcode");
|
2024-03-27 18:01:58 +00:00
|
|
|
if(macro_len == 0x100) return error_asm("Macros limit exceeded");
|
|
|
|
m = ¯os[macro_len++];
|
2024-03-27 17:27:17 +00:00
|
|
|
m->name = push(name, 0);
|
2024-03-27 18:01:58 +00:00
|
|
|
m->content = dictnext;
|
2024-03-27 19:11:51 +00:00
|
|
|
while(f && fread(&c, 1, 1, f) && c != '{')
|
2024-03-27 20:34:44 +00:00
|
|
|
if(c == 0xa) *ln += 1;
|
2024-03-27 19:11:51 +00:00
|
|
|
while(f && fread(&c, 1, 1, f) && c != '}') {
|
2024-03-27 20:34:44 +00:00
|
|
|
if(c == 0xa) *ln += 1;
|
2024-03-27 19:03:24 +00:00
|
|
|
if(c == '%') return 0;
|
2024-03-27 20:11:17 +00:00
|
|
|
if(c == '(') walkcomment(f, ln);
|
2024-03-27 19:03:24 +00:00
|
|
|
*dictnext++ = c;
|
2021-03-14 20:30:17 +00:00
|
|
|
}
|
2024-03-27 18:01:58 +00:00
|
|
|
*dictnext++ = 0;
|
2021-03-14 20:30:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
makelabel(char *name, int setscope, int *ln)
|
2021-02-04 21:49:03 +00:00
|
|
|
{
|
2024-03-27 17:39:34 +00:00
|
|
|
Item *l;
|
2024-03-26 18:10:08 +00:00
|
|
|
if(name[0] == '&')
|
2024-03-27 16:37:19 +00:00
|
|
|
name = makesublabel(name + 1);
|
2024-03-25 22:20:43 +00:00
|
|
|
if(!slen(name)) return error_asm("Label is empty");
|
|
|
|
if(findlabel(name)) return error_asm("Label is duplicate");
|
|
|
|
if(sihx(name)) return error_asm("Label is hex number");
|
|
|
|
if(isopcode(name)) return error_asm("Label is opcode");
|
2024-03-26 17:34:20 +00:00
|
|
|
if(cndx(runes, name[0]) >= 0) return error_asm("Label name is runic");
|
2024-03-27 18:01:58 +00:00
|
|
|
if(label_len == 0x400) return error_asm("Labels limit exceeded");
|
|
|
|
l = &labels[label_len++];
|
2024-03-27 18:33:07 +00:00
|
|
|
l->name = push(name, 0);
|
2024-03-27 18:01:58 +00:00
|
|
|
l->addr = ptr;
|
2021-03-01 17:16:40 +00:00
|
|
|
l->refs = 0;
|
2024-03-26 20:31:11 +00:00
|
|
|
if(setscope) {
|
|
|
|
int i = 0;
|
|
|
|
while(name[i] != '/' && i < 0x3e && (scope[i] = name[i])) i++;
|
|
|
|
scope[i] = '\0';
|
|
|
|
}
|
2021-02-04 21:49:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-08 23:46:35 +00:00
|
|
|
static char *
|
|
|
|
makelambda(int id)
|
|
|
|
{
|
2024-03-26 18:27:45 +00:00
|
|
|
lambda[0] = (char)0xce;
|
|
|
|
lambda[1] = (char)0xbb;
|
|
|
|
lambda[2] = hexad[id >> 0x4];
|
|
|
|
lambda[3] = hexad[id & 0xf];
|
|
|
|
return lambda;
|
2023-08-08 23:46:35 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 03:30:55 +00:00
|
|
|
static int
|
|
|
|
makepad(char *w)
|
|
|
|
{
|
2024-03-27 17:39:34 +00:00
|
|
|
Item *l;
|
2024-03-27 18:01:58 +00:00
|
|
|
int rel = w[0] == '$' ? ptr : 0;
|
2024-03-26 03:30:55 +00:00
|
|
|
if(sihx(w + 1))
|
2024-03-27 18:33:07 +00:00
|
|
|
return ptr = shex(w + 1) + rel;
|
|
|
|
if((l = findlabel(w + 1)))
|
|
|
|
return ptr = l->addr + rel;
|
|
|
|
return 0;
|
2024-03-26 03:30:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 22:44:28 +00:00
|
|
|
static int
|
2024-03-26 18:46:15 +00:00
|
|
|
addref(char *label, char rune, Uint16 addr)
|
2021-11-27 22:44:28 +00:00
|
|
|
{
|
2024-03-27 17:39:34 +00:00
|
|
|
Item *r;
|
2024-03-27 18:01:58 +00:00
|
|
|
if(refs_len >= 0x1000)
|
2024-03-27 20:11:17 +00:00
|
|
|
return error_top("References limit exceeded", label);
|
2024-03-27 18:01:58 +00:00
|
|
|
r = &refs[refs_len++];
|
2023-08-08 23:46:35 +00:00
|
|
|
if(label[0] == '{') {
|
2024-03-27 18:01:58 +00:00
|
|
|
lambda_stack[lambda_ptr++] = lambda_len;
|
|
|
|
r->name = push(makelambda(lambda_len++), 0);
|
2024-02-26 00:06:54 +00:00
|
|
|
} else if(label[0] == '&' || label[0] == '/') {
|
2024-03-27 16:37:19 +00:00
|
|
|
r->name = makesublabel(label + 1);
|
2024-03-26 18:51:48 +00:00
|
|
|
} else
|
2024-03-27 17:27:17 +00:00
|
|
|
r->name = push(label, 0);
|
2023-03-01 05:47:45 +00:00
|
|
|
r->rune = rune;
|
2021-11-27 22:44:28 +00:00
|
|
|
r->addr = addr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-02-19 00:26:55 +00:00
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
writebyte(Uint8 b, int *ln)
|
2021-02-15 22:04:58 +00:00
|
|
|
{
|
2024-03-27 18:41:48 +00:00
|
|
|
if(ptr < PAGE)
|
2024-03-25 22:20:43 +00:00
|
|
|
return error_asm("Writing in zero-page");
|
2024-03-27 18:01:58 +00:00
|
|
|
else if(ptr >= 0x10000)
|
2024-03-25 22:20:43 +00:00
|
|
|
return error_asm("Writing outside memory");
|
2024-03-27 18:01:58 +00:00
|
|
|
else if(ptr < length)
|
2024-03-25 22:20:43 +00:00
|
|
|
return error_asm("Writing rewind");
|
2024-03-27 18:01:58 +00:00
|
|
|
data[ptr++] = b;
|
|
|
|
length = ptr;
|
2022-02-19 00:26:55 +00:00
|
|
|
return 1;
|
2021-02-15 22:04:58 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 19:40:43 +00:00
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
writehex(char *w, int *ln)
|
2024-03-26 19:40:43 +00:00
|
|
|
{
|
2024-03-26 19:53:06 +00:00
|
|
|
if(*w == '#')
|
2024-03-27 20:11:17 +00:00
|
|
|
writebyte(findopcode("LIT") | (slen(++w) > 2) << 5, ln);
|
2024-03-26 19:53:06 +00:00
|
|
|
if(slen(w) == 2)
|
2024-03-27 20:11:17 +00:00
|
|
|
return writebyte(shex(w), ln);
|
2024-03-26 19:53:06 +00:00
|
|
|
else if(slen(w) == 4)
|
|
|
|
return writeshort(shex(w));
|
|
|
|
else
|
|
|
|
return 0;
|
2024-03-26 19:40:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 22:07:25 +00:00
|
|
|
static int
|
2024-03-26 20:53:23 +00:00
|
|
|
makeinclude(char *filename)
|
2021-11-27 22:07:25 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2024-03-27 20:11:17 +00:00
|
|
|
int res = 0, ln = 0;
|
2024-03-26 18:19:01 +00:00
|
|
|
if(!(f = fopen(filename, "r")))
|
2024-03-26 17:58:55 +00:00
|
|
|
return error_top("Invalid source", filename);
|
2024-03-26 18:33:25 +00:00
|
|
|
scpy(filename, source, 0x40);
|
2024-03-27 20:11:17 +00:00
|
|
|
res = walkfile(f, &ln);
|
2021-11-27 22:07:25 +00:00
|
|
|
fclose(f);
|
2024-03-26 17:58:55 +00:00
|
|
|
return res;
|
2021-11-27 22:07:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static int
|
2024-03-27 20:11:17 +00:00
|
|
|
parse(char *w, FILE *f, int *ln)
|
2021-01-29 20:14:37 +00:00
|
|
|
{
|
2024-03-26 20:35:37 +00:00
|
|
|
char c;
|
2024-03-27 17:39:34 +00:00
|
|
|
Item *m;
|
2021-11-27 19:33:22 +00:00
|
|
|
switch(w[0]) {
|
2024-03-27 20:11:17 +00:00
|
|
|
case '(': return !walkcomment(f, ln) ? error_asm("Invalid comment") : 1;
|
2024-03-26 20:53:23 +00:00
|
|
|
case '~': return !makeinclude(w + 1) ? error_asm("Invalid include") : 1;
|
2024-03-27 20:11:17 +00:00
|
|
|
case '%': return !makemacro(w + 1, f, ln) ? error_asm("Invalid macro") : 1;
|
|
|
|
case '@': return !makelabel(w + 1, 1, ln) ? error_asm("Invalid label") : 1;
|
|
|
|
case '&': return !makelabel(w, 0, ln) ? error_asm("Invalid sublabel") : 1;
|
|
|
|
case '#': return !sihx(w + 1) || !writehex(w, ln) ? error_asm("Invalid hexadecimal") : 1;
|
|
|
|
case '_': return addref(w + 1, w[0], ptr) && writebyte(0xff, ln);
|
|
|
|
case ',': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ln) && writebyte(0xff, ln);
|
|
|
|
case '-': return addref(w + 1, w[0], ptr) && writebyte(0xff, ln);
|
|
|
|
case '.': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT"), ln) && writebyte(0xff, ln);
|
2024-03-26 20:11:07 +00:00
|
|
|
case ':': fprintf(stderr, "Deprecated rune %s, use =%s\n", w, w + 1); /* fall-through */
|
2024-03-27 18:01:58 +00:00
|
|
|
case '=': return addref(w + 1, w[0], ptr) && writeshort(0xffff);
|
2024-03-27 20:11:17 +00:00
|
|
|
case ';': return addref(w + 1, w[0], ptr + 1) && writebyte(findopcode("LIT2"), ln) && writeshort(0xffff);
|
|
|
|
case '?': return addref(w + 1, w[0], ptr + 1) && writebyte(0x20, ln) && writeshort(0xffff);
|
|
|
|
case '!': return addref(w + 1, w[0], ptr + 1) && writebyte(0x40, ln) && writeshort(0xffff);
|
|
|
|
case '}': return !makelabel(makelambda(lambda_stack[--lambda_ptr]), 0, ln) ? error_asm("Invalid label") : 1;
|
2024-03-26 20:11:07 +00:00
|
|
|
case '$':
|
|
|
|
case '|': return !makepad(w) ? error_asm("Invalid padding") : 1;
|
2022-06-06 14:57:29 +00:00
|
|
|
case '[':
|
|
|
|
case ']':
|
2022-06-07 18:39:43 +00:00
|
|
|
if(slen(w) == 1) break; /* else fallthrough */
|
2024-03-26 20:11:07 +00:00
|
|
|
case '"': /* raw string */
|
|
|
|
while((c = *(++w)))
|
2024-03-27 20:11:17 +00:00
|
|
|
if(!writebyte(c, ln)) return 0;
|
2024-03-26 20:11:07 +00:00
|
|
|
break;
|
2021-11-27 19:33:22 +00:00
|
|
|
default:
|
2024-03-26 20:11:07 +00:00
|
|
|
if(sihx(w))
|
2024-03-27 20:11:17 +00:00
|
|
|
return writehex(w, ln);
|
2024-03-26 20:11:07 +00:00
|
|
|
else if(isopcode(w))
|
2024-03-27 20:11:17 +00:00
|
|
|
return writebyte(findopcode(w), ln);
|
2024-03-27 17:18:06 +00:00
|
|
|
else if((m = findmacro(w)))
|
2024-03-27 20:11:17 +00:00
|
|
|
return walkmacro(m, ln);
|
2024-03-27 17:18:06 +00:00
|
|
|
else
|
2024-03-27 20:11:17 +00:00
|
|
|
return addref(w, ' ', ptr + 1) && writebyte(0x60, ln) && writeshort(0xffff);
|
2021-01-30 22:25:48 +00:00
|
|
|
}
|
2021-02-04 20:22:08 +00:00
|
|
|
return 1;
|
2021-01-30 22:25:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static int
|
2021-11-27 19:33:22 +00:00
|
|
|
resolve(void)
|
2021-01-30 22:25:48 +00:00
|
|
|
{
|
2024-03-27 17:39:34 +00:00
|
|
|
Item *l;
|
2021-11-27 19:33:22 +00:00
|
|
|
int i;
|
2023-01-12 13:22:21 +00:00
|
|
|
Uint16 a;
|
2024-03-27 18:01:58 +00:00
|
|
|
for(i = 0; i < refs_len; i++) {
|
|
|
|
Item *r = &refs[i];
|
|
|
|
Uint8 *rom = data + r->addr;
|
2021-11-27 19:33:22 +00:00
|
|
|
switch(r->rune) {
|
2022-11-11 04:54:53 +00:00
|
|
|
case '_':
|
2023-01-12 13:15:07 +00:00
|
|
|
case ',':
|
2021-11-27 19:33:22 +00:00
|
|
|
if(!(l = findlabel(r->name)))
|
2024-03-08 05:00:42 +00:00
|
|
|
return error_top("Unknown relative reference", r->name);
|
2024-03-26 20:03:47 +00:00
|
|
|
*rom = (Sint8)(l->addr - r->addr - 2);
|
2024-03-27 18:01:58 +00:00
|
|
|
if((Sint8)data[r->addr] != (l->addr - r->addr - 2))
|
2024-03-25 22:20:43 +00:00
|
|
|
return error_top("Relative reference is too far", r->name);
|
2021-11-27 19:33:22 +00:00
|
|
|
l->refs++;
|
|
|
|
break;
|
2022-11-11 04:54:53 +00:00
|
|
|
case '-':
|
|
|
|
case '.':
|
|
|
|
if(!(l = findlabel(r->name)))
|
2024-03-08 05:00:42 +00:00
|
|
|
return error_top("Unknown zero-page reference", r->name);
|
2024-03-27 04:11:31 +00:00
|
|
|
*rom = l->addr;
|
2021-11-27 19:33:22 +00:00
|
|
|
l->refs++;
|
|
|
|
break;
|
2024-03-03 23:31:37 +00:00
|
|
|
case ':':
|
2022-11-10 00:40:07 +00:00
|
|
|
case '=':
|
2023-01-12 13:15:07 +00:00
|
|
|
case ';':
|
2023-01-12 13:22:21 +00:00
|
|
|
if(!(l = findlabel(r->name)))
|
2024-03-08 05:00:42 +00:00
|
|
|
return error_top("Unknown absolute reference", r->name);
|
2024-03-27 04:11:31 +00:00
|
|
|
*rom++ = l->addr >> 8, *rom = l->addr;
|
2023-01-12 13:22:21 +00:00
|
|
|
l->refs++;
|
|
|
|
break;
|
2023-01-12 13:15:07 +00:00
|
|
|
case '?':
|
|
|
|
case '!':
|
|
|
|
default:
|
2021-11-27 19:33:22 +00:00
|
|
|
if(!(l = findlabel(r->name)))
|
2024-03-26 20:03:47 +00:00
|
|
|
return error_top("Unknown subroutine reference", r->name);
|
2023-01-12 13:22:21 +00:00
|
|
|
a = l->addr - r->addr - 2;
|
2024-03-27 04:11:31 +00:00
|
|
|
*rom++ = a >> 8, *rom = a;
|
2021-11-27 19:33:22 +00:00
|
|
|
l->refs++;
|
|
|
|
break;
|
2021-03-11 23:47:28 +00:00
|
|
|
}
|
2021-01-29 20:14:37 +00:00
|
|
|
}
|
2021-02-04 20:22:08 +00:00
|
|
|
return 1;
|
2021-01-29 20:14:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 22:07:25 +00:00
|
|
|
static void
|
|
|
|
review(char *filename)
|
|
|
|
{
|
|
|
|
int i;
|
2024-03-27 18:01:58 +00:00
|
|
|
for(i = 0; i < label_len; i++)
|
|
|
|
if(labels[i].name[0] - 'A' > 25 && !labels[i].refs)
|
|
|
|
fprintf(stdout, "-- Unused label: %s\n", labels[i].name);
|
2024-03-08 05:00:42 +00:00
|
|
|
fprintf(stdout,
|
2021-11-29 16:48:12 +00:00
|
|
|
"Assembled %s in %d bytes(%.2f%% used), %d labels, %d macros.\n",
|
|
|
|
filename,
|
2024-03-27 18:41:48 +00:00
|
|
|
length - PAGE,
|
|
|
|
(length - PAGE) / 652.80,
|
2024-03-27 18:01:58 +00:00
|
|
|
label_len,
|
|
|
|
macro_len);
|
2021-11-27 22:07:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 20:30:04 +00:00
|
|
|
static void
|
|
|
|
writesym(char *filename)
|
|
|
|
{
|
2023-01-01 21:40:58 +00:00
|
|
|
int i;
|
2022-12-10 19:07:45 +00:00
|
|
|
char symdst[0x60];
|
2022-12-11 03:54:58 +00:00
|
|
|
FILE *fp;
|
2022-12-10 19:07:45 +00:00
|
|
|
if(slen(filename) > 0x60 - 5)
|
|
|
|
return;
|
|
|
|
fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
|
2022-12-09 20:30:04 +00:00
|
|
|
if(fp != NULL) {
|
2024-03-27 18:01:58 +00:00
|
|
|
for(i = 0; i < label_len; i++) {
|
|
|
|
Uint8 hb = labels[i].addr >> 8, lb = labels[i].addr;
|
2023-01-12 16:40:33 +00:00
|
|
|
fwrite(&hb, 1, 1, fp);
|
|
|
|
fwrite(&lb, 1, 1, fp);
|
2024-03-27 18:01:58 +00:00
|
|
|
fwrite(labels[i].name, slen(labels[i].name) + 1, 1, fp);
|
2022-12-09 20:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:14:37 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2024-03-26 17:58:55 +00:00
|
|
|
FILE *dst;
|
2024-03-27 18:41:48 +00:00
|
|
|
ptr = PAGE;
|
2024-03-26 18:22:02 +00:00
|
|
|
scpy("on-reset", scope, 0x40);
|
2024-03-25 22:27:45 +00:00
|
|
|
if(argc == 1) return error_top("usage", "uxnasm [-v] input.tal output.rom");
|
2024-03-26 17:01:54 +00:00
|
|
|
if(scmp(argv[1], "-v", 2)) return !fprintf(stdout, "Uxnasm - Uxntal Assembler, 26 Mar 2024.\n");
|
2024-03-26 20:53:23 +00:00
|
|
|
if(!makeinclude(argv[1]) || !resolve()) return !error_top("Assembly", "Failed to assemble rom.");
|
2024-03-25 22:27:45 +00:00
|
|
|
if(!(dst = fopen(argv[2], "wb"))) return !error_top("Invalid Output", argv[2]);
|
2024-03-27 18:41:48 +00:00
|
|
|
if(length <= PAGE) return !error_top("Assembly", "Output rom is empty.");
|
2024-03-25 22:27:45 +00:00
|
|
|
review(argv[2]);
|
2024-03-27 18:41:48 +00:00
|
|
|
fwrite(data + PAGE, length - PAGE, 1, dst);
|
2024-03-25 22:27:45 +00:00
|
|
|
writesym(argv[2]);
|
2021-01-29 20:14:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|