cirrus.c 7.8 KB

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