g364fb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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.h"
  20. #include "console.h"
  21. #include "pixel_ops.h"
  22. #include "trace.h"
  23. #include "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. DisplayState *ds;
  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. int i, w;
  73. uint8_t *vram;
  74. uint8_t *data_display, *dd;
  75. ram_addr_t page, page_min, page_max;
  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 (ds_get_bits_per_pixel(s->ds)) {
  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. ds_get_bits_per_pixel(s->ds));
  101. return;
  102. }
  103. page = 0;
  104. page_min = (ram_addr_t)-1;
  105. page_max = 0;
  106. x = y = 0;
  107. xmin = s->width;
  108. xmax = 0;
  109. ymin = s->height;
  110. ymax = 0;
  111. if (!(s->ctla & CTLA_NO_CURSOR)) {
  112. xcursor = s->cursor_position >> 12;
  113. ycursor = s->cursor_position & 0xfff;
  114. } else {
  115. xcursor = ycursor = -65;
  116. }
  117. vram = s->vram + s->top_of_screen;
  118. /* XXX: out of range in vram? */
  119. data_display = dd = ds_get_data(s->ds);
  120. while (y < s->height) {
  121. if (check_dirty(s, page)) {
  122. if (y < ymin)
  123. ymin = ymax = y;
  124. if (page_min == (ram_addr_t)-1)
  125. page_min = page;
  126. page_max = page;
  127. if (x < xmin)
  128. xmin = x;
  129. for (i = 0; i < G364_PAGE_SIZE; i++) {
  130. uint8_t index;
  131. unsigned int color;
  132. if (unlikely((y >= ycursor && y < ycursor + 64) &&
  133. (x >= xcursor && x < xcursor + 64))) {
  134. /* pointer area */
  135. int xdiff = x - xcursor;
  136. uint16_t curs = s->cursor[(y - ycursor) * 8 + xdiff / 8];
  137. int op = (curs >> ((xdiff & 7) * 2)) & 3;
  138. if (likely(op == 0)) {
  139. /* transparent */
  140. index = *vram;
  141. color = (*rgb_to_pixel)(
  142. s->color_palette[index][0],
  143. s->color_palette[index][1],
  144. s->color_palette[index][2]);
  145. } else {
  146. /* get cursor color */
  147. index = op - 1;
  148. color = (*rgb_to_pixel)(
  149. s->cursor_palette[index][0],
  150. s->cursor_palette[index][1],
  151. s->cursor_palette[index][2]);
  152. }
  153. } else {
  154. /* normal area */
  155. index = *vram;
  156. color = (*rgb_to_pixel)(
  157. s->color_palette[index][0],
  158. s->color_palette[index][1],
  159. s->color_palette[index][2]);
  160. }
  161. memcpy(dd, &color, w);
  162. dd += w;
  163. x++;
  164. vram++;
  165. if (x == s->width) {
  166. xmax = s->width - 1;
  167. y++;
  168. if (y == s->height) {
  169. ymax = s->height - 1;
  170. goto done;
  171. }
  172. data_display = dd = data_display + ds_get_linesize(s->ds);
  173. xmin = 0;
  174. x = 0;
  175. }
  176. }
  177. if (x > xmax)
  178. xmax = x;
  179. if (y > ymax)
  180. ymax = y;
  181. } else {
  182. int dy;
  183. if (page_min != (ram_addr_t)-1) {
  184. reset_dirty(s, page_min, page_max);
  185. page_min = (ram_addr_t)-1;
  186. page_max = 0;
  187. dpy_update(s->ds, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
  188. xmin = s->width;
  189. xmax = 0;
  190. ymin = s->height;
  191. ymax = 0;
  192. }
  193. x += G364_PAGE_SIZE;
  194. dy = x / s->width;
  195. x = x % s->width;
  196. y += dy;
  197. vram += G364_PAGE_SIZE;
  198. data_display += dy * ds_get_linesize(s->ds);
  199. dd = data_display + x * w;
  200. }
  201. page += G364_PAGE_SIZE;
  202. }
  203. done:
  204. if (page_min != (ram_addr_t)-1) {
  205. dpy_update(s->ds, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
  206. reset_dirty(s, page_min, page_max);
  207. }
  208. }
  209. static void g364fb_draw_blank(G364State *s)
  210. {
  211. int i, w;
  212. uint8_t *d;
  213. if (s->blanked) {
  214. /* Screen is already blank. No need to redraw it */
  215. return;
  216. }
  217. w = s->width * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
  218. d = ds_get_data(s->ds);
  219. for (i = 0; i < s->height; i++) {
  220. memset(d, 0, w);
  221. d += ds_get_linesize(s->ds);
  222. }
  223. dpy_update(s->ds, 0, 0, s->width, s->height);
  224. s->blanked = 1;
  225. }
  226. static void g364fb_update_display(void *opaque)
  227. {
  228. G364State *s = opaque;
  229. qemu_flush_coalesced_mmio_buffer();
  230. if (s->width == 0 || s->height == 0)
  231. return;
  232. if (s->width != ds_get_width(s->ds) || s->height != ds_get_height(s->ds)) {
  233. qemu_console_resize(s->ds, s->width, s->height);
  234. }
  235. if (s->ctla & CTLA_FORCE_BLANK) {
  236. g364fb_draw_blank(s);
  237. } else if (s->depth == 8) {
  238. g364fb_draw_graphic8(s);
  239. } else {
  240. error_report("g364: unknown guest depth %d", s->depth);
  241. }
  242. qemu_irq_raise(s->irq);
  243. }
  244. static inline void g364fb_invalidate_display(void *opaque)
  245. {
  246. G364State *s = opaque;
  247. s->blanked = 0;
  248. memory_region_set_dirty(&s->mem_vram, 0, s->vram_size);
  249. }
  250. static void g364fb_reset(G364State *s)
  251. {
  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(s->vram, 0, s->vram_size);
  261. g364fb_invalidate_display(s);
  262. }
  263. static void g364fb_screen_dump(void *opaque, const char *filename, bool cswitch)
  264. {
  265. G364State *s = opaque;
  266. int y, x;
  267. uint8_t index;
  268. uint8_t *data_buffer;
  269. FILE *f;
  270. qemu_flush_coalesced_mmio_buffer();
  271. if (s->depth != 8) {
  272. error_report("g364: unknown guest depth %d", s->depth);
  273. return;
  274. }
  275. f = fopen(filename, "wb");
  276. if (!f)
  277. return;
  278. if (s->ctla & CTLA_FORCE_BLANK) {
  279. /* blank screen */
  280. fprintf(f, "P4\n%d %d\n",
  281. s->width, s->height);
  282. for (y = 0; y < s->height; y++)
  283. for (x = 0; x < s->width; x++)
  284. fputc(0, f);
  285. } else {
  286. data_buffer = s->vram + s->top_of_screen;
  287. fprintf(f, "P6\n%d %d\n%d\n",
  288. s->width, s->height, 255);
  289. for (y = 0; y < s->height; y++)
  290. for (x = 0; x < s->width; x++, data_buffer++) {
  291. index = *data_buffer;
  292. fputc(s->color_palette[index][0], f);
  293. fputc(s->color_palette[index][1], f);
  294. fputc(s->color_palette[index][2], f);
  295. }
  296. }
  297. fclose(f);
  298. }
  299. /* called for accesses to io ports */
  300. static uint64_t g364fb_ctrl_read(void *opaque,
  301. target_phys_addr_t addr,
  302. unsigned int size)
  303. {
  304. G364State *s = opaque;
  305. uint32_t val;
  306. if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
  307. /* cursor pattern */
  308. int idx = (addr - REG_CURS_PAT) >> 3;
  309. val = s->cursor[idx];
  310. } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
  311. /* cursor palette */
  312. int idx = (addr - REG_CURS_PAL) >> 3;
  313. val = ((uint32_t)s->cursor_palette[idx][0] << 16);
  314. val |= ((uint32_t)s->cursor_palette[idx][1] << 8);
  315. val |= ((uint32_t)s->cursor_palette[idx][2] << 0);
  316. } else {
  317. switch (addr) {
  318. case REG_DISPLAY:
  319. val = s->width / 4;
  320. break;
  321. case REG_VDISPLAY:
  322. val = s->height * 2;
  323. break;
  324. case REG_CTLA:
  325. val = s->ctla;
  326. break;
  327. default:
  328. {
  329. error_report("g364: invalid read at [" TARGET_FMT_plx "]",
  330. addr);
  331. val = 0;
  332. break;
  333. }
  334. }
  335. }
  336. trace_g364fb_read(addr, val);
  337. return val;
  338. }
  339. static void g364fb_update_depth(G364State *s)
  340. {
  341. static const int depths[8] = { 1, 2, 4, 8, 15, 16, 0 };
  342. s->depth = depths[(s->ctla & 0x00700000) >> 20];
  343. }
  344. static void g364_invalidate_cursor_position(G364State *s)
  345. {
  346. int ymin, ymax, start, end;
  347. /* invalidate only near the cursor */
  348. ymin = s->cursor_position & 0xfff;
  349. ymax = MIN(s->height, ymin + 64);
  350. start = ymin * ds_get_linesize(s->ds);
  351. end = (ymax + 1) * ds_get_linesize(s->ds);
  352. memory_region_set_dirty(&s->mem_vram, start, end - start);
  353. }
  354. static void g364fb_ctrl_write(void *opaque,
  355. target_phys_addr_t addr,
  356. uint64_t val,
  357. unsigned int size)
  358. {
  359. G364State *s = opaque;
  360. trace_g364fb_write(addr, val);
  361. if (addr >= REG_CLR_PAL && addr < REG_CLR_PAL + 0x800) {
  362. /* color palette */
  363. int idx = (addr - REG_CLR_PAL) >> 3;
  364. s->color_palette[idx][0] = (val >> 16) & 0xff;
  365. s->color_palette[idx][1] = (val >> 8) & 0xff;
  366. s->color_palette[idx][2] = val & 0xff;
  367. g364fb_invalidate_display(s);
  368. } else if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
  369. /* cursor pattern */
  370. int idx = (addr - REG_CURS_PAT) >> 3;
  371. s->cursor[idx] = val;
  372. g364fb_invalidate_display(s);
  373. } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
  374. /* cursor palette */
  375. int idx = (addr - REG_CURS_PAL) >> 3;
  376. s->cursor_palette[idx][0] = (val >> 16) & 0xff;
  377. s->cursor_palette[idx][1] = (val >> 8) & 0xff;
  378. s->cursor_palette[idx][2] = val & 0xff;
  379. g364fb_invalidate_display(s);
  380. } else {
  381. switch (addr) {
  382. case REG_BOOT: /* Boot timing */
  383. case 0x00108: /* Line timing: half sync */
  384. case 0x00110: /* Line timing: back porch */
  385. case 0x00120: /* Line timing: short display */
  386. case 0x00128: /* Frame timing: broad pulse */
  387. case 0x00130: /* Frame timing: v sync */
  388. case 0x00138: /* Frame timing: v preequalise */
  389. case 0x00140: /* Frame timing: v postequalise */
  390. case 0x00148: /* Frame timing: v blank */
  391. case 0x00158: /* Line timing: line time */
  392. case 0x00160: /* Frame store: line start */
  393. case 0x00168: /* vram cycle: mem init */
  394. case 0x00170: /* vram cycle: transfer delay */
  395. case 0x00200: /* vram cycle: mask register */
  396. /* ignore */
  397. break;
  398. case REG_TOP:
  399. s->top_of_screen = val;
  400. g364fb_invalidate_display(s);
  401. break;
  402. case REG_DISPLAY:
  403. s->width = val * 4;
  404. break;
  405. case REG_VDISPLAY:
  406. s->height = val / 2;
  407. break;
  408. case REG_CTLA:
  409. s->ctla = val;
  410. g364fb_update_depth(s);
  411. g364fb_invalidate_display(s);
  412. break;
  413. case REG_CURS_POS:
  414. g364_invalidate_cursor_position(s);
  415. s->cursor_position = val;
  416. g364_invalidate_cursor_position(s);
  417. break;
  418. case REG_RESET:
  419. g364fb_reset(s);
  420. break;
  421. default:
  422. error_report("g364: invalid write of 0x%" PRIx64
  423. " at [" TARGET_FMT_plx "]", val, addr);
  424. break;
  425. }
  426. }
  427. qemu_irq_lower(s->irq);
  428. }
  429. static const MemoryRegionOps g364fb_ctrl_ops = {
  430. .read = g364fb_ctrl_read,
  431. .write = g364fb_ctrl_write,
  432. .endianness = DEVICE_LITTLE_ENDIAN,
  433. .impl.min_access_size = 4,
  434. .impl.max_access_size = 4,
  435. };
  436. static int g364fb_post_load(void *opaque, int version_id)
  437. {
  438. G364State *s = opaque;
  439. /* force refresh */
  440. g364fb_update_depth(s);
  441. g364fb_invalidate_display(s);
  442. return 0;
  443. }
  444. static const VMStateDescription vmstate_g364fb = {
  445. .name = "g364fb",
  446. .version_id = 1,
  447. .minimum_version_id = 1,
  448. .minimum_version_id_old = 1,
  449. .post_load = g364fb_post_load,
  450. .fields = (VMStateField[]) {
  451. VMSTATE_VBUFFER_UINT32(vram, G364State, 1, NULL, 0, vram_size),
  452. VMSTATE_BUFFER_UNSAFE(color_palette, G364State, 0, 256 * 3),
  453. VMSTATE_BUFFER_UNSAFE(cursor_palette, G364State, 0, 9),
  454. VMSTATE_UINT16_ARRAY(cursor, G364State, 512),
  455. VMSTATE_UINT32(cursor_position, G364State),
  456. VMSTATE_UINT32(ctla, G364State),
  457. VMSTATE_UINT32(top_of_screen, G364State),
  458. VMSTATE_UINT32(width, G364State),
  459. VMSTATE_UINT32(height, G364State),
  460. VMSTATE_END_OF_LIST()
  461. }
  462. };
  463. static void g364fb_init(DeviceState *dev, G364State *s)
  464. {
  465. s->vram = g_malloc0(s->vram_size);
  466. s->ds = graphic_console_init(g364fb_update_display,
  467. g364fb_invalidate_display,
  468. g364fb_screen_dump, NULL, s);
  469. memory_region_init_io(&s->mem_ctrl, &g364fb_ctrl_ops, s, "ctrl", 0x180000);
  470. memory_region_init_ram_ptr(&s->mem_vram, "vram",
  471. s->vram_size, s->vram);
  472. vmstate_register_ram(&s->mem_vram, dev);
  473. memory_region_set_coalescing(&s->mem_vram);
  474. }
  475. typedef struct {
  476. SysBusDevice busdev;
  477. G364State g364;
  478. } G364SysBusState;
  479. static int g364fb_sysbus_init(SysBusDevice *dev)
  480. {
  481. G364State *s = &FROM_SYSBUS(G364SysBusState, dev)->g364;
  482. g364fb_init(&dev->qdev, s);
  483. sysbus_init_irq(dev, &s->irq);
  484. sysbus_init_mmio(dev, &s->mem_ctrl);
  485. sysbus_init_mmio(dev, &s->mem_vram);
  486. return 0;
  487. }
  488. static void g364fb_sysbus_reset(DeviceState *d)
  489. {
  490. G364SysBusState *s = DO_UPCAST(G364SysBusState, busdev.qdev, d);
  491. g364fb_reset(&s->g364);
  492. }
  493. static Property g364fb_sysbus_properties[] = {
  494. DEFINE_PROP_HEX32("vram_size", G364SysBusState, g364.vram_size,
  495. 8 * 1024 * 1024),
  496. DEFINE_PROP_END_OF_LIST(),
  497. };
  498. static void g364fb_sysbus_class_init(ObjectClass *klass, void *data)
  499. {
  500. DeviceClass *dc = DEVICE_CLASS(klass);
  501. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  502. k->init = g364fb_sysbus_init;
  503. dc->desc = "G364 framebuffer";
  504. dc->reset = g364fb_sysbus_reset;
  505. dc->vmsd = &vmstate_g364fb;
  506. dc->props = g364fb_sysbus_properties;
  507. }
  508. static TypeInfo g364fb_sysbus_info = {
  509. .name = "sysbus-g364",
  510. .parent = TYPE_SYS_BUS_DEVICE,
  511. .instance_size = sizeof(G364SysBusState),
  512. .class_init = g364fb_sysbus_class_init,
  513. };
  514. static void g364fb_register_types(void)
  515. {
  516. type_register_static(&g364fb_sysbus_info);
  517. }
  518. type_init(g364fb_register_types)