An emulator, assembler, and disassembler for the Sega Game Gear
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* Copyright (C) 2014-2017 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 <SDL.h>
  5. #include "vdp.h"
  6. #include "util.h"
  7. #define FLAG_CONTROL 0x01
  8. #define FLAG_FRAME_INT 0x02
  9. #define FLAG_LINE_INT 0x04
  10. #define FLAG_SPR_OVF 0x08
  11. #define FLAG_SPR_COL 0x10
  12. #define CODE_VRAM_READ 0
  13. #define CODE_VRAM_WRITE 1
  14. #define CODE_REG_WRITE 2
  15. #define CODE_CRAM_WRITE 3
  16. #define COLBUF_BG_PRIORITY 0x10
  17. #define COLBUF_OPAQUE_SPRITE 0x20
  18. /*
  19. Initialize the Video Display Processor (VDP).
  20. The VDP will write to its pixels array whenever it draws a scanline. It
  21. defaults to NULL, but you should set it to something if you want to see its
  22. output.
  23. */
  24. void vdp_init(VDP *vdp)
  25. {
  26. vdp->pixels = NULL;
  27. vdp->vram = cr_malloc(sizeof(uint8_t) * VDP_VRAM_SIZE);
  28. vdp->cram = cr_malloc(sizeof(uint8_t) * VDP_CRAM_SIZE);
  29. }
  30. /*
  31. Free memory previously allocated by the VDP.
  32. */
  33. void vdp_free(VDP *vdp)
  34. {
  35. free(vdp->vram);
  36. free(vdp->cram);
  37. }
  38. /*
  39. Power on the VDP, setting up initial state.
  40. */
  41. void vdp_power(VDP *vdp)
  42. {
  43. memset(vdp->vram, 0x00, VDP_VRAM_SIZE);
  44. memset(vdp->cram, 0x00, VDP_CRAM_SIZE);
  45. vdp->regs[0x00] = 0x00;
  46. vdp->regs[0x01] = 0x00;
  47. vdp->regs[0x02] = 0xFF;
  48. vdp->regs[0x03] = 0xFF;
  49. vdp->regs[0x04] = 0xFF;
  50. vdp->regs[0x05] = 0xFF;
  51. vdp->regs[0x06] = 0xFF;
  52. vdp->regs[0x07] = 0x00;
  53. vdp->regs[0x08] = 0x00;
  54. vdp->regs[0x09] = 0x00;
  55. vdp->regs[0x0A] = 0x01;
  56. vdp->h_counter = 0;
  57. vdp->v_counter = 0;
  58. vdp->v_count_jump = false;
  59. vdp->flags = 0;
  60. vdp->control_code = 0;
  61. vdp->control_addr = 0;
  62. vdp->line_count = 0x01;
  63. vdp->read_buf = 0;
  64. vdp->cram_latch = 0;
  65. }
  66. /*
  67. Return whether line-completion interrupts are enabled.
  68. */
  69. static bool should_line_interrupt(const VDP *vdp)
  70. {
  71. return vdp->regs[0x00] & 0x10;
  72. }
  73. /*
  74. Return whether frame-completion interrupts are enabled.
  75. */
  76. static bool should_frame_interrupt(const VDP *vdp)
  77. {
  78. return vdp->regs[0x01] & 0x20;
  79. }
  80. /*
  81. Return whether the display should be visible or completely blank.
  82. */
  83. static bool is_display_visible(const VDP *vdp)
  84. {
  85. return vdp->regs[0x01] & 0x40;
  86. }
  87. /*
  88. Get the height of all sprites in patterns (1 pattern = 8x8 pixels).
  89. */
  90. static uint8_t get_sprite_height(const VDP *vdp)
  91. {
  92. return (vdp->regs[0x01] & 0x02) ? 2 : 1;
  93. }
  94. /*
  95. Return the base address of the pattern name table.
  96. */
  97. static uint16_t get_pnt_base(const VDP *vdp)
  98. {
  99. return (vdp->regs[0x02] & 0x0E) << 10;
  100. }
  101. /*
  102. Return the base address of the sprite attribute table.
  103. */
  104. static uint16_t get_sat_base(const VDP *vdp)
  105. {
  106. return (vdp->regs[0x05] & 0x7E) << 7;
  107. }
  108. /*
  109. Return the global offset of the sprite generator table.
  110. */
  111. static uint16_t get_sgt_offset(const VDP *vdp)
  112. {
  113. return (vdp->regs[0x06] & 0x04) << 6;
  114. }
  115. /*
  116. Return the backdrop color as a CRAM index.
  117. */
  118. static uint8_t get_backdrop_color(const VDP *vdp)
  119. {
  120. return ((vdp->regs[0x07] & 0x0F) << 1) + 0x20;
  121. }
  122. /*
  123. Return the horizontal background scroll value.
  124. */
  125. static uint8_t get_bg_hscroll(const VDP *vdp)
  126. {
  127. return vdp->regs[0x08];
  128. }
  129. /*
  130. Return the vertical background scroll value.
  131. */
  132. static uint8_t get_bg_vscroll(const VDP *vdp)
  133. {
  134. return vdp->regs[0x09];
  135. }
  136. /*
  137. Return the packed background tile at the given row and column.
  138. */
  139. static uint16_t get_background_tile(const VDP *vdp, uint8_t row, uint8_t col)
  140. {
  141. uint8_t *pnt = vdp->vram + get_pnt_base(vdp);
  142. uint16_t index = row * 32 + col;
  143. return pnt[2 * index] + (pnt[2 * index + 1] << 8);
  144. }
  145. /*
  146. Get the CRAM color index of the given (row, col) in the given pattern.
  147. */
  148. static uint8_t read_pattern(const VDP *vdp, uint16_t pattern,
  149. uint8_t row, uint8_t col)
  150. {
  151. uint8_t *planes = &vdp->vram[32 * pattern + 4 * row];
  152. return ((planes[0] >> (7 - col)) & 1) +
  153. (((planes[1] >> (7 - col)) & 1) << 1) +
  154. (((planes[2] >> (7 - col)) & 1) << 2) +
  155. (((planes[3] >> (7 - col)) & 1) << 3);
  156. }
  157. /*
  158. Return the BGR444 color at the given CRAM index.
  159. The index should be between 0 and 15, as there are 16 colors per palette.
  160. */
  161. static uint16_t get_color(const VDP *vdp, uint8_t index, bool palette)
  162. {
  163. uint8_t offset = 2 * (index + 16 * palette);
  164. return vdp->cram[offset] + (vdp->cram[offset + 1] << 8);
  165. }
  166. /*
  167. Toggle visibility of the display.
  168. This sets the alpha channel of every pixel to be 0xFF (if true)
  169. or 0x00 (if false).
  170. */
  171. static void toggle_display_visibility(VDP *vdp, bool visible)
  172. {
  173. if (!vdp->pixels)
  174. return;
  175. uint8_t a = visible ? 0xFF : 0x00;
  176. for (size_t i = 0; i < 160 * 144; i++)
  177. vdp->pixels[i] = (a << 24) | (vdp->pixels[i] & 0x00FFFFFF);
  178. }
  179. /*
  180. Draw a pixel onto our pixel array at the given coordinates.
  181. The color should be in BGR444 format, as returned by get_color().
  182. */
  183. static void draw_pixel(VDP *vdp, uint8_t y, uint8_t x, uint16_t color)
  184. {
  185. uint8_t a = is_display_visible(vdp) ? 0xFF : 0x00;
  186. uint8_t r = 0x11 * (color & 0x000F);
  187. uint8_t g = 0x11 * ((color & 0x00F0) >> 4);
  188. uint8_t b = 0x11 * ((color & 0x0F00) >> 8);
  189. uint32_t argb = (a << 24) + (r << 16) + (g << 8) + b;
  190. vdp->pixels[y * 160 + x] = argb;
  191. }
  192. /*
  193. Draw the background of the current scanline.
  194. */
  195. static void draw_background(VDP *vdp, uint8_t *colbuf)
  196. {
  197. uint8_t src_row = (vdp->v_counter + get_bg_vscroll(vdp)) % (28 << 3);
  198. uint8_t dst_row = vdp->v_counter - 0x18;
  199. uint8_t vcell = src_row >> 3;
  200. uint8_t hcell, col;
  201. uint8_t start_col = get_bg_hscroll(vdp) >> 3;
  202. uint8_t fine_scroll = get_bg_hscroll(vdp) % 8;
  203. for (col = 5; col < 20 + 6; col++) {
  204. hcell = (32 - start_col + col) % 32;
  205. uint16_t tile = get_background_tile(vdp, vcell, hcell);
  206. uint16_t pattern = tile & 0x01FF;
  207. bool palette = tile & 0x0800;
  208. bool priority = tile & 0x1000;
  209. bool vflip = tile & 0x0400;
  210. bool hflip = tile & 0x0200;
  211. uint8_t vshift = vflip ? (7 - src_row % 8) : (src_row % 8), hshift;
  212. uint8_t pixel, index;
  213. int16_t dst_col;
  214. uint16_t color;
  215. for (pixel = 0; pixel < 8; pixel++) {
  216. dst_col = ((col - 6) << 3) + pixel + fine_scroll;
  217. if (dst_col < 0 || dst_col >= 160)
  218. continue;
  219. hshift = hflip ? (7 - pixel) : pixel;
  220. index = read_pattern(vdp, pattern, vshift, hshift);
  221. color = get_color(vdp, index, palette);
  222. draw_pixel(vdp, dst_row, dst_col, color);
  223. if (priority && index != 0)
  224. colbuf[dst_col] |= COLBUF_BG_PRIORITY;
  225. }
  226. }
  227. }
  228. /*
  229. Draw sprites in the current scanline.
  230. */
  231. static void draw_sprites(VDP *vdp, uint8_t *colbuf)
  232. {
  233. uint8_t *sat = vdp->vram + get_sat_base(vdp);
  234. uint8_t spritebuf[8], nsprites = 0, i;
  235. uint8_t height = get_sprite_height(vdp);
  236. for (i = 0; i < 64; i++) {
  237. uint8_t y = sat[i] + 1;
  238. if (y == 0xD0 + 1)
  239. break;
  240. if (vdp->v_counter >= y && vdp->v_counter < y + (height * 8)) {
  241. if (nsprites >= 8) {
  242. vdp->flags |= FLAG_SPR_OVF;
  243. break;
  244. }
  245. spritebuf[nsprites++] = i;
  246. }
  247. }
  248. uint8_t dst_row = vdp->v_counter - 0x18;
  249. while (nsprites-- > 0) {
  250. i = spritebuf[nsprites];
  251. uint8_t y = sat[i] + 1;
  252. uint8_t x = sat[0x80 + 2 * i];
  253. uint8_t vshift;
  254. uint16_t pattern;
  255. if (height == 1) {
  256. pattern = get_sgt_offset(vdp) + sat[0x80 + 2 * i + 1];
  257. vshift = vdp->v_counter - y;
  258. } else if (height == 2) {
  259. pattern = (get_sgt_offset(vdp) + sat[0x80 + 2 * i + 1]) & 0x1FE;
  260. pattern |= (vdp->v_counter - y) >> 3;
  261. vshift = (vdp->v_counter - y) % 8;
  262. } else {
  263. // TODO: sprite doubling
  264. }
  265. uint8_t pixel, index;
  266. uint16_t color;
  267. int16_t dst_col;
  268. for (pixel = 0; pixel < 8; pixel++) {
  269. dst_col = x + pixel - (6 << 3);
  270. if (dst_col < 0 || dst_col >= 160)
  271. continue;
  272. if (colbuf[dst_col] & COLBUF_BG_PRIORITY)
  273. continue;
  274. index = read_pattern(vdp, pattern, vshift, pixel);
  275. if (index == 0)
  276. continue;
  277. if (colbuf[dst_col] & COLBUF_OPAQUE_SPRITE)
  278. vdp->flags |= FLAG_SPR_COL;
  279. else
  280. colbuf[dst_col] |= COLBUF_OPAQUE_SPRITE;
  281. color = get_color(vdp, index, 1);
  282. draw_pixel(vdp, dst_row, dst_col, color);
  283. }
  284. }
  285. }
  286. /*
  287. Draw the current scanline.
  288. */
  289. static void draw_scanline(VDP *vdp)
  290. {
  291. if (!vdp->pixels)
  292. return;
  293. uint8_t colbuf[160] = {0x00};
  294. draw_background(vdp, colbuf);
  295. draw_sprites(vdp, colbuf);
  296. }
  297. /*
  298. Update the line counter, which triggers line interrupts.
  299. */
  300. static void update_line_counter(VDP *vdp)
  301. {
  302. if (vdp->v_counter < 0xC0) {
  303. if (vdp->line_count == 0x00) {
  304. vdp->flags |= FLAG_LINE_INT;
  305. vdp->line_count = vdp->regs[0x0A];
  306. } else {
  307. vdp->line_count--;
  308. }
  309. } else {
  310. vdp->line_count = vdp->regs[0x0A];
  311. }
  312. }
  313. /*
  314. Advance the V counter for the next scanline.
  315. */
  316. static void advance_scanline(VDP *vdp)
  317. {
  318. if (vdp->v_counter == 0xDA)
  319. vdp->v_count_jump = !vdp->v_count_jump;
  320. if (vdp->v_counter == 0xDA && vdp->v_count_jump)
  321. vdp->v_counter = 0xD5;
  322. else
  323. vdp->v_counter++;
  324. }
  325. /*
  326. Simulate one line within the VDP.
  327. */
  328. void vdp_simulate_line(VDP *vdp)
  329. {
  330. if (vdp->v_counter >= 0x18 && vdp->v_counter < 0xA8)
  331. draw_scanline(vdp);
  332. if (vdp->v_counter == 0xC0)
  333. vdp->flags |= FLAG_FRAME_INT;
  334. update_line_counter(vdp);
  335. advance_scanline(vdp);
  336. }
  337. /*
  338. Read a byte from the VDP's control port, revealing status flags.
  339. The status byte consists of:
  340. 7 6 5 4 3 2 1 0
  341. F 9S C * * * * *
  342. - F: Frame interrupt: set when the effective display area is completed
  343. - 9S: 9th sprite / Sprite overflow: more than eight sprites on a scanline
  344. - C: Sprite collision: two sprites have an overlapping pixel
  345. - *: Unused
  346. The control and line interrupt flags are also reset.
  347. */
  348. uint8_t vdp_read_control(VDP *vdp)
  349. {
  350. uint8_t status =
  351. (!!(vdp->flags & FLAG_FRAME_INT) << 7) +
  352. (!!(vdp->flags & FLAG_SPR_OVF) << 6) +
  353. (!!(vdp->flags & FLAG_SPR_COL) << 5);
  354. vdp->flags = 0;
  355. return status;
  356. }
  357. /*
  358. Read a byte from the VDP's data port.
  359. This will return the contents of the read buffer, and then fill the buffer
  360. with the VRAM at the current control address, before incrementing the
  361. control address. The control flag is also reset.
  362. */
  363. uint8_t vdp_read_data(VDP *vdp)
  364. {
  365. uint8_t buffer = vdp->read_buf;
  366. vdp->read_buf = vdp->vram[vdp->control_addr];
  367. vdp->control_addr = (vdp->control_addr + 1) & 0x3FFF;
  368. vdp->flags &= ~FLAG_CONTROL;
  369. return buffer;
  370. }
  371. /*
  372. Set the given VDP register.
  373. */
  374. static void write_reg(VDP *vdp, uint8_t reg, uint8_t byte)
  375. {
  376. if (reg == 0x01 && (vdp->regs[reg] & 0x40) != (byte & 0x40))
  377. toggle_display_visibility(vdp, byte & 0x40);
  378. vdp->regs[reg] = byte;
  379. }
  380. /*
  381. Write a byte into the VDP's control port.
  382. Depending on the status of the control flag, this will either update the
  383. lower byte of the control address, or the upper six bits of the control
  384. address and the control code. The flag is toggled by each control write,
  385. and reset by each control read and data read or write.
  386. If the control code indicates a VRAM read, the read buffer will be filled
  387. with the VRAM at the given control address, which is then incremented. If
  388. the code indicates a register write, the corresponding register
  389. (byte & 0x0F) will be written with the lower byte of the control address.
  390. */
  391. void vdp_write_control(VDP *vdp, uint8_t byte)
  392. {
  393. vdp->flags ^= FLAG_CONTROL;
  394. if (vdp->flags & FLAG_CONTROL) { // First byte
  395. vdp->control_addr = (vdp->control_addr & 0x3F00) + byte;
  396. return;
  397. }
  398. vdp->control_addr = ((byte & 0x3F) << 8) + (vdp->control_addr & 0xFF);
  399. vdp->control_code = byte >> 6;
  400. if (vdp->control_code == CODE_VRAM_READ) {
  401. vdp->read_buf = vdp->vram[vdp->control_addr];
  402. vdp->control_addr = (vdp->control_addr + 1) & 0x3FFF;
  403. } else if (vdp->control_code == CODE_REG_WRITE) {
  404. uint8_t reg = byte & 0x0F;
  405. if (reg <= VDP_REGS)
  406. write_reg(vdp, reg, vdp->control_addr & 0xFF);
  407. }
  408. }
  409. /*
  410. Write a byte into CRAM. Handles even/odd address latching.
  411. */
  412. static void write_cram(VDP *vdp, uint8_t byte)
  413. {
  414. if (!(vdp->control_addr % 2)) {
  415. vdp->cram_latch = byte;
  416. } else {
  417. vdp->cram[(vdp->control_addr - 1) & 0x3F] = vdp->cram_latch;
  418. vdp->cram[ vdp->control_addr & 0x3F] = byte & 0x0F;
  419. }
  420. }
  421. /*
  422. Write a byte into the VDP's data port.
  423. Depending on the control code, this either writes into the VRAM or CRAM at
  424. the current control address, which is then incremented. The control flag is
  425. also reset, and the read buffer is squashed.
  426. */
  427. void vdp_write_data(VDP *vdp, uint8_t byte)
  428. {
  429. if (vdp->control_code == CODE_CRAM_WRITE)
  430. write_cram(vdp, byte);
  431. else
  432. vdp->vram[vdp->control_addr] = byte;
  433. vdp->control_addr = (vdp->control_addr + 1) & 0x3FFF;
  434. vdp->flags &= ~FLAG_CONTROL;
  435. vdp->read_buf = byte;
  436. }
  437. /*
  438. Return whether the VDP is currently asserting an interrupt.
  439. */
  440. bool vdp_assert_irq(VDP *vdp)
  441. {
  442. return (vdp->flags & FLAG_FRAME_INT && should_frame_interrupt(vdp)) ||
  443. (vdp->flags & FLAG_LINE_INT && should_line_interrupt(vdp));
  444. }
  445. /*
  446. @DEBUG_LEVEL
  447. Print out all register values to stdout.
  448. */
  449. void vdp_dump_registers(const VDP *vdp)
  450. {
  451. const uint8_t *regs = vdp->regs;
  452. DEBUG("Dumping VDP register values:")
  453. // TODO: show flags
  454. DEBUG("- $00: 0x%02X (" BINARY_FMT ")", regs[0x00], BINARY_VAL(regs[0]))
  455. DEBUG("- $01: 0x%02X (" BINARY_FMT ")", regs[0x01], BINARY_VAL(regs[1]))
  456. DEBUG("- $02: 0x%02X (PNT: 0x%04X)", regs[0x02], get_pnt_base(vdp))
  457. DEBUG("- $03: 0x%02X (CT)", regs[0x03])
  458. DEBUG("- $04: 0x%02X (BPG)", regs[0x04])
  459. DEBUG("- $05: 0x%02X (SAT: 0x%04X)", regs[0x05], get_sat_base(vdp))
  460. DEBUG("- $06: 0x%02X (SGT: 0x%04X)", regs[0x06], get_sgt_offset(vdp) << 5)
  461. DEBUG("- $07: 0x%02X (BDC: 0x%02X)", regs[0x07], get_backdrop_color(vdp))
  462. DEBUG("- $08: 0x%02X (HS)", regs[0x08])
  463. DEBUG("- $09: 0x%02X (VS)", regs[0x09])
  464. DEBUG("- $0A: 0x%02X (LC)", regs[0x0A])
  465. }