g364fb.c 17 KB

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