diff --git a/cpu.c b/cpu.c index 3af8e53..0c200b6 100644 --- a/cpu.c +++ b/cpu.c @@ -11,10 +11,10 @@ void free_fetch_data(__attribute__((unused)) struct fetch_data_t *tofree){ } int fetch(struct simdata_t *simdata){ free_instr_list(&simdata->cpu_gui_hints->fetching_list); - simdata->decode_data->in_bytecode=(uint32_t)(simdata->RAM[simdata->PC])<<24|(uint32_t)(simdata->RAM[simdata->PC+1])<<16|(uint32_t)(simdata->RAM[simdata->PC+2])<<8|(uint32_t)(simdata->RAM[simdata->PC+3]); - simdata->decode_data->address=simdata->PC; - add_to_instr_list(&simdata->cpu_gui_hints->fetching_list,simdata->PC); - simdata->PC+=4; + simdata->decode_data->in_bytecode=(uint32_t)(simdata->RAM[simdata->registers->PC])<<24|(uint32_t)(simdata->RAM[simdata->registers->PC+1])<<16|(uint32_t)(simdata->RAM[simdata->registers->PC+2])<<8|(uint32_t)(simdata->RAM[simdata->registers->PC+3]); + simdata->decode_data->address=simdata->registers->PC; + add_to_instr_list(&simdata->cpu_gui_hints->fetching_list,simdata->registers->PC); + simdata->registers->PC+=4; return 0; } @@ -200,18 +200,18 @@ int exec(struct simdata_t *simdata){ } if(condition){ if(simdata->exec_data->EXEC_ACTION==CALL){ - simdata->RAM[simdata->SP]=simdata->PC; - simdata->SP+=6; + simdata->RAM[simdata->registers->SP]=simdata->registers->PC; + simdata->registers->SP+=6; } if(simdata->exec_data->out_op->OP_ADDR==IMMEDIATE) - simdata->PC=simdata->exec_data->out_op->data; + simdata->registers->PC=simdata->exec_data->out_op->data; else return 1; } break; case RET: - simdata->SP-=6; - simdata->PC=simdata->RAM[simdata->SP]; + simdata->registers->SP-=6; + simdata->registers->PC=simdata->RAM[simdata->registers->SP]; break; case EXEC_ALU: if( simdata->exec_data->in_op1->OP_ADDR==REGISTER && @@ -273,12 +273,12 @@ int exec(struct simdata_t *simdata){ case REGISTER: /* This is for special registers like the SP which is 24bits long */ if(simdata->exec_data->in_op1->OP_ADDR==IMMEDIATE){ if( (simdata->exec_data->in_op1->data&0xFF000000) == 0 ) - simdata->SP=simdata->exec_data->in_op1->data; + simdata->registers->SP=simdata->exec_data->in_op1->data; else return 2; }else{ if( (simdata->registers->GPR[simdata->exec_data->in_op1->data]&0xFF000000) == 0 ) - simdata->SP=simdata->registers->GPR[simdata->exec_data->in_op1->data]; + simdata->registers->SP=simdata->registers->GPR[simdata->exec_data->in_op1->data]; else return 2; } @@ -349,6 +349,8 @@ int cpu_simdata_malloc(struct simdata_t *simdata){ simdata->decode_data=malloc_decode_data(); simdata->exec_data=malloc_exec_data(); simdata->registers=malloc(sizeof(struct registers_t)); + simdata->registers->PC=0x00000000; + simdata->registers->SP&=0x00FFFFFF; //intentionally leave it uninitialised but within the legal range if((simdata->fetch_data!=NULL)&&(simdata->decode_data!=NULL)&&(simdata->exec_data!=NULL)) return 1; else diff --git a/cpu.h b/cpu.h index e231db2..ccde7fc 100644 --- a/cpu.h +++ b/cpu.h @@ -79,6 +79,8 @@ int cpu_cycle_clock(struct simdata_t *simdata); struct registers_t{ uint32_t GPR[8]; uint32_t FLAGS; + uint32_t PC; + uint32_t SP; }; #endif diff --git a/gui.c b/gui.c index c6cb63a..207a444 100644 --- a/gui.c +++ b/gui.c @@ -243,6 +243,11 @@ int update_general_disas(struct simdata_t *simdata){ int width,height; getmaxyx(general_disas,height,width); box(general_disas, 0 , 0); + mvwprintw(general_disas,0,width/2-7,"[ DISASSEMBLY ]"); + if(simdata->registers==NULL||simdata->RAM==NULL){ + mvwprintw(general_disas,1,1,"Data not initialised"); + return 0; + } int offset_arrow; int have_legend=(width>29); @@ -253,17 +258,16 @@ int update_general_disas(struct simdata_t *simdata){ offset_arrow=2; else offset_arrow=3; - mvwprintw(general_disas,0,width/2-7,"[ DISASSEMBLY ]"); if(width<18){ mvwprintw(general_disas,1,1,"too small window"); }else{ - uint32_t ADDRESS=(simdata->PC-(usable_height/2-2)*4)&0x00FFFFFF; + uint32_t ADDRESS=(simdata->registers->PC-(usable_height/2-2)*4)&0x00FFFFFF; for (int i=2;iPC) + if(ADDRESS==simdata->registers->PC) wattron(general_disas,A_BOLD); mvwprintw(general_disas,i,4+overall_offset,"%06X",ADDRESS); char* disas=disassemble(*(uint32_t*)(simdata->RAM+ADDRESS)); @@ -275,7 +279,7 @@ int update_general_disas(struct simdata_t *simdata){ mvwprintw(general_disas,i,13+overall_offset,"%s",disas); select_instruction_color(simdata,general_disas,ADDRESS,0); - if(ADDRESS==simdata->PC) + if(ADDRESS==simdata->registers->PC) wattroff(general_disas,A_BOLD); ADDRESS=(ADDRESS+4)&0xFFFFFF; @@ -319,6 +323,10 @@ int update_general_stack(struct simdata_t *simdata){ getmaxyx(general_stack,height,width); box(general_stack, 0 , 0); mvwprintw(general_stack,0,width/2-4,"[ STACK ]"); + if(simdata->registers==NULL||simdata->RAM==NULL){ + mvwprintw(general_stack,1,1,"Data not initialised"); + return 0; + } if(width<13){ mvwprintw(general_stack,1,1,"too small window"); }else{ @@ -333,13 +341,13 @@ int update_general_stack(struct simdata_t *simdata){ } // draw data - uint32_t ADDRESS=(simdata->SP-(height/2-2)*4)&0x00FFFFFF; + uint32_t ADDRESS=(simdata->registers->SP-(height/2-2)*4)&0x00FFFFFF; for(int i=0;iSP) + if(ADDRESS==simdata->registers->SP) wattron(general_stack,A_BOLD); mvwprintw(general_stack,i+2,offset_data,"%08X",*(uint32_t*)(simdata->RAM+ADDRESS)); - if(ADDRESS==simdata->SP) + if(ADDRESS==simdata->registers->SP) wattroff(general_stack,A_BOLD); ADDRESS=(ADDRESS+4)&0xFFFFFF; } @@ -375,13 +383,17 @@ int update_general_memdump(struct simdata_t *simdata){ getmaxyx(general_memdump,height,width); box(general_memdump, 0 , 0); mvwprintw(general_memdump,0,terminal_width/4-6,"[ MEMDUMP ]"); + if(simdata->registers==NULL||simdata->RAM==NULL){ + mvwprintw(general_memdump,1,1,"Data not initialised"); + return 0; + } if(width<16){ mvwprintw(general_memdump,1,1,"too small window"); }else{ int n=(width-12)/4; // bytes in each line int usable_height=height-3-(width-12)%2; - uint32_t ADDRESS=(simdata->PC-n*usable_height/2)&0x00FFFFFF; + uint32_t ADDRESS=(simdata->registers->PC-n*usable_height/2)&0x00FFFFFF; uint32_t color_addr; int have_color; @@ -391,7 +403,7 @@ int update_general_memdump(struct simdata_t *simdata){ wprintw(general_memdump,"%06x ",ADDRESS); uint32_t temp_address=ADDRESS; for (int i=0;i=simdata->PC&&ADDRESS<=simdata->PC+3) + if(ADDRESS>=simdata->registers->PC&&ADDRESS<=simdata->registers->PC+3) wattron(general_memdump,A_BOLD); else wattroff(general_memdump,A_BOLD); @@ -412,7 +424,7 @@ int update_general_memdump(struct simdata_t *simdata){ select_instruction_color(simdata,general_memdump,color_addr,0); ADDRESS=temp_address; for (int i=0;i=simdata->PC&&ADDRESS<=simdata->PC+3) + if(ADDRESS>=simdata->registers->PC&&ADDRESS<=simdata->registers->PC+3) wattron(general_memdump,A_BOLD); else wattroff(general_memdump,A_BOLD); @@ -430,6 +442,10 @@ int update_general_registers(struct simdata_t *simdata){ getmaxyx(general_registers,height,width); box(general_registers, 0 , 0); mvwprintw(general_registers,0,terminal_width/4-7,"[ REGISTERS ]"); + if(simdata->registers==NULL){ + mvwprintw(general_registers,1,1,"Data not initialised"); + return 0; + } if(simdata->registers==NULL) mvwprintw(general_registers,1,1,"Registers data structure not initialised"); else{ diff --git a/simdata.c b/simdata.c index 6218187..dad352d 100644 --- a/simdata.c +++ b/simdata.c @@ -36,8 +36,6 @@ struct simdata_t *init_simdata(){ return 0; } - ret->PC=0; - ret->SP=0; ret->registers=NULL; ret->cpu_state=CPU_RUNNING; ret->cpu_gui_hints=malloc(sizeof(struct cpu_gui_hints_t)); diff --git a/simdata.h b/simdata.h index c83f6b3..8999d5f 100644 --- a/simdata.h +++ b/simdata.h @@ -9,8 +9,6 @@ enum CPU_STATE_t{ struct simdata_t{ long unsigned int current_clock; uint8_t *RAM; - uint32_t PC; - uint32_t SP; struct fetch_data_t *fetch_data; struct decode_data_t *decode_data; struct exec_data_t *exec_data;