2025-10-03 21:09:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include "runmem.h"
|
2025-10-04 20:57:45 +00:00
|
|
|
#include "mem_man.h"
|
2025-10-03 21:09:25 +00:00
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2025-11-03 01:14:14 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
|
fprintf(stderr, "Usage: %s <code_file>\n", argv[0]);
|
2025-10-03 21:09:25 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 21:06:45 +00:00
|
|
|
FILE *f = fopen(argv[1], "rb");
|
|
|
|
|
if (!f) {
|
|
|
|
|
perror("fopen");
|
2025-10-03 21:09:25 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 21:06:45 +00:00
|
|
|
/* Determine file size */
|
|
|
|
|
if (fseek(f, 0, SEEK_END) != 0) {
|
|
|
|
|
perror("fseek");
|
|
|
|
|
fclose(f);
|
2025-10-03 21:09:25 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2025-10-04 21:06:45 +00:00
|
|
|
long sz = ftell(f);
|
|
|
|
|
if (sz < 0) {
|
|
|
|
|
perror("ftell");
|
|
|
|
|
fclose(f);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
rewind(f);
|
2025-10-03 21:09:25 +00:00
|
|
|
|
2025-10-04 21:06:45 +00:00
|
|
|
if (sz == 0) {
|
2025-10-03 21:09:25 +00:00
|
|
|
fprintf(stderr, "Error: File is empty\n");
|
2025-10-04 21:06:45 +00:00
|
|
|
fclose(f);
|
2025-10-03 21:09:25 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 21:06:45 +00:00
|
|
|
uint8_t *file_buff = allocate_buffer_memory((uint32_t)sz);
|
2025-10-03 21:09:25 +00:00
|
|
|
|
2025-10-04 21:06:45 +00:00
|
|
|
/* Read file into allocated memory */
|
|
|
|
|
size_t nread = fread(file_buff, 1, (size_t)sz, f);
|
|
|
|
|
fclose(f);
|
|
|
|
|
if (nread != (size_t)sz) {
|
|
|
|
|
perror("fread");
|
2025-10-03 21:09:25 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 20:56:04 +00:00
|
|
|
int ret = parse_commands(file_buff);
|
2025-10-03 21:09:25 +00:00
|
|
|
|
2025-11-03 01:14:14 +00:00
|
|
|
if (ret == 2) {
|
|
|
|
|
/* Dump code for debugging */
|
|
|
|
|
if (argc != 3) {
|
|
|
|
|
fprintf(stderr, "Usage: %s <code_file> <memory_dump_file>\n", argv[0]);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2025-11-03 08:43:05 +00:00
|
|
|
f = fopen(argv[2], "wb");
|
2025-11-03 01:14:14 +00:00
|
|
|
fwrite(executable_memory, 1, (size_t)executable_memory_len, f);
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 21:09:25 +00:00
|
|
|
free_memory();
|
|
|
|
|
|
2025-10-07 20:56:04 +00:00
|
|
|
return ret < 0;
|
2025-10-03 21:09:25 +00:00
|
|
|
}
|