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.
 
 
 
 
 

144 lines
4.1 KiB

  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <stdlib.h>
  4. #include "gamegear.h"
  5. #include "logging.h"
  6. #include "util.h"
  7. /* Clock speed in Hz was taken from the official Sega GG documentation */
  8. #define CPU_CLOCK_SPEED 3579545
  9. /*
  10. Create and return a pointer to a new GameGear object.
  11. */
  12. GameGear* gamegear_create()
  13. {
  14. GameGear *gg = cr_malloc(sizeof(GameGear));
  15. mmu_init(&gg->mmu);
  16. vdp_init(&gg->vdp);
  17. io_init(&gg->io, &gg->vdp);
  18. z80_init(&gg->cpu, &gg->mmu, &gg->io);
  19. gg->powered = false;
  20. gg->exc_buffer[0] = '\0';
  21. return gg;
  22. }
  23. /*
  24. Destroy a previously-allocated GameGear object.
  25. Does *not* destroy any loaded ROM objects.
  26. */
  27. void gamegear_destroy(GameGear *gg)
  28. {
  29. mmu_free(&gg->mmu);
  30. vdp_free(&gg->vdp);
  31. free(gg);
  32. }
  33. /*
  34. Load a ROM image into the GameGear object.
  35. Does *not* steal a reference to the ROM object, so it must be kept alive
  36. until another ROM is loaded or the GameGear is destroyed. Calling this
  37. function while the GameGear is powered on has no effect.
  38. */
  39. void gamegear_load(GameGear *gg, const ROM *rom)
  40. {
  41. if (gg->powered)
  42. return;
  43. mmu_load_rom(&gg->mmu, rom->data, rom->size);
  44. }
  45. /*
  46. Set the GameGear object's power state (true = on; false = off).
  47. Powering on the GameGear executes boot code (e.g. clearing memory and
  48. setting initial register values) and starts the clock. Powering it off
  49. stops the clock and clears any exception data.
  50. Setting the power state to its current value has no effect.
  51. */
  52. void gamegear_power(GameGear *gg, bool state)
  53. {
  54. if (gg->powered == state)
  55. return;
  56. if (state) {
  57. mmu_power(&gg->mmu);
  58. vdp_power(&gg->vdp);
  59. io_power(&gg->io);
  60. z80_power(&gg->cpu);
  61. gg->last_tick = get_time_ns();
  62. } else {
  63. gg->exc_buffer[0] = '\0';
  64. }
  65. gg->powered = state;
  66. }
  67. /*
  68. Update the simulation of the GameGear.
  69. This function simulates the number of clock cycles corresponding to the
  70. time since the last call to gamegear_simulate() or gamegear_power() if the
  71. system was just powered on. If the system is powered off, this function
  72. does nothing.
  73. The return value indicates whether an exception flag has been set
  74. somewhere. If true, emulation must be stopped. gamegear_get_exception() can
  75. be used to fetch exception information. Power-cycling the GameGear with
  76. gamegear_power(gg, false) followed by gamegear_power(gg, true) will reset
  77. the exception flag and allow emulation to restart normally.
  78. */
  79. bool gamegear_simulate(GameGear *gg)
  80. {
  81. if (!gg->powered)
  82. return false;
  83. uint64_t last = gg->last_tick, tick;
  84. tick = gg->last_tick = get_time_ns();
  85. return z80_do_cycles(&gg->cpu, (tick - last) * CPU_CLOCK_SPEED / 1e9);
  86. }
  87. /*
  88. If an exception flag has been set in the GameGear, return the reason.
  89. This function returns a const pointer to a buffer holding a human-readable
  90. exception string (although it may be cryptic to an end-user). The buffer is
  91. owned by the GameGear object and should not be freed - its contents last
  92. until the GameGear's power state is changed. If no exception flag is set,
  93. this function returns NULL.
  94. */
  95. const char* gamegear_get_exception(GameGear *gg)
  96. {
  97. #define SET_EXC(...) snprintf(gg->exc_buffer, GG_EXC_BUFF_SIZE, __VA_ARGS__);
  98. if (!gg->powered)
  99. return NULL;
  100. if (!gg->exc_buffer[0]) {
  101. if (gg->cpu.except) {
  102. switch (gg->cpu.exc_code) {
  103. case Z80_EXC_NOT_POWERED:
  104. SET_EXC("CPU not powered")
  105. break;
  106. case Z80_EXC_UNIMPLEMENTED_OPCODE:
  107. SET_EXC("unimplemented opcode: 0x%02X", gg->cpu.exc_data)
  108. break;
  109. case Z80_EXC_IO_ERROR:
  110. SET_EXC("I/O error on port 0x%02X", gg->cpu.exc_data)
  111. break;
  112. default:
  113. SET_EXC("unknown exception")
  114. break;
  115. }
  116. } else {
  117. return NULL;
  118. }
  119. }
  120. return gg->exc_buffer;
  121. #undef SET_EXC
  122. }