Mirror of CollapseOS
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.

91 lines
2.0KB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "tms9918.h"
  4. static uint8_t COLORS[0x10] = { // TODO: put actual color codes
  5. 0, // transparent
  6. 0, // black
  7. 1, // medium green
  8. 1, // light green
  9. 1, // dark blue
  10. 1, // light blue
  11. 1, // dark red
  12. 1, // cyan
  13. 1, // medium red
  14. 1, // light red
  15. 1, // dark yellow
  16. 1, // light yellow
  17. 1, // dark green
  18. 1, // magenta
  19. 1, // gray
  20. 1, // white
  21. };
  22. void tms_init(TMS9918 *tms)
  23. {
  24. memset(tms->vram, 0, TMS_VRAM_SIZE);
  25. memset(tms->regs, 0, 0x10);
  26. tms->has_cmdlsb = false;
  27. tms->curaddr = 0;
  28. tms->width = 40*6;
  29. tms->height = 24*8;
  30. }
  31. uint8_t tms_cmd_rd(TMS9918 *tms)
  32. {
  33. return 0;
  34. }
  35. void tms_cmd_wr(TMS9918 *tms, uint8_t val)
  36. {
  37. if (!tms->has_cmdlsb) {
  38. tms->cmdlsb = val;
  39. tms->has_cmdlsb = true;
  40. return;
  41. }
  42. tms->has_cmdlsb = false;
  43. if ((val & 0xc0) == 0x80) {
  44. // set register
  45. tms->regs[val&0xf] = tms->cmdlsb;
  46. } else {
  47. // VRAM
  48. tms->curaddr = ((val&0x3f) << 8) + tms->cmdlsb;
  49. }
  50. }
  51. uint8_t tms_data_rd(TMS9918 *tms)
  52. {
  53. if (tms->curaddr < TMS_VRAM_SIZE) {
  54. return tms->vram[tms->curaddr++];
  55. } else {
  56. return 0;
  57. }
  58. }
  59. void tms_data_wr(TMS9918 *tms, uint8_t val)
  60. {
  61. if (tms->curaddr < TMS_VRAM_SIZE) {
  62. tms->vram[tms->curaddr++] = val;
  63. }
  64. }
  65. // Returns a 8-bit RGB value (0b00bbggrr)
  66. uint8_t tms_pixel(TMS9918 *tms, uint16_t x, uint16_t y)
  67. {
  68. if ((tms->regs[1]&0x18) == 0x10 && (tms->regs[0]&0x40) == 0) {
  69. // Text mode
  70. uint16_t nameoff = (tms->regs[2] & 0xf) << 10;
  71. uint16_t patternoff = (tms->regs[4] & 0x7) << 11;
  72. uint8_t nameid = tms->vram[nameoff+(((y/8) * 40) + (x/6))];
  73. uint8_t patternline = tms->vram[patternoff+(nameid*8)+(y%8)];
  74. uint8_t color = tms->regs[7];
  75. if ((patternline>>(8-(x%6)))&1) {
  76. color >>= 4;
  77. }
  78. color &= 0xf;
  79. return color;
  80. } else { // unsupported mode
  81. return 0;
  82. }
  83. }