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.
 
 
 
 
 

266 lines
7.0 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 <string.h>
  4. #include "vdp.h"
  5. #include "util.h"
  6. #define CODE_VRAM_READ 0
  7. #define CODE_VRAM_WRITE 1
  8. #define CODE_REG_WRITE 2
  9. #define CODE_CRAM_WRITE 3
  10. /*
  11. Initialize the Video Display Processor (VDP).
  12. */
  13. void vdp_init(VDP *vdp)
  14. {
  15. vdp->vram = cr_malloc(sizeof(uint8_t) * VDP_VRAM_SIZE);
  16. vdp->cram = cr_malloc(sizeof(uint8_t) * VDP_CRAM_SIZE);
  17. memset(vdp->vram, 0x00, VDP_VRAM_SIZE);
  18. memset(vdp->cram, 0x00, VDP_CRAM_SIZE);
  19. }
  20. /*
  21. Free memory previously allocated by the VDP.
  22. */
  23. void vdp_free(VDP *vdp)
  24. {
  25. free(vdp->vram);
  26. }
  27. /*
  28. Power on the VDP, setting up initial state.
  29. */
  30. void vdp_power(VDP *vdp)
  31. {
  32. vdp->regs[0x00] = 0x00;
  33. vdp->regs[0x01] = 0x00;
  34. vdp->regs[0x02] = 0xFF;
  35. vdp->regs[0x03] = 0xFF;
  36. vdp->regs[0x04] = 0xFF;
  37. vdp->regs[0x05] = 0xFF;
  38. vdp->regs[0x06] = 0xFF;
  39. vdp->regs[0x07] = 0x00;
  40. vdp->regs[0x08] = 0x00;
  41. vdp->regs[0x09] = 0x00;
  42. vdp->regs[0x0A] = 0x01;
  43. vdp->h_counter = 0;
  44. vdp->v_counter = 0;
  45. vdp->v_count_jump = false;
  46. vdp->control_code = 0;
  47. vdp->control_addr = 0;
  48. vdp->control_flag = false;
  49. vdp->stat_int = vdp->stat_ovf = vdp->stat_col = 0;
  50. vdp->read_buf = 0;
  51. vdp->cram_latch = 0;
  52. }
  53. /*
  54. Return whether frame-completion interrupts are enabled.
  55. */
  56. static bool should_frame_interrupt(const VDP *vdp)
  57. {
  58. return vdp->regs[0x01] & 0x20;
  59. }
  60. /*
  61. Return the base address of the pattern name table.
  62. */
  63. static uint16_t get_pnt_base(const VDP *vdp)
  64. {
  65. return (vdp->regs[0x02] & 0x0E) << 10;
  66. }
  67. /*
  68. Return the base address of the sprite attribute table.
  69. */
  70. static uint16_t get_sat_base(const VDP *vdp)
  71. {
  72. return (vdp->regs[0x05] & 0x7E) << 7;
  73. }
  74. /*
  75. Return the base address of the sprite generator table.
  76. */
  77. static uint16_t get_sgt_base(const VDP *vdp)
  78. {
  79. return (vdp->regs[0x06] & 0x04) << 11;
  80. }
  81. /*
  82. Return the CRAM address of the backdrop color.
  83. */
  84. static uint8_t get_backdrop_addr(const VDP *vdp)
  85. {
  86. return ((vdp->regs[0x07] & 0x0F) << 1) + 0x20;
  87. }
  88. /*
  89. Advance the V counter for the next scanline.
  90. */
  91. static void advance_scanline(VDP *vdp)
  92. {
  93. if (vdp->v_counter == 0xDA)
  94. vdp->v_count_jump = !vdp->v_count_jump;
  95. if (vdp->v_counter == 0xDA && vdp->v_count_jump)
  96. vdp->v_counter = 0xD5;
  97. else
  98. vdp->v_counter++;
  99. }
  100. /*
  101. Simulate one scanline within the VDP.
  102. TODO: elaborate
  103. */
  104. void vdp_simulate_line(VDP *vdp)
  105. {
  106. if (vdp->v_counter >= 0x18 && vdp->v_counter < 0xA8) {
  107. // TODO: draw current line
  108. }
  109. if (vdp->v_counter == 0xC0)
  110. vdp->stat_int = true;
  111. advance_scanline(vdp);
  112. }
  113. /*
  114. Read a byte from the VDP's control port, revealing status flags.
  115. The status byte consists of:
  116. 7 6 5 4 3 2 1 0
  117. F 9S C * * * * *
  118. - F: Interrupt flag: set when the effective display area is completed
  119. - 9S: 9th sprite / Sprite overflow: more than eight sprites on a scanline
  120. - C: Collision flag: two sprites have an overlapping pixel
  121. - *: Unused
  122. The control flag is also reset.
  123. */
  124. uint8_t vdp_read_control(VDP *vdp)
  125. {
  126. uint8_t status =
  127. (vdp->stat_int << 8) + (vdp->stat_ovf << 7) + (vdp->stat_col << 6);
  128. vdp->stat_int = vdp->stat_ovf = vdp->stat_col = 0;
  129. vdp->control_flag = false;
  130. return status;
  131. }
  132. /*
  133. Read a byte from the VDP's data port.
  134. This will return the contents of the read buffer, and then fill the buffer
  135. with the VRAM at the current control address, before incrementing the
  136. control address. The control flag is also reset.
  137. */
  138. uint8_t vdp_read_data(VDP *vdp)
  139. {
  140. uint8_t buffer = vdp->read_buf;
  141. vdp->read_buf = vdp->vram[vdp->control_addr];
  142. vdp->control_addr = (vdp->control_addr + 1) % 0x3FFF;
  143. vdp->control_flag = false;
  144. return buffer;
  145. }
  146. /*
  147. Write a byte into the VDP's control port.
  148. Depending on the status of the control flag, this will either update the
  149. lower byte of the control address, or the upper six bits of the control
  150. address and the control code. The flag is toggled by each control write,
  151. and reset by each control read and data read or write.
  152. If the control code indicates a VRAM read, the read buffer will be filled
  153. with the VRAM at the given control address, which is then incremented. If
  154. the code indicates a register write, the corresponding register
  155. (byte & 0x0F) will be written with the lower byte of the control address.
  156. */
  157. void vdp_write_control(VDP *vdp, uint8_t byte)
  158. {
  159. if (!vdp->control_flag) { // First byte
  160. vdp->control_addr = (vdp->control_addr & 0x3F00) + byte;
  161. } else { // Second byte
  162. vdp->control_addr = ((byte & 0x3F) << 8) + (vdp->control_addr & 0xFF);
  163. vdp->control_code = byte >> 6;
  164. }
  165. if (vdp->control_code == CODE_VRAM_READ) {
  166. vdp->read_buf = vdp->vram[vdp->control_addr];
  167. vdp->control_addr = (vdp->control_addr + 1) % 0x3FFF;
  168. } else if (vdp->control_code == CODE_REG_WRITE) {
  169. uint8_t reg = byte & 0x0F;
  170. if (reg <= VDP_REGS)
  171. vdp->regs[reg] = vdp->control_addr & 0xFF;
  172. }
  173. vdp->control_flag = !vdp->control_flag;
  174. }
  175. /*
  176. Write a byte into CRAM. Handles even/odd address latching.
  177. */
  178. static void write_cram(VDP *vdp, uint8_t byte)
  179. {
  180. if (!(vdp->control_addr % 2)) {
  181. vdp->cram_latch = byte;
  182. } else {
  183. vdp->cram[(vdp->control_addr - 1) % 0x3F] = vdp->cram_latch;
  184. vdp->cram[ vdp->control_addr % 0x3F] = byte % 0x0F;
  185. }
  186. }
  187. /*
  188. Write a byte into the VDP's data port.
  189. Depending on the control code, this either writes into the VRAM or CRAM at
  190. the current control address, which is then incremented. The control flag is
  191. also reset, and the read buffer is squashed.
  192. */
  193. void vdp_write_data(VDP *vdp, uint8_t byte)
  194. {
  195. if (vdp->control_code == CODE_CRAM_WRITE)
  196. write_cram(vdp, byte);
  197. else
  198. vdp->vram[vdp->control_addr] = byte;
  199. vdp->control_addr = (vdp->control_addr + 1) % 0x3FFF;
  200. vdp->control_flag = false;
  201. vdp->read_buf = byte;
  202. }
  203. /*
  204. Return whether the VDP is currently asserting an interrupt.
  205. */
  206. bool vdp_assert_irq(VDP *vdp)
  207. {
  208. return vdp->stat_int && should_frame_interrupt(vdp);
  209. }
  210. /*
  211. @DEBUG_LEVEL
  212. Print out all register values to stdout.
  213. */
  214. void vdp_dump_registers(const VDP *vdp)
  215. {
  216. const uint8_t *regs = vdp->regs;
  217. DEBUG("Dumping VDP register values:")
  218. DEBUG("- $00: 0x%02X (" BINARY_FMT ")", regs[0x00], BINARY_VAL(regs[0]))
  219. DEBUG("- $01: 0x%02X (" BINARY_FMT ")", regs[0x01], BINARY_VAL(regs[1]))
  220. DEBUG("- $02: 0x%02X (PNT: 0x%04X)", regs[0x02], get_pnt_base(vdp))
  221. DEBUG("- $03: 0x%02X (CT)", regs[0x03])
  222. DEBUG("- $04: 0x%02X (BPG)", regs[0x04])
  223. DEBUG("- $05: 0x%02X (SAT: 0x%04X)", regs[0x05], get_sat_base(vdp))
  224. DEBUG("- $06: 0x%02X (SGT: 0x%04X)", regs[0x06], get_sgt_base(vdp))
  225. DEBUG("- $07: 0x%02X (BDC: 0x%02X)", regs[0x07], get_backdrop_addr(vdp))
  226. DEBUG("- $08: 0x%02X (HS)", regs[0x08])
  227. DEBUG("- $09: 0x%02X (VS)", regs[0x09])
  228. DEBUG("- $0A: 0x%02X (LC)", regs[0x0A])
  229. }