stardance has been extended another month! the new deadline is october 31 :)

You are browsing as a guest. Sign up (or log in) to start making projects!

5h 0m 13s logged

Devlog 7 - Timing

  • The next main thing I implemented were some more instructions - namely SRL, RR, RRA and ADD HL. These weren’t too hard, as the first 3 are similar to regular bit shifts, and ADD HL is just adding. While I was working on these, I also fixed some bugs with the existing instructions
  • This allowed me to finally pass the cpu_instrs 6 test ROM, which although not really a massive goal, did help to signify the progress I had made. I then moved onto the 7th test, for which I implemented some load functions for the stack pointer register
  • The biggest improvement of this devlog was getting everything timing correctly. The Gameboy is supposed to run at 4.19 MHz and 59.7 FPS, but at the time, the emulator was just running as fast as possible.
  • To solve this, I first made each instruction call a function to add the amount of cycles it takes to a unwaitedCycles variable.
  • Then, I saved the current time in nanoseconds since the program started (provided by the SDL_GetTicksNS() function) into a variable at the start of an instruction’s execution. After the instruction is executed, a function is then called that finds the expected execution time the by doing the amount of cycles multiplied by the period of 4.19MHz (about 238 nanoseconds). This expected time is then subtracted from the actual time, which is the start time stored previously subtracted from the current time. The program is then delayed by the result, which should keep the CPU at around 4.19MHz, unless it takes too long.
  • I did have some concerns with my emulator’s performance, as when running as a debug build, it wasn’t running nearly fast enough to reach 4.19MHz, but after compiling as a release build with optimisation, it was much faster and ran under the required time. However, I’m not sure how consistent this will be between devices, so I may have to optimise later
  • Finally, I added a similar function to limit the FPS to around 60 (technically 59.7), which just checks the time since the last frame was rendered, and if it hasn’t been long enough, it continues CPU execution and doesn’t render. You can see the frame limit working in the screenshot, where the frametime is a consistent 16.8 ms and the FPS is locked to 60, as opposed to before where it was unlimited. (Ignore the text, it doesn’t change yet to show a ROM has been loaded)
0
16

Comments 0

No comments yet. Be the first!