Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void chip8_cycle(Chip8 *vm)
- {
- vm->ticks++;
- fetch_decode_and_execute(vm);
- }
- int main(int argc, char *argv[])
- {
- if (argc != 3) {
- SDL_Log("Usage: %s <instr-per-sec> <rom-file>", argv[0]);
- return 1;
- }
- // Instructions per second executed by the VM (e.g. 540)
- int ips = SDL_atoi(argv[1]);
- // Read ROM file ...
- Chip8 vm;
- chip8_init(&vm);
- chip8_load_rom(&vm, rom, rom_size);
- // If ips=540 clock_delay=1.85ms and timers_period=9
- double clock_delay = 1000.0 / ips;
- int timers_period = ips / 60;
- // Initialize graphics ...
- while (true) {
- Uint64 start = SDL_GetPerformanceCounter();
- // Handle input event ...
- chip8_cycle(&vm);
- // If ips=540 decrease the timers every 9th instruction
- if (vm.ticks % timers_period == 0)
- chip8_decrease_timers(&vm);
- // If executed instruction == 0xDxyn (DRAW) update graphics
- if ((vm.opcode & 0xF000) == 0xD000)
- // Update graphics ...
- Uint64 end = SDL_GetPerformanceCounter();
- double frequency = (double) SDL_GetPerformanceFrequency();
- double elapsed_time = ((end - start) * 1000) / frequency;
- if (elapsed_time < clock_delay)
- SDL_Delay((Uint32) (clock_delay - elapsed_time));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment