From 247d0cc1332593eaaddea593924bfede51c464ec Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 24 Apr 2024 16:06:24 +0200 Subject: [PATCH] exit only from main.c; inline emulate into main --- src/debugger.c | 72 +++++++++++++------------------------------- src/debugger.h | 17 ++++++++++- src/emu_emulate.c | 49 ------------------------------ src/emu_init.c | 12 +++++++- src/emulator.h | 21 ++++++++++--- src/emulator_inner.h | 4 --- src/main.c | 65 ++++++++++++++++++++++++++++++++++----- src/ui.c | 30 ++++++++++++++++++ src/ui.h | 5 +++ src/ui_sdl.c | 8 ++--- src/ui_text.c | 14 +++++---- src/ui_x11.c | 16 +++------- 12 files changed, 174 insertions(+), 139 deletions(-) diff --git a/src/debugger.c b/src/debugger.c index d9da4dc..974d8d6 100644 --- a/src/debugger.c +++ b/src/debugger.c @@ -16,8 +16,6 @@ #define MAX_ARGS 16 -#define BREAKPOINT_HIT 4 - #define TAB_SKIP 8 /* @@ -72,16 +70,11 @@ #define UM_PRE 0x10b7c /* Unit Operator prefix */ #define UM_END 0x10b86 /* Unit Operator _ */ -/* - * exec_flags values - */ -#define EXEC_BKPT 1 - int enter_debugger = 0; bool in_debugger = false; int exec_flags = 0; -static int continue_flag; +static bool continue_flag; static char instr[ 100 ]; /* @@ -95,14 +88,6 @@ static char instr[ 100 ]; #define DSKTOP_GX 0x806f8 #define DSKBOT_GX 0x806fd -/* - * Breakpoint related stuff - */ -#define BP_EXEC 1 -#define BP_READ 2 -#define BP_WRITE 4 -#define BP_RANGE 8 - #define MAX_BREAKPOINTS 32 int num_bkpts; @@ -3207,7 +3192,7 @@ cmd_tbl[] = { {0, 0, 0 } }; -static int check_breakpoint( int type, word_20 addr ) +int check_breakpoint( int type, word_20 addr ) { struct breakpoint* bp; int i, n; @@ -3441,7 +3426,7 @@ static void cmd_break( int argc, char** argv ) } } -static void cmd_continue( int argc, char** argv ) { continue_flag = 1; } +static void cmd_continue( int argc, char** argv ) { continue_flag = true; } static void cmd_delete( int argc, char** argv ) { @@ -3495,14 +3480,6 @@ static void cmd_delete( int argc, char** argv ) } } -static void cmd_exit( int argc, char** argv ) -{ - if ( confirm( "Exit the emulator WITHOUT saving its state?" ) ) { - printf( "Exit.\n" ); - exit( 0 ); - } -} - static void cmd_go( int argc, char** argv ) { word_20 addr; @@ -3590,12 +3567,23 @@ static void cmd_mode( int argc, char** argv ) } } +static void cmd_exit( int argc, char** argv ) +{ + if ( confirm( "Exit the emulator WITHOUT saving its state?" ) ) { + printf( "Exit.\n" ); + + save_before_exit = false; + please_exit = true; + } +} + static void cmd_quit( int argc, char** argv ) { if ( confirm( "Quit the emulator and save its state?" ) ) { printf( "Exit.\n" ); - exit_emulator(); - exit( 0 ); + + save_before_exit = true; + please_exit = true; } } @@ -4086,8 +4074,7 @@ int debug( void ) if ( config.verbose ) printf( "usnterrupt (SIGINT) ignored\n" ); - exit_emulator(); - exit( 1 ); + please_exit = true; if ( enter_debugger & BREAKPOINT_HIT ) if ( config.verbose ) @@ -4114,7 +4101,7 @@ int debug( void ) stop_timer( RUN_TIMER ); start_timer( IDLE_TIMER ); - continue_flag = 0; + continue_flag = false; if ( enter_debugger & ILLEGAL_INSTRUCTION ) { printf( "ILLEGAL INSTRUCTION at %.5lX : %s\n", saturn.PC, str_nibbles( saturn.PC, 16 ) ); @@ -4136,10 +4123,10 @@ int debug( void ) /* * read a command */ - rl = readline( "x48-debug> " ); + rl = readline( "x48ng-debug> " ); if ( rl == ( char* )0 ) { - continue_flag = 1; + continue_flag = true; continue; } if ( *rl == '\0' ) { @@ -4198,7 +4185,7 @@ int debug( void ) } in_debugger = false; - } while ( !continue_flag ); + } while ( !continue_flag && !please_exit ); /* * adjust the hp48's timers @@ -4241,20 +4228,3 @@ int debug( void ) return 0; } - -void emulate_debug( void ) -{ - do { - step_instruction(); - - if ( exec_flags & EXEC_BKPT ) { - if ( check_breakpoint( BP_EXEC, saturn.PC ) ) { - enter_debugger |= BREAKPOINT_HIT; - break; - } - } - - if ( schedule_event-- == 0 ) - schedule(); - } while ( !enter_debugger ); -} diff --git a/src/debugger.h b/src/debugger.h index 8b2e78b..940ed04 100644 --- a/src/debugger.h +++ b/src/debugger.h @@ -11,6 +11,21 @@ #define HP_MNEMONICS 0 #define CLASS_MNEMONICS 1 +#define BREAKPOINT_HIT 4 + +/* + * exec_flags values + */ +#define EXEC_BKPT 1 + +/* + * Breakpoint related stuff + */ +#define BP_EXEC 1 +#define BP_READ 2 +#define BP_WRITE 4 +#define BP_RANGE 8 + extern int enter_debugger; extern bool in_debugger; extern int exec_flags; @@ -18,7 +33,7 @@ extern int exec_flags; /**************/ /* debugger.c */ /**************/ +extern int check_breakpoint( int type, word_20 addr ); extern int debug( void ); -extern void emulate_debug( void ); #endif /* !_DEBUGGER_H */ diff --git a/src/emu_emulate.c b/src/emu_emulate.c index b7e5ad6..7b830d2 100644 --- a/src/emu_emulate.c +++ b/src/emu_emulate.c @@ -62,9 +62,6 @@ int set_t1; long schedule_event = 0; -long sched_timer1; -long sched_timer2; - long sched_instr_rollover = SCHED_INSTR_ROLLOVER; long sched_receive = SCHED_RECEIVE; long sched_adjtime = SCHED_ADJTIME; @@ -2836,49 +2833,3 @@ inline void schedule( void ) ui_get_event(); } } - -void emulate( void ) -{ - struct timeval tv; - struct timeval tv2; - struct timezone tz; - - reset_timer( T1_TIMER ); - reset_timer( RUN_TIMER ); - reset_timer( IDLE_TIMER ); - - set_accesstime(); - start_timer( T1_TIMER ); - - start_timer( RUN_TIMER ); - - sched_timer1 = t1_i_per_tick = saturn.t1_tick; - sched_timer2 = t2_i_per_tick = saturn.t2_tick; - - set_t1 = saturn.timer1; - - do { - step_instruction(); - - for ( int i = 0; i < ( int )( sizeof( saturn.keybuf.rows ) / sizeof( saturn.keybuf.rows[ 0 ] ) ); i++ ) { - if ( saturn.keybuf.rows[ i ] || config.throttle ) { - /* Throttling speed if needed */ - gettimeofday( &tv, &tz ); - gettimeofday( &tv2, &tz ); - while ( ( tv.tv_sec == tv2.tv_sec ) && ( ( tv.tv_usec - tv2.tv_usec ) < 2 ) ) - gettimeofday( &tv, &tz ); - - tv2.tv_usec = tv.tv_usec; - tv2.tv_sec = tv.tv_sec; - break; - } - } - - if ( schedule_event < 0 ) { - fprintf( stderr, "bug" ); - schedule_event = 0; - } - if ( schedule_event-- <= 0 ) - schedule(); - } while ( !enter_debugger ); -} diff --git a/src/emu_init.c b/src/emu_init.c index 1bd0084..0fcbf2b 100644 --- a/src/emu_init.c +++ b/src/emu_init.c @@ -18,6 +18,9 @@ #define RAM_SIZE_SX 0x10000 #define RAM_SIZE_GX 0x40000 +bool please_exit = false; +bool save_before_exit = true; + bool rom_is_new = true; long ram_size; long port1_size; @@ -998,6 +1001,9 @@ void init_display( void ) void start_emulator( void ) { + please_exit = false; + save_before_exit = true; + /* If files are successfully read => return and let's go */ if ( read_files() ) { if ( config.resetOnStartup ) @@ -1016,4 +1022,8 @@ void start_emulator( void ) init_display(); } -void exit_emulator( void ) { write_files(); } +void exit_emulator( void ) +{ + if ( save_before_exit ) + write_files(); +} diff --git a/src/emulator.h b/src/emulator.h index 65eb709..deb15bc 100644 --- a/src/emulator.h +++ b/src/emulator.h @@ -4,6 +4,9 @@ #include /* int64_t */ #include +#define T1_TIMER 0 +/* #define T2_TIMER 1 /\* unused? *\/ */ + #define DEC 10 #define HEX 16 @@ -307,6 +310,19 @@ extern hpkey_t keyboard[ NB_KEYS ]; extern int annunciators_bits[ NB_ANNUNCIATORS ]; +extern long sched_timer1; +extern long sched_timer2; +extern unsigned long t1_i_per_tick; +extern unsigned long t2_i_per_tick; + +extern bool please_exit; +extern bool save_before_exit; + +/***************/ +/* emu_timer.c */ +/***************/ +extern void reset_timer( int timer ); + /**************/ /* emu_init.c */ /**************/ @@ -325,9 +341,4 @@ extern void press_key( int hpkey ); /* used in ui_*.c */ extern void release_key( int hpkey ); /* used in ui_*.c */ extern void release_all_keys( void ); /* used in ui_*.c */ -/*****************/ -/* emu_emulate.c */ -/*****************/ -extern void emulate( void ); /* used in main.c */ - #endif /* !_EMULATOR_H */ diff --git a/src/emulator_inner.h b/src/emulator_inner.h index 3f8ca25..142a32c 100644 --- a/src/emulator_inner.h +++ b/src/emulator_inner.h @@ -5,9 +5,6 @@ #define OUT_FIELD 17 -#define T1_TIMER 0 -/* #define T2_TIMER 1 /\* unused? *\/ */ - extern device_t device; extern int set_t1; @@ -29,7 +26,6 @@ extern int end_fields[ 19 ]; /***************/ /* emu_timer.c */ /***************/ -extern void reset_timer( int timer ); extern void restart_timer( int timer ); extern word_64 get_timer( int timer ); diff --git a/src/main.c b/src/main.c index ca44f14..c8162c4 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ void signal_handler( int sig ) sigalarm_triggered = true; break; case SIGPIPE: + ui_stop(); exit_emulator(); exit( 0 ); default: @@ -109,14 +110,64 @@ int main( int argc, char** argv ) /* Start emulation loop */ /************************/ do { - if ( exec_flags ) - emulate_debug(); - else - emulate(); + struct timeval tv; + struct timeval tv2; + struct timezone tz; - debug(); - } while ( true ); + reset_timer( T1_TIMER ); + reset_timer( RUN_TIMER ); + reset_timer( IDLE_TIMER ); + + set_accesstime(); + start_timer( T1_TIMER ); + + start_timer( RUN_TIMER ); + + sched_timer1 = t1_i_per_tick = saturn.t1_tick; + sched_timer2 = t2_i_per_tick = saturn.t2_tick; + + set_t1 = saturn.timer1; + + do { + step_instruction(); + + if ( exec_flags & EXEC_BKPT ) { + if ( check_breakpoint( BP_EXEC, saturn.PC ) ) { + enter_debugger |= BREAKPOINT_HIT; + break; + } + } + + for ( int i = 0; i < ( int )( sizeof( saturn.keybuf.rows ) / sizeof( saturn.keybuf.rows[ 0 ] ) ); i++ ) { + if ( saturn.keybuf.rows[ i ] || config.throttle ) { + /* Throttling speed if needed */ + gettimeofday( &tv, &tz ); + gettimeofday( &tv2, &tz ); + while ( ( tv.tv_sec == tv2.tv_sec ) && ( ( tv.tv_usec - tv2.tv_usec ) < 2 ) ) + gettimeofday( &tv, &tz ); + + tv2.tv_usec = tv.tv_usec; + tv2.tv_sec = tv.tv_sec; + break; + } + } + + if ( schedule_event < 0 ) { + fprintf( stderr, "bug" ); + schedule_event = 0; + } + if ( schedule_event-- <= 0 ) + schedule(); + } while ( !please_exit && !enter_debugger ); + + fprintf( stderr, "please_exit = %i, enter_debugger = %i\n", please_exit, enter_debugger ); + + if ( enter_debugger ) + debug(); + } while ( !please_exit ); + + ui_stop(); + exit_emulator(); - /* never reached */ return 0; } diff --git a/src/ui.c b/src/ui.c index 199afce..1c831b0 100644 --- a/src/ui.c +++ b/src/ui.c @@ -199,3 +199,33 @@ void start_UI( int argc, char** argv ) break; } } + +void ui_stop() +{ + ui_init_LCD(); + + switch ( config.frontend_type ) { +#if ( defined( HAS_X11 ) ) + case FRONTEND_X11: + default: + x11_ui_stop(); + break; +#endif + +#if ( defined( HAS_SDL ) ) + case FRONTEND_SDL: +# if ( !defined( HAS_X11 ) ) + default: +# endif + sdl_ui_stop(); + break; +#endif + + case FRONTEND_TEXT: +#if ( !defined( HAS_X11 ) && !defined( HAS_SDL ) ) + default: +#endif + text_ui_stop(); + break; + } +} diff --git a/src/ui.h b/src/ui.h index d865631..1cc08f6 100644 --- a/src/ui.h +++ b/src/ui.h @@ -28,13 +28,16 @@ extern letter_t small_font[ 128 ]; /*************/ #ifdef HAS_X11 extern void init_x11_ui( int argc, char** argv ); +extern void x11_ui_stop(); #endif #ifdef HAS_SDL extern void init_sdl_ui( int argc, char** argv ); +extern void sdl_ui_stop(); #endif extern void init_text_ui( int argc, char** argv ); +extern void text_ui_stop(); /*************************************************/ /* public API: if it's there it's used elsewhere */ @@ -61,6 +64,8 @@ extern void ( *ui_refresh_LCD )( void ); /*******************/ /* used in: main.c */ /*******************/ +extern void ui_stop( void ); + extern void start_UI( int argc, char** argv ); #endif /* !_UI_H */ diff --git a/src/ui_sdl.c b/src/ui_sdl.c index 60116a1..8746bd9 100644 --- a/src/ui_sdl.c +++ b/src/ui_sdl.c @@ -724,8 +724,7 @@ static int SDLKeyToKey( SDLKey k ) break; case SDLK_F7: case SDLK_F10: - exit_emulator(); - exit( 0 ); + please_exit = true; break; default: return -1; @@ -1779,8 +1778,7 @@ int sdl_get_event( void ) if ( SDL_PollEvent( &event ) ) { switch ( event.type ) { case SDL_QUIT: - exit_emulator(); - exit( 0 ); + please_exit = true; break; /* // Mouse move: react to state changes in the buttons that are @@ -2038,6 +2036,8 @@ void sdl_adjust_contrast() sdl_draw_annunc(); } +void sdl_ui_stop() {} + void init_sdl_ui( int argc, char** argv ) { /* Set public API to this UI's functions */ diff --git a/src/ui_text.c b/src/ui_text.c index 9fa5bdb..73b8119 100644 --- a/src/ui_text.c +++ b/src/ui_text.c @@ -543,12 +543,7 @@ int text_get_event( void ) case '|': /* Shift+\ */ case KEY_SEND: /* Shift+End */ case KEY_F( 10 ): - nodelay( stdscr, FALSE ); - echo(); - - endwin(); - exit_emulator(); - exit( 0 ); + please_exit = true; break; } @@ -559,6 +554,13 @@ int text_get_event( void ) return 1; } +void text_ui_stop() +{ + nodelay( stdscr, FALSE ); + echo(); + endwin(); +} + void init_text_ui( int argc, char** argv ) { /* Set public API to this UIs functions */ diff --git a/src/ui_x11.c b/src/ui_x11.c index 28ac57f..ee7bd34 100644 --- a/src/ui_x11.c +++ b/src/ui_x11.c @@ -2845,9 +2845,7 @@ int decode_key( XEvent* xev, KeySym sym, char* buf, int buflen ) break; case XK_F7: case XK_F10: - exit_emulator(); - XCloseDisplay( dpy ); - exit( 0 ); + please_exit = true; break; default: break; @@ -2856,6 +2854,8 @@ int decode_key( XEvent* xev, KeySym sym, char* buf, int buflen ) return wake; } +void x11_ui_stop() { XCloseDisplay( dpy ); } + void x11_release_all_keys( void ) { release_all_keys(); @@ -3425,14 +3425,8 @@ int x11_get_event( void ) cm = ( XClientMessageEvent* )&xev; if ( cm->message_type == wm_protocols ) { - if ( cm->data.l[ 0 ] == ( long )wm_delete_window ) { - exit_emulator(); - - XCloseDisplay( dpy ); - - exit( 0 ); - } - + if ( cm->data.l[ 0 ] == ( long )wm_delete_window ) + please_exit = true; if ( cm->data.l[ 0 ] == ( long )wm_save_yourself ) save_command_line(); }