Started migrating to new assembler

This commit is contained in:
neauoire 2021-04-19 17:23:30 -07:00
parent d61e724d48
commit 82f7103a55
6 changed files with 77 additions and 77 deletions

View File

@ -32,7 +32,7 @@ else
fi
echo "Assembling.."
./bin/assembler projects/software/nasu.usm bin/boot.rom
./bin/assembler projects/examples/dev.controller.buttons.usm bin/boot.rom
echo "Running.."
if [ "${2}" = '--cli' ];

View File

@ -6,60 +6,87 @@
( variables )
;slime { color 1 }
@slime [
&color 00
&end
]
( devices )
|0100 ;System { vector 2 pad 6 r 2 g 2 b 2 }
|0120 ;Screen { vector 2 width 2 height 2 pad 2 x 2 y 2 addr 2 color 1 }
|0140 ;Controller { vector 2 button 1 key 1 }
|0100 @System [
&vector 0000
&pad 0000
0000
0000
&r 0000
&g 0000
&b 0000
&end
]
|0120 @Screen [
&vector 0000
&width 0000
&height 0000
&pad 0000
&x 0000
&y 0000
&addr 0000
&color 00
&end
]
|0140 @Controller [
&vector 0000
&button 00
&key 00
&end
]
|0200
( theme ) #0daf =System.r #02ff =System.g #035f =System.b
( vectors ) ,FRAME =Screen.vector
( theme ) #0daf =System/r #02ff =System/g #035f =System/b
( vectors ) ,on-frame =Screen/vector
( set origin )
~Screen.width 2/ =Screen.x
~Screen.height 2/ =Screen.y
,default_icn =Screen.addr
#31 =Screen.color
#2a =slime
~Screen/width 2/ =Screen/x
~Screen/height 2/ =Screen/y
,default_icn =Screen/addr
#31 =Screen/color
#2a =slime/color
BRK
@FRAME
@on-frame
#2a =slime
,default_icn =Screen.addr
#2a =slime/color
,default_icn =Screen/addr
( hold ctrl key to change slime color )
~Controller.button #0f AND
DUP #01 NEQ ^$no-ctrl JNZ #25 =slime $no-ctrl
DUP #02 NEQ ^$no-alt JNZ #2f =slime $no-alt
~Controller/button #0f AND
DUP #01 NEQ ^$no-ctrl JNZ #25 =slime/color $no-ctrl
DUP #02 NEQ ^$no-alt JNZ #2f =slime/color $no-alt
POP
( clear ) #30 =Screen.color
( clear ) #30 =Screen/color
( detect movement )
~Controller.button #f0 AND
~Controller/button #f0 AND
DUP #04 SFT #01 AND #01 NEQ ^$no-up JNZ
( move ) ~Screen.y -- =Screen.y ,up_icn =Screen.addr $no-up
( move ) ~Screen/y -- =Screen/y ,up_icn =Screen/addr $no-up
DUP #05 SFT #01 AND #01 NEQ ^$no-down JNZ
( move ) ~Screen.y ++ =Screen.y ,down_icn =Screen.addr $no-down
( move ) ~Screen/y ++ =Screen/y ,down_icn =Screen/addr $no-down
DUP #06 SFT #01 AND #01 NEQ ^$no-left JNZ
( move ) ~Screen.x -- =Screen.x ,left_icn =Screen.addr $no-left
( move ) ~Screen/x -- =Screen/x ,left_icn =Screen/addr $no-left
DUP #07 SFT #01 AND #01 NEQ ^$no-right JNZ
( move ) ~Screen.x ++ =Screen.x ,right_icn =Screen.addr $no-right
( move ) ~Screen/x ++ =Screen/x ,right_icn =Screen/addr $no-right
POP
( draw face )
#31 =Screen.color
#31 =Screen/color
( draw slime )
,slime_icn =Screen.addr
~slime =Screen.color
,slime_icn =Screen/addr
~slime/color =Screen/color
BRK

BIN
src/assembler Executable file

Binary file not shown.

View File

@ -13,7 +13,6 @@ WITH REGARD TO THIS SOFTWARE.
#define WORDLENMAX 32
#define MACROMAX 64
#define OFFSET 0x0200
typedef unsigned char Uint8;
typedef signed char Sint8;
@ -132,22 +131,6 @@ findlabeladdr(char *s)
return 0;
}
Uint8
findlabellen(char *s)
{
int i;
char *param;
Label *l = findlabel(s);
if(scin(s, '.') < 1)
return l->map[0].size;
param = s + scin(s, '.') + 1;
for(i = 0; i < l->maps; ++i)
if(scmp(l->map[i].name, param, 64))
return l->map[i].size;
printf("!!! Warning %s.%s\n", l->name, param);
return 0;
}
Uint8
findopcode(char *s)
{
@ -175,7 +158,7 @@ char *
sublabel(char *src, char *scope, char *name)
{
scpy(scope, src, 64);
scpy("-", src + slen(src), 64);
scpy("/", src + slen(src), 64);
scpy(name, src + slen(src), 64);
return src;
}
@ -225,6 +208,13 @@ makelabel(char *name, Uint16 addr)
return error("Label name is hex number", name);
if(findopcode(name))
return error("Label name is invalid", name);
/* set length of last label */
if(p.llen) {
l = &p.labels[p.llen - 1];
l->len = addr - l->addr;
printf(" Set length of #%d \n", l->len);
}
/* make new label */
l = &p.labels[p.llen++];
l->addr = addr;
l->refs = 0;
@ -233,25 +223,6 @@ makelabel(char *name, Uint16 addr)
return 1;
}
int
makevariable(char *name, Uint16 *addr, FILE *f)
{
Label *l;
char word[64];
if(!makelabel(name, *addr))
return error("Could not create variable", name);
l = findlabel(name);
while(fscanf(f, "%s", word)) {
if(word[0] == '{') continue;
if(word[0] == '}') break;
scpy(word, l->map[l->maps].name, 64);
fscanf(f, "%02x", &l->map[l->maps].size);
*addr += l->map[l->maps].size;
l->len += l->map[l->maps++].size;
}
return 1;
}
int
skipblock(char *w, int *cap, char a, char b)
{
@ -303,25 +274,25 @@ parsetoken(char *w)
pushbyte((Sint8)(l->addr - p.ptr - 3), 1);
return ++l->refs;
} else if(w[0] == '=' && (l = findlabel(w + 1))) {
if(!findlabellen(w + 1) || findlabellen(w + 1) > 2)
if(!l->len || l->len > 2)
return error("Invalid store helper", w);
if(findlabeladdr(w + 1) < 0x0100) {
pushbyte(findlabeladdr(w + 1), 1);
pushbyte(findopcode(findlabellen(w + 1) == 2 ? "STR" : "POK"), 0);
pushbyte(findopcode(l->len == 2 ? "STR" : "POK"), 0);
} else {
pushshort(findlabeladdr(w + 1), 1);
pushbyte(findopcode(findlabellen(w + 1) == 2 ? "STR2" : "POK2"), 0);
pushbyte(findopcode(l->len == 2 ? "STR2" : "POK2"), 0);
}
return ++l->refs;
} else if(w[0] == '~' && (l = findlabel(w + 1))) {
if(!findlabellen(w + 1) || findlabellen(w + 1) > 2)
if(!l->len || l->len > 2)
return error("Invalid load helper", w);
if(findlabeladdr(w + 1) < 0x0100) {
pushbyte(findlabeladdr(w + 1), 1);
pushbyte(findopcode(findlabellen(w + 1) == 2 ? "LDR" : "PEK"), 0);
pushbyte(findopcode(l->len == 2 ? "LDR" : "PEK"), 0);
} else {
pushshort(findlabeladdr(w + 1), 1);
pushbyte(findopcode(findlabellen(w + 1) == 2 ? "LDR2" : "PEK2"), 0);
pushbyte(findopcode(l->len == 2 ? "LDR2" : "PEK2"), 0);
}
return ++l->refs;
} else if(w[0] == '.' && (l = findlabel(w + 1))) {
@ -363,7 +334,10 @@ pass1(FILE *f)
printf("Pass 1\n");
while(fscanf(f, "%s", w) == 1) {
if(skipblock(w, &ccmnt, '(', ')')) continue;
if(skipblock(w, &cbits, '[', ']')) {
if(w[0] == '&') {
if(!makelabel(sublabel(subw, scope, w + 1), addr))
return error("Pass1 failed", w);
} else if(skipblock(w, &cbits, '[', ']')) {
if(w[0] == '[' || w[0] == ']')
continue;
if(slen(w) == 4 && sihx(w))
@ -383,9 +357,6 @@ pass1(FILE *f)
} else if(w[0] == '$') {
if(!makelabel(sublabel(subw, scope, w + 1), addr))
return error("Pass1 failed", w);
} else if(w[0] == ';') {
if(!makevariable(w + 1, &addr, f))
return error("Pass1 failed", w);
} else if(w[0] == '|') {
if(shex(w + 1) < addr)
return error("Memory Overwrite", w);
@ -406,6 +377,7 @@ pass2(FILE *f)
while(fscanf(f, "%s", w) == 1) {
if(w[0] == '$') continue;
if(w[0] == '%') continue;
if(w[0] == '&') continue;
if(skipblock(w, &ccmnt, '(', ')')) continue;
if(skipblock(w, &ctemplate, '{', '}')) continue;
if(w[0] == ';') {
@ -440,7 +412,7 @@ void
cleanup(char *filename)
{
int i;
printf("Assembled %s(%0.2fkb), %d labels, %d macros.\n\n", filename, (p.ptr - OFFSET) / 1000.0, p.llen, p.mlen);
printf("Assembled %s(%0.2fkb), %d labels, %d macros.\n\n", filename, p.ptr / 1000.0, p.llen, p.mlen);
for(i = 0; i < p.llen; ++i)
if(!p.labels[i].refs)
printf("--- Unused label: %s\n", p.labels[i].name);
@ -459,7 +431,7 @@ main(int argc, char *argv[])
return !error("Open", "Failed");
if(!pass1(f) || !pass2(f))
return !error("Assembly", "Failed");
fwrite(p.data + OFFSET, p.ptr - OFFSET, 1, fopen(argv[2], "wb"));
fwrite(p.data, p.ptr, 1, fopen(argv[2], "wb"));
fclose(f);
cleanup(argv[2]);
return 0;

View File

@ -166,7 +166,7 @@ loaduxn(Uxn *u, char *filepath)
FILE *f;
if(!(f = fopen(filepath, "rb")))
return haltuxn(u, "Missing input rom.", 0);
fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f);
fread(u->ram.dat + LOAD_OFFSET, sizeof(u->ram.dat) - LOAD_OFFSET, 1, f);
printf("Uxn loaded[%s].\n", filepath);
return 1;
}

View File

@ -18,6 +18,7 @@ typedef signed short Sint16;
#define PAGE_DEVICE 0x0100
#define PAGE_PROGRAM 0x0200
#define LOAD_OFFSET 0x0000
typedef struct {
Uint8 ptr, error;