1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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"
34
35#include <X11/Xlib.h>
36#include <epoxy/gl.h>
37#include <epoxy/glx.h>
38
39enum {
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
60};
61
62enum {
63 CTL_START_BUSY = (1<<0),
64 CTL_CHROMAKEY = (1<<1),
65};
66
67enum {
68 MAX_BRIGHTNESS = 63,
69 MAX_ALPHA = 63,
70};
71
72enum {
73 MESH_MAXSIZE = 128,
74};
75
76struct vertex {
77 int x;
78 int y;
79} QEMU_PACKED;
80
81#define TYPE_MILKYMIST_TMU2 "milkymist-tmu2"
82#define MILKYMIST_TMU2(obj) \
83 OBJECT_CHECK(MilkymistTMU2State, (obj), TYPE_MILKYMIST_TMU2)
84
85struct MilkymistTMU2State {
86 SysBusDevice parent_obj;
87
88 MemoryRegion regs_region;
89 Chardev *chr;
90 qemu_irq irq;
91
92 uint32_t regs[R_MAX];
93
94 Display *dpy;
95 GLXFBConfig glx_fb_config;
96 GLXContext glx_context;
97};
98typedef struct MilkymistTMU2State MilkymistTMU2State;
99
100static const int glx_fbconfig_attr[] = {
101 GLX_GREEN_SIZE, 5,
102 GLX_GREEN_SIZE, 6,
103 GLX_BLUE_SIZE, 5,
104 None
105};
106
107static int tmu2_glx_init(MilkymistTMU2State *s)
108{
109 GLXFBConfig *configs;
110 int nelements;
111
112 s->dpy = XOpenDisplay(NULL);
113 if (s->dpy == NULL) {
114 return 1;
115 }
116
117 configs = glXChooseFBConfig(s->dpy, 0, glx_fbconfig_attr, &nelements);
118 if (configs == NULL) {
119 return 1;
120 }
121
122 s->glx_fb_config = *configs;
123 XFree(configs);
124
125
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;
130 }
131
132 return 0;
133}
134
135static void tmu2_gl_map(struct vertex *mesh, int texhres, int texvres,
136 int hmeshlast, int vmeshlast, int ho, int vo, int sw, int sh)
137{
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));
143
144 glLoadIdentity();
145 glTranslatef(ho, vo, 0);
146 glEnable(GL_TEXTURE_2D);
147 glBegin(GL_QUADS);
148
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;
155
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);
164
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);
173 }
174 }
175
176 glEnd();
177}
178
179static void tmu2_start(MilkymistTMU2State *s)
180{
181 int pbuffer_attrib[6] = {
182 GLX_PBUFFER_WIDTH,
183 0,
184 GLX_PBUFFER_HEIGHT,
185 0,
186 GLX_PRESERVED_CONTENTS,
187 True
188 };
189
190 GLXPbuffer pbuffer;
191 GLuint texture;
192 void *fb;
193 hwaddr fb_len;
194 void *mesh;
195 hwaddr mesh_len;
196 float m;
197
198 trace_milkymist_tmu2_start();
199
200
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);
205
206
207 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
208 glPixelStorei(GL_PACK_SWAP_BYTES, 1);
209
210
211 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
212 glPixelStorei(GL_PACK_ALIGNMENT, 2);
213
214
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;
224 }
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);
228
229
230
231
232
233
234
235
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);
242 }
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);
247 }
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);
252 }
253
254
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);
259
260
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;
268 }
269
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);
278
279
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;
287 }
288
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);
295
296
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;
304 }
305
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);
309
310
311 glDeleteTextures(1, &texture);
312 glXMakeContextCurrent(s->dpy, None, None, NULL);
313 glXDestroyPbuffer(s->dpy, pbuffer);
314
315 s->regs[R_CTL] &= ~CTL_START_BUSY;
316
317 trace_milkymist_tmu2_pulse_irq();
318 qemu_irq_pulse(s->irq);
319}
320
321static uint64_t tmu2_read(void *opaque, hwaddr addr,
322 unsigned size)
323{
324 MilkymistTMU2State *s = opaque;
325 uint32_t r = 0;
326
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;
350
351 default:
352 error_report("milkymist_tmu2: read access to unknown register 0x"
353 TARGET_FMT_plx, addr << 2);
354 break;
355 }
356
357 trace_milkymist_tmu2_memory_read(addr << 2, r);
358
359 return r;
360}
361
362static void tmu2_check_registers(MilkymistTMU2State *s)
363{
364 if (s->regs[R_BRIGHTNESS] > MAX_BRIGHTNESS) {
365 error_report("milkymist_tmu2: max brightness is %d", MAX_BRIGHTNESS);
366 }
367
368 if (s->regs[R_ALPHA] > MAX_ALPHA) {
369 error_report("milkymist_tmu2: max alpha is %d", MAX_ALPHA);
370 }
371
372 if (s->regs[R_VERTICESADDR] & 0x07) {
373 error_report("milkymist_tmu2: vertex mesh address has to be 64-bit "
374 "aligned");
375 }
376
377 if (s->regs[R_TEXFBUF] & 0x01) {
378 error_report("milkymist_tmu2: texture buffer address has to be "
379 "16-bit aligned");
380 }
381}
382
383static void tmu2_write(void *opaque, hwaddr addr, uint64_t value,
384 unsigned size)
385{
386 MilkymistTMU2State *s = opaque;
387
388 trace_milkymist_tmu2_memory_write(addr, value);
389
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);
396 }
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;
418
419 default:
420 error_report("milkymist_tmu2: write access to unknown register 0x"
421 TARGET_FMT_plx, addr << 2);
422 break;
423 }
424
425 tmu2_check_registers(s);
426}
427
428static 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,
434 },
435 .endianness = DEVICE_NATIVE_ENDIAN,
436};
437
438static void milkymist_tmu2_reset(DeviceState *d)
439{
440 MilkymistTMU2State *s = MILKYMIST_TMU2(d);
441 int i;
442
443 for (i = 0; i < R_MAX; i++) {
444 s->regs[i] = 0;
445 }
446}
447
448static void milkymist_tmu2_init(Object *obj)
449{
450 MilkymistTMU2State *s = MILKYMIST_TMU2(obj);
451 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
452
453 sysbus_init_irq(dev, &s->irq);
454
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);
458}
459
460static void milkymist_tmu2_realize(DeviceState *dev, Error **errp)
461{
462 MilkymistTMU2State *s = MILKYMIST_TMU2(dev);
463
464 if (tmu2_glx_init(s)) {
465 error_setg(errp, "tmu2_glx_init failed");
466 }
467}
468
469static 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()
476 }
477};
478
479static void milkymist_tmu2_class_init(ObjectClass *klass, void *data)
480{
481 DeviceClass *dc = DEVICE_CLASS(klass);
482
483 dc->realize = milkymist_tmu2_realize;
484 dc->reset = milkymist_tmu2_reset;
485 dc->vmsd = &vmstate_milkymist_tmu2;
486}
487
488static 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,
494};
495
496static void milkymist_tmu2_register_types(void)
497{
498 type_register_static(&milkymist_tmu2_info);
499}
500
501type_init(milkymist_tmu2_register_types)
502