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