2021-01-29 15:14:37 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright (c) 2021 Devine Lu Linvega
|
|
|
|
|
|
|
|
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-01-29 16:59:16 -05:00
|
|
|
#define PRGLEN 256
|
2021-01-29 15:14:37 -05:00
|
|
|
|
|
|
|
typedef unsigned char Uint8;
|
|
|
|
|
2021-01-29 16:59:16 -05:00
|
|
|
typedef struct {
|
|
|
|
int ptr;
|
2021-01-29 20:49:10 -05:00
|
|
|
Uint8 data[PRGLEN];
|
2021-01-29 16:59:16 -05:00
|
|
|
} Program;
|
|
|
|
|
2021-01-29 20:49:10 -05:00
|
|
|
char opcodes[][4] = {"BRK", "LIT", "DUP", "DRP", "SWP", "SLP", "PSH", "POP", "JMP", "JSR", "RST", "BEQ", "EQU", "NEQ", "LTH", "GTH"};
|
|
|
|
|
2021-01-29 16:59:16 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:49:10 -05:00
|
|
|
char *
|
|
|
|
suca(char *s) /* string to uppercase */
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char c;
|
|
|
|
while((c = s[i]))
|
|
|
|
s[i++] = c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:59:16 -05:00
|
|
|
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)
|
|
|
|
{
|
2021-01-29 20:49:10 -05:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < 16; ++i)
|
|
|
|
if(scmp(opcodes[i], suca(s)))
|
|
|
|
return i;
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
echo(Uint8 *s, Uint8 len, Uint8 ptr, char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
printf("%s\n", name);
|
|
|
|
for(i = 0; i < len; ++i) {
|
|
|
|
if(i % 16 == 0)
|
|
|
|
printf("\n");
|
|
|
|
if(ptr == i)
|
|
|
|
printf("[%02x]", s[i]);
|
|
|
|
else
|
|
|
|
printf(" %02x ", s[i]);
|
2021-01-29 16:59:16 -05:00
|
|
|
}
|
2021-01-29 20:49:10 -05:00
|
|
|
printf("\n");
|
2021-01-29 16:59:16 -05:00
|
|
|
}
|
2021-01-29 15:14:37 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
pass1(FILE *f)
|
|
|
|
{
|
2021-01-29 16:59:16 -05:00
|
|
|
char word[64];
|
|
|
|
while(fscanf(f, "%s", word) == 1) {
|
2021-01-29 20:49:10 -05:00
|
|
|
int op = getopcode(word);
|
|
|
|
if(op == 0xff)
|
|
|
|
op = shex(word);
|
|
|
|
p.data[p.ptr++] = op;
|
2021-01-29 15:14:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
error(char *name)
|
|
|
|
{
|
|
|
|
printf("Error: %s\n", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
if(argc < 3)
|
|
|
|
return error("No input.");
|
|
|
|
if(!(f = fopen(argv[1], "r")))
|
|
|
|
return error("Missing input.");
|
|
|
|
pass1(f);
|
2021-01-29 16:59:16 -05:00
|
|
|
fwrite(p.data, sizeof(p.data), 1, fopen(argv[2], "wb"));
|
|
|
|
fclose(f);
|
2021-01-29 20:49:10 -05:00
|
|
|
echo(p.data, 0x40, 0, "program");
|
2021-01-29 15:14:37 -05:00
|
|
|
return 0;
|
|
|
|
}
|