g364fb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * QEMU G364 framebuffer Emulator.
  3. *
  4. * Copyright (c) 2007-2011 Herve Poussineau
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "hw/hw.h"
  20. #include "ui/console.h"
  21. #include "ui/pixel_ops.h"
  22. #include "trace.h"
  23. #include "hw/sysbus.h"
  24. typedef struct G364State {
  25. /* hardware */
  26. uint8_t *vram;
  27. uint32_t vram_size;
  28. qemu_irq irq;
  29. MemoryRegion mem_vram;
  30. MemoryRegion mem_ctrl;
  31. /* registers */
  32. uint8_t color_palette[256][3];
  33. uint8_t cursor_palette[3][3];
  34. uint16_t cursor[512];
  35. uint32_t cursor_position;
  36. uint32_t ctla;
  37. uint32_t top_of_screen;
  38. uint32_t width, height; /* in pixels */
  39. /* display refresh support */
  40. QemuConsole *con;
  41. int depth;
  42. int blanked;
  43. } G364State;
  44. #define REG_BOOT 0x000000
  45. #define REG_DISPLAY 0x000118
  46. #define REG_VDISPLAY 0x000150
  47. #define REG_CTLA 0x000300
  48. #define REG_TOP 0x000400
  49. #define REG_CURS_PAL 0x000508
  50. #define REG_CURS_POS 0x000638
  51. #define REG_CLR_PAL 0x000800
  52. #define REG_CURS_PAT 0x001000
  53. #define REG_RESET 0x100000
  54. #define CTLA_FORCE_BLANK 0x00000400
  55. #define CTLA_NO_CURSOR 0x00800000
  56. #define G364_PAGE_SIZE 4096
  57. static inline int check_dirty(G364State *s, ram_addr_t page)
  58. {
  59. return memory_region_get_dirty(&s->mem_vram, page, G364_PAGE_SIZE,
  60. DIRTY_MEMORY_VGA);
  61. }
  62. static inline void reset_dirty(G364State *s,
  63. ram_addr_t page_min, ram_addr_t page_max)
  64. {
  65. memory_region_reset_dirty(&s->mem_vram,
  66. page_min,
  67. page_max + G364_PAGE_SIZE - page_min - 1,
  68. DIRTY_MEMORY_VGA);
  69. }
  70. static void g364fb_draw_graphic8(G364State *s)
  71. {
  72. DisplaySurface *surface = qemu_console_surface(s->con);
  73. int i, w;
  74. uint8_t *vram;
  75. uint8_t *data_display, *dd;
  76. ram_addr_t page, page_min, page_max;
  77. int x, y;
  78. int xmin, xmax;
  79. int ymin, ymax;
  80. int xcursor, ycursor;
  81. unsigned int (*rgb_to_pixel)(unsigned int r, unsigned int g, unsigned int b);
  82. switch (surface_bits_per_pixel(surface)) {
  83. case 8:
  84. rgb_to_pixel = rgb_to_pixel8;
  85. w = 1;
  86. break;
  87. case 15:
  88. rgb_to_pixel = rgb_to_pixel15;
  89. w = 2;
  90. break;
  91. case 16:
  92. rgb_to_pixel = rgb_to_pixel16;
  93. w = 2;
  94. break;
  95. case 32:
  96. rgb_to_pixel = rgb_to_pixel32;
  97. w = 4;
  98. break;
  99. default:
  100. hw_error("g364: unknown host depth %d",
  101. surface_bits_per_pixel(surface));
  102. return;
  103. }
  104. page = 0;
  105. page_min = (ram_addr_t)-1;
  106. page_max = 0;
  107. x = y = 0;
  108. xmin = s->width;
  109. xmax = 0;
  110. ymin = s->height;
  111. ymax = 0;
  112. if (!(s->ctla & CTLA_NO_CURSOR)) {
  113. xcursor = s->cursor_position >> 12;
  114. ycursor = s->cursor_position & 0xfff;
  115. } else {
  116. xcursor = ycursor = -65;
  117. }
  118. vram = s->vram + s->top_of_screen;
  119. /* XXX: out of range in vram? */
  120. data_display = dd = surface_data(surface);
  121. while (y < s->height) {
  122. if (check_dirty(s, page)) {
  123. if (y < ymin)
  124. ymin = ymax = y;
  125. if (page_min == (ram_addr_t)-1)
  126. page_min = page;
  127. page_max = page;
  128. if (x < xmin)
  129. xmin = x;
  130. for (i = 0; i < G364_PAGE_SIZE; i++) {
  131. uint8_t index;
  132. unsigned int color;
  133. if (unlikely((y >= ycursor && y < ycursor + 64) &&
  134. (x >= xcursor && x < xcursor + 64))) {
  135. /* pointer area */
  136. int xdiff = x - xcursor;
  137. uint16_t curs = s->cursor[(y - ycursor) * 8 + xdiff / 8];
  138. int op = (curs >> ((xdiff & 7) * 2)) & 3;
  139. if (likely(op == 0)) {
  140. /* transparent */
  141. index = *vram;
  142. color = (*rgb_to_pixel)(
  143. s->color_palette[index][0],
  144. s->color_palette[index][1],
  145. s->color_palette[index][2]);
  146. } else {
  147. /* get cursor color */
  148. index = op - 1;
  149. color = (*rgb_to_pixel)(
  150. s->cursor_palette[index][0],
  151. s->cursor_palette[index][1],
  152. s->cursor_palette[index][2]);
  153. }
  154. } else {
  155. /* normal area */
  156. index = *vram;
  157. color = (*rgb_to_pixel)(
  158. s->color_palette[index][0],
  159. s->color_palette[index][1],
  160. s->color_palette[index][2]);
  161. }
  162. memcpy(dd, &color, w);
  163. dd += w;
  164. x++;
  165. vram++;
  166. if (x == s->width) {
  167. xmax = s->width - 1;
  168. y++;
  169. if (y == s->height) {
  170. ymax = s->height - 1;
  171. goto done;
  172. }
  173. data_display = dd = data_display + surface_stride(surface);
  174. xmin = 0;
  175. x = 0;
  176. }
  177. }
  178. if (x > xmax)
  179. xmax = x;
  180. if (y > ymax)
  181. ymax = y;
  182. } else {
  183. int dy;
  184. if (page_min != (ram_addr_t)-1) {
  185. reset_dirty(s, page_min, page_max);
  186. page_min = (ram_addr_t)-1;
  187. page_max = 0;
  188. dpy_gfx_update(s->con, xmin, ymin,
  189. xmax - xmin + 1, ymax - ymin + 1);
  190. xmin = s->width;
  191. xmax = 0;
  192. ymin = s->height;
  193. ymax = 0;
  194. }
  195. x += G364_PAGE_SIZE;
  196. dy = x / s->width;
  197. x = x % s->width;
  198. y += dy;
  199. vram += G364_PAGE_SIZE;
  200. data_display += dy * surface_stride(surface);
  201. dd = data_display + x * w;
  202. }
  203. page += G364_PAGE_SIZE;
  204. }
  205. done:
  206. if (page_min != (ram_addr_t)-1) {
  207. dpy_gfx_update(s->con, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
  208. reset_dirty(s, page_min, page_max);
  209. }
  210. }
  211. static void g364fb_draw_blank(G364State *s)
  212. {
  213. DisplaySurface *surface = qemu_console_surface(s->con);
  214. int i, w;
  215. uint8_t *d;
  216. if (s->blanked) {
  217. /* Screen is already blank. No need to redraw it */
  218. return;
  219. }
  220. w = s->width * surface_bytes_per_pixel(surface);
  221. d = surface_data(surface);
  222. for (i = 0; i < s->height; i++) {
  223. memset(d, 0, w);
  224. d += surface_stride(surface);
  225. }
  226. dpy_gfx_update(s->con, 0, 0, s->width, s->height);
  227. s->blanked = 1;
  228. }
  229. static void g364fb_update_display(void *opaque)
  230. {
  231. G364State *s = opaque;
  232. DisplaySurface *surface = qemu_console_surface(s->con);
  233. qemu_flush_coalesced_mmio_buffer();
  234. if (s->width == 0 || s->height == 0)
  235. return;
  236. if (s->width != surface_width(surface) ||
  237. s->height != surface_height(surface)) {
  238. qemu_console_resize(s->con, s->width, s->height);
  239. }
  240. if (s->ctla & CTLA_FORCE_BLANK) {
  241. g364fb_draw_blank(s);
  242. } else if (s->depth == 8) {
  243. g364fb_draw_graphic8(s);
  244. } else {
  245. error_report("g364: unknown guest depth %d", s->depth);
  246. }
  247. qemu_irq_raise(s->irq);
  248. }
  249. static inline void g364fb_invalidate_display(void *opaque)
  250. {
  251. G364State *s = opaque;
  252. s->blanked = 0;
  253. memory_region_set_dirty(&s->mem_vram, 0, s->vram_size);
  254. }
  255. static void g364fb_reset(G364State *s)
  256. {
  257. qemu_irq_lower(s->irq);
  258. memset(s->color_palette, 0, sizeof(s->color_palette));
  259. memset(s->cursor_palette, 0, sizeof(s->cursor_palette));
  260. memset(s->cursor, 0, sizeof(s->cursor));
  261. s->cursor_position = 0;
  262. s->ctla = 0;
  263. s->top_of_screen = 0;
  264. s->width = s->height = 0;
  265. memset(s->vram, 0, s->vram_size);
  266. g364fb_invalidate_display(s);
  267. }
  268. /* called for accesses to io ports */
  269. static uint64_t g364fb_ctrl_read(void *opaque,
  270. hwaddr addr,
  271. unsigned int size)
  272. {
  273. G364State *s = opaque;
  274. uint32_t val;
  275. if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
  276. /* cursor pattern */
  277. int idx = (addr - REG_CURS_PAT) >> 3;
  278. val = s->cursor[idx];
  279. } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
  280. /* cursor palette */
  281. int idx = (addr - REG_CURS_PAL) >> 3;
  282. val = ((uint32_t)s->cursor_palette[idx][0] << 16);
  283. val |= ((uint32_t)s->cursor_palette[idx][1] << 8);
  284. val |= ((uint32_t)s->cursor_palette[idx][2] << 0);
  285. } else {
  286. switch (addr) {
  287. case REG_DISPLAY:
  288. val = s->width / 4;
  289. break;
  290. case REG_VDISPLAY:
  291. val = s->height * 2;
  292. break;
  293. case REG_CTLA:
  294. val = s->ctla;
  295. break;
  296. default:
  297. {
  298. error_report("g364: invalid read at [" TARGET_FMT_plx "]",
  299. addr);
  300. val = 0;
  301. break;
  302. }
  303. }
  304. }
  305. trace_g364fb_read(addr, val);
  306. return val;
  307. }
  308. static void g364fb_update_depth(G364State *s)
  309. {
  310. static const int depths[8] = { 1, 2, 4, 8, 15, 16, 0 };
  311. s->depth = depths[(s->ctla & 0x00700000) >> 20];
  312. }
  313. static void g364_invalidate_cursor_position(G364State *s)
  314. {
  315. DisplaySurface *surface = qemu_console_surface(s->con);
  316. int ymin, ymax, start, end;
  317. /* invalidate only near the cursor */
  318. ymin = s->cursor_position & 0xfff;
  319. ymax = MIN(s->height, ymin + 64);
  320. start = ymin * surface_stride(surface);
  321. end = (ymax + 1) * surface_stride(surface);
  322. memory_region_set_dirty(&s->mem_vram, start, end - start);
  323. }
  324. static void g364fb_ctrl_write(void *opaque,
  325. hwaddr addr,
  326. uint64_t val,
  327. unsigned int size)
  328. {
  329. G364State *s = opaque;
  330. trace_g364fb_write(addr, val);
  331. if (addr >= REG_CLR_PAL && addr < REG_CLR_PAL + 0x800) {
  332. /* color palette */
  333. int idx = (addr - REG_CLR_PAL) >> 3;
  334. s->color_palette[idx][0] = (val >> 16) & 0xff;
  335. s->color_palette[idx][1] = (val >> 8) & 0xff;
  336. s->color_palette[idx][2] = val & 0xff;
  337. g364fb_invalidate_display(s);
  338. } else if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
  339. /* cursor pattern */
  340. int idx = (addr - REG_CURS_PAT) >> 3;
  341. s->cursor[idx] = val;
  342. g364fb_invalidate_display(s);
  343. } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
  344. /* cursor palette */
  345. int idx = (addr - REG_CURS_PAL) >> 3;
  346. s->cursor_palette[idx][0] = (val >> 16) & 0xff;
  347. s->cursor_palette[idx][1] = (val >> 8) & 0xff;
  348. s->cursor_palette[idx][2] = val & 0xff;
  349. g364fb_invalidate_display(s);
  350. } else {
  351. switch (addr) {
  352. case REG_BOOT: /* Boot timing */
  353. case 0x00108: /* Line timing: half sync */
  354. case 0x00110: /* Line timing: back porch */
  355. case 0x00120: /* Line timing: short display */
  356. case 0x00128: /* Frame timing: broad pulse */
  357. case 0x00130: /* Frame timing: v sync */
  358. case 0x00138: /* Frame timing: v preequalise */
  359. case 0x00140: /* Frame timing: v postequalise */
  360. case 0x00148: /* Frame timing: v blank */
  361. case 0x00158: /* Line timing: line time */
  362. case 0x00160: /* Frame store: line start */
  363. case 0x00168: /* vram cycle: mem init */
  364. case 0x00170: /* vram cycle: transfer delay */
  365. case 0x00200: /* vram cycle: mask register */
  366. /* ignore */
  367. break;
  368. case REG_TOP:
  369. s->top_of_screen = val;
  370. g364fb_invalidate_display(s);
  371. break;
  372. case REG_DISPLAY:
  373. s->width = val * 4;
  374. break;
  375. case REG_VDISPLAY:
  376. s->height = val / 2;
  377. break;
  378. case REG_CTLA:
  379. s->ctla = val;
  380. g364fb_update_depth(s);
  381. g364fb_invalidate_display(s);
  382. break;
  383. case REG_CURS_POS:
  384. g364_invalidate_cursor_position(s);
  385. s->cursor_position = val;
  386. g364_invalidate_cursor_position(s);
  387. break;
  388. case REG_RESET:
  389. g364fb_reset(s);
  390. break;
  391. default:
  392. error_report("g364: invalid write of 0x%" PRIx64
  393. " at [" TARGET_FMT_plx "]", val, addr);
  394. break;
  395. }
  396. }
  397. qemu_irq_lower(s->irq);
  398. }
  399. static const MemoryRegionOps g364fb_ctrl_ops = {
  400. .read = g364fb_ctrl_read,
  401. .write = g364fb_ctrl_write,
  402. .endianness = DEVICE_LITTLE_ENDIAN,
  403. .impl.min_access_size = 4,
  404. .impl.max_access_size = 4,
  405. };
  406. static int g364fb_post_load(void *opaque, int version_id)
  407. {
  408. G364State *s = opaque;
  409. /* force refresh */
  410. g364fb_update_depth(s);
  411. g364fb_invalidate_display(s);
  412. return 0;
  413. }
  414. static const VMStateDescription vmstate_g364fb = {
  415. .name = "g364fb",
  416. .version_id = 1,
  417. .minimum_version_id = 1,
  418. .minimum_version_id_old = 1,
  419. .post_load = g364fb_post_load,
  420. .fields = (VMStateField[]) {
  421. VMSTATE_VBUFFER_UINT32(vram, G364State, 1, NULL, 0, vram_size),
  422. VMSTATE_BUFFER_UNSAFE(color_palette, G364State, 0, 256 * 3),
  423. VMSTATE_BUFFER_UNSAFE(cursor_palette, G364State, 0, 9),
  424. VMSTATE_UINT16_ARRAY(cursor, G364State, 512),
  425. VMSTATE_UINT32(cursor_position, G364State),
  426. VMSTATE_UINT32(ctla, G364State),
  427. VMSTATE_UINT32(top_of_screen, G364State),
  428. VMSTATE_UINT32(width, G364State),
  429. VMSTATE_UINT32(height, G364State),
  430. VMSTATE_END_OF_LIST()
  431. }
  432. };
  433. static const GraphicHwOps g364fb_ops = {
  434. .invalidate = g364fb_invalidate_display,
  435. .gfx_update = g364fb_update_display,
  436. };
  437. static void g364fb_init(DeviceState *dev, G364State *s)
  438. {
  439. s->vram = g_malloc0(s->vram_size);
  440. s->con = graphic_console_init(dev, 0, &g364fb_ops, s);
  441. memory_region_init_io(&s->mem_ctrl, NULL, &g364fb_ctrl_ops, s, "ctrl", 0x180000);
  442. memory_region_init_ram_ptr(&s->mem_vram, NULL, "vram",
  443. s->vram_size, s->vram);
  444. vmstate_register_ram(&s->mem_vram, dev);
  445. memory_region_set_coalescing(&s->mem_vram);
  446. }
  447. #define TYPE_G364 "sysbus-g364"
  448. #define G364(obj) OBJECT_CHECK(G364SysBusState, (obj), TYPE_G364)
  449. typedef struct {
  450. SysBusDevice parent_obj;
  451. G364State g364;
  452. } G364SysBusState;
  453. static int g364fb_sysbus_init(SysBusDevice *sbd)
  454. {
  455. DeviceState *dev = DEVICE(sbd);
  456. G364SysBusState *sbs = G364(dev);
  457. G364State *s = &sbs->g364;
  458. g364fb_init(dev, s);
  459. sysbus_init_irq(sbd, &s->irq);
  460. sysbus_init_mmio(sbd, &s->mem_ctrl);
  461. sysbus_init_mmio(sbd, &s->mem_vram);
  462. return 0;
  463. }
  464. static void g364fb_sysbus_reset(DeviceState *d)
  465. {
  466. G364SysBusState *s = G364(d);
  467. g364fb_reset(&s->g364);
  468. }
  469. static Property g364fb_sysbus_properties[] = {
  470. DEFINE_PROP_UINT32("vram_size", G364SysBusState, g364.vram_size,
  471. 8 * 1024 * 1024),
  472. DEFINE_PROP_END_OF_LIST(),
  473. };
  474. static void g364fb_sysbus_class_init(ObjectClass *klass, void *data)
  475. {
  476. DeviceClass *dc = DEVICE_CLASS(klass);
  477. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  478. k->init = g364fb_sysbus_init;
  479. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  480. dc->desc = "G364 framebuffer";
  481. dc->reset = g364fb_sysbus_reset;
  482. dc->vmsd = &vmstate_g364fb;
  483. dc->props = g364fb_sysbus_properties;
  484. }
  485. static const TypeInfo g364fb_sysbus_info = {
  486. .name = TYPE_G364,
  487. .parent = TYPE_SYS_BUS_DEVICE,
  488. .instance_size = sizeof(G364SysBusState),
  489. .class_init = g364fb_sysbus_class_init,
  490. };
  491. static void g364fb_register_types(void)
  492. {
  493. type_register_static(&g364fb_sysbus_info);
  494. }
  495. type_init(g364fb_register_types)