block: Ignore generated job QAPI files
[qemu/ar7.git] / hw / display / milkymist-tmu2.c
blob3ce44fdfce4e84ad409bcf88d4d6c4c51c09eb50
1 /*
2 * QEMU model of the Milkymist texture mapping unit.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 * Copyright (c) 2010 Sebastien Bourdeauducq
6 * <sebastien.bourdeauducq@lekernel.net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Specification available at:
23 * http://milkymist.walle.cc/socdoc/tmu2.pdf
27 #include "qemu/osdep.h"
28 #include "hw/hw.h"
29 #include "hw/sysbus.h"
30 #include "trace.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
35 #include <X11/Xlib.h>
36 #include <epoxy/gl.h>
37 #include <epoxy/glx.h>
39 enum {
40 R_CTL = 0,
41 R_HMESHLAST,
42 R_VMESHLAST,
43 R_BRIGHTNESS,
44 R_CHROMAKEY,
45 R_VERTICESADDR,
46 R_TEXFBUF,
47 R_TEXHRES,
48 R_TEXVRES,
49 R_TEXHMASK,
50 R_TEXVMASK,
51 R_DSTFBUF,
52 R_DSTHRES,
53 R_DSTVRES,
54 R_DSTHOFFSET,
55 R_DSTVOFFSET,
56 R_DSTSQUAREW,
57 R_DSTSQUAREH,
58 R_ALPHA,
59 R_MAX
62 enum {
63 CTL_START_BUSY = (1<<0),
64 CTL_CHROMAKEY = (1<<1),
67 enum {
68 MAX_BRIGHTNESS = 63,
69 MAX_ALPHA = 63,
72 enum {
73 MESH_MAXSIZE = 128,
76 struct vertex {
77 int x;
78 int y;
79 } QEMU_PACKED;
81 #define TYPE_MILKYMIST_TMU2 "milkymist-tmu2"
82 #define MILKYMIST_TMU2(obj) \
83 OBJECT_CHECK(MilkymistTMU2State, (obj), TYPE_MILKYMIST_TMU2)
85 struct MilkymistTMU2State {
86 SysBusDevice parent_obj;
88 MemoryRegion regs_region;
89 Chardev *chr;
90 qemu_irq irq;
92 uint32_t regs[R_MAX];
94 Display *dpy;
95 GLXFBConfig glx_fb_config;
96 GLXContext glx_context;
98 typedef struct MilkymistTMU2State MilkymistTMU2State;
100 static const int glx_fbconfig_attr[] = {
101 GLX_GREEN_SIZE, 5,
102 GLX_GREEN_SIZE, 6,
103 GLX_BLUE_SIZE, 5,
104 None
107 static int tmu2_glx_init(MilkymistTMU2State *s)
109 GLXFBConfig *configs;
110 int nelements;
112 s->dpy = XOpenDisplay(NULL); /* FIXME: call XCloseDisplay() */
113 if (s->dpy == NULL) {
114 return 1;
117 configs = glXChooseFBConfig(s->dpy, 0, glx_fbconfig_attr, &nelements);
118 if (configs == NULL) {
119 return 1;
122 s->glx_fb_config = *configs;
123 XFree(configs);
125 /* FIXME: call glXDestroyContext() */
126 s->glx_context = glXCreateNewContext(s->dpy, s->glx_fb_config,
127 GLX_RGBA_TYPE, NULL, 1);
128 if (s->glx_context == NULL) {
129 return 1;
132 return 0;
135 static void tmu2_gl_map(struct vertex *mesh, int texhres, int texvres,
136 int hmeshlast, int vmeshlast, int ho, int vo, int sw, int sh)
138 int x, y;
139 int x0, y0, x1, y1;
140 int u0, v0, u1, v1, u2, v2, u3, v3;
141 double xscale = 1.0 / ((double)(64 * texhres));
142 double yscale = 1.0 / ((double)(64 * texvres));
144 glLoadIdentity();
145 glTranslatef(ho, vo, 0);
146 glEnable(GL_TEXTURE_2D);
147 glBegin(GL_QUADS);
149 for (y = 0; y < vmeshlast; y++) {
150 y0 = y * sh;
151 y1 = y0 + sh;
152 for (x = 0; x < hmeshlast; x++) {
153 x0 = x * sw;
154 x1 = x0 + sw;
156 u0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].x);
157 v0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].y);
158 u1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].x);
159 v1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].y);
160 u2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].x);
161 v2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].y);
162 u3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].x);
163 v3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].y);
165 glTexCoord2d(((double)u0) * xscale, ((double)v0) * yscale);
166 glVertex3i(x0, y0, 0);
167 glTexCoord2d(((double)u1) * xscale, ((double)v1) * yscale);
168 glVertex3i(x1, y0, 0);
169 glTexCoord2d(((double)u2) * xscale, ((double)v2) * yscale);
170 glVertex3i(x1, y1, 0);
171 glTexCoord2d(((double)u3) * xscale, ((double)v3) * yscale);
172 glVertex3i(x0, y1, 0);
176 glEnd();
179 static void tmu2_start(MilkymistTMU2State *s)
181 int pbuffer_attrib[6] = {
182 GLX_PBUFFER_WIDTH,
184 GLX_PBUFFER_HEIGHT,
186 GLX_PRESERVED_CONTENTS,
187 True
190 GLXPbuffer pbuffer;
191 GLuint texture;
192 void *fb;
193 hwaddr fb_len;
194 void *mesh;
195 hwaddr mesh_len;
196 float m;
198 trace_milkymist_tmu2_start();
200 /* Create and set up a suitable OpenGL context */
201 pbuffer_attrib[1] = s->regs[R_DSTHRES];
202 pbuffer_attrib[3] = s->regs[R_DSTVRES];
203 pbuffer = glXCreatePbuffer(s->dpy, s->glx_fb_config, pbuffer_attrib);
204 glXMakeContextCurrent(s->dpy, pbuffer, pbuffer, s->glx_context);
206 /* Fixup endianness. TODO: would it work on BE hosts? */
207 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
208 glPixelStorei(GL_PACK_SWAP_BYTES, 1);
210 /* Row alignment */
211 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
212 glPixelStorei(GL_PACK_ALIGNMENT, 2);
214 /* Read the QEMU source framebuffer into an OpenGL texture */
215 glGenTextures(1, &texture);
216 glBindTexture(GL_TEXTURE_2D, texture);
217 fb_len = 2ULL * s->regs[R_TEXHRES] * s->regs[R_TEXVRES];
218 fb = cpu_physical_memory_map(s->regs[R_TEXFBUF], &fb_len, 0);
219 if (fb == NULL) {
220 glDeleteTextures(1, &texture);
221 glXMakeContextCurrent(s->dpy, None, None, NULL);
222 glXDestroyPbuffer(s->dpy, pbuffer);
223 return;
225 glTexImage2D(GL_TEXTURE_2D, 0, 3, s->regs[R_TEXHRES], s->regs[R_TEXVRES],
226 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
227 cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
229 /* Set up texturing options */
230 /* WARNING:
231 * Many cases of TMU2 masking are not supported by OpenGL.
232 * We only implement the most common ones:
233 * - full bilinear filtering vs. nearest texel
234 * - texture clamping vs. texture wrapping
236 if ((s->regs[R_TEXHMASK] & 0x3f) > 0x20) {
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
238 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
239 } else {
240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
243 if ((s->regs[R_TEXHMASK] >> 6) & s->regs[R_TEXHRES]) {
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
245 } else {
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
248 if ((s->regs[R_TEXVMASK] >> 6) & s->regs[R_TEXVRES]) {
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
250 } else {
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 /* Translucency and decay */
255 glEnable(GL_BLEND);
256 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
257 m = (float)(s->regs[R_BRIGHTNESS] + 1) / 64.0f;
258 glColor4f(m, m, m, (float)(s->regs[R_ALPHA] + 1) / 64.0f);
260 /* Read the QEMU dest. framebuffer into the OpenGL framebuffer */
261 fb_len = 2ULL * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
262 fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 0);
263 if (fb == NULL) {
264 glDeleteTextures(1, &texture);
265 glXMakeContextCurrent(s->dpy, None, None, NULL);
266 glXDestroyPbuffer(s->dpy, pbuffer);
267 return;
270 glDrawPixels(s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
271 GL_UNSIGNED_SHORT_5_6_5, fb);
272 cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
273 glViewport(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES]);
274 glMatrixMode(GL_PROJECTION);
275 glLoadIdentity();
276 glOrtho(0.0, s->regs[R_DSTHRES], 0.0, s->regs[R_DSTVRES], -1.0, 1.0);
277 glMatrixMode(GL_MODELVIEW);
279 /* Map the texture */
280 mesh_len = MESH_MAXSIZE*MESH_MAXSIZE*sizeof(struct vertex);
281 mesh = cpu_physical_memory_map(s->regs[R_VERTICESADDR], &mesh_len, 0);
282 if (mesh == NULL) {
283 glDeleteTextures(1, &texture);
284 glXMakeContextCurrent(s->dpy, None, None, NULL);
285 glXDestroyPbuffer(s->dpy, pbuffer);
286 return;
289 tmu2_gl_map((struct vertex *)mesh,
290 s->regs[R_TEXHRES], s->regs[R_TEXVRES],
291 s->regs[R_HMESHLAST], s->regs[R_VMESHLAST],
292 s->regs[R_DSTHOFFSET], s->regs[R_DSTVOFFSET],
293 s->regs[R_DSTSQUAREW], s->regs[R_DSTSQUAREH]);
294 cpu_physical_memory_unmap(mesh, mesh_len, 0, mesh_len);
296 /* Write back the OpenGL framebuffer to the QEMU framebuffer */
297 fb_len = 2ULL * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
298 fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 1);
299 if (fb == NULL) {
300 glDeleteTextures(1, &texture);
301 glXMakeContextCurrent(s->dpy, None, None, NULL);
302 glXDestroyPbuffer(s->dpy, pbuffer);
303 return;
306 glReadPixels(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
307 GL_UNSIGNED_SHORT_5_6_5, fb);
308 cpu_physical_memory_unmap(fb, fb_len, 1, fb_len);
310 /* Free OpenGL allocs */
311 glDeleteTextures(1, &texture);
312 glXMakeContextCurrent(s->dpy, None, None, NULL);
313 glXDestroyPbuffer(s->dpy, pbuffer);
315 s->regs[R_CTL] &= ~CTL_START_BUSY;
317 trace_milkymist_tmu2_pulse_irq();
318 qemu_irq_pulse(s->irq);
321 static uint64_t tmu2_read(void *opaque, hwaddr addr,
322 unsigned size)
324 MilkymistTMU2State *s = opaque;
325 uint32_t r = 0;
327 addr >>= 2;
328 switch (addr) {
329 case R_CTL:
330 case R_HMESHLAST:
331 case R_VMESHLAST:
332 case R_BRIGHTNESS:
333 case R_CHROMAKEY:
334 case R_VERTICESADDR:
335 case R_TEXFBUF:
336 case R_TEXHRES:
337 case R_TEXVRES:
338 case R_TEXHMASK:
339 case R_TEXVMASK:
340 case R_DSTFBUF:
341 case R_DSTHRES:
342 case R_DSTVRES:
343 case R_DSTHOFFSET:
344 case R_DSTVOFFSET:
345 case R_DSTSQUAREW:
346 case R_DSTSQUAREH:
347 case R_ALPHA:
348 r = s->regs[addr];
349 break;
351 default:
352 error_report("milkymist_tmu2: read access to unknown register 0x"
353 TARGET_FMT_plx, addr << 2);
354 break;
357 trace_milkymist_tmu2_memory_read(addr << 2, r);
359 return r;
362 static void tmu2_check_registers(MilkymistTMU2State *s)
364 if (s->regs[R_BRIGHTNESS] > MAX_BRIGHTNESS) {
365 error_report("milkymist_tmu2: max brightness is %d", MAX_BRIGHTNESS);
368 if (s->regs[R_ALPHA] > MAX_ALPHA) {
369 error_report("milkymist_tmu2: max alpha is %d", MAX_ALPHA);
372 if (s->regs[R_VERTICESADDR] & 0x07) {
373 error_report("milkymist_tmu2: vertex mesh address has to be 64-bit "
374 "aligned");
377 if (s->regs[R_TEXFBUF] & 0x01) {
378 error_report("milkymist_tmu2: texture buffer address has to be "
379 "16-bit aligned");
383 static void tmu2_write(void *opaque, hwaddr addr, uint64_t value,
384 unsigned size)
386 MilkymistTMU2State *s = opaque;
388 trace_milkymist_tmu2_memory_write(addr, value);
390 addr >>= 2;
391 switch (addr) {
392 case R_CTL:
393 s->regs[addr] = value;
394 if (value & CTL_START_BUSY) {
395 tmu2_start(s);
397 break;
398 case R_BRIGHTNESS:
399 case R_HMESHLAST:
400 case R_VMESHLAST:
401 case R_CHROMAKEY:
402 case R_VERTICESADDR:
403 case R_TEXFBUF:
404 case R_TEXHRES:
405 case R_TEXVRES:
406 case R_TEXHMASK:
407 case R_TEXVMASK:
408 case R_DSTFBUF:
409 case R_DSTHRES:
410 case R_DSTVRES:
411 case R_DSTHOFFSET:
412 case R_DSTVOFFSET:
413 case R_DSTSQUAREW:
414 case R_DSTSQUAREH:
415 case R_ALPHA:
416 s->regs[addr] = value;
417 break;
419 default:
420 error_report("milkymist_tmu2: write access to unknown register 0x"
421 TARGET_FMT_plx, addr << 2);
422 break;
425 tmu2_check_registers(s);
428 static const MemoryRegionOps tmu2_mmio_ops = {
429 .read = tmu2_read,
430 .write = tmu2_write,
431 .valid = {
432 .min_access_size = 4,
433 .max_access_size = 4,
435 .endianness = DEVICE_NATIVE_ENDIAN,
438 static void milkymist_tmu2_reset(DeviceState *d)
440 MilkymistTMU2State *s = MILKYMIST_TMU2(d);
441 int i;
443 for (i = 0; i < R_MAX; i++) {
444 s->regs[i] = 0;
448 static void milkymist_tmu2_init(Object *obj)
450 MilkymistTMU2State *s = MILKYMIST_TMU2(obj);
451 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
453 sysbus_init_irq(dev, &s->irq);
455 memory_region_init_io(&s->regs_region, obj, &tmu2_mmio_ops, s,
456 "milkymist-tmu2", R_MAX * 4);
457 sysbus_init_mmio(dev, &s->regs_region);
460 static void milkymist_tmu2_realize(DeviceState *dev, Error **errp)
462 MilkymistTMU2State *s = MILKYMIST_TMU2(dev);
464 if (tmu2_glx_init(s)) {
465 error_setg(errp, "tmu2_glx_init failed");
469 static const VMStateDescription vmstate_milkymist_tmu2 = {
470 .name = "milkymist-tmu2",
471 .version_id = 1,
472 .minimum_version_id = 1,
473 .fields = (VMStateField[]) {
474 VMSTATE_UINT32_ARRAY(regs, MilkymistTMU2State, R_MAX),
475 VMSTATE_END_OF_LIST()
479 static void milkymist_tmu2_class_init(ObjectClass *klass, void *data)
481 DeviceClass *dc = DEVICE_CLASS(klass);
483 dc->realize = milkymist_tmu2_realize;
484 dc->reset = milkymist_tmu2_reset;
485 dc->vmsd = &vmstate_milkymist_tmu2;
488 static const TypeInfo milkymist_tmu2_info = {
489 .name = TYPE_MILKYMIST_TMU2,
490 .parent = TYPE_SYS_BUS_DEVICE,
491 .instance_size = sizeof(MilkymistTMU2State),
492 .instance_init = milkymist_tmu2_init,
493 .class_init = milkymist_tmu2_class_init,
496 static void milkymist_tmu2_register_types(void)
498 type_register_static(&milkymist_tmu2_info);
501 type_init(milkymist_tmu2_register_types)