An emulator, assembler, and disassembler for the Sega Game Gear
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

135 righe
3.8 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. z80_init(&gg->cpu, &gg->mmu);
  17. gg->powered = false;
  18. gg->exc_buffer[0] = '\0';
  19. return gg;
  20. }
  21. /*
  22. Destroy a previously-allocated GameGear object.
  23. Does *not* destroy any loaded ROM objects.
  24. */
  25. void gamegear_destroy(GameGear *gg)
  26. {
  27. mmu_free(&gg->mmu);
  28. free(gg);
  29. }
  30. /*
  31. Load a ROM image into the GameGear object.
  32. Does *not* steal a reference to the ROM object, so it must be kept alive
  33. until another ROM is loaded or the GameGear is destroyed. Calling this
  34. function while the GameGear is powered on has no effect.
  35. */
  36. void gamegear_load(GameGear *gg, const ROM *rom)
  37. {
  38. if (gg->powered)
  39. return;
  40. mmu_load_rom(&gg->mmu, rom->data, rom->size);
  41. }
  42. /*
  43. Set the GameGear object's power state (true = on; false = off).
  44. Powering on the GameGear executes boot code (e.g. clearing memory and
  45. setting initial register values) and starts the clock. Powering it off
  46. stops the clock and clears any exception data.
  47. Setting the power state to its current value has no effect.
  48. */
  49. void gamegear_power(GameGear *gg, bool state)
  50. {
  51. if (gg->powered == state)
  52. return;
  53. if (state) {
  54. mmu_power(&gg->mmu);
  55. z80_power(&gg->cpu);
  56. gg->last_tick = get_time_ns();
  57. } else {
  58. gg->exc_buffer[0] = '\0';
  59. }
  60. gg->powered = state;
  61. }
  62. /*
  63. Update the simulation of the GameGear.
  64. This function simulates the number of clock cycles corresponding to the
  65. time since the last call to gamegear_simulate() or gamegear_power() if the
  66. system was just powered on. If the system is powered off, this function
  67. does nothing.
  68. The return value indicates whether an exception flag has been set
  69. somewhere. If true, emulation must be stopped. gamegear_get_exception() can
  70. be used to fetch exception information. Power-cycling the GameGear with
  71. gamegear_power(gg, false) followed by gamegear_power(gg, true) will reset
  72. the exception flag and allow emulation to restart normally.
  73. */
  74. bool gamegear_simulate(GameGear *gg)
  75. {
  76. if (!gg->powered)
  77. return false;
  78. uint64_t last = gg->last_tick, tick;
  79. tick = gg->last_tick = get_time_ns();
  80. return z80_do_cycles(&gg->cpu, (tick - last) * CPU_CLOCK_SPEED / 1e9);
  81. }
  82. /*
  83. If an exception flag has been set in the GameGear, return the reason.
  84. This function returns a const pointer to a buffer holding a human-readable
  85. exception string (although it may be cryptic to an end-user). The buffer is
  86. owned by the GameGear object and should not be freed - its contents last
  87. until the GameGear's power state is changed. If no exception flag is set,
  88. this function returns NULL.
  89. */
  90. const char* gamegear_get_exception(GameGear *gg)
  91. {
  92. #define SET_EXC(...) snprintf(gg->exc_buffer, GG_EXC_BUFF_SIZE, __VA_ARGS__);
  93. if (!gg->powered)
  94. return NULL;
  95. if (!gg->exc_buffer[0]) {
  96. if (gg->cpu.except) {
  97. switch (gg->cpu.exc_code) {
  98. case Z80_EXC_NOT_POWERED:
  99. SET_EXC("CPU not powered")
  100. break;
  101. case Z80_EXC_UNIMPLEMENTED_OPCODE:
  102. SET_EXC("unimplemented opcode: 0x%02X", gg->cpu.exc_data)
  103. break;
  104. default:
  105. SET_EXC("unknown exception")
  106. break;
  107. }
  108. } else {
  109. return NULL;
  110. }
  111. }
  112. return gg->exc_buffer;
  113. #undef SET_EXC
  114. }