2024-01-23 13:33:04 +00:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
#include <ncurses.h>
|
2024-01-23 17:38:44 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "simdata.h"
|
2024-01-29 12:48:53 +00:00
|
|
|
#include "assembly.h"
|
|
|
|
#include <stdlib.h>
|
2024-01-22 12:11:07 +00:00
|
|
|
|
2024-01-26 16:11:57 +00:00
|
|
|
#define GENERAL_STACK_MAX_WIDTH 16
|
2024-01-29 12:48:53 +00:00
|
|
|
#define GENERAL_DISAS_WIDTH 41
|
2024-01-26 16:11:57 +00:00
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
int terminal_width;
|
|
|
|
int terminal_height;
|
|
|
|
|
|
|
|
WINDOW *tabs;
|
2024-01-23 19:35:57 +00:00
|
|
|
WINDOW *general_memdump;
|
2024-01-26 16:11:57 +00:00
|
|
|
WINDOW *general_stack;
|
2024-01-29 12:48:53 +00:00
|
|
|
WINDOW *general_disas;
|
2024-01-22 12:11:07 +00:00
|
|
|
|
2024-01-23 13:33:04 +00:00
|
|
|
int monochrome;
|
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gui_ncurses_refresh(){
|
|
|
|
if(refresh()==ERR)
|
|
|
|
return 1;
|
|
|
|
if(wrefresh(tabs)==ERR)
|
|
|
|
return 1;
|
2024-01-23 19:35:57 +00:00
|
|
|
if(wrefresh(general_memdump)==ERR)
|
|
|
|
return 1;
|
2024-01-26 16:11:57 +00:00
|
|
|
if(wrefresh(general_stack)==ERR)
|
|
|
|
return 1;
|
2024-01-29 12:48:53 +00:00
|
|
|
if(wrefresh(general_disas)==ERR)
|
|
|
|
return 1;
|
2024-01-22 12:11:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:35:57 +00:00
|
|
|
char *tab_name[]={"Overview","Memory","Internal"};
|
2024-01-23 17:38:44 +00:00
|
|
|
|
|
|
|
unsigned int CURRENT_TAB=0;
|
|
|
|
|
2024-01-23 19:35:57 +00:00
|
|
|
enum CPU_STATE_t CPU_STATE=SINGLE_STEPPING;
|
|
|
|
//enum CPU_STATE_t CPU_STATE=STOPPED;
|
2024-01-23 17:38:44 +00:00
|
|
|
|
|
|
|
void update_tabs(){
|
2024-01-26 16:11:57 +00:00
|
|
|
wattron(tabs,A_BOLD);
|
2024-01-23 17:38:44 +00:00
|
|
|
wattron(tabs,COLOR_PAIR(2));
|
|
|
|
for(int i=0;i<terminal_width;i++)
|
2024-01-23 19:35:57 +00:00
|
|
|
mvwprintw(tabs,0,i," ");
|
2024-01-23 17:38:44 +00:00
|
|
|
|
2024-01-23 19:35:57 +00:00
|
|
|
int x=2;
|
2024-01-23 17:38:44 +00:00
|
|
|
for(unsigned int i=0;i<sizeof(tab_name)/sizeof(tab_name[0]);i++){
|
|
|
|
if(i==CURRENT_TAB)
|
|
|
|
wattron(tabs,COLOR_PAIR(4));
|
|
|
|
else
|
|
|
|
wattron(tabs,COLOR_PAIR(5));
|
|
|
|
mvwprintw(tabs,0,x," %1d",i);
|
|
|
|
x+=2;
|
|
|
|
if(i==CURRENT_TAB)
|
|
|
|
wattron(tabs,COLOR_PAIR(3));
|
2024-01-26 16:11:57 +00:00
|
|
|
else{
|
2024-01-23 17:38:44 +00:00
|
|
|
wattron(tabs,COLOR_PAIR(2));
|
2024-01-26 16:11:57 +00:00
|
|
|
wattroff(tabs,A_BOLD);
|
|
|
|
}
|
2024-01-23 17:38:44 +00:00
|
|
|
mvwprintw(tabs,0,x," %s ",tab_name[i]);
|
2024-01-26 16:11:57 +00:00
|
|
|
wattron(tabs,A_BOLD);
|
2024-01-23 17:38:44 +00:00
|
|
|
x+=strlen(tab_name[i])+2+3;
|
|
|
|
}
|
|
|
|
switch(CPU_STATE){
|
|
|
|
case RUNNING:
|
|
|
|
wattron(tabs,COLOR_PAIR(6));
|
|
|
|
mvwprintw(tabs,0,terminal_width-13,"[ RUNNING ");
|
|
|
|
break;
|
|
|
|
case SINGLE_STEPPING:
|
|
|
|
wattron(tabs,COLOR_PAIR(7));
|
|
|
|
mvwprintw(tabs,0,terminal_width-21,"[ SINGLE-STEPPING ");
|
|
|
|
break;
|
|
|
|
case STOPPED:
|
|
|
|
wattron(tabs,COLOR_PAIR(8));
|
|
|
|
mvwprintw(tabs,0,terminal_width-13,"[ STOPPED ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mvwprintw(tabs,0,terminal_width-3,"]");
|
|
|
|
}
|
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
int start_gui(){
|
|
|
|
if(!initscr())
|
|
|
|
return 1;
|
2024-01-23 13:33:04 +00:00
|
|
|
monochrome=( has_colors() == FALSE );
|
2024-01-23 17:38:44 +00:00
|
|
|
init_color(8, 700, 700, 700);
|
|
|
|
init_color(COLOR_WHITE, 100, 1000, 1000);
|
2024-01-23 13:33:04 +00:00
|
|
|
if(!monochrome){
|
|
|
|
start_color();
|
|
|
|
init_pair(1, COLOR_RED, COLOR_BLACK);
|
2024-01-23 17:38:44 +00:00
|
|
|
|
|
|
|
//tab colors
|
|
|
|
init_pair(2, COLOR_BLACK, 8);
|
|
|
|
init_pair(3, 8, COLOR_BLACK);
|
|
|
|
init_pair(4, COLOR_WHITE, COLOR_BLACK);
|
|
|
|
init_pair(5, COLOR_WHITE, 8);
|
|
|
|
init_pair(6, COLOR_BLACK,COLOR_GREEN);
|
|
|
|
init_pair(7, COLOR_BLACK,COLOR_YELLOW);
|
|
|
|
init_pair(8, COLOR_BLACK,COLOR_RED);
|
2024-01-23 13:33:04 +00:00
|
|
|
}
|
2024-01-23 16:06:28 +00:00
|
|
|
noecho();
|
2024-01-22 12:11:07 +00:00
|
|
|
curs_set(0);
|
|
|
|
get_terminal_size();
|
|
|
|
mvprintw((terminal_height-3)/2+3,terminal_width/2-15,"Initialising the simulator...");
|
2024-01-23 17:38:44 +00:00
|
|
|
tabs = newwin(1,terminal_width,0,0);
|
|
|
|
update_tabs();
|
2024-01-29 12:48:53 +00:00
|
|
|
int len;
|
2024-01-26 16:11:57 +00:00
|
|
|
int x=4;
|
|
|
|
general_memdump=newwin(terminal_height*0.8,terminal_width/2,x,5);
|
|
|
|
x+=terminal_width/2;
|
2024-01-29 12:48:53 +00:00
|
|
|
x++;
|
|
|
|
|
|
|
|
x+=3;
|
|
|
|
len=(terminal_width/6)>GENERAL_DISAS_WIDTH?GENERAL_DISAS_WIDTH:(terminal_width/6);
|
|
|
|
general_disas=newwin(terminal_height*0.8,len,4,x);
|
|
|
|
x+=len;
|
2024-01-26 16:11:57 +00:00
|
|
|
|
|
|
|
x+=3;
|
2024-01-29 12:48:53 +00:00
|
|
|
len=(terminal_width/6)>GENERAL_STACK_MAX_WIDTH?GENERAL_STACK_MAX_WIDTH:(terminal_width/6);
|
2024-01-26 16:11:57 +00:00
|
|
|
general_stack=newwin(terminal_height*0.8,len,4,x);
|
|
|
|
x+=len;
|
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
if(gui_ncurses_refresh())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-23 13:33:04 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2024-01-29 12:48:53 +00:00
|
|
|
int update_general_disas(struct simdata_t *simdata){
|
|
|
|
int width,height;
|
|
|
|
getmaxyx(general_disas,height,width);
|
|
|
|
box(general_disas, 0 , 0);
|
|
|
|
int offset_arrow;
|
|
|
|
if(width<41)
|
|
|
|
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-(height/2-2)*3)&0x00FFFFFF;
|
|
|
|
for (int i=2;i<height-2;i++){
|
|
|
|
int overall_offset=offset_arrow-2;
|
|
|
|
|
|
|
|
mvwaddch(general_disas,i,11+overall_offset,ACS_VLINE);
|
|
|
|
|
|
|
|
if(ADDRESS==simdata->PC)
|
|
|
|
wattron(general_disas,A_BOLD);
|
|
|
|
mvwprintw(general_disas,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;
|
|
|
|
mvwprintw(general_disas,i,13+overall_offset,"%s",disas);
|
|
|
|
if(ADDRESS==simdata->PC)
|
|
|
|
wattroff(general_disas,A_BOLD);
|
|
|
|
|
|
|
|
ADDRESS=(ADDRESS+3)&0xFFFFFF;
|
|
|
|
free(disas);
|
|
|
|
}
|
|
|
|
mvwaddch(general_disas,height/2,offset_arrow,ACS_RARROW);
|
|
|
|
mvwaddch(general_disas,height/2,offset_arrow-1,ACS_ULCORNER);
|
|
|
|
mvwaddch(general_disas,height/2+1,offset_arrow-1,ACS_VLINE);
|
|
|
|
mvwaddch(general_disas,height/2+2,offset_arrow-1,ACS_VLINE);
|
|
|
|
mvwaddch(general_disas,height/2+3,offset_arrow-1,ACS_VLINE);
|
|
|
|
mvwaddch(general_disas,height/2+4,offset_arrow-1,ACS_VLINE);
|
|
|
|
mvwaddch(general_disas,height/2+5,offset_arrow-1,ACS_VLINE);
|
|
|
|
mvwaddch(general_disas,height/2+6,offset_arrow-1,'P');
|
|
|
|
if(offset_arrow==2)
|
|
|
|
mvwaddch(general_disas,height/2+7,offset_arrow-1,'C');
|
|
|
|
else
|
|
|
|
mvwaddch(general_disas,height/2+6,offset_arrow,'C');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-26 16:11:57 +00:00
|
|
|
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(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->SP-(height/2-2)*4)&0x00FFFFFF;
|
|
|
|
|
|
|
|
for(int i=0;i<height-4;i++){
|
|
|
|
if(ADDRESS==simdata->SP)
|
|
|
|
wattron(general_stack,A_BOLD);
|
|
|
|
mvwprintw(general_stack,i+2,offset_data,"%08X",*(uint32_t*)(simdata->RAM+ADDRESS));
|
|
|
|
if(ADDRESS==simdata->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;
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:35:57 +00:00
|
|
|
int update_general_memdump(struct simdata_t *simdata){
|
|
|
|
int width,height;
|
|
|
|
getmaxyx(general_memdump,height,width);
|
|
|
|
box(general_memdump, 0 , 0);
|
|
|
|
mvwprintw(general_memdump,0,terminal_width/4-6,"[ MEMDUMP ]");
|
|
|
|
if(width<16){
|
|
|
|
mvwprintw(general_memdump,1,1,"too small window");
|
|
|
|
}else{
|
2024-01-23 23:56:40 +00:00
|
|
|
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;
|
|
|
|
for(int h=0;h<usable_height;h++){
|
2024-01-23 19:35:57 +00:00
|
|
|
wmove(general_memdump,h+2,2+(width-12)%2);
|
|
|
|
wprintw(general_memdump,"%06x ",ADDRESS);
|
|
|
|
uint32_t temp_address=ADDRESS;
|
2024-01-26 12:24:43 +00:00
|
|
|
for (int i=0;i<n;i++){
|
|
|
|
wprintw(general_memdump,"%02x ",simdata->RAM[ADDRESS]);
|
|
|
|
ADDRESS=(ADDRESS+1)&0xFFFFFF;
|
|
|
|
}
|
|
|
|
ADDRESS=temp_address;
|
2024-01-23 19:35:57 +00:00
|
|
|
for (int i=0;i<n;i++){
|
|
|
|
wprintw(general_memdump,"%c",(simdata->RAM[ADDRESS]>=0x20&&simdata->RAM[ADDRESS]<0x7F)?simdata->RAM[ADDRESS]:'.');
|
2024-01-23 23:56:40 +00:00
|
|
|
ADDRESS=(ADDRESS+1)&0xFFFFFF;
|
2024-01-23 19:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int update_gui(struct simdata_t *simdata){
|
|
|
|
clear();
|
|
|
|
update_tabs();
|
|
|
|
if(update_general_memdump(simdata))
|
|
|
|
return 1;
|
2024-01-26 16:11:57 +00:00
|
|
|
if(update_general_stack(simdata))
|
|
|
|
return 1;
|
2024-01-29 12:48:53 +00:00
|
|
|
if(update_general_disas(simdata))
|
|
|
|
return 1;
|
2024-01-23 19:35:57 +00:00
|
|
|
if(gui_ncurses_refresh())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-01-22 12:11:07 +00:00
|
|
|
int gui_continue_request(){
|
|
|
|
if(getch()==ERR)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int end_gui(){
|
|
|
|
if(endwin()==ERR)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|