int main(int argc, char **argv) { int ret = 1; memset(&g_context, 0, sizeof(g_context)); g_context.sock = -1; g_context.outsock = -1; g_context.errsock = -1; g_context.fssock = -1; g_context.fstdout = stdout; g_context.fstderr = stderr; if(parse_args(argc, argv, &g_context.args)) { build_histfile(); if(shell() == 0) { ret = g_context.lasterr; } shutdown_app(); } else { print_help(); } return ret; }
// this routine is called either by GLUT or directly. It manages time evolution void idle () { if(sim_time>stop_time) shutdown_app(); // quit when time runs out if(is_step || !is_stepping) { is_step=false; double new_real = get_real_secs(); if(is_sim_throttling) { // time math avoids incremental deltas to limit error accumulation double real_delta = new_real - last_inflection_real; double new_time = real_delta/time_ratio + last_inflection_sim; if(new_time-last_sim_time > step_size/2) { flo tfps = 1/(new_real-last_real); last_real = new_real; fps = (1-FPS_DECAY)*tfps + FPS_DECAY*fps; // step_size = min step sim_time = max(new_time, last_sim_time + step_size); evolution_lagging = (sim_time-last_sim_time > step_size*10); if(evolution_lagging) { // maximum step is 10x normal step sim_time = last_sim_time+(step_size*10); } advance_time(); last_sim_time=sim_time; } } else { flo tfps = 1/(new_real-last_real); last_real = new_real; fps = (1-FPS_DECAY)*tfps + FPS_DECAY*fps; sim_time+=step_size; evolution_lagging=false; advance_time(); last_sim_time=sim_time; } } }
void sig_call(int sig) { if((sig == SIGINT) || (sig == SIGTERM)) { fprintf(stderr, "Exiting\n"); shutdown_app(); exit(1); } }
bool app_handle_key(KeyEvent *key) { if(key->normal) { if(key->ctrl) { switch(key->key) { case 19: // Ctrl-S = slow down if(time_ratio < MAXIMUM_RATIO) { time_ratio *= TIME_RATIO_STEP; step_size /= TIME_RATIO_STEP; } last_inflection_sim=sim_time; last_inflection_real=get_real_secs(); return true; case 1: // Ctrl-A = speed up if(time_ratio > MINIMUM_RATIO) { time_ratio /= TIME_RATIO_STEP; step_size *= TIME_RATIO_STEP; } last_inflection_sim=sim_time; last_inflection_real=get_real_secs(); return true; case 4: // Ctrl-D = real-time step_size *= time_ratio; time_ratio = 1; last_inflection_sim=sim_time; last_inflection_real=get_real_secs(); return true; } } else { switch(key->key) { case 'q': shutdown_app(); case 's': is_stepping = 1; is_step = 1; return true; case 'x': is_stepping = 0; is_step = 0; last_inflection_sim=sim_time; last_inflection_real=get_real_secs(); return true; case 'T': show_time=!show_time; return true; case 'X': is_sim_throttling = !is_sim_throttling; last_inflection_sim=sim_time; last_inflection_real=get_real_secs(); return true; case 'l': int len; uint8_t* s = compiler->compile(compiler->last_script,&len); computer->load_script_at_selection(s,len); return true; } } } return false; }
void cli_handler(char *buf) { if(buf) { if(g_context.ttymode) { if(strncmp(buf, "~.", 2) == 0) { char prompt[PATH_MAX]; g_context.ttymode = 0; rl_callback_handler_remove(); snprintf(prompt, PATH_MAX, "%s> ", g_context.currpath); rl_callback_handler_install(prompt, cli_handler); } else if(g_context.outsock >= 0) { char b[1024]; snprintf(b, sizeof(b), "%s\n", buf); write(g_context.outsock, b, strlen(b)); } return; } while(isspace(*buf)) { buf++; } if(*buf == 0) { if(g_context.asmmode) { char prompt[PATH_MAX]; g_context.asmmode = 0; g_context.asmaddr = 0; rl_callback_handler_remove(); snprintf(prompt, PATH_MAX, "%s> ", g_context.currpath); rl_callback_handler_install(prompt, cli_handler); } return; } if(in_script()) { /* When a script is running only accept stop */ if(strcmp(buf, "stop") == 0) { close_script(); } return; } if(g_context.asmmode) { char cmd[1024]; /* Do assembler */ unsigned int opcode; if(asmAssemble(buf, g_context.asmaddr, &opcode) == 0) { sprintf(cmd, "pokew 0x%08X 0x%08X ", g_context.asmaddr, opcode); execute_line(cmd); } return; } add_history(buf); if(buf[0] == '!') { if(strncmp(&buf[1], "cd ", 3) == 0) { chdir(&buf[4]); } else { system(&buf[1]); } return; } else if(buf[0] == '@') { if(g_context.fssock >= 0) { (void) write(g_context.fssock, &buf[1], strlen(&buf[1])); } return; } else if(buf[0] == '%') { execute_script(&buf[1]); return; } execute_line(buf); } else { shutdown_app(); exit(0); } }