2017-10-16 18:09:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2019-01-18 02:20:08 +01:00
|
|
|
#include "strl.h"
|
2019-09-23 17:11:56 +02:00
|
|
|
|
|
|
|
#ifndef BIT64
|
2019-09-17 18:40:25 +02:00
|
|
|
#define CELL int32_t
|
2019-09-23 17:11:56 +02:00
|
|
|
#define CELL_MIN INT_MIN + 1
|
|
|
|
#define CELL_MAX INT_MAX - 1
|
|
|
|
#else
|
|
|
|
#define CELL int64_t
|
|
|
|
#define CELL_MIN LLONG_MIN + 1
|
|
|
|
#define CELL_MAX LLONG_MAX - 1
|
2019-09-22 05:01:49 +02:00
|
|
|
#endif
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
#define KiB * 1024
|
|
|
|
#define MAX_NAMES 1024
|
|
|
|
#define STRING_LEN 64
|
|
|
|
#define IMAGE_SIZE 128 KiB
|
2019-09-23 17:11:56 +02:00
|
|
|
|
|
|
|
char Labels[MAX_NAMES][STRING_LEN];
|
2019-09-17 18:40:25 +02:00
|
|
|
CELL Pointers[MAX_NAMES];
|
|
|
|
CELL np;
|
2017-10-16 18:09:39 +02:00
|
|
|
char source[1 KiB];
|
2019-09-17 18:40:25 +02:00
|
|
|
CELL target[IMAGE_SIZE];
|
|
|
|
CELL here;
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
void save() {
|
|
|
|
FILE *fp;
|
|
|
|
if ((fp = fopen("ngaImage", "wb")) == NULL) {
|
|
|
|
printf("Unable to save the image!\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
2019-09-17 18:40:25 +02:00
|
|
|
fwrite(&target, sizeof(CELL), here, fp);
|
2017-10-16 18:09:39 +02:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2019-09-17 18:40:25 +02:00
|
|
|
CELL lookup(char *name) {
|
|
|
|
CELL slice = -1;
|
|
|
|
CELL n = np;
|
2017-10-16 18:09:39 +02:00
|
|
|
while (n > 0) {
|
|
|
|
n--;
|
|
|
|
if (strcmp(Labels[n], name) == 0)
|
|
|
|
slice = Pointers[n];
|
|
|
|
}
|
|
|
|
return slice;
|
|
|
|
}
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2019-09-17 18:40:25 +02:00
|
|
|
void add_label(char *name, CELL slice) {
|
2017-10-16 18:09:39 +02:00
|
|
|
if (lookup(name) == -1) {
|
2019-05-29 14:31:42 +02:00
|
|
|
bsd_strlcpy(Labels[np], name, STRING_LEN);
|
2017-10-16 18:09:39 +02:00
|
|
|
Pointers[np] = slice;
|
|
|
|
np++;
|
|
|
|
} else {
|
|
|
|
printf("Fatal error: %s already defined\n", name);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
void read_line(FILE *file, char *line_buffer) {
|
|
|
|
int ch = getc(file);
|
|
|
|
int count = 0;
|
|
|
|
while ((ch != '\n') && (ch != EOF)) {
|
|
|
|
line_buffer[count] = ch;
|
|
|
|
count++;
|
|
|
|
ch = getc(file);
|
|
|
|
}
|
|
|
|
line_buffer[count] = '\0';
|
|
|
|
}
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2019-09-17 18:40:25 +02:00
|
|
|
CELL opcode_for(char *s) {
|
2019-09-23 22:02:11 +02:00
|
|
|
char* opcodeList = "..lidudrswpupojucaccreeqneltgtfestadsumudianorxoshzrenieiqii";
|
|
|
|
int16_t* s16 = (int16_t *)s;
|
|
|
|
int16_t* op16 = (int16_t *)opcodeList;
|
|
|
|
int i = 0;
|
|
|
|
for(i = 0; i <= 30; i++){
|
|
|
|
if(s16[0] == op16[i]){
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2017-10-16 18:09:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
void pass1(char *fname) {
|
|
|
|
int inBlock = 0;
|
|
|
|
char *buffer = (char *)source;
|
|
|
|
unsigned int opcode;
|
|
|
|
char inst[3];
|
|
|
|
FILE *fp;
|
|
|
|
inst[2] = '\0';
|
|
|
|
here = 0;
|
|
|
|
fp = fopen(fname, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
printf("Unable to load file\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
while (!feof(fp)) {
|
|
|
|
read_line(fp, buffer);
|
2017-10-20 15:02:14 +02:00
|
|
|
if (strcmp(buffer, "~~~") == 0) {
|
2017-10-16 18:09:39 +02:00
|
|
|
if (inBlock == 0)
|
|
|
|
inBlock = 1;
|
|
|
|
else
|
|
|
|
inBlock = 0;
|
|
|
|
} else {
|
|
|
|
if (inBlock == 1) {
|
|
|
|
if (buffer[1] == '\t' || buffer[1] == ' ') {
|
|
|
|
switch (buffer[0]) {
|
|
|
|
case 'i': memcpy(inst, buffer + 8, 2);
|
|
|
|
opcode = opcode_for(inst);
|
|
|
|
opcode = opcode << 8;
|
|
|
|
memcpy(inst, buffer + 6, 2);
|
|
|
|
opcode += opcode_for(inst);
|
|
|
|
opcode = opcode << 8;
|
|
|
|
memcpy(inst, buffer + 4, 2);
|
|
|
|
opcode += opcode_for(inst);
|
|
|
|
opcode = opcode << 8;
|
|
|
|
memcpy(inst, buffer + 2, 2);
|
|
|
|
opcode += opcode_for(inst);
|
|
|
|
target[here++] = opcode;
|
|
|
|
break;
|
|
|
|
case 'r': target[here++] = -1;
|
|
|
|
break;
|
|
|
|
case 'd': target[here++] = atoi(buffer+2);
|
|
|
|
break;
|
|
|
|
case 's': opcode = 2;
|
|
|
|
while (opcode < strlen(buffer))
|
|
|
|
target[here++] = buffer[opcode++];
|
|
|
|
target[here++] = 0;
|
|
|
|
break;
|
|
|
|
case ':': add_label(buffer+2, here);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
void pass2(char *fname) {
|
2018-01-27 21:36:11 +01:00
|
|
|
char *buffer;
|
2017-10-16 18:09:39 +02:00
|
|
|
FILE *fp;
|
2018-01-27 21:36:11 +01:00
|
|
|
int inBlock;
|
|
|
|
inBlock = 0;
|
|
|
|
buffer = (char *)source;
|
2017-10-16 18:09:39 +02:00
|
|
|
here = 0;
|
|
|
|
fp = fopen(fname, "r");
|
2019-05-28 22:26:28 +02:00
|
|
|
if (fp == NULL) {
|
|
|
|
printf("Unable to load file\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
2017-10-16 18:09:39 +02:00
|
|
|
while (!feof(fp)) {
|
|
|
|
read_line(fp, buffer);
|
2017-10-20 15:02:14 +02:00
|
|
|
if (strcmp(buffer, "~~~") == 0) {
|
2017-10-16 18:09:39 +02:00
|
|
|
if (inBlock == 0)
|
|
|
|
inBlock = 1;
|
|
|
|
else
|
|
|
|
inBlock = 0;
|
|
|
|
} else {
|
|
|
|
if (inBlock == 1) {
|
|
|
|
switch (buffer[0]) {
|
|
|
|
case 'i': here++; break;
|
|
|
|
case 'r': target[here++] = lookup(buffer+2);
|
|
|
|
if (lookup(buffer+2) == -1)
|
|
|
|
printf("Lookup failed: '%s'\n", buffer+2);
|
|
|
|
break;
|
|
|
|
case 'd': here++; break;
|
|
|
|
case 's': here = here + strlen(buffer) - 1; break;
|
|
|
|
case ':': break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2019-09-23 17:11:56 +02:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
np = 0;
|
|
|
|
if (argc > 1) {
|
|
|
|
pass1(argv[1]);
|
|
|
|
pass2(argv[1]);
|
|
|
|
save();
|
2019-09-17 18:40:25 +02:00
|
|
|
printf("Wrote %lld cells to ngaImage\n", (long long)here);
|
2017-10-16 18:09:39 +02:00
|
|
|
}
|
|
|
|
else
|
2019-01-11 04:10:25 +01:00
|
|
|
printf("muri\n(c) 2017-2019 charles childers\n\n%s filename\n", argv[0]);
|
2017-10-16 18:09:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|