An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

63 lines
1.4 KiB

  1. /* Copyright (C) 2014-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <signal.h>
  4. #include <stdbool.h>
  5. #include <unistd.h>
  6. #include "emulator.h"
  7. #include "logging.h"
  8. #include "util.h"
  9. #define NS_PER_FRAME (1000 * 1000 * 1000 / 60)
  10. static volatile bool caught_signal;
  11. /*
  12. Signal handler for SIGINT.
  13. */
  14. static void handle_sigint(int sig)
  15. {
  16. (void) sig; // We don't care
  17. caught_signal = true;
  18. }
  19. /*
  20. Emulate a Game Gear. Handle I/O with the host computer.
  21. Block until emulation is finished.
  22. */
  23. void emulate(GameGear *gg)
  24. {
  25. caught_signal = false;
  26. signal(SIGINT, handle_sigint);
  27. DEBUG("Emulator powering GameGear")
  28. gamegear_power(gg, true);
  29. while (!caught_signal) {
  30. uint64_t start = get_time_ns(), delta;
  31. if (gamegear_simulate_frame(gg)) {
  32. ERROR("caught exception: %s", gamegear_get_exception(gg))
  33. if (DEBUG_LEVEL)
  34. z80_dump_registers(&gg->cpu);
  35. break;
  36. }
  37. // TODO: SDL draw / switch buffers here
  38. delta = get_time_ns() - start;
  39. if (delta < NS_PER_FRAME)
  40. usleep((NS_PER_FRAME - delta) / 1000);
  41. }
  42. if (caught_signal) {
  43. WARN("caught signal, stopping...")
  44. if (DEBUG_LEVEL)
  45. z80_dump_registers(&gg->cpu);
  46. }
  47. DEBUG("Emulator unpowering GameGear")
  48. gamegear_power(gg, false);
  49. signal(SIGINT, SIG_DFL);
  50. }