diff --git a/README.md b/README.md index fe58e50..fcbb43b 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ VALUE OPCODE EXPLANATION ### Assembly +- `%25`, decimal +- `#25`, hex + ``` 2 2 + $ef ``` diff --git a/build.sh b/build.sh index edc664c..378eef4 100755 --- a/build.sh +++ b/build.sh @@ -20,4 +20,4 @@ echo "Size: $(du -sk ./uxn)" # run ./uxnasm program.usm program.rom -# ./uxn program.rom +./uxn program.rom diff --git a/program.usm b/program.usm index 0ad5dda..4af9ba3 100644 --- a/program.usm +++ b/program.usm @@ -1 +1 @@ -2 2 + . \ No newline at end of file +#12 #34 add \ No newline at end of file diff --git a/uxn.c b/uxn.c index b788bb3..6c88ec0 100644 --- a/uxn.c +++ b/uxn.c @@ -15,6 +15,7 @@ WITH REGARD TO THIS SOFTWARE. #define ECHO 1 typedef unsigned char Uint8; +typedef unsigned char Uint16; typedef struct { @@ -23,12 +24,13 @@ typedef struct { Uint8 sptr; Uint8 stack[STACK_DEPTH]; Uint8 address[STACK_DEPTH]; -Uint8 memory[STACK_DEPTH]; +Uint16 memory[STACK_DEPTH]; void -stackprint(Uint8 *s, Uint8 len) +echo(Uint8 *s, Uint8 len, char *name) { int i; + printf("%s\n", name); for(i = 0; i < len; ++i) { if(i % 16 == 0) printf("\n"); @@ -65,13 +67,12 @@ disk(Computer *cpu, FILE *f) reset(cpu); if(!fread(buffer, sizeof(buffer), 1, f)) return 0; - /* - + for(i = 0; i < 128; i++) { cpu->memory[i * 2] |= (buffer[i] >> 8) & 0xFF; cpu->memory[i * 2 + 1] |= buffer[i] & 0xFF; } - */ + return 1; } @@ -92,12 +93,8 @@ main(int argc, char *argv[]) if(!disk(&cpu, f)) return error("Unreadable input."); run(&cpu, ECHO); - /* program */ - op_push(stack, 0xef); - op_pop(stack); - op_push(stack, 0x02); - op_push(stack, 0x03); /* print result */ - stackprint(stack, 0x40); + echo(stack, 0x40, "stack"); + echo(memory, 0x40, "memory"); return 0; } diff --git a/uxnasm.c b/uxnasm.c index c0ca6ff..c3a6b5d 100644 --- a/uxnasm.c +++ b/uxnasm.c @@ -11,20 +11,69 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -#define BUFLEN 256 +#define PRGLEN 256 typedef unsigned char Uint8; -typedef unsigned char Uint16; +typedef unsigned short Uint16; -unsigned short program[BUFLEN]; +typedef struct { + int ptr; + Uint16 data[PRGLEN]; +} Program; + +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; +} + +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'); + return n; +} + +#pragma mark - Helpers + +Uint8 +getopcode(char *s) +{ + if(scmp(s, "add")) { + return 0x01; + } + return 0; +} void pass1(FILE *f) { - int instrid = 0; - char line[BUFLEN]; - while(fgets(line, BUFLEN, f)) { - printf("%s\n", line); + char word[64]; + while(fscanf(f, "%s", word) == 1) { + int lit = 0, val = 0; + if(word[0] == '#') { + lit = 0; + val = shex(word + 1); + } else { + lit = 1; + val = getopcode(word); + } + printf("#%d -> %s[%02x %02x]\n", p.ptr, word, lit, val); + p.data[p.ptr++] = (val << 8) + (lit & 0xff); } } @@ -44,6 +93,7 @@ main(int argc, char *argv[]) if(!(f = fopen(argv[1], "r"))) return error("Missing input."); pass1(f); - fwrite(program, sizeof(program), 1, fopen(argv[2], "wb")); + fwrite(p.data, sizeof(p.data), 1, fopen(argv[2], "wb")); + fclose(f); return 0; }