2024-01-23 13:33:04 +00:00
|
|
|
/* main.c
|
|
|
|
|
|
|
|
This file is part of the "First" CPU simulator project.
|
|
|
|
|
|
|
|
Copyright (c) 2024 Efthymios Kritikos
|
|
|
|
|
|
|
|
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 3 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, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
#include <stdio.h>
|
2024-01-23 13:33:04 +00:00
|
|
|
#include <stdlib.h>
|
2024-01-22 12:11:07 +00:00
|
|
|
#include "gui.h"
|
|
|
|
#include "simdata.h"
|
2024-01-23 13:33:04 +00:00
|
|
|
#include "stdint.h"
|
2024-01-23 16:06:28 +00:00
|
|
|
#include <unistd.h>
|
2024-01-22 12:11:07 +00:00
|
|
|
|
2024-01-23 16:06:28 +00:00
|
|
|
void help(char* progname){
|
|
|
|
printf("Usage: %s -i <file> \n", progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
2024-01-22 12:11:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2024-01-23 13:33:04 +00:00
|
|
|
|
2024-01-23 16:06:28 +00:00
|
|
|
/// 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);
|
|
|
|
|
2024-01-23 13:33:04 +00:00
|
|
|
uint8_t *RAM=malloc(16777216); //Maximum addressable memory
|
|
|
|
if(!RAM){
|
|
|
|
gui_error("failed to allocate 16MiB of memory for the system ram");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-01-23 16:06:28 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-23 13:33:04 +00:00
|
|
|
struct simdata_t *simdata = init_simdata();
|
|
|
|
if(!simdata){
|
|
|
|
gui_error("failed to initialise simdata");
|
2024-01-23 16:06:28 +00:00
|
|
|
free(RAM);
|
2024-01-23 13:33:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2024-01-23 16:06:28 +00:00
|
|
|
char *temp=malloc(1000);
|
|
|
|
sprintf(temp,"Last byte of ram is 0x%02x",RAM[0xFFFFFF]);
|
|
|
|
gui_error(temp);
|
|
|
|
free(temp);
|
2024-01-23 13:33:04 +00:00
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
if(gui_continue_request()){
|
2024-01-23 13:33:04 +00:00
|
|
|
end_gui();
|
2024-01-22 12:11:07 +00:00
|
|
|
printf("Failed on gui_continue_request()\n");
|
|
|
|
free_simdata(simdata);
|
2024-01-23 16:06:28 +00:00
|
|
|
free(RAM);
|
2024-01-22 12:11:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(end_gui()){
|
|
|
|
printf("Failed on end_gui()\n");
|
|
|
|
free_simdata(simdata);
|
2024-01-23 16:06:28 +00:00
|
|
|
free(RAM);
|
2024-01-22 12:11:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_simdata(simdata);
|
2024-01-23 13:33:04 +00:00
|
|
|
free(RAM);
|
2024-01-22 12:11:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|