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.1 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"
29 #include "hw/sysbus.h"
30 #include "migration/vmstate.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "qemu/module.h"
35 #include "qapi/error.h"
36 #include "hw/display/milkymist_tmu2.h"
40 #include <epoxy/glx.h>
41 #include "qom/object.h"
67 CTL_START_BUSY
= (1<<0),
68 CTL_CHROMAKEY
= (1<<1),
85 #define TYPE_MILKYMIST_TMU2 "milkymist-tmu2"
86 OBJECT_DECLARE_SIMPLE_TYPE(MilkymistTMU2State
, MILKYMIST_TMU2
)
88 struct MilkymistTMU2State
{
89 SysBusDevice parent_obj
;
91 MemoryRegion regs_region
;
98 GLXFBConfig glx_fb_config
;
99 GLXContext glx_context
;
102 static const int glx_fbconfig_attr
[] = {
109 static int tmu2_glx_init(MilkymistTMU2State
*s
)
111 GLXFBConfig
*configs
;
114 s
->dpy
= XOpenDisplay(NULL
); /* FIXME: call XCloseDisplay() */
115 if (s
->dpy
== NULL
) {
119 configs
= glXChooseFBConfig(s
->dpy
, 0, glx_fbconfig_attr
, &nelements
);
120 if (configs
== NULL
) {
124 s
->glx_fb_config
= *configs
;
127 /* FIXME: call glXDestroyContext() */
128 s
->glx_context
= glXCreateNewContext(s
->dpy
, s
->glx_fb_config
,
129 GLX_RGBA_TYPE
, NULL
, 1);
130 if (s
->glx_context
== NULL
) {
137 static void tmu2_gl_map(struct vertex
*mesh
, int texhres
, int texvres
,
138 int hmeshlast
, int vmeshlast
, int ho
, int vo
, int sw
, int sh
)
142 int u0
, v0
, u1
, v1
, u2
, v2
, u3
, v3
;
143 double xscale
= 1.0 / ((double)(64 * texhres
));
144 double yscale
= 1.0 / ((double)(64 * texvres
));
147 glTranslatef(ho
, vo
, 0);
148 glEnable(GL_TEXTURE_2D
);
151 for (y
= 0; y
< vmeshlast
; y
++) {
154 for (x
= 0; x
< hmeshlast
; x
++) {
158 u0
= be32_to_cpu(mesh
[MESH_MAXSIZE
* y
+ x
].x
);
159 v0
= be32_to_cpu(mesh
[MESH_MAXSIZE
* y
+ x
].y
);
160 u1
= be32_to_cpu(mesh
[MESH_MAXSIZE
* y
+ x
+ 1].x
);
161 v1
= be32_to_cpu(mesh
[MESH_MAXSIZE
* y
+ x
+ 1].y
);
162 u2
= be32_to_cpu(mesh
[MESH_MAXSIZE
* (y
+ 1) + x
+ 1].x
);
163 v2
= be32_to_cpu(mesh
[MESH_MAXSIZE
* (y
+ 1) + x
+ 1].y
);
164 u3
= be32_to_cpu(mesh
[MESH_MAXSIZE
* (y
+ 1) + x
].x
);
165 v3
= be32_to_cpu(mesh
[MESH_MAXSIZE
* (y
+ 1) + x
].y
);
167 glTexCoord2d(((double)u0
) * xscale
, ((double)v0
) * yscale
);
168 glVertex3i(x0
, y0
, 0);
169 glTexCoord2d(((double)u1
) * xscale
, ((double)v1
) * yscale
);
170 glVertex3i(x1
, y0
, 0);
171 glTexCoord2d(((double)u2
) * xscale
, ((double)v2
) * yscale
);
172 glVertex3i(x1
, y1
, 0);
173 glTexCoord2d(((double)u3
) * xscale
, ((double)v3
) * yscale
);
174 glVertex3i(x0
, y1
, 0);
181 static void tmu2_start(MilkymistTMU2State
*s
)
183 int pbuffer_attrib
[6] = {
188 GLX_PRESERVED_CONTENTS
,
200 trace_milkymist_tmu2_start();
202 /* Create and set up a suitable OpenGL context */
203 pbuffer_attrib
[1] = s
->regs
[R_DSTHRES
];
204 pbuffer_attrib
[3] = s
->regs
[R_DSTVRES
];
205 pbuffer
= glXCreatePbuffer(s
->dpy
, s
->glx_fb_config
, pbuffer_attrib
);
206 glXMakeContextCurrent(s
->dpy
, pbuffer
, pbuffer
, s
->glx_context
);
208 /* Fixup endianness. TODO: would it work on BE hosts? */
209 glPixelStorei(GL_UNPACK_SWAP_BYTES
, 1);
210 glPixelStorei(GL_PACK_SWAP_BYTES
, 1);
213 glPixelStorei(GL_UNPACK_ALIGNMENT
, 2);
214 glPixelStorei(GL_PACK_ALIGNMENT
, 2);
216 /* Read the QEMU source framebuffer into an OpenGL texture */
217 glGenTextures(1, &texture
);
218 glBindTexture(GL_TEXTURE_2D
, texture
);
219 fb_len
= 2ULL * s
->regs
[R_TEXHRES
] * s
->regs
[R_TEXVRES
];
220 fb
= cpu_physical_memory_map(s
->regs
[R_TEXFBUF
], &fb_len
, false);
222 glDeleteTextures(1, &texture
);
223 glXMakeContextCurrent(s
->dpy
, None
, None
, NULL
);
224 glXDestroyPbuffer(s
->dpy
, pbuffer
);
227 glTexImage2D(GL_TEXTURE_2D
, 0, 3, s
->regs
[R_TEXHRES
], s
->regs
[R_TEXVRES
],
228 0, GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, fb
);
229 cpu_physical_memory_unmap(fb
, fb_len
, 0, fb_len
);
231 /* Set up texturing options */
233 * Many cases of TMU2 masking are not supported by OpenGL.
234 * We only implement the most common ones:
235 * - full bilinear filtering vs. nearest texel
236 * - texture clamping vs. texture wrapping
238 if ((s
->regs
[R_TEXHMASK
] & 0x3f) > 0x20) {
239 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
240 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
242 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
243 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
245 if ((s
->regs
[R_TEXHMASK
] >> 6) & s
->regs
[R_TEXHRES
]) {
246 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
248 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
250 if ((s
->regs
[R_TEXVMASK
] >> 6) & s
->regs
[R_TEXVRES
]) {
251 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
253 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
256 /* Translucency and decay */
258 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
259 m
= (float)(s
->regs
[R_BRIGHTNESS
] + 1) / 64.0f
;
260 glColor4f(m
, m
, m
, (float)(s
->regs
[R_ALPHA
] + 1) / 64.0f
);
262 /* Read the QEMU dest. framebuffer into the OpenGL framebuffer */
263 fb_len
= 2ULL * s
->regs
[R_DSTHRES
] * s
->regs
[R_DSTVRES
];
264 fb
= cpu_physical_memory_map(s
->regs
[R_DSTFBUF
], &fb_len
, false);
266 glDeleteTextures(1, &texture
);
267 glXMakeContextCurrent(s
->dpy
, None
, None
, NULL
);
268 glXDestroyPbuffer(s
->dpy
, pbuffer
);
272 glDrawPixels(s
->regs
[R_DSTHRES
], s
->regs
[R_DSTVRES
], GL_RGB
,
273 GL_UNSIGNED_SHORT_5_6_5
, fb
);
274 cpu_physical_memory_unmap(fb
, fb_len
, 0, fb_len
);
275 glViewport(0, 0, s
->regs
[R_DSTHRES
], s
->regs
[R_DSTVRES
]);
276 glMatrixMode(GL_PROJECTION
);
278 glOrtho(0.0, s
->regs
[R_DSTHRES
], 0.0, s
->regs
[R_DSTVRES
], -1.0, 1.0);
279 glMatrixMode(GL_MODELVIEW
);
281 /* Map the texture */
282 mesh_len
= MESH_MAXSIZE
*MESH_MAXSIZE
*sizeof(struct vertex
);
283 mesh
= cpu_physical_memory_map(s
->regs
[R_VERTICESADDR
], &mesh_len
, false);
285 glDeleteTextures(1, &texture
);
286 glXMakeContextCurrent(s
->dpy
, None
, None
, NULL
);
287 glXDestroyPbuffer(s
->dpy
, pbuffer
);
291 tmu2_gl_map((struct vertex
*)mesh
,
292 s
->regs
[R_TEXHRES
], s
->regs
[R_TEXVRES
],
293 s
->regs
[R_HMESHLAST
], s
->regs
[R_VMESHLAST
],
294 s
->regs
[R_DSTHOFFSET
], s
->regs
[R_DSTVOFFSET
],
295 s
->regs
[R_DSTSQUAREW
], s
->regs
[R_DSTSQUAREH
]);
296 cpu_physical_memory_unmap(mesh
, mesh_len
, 0, mesh_len
);
298 /* Write back the OpenGL framebuffer to the QEMU framebuffer */
299 fb_len
= 2ULL * s
->regs
[R_DSTHRES
] * s
->regs
[R_DSTVRES
];
300 fb
= cpu_physical_memory_map(s
->regs
[R_DSTFBUF
], &fb_len
, true);
302 glDeleteTextures(1, &texture
);
303 glXMakeContextCurrent(s
->dpy
, None
, None
, NULL
);
304 glXDestroyPbuffer(s
->dpy
, pbuffer
);
308 glReadPixels(0, 0, s
->regs
[R_DSTHRES
], s
->regs
[R_DSTVRES
], GL_RGB
,
309 GL_UNSIGNED_SHORT_5_6_5
, fb
);
310 cpu_physical_memory_unmap(fb
, fb_len
, 1, fb_len
);
312 /* Free OpenGL allocs */
313 glDeleteTextures(1, &texture
);
314 glXMakeContextCurrent(s
->dpy
, None
, None
, NULL
);
315 glXDestroyPbuffer(s
->dpy
, pbuffer
);
317 s
->regs
[R_CTL
] &= ~CTL_START_BUSY
;
319 trace_milkymist_tmu2_pulse_irq();
320 qemu_irq_pulse(s
->irq
);
323 static uint64_t tmu2_read(void *opaque
, hwaddr addr
,
326 MilkymistTMU2State
*s
= opaque
;
354 error_report("milkymist_tmu2: read access to unknown register 0x"
355 TARGET_FMT_plx
, addr
<< 2);
359 trace_milkymist_tmu2_memory_read(addr
<< 2, r
);
364 static void tmu2_check_registers(MilkymistTMU2State
*s
)
366 if (s
->regs
[R_BRIGHTNESS
] > MAX_BRIGHTNESS
) {
367 error_report("milkymist_tmu2: max brightness is %d", MAX_BRIGHTNESS
);
370 if (s
->regs
[R_ALPHA
] > MAX_ALPHA
) {
371 error_report("milkymist_tmu2: max alpha is %d", MAX_ALPHA
);
374 if (s
->regs
[R_VERTICESADDR
] & 0x07) {
375 error_report("milkymist_tmu2: vertex mesh address has to be 64-bit "
379 if (s
->regs
[R_TEXFBUF
] & 0x01) {
380 error_report("milkymist_tmu2: texture buffer address has to be "
385 static void tmu2_write(void *opaque
, hwaddr addr
, uint64_t value
,
388 MilkymistTMU2State
*s
= opaque
;
390 trace_milkymist_tmu2_memory_write(addr
, value
);
395 s
->regs
[addr
] = value
;
396 if (value
& CTL_START_BUSY
) {
418 s
->regs
[addr
] = value
;
422 error_report("milkymist_tmu2: write access to unknown register 0x"
423 TARGET_FMT_plx
, addr
<< 2);
427 tmu2_check_registers(s
);
430 static const MemoryRegionOps tmu2_mmio_ops
= {
434 .min_access_size
= 4,
435 .max_access_size
= 4,
437 .endianness
= DEVICE_NATIVE_ENDIAN
,
440 static void milkymist_tmu2_reset(DeviceState
*d
)
442 MilkymistTMU2State
*s
= MILKYMIST_TMU2(d
);
445 for (i
= 0; i
< R_MAX
; i
++) {
450 static void milkymist_tmu2_init(Object
*obj
)
452 MilkymistTMU2State
*s
= MILKYMIST_TMU2(obj
);
453 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
455 sysbus_init_irq(dev
, &s
->irq
);
457 memory_region_init_io(&s
->regs_region
, obj
, &tmu2_mmio_ops
, s
,
458 "milkymist-tmu2", R_MAX
* 4);
459 sysbus_init_mmio(dev
, &s
->regs_region
);
462 static void milkymist_tmu2_realize(DeviceState
*dev
, Error
**errp
)
464 MilkymistTMU2State
*s
= MILKYMIST_TMU2(dev
);
466 if (tmu2_glx_init(s
)) {
467 error_setg(errp
, "tmu2_glx_init failed");
471 static const VMStateDescription vmstate_milkymist_tmu2
= {
472 .name
= "milkymist-tmu2",
474 .minimum_version_id
= 1,
475 .fields
= (VMStateField
[]) {
476 VMSTATE_UINT32_ARRAY(regs
, MilkymistTMU2State
, R_MAX
),
477 VMSTATE_END_OF_LIST()
481 static void milkymist_tmu2_class_init(ObjectClass
*klass
, void *data
)
483 DeviceClass
*dc
= DEVICE_CLASS(klass
);
485 dc
->realize
= milkymist_tmu2_realize
;
486 dc
->reset
= milkymist_tmu2_reset
;
487 dc
->vmsd
= &vmstate_milkymist_tmu2
;
490 static const TypeInfo milkymist_tmu2_info
= {
491 .name
= TYPE_MILKYMIST_TMU2
,
492 .parent
= TYPE_SYS_BUS_DEVICE
,
493 .instance_size
= sizeof(MilkymistTMU2State
),
494 .instance_init
= milkymist_tmu2_init
,
495 .class_init
= milkymist_tmu2_class_init
,
498 static void milkymist_tmu2_register_types(void)
500 type_register_static(&milkymist_tmu2_info
);
503 type_init(milkymist_tmu2_register_types
)
505 DeviceState
*milkymist_tmu2_create(hwaddr base
, qemu_irq irq
)
509 GLXFBConfig
*configs
;
511 int ver_major
, ver_minor
;
513 /* check that GLX will work */
514 d
= XOpenDisplay(NULL
);
519 if (!glXQueryVersion(d
, &ver_major
, &ver_minor
)) {
521 * Yeah, sometimes getting the GLX version can fail.
528 if ((ver_major
< 1) || ((ver_major
== 1) && (ver_minor
< 3))) {
529 printf("Your GLX version is %d.%d,"
530 "but TMU emulation needs at least 1.3. TMU disabled.\n",
531 ver_major
, ver_minor
);
536 configs
= glXChooseFBConfig(d
, 0, glx_fbconfig_attr
, &nelements
);
537 if (configs
== NULL
) {
545 dev
= qdev_new(TYPE_MILKYMIST_TMU2
);
546 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
547 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
548 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);