sm501_template.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Pixel drawing function templates for QEMU SM501 Device
  3. *
  4. * Copyright (c) 2008 Shin-ichiro KAWASAKI
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #if DEPTH == 8
  25. #define BPP 1
  26. #define PIXEL_TYPE uint8_t
  27. #elif DEPTH == 15 || DEPTH == 16
  28. #define BPP 2
  29. #define PIXEL_TYPE uint16_t
  30. #elif DEPTH == 32
  31. #define BPP 4
  32. #define PIXEL_TYPE uint32_t
  33. #else
  34. #error unsupport depth
  35. #endif
  36. #ifdef BGR_FORMAT
  37. #define PIXEL_NAME glue(DEPTH, bgr)
  38. #else
  39. #define PIXEL_NAME DEPTH
  40. #endif /* BGR_FORMAT */
  41. static void glue(draw_line8_, PIXEL_NAME)(
  42. uint8_t *d, const uint8_t *s, int width, const uint32_t *pal)
  43. {
  44. uint8_t v, r, g, b;
  45. do {
  46. v = ldub_p(s);
  47. r = (pal[v] >> 16) & 0xff;
  48. g = (pal[v] >> 8) & 0xff;
  49. b = (pal[v] >> 0) & 0xff;
  50. *(PIXEL_TYPE *)d = glue(rgb_to_pixel, PIXEL_NAME)(r, g, b);
  51. s++;
  52. d += BPP;
  53. } while (--width != 0);
  54. }
  55. static void glue(draw_line16_, PIXEL_NAME)(
  56. uint8_t *d, const uint8_t *s, int width, const uint32_t *pal)
  57. {
  58. uint16_t rgb565;
  59. uint8_t r, g, b;
  60. do {
  61. rgb565 = lduw_le_p(s);
  62. r = (rgb565 >> 8) & 0xf8;
  63. g = (rgb565 >> 3) & 0xfc;
  64. b = (rgb565 << 3) & 0xf8;
  65. *(PIXEL_TYPE *)d = glue(rgb_to_pixel, PIXEL_NAME)(r, g, b);
  66. s += 2;
  67. d += BPP;
  68. } while (--width != 0);
  69. }
  70. static void glue(draw_line32_, PIXEL_NAME)(
  71. uint8_t *d, const uint8_t *s, int width, const uint32_t *pal)
  72. {
  73. uint8_t r, g, b;
  74. do {
  75. r = s[2];
  76. g = s[1];
  77. b = s[0];
  78. *(PIXEL_TYPE *)d = glue(rgb_to_pixel, PIXEL_NAME)(r, g, b);
  79. s += 4;
  80. d += BPP;
  81. } while (--width != 0);
  82. }
  83. /**
  84. * Draw hardware cursor image on the given line.
  85. */
  86. static void glue(draw_hwc_line_, PIXEL_NAME)(uint8_t *d, const uint8_t *s,
  87. int width, const uint8_t *palette, int c_x, int c_y)
  88. {
  89. int i;
  90. uint8_t r, g, b, v, bitset = 0;
  91. /* get cursor position */
  92. assert(0 <= c_y && c_y < SM501_HWC_HEIGHT);
  93. s += SM501_HWC_WIDTH * c_y / 4; /* 4 pixels per byte */
  94. d += c_x * BPP;
  95. for (i = 0; i < SM501_HWC_WIDTH && c_x + i < width; i++) {
  96. /* get pixel value */
  97. if (i % 4 == 0) {
  98. bitset = ldub_p(s);
  99. s++;
  100. }
  101. v = bitset & 3;
  102. bitset >>= 2;
  103. /* write pixel */
  104. if (v) {
  105. v--;
  106. r = palette[v * 3 + 0];
  107. g = palette[v * 3 + 1];
  108. b = palette[v * 3 + 2];
  109. *(PIXEL_TYPE *)d = glue(rgb_to_pixel, PIXEL_NAME)(r, g, b);
  110. }
  111. d += BPP;
  112. }
  113. }
  114. #undef DEPTH
  115. #undef BPP
  116. #undef PIXEL_TYPE
  117. #undef PIXEL_NAME
  118. #undef BGR_FORMAT