2020-10-16 18:36:18 +02:00
|
|
|
/* RETRO ------------------------------------------------------
|
|
|
|
A personal, minimalistic forth
|
|
|
|
Copyright (c) 2016 - 2019 Charles Childers
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2020-10-16 18:36:18 +02:00
|
|
|
This loads an image file and generates a C formatted output
|
|
|
|
suitable for being linked into the virtual machine. It's
|
|
|
|
used to create the `image.c` that gets linked into `retro`.
|
|
|
|
---------------------------------------------------------- */
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2017-10-19 21:11:45 +02:00
|
|
|
#include <stdint.h>
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2019-09-23 17:11:56 +02:00
|
|
|
#ifndef BIT64
|
|
|
|
#define CELL int32_t
|
|
|
|
#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
|
|
|
|
2019-02-21 22:59:35 +01:00
|
|
|
CELL memory[512*1024];
|
2017-10-19 21:11:45 +02:00
|
|
|
|
|
|
|
CELL ngaLoadImage(char *imageFile) {
|
|
|
|
FILE *fp;
|
|
|
|
CELL imageSize;
|
|
|
|
long fileLen;
|
|
|
|
if ((fp = fopen(imageFile, "rb")) != NULL) {
|
|
|
|
/* Determine length (in cells) */
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
fileLen = ftell(fp) / sizeof(CELL);
|
|
|
|
rewind(fp);
|
|
|
|
/* Read the file into memory */
|
|
|
|
imageSize = fread(&memory, sizeof(CELL), fileLen, fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Unable to find the ngaImage!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return imageSize;
|
|
|
|
}
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
void output_header(int size) {
|
2020-10-16 18:36:18 +02:00
|
|
|
printf("#include <stdint.h>\n");
|
|
|
|
printf("#ifndef CELL\n");
|
|
|
|
printf("#define CELL int32_t\n");
|
|
|
|
printf("#endif\n");
|
2019-09-22 05:01:49 +02:00
|
|
|
printf("CELL ngaImageCells = %lld;\n", (long long)size);
|
|
|
|
printf("CELL ngaImage[] = { ");
|
2017-10-16 18:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int32_t size = 0;
|
2018-01-27 21:25:18 +01:00
|
|
|
int32_t i;
|
2018-02-02 22:43:29 +01:00
|
|
|
int32_t n;
|
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
if (argc == 2)
|
|
|
|
size = ngaLoadImage(argv[1]);
|
|
|
|
else
|
|
|
|
size = ngaLoadImage("ngaImage");
|
|
|
|
|
|
|
|
output_header(size);
|
|
|
|
|
|
|
|
i = 0;
|
2018-02-02 22:43:29 +01:00
|
|
|
n = 0;
|
2017-10-16 18:09:39 +02:00
|
|
|
while (i < size) {
|
2018-02-02 22:43:29 +01:00
|
|
|
n++;
|
|
|
|
if (n == 20) {
|
|
|
|
printf("\n ");
|
|
|
|
n = 0;
|
|
|
|
}
|
2017-10-16 18:09:39 +02:00
|
|
|
if (i+1 < size)
|
2019-09-17 18:40:25 +02:00
|
|
|
printf("%lld,", (long long)memory[i]);
|
2017-10-16 18:09:39 +02:00
|
|
|
else
|
2019-09-17 18:40:25 +02:00
|
|
|
printf("%lld };\n", (long long)memory[i]);
|
2017-10-16 18:09:39 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|