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.

92 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->databuf = 0;
  29. tms->width = 40*6;
  30. tms->height = 24*8;
  31. }
  32. uint8_t tms_cmd_rd(TMS9918 *tms)
  33. {
  34. return 0;
  35. }
  36. void tms_cmd_wr(TMS9918 *tms, uint8_t val)
  37. {
  38. if (!tms->has_cmdlsb) {
  39. tms->cmdlsb = val;
  40. tms->has_cmdlsb = true;
  41. return;
  42. }
  43. tms->has_cmdlsb = false;
  44. if ((val & 0xc0) == 0x80) {
  45. // set register
  46. tms->regs[val&0xf] = tms->cmdlsb;
  47. } else {
  48. // VRAM
  49. tms->curaddr = ((val&0x3f) << 8) + tms->cmdlsb;
  50. if ((val & 0x40) == 0) { // reading VRAM
  51. tms->databuf = tms->vram[tms->curaddr];
  52. }
  53. }
  54. }
  55. uint8_t tms_data_rd(TMS9918 *tms)
  56. {
  57. return tms->databuf;
  58. }
  59. void tms_data_wr(TMS9918 *tms, uint8_t val)
  60. {
  61. tms->databuf = val;
  62. if (tms->curaddr < TMS_VRAM_SIZE) {
  63. tms->vram[tms->curaddr++] = val;
  64. }
  65. }
  66. // Returns a 8-bit RGB value (0b00bbggrr)
  67. uint8_t tms_pixel(TMS9918 *tms, uint16_t x, uint16_t y)
  68. {
  69. if ((tms->regs[1]&0x18) == 0x10 && (tms->regs[0]&0x40) == 0) {
  70. // Text mode
  71. uint16_t nameoff = (tms->regs[2] & 0xf) << 10;
  72. uint16_t patternoff = (tms->regs[4] & 0x7) << 11;
  73. uint8_t nameid = tms->vram[nameoff+(((y/8) * 40) + (x/6))];
  74. uint8_t patternline = tms->vram[patternoff+(nameid*8)+(y%8)];
  75. uint8_t color = tms->regs[7];
  76. if ((patternline>>(8-(x%6)))&1) {
  77. color >>= 4;
  78. }
  79. color &= 0xf;
  80. return color;
  81. } else { // unsupported mode
  82. return 0;
  83. }
  84. }