sd: limit 'req.cmd' while using as an array index
[qemu/kevin.git] / hw / display / milkymist-tmu2.c
blob9bc88f93b687322a22aa43a8c2b370d67cf5a45a
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://www.milkymist.org/socdoc/tmu2.pdf
27 #include "qemu/osdep.h"
28 #include "hw/hw.h"
29 #include "hw/sysbus.h"
30 #include "trace.h"
31 #include "qemu/error-report.h"
33 #include <X11/Xlib.h>
34 #include <epoxy/gl.h>
35 #include <epoxy/glx.h>
37 enum {
38 R_CTL = 0,
39 R_HMESHLAST,
40 R_VMESHLAST,
41 R_BRIGHTNESS,
42 R_CHROMAKEY,
43 R_VERTICESADDR,
44 R_TEXFBUF,
45 R_TEXHRES,
46 R_TEXVRES,
47 R_TEXHMASK,
48 R_TEXVMASK,
49 R_DSTFBUF,
50 R_DSTHRES,
51 R_DSTVRES,
52 R_DSTHOFFSET,
53 R_DSTVOFFSET,
54 R_DSTSQUAREW,
55 R_DSTSQUAREH,
56 R_ALPHA,
57 R_MAX
60 enum {
61 CTL_START_BUSY = (1<<0),
62 CTL_CHROMAKEY = (1<<1),
65 enum {
66 MAX_BRIGHTNESS = 63,
67 MAX_ALPHA = 63,
70 enum {
71 MESH_MAXSIZE = 128,
74 struct vertex {
75 int x;
76 int y;
77 } QEMU_PACKED;
79 #define TYPE_MILKYMIST_TMU2 "milkymist-tmu2"
80 #define MILKYMIST_TMU2(obj) \
81 OBJECT_CHECK(MilkymistTMU2State, (obj), TYPE_MILKYMIST_TMU2)
83 struct MilkymistTMU2State {
84 SysBusDevice parent_obj;
86 MemoryRegion regs_region;
87 CharDriverState *chr;
88 qemu_irq irq;
90 uint32_t regs[R_MAX];
92 Display *dpy;
93 GLXFBConfig glx_fb_config;
94 GLXContext glx_context;
96 typedef struct MilkymistTMU2State MilkymistTMU2State;
98 static const int glx_fbconfig_attr[] = {
99 GLX_GREEN_SIZE, 5,
100 GLX_GREEN_SIZE, 6,
101 GLX_BLUE_SIZE, 5,
102 None
105 static int tmu2_glx_init(MilkymistTMU2State *s)
107 GLXFBConfig *configs;
108 int nelements;
110 s->dpy = XOpenDisplay(NULL); /* FIXME: call XCloseDisplay() */
111 if (s->dpy == NULL) {
112 return 1;
115 configs = glXChooseFBConfig(s->dpy, 0, glx_fbconfig_attr, &nelements);
116 if (configs == NULL) {
117 return 1;
120 s->glx_fb_config = *configs;
121 XFree(configs);
123 /* FIXME: call glXDestroyContext() */
124 s->glx_context = glXCreateNewContext(s->dpy, s->glx_fb_config,
125 GLX_RGBA_TYPE, NULL, 1);
126 if (s->glx_context == NULL) {
127 return 1;
130 return 0;
133 static void tmu2_gl_map(struct vertex *mesh, int texhres, int texvres,
134 int hmeshlast, int vmeshlast, int ho, int vo, int sw, int sh)
136 int x, y;
137 int x0, y0, x1, y1;
138 int u0, v0, u1, v1, u2, v2, u3, v3;
139 double xscale = 1.0 / ((double)(64 * texhres));
140 double yscale = 1.0 / ((double)(64 * texvres));
142 glLoadIdentity();
143 glTranslatef(ho, vo, 0);
144 glEnable(GL_TEXTURE_2D);
145 glBegin(GL_QUADS);
147 for (y = 0; y < vmeshlast; y++) {
148 y0 = y * sh;
149 y1 = y0 + sh;
150 for (x = 0; x < hmeshlast; x++) {
151 x0 = x * sw;
152 x1 = x0 + sw;
154 u0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].x);
155 v0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].y);
156 u1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].x);
157 v1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].y);
158 u2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].x);
159 v2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].y);
160 u3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].x);
161 v3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].y);
163 glTexCoord2d(((double)u0) * xscale, ((double)v0) * yscale);
164 glVertex3i(x0, y0, 0);
165 glTexCoord2d(((double)u1) * xscale, ((double)v1) * yscale);
166 glVertex3i(x1, y0, 0);
167 glTexCoord2d(((double)u2) * xscale, ((double)v2) * yscale);
168 glVertex3i(x1, y1, 0);
169 glTexCoord2d(((double)u3) * xscale, ((double)v3) * yscale);
170 glVertex3i(x0, y1, 0);
174 glEnd();
177 static void tmu2_start(MilkymistTMU2State *s)
179 int pbuffer_attrib[6] = {
180 GLX_PBUFFER_WIDTH,
182 GLX_PBUFFER_HEIGHT,
184 GLX_PRESERVED_CONTENTS,
185 True
188 GLXPbuffer pbuffer;
189 GLuint texture;
190 void *fb;
191 hwaddr fb_len;
192 void *mesh;
193 hwaddr mesh_len;
194 float m;
196 trace_milkymist_tmu2_start();
198 /* Create and set up a suitable OpenGL context */
199 pbuffer_attrib[1] = s->regs[R_DSTHRES];
200 pbuffer_attrib[3] = s->regs[R_DSTVRES];
201 pbuffer = glXCreatePbuffer(s->dpy, s->glx_fb_config, pbuffer_attrib);
202 glXMakeContextCurrent(s->dpy, pbuffer, pbuffer, s->glx_context);
204 /* Fixup endianness. TODO: would it work on BE hosts? */
205 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
206 glPixelStorei(GL_PACK_SWAP_BYTES, 1);
208 /* Row alignment */
209 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
210 glPixelStorei(GL_PACK_ALIGNMENT, 2);
212 /* Read the QEMU source framebuffer into an OpenGL texture */
213 glGenTextures(1, &texture);
214 glBindTexture(GL_TEXTURE_2D, texture);
215 fb_len = 2*s->regs[R_TEXHRES]*s->regs[R_TEXVRES];
216 fb = cpu_physical_memory_map(s->regs[R_TEXFBUF], &fb_len, 0);
217 if (fb == NULL) {
218 glDeleteTextures(1, &texture);
219 glXMakeContextCurrent(s->dpy, None, None, NULL);
220 glXDestroyPbuffer(s->dpy, pbuffer);
221 return;
223 glTexImage2D(GL_TEXTURE_2D, 0, 3, s->regs[R_TEXHRES], s->regs[R_TEXVRES],
224 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
225 cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
227 /* Set up texturing options */
228 /* WARNING:
229 * Many cases of TMU2 masking are not supported by OpenGL.
230 * We only implement the most common ones:
231 * - full bilinear filtering vs. nearest texel
232 * - texture clamping vs. texture wrapping
234 if ((s->regs[R_TEXHMASK] & 0x3f) > 0x20) {
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
237 } else {
238 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
239 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
241 if ((s->regs[R_TEXHMASK] >> 6) & s->regs[R_TEXHRES]) {
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
243 } else {
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
246 if ((s->regs[R_TEXVMASK] >> 6) & s->regs[R_TEXVRES]) {
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
248 } else {
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
252 /* Translucency and decay */
253 glEnable(GL_BLEND);
254 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
255 m = (float)(s->regs[R_BRIGHTNESS] + 1) / 64.0f;
256 glColor4f(m, m, m, (float)(s->regs[R_ALPHA] + 1) / 64.0f);
258 /* Read the QEMU dest. framebuffer into the OpenGL framebuffer */
259 fb_len = 2 * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
260 fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 0);
261 if (fb == NULL) {
262 glDeleteTextures(1, &texture);
263 glXMakeContextCurrent(s->dpy, None, None, NULL);
264 glXDestroyPbuffer(s->dpy, pbuffer);
265 return;
268 glDrawPixels(s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
269 GL_UNSIGNED_SHORT_5_6_5, fb);
270 cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
271 glViewport(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES]);
272 glMatrixMode(GL_PROJECTION);
273 glLoadIdentity();
274 glOrtho(0.0, s->regs[R_DSTHRES], 0.0, s->regs[R_DSTVRES], -1.0, 1.0);
275 glMatrixMode(GL_MODELVIEW);
277 /* Map the texture */
278 mesh_len = MESH_MAXSIZE*MESH_MAXSIZE*sizeof(struct vertex);
279 mesh = cpu_physical_memory_map(s->regs[R_VERTICESADDR], &mesh_len, 0);
280 if (mesh == NULL) {
281 glDeleteTextures(1, &texture);
282 glXMakeContextCurrent(s->dpy, None, None, NULL);
283 glXDestroyPbuffer(s->dpy, pbuffer);
284 return;
287 tmu2_gl_map((struct vertex *)mesh,
288 s->regs[R_TEXHRES], s->regs[R_TEXVRES],
289 s->regs[R_HMESHLAST], s->regs[R_VMESHLAST],
290 s->regs[R_DSTHOFFSET], s->regs[R_DSTVOFFSET],
291 s->regs[R_DSTSQUAREW], s->regs[R_DSTSQUAREH]);
292 cpu_physical_memory_unmap(mesh, mesh_len, 0, mesh_len);
294 /* Write back the OpenGL framebuffer to the QEMU framebuffer */
295 fb_len = 2 * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
296 fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 1);
297 if (fb == NULL) {
298 glDeleteTextures(1, &texture);
299 glXMakeContextCurrent(s->dpy, None, None, NULL);
300 glXDestroyPbuffer(s->dpy, pbuffer);
301 return;
304 glReadPixels(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
305 GL_UNSIGNED_SHORT_5_6_5, fb);
306 cpu_physical_memory_unmap(fb, fb_len, 1, fb_len);
308 /* Free OpenGL allocs */
309 glDeleteTextures(1, &texture);
310 glXMakeContextCurrent(s->dpy, None, None, NULL);
311 glXDestroyPbuffer(s->dpy, pbuffer);
313 s->regs[R_CTL] &= ~CTL_START_BUSY;
315 trace_milkymist_tmu2_pulse_irq();
316 qemu_irq_pulse(s->irq);
319 static uint64_t tmu2_read(void *opaque, hwaddr addr,
320 unsigned size)
322 MilkymistTMU2State *s = opaque;
323 uint32_t r = 0;
325 addr >>= 2;
326 switch (addr) {
327 case R_CTL:
328 case R_HMESHLAST:
329 case R_VMESHLAST:
330 case R_BRIGHTNESS:
331 case R_CHROMAKEY:
332 case R_VERTICESADDR:
333 case R_TEXFBUF:
334 case R_TEXHRES:
335 case R_TEXVRES:
336 case R_TEXHMASK:
337 case R_TEXVMASK:
338 case R_DSTFBUF:
339 case R_DSTHRES:
340 case R_DSTVRES:
341 case R_DSTHOFFSET:
342 case R_DSTVOFFSET:
343 case R_DSTSQUAREW:
344 case R_DSTSQUAREH:
345 case R_ALPHA:
346 r = s->regs[addr];
347 break;
349 default:
350 error_report("milkymist_tmu2: read access to unknown register 0x"
351 TARGET_FMT_plx, addr << 2);
352 break;
355 trace_milkymist_tmu2_memory_read(addr << 2, r);
357 return r;
360 static void tmu2_check_registers(MilkymistTMU2State *s)
362 if (s->regs[R_BRIGHTNESS] > MAX_BRIGHTNESS) {
363 error_report("milkymist_tmu2: max brightness is %d", MAX_BRIGHTNESS);
366 if (s->regs[R_ALPHA] > MAX_ALPHA) {
367 error_report("milkymist_tmu2: max alpha is %d", MAX_ALPHA);
370 if (s->regs[R_VERTICESADDR] & 0x07) {
371 error_report("milkymist_tmu2: vertex mesh address has to be 64-bit "
372 "aligned");
375 if (s->regs[R_TEXFBUF] & 0x01) {
376 error_report("milkymist_tmu2: texture buffer address has to be "
377 "16-bit aligned");
381 static void tmu2_write(void *opaque, hwaddr addr, uint64_t value,
382 unsigned size)
384 MilkymistTMU2State *s = opaque;
386 trace_milkymist_tmu2_memory_write(addr, value);
388 addr >>= 2;
389 switch (addr) {
390 case R_CTL:
391 s->regs[addr] = value;
392 if (value & CTL_START_BUSY) {
393 tmu2_start(s);
395 break;
396 case R_BRIGHTNESS:
397 case R_HMESHLAST:
398 case R_VMESHLAST:
399 case R_CHROMAKEY:
400 case R_VERTICESADDR:
401 case R_TEXFBUF:
402 case R_TEXHRES:
403 case R_TEXVRES:
404 case R_TEXHMASK:
405 case R_TEXVMASK:
406 case R_DSTFBUF:
407 case R_DSTHRES:
408 case R_DSTVRES:
409 case R_DSTHOFFSET:
410 case R_DSTVOFFSET:
411 case R_DSTSQUAREW:
412 case R_DSTSQUAREH:
413 case R_ALPHA:
414 s->regs[addr] = value;
415 break;
417 default:
418 error_report("milkymist_tmu2: write access to unknown register 0x"
419 TARGET_FMT_plx, addr << 2);
420 break;
423 tmu2_check_registers(s);
426 static const MemoryRegionOps tmu2_mmio_ops = {
427 .read = tmu2_read,
428 .write = tmu2_write,
429 .valid = {
430 .min_access_size = 4,
431 .max_access_size = 4,
433 .endianness = DEVICE_NATIVE_ENDIAN,
436 static void milkymist_tmu2_reset(DeviceState *d)
438 MilkymistTMU2State *s = MILKYMIST_TMU2(d);
439 int i;
441 for (i = 0; i < R_MAX; i++) {
442 s->regs[i] = 0;
446 static int milkymist_tmu2_init(SysBusDevice *dev)
448 MilkymistTMU2State *s = MILKYMIST_TMU2(dev);
450 if (tmu2_glx_init(s)) {
451 return 1;
454 sysbus_init_irq(dev, &s->irq);
456 memory_region_init_io(&s->regs_region, OBJECT(s), &tmu2_mmio_ops, s,
457 "milkymist-tmu2", R_MAX * 4);
458 sysbus_init_mmio(dev, &s->regs_region);
460 return 0;
463 static const VMStateDescription vmstate_milkymist_tmu2 = {
464 .name = "milkymist-tmu2",
465 .version_id = 1,
466 .minimum_version_id = 1,
467 .fields = (VMStateField[]) {
468 VMSTATE_UINT32_ARRAY(regs, MilkymistTMU2State, R_MAX),
469 VMSTATE_END_OF_LIST()
473 static void milkymist_tmu2_class_init(ObjectClass *klass, void *data)
475 DeviceClass *dc = DEVICE_CLASS(klass);
476 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
478 k->init = milkymist_tmu2_init;
479 dc->reset = milkymist_tmu2_reset;
480 dc->vmsd = &vmstate_milkymist_tmu2;
483 static const TypeInfo milkymist_tmu2_info = {
484 .name = TYPE_MILKYMIST_TMU2,
485 .parent = TYPE_SYS_BUS_DEVICE,
486 .instance_size = sizeof(MilkymistTMU2State),
487 .class_init = milkymist_tmu2_class_init,
490 static void milkymist_tmu2_register_types(void)
492 type_register_static(&milkymist_tmu2_info);
495 type_init(milkymist_tmu2_register_types)