]> Git Repo - qemu.git/blame - hw/display/g364fb.c
Include hw/qdev-properties.h less
[qemu.git] / hw / display / g364fb.c
CommitLineData
1fc3d392
AJ
1/*
2 * QEMU G364 framebuffer Emulator.
3 *
97a3f6ff 4 * Copyright (c) 2007-2011 Herve Poussineau
1fc3d392
AJ
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 *
fad6cb1a 16 * You should have received a copy of the GNU General Public License along
8167ee88 17 * with this program; if not, see <http://www.gnu.org/licenses/>.
1fc3d392
AJ
18 */
19
47df5154 20#include "qemu/osdep.h"
f0353b0d 21#include "qemu/units.h"
83c9f4ca 22#include "hw/hw.h"
64552b6b 23#include "hw/irq.h"
a27bd6c7 24#include "hw/qdev-properties.h"
d49b6836 25#include "qemu/error-report.h"
0b8fa32f 26#include "qemu/module.h"
28ecbaee
PB
27#include "ui/console.h"
28#include "ui/pixel_ops.h"
b213b370 29#include "trace.h"
83c9f4ca 30#include "hw/sysbus.h"
d6454270 31#include "migration/vmstate.h"
0add30cf 32
1fc3d392 33typedef struct G364State {
0add30cf
AJ
34 /* hardware */
35 uint8_t *vram;
97a3f6ff 36 uint32_t vram_size;
0add30cf 37 qemu_irq irq;
97a3f6ff
HP
38 MemoryRegion mem_vram;
39 MemoryRegion mem_ctrl;
0add30cf
AJ
40 /* registers */
41 uint8_t color_palette[256][3];
42 uint8_t cursor_palette[3][3];
43 uint16_t cursor[512];
44 uint32_t cursor_position;
1fc3d392 45 uint32_t ctla;
0add30cf
AJ
46 uint32_t top_of_screen;
47 uint32_t width, height; /* in pixels */
1fc3d392 48 /* display refresh support */
c78f7137 49 QemuConsole *con;
0add30cf
AJ
50 int depth;
51 int blanked;
1fc3d392
AJ
52} G364State;
53
97a3f6ff
HP
54#define REG_BOOT 0x000000
55#define REG_DISPLAY 0x000118
56#define REG_VDISPLAY 0x000150
57#define REG_CTLA 0x000300
58#define REG_TOP 0x000400
59#define REG_CURS_PAL 0x000508
60#define REG_CURS_POS 0x000638
61#define REG_CLR_PAL 0x000800
62#define REG_CURS_PAT 0x001000
63#define REG_RESET 0x100000
0add30cf
AJ
64
65#define CTLA_FORCE_BLANK 0x00000400
66#define CTLA_NO_CURSOR 0x00800000
67
1213406b
BS
68#define G364_PAGE_SIZE 4096
69
f7189ac8 70static inline int check_dirty(G364State *s, DirtyBitmapSnapshot *snap, ram_addr_t page)
0add30cf 71{
f7189ac8 72 return memory_region_snapshot_get_dirty(&s->mem_vram, snap, page, G364_PAGE_SIZE);
0add30cf
AJ
73}
74
75static void g364fb_draw_graphic8(G364State *s)
1fc3d392 76{
c78f7137 77 DisplaySurface *surface = qemu_console_surface(s->con);
f7189ac8 78 DirtyBitmapSnapshot *snap;
0add30cf
AJ
79 int i, w;
80 uint8_t *vram;
81 uint8_t *data_display, *dd;
7fcf0c24 82 ram_addr_t page;
0add30cf
AJ
83 int x, y;
84 int xmin, xmax;
85 int ymin, ymax;
86 int xcursor, ycursor;
87 unsigned int (*rgb_to_pixel)(unsigned int r, unsigned int g, unsigned int b);
88
c78f7137 89 switch (surface_bits_per_pixel(surface)) {
1fc3d392 90 case 8:
0add30cf
AJ
91 rgb_to_pixel = rgb_to_pixel8;
92 w = 1;
1fc3d392
AJ
93 break;
94 case 15:
0add30cf
AJ
95 rgb_to_pixel = rgb_to_pixel15;
96 w = 2;
1fc3d392
AJ
97 break;
98 case 16:
0add30cf
AJ
99 rgb_to_pixel = rgb_to_pixel16;
100 w = 2;
1fc3d392
AJ
101 break;
102 case 32:
0add30cf
AJ
103 rgb_to_pixel = rgb_to_pixel32;
104 w = 4;
1fc3d392
AJ
105 break;
106 default:
b213b370 107 hw_error("g364: unknown host depth %d",
c78f7137 108 surface_bits_per_pixel(surface));
1fc3d392
AJ
109 return;
110 }
111
97a3f6ff 112 page = 0;
0add30cf
AJ
113
114 x = y = 0;
115 xmin = s->width;
116 xmax = 0;
117 ymin = s->height;
118 ymax = 0;
119
120 if (!(s->ctla & CTLA_NO_CURSOR)) {
121 xcursor = s->cursor_position >> 12;
122 ycursor = s->cursor_position & 0xfff;
123 } else {
124 xcursor = ycursor = -65;
125 }
126
127 vram = s->vram + s->top_of_screen;
128 /* XXX: out of range in vram? */
c78f7137 129 data_display = dd = surface_data(surface);
f7189ac8
PB
130 snap = memory_region_snapshot_and_clear_dirty(&s->mem_vram, 0, s->vram_size,
131 DIRTY_MEMORY_VGA);
0add30cf 132 while (y < s->height) {
f7189ac8 133 if (check_dirty(s, snap, page)) {
0add30cf
AJ
134 if (y < ymin)
135 ymin = ymax = y;
0add30cf
AJ
136 if (x < xmin)
137 xmin = x;
1213406b 138 for (i = 0; i < G364_PAGE_SIZE; i++) {
0add30cf
AJ
139 uint8_t index;
140 unsigned int color;
141 if (unlikely((y >= ycursor && y < ycursor + 64) &&
142 (x >= xcursor && x < xcursor + 64))) {
143 /* pointer area */
144 int xdiff = x - xcursor;
145 uint16_t curs = s->cursor[(y - ycursor) * 8 + xdiff / 8];
146 int op = (curs >> ((xdiff & 7) * 2)) & 3;
147 if (likely(op == 0)) {
148 /* transparent */
149 index = *vram;
150 color = (*rgb_to_pixel)(
151 s->color_palette[index][0],
152 s->color_palette[index][1],
153 s->color_palette[index][2]);
154 } else {
155 /* get cursor color */
156 index = op - 1;
157 color = (*rgb_to_pixel)(
158 s->cursor_palette[index][0],
159 s->cursor_palette[index][1],
160 s->cursor_palette[index][2]);
161 }
162 } else {
163 /* normal area */
164 index = *vram;
165 color = (*rgb_to_pixel)(
166 s->color_palette[index][0],
167 s->color_palette[index][1],
168 s->color_palette[index][2]);
169 }
170 memcpy(dd, &color, w);
171 dd += w;
172 x++;
173 vram++;
174 if (x == s->width) {
175 xmax = s->width - 1;
176 y++;
177 if (y == s->height) {
178 ymax = s->height - 1;
179 goto done;
180 }
c78f7137 181 data_display = dd = data_display + surface_stride(surface);
0add30cf
AJ
182 xmin = 0;
183 x = 0;
184 }
185 }
186 if (x > xmax)
187 xmax = x;
188 if (y > ymax)
189 ymax = y;
190 } else {
191 int dy;
7fcf0c24 192 if (xmax || ymax) {
c78f7137 193 dpy_gfx_update(s->con, xmin, ymin,
a93a4a22 194 xmax - xmin + 1, ymax - ymin + 1);
0add30cf
AJ
195 xmin = s->width;
196 xmax = 0;
197 ymin = s->height;
198 ymax = 0;
199 }
1213406b 200 x += G364_PAGE_SIZE;
0add30cf
AJ
201 dy = x / s->width;
202 x = x % s->width;
203 y += dy;
1213406b 204 vram += G364_PAGE_SIZE;
c78f7137 205 data_display += dy * surface_stride(surface);
0add30cf
AJ
206 dd = data_display + x * w;
207 }
1213406b 208 page += G364_PAGE_SIZE;
0add30cf
AJ
209 }
210
211done:
7fcf0c24 212 if (xmax || ymax) {
c78f7137 213 dpy_gfx_update(s->con, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
0add30cf 214 }
723250d6 215 g_free(snap);
1fc3d392
AJ
216}
217
0add30cf 218static void g364fb_draw_blank(G364State *s)
1fc3d392 219{
c78f7137 220 DisplaySurface *surface = qemu_console_surface(s->con);
1fc3d392
AJ
221 int i, w;
222 uint8_t *d;
223
0add30cf
AJ
224 if (s->blanked) {
225 /* Screen is already blank. No need to redraw it */
1fc3d392 226 return;
0add30cf 227 }
1fc3d392 228
c78f7137
GH
229 w = s->width * surface_bytes_per_pixel(surface);
230 d = surface_data(surface);
0add30cf 231 for (i = 0; i < s->height; i++) {
1fc3d392 232 memset(d, 0, w);
c78f7137 233 d += surface_stride(surface);
1fc3d392 234 }
221bb2d5 235
91155f8b 236 dpy_gfx_update_full(s->con);
0add30cf 237 s->blanked = 1;
1fc3d392
AJ
238}
239
1fc3d392
AJ
240static void g364fb_update_display(void *opaque)
241{
242 G364State *s = opaque;
c78f7137 243 DisplaySurface *surface = qemu_console_surface(s->con);
1fc3d392 244
e9a07334
JK
245 qemu_flush_coalesced_mmio_buffer();
246
0add30cf 247 if (s->width == 0 || s->height == 0)
221bb2d5
AJ
248 return;
249
c78f7137
GH
250 if (s->width != surface_width(surface) ||
251 s->height != surface_height(surface)) {
252 qemu_console_resize(s->con, s->width, s->height);
221bb2d5 253 }
0add30cf
AJ
254
255 if (s->ctla & CTLA_FORCE_BLANK) {
256 g364fb_draw_blank(s);
257 } else if (s->depth == 8) {
258 g364fb_draw_graphic8(s);
259 } else {
b213b370 260 error_report("g364: unknown guest depth %d", s->depth);
1fc3d392 261 }
0add30cf
AJ
262
263 qemu_irq_raise(s->irq);
1fc3d392
AJ
264}
265
86178a57 266static inline void g364fb_invalidate_display(void *opaque)
1fc3d392
AJ
267{
268 G364State *s = opaque;
0add30cf
AJ
269
270 s->blanked = 0;
fd4aa979 271 memory_region_set_dirty(&s->mem_vram, 0, s->vram_size);
1fc3d392
AJ
272}
273
97a3f6ff 274static void g364fb_reset(G364State *s)
1fc3d392 275{
0add30cf
AJ
276 qemu_irq_lower(s->irq);
277
278 memset(s->color_palette, 0, sizeof(s->color_palette));
279 memset(s->cursor_palette, 0, sizeof(s->cursor_palette));
280 memset(s->cursor, 0, sizeof(s->cursor));
281 s->cursor_position = 0;
282 s->ctla = 0;
283 s->top_of_screen = 0;
284 s->width = s->height = 0;
285 memset(s->vram, 0, s->vram_size);
97a3f6ff 286 g364fb_invalidate_display(s);
1fc3d392
AJ
287}
288
1fc3d392 289/* called for accesses to io ports */
97a3f6ff 290static uint64_t g364fb_ctrl_read(void *opaque,
a8170e5e 291 hwaddr addr,
97a3f6ff 292 unsigned int size)
1fc3d392 293{
0add30cf 294 G364State *s = opaque;
1fc3d392
AJ
295 uint32_t val;
296
0add30cf
AJ
297 if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
298 /* cursor pattern */
299 int idx = (addr - REG_CURS_PAT) >> 3;
300 val = s->cursor[idx];
301 } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
302 /* cursor palette */
303 int idx = (addr - REG_CURS_PAL) >> 3;
304 val = ((uint32_t)s->cursor_palette[idx][0] << 16);
305 val |= ((uint32_t)s->cursor_palette[idx][1] << 8);
306 val |= ((uint32_t)s->cursor_palette[idx][2] << 0);
307 } else {
308 switch (addr) {
0add30cf
AJ
309 case REG_DISPLAY:
310 val = s->width / 4;
311 break;
312 case REG_VDISPLAY:
313 val = s->height * 2;
314 break;
315 case REG_CTLA:
316 val = s->ctla;
317 break;
318 default:
319 {
b213b370
HP
320 error_report("g364: invalid read at [" TARGET_FMT_plx "]",
321 addr);
0add30cf
AJ
322 val = 0;
323 break;
324 }
325 }
1fc3d392
AJ
326 }
327
b213b370 328 trace_g364fb_read(addr, val);
1fc3d392
AJ
329
330 return val;
331}
332
0add30cf 333static void g364fb_update_depth(G364State *s)
1fc3d392 334{
38972938 335 static const int depths[8] = { 1, 2, 4, 8, 15, 16, 0 };
0add30cf
AJ
336 s->depth = depths[(s->ctla & 0x00700000) >> 20];
337}
1fc3d392 338
0add30cf
AJ
339static void g364_invalidate_cursor_position(G364State *s)
340{
c78f7137 341 DisplaySurface *surface = qemu_console_surface(s->con);
fd4aa979 342 int ymin, ymax, start, end;
1fc3d392 343
0add30cf
AJ
344 /* invalidate only near the cursor */
345 ymin = s->cursor_position & 0xfff;
346 ymax = MIN(s->height, ymin + 64);
c78f7137
GH
347 start = ymin * surface_stride(surface);
348 end = (ymax + 1) * surface_stride(surface);
1fc3d392 349
fd4aa979 350 memory_region_set_dirty(&s->mem_vram, start, end - start);
0add30cf
AJ
351}
352
97a3f6ff 353static void g364fb_ctrl_write(void *opaque,
a8170e5e 354 hwaddr addr,
97a3f6ff
HP
355 uint64_t val,
356 unsigned int size)
0add30cf
AJ
357{
358 G364State *s = opaque;
359
b213b370 360 trace_g364fb_write(addr, val);
0add30cf
AJ
361
362 if (addr >= REG_CLR_PAL && addr < REG_CLR_PAL + 0x800) {
1fc3d392 363 /* color palette */
0add30cf
AJ
364 int idx = (addr - REG_CLR_PAL) >> 3;
365 s->color_palette[idx][0] = (val >> 16) & 0xff;
366 s->color_palette[idx][1] = (val >> 8) & 0xff;
367 s->color_palette[idx][2] = val & 0xff;
368 g364fb_invalidate_display(s);
369 } else if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
370 /* cursor pattern */
371 int idx = (addr - REG_CURS_PAT) >> 3;
372 s->cursor[idx] = val;
373 g364fb_invalidate_display(s);
374 } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
375 /* cursor palette */
376 int idx = (addr - REG_CURS_PAL) >> 3;
377 s->cursor_palette[idx][0] = (val >> 16) & 0xff;
378 s->cursor_palette[idx][1] = (val >> 8) & 0xff;
379 s->cursor_palette[idx][2] = val & 0xff;
380 g364fb_invalidate_display(s);
1fc3d392
AJ
381 } else {
382 switch (addr) {
97a3f6ff
HP
383 case REG_BOOT: /* Boot timing */
384 case 0x00108: /* Line timing: half sync */
385 case 0x00110: /* Line timing: back porch */
386 case 0x00120: /* Line timing: short display */
387 case 0x00128: /* Frame timing: broad pulse */
388 case 0x00130: /* Frame timing: v sync */
389 case 0x00138: /* Frame timing: v preequalise */
390 case 0x00140: /* Frame timing: v postequalise */
391 case 0x00148: /* Frame timing: v blank */
392 case 0x00158: /* Line timing: line time */
393 case 0x00160: /* Frame store: line start */
394 case 0x00168: /* vram cycle: mem init */
395 case 0x00170: /* vram cycle: transfer delay */
396 case 0x00200: /* vram cycle: mask register */
397 /* ignore */
398 break;
399 case REG_TOP:
400 s->top_of_screen = val;
401 g364fb_invalidate_display(s);
402 break;
403 case REG_DISPLAY:
404 s->width = val * 4;
405 break;
406 case REG_VDISPLAY:
407 s->height = val / 2;
408 break;
409 case REG_CTLA:
410 s->ctla = val;
411 g364fb_update_depth(s);
412 g364fb_invalidate_display(s);
413 break;
414 case REG_CURS_POS:
415 g364_invalidate_cursor_position(s);
416 s->cursor_position = val;
417 g364_invalidate_cursor_position(s);
418 break;
419 case REG_RESET:
420 g364fb_reset(s);
421 break;
422 default:
423 error_report("g364: invalid write of 0x%" PRIx64
424 " at [" TARGET_FMT_plx "]", val, addr);
425 break;
1fc3d392
AJ
426 }
427 }
0add30cf 428 qemu_irq_lower(s->irq);
1fc3d392
AJ
429}
430
97a3f6ff
HP
431static const MemoryRegionOps g364fb_ctrl_ops = {
432 .read = g364fb_ctrl_read,
433 .write = g364fb_ctrl_write,
434 .endianness = DEVICE_LITTLE_ENDIAN,
435 .impl.min_access_size = 4,
436 .impl.max_access_size = 4,
1fc3d392
AJ
437};
438
97a3f6ff 439static int g364fb_post_load(void *opaque, int version_id)
1fc3d392
AJ
440{
441 G364State *s = opaque;
0add30cf
AJ
442
443 /* force refresh */
444 g364fb_update_depth(s);
445 g364fb_invalidate_display(s);
1fc3d392 446
0add30cf 447 return 0;
1fc3d392
AJ
448}
449
97a3f6ff
HP
450static const VMStateDescription vmstate_g364fb = {
451 .name = "g364fb",
452 .version_id = 1,
453 .minimum_version_id = 1,
97a3f6ff
HP
454 .post_load = g364fb_post_load,
455 .fields = (VMStateField[]) {
59046ec2 456 VMSTATE_VBUFFER_UINT32(vram, G364State, 1, NULL, vram_size),
97a3f6ff
HP
457 VMSTATE_BUFFER_UNSAFE(color_palette, G364State, 0, 256 * 3),
458 VMSTATE_BUFFER_UNSAFE(cursor_palette, G364State, 0, 9),
459 VMSTATE_UINT16_ARRAY(cursor, G364State, 512),
460 VMSTATE_UINT32(cursor_position, G364State),
461 VMSTATE_UINT32(ctla, G364State),
462 VMSTATE_UINT32(top_of_screen, G364State),
463 VMSTATE_UINT32(width, G364State),
464 VMSTATE_UINT32(height, G364State),
465 VMSTATE_END_OF_LIST()
466 }
467};
1fc3d392 468
380cd056
GH
469static const GraphicHwOps g364fb_ops = {
470 .invalidate = g364fb_invalidate_display,
471 .gfx_update = g364fb_update_display,
472};
473
97a3f6ff 474static void g364fb_init(DeviceState *dev, G364State *s)
1fc3d392 475{
97a3f6ff 476 s->vram = g_malloc0(s->vram_size);
1fc3d392 477
5643706a 478 s->con = graphic_console_init(dev, 0, &g364fb_ops, s);
1fc3d392 479
2c9b15ca
PB
480 memory_region_init_io(&s->mem_ctrl, NULL, &g364fb_ctrl_ops, s, "ctrl", 0x180000);
481 memory_region_init_ram_ptr(&s->mem_vram, NULL, "vram",
97a3f6ff 482 s->vram_size, s->vram);
c5705a77 483 vmstate_register_ram(&s->mem_vram, dev);
74259ae5 484 memory_region_set_log(&s->mem_vram, true, DIRTY_MEMORY_VGA);
97a3f6ff
HP
485}
486
0f31aa86
AF
487#define TYPE_G364 "sysbus-g364"
488#define G364(obj) OBJECT_CHECK(G364SysBusState, (obj), TYPE_G364)
489
97a3f6ff 490typedef struct {
0f31aa86
AF
491 SysBusDevice parent_obj;
492
97a3f6ff
HP
493 G364State g364;
494} G364SysBusState;
1fc3d392 495
0323ee43 496static void g364fb_sysbus_realize(DeviceState *dev, Error **errp)
97a3f6ff 497{
0f31aa86
AF
498 G364SysBusState *sbs = G364(dev);
499 G364State *s = &sbs->g364;
0323ee43 500 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
97a3f6ff 501
0f31aa86
AF
502 g364fb_init(dev, s);
503 sysbus_init_irq(sbd, &s->irq);
504 sysbus_init_mmio(sbd, &s->mem_ctrl);
505 sysbus_init_mmio(sbd, &s->mem_vram);
1fc3d392 506}
97a3f6ff
HP
507
508static void g364fb_sysbus_reset(DeviceState *d)
509{
0f31aa86
AF
510 G364SysBusState *s = G364(d);
511
97a3f6ff
HP
512 g364fb_reset(&s->g364);
513}
514
999e12bb 515static Property g364fb_sysbus_properties[] = {
f0353b0d 516 DEFINE_PROP_UINT32("vram_size", G364SysBusState, g364.vram_size, 8 * MiB),
999e12bb
AL
517 DEFINE_PROP_END_OF_LIST(),
518};
519
520static void g364fb_sysbus_class_init(ObjectClass *klass, void *data)
521{
39bffca2 522 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 523
0323ee43 524 dc->realize = g364fb_sysbus_realize;
125ee0ed 525 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
39bffca2
AL
526 dc->desc = "G364 framebuffer";
527 dc->reset = g364fb_sysbus_reset;
528 dc->vmsd = &vmstate_g364fb;
529 dc->props = g364fb_sysbus_properties;
999e12bb
AL
530}
531
8c43a6f0 532static const TypeInfo g364fb_sysbus_info = {
0f31aa86 533 .name = TYPE_G364,
39bffca2
AL
534 .parent = TYPE_SYS_BUS_DEVICE,
535 .instance_size = sizeof(G364SysBusState),
536 .class_init = g364fb_sysbus_class_init,
97a3f6ff
HP
537};
538
83f7d43a 539static void g364fb_register_types(void)
97a3f6ff 540{
39bffca2 541 type_register_static(&g364fb_sysbus_info);
97a3f6ff
HP
542}
543
83f7d43a 544type_init(g364fb_register_types)
This page took 0.926839 seconds and 4 git commands to generate.