Added code to parse the command line and read the rom to memory

This commit is contained in:
(Tim) Efthimis Kritikos 2024-01-23 16:06:28 +00:00
parent e098cacb37
commit a5f7444733
3 changed files with 60 additions and 6 deletions

View File

@ -1,10 +1,10 @@
all:first all:first
first:gui.o main.o simdata.o first:gui.o main.o simdata.o
gcc $^ -fsanitize=address -lncurses -ltinfo -o $@ gcc -ggdb $^ -fsanitize=address -lncurses -ltinfo -o $@
%.o:%.c %.o:%.c
gcc -c $< -Wall -Wextra -Werror -fsanitize=address gcc -ggdb -c $< -Wall -Wextra -Werror -fsanitize=address
.PHONY: clean .PHONY: clean
clean: clean:

1
gui.c
View File

@ -51,6 +51,7 @@ int start_gui(){
start_color(); start_color();
init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK);
} }
noecho();
curs_set(0); curs_set(0);
get_terminal_size(); get_terminal_size();
mvprintw((terminal_height-3)/2+3,terminal_width/2-15,"Initialising the simulator..."); mvprintw((terminal_height-3)/2+3,terminal_width/2-15,"Initialising the simulator...");

61
main.c
View File

@ -22,35 +22,88 @@
#include "gui.h" #include "gui.h"
#include "simdata.h" #include "simdata.h"
#include "stdint.h" #include "stdint.h"
#include <unistd.h>
int main(){ void help(char* progname){
if(start_gui()){ printf("Usage: %s -i <file> \n", progname);
printf("Failed on start_gui()\n"); }
int main(int argc, char* argd[] ){
/// PARSE COMMAND LINE ///
char *infile = NULL;
int opt;
while ((opt = getopt(argc, argd, "hi:")) != -1) {
switch (opt) {
case 'h':
help(argd[0]);
return 0;
break;
case 'i':
infile=optarg;
break;
default:
help(argd[0]);
return 1;
break;
}
}
if (infile==NULL){
help(argd[0]);
return 1; return 1;
} }
/// READ ROM FILE ///
FILE* rom=fopen(infile,"r");
fseek(rom, 0, SEEK_END);
if(ftell(rom)!=16777216){
printf("ERROR: ROM file isn't 16MiB\n");
return 1;
}
fseek(rom, 0, SEEK_SET);
uint8_t *RAM=malloc(16777216); //Maximum addressable memory uint8_t *RAM=malloc(16777216); //Maximum addressable memory
if(!RAM){ if(!RAM){
gui_error("failed to allocate 16MiB of memory for the system ram"); gui_error("failed to allocate 16MiB of memory for the system ram");
return 1; return 1;
} }
if(fread(RAM, 16777216, 1, rom)==0){
printf("ERROR: failed to read input file\n");
free(RAM);
return 1;
}
fclose(rom);
/// INITIALISE GUI ///
if(start_gui()){
printf("Failed on start_gui()\n");
free(RAM);
return 1;
}
struct simdata_t *simdata = init_simdata(); struct simdata_t *simdata = init_simdata();
if(!simdata){ if(!simdata){
gui_error("failed to initialise simdata"); gui_error("failed to initialise simdata");
free(RAM);
return 1; return 1;
} }
gui_error("failed to initialise simdata"); char *temp=malloc(1000);
sprintf(temp,"Last byte of ram is 0x%02x",RAM[0xFFFFFF]);
gui_error(temp);
free(temp);
if(gui_continue_request()){ if(gui_continue_request()){
end_gui(); end_gui();
printf("Failed on gui_continue_request()\n"); printf("Failed on gui_continue_request()\n");
free_simdata(simdata); free_simdata(simdata);
free(RAM);
return 1; return 1;
} }
if(end_gui()){ if(end_gui()){
printf("Failed on end_gui()\n"); printf("Failed on end_gui()\n");
free_simdata(simdata); free_simdata(simdata);
free(RAM);
return 1; return 1;
} }