xref: /qemu/hw/display/macfb.c (revision e6108b96363bda0704ca69e5dfdb4b07dc589336)
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 
13 #include "qemu/osdep.h"
14 #include "qemu/units.h"
15 #include "hw/sysbus.h"
16 #include "ui/console.h"
17 #include "ui/pixel_ops.h"
18 #include "hw/nubus/nubus.h"
19 #include "hw/display/macfb.h"
20 #include "qapi/error.h"
21 #include "hw/qdev-properties.h"
22 #include "migration/vmstate.h"
23 #include "trace.h"
24 
25 #define VIDEO_BASE 0x00001000
26 #define DAFB_BASE  0x00800000
27 
28 #define MACFB_PAGE_SIZE 4096
29 #define MACFB_VRAM_SIZE (4 * MiB)
30 
31 #define DAFB_MODE_SENSE     0x1c
32 #define DAFB_RESET          0x200
33 #define DAFB_LUT            0x213
34 
35 
36 /*
37  * Quadra sense codes taken from Apple Technical Note HW26:
38  * "Macintosh Quadra Built-In Video". The sense codes and
39  * extended sense codes have different meanings:
40  *
41  * Sense:
42  *    bit 2: SENSE2 (pin 10)
43  *    bit 1: SENSE1 (pin 7)
44  *    bit 0: SENSE0 (pin 4)
45  *
46  * 0 = pin tied to ground
47  * 1 = pin unconnected
48  *
49  * Extended Sense:
50  *    bit 2: pins 4-10
51  *    bit 1: pins 10-7
52  *    bit 0: pins 7-4
53  *
54  * 0 = pins tied together
55  * 1 = pins unconnected
56  *
57  * Reads from the sense register appear to be active low, i.e. a 1 indicates
58  * that the pin is tied to ground, a 0 indicates the pin is disconnected.
59  *
60  * Writes to the sense register appear to activate pulldowns i.e. a 1 enables
61  * a pulldown on a particular pin.
62  *
63  * The MacOS toolbox appears to use a series of reads and writes to first
64  * determine if extended sense is to be used, and then check which pins are
65  * tied together in order to determine the display type.
66  */
67 
68 typedef struct MacFbSense {
69     uint8_t type;
70     uint8_t sense;
71     uint8_t ext_sense;
72 } MacFbSense;
73 
74 static MacFbSense macfb_sense_table[] = {
75     { MACFB_DISPLAY_APPLE_21_COLOR, 0x0, 0 },
76     { MACFB_DISPLAY_APPLE_PORTRAIT, 0x1, 0 },
77     { MACFB_DISPLAY_APPLE_12_RGB, 0x2, 0 },
78     { MACFB_DISPLAY_APPLE_2PAGE_MONO, 0x3, 0 },
79     { MACFB_DISPLAY_NTSC_UNDERSCAN, 0x4, 0 },
80     { MACFB_DISPLAY_NTSC_OVERSCAN, 0x4, 0 },
81     { MACFB_DISPLAY_APPLE_12_MONO, 0x6, 0 },
82     { MACFB_DISPLAY_APPLE_13_RGB, 0x6, 0 },
83     { MACFB_DISPLAY_16_COLOR, 0x7, 0x3 },
84     { MACFB_DISPLAY_PAL1_UNDERSCAN, 0x7, 0x0 },
85     { MACFB_DISPLAY_PAL1_OVERSCAN, 0x7, 0x0 },
86     { MACFB_DISPLAY_PAL2_UNDERSCAN, 0x7, 0x6 },
87     { MACFB_DISPLAY_PAL2_OVERSCAN, 0x7, 0x6 },
88     { MACFB_DISPLAY_VGA, 0x7, 0x5 },
89     { MACFB_DISPLAY_SVGA, 0x7, 0x5 },
90 };
91 
92 
93 typedef void macfb_draw_line_func(MacfbState *s, uint8_t *d, uint32_t addr,
94                                   int width);
95 
96 static inline uint8_t macfb_read_byte(MacfbState *s, uint32_t addr)
97 {
98     return s->vram[addr & s->vram_bit_mask];
99 }
100 
101 /* 1-bit color */
102 static void macfb_draw_line1(MacfbState *s, uint8_t *d, uint32_t addr,
103                              int width)
104 {
105     uint8_t r, g, b;
106     int x;
107 
108     for (x = 0; x < width; x++) {
109         int bit = x & 7;
110         int idx = (macfb_read_byte(s, addr) >> (7 - bit)) & 1;
111         r = g = b  = ((1 - idx) << 7);
112         addr += (bit == 7);
113 
114         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
115         d += 4;
116     }
117 }
118 
119 /* 2-bit color */
120 static void macfb_draw_line2(MacfbState *s, uint8_t *d, uint32_t addr,
121                              int width)
122 {
123     uint8_t r, g, b;
124     int x;
125 
126     for (x = 0; x < width; x++) {
127         int bit = (x & 3);
128         int idx = (macfb_read_byte(s, addr) >> ((3 - bit) << 1)) & 3;
129         r = s->color_palette[idx * 3];
130         g = s->color_palette[idx * 3 + 1];
131         b = s->color_palette[idx * 3 + 2];
132         addr += (bit == 3);
133 
134         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
135         d += 4;
136     }
137 }
138 
139 /* 4-bit color */
140 static void macfb_draw_line4(MacfbState *s, uint8_t *d, uint32_t addr,
141                              int width)
142 {
143     uint8_t r, g, b;
144     int x;
145 
146     for (x = 0; x < width; x++) {
147         int bit = x & 1;
148         int idx = (macfb_read_byte(s, addr) >> ((1 - bit) << 2)) & 15;
149         r = s->color_palette[idx * 3];
150         g = s->color_palette[idx * 3 + 1];
151         b = s->color_palette[idx * 3 + 2];
152         addr += (bit == 1);
153 
154         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
155         d += 4;
156     }
157 }
158 
159 /* 8-bit color */
160 static void macfb_draw_line8(MacfbState *s, uint8_t *d, uint32_t addr,
161                              int width)
162 {
163     uint8_t r, g, b;
164     int x;
165 
166     for (x = 0; x < width; x++) {
167         r = s->color_palette[macfb_read_byte(s, addr) * 3];
168         g = s->color_palette[macfb_read_byte(s, addr) * 3 + 1];
169         b = s->color_palette[macfb_read_byte(s, addr) * 3 + 2];
170         addr++;
171 
172         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
173         d += 4;
174     }
175 }
176 
177 /* 16-bit color */
178 static void macfb_draw_line16(MacfbState *s, uint8_t *d, uint32_t addr,
179                               int width)
180 {
181     uint8_t r, g, b;
182     int x;
183 
184     for (x = 0; x < width; x++) {
185         uint16_t pixel;
186         pixel = (macfb_read_byte(s, addr) << 8) | macfb_read_byte(s, addr + 1);
187         r = ((pixel >> 10) & 0x1f) << 3;
188         g = ((pixel >> 5) & 0x1f) << 3;
189         b = (pixel & 0x1f) << 3;
190         addr += 2;
191 
192         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
193         d += 4;
194     }
195 }
196 
197 /* 24-bit color */
198 static void macfb_draw_line24(MacfbState *s, uint8_t *d, uint32_t addr,
199                               int width)
200 {
201     uint8_t r, g, b;
202     int x;
203 
204     for (x = 0; x < width; x++) {
205         r = macfb_read_byte(s, addr);
206         g = macfb_read_byte(s, addr + 1);
207         b = macfb_read_byte(s, addr + 2);
208         addr += 3;
209 
210         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
211         d += 4;
212     }
213 }
214 
215 
216 enum {
217     MACFB_DRAW_LINE1,
218     MACFB_DRAW_LINE2,
219     MACFB_DRAW_LINE4,
220     MACFB_DRAW_LINE8,
221     MACFB_DRAW_LINE16,
222     MACFB_DRAW_LINE24,
223     MACFB_DRAW_LINE_NB,
224 };
225 
226 static macfb_draw_line_func * const
227                               macfb_draw_line_table[MACFB_DRAW_LINE_NB] = {
228     macfb_draw_line1,
229     macfb_draw_line2,
230     macfb_draw_line4,
231     macfb_draw_line8,
232     macfb_draw_line16,
233     macfb_draw_line24,
234 };
235 
236 static int macfb_check_dirty(MacfbState *s, DirtyBitmapSnapshot *snap,
237                              ram_addr_t addr, int len)
238 {
239     return memory_region_snapshot_get_dirty(&s->mem_vram, snap, addr, len);
240 }
241 
242 static void macfb_draw_graphic(MacfbState *s)
243 {
244     DisplaySurface *surface = qemu_console_surface(s->con);
245     DirtyBitmapSnapshot *snap = NULL;
246     ram_addr_t page;
247     uint32_t v = 0;
248     int y, ymin;
249     int macfb_stride = (s->depth * s->width + 7) / 8;
250     macfb_draw_line_func *macfb_draw_line;
251 
252     switch (s->depth) {
253     case 1:
254         v = MACFB_DRAW_LINE1;
255         break;
256     case 2:
257         v = MACFB_DRAW_LINE2;
258         break;
259     case 4:
260         v = MACFB_DRAW_LINE4;
261         break;
262     case 8:
263         v = MACFB_DRAW_LINE8;
264         break;
265     case 16:
266         v = MACFB_DRAW_LINE16;
267         break;
268     case 24:
269         v = MACFB_DRAW_LINE24;
270         break;
271     }
272 
273     macfb_draw_line = macfb_draw_line_table[v];
274     assert(macfb_draw_line != NULL);
275 
276     snap = memory_region_snapshot_and_clear_dirty(&s->mem_vram, 0x0,
277                                              memory_region_size(&s->mem_vram),
278                                              DIRTY_MEMORY_VGA);
279 
280     ymin = -1;
281     page = 0;
282     for (y = 0; y < s->height; y++, page += macfb_stride) {
283         if (macfb_check_dirty(s, snap, page, macfb_stride)) {
284             uint8_t *data_display;
285 
286             data_display = surface_data(surface) + y * surface_stride(surface);
287             macfb_draw_line(s, data_display, page, s->width);
288 
289             if (ymin < 0) {
290                 ymin = y;
291             }
292         } else {
293             if (ymin >= 0) {
294                 dpy_gfx_update(s->con, 0, ymin, s->width, y - ymin);
295                 ymin = -1;
296             }
297         }
298     }
299 
300     if (ymin >= 0) {
301         dpy_gfx_update(s->con, 0, ymin, s->width, y - ymin);
302     }
303 
304     g_free(snap);
305 }
306 
307 static void macfb_invalidate_display(void *opaque)
308 {
309     MacfbState *s = opaque;
310 
311     memory_region_set_dirty(&s->mem_vram, 0, MACFB_VRAM_SIZE);
312 }
313 
314 static uint32_t macfb_sense_read(MacfbState *s)
315 {
316     MacFbSense *macfb_sense;
317     uint8_t sense;
318 
319     macfb_sense = &macfb_sense_table[MACFB_DISPLAY_VGA];
320     if (macfb_sense->sense == 0x7) {
321         /* Extended sense */
322         sense = 0;
323         if (!(macfb_sense->ext_sense & 1)) {
324             /* Pins 7-4 together */
325             if (~s->sense & 3) {
326                 sense = (~s->sense & 7) | 3;
327             }
328         }
329         if (!(macfb_sense->ext_sense & 2)) {
330             /* Pins 10-7 together */
331             if (~s->sense & 6) {
332                 sense = (~s->sense & 7) | 6;
333             }
334         }
335         if (!(macfb_sense->ext_sense & 4)) {
336             /* Pins 4-10 together */
337             if (~s->sense & 5) {
338                 sense = (~s->sense & 7) | 5;
339             }
340         }
341     } else {
342         /* Normal sense */
343         sense = (~macfb_sense->sense & 7) | (~s->sense & 7);
344     }
345 
346     trace_macfb_sense_read(sense);
347     return sense;
348 }
349 
350 static void macfb_sense_write(MacfbState *s, uint32_t val)
351 {
352     s->sense = val;
353 
354     trace_macfb_sense_write(val);
355     return;
356 }
357 
358 static void macfb_update_display(void *opaque)
359 {
360     MacfbState *s = opaque;
361     DisplaySurface *surface = qemu_console_surface(s->con);
362 
363     qemu_flush_coalesced_mmio_buffer();
364 
365     if (s->width == 0 || s->height == 0) {
366         return;
367     }
368 
369     if (s->width != surface_width(surface) ||
370         s->height != surface_height(surface)) {
371         qemu_console_resize(s->con, s->width, s->height);
372     }
373 
374     macfb_draw_graphic(s);
375 }
376 
377 static void macfb_reset(MacfbState *s)
378 {
379     int i;
380 
381     s->palette_current = 0;
382     for (i = 0; i < 256; i++) {
383         s->color_palette[i * 3] = 255 - i;
384         s->color_palette[i * 3 + 1] = 255 - i;
385         s->color_palette[i * 3 + 2] = 255 - i;
386     }
387     memset(s->vram, 0, MACFB_VRAM_SIZE);
388     macfb_invalidate_display(s);
389 }
390 
391 static uint64_t macfb_ctrl_read(void *opaque,
392                                 hwaddr addr,
393                                 unsigned int size)
394 {
395     MacfbState *s = opaque;
396     uint64_t val = 0;
397 
398     switch (addr) {
399     case DAFB_MODE_SENSE:
400         val = macfb_sense_read(s);
401         break;
402     }
403 
404     trace_macfb_ctrl_read(addr, val, size);
405     return val;
406 }
407 
408 static void macfb_ctrl_write(void *opaque,
409                              hwaddr addr,
410                              uint64_t val,
411                              unsigned int size)
412 {
413     MacfbState *s = opaque;
414     switch (addr) {
415     case DAFB_MODE_SENSE:
416         macfb_sense_write(s, val);
417         break;
418     case DAFB_RESET:
419         s->palette_current = 0;
420         break;
421     case DAFB_LUT:
422         s->color_palette[s->palette_current] = val;
423         s->palette_current = (s->palette_current + 1) %
424                              ARRAY_SIZE(s->color_palette);
425         if (s->palette_current % 3) {
426             macfb_invalidate_display(s);
427         }
428         break;
429     }
430 
431     trace_macfb_ctrl_write(addr, val, size);
432 }
433 
434 static const MemoryRegionOps macfb_ctrl_ops = {
435     .read = macfb_ctrl_read,
436     .write = macfb_ctrl_write,
437     .endianness = DEVICE_BIG_ENDIAN,
438     .impl.min_access_size = 1,
439     .impl.max_access_size = 4,
440 };
441 
442 static int macfb_post_load(void *opaque, int version_id)
443 {
444     macfb_invalidate_display(opaque);
445     return 0;
446 }
447 
448 static const VMStateDescription vmstate_macfb = {
449     .name = "macfb",
450     .version_id = 1,
451     .minimum_version_id = 1,
452     .minimum_version_id_old = 1,
453     .post_load = macfb_post_load,
454     .fields = (VMStateField[]) {
455         VMSTATE_UINT8_ARRAY(color_palette, MacfbState, 256 * 3),
456         VMSTATE_UINT32(palette_current, MacfbState),
457         VMSTATE_UINT32(sense, MacfbState),
458         VMSTATE_END_OF_LIST()
459     }
460 };
461 
462 static const GraphicHwOps macfb_ops = {
463     .invalidate = macfb_invalidate_display,
464     .gfx_update = macfb_update_display,
465 };
466 
467 static bool macfb_common_realize(DeviceState *dev, MacfbState *s, Error **errp)
468 {
469     DisplaySurface *surface;
470 
471     if (s->depth != 1 && s->depth != 2 && s->depth != 4 && s->depth != 8 &&
472         s->depth != 16 && s->depth != 24) {
473         error_setg(errp, "unknown guest depth %d", s->depth);
474         return false;
475     }
476 
477     s->con = graphic_console_init(dev, 0, &macfb_ops, s);
478     surface = qemu_console_surface(s->con);
479 
480     if (surface_bits_per_pixel(surface) != 32) {
481         error_setg(errp, "unknown host depth %d",
482                    surface_bits_per_pixel(surface));
483         return false;
484     }
485 
486     memory_region_init_io(&s->mem_ctrl, OBJECT(dev), &macfb_ctrl_ops, s,
487                           "macfb-ctrl", 0x1000);
488 
489     memory_region_init_ram(&s->mem_vram, OBJECT(dev), "macfb-vram",
490                            MACFB_VRAM_SIZE, &error_abort);
491     s->vram = memory_region_get_ram_ptr(&s->mem_vram);
492     s->vram_bit_mask = MACFB_VRAM_SIZE - 1;
493     memory_region_set_coalescing(&s->mem_vram);
494 
495     return true;
496 }
497 
498 static void macfb_sysbus_realize(DeviceState *dev, Error **errp)
499 {
500     MacfbSysBusState *s = MACFB(dev);
501     MacfbState *ms = &s->macfb;
502 
503     if (!macfb_common_realize(dev, ms, errp)) {
504         return;
505     }
506 
507     sysbus_init_mmio(SYS_BUS_DEVICE(s), &ms->mem_ctrl);
508     sysbus_init_mmio(SYS_BUS_DEVICE(s), &ms->mem_vram);
509 }
510 
511 static void macfb_nubus_realize(DeviceState *dev, Error **errp)
512 {
513     NubusDevice *nd = NUBUS_DEVICE(dev);
514     MacfbNubusState *s = NUBUS_MACFB(dev);
515     MacfbNubusDeviceClass *ndc = NUBUS_MACFB_GET_CLASS(dev);
516     MacfbState *ms = &s->macfb;
517 
518     ndc->parent_realize(dev, errp);
519     if (*errp) {
520         return;
521     }
522 
523     if (!macfb_common_realize(dev, ms, errp)) {
524         return;
525     }
526 
527     memory_region_add_subregion(&nd->slot_mem, DAFB_BASE, &ms->mem_ctrl);
528     memory_region_add_subregion(&nd->slot_mem, VIDEO_BASE, &ms->mem_vram);
529 }
530 
531 static void macfb_sysbus_reset(DeviceState *d)
532 {
533     MacfbSysBusState *s = MACFB(d);
534     macfb_reset(&s->macfb);
535 }
536 
537 static void macfb_nubus_reset(DeviceState *d)
538 {
539     MacfbNubusState *s = NUBUS_MACFB(d);
540     macfb_reset(&s->macfb);
541 }
542 
543 static Property macfb_sysbus_properties[] = {
544     DEFINE_PROP_UINT32("width", MacfbSysBusState, macfb.width, 640),
545     DEFINE_PROP_UINT32("height", MacfbSysBusState, macfb.height, 480),
546     DEFINE_PROP_UINT8("depth", MacfbSysBusState, macfb.depth, 8),
547     DEFINE_PROP_END_OF_LIST(),
548 };
549 
550 static Property macfb_nubus_properties[] = {
551     DEFINE_PROP_UINT32("width", MacfbNubusState, macfb.width, 640),
552     DEFINE_PROP_UINT32("height", MacfbNubusState, macfb.height, 480),
553     DEFINE_PROP_UINT8("depth", MacfbNubusState, macfb.depth, 8),
554     DEFINE_PROP_END_OF_LIST(),
555 };
556 
557 static void macfb_sysbus_class_init(ObjectClass *klass, void *data)
558 {
559     DeviceClass *dc = DEVICE_CLASS(klass);
560 
561     dc->realize = macfb_sysbus_realize;
562     dc->desc = "SysBus Macintosh framebuffer";
563     dc->reset = macfb_sysbus_reset;
564     dc->vmsd = &vmstate_macfb;
565     device_class_set_props(dc, macfb_sysbus_properties);
566 }
567 
568 static void macfb_nubus_class_init(ObjectClass *klass, void *data)
569 {
570     DeviceClass *dc = DEVICE_CLASS(klass);
571     MacfbNubusDeviceClass *ndc = NUBUS_MACFB_CLASS(klass);
572 
573     device_class_set_parent_realize(dc, macfb_nubus_realize,
574                                     &ndc->parent_realize);
575     dc->desc = "Nubus Macintosh framebuffer";
576     dc->reset = macfb_nubus_reset;
577     dc->vmsd = &vmstate_macfb;
578     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
579     device_class_set_props(dc, macfb_nubus_properties);
580 }
581 
582 static TypeInfo macfb_sysbus_info = {
583     .name          = TYPE_MACFB,
584     .parent        = TYPE_SYS_BUS_DEVICE,
585     .instance_size = sizeof(MacfbSysBusState),
586     .class_init    = macfb_sysbus_class_init,
587 };
588 
589 static TypeInfo macfb_nubus_info = {
590     .name          = TYPE_NUBUS_MACFB,
591     .parent        = TYPE_NUBUS_DEVICE,
592     .instance_size = sizeof(MacfbNubusState),
593     .class_init    = macfb_nubus_class_init,
594     .class_size    = sizeof(MacfbNubusDeviceClass),
595 };
596 
597 static void macfb_register_types(void)
598 {
599     type_register_static(&macfb_sysbus_info);
600     type_register_static(&macfb_nubus_info);
601 }
602 
603 type_init(macfb_register_types)
604