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.
 
 
 
 
 

170 lines
4.6 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. memset(vdp->vram, 0x00, VDP_VRAM_SIZE);
  17. }
  18. /*
  19. Free memory previously allocated by the VDP.
  20. */
  21. void vdp_free(VDP *vdp)
  22. {
  23. free(vdp->vram);
  24. }
  25. /*
  26. Power on the VDP, setting up initial state.
  27. */
  28. void vdp_power(VDP *vdp)
  29. {
  30. vdp->regs[0x00] = 0x00;
  31. vdp->regs[0x01] = 0x00;
  32. vdp->regs[0x02] = 0xFF;
  33. vdp->regs[0x03] = 0xFF;
  34. vdp->regs[0x04] = 0xFF;
  35. vdp->regs[0x05] = 0xFF;
  36. vdp->regs[0x06] = 0xFF;
  37. vdp->regs[0x07] = 0x00;
  38. vdp->regs[0x08] = 0x00;
  39. vdp->regs[0x09] = 0x00;
  40. vdp->regs[0x0A] = 0x01;
  41. vdp->h_counter = 0;
  42. vdp->v_counter = 0;
  43. vdp->v_count_jump = false;
  44. vdp->control_code = 0;
  45. vdp->control_addr = 0;
  46. vdp->control_flag = false;
  47. vdp->stat_int = vdp->stat_ovf = vdp->stat_col = 0;
  48. vdp->read_buf = 0;
  49. }
  50. /*
  51. Simulate one scanline within the VDP.
  52. TODO: elaborate
  53. */
  54. void vdp_simulate_line(VDP *vdp)
  55. {
  56. if (vdp->v_counter >= 0x18 && vdp->v_counter < 0xA8) {
  57. // TODO: draw current line
  58. }
  59. if (vdp->v_counter == 0xDA)
  60. vdp->v_count_jump = !vdp->v_count_jump;
  61. if (vdp->v_counter == 0xDA && vdp->v_count_jump)
  62. vdp->v_counter = 0xD5;
  63. else
  64. vdp->v_counter++;
  65. }
  66. /*
  67. Read a byte from the VDP's control port, revealing status flags.
  68. The status byte consists of:
  69. 7 6 5 4 3 2 1 0
  70. F 9S C * * * * *
  71. - F: Interrupt flag: set when the effective display area is completed
  72. - 9S: 9th sprite / Sprite overflow: more than eight sprites on a scanline
  73. - C: Collision flag: two sprites have an overlapping pixel
  74. - *: Unused
  75. The control flag is also reset.
  76. */
  77. uint8_t vdp_read_control(VDP *vdp)
  78. {
  79. uint8_t status =
  80. (vdp->stat_int << 8) + (vdp->stat_ovf << 7) + (vdp->stat_col << 6);
  81. vdp->stat_int = vdp->stat_ovf = vdp->stat_col = 0;
  82. vdp->control_flag = false;
  83. return status;
  84. }
  85. /*
  86. Read a byte from the VDP's data port.
  87. This will return the contents of the read buffer, and then fill the buffer
  88. with the VRAM at the current control address, before incrementing the
  89. control address. The control flag is also reset.
  90. */
  91. uint8_t vdp_read_data(VDP *vdp)
  92. {
  93. uint8_t buffer = vdp->read_buf;
  94. vdp->read_buf = vdp->vram[vdp->control_addr];
  95. vdp->control_addr = (vdp->control_addr + 1) % 0x3FFF;
  96. vdp->control_flag = false;
  97. return buffer;
  98. }
  99. /*
  100. Write a byte into the VDP's control port.
  101. Depending on the status of the control flag, this will either update the
  102. lower byte of the control address, or the upper six bits of the control
  103. address and the control code. The flag is toggled by each control write,
  104. and reset by each control read and data read or write.
  105. If the control code indicates a VRAM read, the read buffer will be filled
  106. with the VRAM at the given control address, which is then incremented. If
  107. the code indicates a register write, the corresponding register
  108. (byte & 0x0F) will be written with the lower byte of the control address.
  109. */
  110. void vdp_write_control(VDP *vdp, uint8_t byte)
  111. {
  112. if (!vdp->control_flag) { // First byte
  113. vdp->control_addr = (vdp->control_addr & 0x3F00) + byte;
  114. } else { // Second byte
  115. vdp->control_addr = ((byte & 0x3F) << 8) + (vdp->control_addr & 0xFF);
  116. vdp->control_code = byte >> 6;
  117. }
  118. if (vdp->control_code == CODE_VRAM_READ) {
  119. vdp->read_buf = vdp->vram[vdp->control_addr];
  120. vdp->control_addr = (vdp->control_addr + 1) % 0x3FFF;
  121. } else if (vdp->control_code == CODE_REG_WRITE) {
  122. uint8_t reg = byte & 0x0F;
  123. if (reg <= VDP_REGS)
  124. vdp->regs[reg] = vdp->control_addr & 0xFF;
  125. }
  126. vdp->control_flag = !vdp->control_flag;
  127. }
  128. /*
  129. Write a byte into the VDP's data port.
  130. Depending on the control code, this either writes into the VRAM or CRAM at
  131. the current control address, which is then incremented. The control flag is
  132. also reset, and the read buffer is squashed.
  133. */
  134. void vdp_write_data(VDP *vdp, uint8_t byte)
  135. {
  136. if (vdp->control_code == CODE_CRAM_WRITE)
  137. FATAL("unimplemented: VDP CRAM write @ 0x%04X", vdp->control_addr) // TODO
  138. else
  139. vdp->vram[vdp->control_addr] = byte;
  140. vdp->control_addr = (vdp->control_addr + 1) % 0x3FFF;
  141. vdp->control_flag = false;
  142. vdp->read_buf = byte;
  143. }