CPU: Made the cpu structure, pipelined/non-pipelined selectable at runtime with a flag

This commit is contained in:
(Tim) Efthimis Kritikos 2024-05-02 12:55:29 +01:00
parent 48237ccd95
commit 9fbc6bceab
3 changed files with 75 additions and 39 deletions

64
cpu.c
View File

@ -4,6 +4,10 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
/* Known errors:
* 1) Exec does the calculations on the first clock cycle, there is a bug where in some cases exec isn't run on the last cycle and because of this the CPU still works.
*/
struct fetch_data_t *malloc_fetch_data(){ struct fetch_data_t *malloc_fetch_data(){
struct fetch_data_t *ret=malloc(sizeof(struct fetch_data_t)); struct fetch_data_t *ret=malloc(sizeof(struct fetch_data_t));
@ -606,6 +610,8 @@ int cpu_cycle_clock(struct simdata_t *simdata){
free_instr_list(&simdata->cpu_gui_hints->fetching_list); free_instr_list(&simdata->cpu_gui_hints->fetching_list);
free_instr_list(&simdata->cpu_gui_hints->decoding_list); free_instr_list(&simdata->cpu_gui_hints->decoding_list);
free_instr_list(&simdata->cpu_gui_hints->executing_list); free_instr_list(&simdata->cpu_gui_hints->executing_list);
switch(simdata->cpu_structure){
case CPU_STRUCTURE_SIMPLE_PIPELINED:
switch(exec(simdata)){ switch(exec(simdata)){
case 0: break; case 0: break;
case 2: return 4; case 2: return 4;
@ -619,31 +625,39 @@ int cpu_cycle_clock(struct simdata_t *simdata){
case 0: break; case 0: break;
default: return 3; default: return 3;
} }
// switch(state){ break;
// case 0: case CPU_STRUCTURE_SIMPLE_NON_PIPELINED:
// switch(fetch(simdata)){ switch(state){
// case 0: break; case 0:
// default: return 3; switch(fetch(simdata)){
// } case 0: break;
// break; default: return 3;
// case 1: }
// switch(decode(simdata)){ break;
// case 0: break; case 1:
// default: return 2; switch(decode(simdata)){
// } case 0: break;
// break; default: return 2;
// case 2: }
// switch(exec(simdata)){ break;
// case 0: break; case 2:
// case 2: return 4; switch(exec(simdata)){
// default: return 1; case 0: break;
// } case 2: return 4;
// break; default: return 1;
// } }
// if(state==2) break;
// state=0; }
// else if(state==2){
// state++; if(simdata->decode_data->exec_done)
state=0;
}else
state++;
break;
default:
return 4;
}
simdata->current_clock++; simdata->current_clock++;
return 0; return 0;
} }

25
main.c
View File

@ -42,6 +42,8 @@ void help(char* progname){
" braille\n" " braille\n"
#endif #endif
" -o < file > Output simualtion data to specified json log file\n" " -o < file > Output simualtion data to specified json log file\n"
" -S < cpu structure > Select the structure of the cpu. You can choose one\n"
" of the following: simple, pipelined\n"
, progname); , progname);
} }
@ -62,9 +64,13 @@ int main(int argc, char* argd[] ){
char *assemble = NULL; char *assemble = NULL;
enum FB_DRIVER_t fb_driver=FB_BRAILLE; enum FB_DRIVER_t fb_driver=FB_BRAILLE;
int opt; int opt;
char *json_log_filename=NULL; char *json_log_filename=NULL;
FILE *JSON_LOG_FILE; FILE *JSON_LOG_FILE;
while ((opt = getopt(argc, argd, "hi:a:r:o:")) != -1) {
char *structure="pipelined";
while ((opt = getopt(argc, argd, "hi:a:r:o:S:")) != -1) {
switch (opt) { switch (opt) {
case 'h': case 'h':
help(argd[0]); help(argd[0]);
@ -94,6 +100,9 @@ int main(int argc, char* argd[] ){
case 'o': case 'o':
json_log_filename=optarg; json_log_filename=optarg;
break; break;
case 'S':
structure=optarg;
break;
default: default:
help(argd[0]); help(argd[0]);
return 1; return 1;
@ -110,6 +119,7 @@ int main(int argc, char* argd[] ){
help(argd[0]); help(argd[0]);
return 1; return 1;
} }
if (json_log_filename){ if (json_log_filename){
JSON_LOG_FILE=fopen(json_log_filename,"w"); JSON_LOG_FILE=fopen(json_log_filename,"w");
if(JSON_LOG_FILE==NULL){ if(JSON_LOG_FILE==NULL){
@ -133,6 +143,19 @@ int main(int argc, char* argd[] ){
return 1; return 1;
} }
if(strcmp("simple",structure)==0){
simdata->cpu_structure=CPU_STRUCTURE_SIMPLE_NON_PIPELINED;
}else if(strcmp("pipelined",structure)==0){
simdata->cpu_structure=CPU_STRUCTURE_SIMPLE_PIPELINED;
}else{
printf("Invalid cpu structure \"%s\"\n",structure);
fclose(rom);
free_simdata(simdata);
return 1;
}
if ( assemble != NULL ){ if ( assemble != NULL ){
/* RUN ASSEMBLER */ /* RUN ASSEMBLER */
struct assembler_context_t *assembler_context=malloc_assembler_context(); struct assembler_context_t *assembler_context=malloc_assembler_context();

View File

@ -46,7 +46,6 @@ struct simdata_t *init_simdata(){
ret->cpu_gui_hints->executing_list=NULL; ret->cpu_gui_hints->executing_list=NULL;
ret->terminal_output=malloc(0); ret->terminal_output=malloc(0);
ret->terminal_output_size=0; ret->terminal_output_size=0;
ret->cpu_structure=CPU_STRUCTURE_SIMPLE_PIPELINED;
return ret; return ret;
} }