This commit is contained in:
neauoire 2021-01-29 13:59:16 -08:00
parent 776c16cc20
commit 588a0f8b92
5 changed files with 71 additions and 21 deletions

View File

@ -50,6 +50,9 @@ VALUE OPCODE EXPLANATION
### Assembly
- `%25`, decimal
- `#25`, hex
```
2 2 + $ef
```

View File

@ -20,4 +20,4 @@ echo "Size: $(du -sk ./uxn)"
# run
./uxnasm program.usm program.rom
# ./uxn program.rom
./uxn program.rom

View File

@ -1 +1 @@
2 2 + .
#12 #34 add

19
uxn.c
View File

@ -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;
}

View File

@ -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;
}