cirrus.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Cirrus Logic GD5446 BitBLT acceleration.
  2. Reference: QEMU hw/display/cirrus_vga.c (register offsets, ROP codes). */
  3. #include "cirrus.h"
  4. #include "cursor_shape.h"
  5. #include "pci.h"
  6. #include "vga.h"
  7. /* Sequencer (0x3C4/0x3C5). */
  8. #define SR_UNLOCK 0x06
  9. #define SR_UNLOCK_VAL 0x12
  10. #define SR_CURSOR_X 0x10
  11. #define SR_CURSOR_Y 0x11
  12. #define SR_CURSOR_ATTR 0x12
  13. #define SR_CURSOR_PAT 0x13
  14. #define CURSOR_SHOW 0x01
  15. #define CURSOR_HIDDENPEL 0x02
  16. /* Graphics controller BitBLT registers (0x3CE/0x3CF). */
  17. #define GR_WIDTH_LO 0x20
  18. #define GR_WIDTH_HI 0x21
  19. #define GR_HEIGHT_LO 0x22
  20. #define GR_HEIGHT_HI 0x23
  21. #define GR_DSTPITCH_LO 0x24
  22. #define GR_DSTPITCH_HI 0x25
  23. #define GR_SRCPITCH_LO 0x26
  24. #define GR_SRCPITCH_HI 0x27
  25. #define GR_DSTADDR_LO 0x28
  26. #define GR_DSTADDR_MID 0x29
  27. #define GR_DSTADDR_HI 0x2a
  28. #define GR_SRCADDR_LO 0x2c
  29. #define GR_SRCADDR_MID 0x2d
  30. #define GR_SRCADDR_HI 0x2e
  31. #define GR_MODE 0x30
  32. #define GR_STATUS 0x31
  33. #define GR_ROP 0x32
  34. #define GR_MODEEXT 0x33
  35. #define BLT_BUSY 0x01
  36. #define BLT_START 0x02
  37. #define BLTMODE_BACKWARDS 0x01
  38. #define BLTMODE_PIXELWIDTH_MASK 0x30
  39. #define BLTMODE_PIXELWIDTH16 0x10
  40. #define BLTMODE_PIXELWIDTH24 0x20
  41. #define BLTMODE_PIXELWIDTH32 0x30
  42. #define BLTMODE_PATTERNCOPY 0x40
  43. #define BLTMODE_COLOREXPAND 0x80
  44. #define BLTMODEEXT_SOLIDFILL 0x04
  45. #define ROP_SRC 0x0d
  46. static int cirrus_present;
  47. static void gr_write(unsigned char idx, unsigned char val)
  48. {
  49. port_outb(0x3CE, idx);
  50. port_outb(0x3CF, val);
  51. }
  52. static unsigned char gr_read(unsigned char idx)
  53. {
  54. port_outb(0x3CE, idx);
  55. return port_inb(0x3CF);
  56. }
  57. static void sr_write(unsigned char idx, unsigned char val)
  58. {
  59. port_outb(0x3C4, idx);
  60. port_outb(0x3C5, val);
  61. }
  62. static unsigned char sr_read(unsigned char idx)
  63. {
  64. port_outb(0x3C4, idx);
  65. return port_inb(0x3C5);
  66. }
  67. void cirrus_init(void)
  68. {
  69. cirrus_present = 0;
  70. sr_write(SR_UNLOCK, SR_UNLOCK_VAL);
  71. if (sr_read(SR_UNLOCK) == SR_UNLOCK_VAL)
  72. cirrus_present = 1;
  73. }
  74. int cirrus_available(void)
  75. {
  76. return cirrus_present;
  77. }
  78. void cirrus_blit(unsigned int sx, unsigned int sy,
  79. unsigned int dx, unsigned int dy,
  80. unsigned int w, unsigned int h)
  81. {
  82. unsigned int bpp = vga_fb_bpp();
  83. unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
  84. unsigned int pitch = vga_fb_pitch();
  85. unsigned int src = sy * pitch + sx * pb;
  86. unsigned int dst = dy * pitch + dx * pb;
  87. unsigned int width_bytes = w * pb;
  88. unsigned int mode = 0;
  89. if (bpp == 16)
  90. mode |= BLTMODE_PIXELWIDTH16;
  91. else if (bpp == 24)
  92. mode |= BLTMODE_PIXELWIDTH24;
  93. else
  94. mode |= BLTMODE_PIXELWIDTH32;
  95. if (dst > src) {
  96. /* Overlapping, moving down/right: walk from the bottom-right so the
  97. source isn't clobbered. The engine runs backward with a positive
  98. pitch written; the address points at the last byte of the region. */
  99. mode |= BLTMODE_BACKWARDS;
  100. src += (h - 1) * pitch + (width_bytes - 1);
  101. dst += (h - 1) * pitch + (width_bytes - 1);
  102. }
  103. gr_write(GR_WIDTH_LO, (unsigned char)((width_bytes - 1) & 0xFF));
  104. gr_write(GR_WIDTH_HI, (unsigned char)(((width_bytes - 1) >> 8) & 0xFF));
  105. gr_write(GR_HEIGHT_LO, (unsigned char)((h - 1) & 0xFF));
  106. gr_write(GR_HEIGHT_HI, (unsigned char)(((h - 1) >> 8) & 0xFF));
  107. gr_write(GR_DSTPITCH_LO, (unsigned char)(pitch & 0xFF));
  108. gr_write(GR_DSTPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
  109. gr_write(GR_SRCPITCH_LO, (unsigned char)(pitch & 0xFF));
  110. gr_write(GR_SRCPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
  111. gr_write(GR_DSTADDR_LO, (unsigned char)(dst & 0xFF));
  112. gr_write(GR_DSTADDR_MID, (unsigned char)((dst >> 8) & 0xFF));
  113. gr_write(GR_DSTADDR_HI, (unsigned char)((dst >> 16) & 0xFF));
  114. gr_write(GR_SRCADDR_LO, (unsigned char)(src & 0xFF));
  115. gr_write(GR_SRCADDR_MID, (unsigned char)((src >> 8) & 0xFF));
  116. gr_write(GR_SRCADDR_HI, (unsigned char)((src >> 16) & 0xFF));
  117. gr_write(GR_MODE, (unsigned char)mode);
  118. gr_write(GR_ROP, ROP_SRC);
  119. gr_write(GR_MODEEXT, 0);
  120. gr_write(GR_STATUS, 0); /* ensure START is clear so the write triggers */
  121. gr_write(GR_STATUS, BLT_START);
  122. while (gr_read(GR_STATUS) & BLT_BUSY)
  123. ;
  124. }
  125. void cirrus_fill(unsigned int x, unsigned int y,
  126. unsigned int w, unsigned int h,
  127. unsigned int r, unsigned int g, unsigned int b)
  128. {
  129. unsigned int bpp = vga_fb_bpp();
  130. unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
  131. unsigned int pitch = vga_fb_pitch();
  132. unsigned int dst = y * pitch + x * pb;
  133. unsigned int width_bytes = w * pb;
  134. unsigned int mode = BLTMODE_PATTERNCOPY | BLTMODE_COLOREXPAND;
  135. /* Set the foreground color (the solid fill source). */
  136. if (bpp == 16) {
  137. unsigned short rgb565 = (unsigned short)
  138. (((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  139. mode |= BLTMODE_PIXELWIDTH16;
  140. gr_write(0x01, (unsigned char)(rgb565 & 0xFF));
  141. gr_write(0x11, (unsigned char)((rgb565 >> 8) & 0xFF));
  142. } else if (bpp == 24) {
  143. mode |= BLTMODE_PIXELWIDTH24;
  144. gr_write(0x01, (unsigned char)b);
  145. gr_write(0x11, (unsigned char)g);
  146. gr_write(0x13, (unsigned char)r);
  147. } else {
  148. mode |= BLTMODE_PIXELWIDTH32;
  149. gr_write(0x01, (unsigned char)b);
  150. gr_write(0x11, (unsigned char)g);
  151. gr_write(0x13, (unsigned char)r);
  152. gr_write(0x15, 0);
  153. }
  154. gr_write(GR_WIDTH_LO, (unsigned char)((width_bytes - 1) & 0xFF));
  155. gr_write(GR_WIDTH_HI, (unsigned char)(((width_bytes - 1) >> 8) & 0xFF));
  156. gr_write(GR_HEIGHT_LO, (unsigned char)((h - 1) & 0xFF));
  157. gr_write(GR_HEIGHT_HI, (unsigned char)(((h - 1) >> 8) & 0xFF));
  158. gr_write(GR_DSTPITCH_LO, (unsigned char)(pitch & 0xFF));
  159. gr_write(GR_DSTPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
  160. gr_write(GR_DSTADDR_LO, (unsigned char)(dst & 0xFF));
  161. gr_write(GR_DSTADDR_MID, (unsigned char)((dst >> 8) & 0xFF));
  162. gr_write(GR_DSTADDR_HI, (unsigned char)((dst >> 16) & 0xFF));
  163. gr_write(GR_MODE, (unsigned char)mode);
  164. gr_write(GR_ROP, ROP_SRC);
  165. gr_write(GR_MODEEXT, BLTMODEEXT_SOLIDFILL);
  166. gr_write(GR_STATUS, 0); /* ensure START is clear so the write triggers */
  167. gr_write(GR_STATUS, BLT_START);
  168. while (gr_read(GR_STATUS) & BLT_BUSY)
  169. ;
  170. }
  171. void cirrus_cursor_setup(void)
  172. {
  173. unsigned char bitmap[256];
  174. unsigned int r, i;
  175. unsigned int vram = vga_fb_vram_kb() * 1024;
  176. unsigned char *dst;
  177. /* 32x32 two-plane cursor: AND plane (offset 0..127), XOR plane (128..255),
  178. 4 bytes per row. The shared arrow is placed in the top-left of the
  179. box (the cursor position is the top-left of the 32x32 box):
  180. AND=0 XOR=0 -> transparent
  181. AND=0 XOR=1 -> color0 (white fill)
  182. AND=1 XOR=1 -> color1 (black outline)
  183. The hidden DAC entries 0 and 15 are programmed white and black. */
  184. for (i = 0; i < 256; i++)
  185. bitmap[i] = 0;
  186. for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++) {
  187. unsigned short fill = cursor_shape_fill[r];
  188. unsigned short outl = cursor_shape_outline[r];
  189. unsigned short xor = fill | outl;
  190. bitmap[r * 4 + 0] = (unsigned char)((outl >> 8) & 0xFF); /* AND x=0..7 */
  191. bitmap[r * 4 + 1] = (unsigned char)(outl & 0xFF); /* AND x=8..15 */
  192. bitmap[128 + r * 4 + 0] = (unsigned char)((xor >> 8) & 0xFF); /* XOR x=0..7 */
  193. bitmap[128 + r * 4 + 1] = (unsigned char)(xor & 0xFF); /* XOR x=8..15 */
  194. }
  195. /* Upload to the top of VRAM (SR[0x13] = 0 selects the first 256-byte block). */
  196. sr_write(SR_CURSOR_PAT, 0);
  197. dst = (unsigned char *)(vga_fb_lfb_addr() + vram - 16384);
  198. for (i = 0; i < 256; i++)
  199. dst[i] = bitmap[i];
  200. /* Cursor colors via the hidden DAC palette: entry 0 = white, 15 = black. */
  201. sr_write(SR_CURSOR_ATTR, CURSOR_HIDDENPEL);
  202. port_outb(0x3C8, 0);
  203. port_outb(0x3C9, 63); port_outb(0x3C9, 63); port_outb(0x3C9, 63);
  204. port_outb(0x3C8, 15);
  205. port_outb(0x3C9, 0); port_outb(0x3C9, 0); port_outb(0x3C9, 0);
  206. sr_write(SR_CURSOR_ATTR, CURSOR_SHOW);
  207. }
  208. void cirrus_cursor_move(unsigned int x, unsigned int y)
  209. {
  210. /* Position is 11 bits; low 3 bits select the index alias, high 8 are data. */
  211. sr_write(SR_CURSOR_X | ((x & 0x7) << 5), (unsigned char)(x >> 3));
  212. sr_write(SR_CURSOR_Y | ((y & 0x7) << 5), (unsigned char)(y >> 3));
  213. }
  214. void cirrus_cursor_show(int show)
  215. {
  216. unsigned char v = sr_read(SR_CURSOR_ATTR);
  217. if (show)
  218. v |= CURSOR_SHOW;
  219. else
  220. v &= ~CURSOR_SHOW;
  221. sr_write(SR_CURSOR_ATTR, v);
  222. }