An emulator, assembler, and disassembler for the Sega Game Gear
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

gamegear.c 4.0 KiB

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