184 lines
5.5 KiB
C
184 lines
5.5 KiB
C
/*
|
|
* ntsys.h
|
|
*
|
|
* Copyright 2026
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
* MA 02110-1301, USA.
|
|
*
|
|
*
|
|
*/
|
|
|
|
#if (!defined(__LIB_ntsys_H_))
|
|
#define __LIB_ntsys_H_
|
|
|
|
#define NTSYS_VERSION "1.0"
|
|
|
|
#define NAME_OF_32_BITS_TYPE unsigned int
|
|
#define NAME_OF_64_BITS_TYPE unsigned long int
|
|
#define NAME_OF_16_BITS_TYPE unsigned short int
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "ntfiles.h"
|
|
|
|
#if (!defined(EXIT_SUCCESS))
|
|
#define EXIT_SUCCESS 0
|
|
#endif
|
|
|
|
#if (!defined(EXIT_FAILURE))
|
|
#define EXIT_FAILURE 1
|
|
#endif
|
|
|
|
static void ntsys_error(char* _err) {
|
|
printf("\033[1mntsys: \033[91m%s\033[0m\n", _err);
|
|
}
|
|
|
|
static void __help(void) {
|
|
puts (
|
|
"BINARY Emulator V" NTSYS_VERSION "\n"
|
|
" " "VM for micron files" "\n"
|
|
"Using:" "\n"
|
|
" " "ntsys [OPTIONS...] [FILENAME]" "\n"
|
|
"Examples:" "\n"
|
|
" " "ntsys --asm FILE.asm" "\n"
|
|
" " "ntsys [--asm] FILE.asm --out FILE.exe" "\n"
|
|
" " "ntsys FILE.exe" "\n"
|
|
"Options:" "\n"
|
|
" " "--help / -h - show this help" "\n"
|
|
" " "--version / -v - show version" "\n"
|
|
" " "--asm / -a - compile assembler program" "\n"
|
|
" " "--out / -o - set output file name" "\0"
|
|
);
|
|
}
|
|
|
|
static int __exe_run(__e_byte_t* memory, unsigned int memorysize, unsigned long filesize, FILE* fp) {
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int ntsys_api_call(char* filename) {
|
|
|
|
/* File data */
|
|
|
|
__e_image_header_t head;
|
|
__e_byte_t head_bin[8];
|
|
|
|
/* Parser */
|
|
|
|
FILE* fp = fopen(filename, "r");
|
|
if (fp == NULL) {
|
|
ntsys_error("Cannot open file!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
short int res1 = ((fread(&head_bin, 1, sizeof(head_bin), fp)));
|
|
head.e_magic[0] = head_bin[0];
|
|
head.e_magic[1] = head_bin[1];
|
|
head.e_magic[2] = (__e_byte_t)0;
|
|
int __i = 1;
|
|
size_t i = 0;
|
|
if (*((char *)&__i) == 1) {
|
|
/* LITTLE ENDIAN */
|
|
head.e_memorysize[0] = head_bin[7];
|
|
head.e_memorysize[1] = head_bin[6];
|
|
i = 3;
|
|
while (1) {
|
|
head.e_filesize[i] = head_bin[5 - i];
|
|
if (i == 0) break;
|
|
i --;
|
|
}
|
|
} else {
|
|
/* BIG ENDIAN */
|
|
head.e_memorysize[0] = head_bin[6];
|
|
head.e_memorysize[1] = head_bin[7];
|
|
while (i != 4) {
|
|
head.e_filesize[i] = head_bin[2 + i];
|
|
i ++;
|
|
}
|
|
}
|
|
short int res2 = (strcmp(((char*)head.e_magic), "MZ"));
|
|
if (res1 != sizeof(head_bin) || res2 != 0) {
|
|
ntsys_error("Header read error or digital signature is invalid!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
unsigned long int filesize = 0;
|
|
switch (4) {
|
|
case sizeof(NAME_OF_64_BITS_TYPE): filesize = *((NAME_OF_64_BITS_TYPE*)head.e_filesize); break;
|
|
case sizeof(NAME_OF_32_BITS_TYPE): filesize = *((NAME_OF_32_BITS_TYPE*)head.e_filesize); break;
|
|
default:
|
|
ntsys_error("Unknown data type! You must recompile the program and specify a different type.");
|
|
return EXIT_FAILURE;
|
|
}
|
|
__e_byte_t* memory = malloc(*((NAME_OF_16_BITS_TYPE*)head.e_memorysize));
|
|
int stat = __exe_run(memory, (unsigned int)*((NAME_OF_16_BITS_TYPE*)head.e_memorysize), filesize, fp);
|
|
fclose(fp);
|
|
free(memory);
|
|
return stat;
|
|
}
|
|
|
|
int ntsys_compile_asm(char* in_file, char* out_file) {
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int _lib_ntsys_main(int argc, char** argv) {
|
|
unsigned int t = 1;
|
|
char* filename = NULL;
|
|
char* asm_out = "out.exe";
|
|
char mode = 'R';
|
|
unsigned char out = 0;
|
|
while (t < argc) {
|
|
char* str = argv[t];
|
|
if (str[0] == '-') {
|
|
unsigned char f = 0;
|
|
if (str[1] == '-') f = 1;
|
|
switch (str[1 + f]) {
|
|
case 'h':
|
|
__help();
|
|
return EXIT_SUCCESS;
|
|
case 'v':
|
|
puts(NTSYS_VERSION);
|
|
return EXIT_SUCCESS;
|
|
case 'a':
|
|
mode = 'A';
|
|
break;
|
|
case 'o':
|
|
out = 1;
|
|
mode = 'A';
|
|
break;
|
|
default:
|
|
ntsys_error("Unknown option!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
} else {
|
|
if (out) {
|
|
asm_out = str;
|
|
out = 0;
|
|
} else {
|
|
filename = str;
|
|
}
|
|
}
|
|
t ++;
|
|
}
|
|
if (filename == NULL || out) {
|
|
ntsys_error("Please specify the file name!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (mode == 'R') return ntsys_api_call(filename);
|
|
return ntsys_compile_asm(filename, asm_out);
|
|
}
|
|
|
|
#endif /* __LIB_ntsys_H_ */
|
|
|