g364fb.c 16 KB

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