2 * QEMU model of the Milkymist texture mapping unit.
5 * Copyright (c) 2010 Sebastien Bourdeauducq
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"
29 #include "hw/sysbus.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
37 #include <epoxy/glx.h>
63 CTL_START_BUSY = (1<<0),
64 CTL_CHROMAKEY = (1<<1),
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;
95 GLXFBConfig glx_fb_config;
96 GLXContext glx_context;
98 typedef struct MilkymistTMU2State MilkymistTMU2State;
100 static const int glx_fbconfig_attr[] = {
107 static int tmu2_glx_init(MilkymistTMU2State *s)
109 GLXFBConfig *configs;
112 s->dpy = XOpenDisplay(NULL); /* FIXME: call XCloseDisplay() */
113 if (s->dpy == NULL) {
117 configs = glXChooseFBConfig(s->dpy, 0, glx_fbconfig_attr, &nelements);
118 if (configs == NULL) {
122 s->glx_fb_config = *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) {
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)
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));
145 glTranslatef(ho, vo, 0);
146 glEnable(GL_TEXTURE_2D);
149 for (y = 0; y < vmeshlast; y++) {
152 for (x = 0; x < hmeshlast; x++) {
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);
179 static void tmu2_start(MilkymistTMU2State *s)
181 int pbuffer_attrib[6] = {
186 GLX_PRESERVED_CONTENTS,
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);
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);
220 glDeleteTextures(1, &texture);
221 glXMakeContextCurrent(s->dpy, None, None, NULL);
222 glXDestroyPbuffer(s->dpy, pbuffer);
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 */
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);
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);
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);
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 /* Translucency and decay */
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);
264 glDeleteTextures(1, &texture);
265 glXMakeContextCurrent(s->dpy, None, None, NULL);
266 glXDestroyPbuffer(s->dpy, pbuffer);
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);
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);
283 glDeleteTextures(1, &texture);
284 glXMakeContextCurrent(s->dpy, None, None, NULL);
285 glXDestroyPbuffer(s->dpy, pbuffer);
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);
300 glDeleteTextures(1, &texture);
301 glXMakeContextCurrent(s->dpy, None, None, NULL);
302 glXDestroyPbuffer(s->dpy, pbuffer);
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,
324 MilkymistTMU2State *s = opaque;
352 error_report("milkymist_tmu2: read access to unknown register 0x"
353 TARGET_FMT_plx, addr << 2);
357 trace_milkymist_tmu2_memory_read(addr << 2, 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 "
377 if (s->regs[R_TEXFBUF] & 0x01) {
378 error_report("milkymist_tmu2: texture buffer address has to be "
383 static void tmu2_write(void *opaque, hwaddr addr, uint64_t value,
386 MilkymistTMU2State *s = opaque;
388 trace_milkymist_tmu2_memory_write(addr, value);
393 s->regs[addr] = value;
394 if (value & CTL_START_BUSY) {
416 s->regs[addr] = value;
420 error_report("milkymist_tmu2: write access to unknown register 0x"
421 TARGET_FMT_plx, addr << 2);
425 tmu2_check_registers(s);
428 static const MemoryRegionOps tmu2_mmio_ops = {
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);
443 for (i = 0; i < R_MAX; i++) {
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",
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)