Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CLOCK_FREQUENCY 60 // 60Hz
- #define CLOCK_PERIOD 16.666f // Delay in milliseconds needed to achieve 60fps (1000/60=16.666)
- void chip8_init(Chip8 *vm, int ips)
- {
- // Initialize the VM ...
- // Instructions per frame. If ips=540 then ipf=9
- vm->IPF = ips / CLOCK_FREQUENCY;
- }
- void chip8_cycle(Chip8 *vm)
- {
- // If ips=540 execute 9 instructions per cycle
- for (int i = 0; i < vm->IPF; i++) {
- fetch_decode_execute(vm);
- }
- chip8_decrease_timers(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, ips);
- chip8_load_rom(&vm, rom, rom_size);
- // Initialize graphics ...
- while (true) {
- Uint64 start = SDL_GetPerformanceCounter();
- // Handle input event ...
- chip8_cycle(&vm);
- // Update graphics ...
- Uint64 end = SDL_GetPerformanceCounter();
- double frequency = (double) SDL_GetPerformanceFrequency();
- double elapsed_time = ((end - start) * 1000) / frequency;
- if (elapsed_time < CLOCK_PERIOD)
- SDL_Delay((Uint32) (CLOCK_PERIOD - elapsed_time));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement