From 80b8c25caba15313bbd27eec97fa5f436b91e683 Mon Sep 17 00:00:00 2001 From: "(Tim) Efthimis Kritikos" Date: Tue, 27 Feb 2024 02:55:14 +0000 Subject: [PATCH] GUI: Split off the framebuffer writing code to a separate file and added support for AALib for terminals without unicode/braille --- Makefile | 3 +- config.h | 1 + gui.c | 196 +--------------------------- gui.h | 1 + gui_display.c | 351 ++++++++++++++++++++++++++++++++++++++++++++++++++ gui_display.h | 8 ++ main.c | 44 ++++++- 7 files changed, 410 insertions(+), 194 deletions(-) create mode 100644 config.h create mode 100644 gui_display.c create mode 100644 gui_display.h diff --git a/Makefile b/Makefile index 26dcf23..dc92687 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ ADDRESS_SANITIZE=1 all:first -OBJECT_FILES=gui.o main.o simdata.o assembly.o cpu.o +OBJECT_FILES=gui.o gui_display.o main.o simdata.o assembly.o cpu.o MAIN_ROM=programs/3d_renderer.rom ROMS=test.rom ${MAIN_ROM} @@ -25,6 +25,7 @@ else endif CFLAGS::=-O2 $(shell pkg-config ncursesw --cflags) +LDFLAGS::=$(shell aalib-config --libs) ifeq "${ADDRESS_SANITIZE}" "1" CFLAGS += -fsanitize=address -ggdb diff --git a/config.h b/config.h new file mode 100644 index 0000000..e133849 --- /dev/null +++ b/config.h @@ -0,0 +1 @@ +#define USE_AALIB diff --git a/gui.c b/gui.c index e307511..4819713 100644 --- a/gui.c +++ b/gui.c @@ -25,6 +25,7 @@ #include "cpu.h" #include "gui.h" #include +#include "gui_display.h" #define GENERAL_STACK_MAX_WIDTH 16 #define GENERAL_DISAS_WIDTH 41 @@ -145,7 +146,9 @@ void update_tabs(){ mvwprintw(tabs,0,terminal_width-3,"]"); } -int start_gui(){ +enum FB_DRIVER_t selected_fb_driver; + +int start_gui(enum FB_DRIVER_t fb_driver){ setlocale(LC_ALL, ""); if(!initscr()) return 1; @@ -238,6 +241,9 @@ int start_gui(){ if(gui_ncurses_refresh()) return 1; + + selected_fb_driver=fb_driver; + return 0; } @@ -602,194 +608,6 @@ int update_general_registers(struct simdata_t *simdata){ return 0; } -int term_curs_x,term_curs_y; - -wchar_t braille_lookup[256]={ -L'⠀',L'⠁',L'⠂',L'⠃',L'⠄',L'⠅',L'⠆',L'⠇',L'⠈',L'⠉',L'⠊',L'⠋',L'⠌',L'⠍',L'⠎',L'⠏',L'⠐',L'⠑',L'⠒',L'⠓',L'⠔',L'⠕',L'⠖',L'⠗',L'⠘',L'⠙',L'⠚',L'⠛',L'⠜',L'⠝',L'⠞',L'⠟', -L'⠠',L'⠡',L'⠢',L'⠣',L'⠤',L'⠥',L'⠦',L'⠧',L'⠨',L'⠩',L'⠪',L'⠫',L'⠬',L'⠭',L'⠮',L'⠯',L'⠰',L'⠱',L'⠲',L'⠳',L'⠴',L'⠵',L'⠶',L'⠷',L'⠸',L'⠹',L'⠺',L'⠻',L'⠼',L'⠽',L'⠾',L'⠿', -L'⡀',L'⡁',L'⡂',L'⡃',L'⡄',L'⡅',L'⡆',L'⡇',L'⡈',L'⡉',L'⡊',L'⡋',L'⡌',L'⡍',L'⡎',L'⡏',L'⡐',L'⡑',L'⡒',L'⡓',L'⡔',L'⡕',L'⡖',L'⡗',L'⡘',L'⡙',L'⡚',L'⡛',L'⡜',L'⡝',L'⡞',L'⡟',L'⡠',L'⡡',L'⡢',L'⡣',L'⡤',L'⡥',L'⡦',L'⡧',L'⡨',L'⡩',L'⡪',L'⡫',L'⡬',L'⡭',L'⡮',L'⡯',L'⡰',L'⡱',L'⡲',L'⡳',L'⡴',L'⡵',L'⡶',L'⡷',L'⡸',L'⡹',L'⡺',L'⡻',L'⡼',L'⡽',L'⡾',L'⡿', -L'⢀',L'⢁',L'⢂',L'⢃',L'⢄',L'⢅',L'⢆',L'⢇',L'⢈',L'⢉',L'⢊',L'⢋',L'⢌',L'⢍',L'⢎',L'⢏',L'⢐',L'⢑',L'⢒',L'⢓',L'⢔',L'⢕',L'⢖',L'⢗',L'⢘',L'⢙',L'⢚',L'⢛',L'⢜',L'⢝',L'⢞',L'⢟',L'⢠',L'⢡',L'⢢',L'⢣',L'⢤',L'⢥',L'⢦',L'⢧',L'⢨',L'⢩',L'⢪',L'⢫',L'⢬',L'⢭',L'⢮',L'⢯',L'⢰',L'⢱',L'⢲',L'⢳',L'⢴',L'⢵',L'⢶',L'⢷',L'⢸',L'⢹',L'⢺',L'⢻',L'⢼',L'⢽',L'⢾',L'⢿', -L'⣀',L'⣁',L'⣂',L'⣃',L'⣄',L'⣅',L'⣆',L'⣇',L'⣈',L'⣉',L'⣊',L'⣋',L'⣌',L'⣍',L'⣎',L'⣏',L'⣐',L'⣑',L'⣒',L'⣓',L'⣔',L'⣕',L'⣖',L'⣗',L'⣘',L'⣙',L'⣚',L'⣛',L'⣜',L'⣝',L'⣞',L'⣟',L'⣠',L'⣡',L'⣢',L'⣣',L'⣤',L'⣥',L'⣦',L'⣧',L'⣨',L'⣩',L'⣪',L'⣫',L'⣬',L'⣭',L'⣮',L'⣯',L'⣰',L'⣱',L'⣲',L'⣳',L'⣴',L'⣵',L'⣶',L'⣷',L'⣸',L'⣹',L'⣺',L'⣻',L'⣼',L'⣽',L'⣾',L'⣿', -}; - -int print_braille_frame_buffer(WINDOW* win,uint8_t *buffer,int width,int height,int fullscreen){ - for(int h=0;hdy ? dx : -dy)/2, e2; - - uint32_t addr; - for(;;){ - addr=x0+y0*buffer_width; - if(addr>0&&addr<(uint64_t)(buffer_width*buffer_height)&&x00) - buffer[addr]=1; - if (x0==x1 && y0==y1) break; - e2 = err; - if (e2 >-dx) { err -= dy; x0 += sx; } - if (e2 < dy) { err += dx; y0 += sy; } - } -} - -int update_terminal_output(WINDOW *win, struct simdata_t *simdata,uint8_t fullscreen){ - int width,height; - werase(win); - getmaxyx(win,height,width); - - term_curs_x=0; - term_curs_y=0; - - int vector=0; - int vstate=0; - - int vfb_width, vfb_height; - if(fullscreen){ - vfb_width=(width)*2; - vfb_height=(height)*4; - }else{ - vfb_width=(width-2)*2; - vfb_height=(height-2)*4; - } - - uint8_t* vector_working_frame_buffer=malloc(vfb_height*vfb_width); - uint8_t* vector_display_frame_buffer=malloc(vfb_height*vfb_width); - memset(vector_working_frame_buffer,0,vfb_height*vfb_width); - memset(vector_display_frame_buffer,0,vfb_height*vfb_width); - - float x0,y0,x1,y1; - 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==0x00000002) - memset(vector_working_frame_buffer,0,vfb_height*vfb_width); - else if(c==0x00000003) - memcpy(vector_display_frame_buffer,vector_working_frame_buffer,vfb_height*vfb_width); - else - vstate++; - break; - case 1: /*x of start*/ - x0=uint32_to_ieee754_float(c); - vstate++; - break; - case 2: /*y of start*/ - y0=uint32_to_ieee754_float(c); - vstate++; - break; - case 3: /*x of end*/ - x1=uint32_to_ieee754_float(c); - vstate++; - break; - case 4: /*y of end*/ - y1=uint32_to_ieee754_float(c); - float min_dim=(vfb_width>vfb_height)?vfb_height:vfb_width; - int xoff=0,yoff=0; - x0*=1.25; - x1*=1.25; - if(vfb_width>vfb_height) - xoff=(vfb_width-vfb_height)/2; - else - yoff=(vfb_height-vfb_width)/2; - braille_frame_buffer_line(vector_working_frame_buffer,vfb_width,vfb_height,xoff+(x0+1)*(min_dim/2),yoff+(y0+1)*(min_dim/2),xoff+(x1+1)*(min_dim/2),yoff+(y1+1)*(min_dim/2)); - vstate=0; - break; - } - }else{ - if(c>=' '&&c<='~'){ - mvwprintw(win,term_curs_y+1,term_curs_x+1,"%c",c); - term_curs_x++; - if(term_curs_x>=width){ - term_curs_x=0; - term_curs_y++; - } - if(term_curs_y>=height){ - wscrl(win,1); - term_curs_y--; - for(int i=0;i=vfb_height){ - wscrl(win,1); - term_curs_y--; - box(win, 0 , 0); - mvwprintw(win,0,width/2-7,"[ TERM OUTPUT ]"); - } - break; - default: - werase(win); - box(win, 0 , 0); - mvwprintw(win,0,width/2-7,"[ TERM OUTPUT ]"); - mvwprintw(win,1,1,"Invalid output"); - return 0; - } - } - } - } - if(!fullscreen){ - box(win, 0 , 0); - if(!vector){ - mvwchgat(win,term_curs_y+1,term_curs_x+1, 1, A_REVERSE, 0, NULL); - mvwprintw(win,0,width/2-7,"[ TERM OUTPUT ]"); - }else{ - print_braille_frame_buffer(win,vector_display_frame_buffer,vfb_width,vfb_height,0); - mvwprintw(win,0,width/2-9,"[ VECTOR DISPLAY ]"); - } - }else - if(vector) - print_braille_frame_buffer(win,vector_display_frame_buffer,vfb_width,vfb_height,1); - free(vector_working_frame_buffer); - free(vector_display_frame_buffer); - - return 0; -} - -int update_general_terminal_output(WINDOW *win, struct simdata_t *simdata){ - return update_terminal_output(win,simdata,0); -} - -int update_display_terminal_output(WINDOW *win, struct simdata_t *simdata){ - return update_terminal_output(win,simdata,1); -} int update_debug_simdata(struct simdata_t *simdata){ int width,height; diff --git a/gui.h b/gui.h index 07a960e..7f61c56 100644 --- a/gui.h +++ b/gui.h @@ -6,6 +6,7 @@ int end_gui(); int gui_error(char *); int update_gui(struct simdata_t *); int clean_cpu_output(struct simdata_t *simdata); +extern enum FB_DRIVER_t selected_fb_driver; struct gui_settings_t{ uint8_t cycles_per_frame; diff --git a/gui_display.c b/gui_display.c new file mode 100644 index 0000000..176f3e2 --- /dev/null +++ b/gui_display.c @@ -0,0 +1,351 @@ +#include "config.h" + +#include +#include "simdata.h" +#include +#include +#include "gui.h" +#include "gui_display.h" + +#ifdef USE_AALIB +#include +#endif + +int term_curs_x,term_curs_y; + +/////////////////////////////////////////////// +//// Braille code +/////////////////////////////////////////////// +wchar_t braille_lookup[256]={ +L'⠀',L'⠁',L'⠂',L'⠃',L'⠄',L'⠅',L'⠆',L'⠇',L'⠈',L'⠉',L'⠊',L'⠋',L'⠌',L'⠍',L'⠎',L'⠏',L'⠐',L'⠑',L'⠒',L'⠓',L'⠔',L'⠕',L'⠖',L'⠗',L'⠘',L'⠙',L'⠚',L'⠛',L'⠜',L'⠝',L'⠞',L'⠟', +L'⠠',L'⠡',L'⠢',L'⠣',L'⠤',L'⠥',L'⠦',L'⠧',L'⠨',L'⠩',L'⠪',L'⠫',L'⠬',L'⠭',L'⠮',L'⠯',L'⠰',L'⠱',L'⠲',L'⠳',L'⠴',L'⠵',L'⠶',L'⠷',L'⠸',L'⠹',L'⠺',L'⠻',L'⠼',L'⠽',L'⠾',L'⠿', +L'⡀',L'⡁',L'⡂',L'⡃',L'⡄',L'⡅',L'⡆',L'⡇',L'⡈',L'⡉',L'⡊',L'⡋',L'⡌',L'⡍',L'⡎',L'⡏',L'⡐',L'⡑',L'⡒',L'⡓',L'⡔',L'⡕',L'⡖',L'⡗',L'⡘',L'⡙',L'⡚',L'⡛',L'⡜',L'⡝',L'⡞',L'⡟',L'⡠',L'⡡',L'⡢',L'⡣',L'⡤',L'⡥',L'⡦',L'⡧',L'⡨',L'⡩',L'⡪',L'⡫',L'⡬',L'⡭',L'⡮',L'⡯',L'⡰',L'⡱',L'⡲',L'⡳',L'⡴',L'⡵',L'⡶',L'⡷',L'⡸',L'⡹',L'⡺',L'⡻',L'⡼',L'⡽',L'⡾',L'⡿', +L'⢀',L'⢁',L'⢂',L'⢃',L'⢄',L'⢅',L'⢆',L'⢇',L'⢈',L'⢉',L'⢊',L'⢋',L'⢌',L'⢍',L'⢎',L'⢏',L'⢐',L'⢑',L'⢒',L'⢓',L'⢔',L'⢕',L'⢖',L'⢗',L'⢘',L'⢙',L'⢚',L'⢛',L'⢜',L'⢝',L'⢞',L'⢟',L'⢠',L'⢡',L'⢢',L'⢣',L'⢤',L'⢥',L'⢦',L'⢧',L'⢨',L'⢩',L'⢪',L'⢫',L'⢬',L'⢭',L'⢮',L'⢯',L'⢰',L'⢱',L'⢲',L'⢳',L'⢴',L'⢵',L'⢶',L'⢷',L'⢸',L'⢹',L'⢺',L'⢻',L'⢼',L'⢽',L'⢾',L'⢿', +L'⣀',L'⣁',L'⣂',L'⣃',L'⣄',L'⣅',L'⣆',L'⣇',L'⣈',L'⣉',L'⣊',L'⣋',L'⣌',L'⣍',L'⣎',L'⣏',L'⣐',L'⣑',L'⣒',L'⣓',L'⣔',L'⣕',L'⣖',L'⣗',L'⣘',L'⣙',L'⣚',L'⣛',L'⣜',L'⣝',L'⣞',L'⣟',L'⣠',L'⣡',L'⣢',L'⣣',L'⣤',L'⣥',L'⣦',L'⣧',L'⣨',L'⣩',L'⣪',L'⣫',L'⣬',L'⣭',L'⣮',L'⣯',L'⣰',L'⣱',L'⣲',L'⣳',L'⣴',L'⣵',L'⣶',L'⣷',L'⣸',L'⣹',L'⣺',L'⣻',L'⣼',L'⣽',L'⣾',L'⣿', +}; + + +int print_braille_frame_buffer(WINDOW* win,uint8_t *buffer,int width,int height,int fullscreen){ + for(int h=0;hdy ? dx : -dy)/2, e2; + + uint32_t addr; + for(;;){ + addr=x0+y0*buffer_width; + if(addr>0&&addr<(uint64_t)(buffer_width*buffer_height)&&x00) + buffer[addr]=1; + if (x0==x1 && y0==y1) break; + e2 = err; + if (e2 >-dx) { err -= dy; x0 += sx; } + if (e2 < dy) { err += dx; y0 += sy; } + } +} + +int update_terminal_output(WINDOW *win, struct simdata_t *simdata,uint8_t fullscreen){ + int width,height; + werase(win); + getmaxyx(win,height,width); + + term_curs_x=0; + term_curs_y=0; + + int vector=0; + int vstate=0; + + init(win,fullscreen); + + int vfb_width, vfb_height; + +#ifdef USE_AALIB + if(selected_fb_driver==FB_AALIB){ + vfb_width=aa_imgwidth(aactx); + vfb_height=aa_imgheight(aactx); + }else +#endif + vfb_width=braille_width,vfb_height=braille_height; + + + uint8_t* vector_working_frame_buffer=malloc(vfb_height*vfb_width); + uint8_t* vector_display_frame_buffer=malloc(vfb_height*vfb_width); + + memset(vector_working_frame_buffer,0,vfb_height*vfb_width); + memset(vector_display_frame_buffer,0,vfb_height*vfb_width); + + float x0,y0,x1,y1; + 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==0x00000002) + memset(vector_working_frame_buffer,0,vfb_height*vfb_width); + else if(c==0x00000003) + memcpy(vector_display_frame_buffer,vector_working_frame_buffer,vfb_height*vfb_width); + else + vstate++; + break; + case 1: /*x of start*/ + x0=uint32_to_ieee754_float(c); + vstate++; + break; + case 2: /*y of start*/ + y0=uint32_to_ieee754_float(c); + vstate++; + break; + case 3: /*x of end*/ + x1=uint32_to_ieee754_float(c); + vstate++; + break; + case 4: /*y of end*/ + y1=uint32_to_ieee754_float(c); + float min_dim=(vfb_width>vfb_height)?vfb_height:vfb_width; + int xoff=0,yoff=0; + x0*=get_aspec_ratio(); + x1*=get_aspec_ratio(); + if(vfb_width>vfb_height) + xoff=(vfb_width-vfb_height)/2; + else + yoff=(vfb_height-vfb_width)/2; + braille_frame_buffer_line(vector_working_frame_buffer,vfb_width,vfb_height,xoff+(x0+1)*(min_dim/2),yoff+(y0+1)*(min_dim/2),xoff+(x1+1)*(min_dim/2),yoff+(y1+1)*(min_dim/2)); + vstate=0; + break; + } + }else{ + if(c>=' '&&c<='~'){ + mvwprintw(win,term_curs_y+1,term_curs_x+1,"%c",c); + term_curs_x++; + if(term_curs_x>=width){ + term_curs_x=0; + term_curs_y++; + } + if(term_curs_y>=height){ + wscrl(win,1); + term_curs_y--; + for(int i=0;i=vfb_height){ + wscrl(win,1); + term_curs_y--; + box(win, 0 , 0); + mvwprintw(win,0,width/2-7,"[ TERM OUTPUT ]"); + } + break; + default: + werase(win); + box(win, 0 , 0); + mvwprintw(win,0,width/2-7,"[ TERM OUTPUT ]"); + mvwprintw(win,1,1,"Invalid output"); + return 0; + } + } + } + } + if(!fullscreen){ + box(win, 0 , 0); + if(!vector){ + mvwchgat(win,term_curs_y+1,term_curs_x+1, 1, A_REVERSE, 0, NULL); + mvwprintw(win,0,width/2-7,"[ TERM OUTPUT ]"); + }else{ + mvwprintw(win,0,width/2-9,"[ VECTOR DISPLAY ]"); + } + }else if(!vector){ + mvwchgat(win,term_curs_y,term_curs_x, 1, A_REVERSE, 0, NULL); + } + + if(vector) + print_frame_buffer(win,vector_display_frame_buffer,vfb_width,vfb_height,fullscreen); + + free(vector_working_frame_buffer); + free(vector_display_frame_buffer); + + +# ifdef USE_AALIB + if(selected_fb_driver==FB_AALIB) + clean_aalib(); + else +#endif + clean_braille(); + + + return 0; +} + + +int update_general_terminal_output(WINDOW *win, struct simdata_t *simdata){ + return update_terminal_output(win,simdata,0); +} + +int update_display_terminal_output(WINDOW *win, struct simdata_t *simdata){ + return update_terminal_output(win,simdata,1); +} diff --git a/gui_display.h b/gui_display.h new file mode 100644 index 0000000..a02aee5 --- /dev/null +++ b/gui_display.h @@ -0,0 +1,8 @@ +#include +int update_general_terminal_output(WINDOW *win, struct simdata_t *simdata); +int update_display_terminal_output(WINDOW *win, struct simdata_t *simdata); + +enum FB_DRIVER_t{ + FB_AALIB, + FB_BRAILLE +}; diff --git a/main.c b/main.c index 4819bcb..2ba9c0d 100644 --- a/main.c +++ b/main.c @@ -17,18 +17,31 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "config.h" #include #include #include "gui.h" +#include "gui_display.h" #include "simdata.h" #include #include #include "assembly.h" #include "cpu.h" #include +#include void help(char* progname){ - printf("Usage: %s -i \n", progname); + printf( + "Usage: %s [options] -i < in file > \n"\ + "Options:\n" + " -a < out rom file > Assemble in file to out rom file\n" + " -r < framebuffer driver > Use one of the following for writing the framebuffer\n" +#ifdef USE_AALIB + " aalib, braille\n" +#else + " braille\n" +#endif + , progname); } #define ADDR_SIZE 16777216 @@ -41,12 +54,14 @@ int dump_rom(char* filename, struct simdata_t *simdata){ return 0; } + int main(int argc, char* argd[] ){ /// PARSE COMMAND LINE /// char *infile = NULL; char *assemble = NULL; + enum FB_DRIVER_t fb_driver=FB_BRAILLE; int opt; - while ((opt = getopt(argc, argd, "hi:a:")) != -1) { + while ((opt = getopt(argc, argd, "hi:a:r:")) != -1) { switch (opt) { case 'h': help(argd[0]); @@ -58,12 +73,33 @@ int main(int argc, char* argd[] ){ case 'a': assemble=optarg; break; + case 'r': + if(optarg==NULL){ + help(argd[0]); + return 1; + } + + if(strcmp(optarg,"aalib")==0) + fb_driver=FB_AALIB; + else if(strcmp(optarg,"braille")==0) + fb_driver=FB_BRAILLE; + else{ + help(argd[0]); + return 1; + } + break; default: help(argd[0]); return 1; - break; } } +#ifndef USE_AALIB + if(fb_driver==FB_AALIB){ + printf("ERROR:Project wasn't compiled with aalib support.\n"); + help(); + return 1; + } +#endif if (infile==NULL){ help(argd[0]); return 1; @@ -188,7 +224,7 @@ int main(int argc, char* argd[] ){ /// INITIALISE GUI /// - if(start_gui()){ + if(start_gui(fb_driver)){ printf("Failed on start_gui()\n"); cpu_simdata_free(simdata); free_simdata(simdata);