g364fb.c 16 KB

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