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 16:06:28 +00:00
|
|
|
#include <unistd.h>
|
2024-01-23 17:43:08 +00:00
|
|
|
#include <errno.h>
|
2024-02-03 18:03:37 +00:00
|
|
|
#include "assembly.h"
|
2024-02-03 22:54:20 +00:00
|
|
|
#include "cpu.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);
|
|
|
|
}
|
|
|
|
|
2024-02-03 18:03:37 +00:00
|
|
|
#define ADDR_SIZE 16777216
|
|
|
|
|
|
|
|
int dump_rom(char* filename, struct simdata_t *simdata){
|
|
|
|
FILE* outfile=fopen(filename,"w");
|
|
|
|
for(int i=0;i<ADDR_SIZE;i++)
|
|
|
|
fputc(*(simdata->RAM+i),outfile);
|
|
|
|
fclose(outfile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-23 16:06:28 +00:00
|
|
|
int main(int argc, char* argd[] ){
|
|
|
|
/// PARSE COMMAND LINE ///
|
|
|
|
char *infile = NULL;
|
2024-02-03 18:03:37 +00:00
|
|
|
char *assemble = NULL;
|
2024-01-23 16:06:28 +00:00
|
|
|
int opt;
|
2024-02-03 18:03:37 +00:00
|
|
|
while ((opt = getopt(argc, argd, "hi:a:")) != -1) {
|
2024-01-23 16:06:28 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'h':
|
|
|
|
help(argd[0]);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
infile=optarg;
|
2024-01-29 12:48:53 +00:00
|
|
|
break;
|
2024-02-03 18:03:37 +00:00
|
|
|
case 'a':
|
|
|
|
assemble=optarg;
|
|
|
|
break;
|
2024-01-23 16:06:28 +00:00
|
|
|
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-02-03 18:03:37 +00:00
|
|
|
/// READ INPUT FILE ///
|
2024-01-23 16:06:28 +00:00
|
|
|
FILE* rom=fopen(infile,"r");
|
2024-01-23 17:43:08 +00:00
|
|
|
|
2024-01-23 19:35:57 +00:00
|
|
|
struct simdata_t *simdata = init_simdata();
|
2024-02-03 22:54:20 +00:00
|
|
|
if(!simdata){
|
|
|
|
printf("Couldn't allocate memory\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-02-03 18:03:37 +00:00
|
|
|
|
|
|
|
if ( assemble != NULL ){
|
|
|
|
/* RUN ASSEMBLER */
|
|
|
|
struct assembler_context_t *assembler_context=malloc(sizeof(struct assembler_context_t));
|
|
|
|
|
|
|
|
for (int i=0;i<ADDR_SIZE;i++)
|
|
|
|
simdata->RAM[i]=0;
|
|
|
|
int linec=0;
|
|
|
|
if (rom != NULL) {
|
|
|
|
char line [1000];
|
|
|
|
uint32_t opcode;
|
|
|
|
uint32_t addr=0;
|
|
|
|
while(fgets(line,sizeof(line),rom)!= NULL){
|
|
|
|
opcode=assemble_line(line,assembler_context);
|
|
|
|
if(opcode>0xFFFFFFF1){
|
|
|
|
printf("Error assembling %s:%d\n", infile,linec+1);
|
|
|
|
fclose(rom);
|
|
|
|
free_assembler_context(assembler_context);
|
|
|
|
free_simdata(simdata);
|
|
|
|
return 1;
|
|
|
|
}else{
|
|
|
|
simdata->RAM[addr]=(0xFF000000&opcode)>>24;
|
|
|
|
addr++;
|
|
|
|
simdata->RAM[addr]=(0xFF0000&opcode)>>16;
|
|
|
|
addr++;
|
|
|
|
simdata->RAM[addr]=(0xFF00&opcode)>>8;
|
|
|
|
addr++;
|
|
|
|
simdata->RAM[addr]=(0xFF&opcode);
|
|
|
|
addr++;
|
|
|
|
}
|
|
|
|
linec++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_assembler_context(assembler_context);
|
|
|
|
dump_rom(assemble,simdata);
|
2024-01-23 19:35:57 +00:00
|
|
|
free_simdata(simdata);
|
2024-02-03 18:03:37 +00:00
|
|
|
}else{
|
|
|
|
/* RUN SIMULATOR */
|
|
|
|
if (rom == NULL) {
|
|
|
|
printf("ERROR: Couldn't open rom file\n");
|
|
|
|
perror(infile);
|
|
|
|
fclose(rom);
|
|
|
|
free_simdata(simdata);
|
|
|
|
return 1;
|
|
|
|
}
|
2024-01-23 16:06:28 +00:00
|
|
|
|
2024-02-03 18:03:37 +00:00
|
|
|
fseek(rom, 0, SEEK_END);
|
|
|
|
if(ftell(rom)!=ADDR_SIZE){
|
|
|
|
printf("ERROR: ROM file isn't 16MiB\n");
|
|
|
|
fclose(rom);
|
|
|
|
free_simdata(simdata);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fseek(rom, 0, SEEK_SET);
|
2024-01-23 16:06:28 +00:00
|
|
|
|
2024-02-03 18:03:37 +00:00
|
|
|
if(!simdata){
|
2024-02-03 22:54:20 +00:00
|
|
|
gui_error("failed to initialise simdata\n");
|
2024-02-03 18:03:37 +00:00
|
|
|
fclose(rom);
|
|
|
|
free_simdata(simdata);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(fread(simdata->RAM, ADDR_SIZE, 1, rom)==0){
|
|
|
|
printf("ERROR: failed to read input file\n");
|
|
|
|
free_simdata(simdata);
|
|
|
|
fclose(rom);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(rom);
|
2024-01-23 16:06:28 +00:00
|
|
|
|
2024-01-23 13:33:04 +00:00
|
|
|
|
2024-02-03 18:03:37 +00:00
|
|
|
/// INITIALISE GUI ///
|
|
|
|
if(start_gui()){
|
|
|
|
printf("Failed on start_gui()\n");
|
2024-02-03 22:54:20 +00:00
|
|
|
cpu_simdata_free(simdata);
|
2024-02-03 18:03:37 +00:00
|
|
|
free_simdata(simdata);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-02-03 22:54:20 +00:00
|
|
|
cpu_simdata_malloc(simdata);
|
2024-02-03 18:03:37 +00:00
|
|
|
|
2024-02-03 22:54:20 +00:00
|
|
|
while(1){
|
|
|
|
update_gui(simdata);
|
|
|
|
|
2024-02-03 23:49:45 +00:00
|
|
|
int ret_code;
|
|
|
|
if((ret_code=gui_continue_request())){
|
|
|
|
if(ret_code==2)
|
|
|
|
break;
|
2024-02-03 22:54:20 +00:00
|
|
|
end_gui();
|
|
|
|
printf("Failed on gui_continue_request()\n");
|
|
|
|
cpu_simdata_free(simdata);
|
|
|
|
free_simdata(simdata);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cpu_cycle_clock(simdata)){
|
|
|
|
cpu_simdata_free(simdata);
|
|
|
|
free_simdata(simdata);
|
|
|
|
end_gui();
|
|
|
|
printf("Failed to execute instruction\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-02-03 18:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(end_gui()){
|
|
|
|
printf("Failed on end_gui()\n");
|
2024-02-03 22:54:20 +00:00
|
|
|
cpu_simdata_free(simdata);
|
2024-02-03 18:03:37 +00:00
|
|
|
free_simdata(simdata);
|
|
|
|
return 1;
|
|
|
|
}
|
2024-01-22 12:11:07 +00:00
|
|
|
|
2024-02-03 22:54:20 +00:00
|
|
|
|
|
|
|
cpu_simdata_free(simdata);
|
2024-01-22 12:11:07 +00:00
|
|
|
free_simdata(simdata);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|