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

88
cpu.c
View File

@ -4,6 +4,10 @@
#include <math.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 *ret=malloc(sizeof(struct fetch_data_t));
@ -606,44 +610,54 @@ int cpu_cycle_clock(struct simdata_t *simdata){
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->executing_list);
switch(exec(simdata)){
case 0: break;
case 2: return 4;
default: return 1;
switch(simdata->cpu_structure){
case CPU_STRUCTURE_SIMPLE_PIPELINED:
switch(exec(simdata)){
case 0: break;
case 2: return 4;
default: return 1;
}
switch(decode(simdata)){
case 0: break;
default: return 2;
}
switch(fetch(simdata)){
case 0: break;
default: return 3;
}
break;
case CPU_STRUCTURE_SIMPLE_NON_PIPELINED:
switch(state){
case 0:
switch(fetch(simdata)){
case 0: break;
default: return 3;
}
break;
case 1:
switch(decode(simdata)){
case 0: break;
default: return 2;
}
break;
case 2:
switch(exec(simdata)){
case 0: break;
case 2: return 4;
default: return 1;
}
break;
}
if(state==2){
if(simdata->decode_data->exec_done)
state=0;
}else
state++;
break;
default:
return 4;
}
switch(decode(simdata)){
case 0: break;
default: return 2;
}
switch(fetch(simdata)){
case 0: break;
default: return 3;
}
// switch(state){
// case 0:
// switch(fetch(simdata)){
// case 0: break;
// default: return 3;
// }
// break;
// case 1:
// switch(decode(simdata)){
// case 0: break;
// default: return 2;
// }
// break;
// case 2:
// switch(exec(simdata)){
// case 0: break;
// case 2: return 4;
// default: return 1;
// }
// break;
// }
// if(state==2)
// state=0;
// else
// state++;
simdata->current_clock++;
return 0;
}

25
main.c
View File

@ -42,6 +42,8 @@ void help(char* progname){
" braille\n"
#endif
" -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);
}
@ -62,9 +64,13 @@ int main(int argc, char* argd[] ){
char *assemble = NULL;
enum FB_DRIVER_t fb_driver=FB_BRAILLE;
int opt;
char *json_log_filename=NULL;
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) {
case 'h':
help(argd[0]);
@ -94,6 +100,9 @@ int main(int argc, char* argd[] ){
case 'o':
json_log_filename=optarg;
break;
case 'S':
structure=optarg;
break;
default:
help(argd[0]);
return 1;
@ -110,6 +119,7 @@ int main(int argc, char* argd[] ){
help(argd[0]);
return 1;
}
if (json_log_filename){
JSON_LOG_FILE=fopen(json_log_filename,"w");
if(JSON_LOG_FILE==NULL){
@ -133,6 +143,19 @@ int main(int argc, char* argd[] ){
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 ){
/* RUN ASSEMBLER */
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->terminal_output=malloc(0);
ret->terminal_output_size=0;
ret->cpu_structure=CPU_STRUCTURE_SIMPLE_PIPELINED;
return ret;
}