macfb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * QEMU Motorola 680x0 Macintosh Video Card Emulation
  3. * Copyright (c) 2012-2018 Laurent Vivier
  4. *
  5. * some parts from QEMU G364 framebuffer Emulator.
  6. * Copyright (c) 2007-2011 Herve Poussineau
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  9. * See the COPYING file in the top-level directory.
  10. *
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/units.h"
  14. #include "hw/sysbus.h"
  15. #include "ui/console.h"
  16. #include "ui/pixel_ops.h"
  17. #include "hw/nubus/nubus.h"
  18. #include "hw/display/macfb.h"
  19. #include "qapi/error.h"
  20. #include "hw/qdev-properties.h"
  21. #include "migration/vmstate.h"
  22. #define VIDEO_BASE 0x00001000
  23. #define DAFB_BASE 0x00800000
  24. #define MACFB_PAGE_SIZE 4096
  25. #define MACFB_VRAM_SIZE (4 * MiB)
  26. #define DAFB_RESET 0x200
  27. #define DAFB_LUT 0x213
  28. typedef void macfb_draw_line_func(MacfbState *s, uint8_t *d, uint32_t addr,
  29. int width);
  30. static inline uint8_t macfb_read_byte(MacfbState *s, uint32_t addr)
  31. {
  32. return s->vram[addr & s->vram_bit_mask];
  33. }
  34. /* 1-bit color */
  35. static void macfb_draw_line1(MacfbState *s, uint8_t *d, uint32_t addr,
  36. int width)
  37. {
  38. uint8_t r, g, b;
  39. int x;
  40. for (x = 0; x < width; x++) {
  41. int bit = x & 7;
  42. int idx = (macfb_read_byte(s, addr) >> (7 - bit)) & 1;
  43. r = g = b = ((1 - idx) << 7);
  44. addr += (bit == 7);
  45. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  46. d += 4;
  47. }
  48. }
  49. /* 2-bit color */
  50. static void macfb_draw_line2(MacfbState *s, uint8_t *d, uint32_t addr,
  51. int width)
  52. {
  53. uint8_t r, g, b;
  54. int x;
  55. for (x = 0; x < width; x++) {
  56. int bit = (x & 3);
  57. int idx = (macfb_read_byte(s, addr) >> ((3 - bit) << 1)) & 3;
  58. r = s->color_palette[idx * 3];
  59. g = s->color_palette[idx * 3 + 1];
  60. b = s->color_palette[idx * 3 + 2];
  61. addr += (bit == 3);
  62. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  63. d += 4;
  64. }
  65. }
  66. /* 4-bit color */
  67. static void macfb_draw_line4(MacfbState *s, uint8_t *d, uint32_t addr,
  68. int width)
  69. {
  70. uint8_t r, g, b;
  71. int x;
  72. for (x = 0; x < width; x++) {
  73. int bit = x & 1;
  74. int idx = (macfb_read_byte(s, addr) >> ((1 - bit) << 2)) & 15;
  75. r = s->color_palette[idx * 3];
  76. g = s->color_palette[idx * 3 + 1];
  77. b = s->color_palette[idx * 3 + 2];
  78. addr += (bit == 1);
  79. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  80. d += 4;
  81. }
  82. }
  83. /* 8-bit color */
  84. static void macfb_draw_line8(MacfbState *s, uint8_t *d, uint32_t addr,
  85. int width)
  86. {
  87. uint8_t r, g, b;
  88. int x;
  89. for (x = 0; x < width; x++) {
  90. r = s->color_palette[macfb_read_byte(s, addr) * 3];
  91. g = s->color_palette[macfb_read_byte(s, addr) * 3 + 1];
  92. b = s->color_palette[macfb_read_byte(s, addr) * 3 + 2];
  93. addr++;
  94. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  95. d += 4;
  96. }
  97. }
  98. /* 16-bit color */
  99. static void macfb_draw_line16(MacfbState *s, uint8_t *d, uint32_t addr,
  100. int width)
  101. {
  102. uint8_t r, g, b;
  103. int x;
  104. for (x = 0; x < width; x++) {
  105. uint16_t pixel;
  106. pixel = (macfb_read_byte(s, addr) << 8) | macfb_read_byte(s, addr + 1);
  107. r = ((pixel >> 10) & 0x1f) << 3;
  108. g = ((pixel >> 5) & 0x1f) << 3;
  109. b = (pixel & 0x1f) << 3;
  110. addr += 2;
  111. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  112. d += 4;
  113. }
  114. }
  115. /* 24-bit color */
  116. static void macfb_draw_line24(MacfbState *s, uint8_t *d, uint32_t addr,
  117. int width)
  118. {
  119. uint8_t r, g, b;
  120. int x;
  121. for (x = 0; x < width; x++) {
  122. r = macfb_read_byte(s, addr);
  123. g = macfb_read_byte(s, addr + 1);
  124. b = macfb_read_byte(s, addr + 2);
  125. addr += 3;
  126. *(uint32_t *)d = rgb_to_pixel32(r, g, b);
  127. d += 4;
  128. }
  129. }
  130. enum {
  131. MACFB_DRAW_LINE1,
  132. MACFB_DRAW_LINE2,
  133. MACFB_DRAW_LINE4,
  134. MACFB_DRAW_LINE8,
  135. MACFB_DRAW_LINE16,
  136. MACFB_DRAW_LINE24,
  137. MACFB_DRAW_LINE_NB,
  138. };
  139. static macfb_draw_line_func * const
  140. macfb_draw_line_table[MACFB_DRAW_LINE_NB] = {
  141. macfb_draw_line1,
  142. macfb_draw_line2,
  143. macfb_draw_line4,
  144. macfb_draw_line8,
  145. macfb_draw_line16,
  146. macfb_draw_line24,
  147. };
  148. static int macfb_check_dirty(MacfbState *s, DirtyBitmapSnapshot *snap,
  149. ram_addr_t addr, int len)
  150. {
  151. return memory_region_snapshot_get_dirty(&s->mem_vram, snap, addr, len);
  152. }
  153. static void macfb_draw_graphic(MacfbState *s)
  154. {
  155. DisplaySurface *surface = qemu_console_surface(s->con);
  156. DirtyBitmapSnapshot *snap = NULL;
  157. ram_addr_t page;
  158. uint32_t v = 0;
  159. int y, ymin;
  160. int macfb_stride = (s->depth * s->width + 7) / 8;
  161. macfb_draw_line_func *macfb_draw_line;
  162. switch (s->depth) {
  163. case 1:
  164. v = MACFB_DRAW_LINE1;
  165. break;
  166. case 2:
  167. v = MACFB_DRAW_LINE2;
  168. break;
  169. case 4:
  170. v = MACFB_DRAW_LINE4;
  171. break;
  172. case 8:
  173. v = MACFB_DRAW_LINE8;
  174. break;
  175. case 16:
  176. v = MACFB_DRAW_LINE16;
  177. break;
  178. case 24:
  179. v = MACFB_DRAW_LINE24;
  180. break;
  181. }
  182. macfb_draw_line = macfb_draw_line_table[v];
  183. assert(macfb_draw_line != NULL);
  184. snap = memory_region_snapshot_and_clear_dirty(&s->mem_vram, 0x0,
  185. memory_region_size(&s->mem_vram),
  186. DIRTY_MEMORY_VGA);
  187. ymin = -1;
  188. page = 0;
  189. for (y = 0; y < s->height; y++, page += macfb_stride) {
  190. if (macfb_check_dirty(s, snap, page, macfb_stride)) {
  191. uint8_t *data_display;
  192. data_display = surface_data(surface) + y * surface_stride(surface);
  193. macfb_draw_line(s, data_display, page, s->width);
  194. if (ymin < 0) {
  195. ymin = y;
  196. }
  197. } else {
  198. if (ymin >= 0) {
  199. dpy_gfx_update(s->con, 0, ymin, s->width, y - ymin);
  200. ymin = -1;
  201. }
  202. }
  203. }
  204. if (ymin >= 0) {
  205. dpy_gfx_update(s->con, 0, ymin, s->width, y - ymin);
  206. }
  207. g_free(snap);
  208. }
  209. static void macfb_invalidate_display(void *opaque)
  210. {
  211. MacfbState *s = opaque;
  212. memory_region_set_dirty(&s->mem_vram, 0, MACFB_VRAM_SIZE);
  213. }
  214. static void macfb_update_display(void *opaque)
  215. {
  216. MacfbState *s = opaque;
  217. DisplaySurface *surface = qemu_console_surface(s->con);
  218. qemu_flush_coalesced_mmio_buffer();
  219. if (s->width == 0 || s->height == 0) {
  220. return;
  221. }
  222. if (s->width != surface_width(surface) ||
  223. s->height != surface_height(surface)) {
  224. qemu_console_resize(s->con, s->width, s->height);
  225. }
  226. macfb_draw_graphic(s);
  227. }
  228. static void macfb_reset(MacfbState *s)
  229. {
  230. int i;
  231. s->palette_current = 0;
  232. for (i = 0; i < 256; i++) {
  233. s->color_palette[i * 3] = 255 - i;
  234. s->color_palette[i * 3 + 1] = 255 - i;
  235. s->color_palette[i * 3 + 2] = 255 - i;
  236. }
  237. memset(s->vram, 0, MACFB_VRAM_SIZE);
  238. macfb_invalidate_display(s);
  239. }
  240. static uint64_t macfb_ctrl_read(void *opaque,
  241. hwaddr addr,
  242. unsigned int size)
  243. {
  244. return 0;
  245. }
  246. static void macfb_ctrl_write(void *opaque,
  247. hwaddr addr,
  248. uint64_t val,
  249. unsigned int size)
  250. {
  251. MacfbState *s = opaque;
  252. switch (addr) {
  253. case DAFB_RESET:
  254. s->palette_current = 0;
  255. break;
  256. case DAFB_LUT:
  257. s->color_palette[s->palette_current++] = val;
  258. if (s->palette_current % 3) {
  259. macfb_invalidate_display(s);
  260. }
  261. break;
  262. }
  263. }
  264. static const MemoryRegionOps macfb_ctrl_ops = {
  265. .read = macfb_ctrl_read,
  266. .write = macfb_ctrl_write,
  267. .endianness = DEVICE_BIG_ENDIAN,
  268. .impl.min_access_size = 1,
  269. .impl.max_access_size = 4,
  270. };
  271. static int macfb_post_load(void *opaque, int version_id)
  272. {
  273. macfb_invalidate_display(opaque);
  274. return 0;
  275. }
  276. static const VMStateDescription vmstate_macfb = {
  277. .name = "macfb",
  278. .version_id = 1,
  279. .minimum_version_id = 1,
  280. .minimum_version_id_old = 1,
  281. .post_load = macfb_post_load,
  282. .fields = (VMStateField[]) {
  283. VMSTATE_UINT8_ARRAY(color_palette, MacfbState, 256 * 3),
  284. VMSTATE_UINT32(palette_current, MacfbState),
  285. VMSTATE_END_OF_LIST()
  286. }
  287. };
  288. static const GraphicHwOps macfb_ops = {
  289. .invalidate = macfb_invalidate_display,
  290. .gfx_update = macfb_update_display,
  291. };
  292. static void macfb_common_realize(DeviceState *dev, MacfbState *s, Error **errp)
  293. {
  294. DisplaySurface *surface;
  295. if (s->depth != 1 && s->depth != 2 && s->depth != 4 && s->depth != 8 &&
  296. s->depth != 16 && s->depth != 24) {
  297. error_setg(errp, "unknown guest depth %d", s->depth);
  298. return;
  299. }
  300. s->con = graphic_console_init(dev, 0, &macfb_ops, s);
  301. surface = qemu_console_surface(s->con);
  302. if (surface_bits_per_pixel(surface) != 32) {
  303. error_setg(errp, "unknown host depth %d",
  304. surface_bits_per_pixel(surface));
  305. return;
  306. }
  307. memory_region_init_io(&s->mem_ctrl, OBJECT(dev), &macfb_ctrl_ops, s,
  308. "macfb-ctrl", 0x1000);
  309. memory_region_init_ram_nomigrate(&s->mem_vram, OBJECT(s), "macfb-vram",
  310. MACFB_VRAM_SIZE, errp);
  311. s->vram = memory_region_get_ram_ptr(&s->mem_vram);
  312. s->vram_bit_mask = MACFB_VRAM_SIZE - 1;
  313. vmstate_register_ram(&s->mem_vram, dev);
  314. memory_region_set_coalescing(&s->mem_vram);
  315. }
  316. static void macfb_sysbus_realize(DeviceState *dev, Error **errp)
  317. {
  318. MacfbSysBusState *s = MACFB(dev);
  319. MacfbState *ms = &s->macfb;
  320. macfb_common_realize(dev, ms, errp);
  321. sysbus_init_mmio(SYS_BUS_DEVICE(s), &ms->mem_ctrl);
  322. sysbus_init_mmio(SYS_BUS_DEVICE(s), &ms->mem_vram);
  323. }
  324. const uint8_t macfb_rom[] = {
  325. 255, 0, 0, 0,
  326. };
  327. static void macfb_nubus_realize(DeviceState *dev, Error **errp)
  328. {
  329. NubusDevice *nd = NUBUS_DEVICE(dev);
  330. MacfbNubusState *s = NUBUS_MACFB(dev);
  331. MacfbNubusDeviceClass *ndc = MACFB_NUBUS_GET_CLASS(dev);
  332. MacfbState *ms = &s->macfb;
  333. ndc->parent_realize(dev, errp);
  334. macfb_common_realize(dev, ms, errp);
  335. memory_region_add_subregion(&nd->slot_mem, DAFB_BASE, &ms->mem_ctrl);
  336. memory_region_add_subregion(&nd->slot_mem, VIDEO_BASE, &ms->mem_vram);
  337. nubus_register_rom(nd, macfb_rom, sizeof(macfb_rom), 1, 9, 0xf);
  338. }
  339. static void macfb_sysbus_reset(DeviceState *d)
  340. {
  341. MacfbSysBusState *s = MACFB(d);
  342. macfb_reset(&s->macfb);
  343. }
  344. static void macfb_nubus_reset(DeviceState *d)
  345. {
  346. MacfbNubusState *s = NUBUS_MACFB(d);
  347. macfb_reset(&s->macfb);
  348. }
  349. static Property macfb_sysbus_properties[] = {
  350. DEFINE_PROP_UINT32("width", MacfbSysBusState, macfb.width, 640),
  351. DEFINE_PROP_UINT32("height", MacfbSysBusState, macfb.height, 480),
  352. DEFINE_PROP_UINT8("depth", MacfbSysBusState, macfb.depth, 8),
  353. DEFINE_PROP_END_OF_LIST(),
  354. };
  355. static Property macfb_nubus_properties[] = {
  356. DEFINE_PROP_UINT32("width", MacfbNubusState, macfb.width, 640),
  357. DEFINE_PROP_UINT32("height", MacfbNubusState, macfb.height, 480),
  358. DEFINE_PROP_UINT8("depth", MacfbNubusState, macfb.depth, 8),
  359. DEFINE_PROP_END_OF_LIST(),
  360. };
  361. static void macfb_sysbus_class_init(ObjectClass *klass, void *data)
  362. {
  363. DeviceClass *dc = DEVICE_CLASS(klass);
  364. dc->realize = macfb_sysbus_realize;
  365. dc->desc = "SysBus Macintosh framebuffer";
  366. dc->reset = macfb_sysbus_reset;
  367. dc->vmsd = &vmstate_macfb;
  368. device_class_set_props(dc, macfb_sysbus_properties);
  369. }
  370. static void macfb_nubus_class_init(ObjectClass *klass, void *data)
  371. {
  372. DeviceClass *dc = DEVICE_CLASS(klass);
  373. MacfbNubusDeviceClass *ndc = MACFB_NUBUS_DEVICE_CLASS(klass);
  374. device_class_set_parent_realize(dc, macfb_nubus_realize,
  375. &ndc->parent_realize);
  376. dc->desc = "Nubus Macintosh framebuffer";
  377. dc->reset = macfb_nubus_reset;
  378. dc->vmsd = &vmstate_macfb;
  379. device_class_set_props(dc, macfb_nubus_properties);
  380. }
  381. static TypeInfo macfb_sysbus_info = {
  382. .name = TYPE_MACFB,
  383. .parent = TYPE_SYS_BUS_DEVICE,
  384. .instance_size = sizeof(MacfbSysBusState),
  385. .class_init = macfb_sysbus_class_init,
  386. };
  387. static TypeInfo macfb_nubus_info = {
  388. .name = TYPE_NUBUS_MACFB,
  389. .parent = TYPE_NUBUS_DEVICE,
  390. .instance_size = sizeof(MacfbNubusState),
  391. .class_init = macfb_nubus_class_init,
  392. .class_size = sizeof(MacfbNubusDeviceClass),
  393. };
  394. static void macfb_register_types(void)
  395. {
  396. type_register_static(&macfb_sysbus_info);
  397. type_register_static(&macfb_nubus_info);
  398. }
  399. type_init(macfb_register_types)