/* gui.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 . */ #include #include #include "simdata.h" #include "assembly.h" #include #include "cpu.h" #include "gui.h" #include #include "gui_display.h" #include "gui_internals.h" #define GENERAL_STACK_MAX_WIDTH 16 #define GENERAL_DISAS_WIDTH 41 int terminal_width; int terminal_height; struct gui_settings_t *gui_settings; WINDOW *tabs; WINDOW *general_memdump; WINDOW *general_stack; WINDOW *general_disas; WINDOW *general_registers; WINDOW *general_terminal_output; WINDOW *memory_memdump; WINDOW *internals_internals; WINDOW *internals_disas; WINDOW *debug_simdata; WINDOW *display_terminal_output; int monochrome; int get_terminal_size(){ int new_height,new_width; getmaxyx(stdscr,new_height,new_width); int changed=(new_width!=terminal_width)||(new_height!=terminal_height); terminal_width=new_width; terminal_height=new_height; return changed; } char *tab_name[]={"Overview","Memory","Internals","Debug","Display"}; unsigned int CURRENT_TAB=1; int gui_ncurses_refresh(){ if(refresh()==ERR) return 1; if(wrefresh(tabs)==ERR) return 1; if(CURRENT_TAB==1){ if(wrefresh(general_memdump)==ERR) return 1; if(wrefresh(general_stack)==ERR) return 1; if(wrefresh(general_disas)==ERR) return 1; if(wrefresh(general_registers)==ERR) return 1; if(wrefresh(general_terminal_output)==ERR) return 1; }else if(CURRENT_TAB==2){ if(wrefresh(memory_memdump)==ERR) return 1; }else if(CURRENT_TAB==3){ if(wrefresh(internals_disas)==ERR) return 1; if(wrefresh(internals_internals)==ERR) return 1; }else if(CURRENT_TAB==4){ if(wrefresh(debug_simdata)==ERR) return 1; }else if(CURRENT_TAB==5){ if(wrefresh(display_terminal_output)==ERR) return 1; } return 0; } enum GUI_CPU_STATE_t GUI_CPU_STATE=GUI_CPU_SINGLE_STEPPING; void update_tabs(){ wattron(tabs,A_BOLD); wattron(tabs,COLOR_PAIR(2)); for(int i=0;iGENERAL_DISAS_WIDTH?GENERAL_DISAS_WIDTH:(terminal_width/6); general_disas=newwin(y2_divider,len,4,x); x+=len; x+=3; len=(terminal_width/6)>GENERAL_STACK_MAX_WIDTH?GENERAL_STACK_MAX_WIDTH:(terminal_width/6); general_stack=newwin(y2_divider,len,4,x); x+=len; general_registers=newwin(terminal_height-y1_divider-border-1,terminal_width/2,y1_divider+border+1,border); x+=terminal_width/2; x++; general_terminal_output=newwin(terminal_height-y2_divider-border-1,terminal_width/2-3*border,y2_divider+border+1,middle); scrollok(general_terminal_output,TRUE); x+=terminal_width/2; x++; memory_memdump=newwin(terminal_height-1-border*2,terminal_width-border*2,1+border,border); debug_simdata=newwin(terminal_height-1-border*2,terminal_width-border*2,1+border,border); internals_internals=newwin(terminal_height-1-border*2,terminal_width-border*3-GENERAL_DISAS_WIDTH,1+border,border); internals_disas=newwin(terminal_height-1-border*2,GENERAL_DISAS_WIDTH,1+border,border+terminal_width-border*3-GENERAL_DISAS_WIDTH); display_terminal_output=newwin(terminal_height-1,terminal_width,1,0); gui_settings=malloc(sizeof(struct gui_settings_t)); gui_settings->cycles_per_frame=100; if(gui_ncurses_refresh()) return 1; selected_fb_driver=fb_driver; return 0; } int gui_error(char *str){ WINDOW *error_win = newwin(3,40,terminal_height/2-1,terminal_width/2-20); box(error_win, 0 , 0); if(!monochrome) wattron(error_win,COLOR_PAIR(1)); mvwprintw(error_win,1,1,"%s",str); if(wrefresh(error_win)==ERR) return 1; getch(); delwin(error_win); return 0; if(!monochrome) wattroff(error_win,COLOR_PAIR(1)); } //// WARNING!! THIS ASSUMES THAT ADDRESS IS CORRECTLY ALIGNED WITH THE INSTRUCTION START/END!! int select_instruction_color(struct simdata_t *simdata, WINDOW* window,uint32_t ADDRESS, int applyremove){ if(simdata->cpu_gui_hints){ struct instr_list_t* pointer; pointer=simdata->cpu_gui_hints->fetching_list; while(pointer!=NULL){ if(pointer->address==ADDRESS){ if(applyremove) wattron(window,COLOR_PAIR(9)); else wattroff(window,COLOR_PAIR(9)); return 0; } pointer=pointer->next; } pointer=simdata->cpu_gui_hints->decoding_list; while(pointer!=NULL){ if(pointer->address==ADDRESS){ if(applyremove) wattron(window,COLOR_PAIR(10)); else wattroff(window,COLOR_PAIR(10)); return 0; } pointer=pointer->next; } pointer=simdata->cpu_gui_hints->executing_list; while(pointer!=NULL){ if(pointer->address==ADDRESS){ if(applyremove) wattron(window,COLOR_PAIR(11)); else wattroff(window,COLOR_PAIR(11)); return 0; } pointer=pointer->next; } } return 1; } int update_disas(WINDOW *win,struct simdata_t *simdata){ werase(win); int width,height; getmaxyx(win,height,width); box(win, 0 , 0); mvwprintw(win,0,width/2-7,"[ DISASSEMBLY ]"); if(simdata->registers==NULL||simdata->RAM==NULL){ mvwprintw(win,1,1,"Data not initialised"); return 0; } int offset_arrow; int have_legend=(width>29); int usable_height=height-2-have_legend; if(width<41) offset_arrow=2; else offset_arrow=3; if(width<18){ mvwprintw(win,1,1,"too small window"); }else{ uint32_t ADDRESS=(simdata->registers->PC-(usable_height/2-2)*4)&0x00FFFFFF; for (int i=2;iregisters->PC) wattron(win,A_BOLD); mvwprintw(win,i,4+overall_offset,"%06X",ADDRESS); char* disas=disassemble(*(uint32_t*)(simdata->RAM+ADDRESS)); int space_left=width-14-2+(overall_offset?0:1); if(space_left<25) disas[space_left+1]=0; select_instruction_color(simdata,win,ADDRESS,1); mvwprintw(win,i,13+overall_offset,"%s",disas); select_instruction_color(simdata,win,ADDRESS,0); if(ADDRESS==simdata->registers->PC) wattroff(win,A_BOLD); ADDRESS=(ADDRESS+4)&0xFFFFFF; free(disas); } mvwaddch(win,usable_height/2,offset_arrow,ACS_RARROW); mvwaddch(win,usable_height/2,offset_arrow-1,ACS_ULCORNER); mvwaddch(win,usable_height/2+1,offset_arrow-1,ACS_VLINE); mvwaddch(win,usable_height/2+2,offset_arrow-1,ACS_VLINE); mvwaddch(win,usable_height/2+3,offset_arrow-1,ACS_VLINE); mvwaddch(win,usable_height/2+4,offset_arrow-1,ACS_VLINE); mvwaddch(win,usable_height/2+5,offset_arrow-1,ACS_VLINE); mvwaddch(win,usable_height/2+6,offset_arrow-1,'P'); if(offset_arrow==2) mvwaddch(win,usable_height/2+7,offset_arrow-1,'C'); else mvwaddch(win,usable_height/2+6,offset_arrow,'C'); if(have_legend){ wattron(win,COLOR_PAIR(12)); mvwprintw(win,height-2,2," "); wattroff(win,COLOR_PAIR(12)); mvwprintw(win,height-2,4,"=Fetch"); wattron(win,COLOR_PAIR(13)); mvwprintw(win,height-2,11," "); wattroff(win,COLOR_PAIR(13)); mvwprintw(win,height-2,13,"=Decode"); wattron(win,COLOR_PAIR(14)); mvwprintw(win,height-2,21," "); wattroff(win,COLOR_PAIR(14)); mvwprintw(win,height-2,23,"=Exec"); } } return 0; } int update_general_stack(struct simdata_t *simdata){ int width,height; 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{ // Calculate config based on window size int offset_data,offset_arrow; if(width<14){ offset_data=3; offset_arrow=2; }else{ offset_data=5; offset_arrow=3; } // draw data uint32_t ADDRESS=(simdata->registers->SP-(height/2-2)*4)&0x00FFFFFF; for(int i=0;iregisters->SP) wattron(general_stack,A_BOLD); mvwprintw(general_stack,i+2,offset_data,"%08X",*(uint32_t*)(simdata->RAM+ADDRESS)); if(ADDRESS==simdata->registers->SP) wattroff(general_stack,A_BOLD); ADDRESS=(ADDRESS+4)&0xFFFFFF; } // draw graphics mvwaddch(general_stack,height/4-2,offset_arrow-1,ACS_UARROW); mvwaddch(general_stack,height/4-1,offset_arrow-1,ACS_VLINE); mvwaddch(general_stack,height/4 ,offset_arrow-1,ACS_VLINE); mvwaddch(general_stack,height/4+1,offset_arrow-1,ACS_VLINE); if(offset_arrow!=2) mvwprintw(general_stack,height/4+2,1,"(+)"); else mvwprintw(general_stack,height/4+2,offset_arrow-1,"+"); mvwaddch(general_stack,height/2,offset_arrow,ACS_RARROW); mvwaddch(general_stack,height/2,offset_arrow-1,ACS_ULCORNER); mvwaddch(general_stack,height/2+1,offset_arrow-1,ACS_VLINE); mvwaddch(general_stack,height/2+2,offset_arrow-1,ACS_VLINE); mvwaddch(general_stack,height/2+3,offset_arrow-1,ACS_VLINE); mvwaddch(general_stack,height/2+4,offset_arrow-1,'S'); if(offset_arrow==2) mvwaddch(general_stack,height/2+5,offset_arrow-1,'P'); else mvwaddch(general_stack,height/2+4,offset_arrow,'P'); } return 0; } //address: If within legal limit (address<0x00FFFFFF) then address*bytes_per_line will be used for the center of the memdump // otherwise PC will be used int update_memdump(WINDOW *win,uint32_t address,struct simdata_t *simdata){ int width,height; getmaxyx(win,height,width); box(win, 0 , 0); mvwprintw(win,0,width/2-5,"[ MEMORY ]"); if(simdata->registers==NULL||simdata->RAM==NULL){ mvwprintw(win,1,1,"Data not initialised"); return 0; } if(width<16){ mvwprintw(win,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 center_address; if(address<=0x00FFFFFF) center_address=address*n; else center_address=simdata->registers->PC; uint32_t ADDRESS=(center_address-n*usable_height/2)&0x00FFFFFF; uint32_t color_addr; int have_color=0; for(int h=0;h=simdata->registers->PC&&ADDRESS<=simdata->registers->PC+3) wattron(win,A_BOLD); else wattroff(win,A_BOLD); if(select_instruction_color(simdata,win,ADDRESS,1)==0){ have_color=5; color_addr=ADDRESS; }else if(have_color) select_instruction_color(simdata,win,color_addr,1); if(have_color){ have_color--; if(!have_color) select_instruction_color(simdata,win,color_addr,0); } wprintw(win,"%02x ",simdata->RAM[ADDRESS]); ADDRESS=(ADDRESS+1)&0xFFFFFF; } if(have_color) select_instruction_color(simdata,win,color_addr,0); ADDRESS=temp_address; for (int i=0;i=simdata->registers->PC&&ADDRESS<=simdata->registers->PC+3) wattron(win,A_BOLD); else wattroff(win,A_BOLD); wprintw(win,"%c",(simdata->RAM[ADDRESS]>=0x20&&simdata->RAM[ADDRESS]<0x7F)?simdata->RAM[ADDRESS]:'.'); ADDRESS=(ADDRESS+1)&0xFFFFFF; } } } return 0; } uint32_t memory_window_address=0x00000000; int update_memory_memdump(struct simdata_t *simdata){ return update_memdump(memory_memdump,memory_window_address,simdata); } int update_general_memdump(struct simdata_t *simdata){ return update_memdump(general_memdump,0xFFFFFFFF,simdata); } int update_general_registers(struct simdata_t *simdata){ int width,height; werase(general_registers); getmaxyx(general_registers,height,width); box(general_registers, 0 , 0); mvwprintw(general_registers,0,width/2-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{ int per_line=width/35; if(per_line==0) mvwprintw(general_registers,1,1,"too small window"); else{ int center_x_offset=((width-2)-35*per_line)/2; int lines_used=(8/(int)per_line<8/(float)per_line)?8/per_line+1:8/per_line; int lines_for_special_registers; lines_for_special_registers=( width < 22+27+8 )?2:1; int center_y_offset=((height-2)-(lines_used+lines_for_special_registers+1))/2; if((lines_used+lines_for_special_registers+1)>(height-2)||width<37) mvwprintw(general_registers,1,1,"too small window"); else{ int n=0,y=0; while(y!=lines_used){ for(int i=0;iregisters->GPR+n); if(float_equiv<9999999999.0&&float_equiv>-9999999999.0) mvwprintw(general_registers,1+y+center_y_offset,1+i*35+center_x_offset,"R%d: %08X (%08f) ",n,simdata->registers->GPR[n],float_equiv); else mvwprintw(general_registers,1+y+center_y_offset,1+i*35+center_x_offset,"R%d: %08X (%cinf) ",n,simdata->registers->GPR[n],(float_equiv>0)?'+':'-'); n++; } } y++; } for(int i=0;i<35*per_line;i++) mvwaddch(general_registers,1+center_y_offset+lines_used,center_x_offset+i,ACS_HLINE); int spec_reg_1_offset, spec_reg_2_offset; if(lines_for_special_registers==2){ spec_reg_1_offset=((width-2)/2)-(23/2); spec_reg_2_offset=((width-2)/2)-(27/2); }else{ spec_reg_1_offset=((width-2)/2)-(22+27+8)/2; spec_reg_2_offset=spec_reg_1_offset+22+8; } wattron(general_registers,A_BOLD); mvwprintw(general_registers,1+center_y_offset+lines_used+1,spec_reg_1_offset," PC"); wattroff(general_registers,A_BOLD); wprintw(general_registers,": %06X ",simdata->registers->PC); wattron(general_registers,A_BOLD); wprintw(general_registers,"SP"); wattroff(general_registers,A_BOLD); wprintw(general_registers,": %06X ",simdata->registers->SP); wattron(general_registers,A_BOLD); mvwprintw(general_registers,center_y_offset+lines_used+1+lines_for_special_registers,spec_reg_2_offset," ZERO"); wattroff(general_registers,A_BOLD); wprintw(general_registers,": %c ",(simdata->registers->FLAGS&1)?'1':'0'); wattron(general_registers,A_BOLD); wprintw(general_registers,"CARRY"); wattroff(general_registers,A_BOLD); wprintw(general_registers,": %c ",(simdata->registers->FLAGS&2)?'1':'0'); wattron(general_registers,A_BOLD); wprintw(general_registers,"SIGN"); wattroff(general_registers,A_BOLD); wprintw(general_registers,": %c ",(simdata->registers->FLAGS&4)?'1':'0'); } } } return 0; } int update_debug_simdata(struct simdata_t *simdata){ int width,height; werase(debug_simdata); getmaxyx(debug_simdata,height,width); box(debug_simdata, 0 , 0); mvwprintw(debug_simdata,0,width/2-5,"[ SIMDATA ]"); if(simdata->registers==NULL){ mvwprintw(debug_simdata,1,1,"Data not initialised"); return 0; } int term_size; char* term_size_unit; if(simdata->terminal_output_size<(1024)) term_size=simdata->terminal_output_size,term_size_unit="Bytes"; else if(simdata->terminal_output_size<(1024*1024)) term_size=simdata->terminal_output_size/1024,term_size_unit="KiB"; else if(simdata->terminal_output_size<(1024*1024*1024)) term_size=simdata->terminal_output_size/(1024*1024),term_size_unit="MiB"; else term_size=simdata->terminal_output_size/(1024*1024*1024),term_size_unit="GiB"; mvwprintw(debug_simdata,1,1,"simdata->terminal_output_size: "); if(simdata->terminal_output_size<100*1024) wattron(debug_simdata,COLOR_PAIR(11)); else if (simdata->terminal_output_size<500*1024) wattron(debug_simdata,COLOR_PAIR(10)); else wattron(debug_simdata,COLOR_PAIR(15)); wprintw(debug_simdata,"%d%s",term_size,term_size_unit); if(simdata->terminal_output_size<100*1024) wattroff(debug_simdata,COLOR_PAIR(11)); else if (simdata->terminal_output_size<500*1024) wattroff(debug_simdata,COLOR_PAIR(10)); else wattroff(debug_simdata,COLOR_PAIR(15)); if(height==3) return 0; else return 0; } int force_disp_terminal_output_update(struct simdata_t *simdata){ if(update_display_terminal_output(display_terminal_output,simdata)) return 1; if(gui_ncurses_refresh()) return 1; if(simdata) return 0; else return 0; } int clear_back_window=1; int write_tabs=1; int update_gui(struct simdata_t *simdata){ if(simdata->cpu_state==CPU_HALTED){ GUI_CPU_STATE=GUI_CPU_STOPPED; if(CURRENT_TAB==5) force_disp_terminal_output_update(simdata); } if(clear_back_window){ clear(); clear_back_window=0; } if(write_tabs){ update_tabs(); write_tabs=0; } if((simdata->current_clock)%(gui_settings->cycles_per_frame)==0 || GUI_CPU_STATE!=GUI_CPU_RUNNING ){ if(CURRENT_TAB==1){ if(update_general_memdump(simdata)) return 1; if(update_general_stack(simdata)) return 1; if(update_disas(general_disas,simdata)) return 1; if(update_general_registers(simdata)) return 1; if(update_general_terminal_output(general_terminal_output,simdata)) return 1; }else if(CURRENT_TAB==2){ if(update_memory_memdump(simdata)) return 1; }else if(CURRENT_TAB==3){ if(update_disas(internals_disas,simdata)) return 1; if(update_internals(internals_internals,simdata)) return 1; }else if(CURRENT_TAB==4){ if(update_debug_simdata(simdata)) return 1; }else if(CURRENT_TAB==5){ if(GUI_CPU_STATE!=GUI_CPU_RUNNING) force_disp_terminal_output_update(simdata); } if(CURRENT_TAB!=5) if(gui_ncurses_refresh()) return 1; } return 0; } int gui_continue_request(struct simdata_t *simdata){ int inch; int release=0; while(release==0){ inch=ERR; if(((simdata->current_clock)%(gui_settings->cycles_per_frame))==0 || GUI_CPU_STATE!=GUI_CPU_RUNNING ){ if(((inch=getch())==ERR)){ if(GUI_CPU_STATE==GUI_CPU_SINGLE_STEPPING) return 1; else release=1; } }else release=1; switch(inch){ case 'r': if(simdata->cpu_state!=CPU_HALTED){ if(GUI_CPU_STATE==GUI_CPU_RUNNING){ GUI_CPU_STATE=GUI_CPU_SINGLE_STEPPING; if(CURRENT_TAB!=5) update_gui(simdata); nodelay(stdscr, FALSE); }else{ GUI_CPU_STATE=GUI_CPU_RUNNING; nodelay(stdscr, TRUE); } } break; case 'q': return 2; case '\n': release=1; break; case '1': if(CURRENT_TAB!=1){ CURRENT_TAB=1; clear(); update_tabs(); update_gui(simdata); } break; case '2': if(CURRENT_TAB!=2){ CURRENT_TAB=2; clear(); update_tabs(); update_gui(simdata); } break; case '3': if(CURRENT_TAB!=3){ CURRENT_TAB=3; clear(); update_tabs(); update_gui(simdata); } break; case '4': if(CURRENT_TAB!=4){ CURRENT_TAB=4; clear(); update_tabs(); update_gui(simdata); } break; case '5': if(CURRENT_TAB!=5){ CURRENT_TAB=5; clear(); update_tabs(); force_disp_terminal_output_update(simdata); } break; case 'j': if(CURRENT_TAB==2){ memory_window_address++; if(memory_window_address==0x01000000) memory_window_address=0x00000000; update_gui(simdata); } break; case 'k': if(CURRENT_TAB==2){ memory_window_address--; if(memory_window_address==0xFFFFFFFF) memory_window_address=0x00FFFFFF; update_gui(simdata); } break; default: break; } } return 0; } uint32_t highest_disps=0;//XXX: remember that if we create a clear screen command it should zero this out too int clean_cpu_output(struct simdata_t *simdata){ uint32_t last_vect_disp=0; uint32_t second_last_vect_disp=simdata->terminal_output_size/4;//TODO: I shouldn't have to initialise this. GCC gives a warning erroneously int vector=0; int vstate=0; uint32_t disps_found=0; if(!simdata) return 0; for(uint64_t i=0;i < simdata->terminal_output_size/4;i++){ uint32_t c=simdata->terminal_output[i]; if(vector){ switch(vstate){ case 0: /*start of line*/ if(c==0x00000001) vector=0; else if(c==0x00000003){ second_last_vect_disp=last_vect_disp; last_vect_disp=i*4; disps_found++; }else vstate++; break; case 1: case 2: case 3: vstate++; break; case 4: vstate=0; break; } }else{ switch(c){ case 0xffffffff: vector=1; break; } } } if(highest_disps=2){ int final_data_size=simdata->terminal_output_size-(second_last_vect_disp+4); memmove((uint8_t*)simdata->terminal_output+4,(uint8_t*)simdata->terminal_output+simdata->terminal_output_size-final_data_size,final_data_size); simdata->terminal_output[0]=0xFFFFFFFF; simdata->terminal_output=realloc(simdata->terminal_output,final_data_size+4); simdata->terminal_output_size=final_data_size+4; if(CURRENT_TAB==5) force_disp_terminal_output_update(simdata); } return 0; } int end_gui(){ if(endwin()==ERR) return 1; return 0; }