/* Run processor until one of following occurs: - An exception is encountered in WB. - max_instr instructions have completed through WB Return number of instructions executed. if statusp nonnull, then will be set to exception status of final instruction if ccp nonnull, then will be set to condition codes of final instruction */ int sim_run(int max_instr, exc_t *statusp, cc_t *ccp) { int icount = 0; exc_t status = EXC_NONE; while (icount < max_instr) { status = sim_step(); icount++; if (status != EXC_NONE) break; } if (statusp) *statusp = status; if (ccp) *ccp = cc; return icount; }
/* Run processor until one of following occurs: - An error status is encountered in WB. - max_instr instructions have completed through WB Return number of instructions executed. if statusp nonnull, then will be set to status of final instruction if ccp nonnull, then will be set to condition codes of final instruction */ int sim_run(int max_instr, byte_t *statusp, cc_t *ccp) { int icount = 0; byte_t run_status = STAT_AOK; while (icount < max_instr) { run_status = sim_step(); icount++; if (run_status != STAT_AOK) break; } if (statusp) *statusp = run_status; if (ccp) *ccp = cc; return icount; }
int main(int argc, char** argv) { // read all used flags while (1) { static struct option long_options[] = { {"invert-match", no_argument, 0, 'v'}, {"line-regexp", no_argument, 0, 'x'}, {"count", no_argument, 0, 'c'}, {"help", no_argument, &display_help, 1}, {"version", no_argument, 0, 'V'}, {"cache-limit", required_argument, 0, OPTION_CACHE_LIMIT}, {0, 0, 0, 0} }; int option_index = 0; int c = getopt_long(argc, argv, "vxcV", long_options, &option_index); if (c == -1) break; switch (c) { case 'v': invert_match = true; break; case 'x': whole_lines = true; break; case 'c': count_matches = true; break; case OPTION_CACHE_LIMIT: cache_mem_limit = parse_size_in_kb(optarg); if (cache_mem_limit == -1U) { print_usage(argv[0]); return 2; } break; case 'V': display_version = true; break; case 0: break; default: print_usage(argv[0]); return 2; } } if (display_help) { printf(help_message, argv[0]); return 0; } if (display_version) { printf(version_message); return 0; } // check if there is exactly one regular expression if (optind != argc-1) { print_usage(argv[0]); return 2; } char* regex = argv[optind]; // parse the regular expression to abstract syntax tree struct syntree* tree; tree = parse(regex, strlen(regex)); // build nfa for the regular expression struct nfa* nfa; nfa = build_nfa(tree, whole_lines); free_tree(tree); // initialize simulation state sim_init(&state, nfa, invert_match, cache_mem_limit); // initialize buffer uintptr_t buffer_size = 65536; buffer_init(&buffer, STDIN_FILENO, STDOUT_FILENO, buffer_size); // main matching loop uintmax_t match_count = 0; bool new_line = true; bool some_match = false; intptr_t res = buffer_next(&buffer); if (!count_matches) buffer_mark(&buffer); while (res == 1) { // save next input byte to ch uint_fast8_t ch = buffer_get(&buffer); if (ch == '\n') { // handle the end of line here new_line = true; // simulate the special line end character if (!state.dfa_state->accept) sim_step(&state, CHAR_INPUT_END); if (sim_is_match(&state)) { // the last line matched, either print it or count it some_match = true; if (count_matches) { ++match_count; } else { int_fast8_t res = buffer_print(&buffer, true); if (res != 1) die(2, (res == -1) ? errno : 0, "Error writing to stdout"); } } // reset simulation state state.dfa_state = state.after_begin; // read next character from input res = buffer_next(&buffer); if (!count_matches) buffer_mark(&buffer); } else { new_line = false; // simulate only if there was no match on the current line if (!state.dfa_state->accept) { // if the character is not valid ASCII, no transitions will take place if (ch > MAX_CHAR) state.dfa_state = state.before_begin; // otherwise do the simulation else sim_step(&state, ch); } // read next character from input res = buffer_next(&buffer); } } if (res == -1) die(2, errno, "Error reading from stdin"); // check if there is no \n at the end of the input // if it is the case, behave as if it was there if (!new_line) { if (!state.dfa_state->accept) sim_step(&state, CHAR_INPUT_END); if (sim_is_match(&state)) { some_match = true; if (count_matches) { ++match_count; } else { int_fast8_t res = buffer_print(&buffer, false); puts(""); if (res != 1) die(2, (res == -1) ? errno : 0, "Error writing to stdout"); } } } // if -c is used, print the count of matches if (count_matches) { printf("%" PRIuMAX "\n", match_count); } // clean up buffer_cleanup(&buffer); sim_cleanup(&state); free_nfa(nfa); // return status is based on whether there was a match or not return some_match ? 0 : 1; }
int main(int argc, char *argv[]) { // Initialize the SDL subsystems we're using. if (SDL_Init(SDL_INIT_VIDEO /* | SDL_INIT_JOYSTICK | SDL_INIT_CDROM | SDL_INIT_AUDIO*/)) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); int width = 1000; int height = 1000; // Set the video mode. if (SDL_SetVideoMode(width, height, 16 /* 32 */, SDL_OPENGL) == 0) { fprintf(stderr, "SDL_SetVideoMode() failed."); exit(1); } ogl::open(); // Turn on alpha blending. glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glMatrixMode(GL_PROJECTION); glOrtho(0, 1000, 0, 1000, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // float lod_tweak = 1.0f; init(); // Mouse state. int mouse_x = 0; int mouse_y = 0; int mouse_buttons = 0; bool paused = false; float speed_scale = 1.0f; Uint32 last_ticks = SDL_GetTicks(); for (;;) { Uint32 ticks = SDL_GetTicks(); int delta_ticks = ticks - last_ticks; float delta_t = delta_ticks / 1000.f; last_ticks = ticks; if (paused == true) { delta_t = 0.0f; } // Handle input. SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: { int key = event.key.keysym.sym; if (key == SDLK_q || key == SDLK_ESCAPE) { exit(0); } else if (key == SDLK_k) { s_scale *= 1.111111f; } else if (key == SDLK_j) { s_scale *= 0.9f; } else if (key == SDLK_p) { // Print measured resistance. float r = measure_resistance(); printf("measured r = %f ohms\n", r); } break; } case SDL_MOUSEMOTION: mouse_x = (int) (event.motion.x); mouse_y = (int) (event.motion.y); break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { int mask = 1 << (event.button.button); if (event.button.state == SDL_PRESSED) { mouse_buttons |= mask; } else { mouse_buttons &= ~mask; } break; } case SDL_QUIT: exit(0); break; default: break; } } glDisable(GL_DEPTH_TEST); // Disable depth testing. glDrawBuffer(GL_BACK); glClear(GL_COLOR_BUFFER_BIT); for (int i = 0; i < 100; i++) { sim_step(); } draw_stuff(); SDL_GL_SwapBuffers(); // SDL_Delay(10); printf("measured r = %f ohms\n", measure_resistance()); } return 0; }
int main(int argc, char *argv[]){ int i; struct timerparams tparams; SDL_Event e; sim_Sim *mysim; SDL_Renderer *ren; int temp; SDL_Surface *iconSurf, *isoSurf; SDL_TimerID timerid; Uint32 updatetime; int tickspersecond; int droppedframes; gridviewport.x = 0; gridviewport.y = 0; gridviewport.w = WIDTH * CELLSIZE; gridviewport.h = HEIGHT * CELLSIZE; isoviewport.x = 500; isoviewport.y = 700; toolbox.x = 0; toolbox.y = 400; toolbox.w = 400; toolbox.h = 400; uistate.toolselected = TOOL_ADD; uistate.demoselected = MODE_NONE; uistate.paused = 0; uistate.mouseinwindow = 1; ren = setupWindow(); SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND); if((iconSurf = SDL_LoadBMP("icons.bmp")) == NULL) { printf("Failed to load icons.bmp"); exit(-1); } iconText = SDL_CreateTextureFromSurface(ren, iconSurf); isoSurf = SDL_LoadBMP("tile.bmp"); isoText = SDL_CreateTextureFromSurface(ren, isoSurf); if((tparams.CustomTimerEvent = SDL_RegisterEvents(1)) == -1){ printf("Failed to register customevent\n"); exit(-1); } tparams.timerlock = 0; if((timerid = SDL_AddTimer(TIMERINTERVAL, timercallback, (void *) &tparams))) ; else fprintf(stderr,"Timer failed to start\n"); mysim = sim_CreateSimulation(WIDTH, HEIGHT, 50.5); int quit = 0; updatetime = SDL_GetTicks(); tickspersecond = 0; while (!quit) { droppedframes = MAXDROPPEDFRAMES; doMouseEvent(&uistate, mysim); drawGrid(ren, mysim, &gridviewport); drawIso(ren, mysim, &isoviewport); drawTools(ren, &uistate, &toolbox); SDL_RenderPresent(ren); //fprintf(stderr, "b"); if(SDL_GetTicks() - updatetime > 1000) { updatetime = SDL_GetTicks(); if(DEBUGPRINT) fprintf(stderr, "%d\n", tickspersecond); tickspersecond = 0; } while ((temp = SDL_PollEvent(&e)) != 0 && !quit) { switch (e.type) { case SDL_QUIT: quit = 1; break; case SDL_USEREVENT: if(!droppedframes || uistate.paused) break; //If it drops too many frames, clear the event queue droppedframes--; //fprintf(stderr, "c"); tickspersecond++; for(i = 0; i < STEPSPERTICK; i++) { doDemos(uistate.demoselected, mysim); sim_step(mysim, TICKTIME); } //printGrid(mysim); tparams.timerlock = 0; break; case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: //doMouseEvent(&e, &selected, &hovered, mysim); break; case SDL_WINDOWEVENT: switch (e.window.event) { case SDL_WINDOWEVENT_ENTER: uistate.mouseinwindow = 1; case SDL_WINDOWEVENT_LEAVE: uistate.mouseinwindow = 0; break; } break; default: if(DEBUGPRINT) fprintf(stderr, "UNKNOWN EVENT TYPE\n"); break; } } } cleanup(); return 0; }
void draw_3D_graphics() { static double Px, Py, Pz, roll, pitch, yaw, yawA; static int initilization = 0; int i; static double x = 0.0, dx; // x - an offset for the object static double y = 0.0, dy; // z offset controlled by keyboard input static double t; // clock time from t0 static double t0; // initial clock time static double T, fps, tp, dt, t_sim; // Declare and load x-file mesh object for later static mesh m1("car.x"); // Change scale m1.Scale = 0.5; // Set initial position for mesh (proper positioning) m1.Roll_0 = 1.645; // Initialize everything here (once) if (!initilization) { t0 = high_resolution_time(); tp = 0.0; t_sim = 0.0; initilization = 1; } // set_view(); set_2D_view(50.0, 50.0); // draw the axes (red = x, y = green, z = blue) draw_XYZ(5.0); // set axes of 5 m length t = high_resolution_time() - t0; // Time since start, as measured by a clock T = t - tp; // calculate dt frame or period fps = 1 / T; tp = t; // save previous time for later dt = 0.001; // we can pick a small dt for performance / stability while (t_sim < t) { sim_step(dt, x, y, yaw); t_sim += dt; } // Use keyboard input to change yaw (upper keys for alphabet) if (KEY('Z')) yaw -= 0.001; if (KEY('X')) yaw += 0.001; // connect graphics parameters with the simulation output // NOTE: BECAUSE OF THE WAY WE'VE WRITTEN OUR SIMULATION CODE, THE SIGN NOTATION IS DIFFERENT! // HENCE THE NEED FOR YAWA = YAW ACTUAL Px = x; Py = y; Pz = 0; roll = 0; pitch = 0; yawA = -yaw; // draw x-file / mesh object m1.draw(Px, Py, Pz, yawA, pitch, roll); }