Statistics
| Branch: | Revision:

root / hw / milkymist-tmu2.c @ 0dad6c35

History | View | Annotate | Download (13.3 kB)

1 0670dadd Michael Walle
/*
2 0670dadd Michael Walle
 *  QEMU model of the Milkymist texture mapping unit.
3 0670dadd Michael Walle
 *
4 0670dadd Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 0670dadd Michael Walle
 *  Copyright (c) 2010 Sebastien Bourdeauducq
6 0670dadd Michael Walle
 *                       <sebastien.bourdeauducq@lekernel.net>
7 0670dadd Michael Walle
 *
8 0670dadd Michael Walle
 * This library is free software; you can redistribute it and/or
9 0670dadd Michael Walle
 * modify it under the terms of the GNU Lesser General Public
10 0670dadd Michael Walle
 * License as published by the Free Software Foundation; either
11 0670dadd Michael Walle
 * version 2 of the License, or (at your option) any later version.
12 0670dadd Michael Walle
 *
13 0670dadd Michael Walle
 * This library is distributed in the hope that it will be useful,
14 0670dadd Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 0670dadd Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 0670dadd Michael Walle
 * Lesser General Public License for more details.
17 0670dadd Michael Walle
 *
18 0670dadd Michael Walle
 * You should have received a copy of the GNU Lesser General Public
19 0670dadd Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 0670dadd Michael Walle
 *
21 0670dadd Michael Walle
 *
22 0670dadd Michael Walle
 * Specification available at:
23 0670dadd Michael Walle
 *   http://www.milkymist.org/socdoc/tmu2.pdf
24 0670dadd Michael Walle
 *
25 0670dadd Michael Walle
 */
26 0670dadd Michael Walle
27 0670dadd Michael Walle
#include "hw.h"
28 0670dadd Michael Walle
#include "sysbus.h"
29 0670dadd Michael Walle
#include "trace.h"
30 0670dadd Michael Walle
#include "qemu-error.h"
31 0670dadd Michael Walle
32 0670dadd Michael Walle
#include <X11/Xlib.h>
33 0670dadd Michael Walle
#include <GL/gl.h>
34 0670dadd Michael Walle
#include <GL/glx.h>
35 0670dadd Michael Walle
36 0670dadd Michael Walle
enum {
37 0670dadd Michael Walle
    R_CTL = 0,
38 0670dadd Michael Walle
    R_HMESHLAST,
39 0670dadd Michael Walle
    R_VMESHLAST,
40 0670dadd Michael Walle
    R_BRIGHTNESS,
41 0670dadd Michael Walle
    R_CHROMAKEY,
42 0670dadd Michael Walle
    R_VERTICESADDR,
43 0670dadd Michael Walle
    R_TEXFBUF,
44 0670dadd Michael Walle
    R_TEXHRES,
45 0670dadd Michael Walle
    R_TEXVRES,
46 0670dadd Michael Walle
    R_TEXHMASK,
47 0670dadd Michael Walle
    R_TEXVMASK,
48 0670dadd Michael Walle
    R_DSTFBUF,
49 0670dadd Michael Walle
    R_DSTHRES,
50 0670dadd Michael Walle
    R_DSTVRES,
51 0670dadd Michael Walle
    R_DSTHOFFSET,
52 0670dadd Michael Walle
    R_DSTVOFFSET,
53 0670dadd Michael Walle
    R_DSTSQUAREW,
54 0670dadd Michael Walle
    R_DSTSQUAREH,
55 0670dadd Michael Walle
    R_ALPHA,
56 0670dadd Michael Walle
    R_MAX
57 0670dadd Michael Walle
};
58 0670dadd Michael Walle
59 0670dadd Michael Walle
enum {
60 0670dadd Michael Walle
    CTL_START_BUSY  = (1<<0),
61 0670dadd Michael Walle
    CTL_CHROMAKEY   = (1<<1),
62 0670dadd Michael Walle
};
63 0670dadd Michael Walle
64 0670dadd Michael Walle
enum {
65 0670dadd Michael Walle
    MAX_BRIGHTNESS = 63,
66 0670dadd Michael Walle
    MAX_ALPHA      = 63,
67 0670dadd Michael Walle
};
68 0670dadd Michael Walle
69 0670dadd Michael Walle
enum {
70 0670dadd Michael Walle
    MESH_MAXSIZE = 128,
71 0670dadd Michael Walle
};
72 0670dadd Michael Walle
73 0670dadd Michael Walle
struct vertex {
74 0670dadd Michael Walle
    int x;
75 0670dadd Michael Walle
    int y;
76 541dc0d4 Stefan Weil
} QEMU_PACKED;
77 0670dadd Michael Walle
78 0670dadd Michael Walle
struct MilkymistTMU2State {
79 0670dadd Michael Walle
    SysBusDevice busdev;
80 7100453f Michael Walle
    MemoryRegion regs_region;
81 0670dadd Michael Walle
    CharDriverState *chr;
82 0670dadd Michael Walle
    qemu_irq irq;
83 0670dadd Michael Walle
84 0670dadd Michael Walle
    uint32_t regs[R_MAX];
85 0670dadd Michael Walle
86 0670dadd Michael Walle
    Display *dpy;
87 0670dadd Michael Walle
    GLXFBConfig glx_fb_config;
88 0670dadd Michael Walle
    GLXContext glx_context;
89 0670dadd Michael Walle
};
90 0670dadd Michael Walle
typedef struct MilkymistTMU2State MilkymistTMU2State;
91 0670dadd Michael Walle
92 0670dadd Michael Walle
static const int glx_fbconfig_attr[] = {
93 0670dadd Michael Walle
    GLX_GREEN_SIZE, 5,
94 0670dadd Michael Walle
    GLX_GREEN_SIZE, 6,
95 0670dadd Michael Walle
    GLX_BLUE_SIZE, 5,
96 0670dadd Michael Walle
    None
97 0670dadd Michael Walle
};
98 0670dadd Michael Walle
99 0670dadd Michael Walle
static int tmu2_glx_init(MilkymistTMU2State *s)
100 0670dadd Michael Walle
{
101 0670dadd Michael Walle
    GLXFBConfig *configs;
102 0670dadd Michael Walle
    int nelements;
103 0670dadd Michael Walle
104 0670dadd Michael Walle
    s->dpy = XOpenDisplay(NULL); /* FIXME: call XCloseDisplay() */
105 0670dadd Michael Walle
    if (s->dpy == NULL) {
106 0670dadd Michael Walle
        return 1;
107 0670dadd Michael Walle
    }
108 0670dadd Michael Walle
109 0670dadd Michael Walle
    configs = glXChooseFBConfig(s->dpy, 0, glx_fbconfig_attr, &nelements);
110 0670dadd Michael Walle
    if (configs == NULL) {
111 0670dadd Michael Walle
        return 1;
112 0670dadd Michael Walle
    }
113 0670dadd Michael Walle
114 0670dadd Michael Walle
    s->glx_fb_config = *configs;
115 0670dadd Michael Walle
    XFree(configs);
116 0670dadd Michael Walle
117 0670dadd Michael Walle
    /* FIXME: call glXDestroyContext() */
118 0670dadd Michael Walle
    s->glx_context = glXCreateNewContext(s->dpy, s->glx_fb_config,
119 0670dadd Michael Walle
            GLX_RGBA_TYPE, NULL, 1);
120 0670dadd Michael Walle
    if (s->glx_context == NULL) {
121 0670dadd Michael Walle
        return 1;
122 0670dadd Michael Walle
    }
123 0670dadd Michael Walle
124 0670dadd Michael Walle
    return 0;
125 0670dadd Michael Walle
}
126 0670dadd Michael Walle
127 0670dadd Michael Walle
static void tmu2_gl_map(struct vertex *mesh, int texhres, int texvres,
128 0670dadd Michael Walle
        int hmeshlast, int vmeshlast, int ho, int vo, int sw, int sh)
129 0670dadd Michael Walle
{
130 0670dadd Michael Walle
    int x, y;
131 0670dadd Michael Walle
    int x0, y0, x1, y1;
132 0670dadd Michael Walle
    int u0, v0, u1, v1, u2, v2, u3, v3;
133 0670dadd Michael Walle
    double xscale = 1.0 / ((double)(64 * texhres));
134 0670dadd Michael Walle
    double yscale = 1.0 / ((double)(64 * texvres));
135 0670dadd Michael Walle
136 0670dadd Michael Walle
    glLoadIdentity();
137 0670dadd Michael Walle
    glTranslatef(ho, vo, 0);
138 0670dadd Michael Walle
    glEnable(GL_TEXTURE_2D);
139 0670dadd Michael Walle
    glBegin(GL_QUADS);
140 0670dadd Michael Walle
141 0670dadd Michael Walle
    for (y = 0; y < vmeshlast; y++) {
142 0670dadd Michael Walle
        y0 = y * sh;
143 0670dadd Michael Walle
        y1 = y0 + sh;
144 0670dadd Michael Walle
        for (x = 0; x < hmeshlast; x++) {
145 0670dadd Michael Walle
            x0 = x * sw;
146 0670dadd Michael Walle
            x1 = x0 + sw;
147 0670dadd Michael Walle
148 0670dadd Michael Walle
            u0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].x);
149 0670dadd Michael Walle
            v0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].y);
150 0670dadd Michael Walle
            u1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].x);
151 0670dadd Michael Walle
            v1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].y);
152 0670dadd Michael Walle
            u2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].x);
153 0670dadd Michael Walle
            v2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].y);
154 0670dadd Michael Walle
            u3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].x);
155 0670dadd Michael Walle
            v3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].y);
156 0670dadd Michael Walle
157 0670dadd Michael Walle
            glTexCoord2d(((double)u0) * xscale, ((double)v0) * yscale);
158 0670dadd Michael Walle
            glVertex3i(x0, y0, 0);
159 0670dadd Michael Walle
            glTexCoord2d(((double)u1) * xscale, ((double)v1) * yscale);
160 0670dadd Michael Walle
            glVertex3i(x1, y0, 0);
161 0670dadd Michael Walle
            glTexCoord2d(((double)u2) * xscale, ((double)v2) * yscale);
162 0670dadd Michael Walle
            glVertex3i(x1, y1, 0);
163 0670dadd Michael Walle
            glTexCoord2d(((double)u3) * xscale, ((double)v3) * yscale);
164 0670dadd Michael Walle
            glVertex3i(x0, y1, 0);
165 0670dadd Michael Walle
        }
166 0670dadd Michael Walle
    }
167 0670dadd Michael Walle
168 0670dadd Michael Walle
    glEnd();
169 0670dadd Michael Walle
}
170 0670dadd Michael Walle
171 0670dadd Michael Walle
static void tmu2_start(MilkymistTMU2State *s)
172 0670dadd Michael Walle
{
173 0670dadd Michael Walle
    int pbuffer_attrib[6] = {
174 0670dadd Michael Walle
        GLX_PBUFFER_WIDTH,
175 0670dadd Michael Walle
        0,
176 0670dadd Michael Walle
        GLX_PBUFFER_HEIGHT,
177 0670dadd Michael Walle
        0,
178 0670dadd Michael Walle
        GLX_PRESERVED_CONTENTS,
179 0670dadd Michael Walle
        True
180 0670dadd Michael Walle
    };
181 0670dadd Michael Walle
182 0670dadd Michael Walle
    GLXPbuffer pbuffer;
183 0670dadd Michael Walle
    GLuint texture;
184 0670dadd Michael Walle
    void *fb;
185 0670dadd Michael Walle
    target_phys_addr_t fb_len;
186 0670dadd Michael Walle
    void *mesh;
187 0670dadd Michael Walle
    target_phys_addr_t mesh_len;
188 0670dadd Michael Walle
    float m;
189 0670dadd Michael Walle
190 0670dadd Michael Walle
    trace_milkymist_tmu2_start();
191 0670dadd Michael Walle
192 0670dadd Michael Walle
    /* Create and set up a suitable OpenGL context */
193 0670dadd Michael Walle
    pbuffer_attrib[1] = s->regs[R_DSTHRES];
194 0670dadd Michael Walle
    pbuffer_attrib[3] = s->regs[R_DSTVRES];
195 0670dadd Michael Walle
    pbuffer = glXCreatePbuffer(s->dpy, s->glx_fb_config, pbuffer_attrib);
196 0670dadd Michael Walle
    glXMakeContextCurrent(s->dpy, pbuffer, pbuffer, s->glx_context);
197 0670dadd Michael Walle
198 0670dadd Michael Walle
    /* Fixup endianness. TODO: would it work on BE hosts? */
199 0670dadd Michael Walle
    glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
200 0670dadd Michael Walle
    glPixelStorei(GL_PACK_SWAP_BYTES, 1);
201 0670dadd Michael Walle
202 0670dadd Michael Walle
    /* Row alignment */
203 0670dadd Michael Walle
    glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
204 0670dadd Michael Walle
    glPixelStorei(GL_PACK_ALIGNMENT, 2);
205 0670dadd Michael Walle
206 0670dadd Michael Walle
    /* Read the QEMU source framebuffer into an OpenGL texture */
207 0670dadd Michael Walle
    glGenTextures(1, &texture);
208 0670dadd Michael Walle
    glBindTexture(GL_TEXTURE_2D, texture);
209 0670dadd Michael Walle
    fb_len = 2*s->regs[R_TEXHRES]*s->regs[R_TEXVRES];
210 0670dadd Michael Walle
    fb = cpu_physical_memory_map(s->regs[R_TEXFBUF], &fb_len, 0);
211 0670dadd Michael Walle
    if (fb == NULL) {
212 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
213 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
214 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
215 0670dadd Michael Walle
        return;
216 0670dadd Michael Walle
    }
217 0670dadd Michael Walle
    glTexImage2D(GL_TEXTURE_2D, 0, 3, s->regs[R_TEXHRES], s->regs[R_TEXVRES],
218 0670dadd Michael Walle
            0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
219 0670dadd Michael Walle
    cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
220 0670dadd Michael Walle
221 0670dadd Michael Walle
    /* Set up texturing options */
222 0670dadd Michael Walle
    /* WARNING:
223 0670dadd Michael Walle
     * Many cases of TMU2 masking are not supported by OpenGL.
224 0670dadd Michael Walle
     * We only implement the most common ones:
225 0670dadd Michael Walle
     *  - full bilinear filtering vs. nearest texel
226 0670dadd Michael Walle
     *  - texture clamping vs. texture wrapping
227 0670dadd Michael Walle
     */
228 0670dadd Michael Walle
    if ((s->regs[R_TEXHMASK] & 0x3f) > 0x20) {
229 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
230 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
231 0670dadd Michael Walle
    } else {
232 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
233 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
234 0670dadd Michael Walle
    }
235 0670dadd Michael Walle
    if ((s->regs[R_TEXHMASK] >> 6) & s->regs[R_TEXHRES]) {
236 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
237 0670dadd Michael Walle
    } else {
238 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
239 0670dadd Michael Walle
    }
240 0670dadd Michael Walle
    if ((s->regs[R_TEXVMASK] >> 6) & s->regs[R_TEXVRES]) {
241 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
242 0670dadd Michael Walle
    } else {
243 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
244 0670dadd Michael Walle
    }
245 0670dadd Michael Walle
246 0670dadd Michael Walle
    /* Translucency and decay */
247 0670dadd Michael Walle
    glEnable(GL_BLEND);
248 0670dadd Michael Walle
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
249 0670dadd Michael Walle
    m = (float)(s->regs[R_BRIGHTNESS] + 1) / 64.0f;
250 0670dadd Michael Walle
    glColor4f(m, m, m, (float)(s->regs[R_ALPHA] + 1) / 64.0f);
251 0670dadd Michael Walle
252 0670dadd Michael Walle
    /* Read the QEMU dest. framebuffer into the OpenGL framebuffer */
253 0670dadd Michael Walle
    fb_len = 2 * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
254 0670dadd Michael Walle
    fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 0);
255 0670dadd Michael Walle
    if (fb == NULL) {
256 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
257 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
258 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
259 0670dadd Michael Walle
        return;
260 0670dadd Michael Walle
    }
261 0670dadd Michael Walle
262 0670dadd Michael Walle
    glDrawPixels(s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
263 0670dadd Michael Walle
            GL_UNSIGNED_SHORT_5_6_5, fb);
264 0670dadd Michael Walle
    cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
265 0670dadd Michael Walle
    glViewport(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES]);
266 0670dadd Michael Walle
    glMatrixMode(GL_PROJECTION);
267 0670dadd Michael Walle
    glLoadIdentity();
268 0670dadd Michael Walle
    glOrtho(0.0, s->regs[R_DSTHRES], 0.0, s->regs[R_DSTVRES], -1.0, 1.0);
269 0670dadd Michael Walle
    glMatrixMode(GL_MODELVIEW);
270 0670dadd Michael Walle
271 0670dadd Michael Walle
    /* Map the texture */
272 0670dadd Michael Walle
    mesh_len = MESH_MAXSIZE*MESH_MAXSIZE*sizeof(struct vertex);
273 0670dadd Michael Walle
    mesh = cpu_physical_memory_map(s->regs[R_VERTICESADDR], &mesh_len, 0);
274 0670dadd Michael Walle
    if (mesh == NULL) {
275 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
276 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
277 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
278 0670dadd Michael Walle
        return;
279 0670dadd Michael Walle
    }
280 0670dadd Michael Walle
281 0670dadd Michael Walle
    tmu2_gl_map((struct vertex *)mesh,
282 0670dadd Michael Walle
        s->regs[R_TEXHRES], s->regs[R_TEXVRES],
283 0670dadd Michael Walle
        s->regs[R_HMESHLAST], s->regs[R_VMESHLAST],
284 0670dadd Michael Walle
        s->regs[R_DSTHOFFSET], s->regs[R_DSTVOFFSET],
285 0670dadd Michael Walle
        s->regs[R_DSTSQUAREW], s->regs[R_DSTSQUAREH]);
286 0670dadd Michael Walle
    cpu_physical_memory_unmap(mesh, mesh_len, 0, mesh_len);
287 0670dadd Michael Walle
288 0670dadd Michael Walle
    /* Write back the OpenGL framebuffer to the QEMU framebuffer */
289 0670dadd Michael Walle
    fb_len = 2 * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
290 0670dadd Michael Walle
    fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 1);
291 0670dadd Michael Walle
    if (fb == NULL) {
292 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
293 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
294 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
295 0670dadd Michael Walle
        return;
296 0670dadd Michael Walle
    }
297 0670dadd Michael Walle
298 0670dadd Michael Walle
    glReadPixels(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
299 0670dadd Michael Walle
            GL_UNSIGNED_SHORT_5_6_5, fb);
300 0670dadd Michael Walle
    cpu_physical_memory_unmap(fb, fb_len, 1, fb_len);
301 0670dadd Michael Walle
302 0670dadd Michael Walle
    /* Free OpenGL allocs */
303 0670dadd Michael Walle
    glDeleteTextures(1, &texture);
304 0670dadd Michael Walle
    glXMakeContextCurrent(s->dpy, None, None, NULL);
305 0670dadd Michael Walle
    glXDestroyPbuffer(s->dpy, pbuffer);
306 0670dadd Michael Walle
307 0670dadd Michael Walle
    s->regs[R_CTL] &= ~CTL_START_BUSY;
308 0670dadd Michael Walle
309 0670dadd Michael Walle
    trace_milkymist_tmu2_pulse_irq();
310 0670dadd Michael Walle
    qemu_irq_pulse(s->irq);
311 0670dadd Michael Walle
}
312 0670dadd Michael Walle
313 7100453f Michael Walle
static uint64_t tmu2_read(void *opaque, target_phys_addr_t addr,
314 7100453f Michael Walle
                          unsigned size)
315 0670dadd Michael Walle
{
316 0670dadd Michael Walle
    MilkymistTMU2State *s = opaque;
317 0670dadd Michael Walle
    uint32_t r = 0;
318 0670dadd Michael Walle
319 0670dadd Michael Walle
    addr >>= 2;
320 0670dadd Michael Walle
    switch (addr) {
321 0670dadd Michael Walle
    case R_CTL:
322 0670dadd Michael Walle
    case R_HMESHLAST:
323 0670dadd Michael Walle
    case R_VMESHLAST:
324 0670dadd Michael Walle
    case R_BRIGHTNESS:
325 0670dadd Michael Walle
    case R_CHROMAKEY:
326 0670dadd Michael Walle
    case R_VERTICESADDR:
327 0670dadd Michael Walle
    case R_TEXFBUF:
328 0670dadd Michael Walle
    case R_TEXHRES:
329 0670dadd Michael Walle
    case R_TEXVRES:
330 0670dadd Michael Walle
    case R_TEXHMASK:
331 0670dadd Michael Walle
    case R_TEXVMASK:
332 0670dadd Michael Walle
    case R_DSTFBUF:
333 0670dadd Michael Walle
    case R_DSTHRES:
334 0670dadd Michael Walle
    case R_DSTVRES:
335 0670dadd Michael Walle
    case R_DSTHOFFSET:
336 0670dadd Michael Walle
    case R_DSTVOFFSET:
337 0670dadd Michael Walle
    case R_DSTSQUAREW:
338 0670dadd Michael Walle
    case R_DSTSQUAREH:
339 0670dadd Michael Walle
    case R_ALPHA:
340 0670dadd Michael Walle
        r = s->regs[addr];
341 0670dadd Michael Walle
        break;
342 0670dadd Michael Walle
343 0670dadd Michael Walle
    default:
344 0670dadd Michael Walle
        error_report("milkymist_tmu2: read access to unknown register 0x"
345 0670dadd Michael Walle
                TARGET_FMT_plx, addr << 2);
346 0670dadd Michael Walle
        break;
347 0670dadd Michael Walle
    }
348 0670dadd Michael Walle
349 0670dadd Michael Walle
    trace_milkymist_tmu2_memory_read(addr << 2, r);
350 0670dadd Michael Walle
351 0670dadd Michael Walle
    return r;
352 0670dadd Michael Walle
}
353 0670dadd Michael Walle
354 0670dadd Michael Walle
static void tmu2_check_registers(MilkymistTMU2State *s)
355 0670dadd Michael Walle
{
356 0670dadd Michael Walle
    if (s->regs[R_BRIGHTNESS] > MAX_BRIGHTNESS) {
357 6daf194d Markus Armbruster
        error_report("milkymist_tmu2: max brightness is %d", MAX_BRIGHTNESS);
358 0670dadd Michael Walle
    }
359 0670dadd Michael Walle
360 0670dadd Michael Walle
    if (s->regs[R_ALPHA] > MAX_ALPHA) {
361 6daf194d Markus Armbruster
        error_report("milkymist_tmu2: max alpha is %d", MAX_ALPHA);
362 0670dadd Michael Walle
    }
363 0670dadd Michael Walle
364 0670dadd Michael Walle
    if (s->regs[R_VERTICESADDR] & 0x07) {
365 0670dadd Michael Walle
        error_report("milkymist_tmu2: vertex mesh address has to be 64-bit "
366 6daf194d Markus Armbruster
                "aligned");
367 0670dadd Michael Walle
    }
368 0670dadd Michael Walle
369 0670dadd Michael Walle
    if (s->regs[R_TEXFBUF] & 0x01) {
370 0670dadd Michael Walle
        error_report("milkymist_tmu2: texture buffer address has to be "
371 6daf194d Markus Armbruster
                "16-bit aligned");
372 0670dadd Michael Walle
    }
373 0670dadd Michael Walle
}
374 0670dadd Michael Walle
375 7100453f Michael Walle
static void tmu2_write(void *opaque, target_phys_addr_t addr, uint64_t value,
376 7100453f Michael Walle
                       unsigned size)
377 0670dadd Michael Walle
{
378 0670dadd Michael Walle
    MilkymistTMU2State *s = opaque;
379 0670dadd Michael Walle
380 0670dadd Michael Walle
    trace_milkymist_tmu2_memory_write(addr, value);
381 0670dadd Michael Walle
382 0670dadd Michael Walle
    addr >>= 2;
383 0670dadd Michael Walle
    switch (addr) {
384 0670dadd Michael Walle
    case R_CTL:
385 0670dadd Michael Walle
        s->regs[addr] = value;
386 0670dadd Michael Walle
        if (value & CTL_START_BUSY) {
387 0670dadd Michael Walle
            tmu2_start(s);
388 0670dadd Michael Walle
        }
389 0670dadd Michael Walle
        break;
390 0670dadd Michael Walle
    case R_BRIGHTNESS:
391 0670dadd Michael Walle
    case R_HMESHLAST:
392 0670dadd Michael Walle
    case R_VMESHLAST:
393 0670dadd Michael Walle
    case R_CHROMAKEY:
394 0670dadd Michael Walle
    case R_VERTICESADDR:
395 0670dadd Michael Walle
    case R_TEXFBUF:
396 0670dadd Michael Walle
    case R_TEXHRES:
397 0670dadd Michael Walle
    case R_TEXVRES:
398 0670dadd Michael Walle
    case R_TEXHMASK:
399 0670dadd Michael Walle
    case R_TEXVMASK:
400 0670dadd Michael Walle
    case R_DSTFBUF:
401 0670dadd Michael Walle
    case R_DSTHRES:
402 0670dadd Michael Walle
    case R_DSTVRES:
403 0670dadd Michael Walle
    case R_DSTHOFFSET:
404 0670dadd Michael Walle
    case R_DSTVOFFSET:
405 0670dadd Michael Walle
    case R_DSTSQUAREW:
406 0670dadd Michael Walle
    case R_DSTSQUAREH:
407 0670dadd Michael Walle
    case R_ALPHA:
408 0670dadd Michael Walle
        s->regs[addr] = value;
409 0670dadd Michael Walle
        break;
410 0670dadd Michael Walle
411 0670dadd Michael Walle
    default:
412 0670dadd Michael Walle
        error_report("milkymist_tmu2: write access to unknown register 0x"
413 0670dadd Michael Walle
                TARGET_FMT_plx, addr << 2);
414 0670dadd Michael Walle
        break;
415 0670dadd Michael Walle
    }
416 0670dadd Michael Walle
417 0670dadd Michael Walle
    tmu2_check_registers(s);
418 0670dadd Michael Walle
}
419 0670dadd Michael Walle
420 7100453f Michael Walle
static const MemoryRegionOps tmu2_mmio_ops = {
421 7100453f Michael Walle
    .read = tmu2_read,
422 7100453f Michael Walle
    .write = tmu2_write,
423 7100453f Michael Walle
    .valid = {
424 7100453f Michael Walle
        .min_access_size = 4,
425 7100453f Michael Walle
        .max_access_size = 4,
426 7100453f Michael Walle
    },
427 7100453f Michael Walle
    .endianness = DEVICE_NATIVE_ENDIAN,
428 0670dadd Michael Walle
};
429 0670dadd Michael Walle
430 0670dadd Michael Walle
static void milkymist_tmu2_reset(DeviceState *d)
431 0670dadd Michael Walle
{
432 0670dadd Michael Walle
    MilkymistTMU2State *s = container_of(d, MilkymistTMU2State, busdev.qdev);
433 0670dadd Michael Walle
    int i;
434 0670dadd Michael Walle
435 0670dadd Michael Walle
    for (i = 0; i < R_MAX; i++) {
436 0670dadd Michael Walle
        s->regs[i] = 0;
437 0670dadd Michael Walle
    }
438 0670dadd Michael Walle
}
439 0670dadd Michael Walle
440 0670dadd Michael Walle
static int milkymist_tmu2_init(SysBusDevice *dev)
441 0670dadd Michael Walle
{
442 0670dadd Michael Walle
    MilkymistTMU2State *s = FROM_SYSBUS(typeof(*s), dev);
443 0670dadd Michael Walle
444 0670dadd Michael Walle
    if (tmu2_glx_init(s)) {
445 0670dadd Michael Walle
        return 1;
446 0670dadd Michael Walle
    }
447 0670dadd Michael Walle
448 0670dadd Michael Walle
    sysbus_init_irq(dev, &s->irq);
449 0670dadd Michael Walle
450 7100453f Michael Walle
    memory_region_init_io(&s->regs_region, &tmu2_mmio_ops, s,
451 7100453f Michael Walle
            "milkymist-tmu2", R_MAX * 4);
452 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->regs_region);
453 0670dadd Michael Walle
454 0670dadd Michael Walle
    return 0;
455 0670dadd Michael Walle
}
456 0670dadd Michael Walle
457 0670dadd Michael Walle
static const VMStateDescription vmstate_milkymist_tmu2 = {
458 0670dadd Michael Walle
    .name = "milkymist-tmu2",
459 0670dadd Michael Walle
    .version_id = 1,
460 0670dadd Michael Walle
    .minimum_version_id = 1,
461 0670dadd Michael Walle
    .minimum_version_id_old = 1,
462 0670dadd Michael Walle
    .fields      = (VMStateField[]) {
463 0670dadd Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistTMU2State, R_MAX),
464 0670dadd Michael Walle
        VMSTATE_END_OF_LIST()
465 0670dadd Michael Walle
    }
466 0670dadd Michael Walle
};
467 0670dadd Michael Walle
468 999e12bb Anthony Liguori
static void milkymist_tmu2_class_init(ObjectClass *klass, void *data)
469 999e12bb Anthony Liguori
{
470 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
471 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
472 999e12bb Anthony Liguori
473 999e12bb Anthony Liguori
    k->init = milkymist_tmu2_init;
474 39bffca2 Anthony Liguori
    dc->reset = milkymist_tmu2_reset;
475 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_milkymist_tmu2;
476 999e12bb Anthony Liguori
}
477 999e12bb Anthony Liguori
478 39bffca2 Anthony Liguori
static TypeInfo milkymist_tmu2_info = {
479 39bffca2 Anthony Liguori
    .name          = "milkymist-tmu2",
480 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
481 39bffca2 Anthony Liguori
    .instance_size = sizeof(MilkymistTMU2State),
482 39bffca2 Anthony Liguori
    .class_init    = milkymist_tmu2_class_init,
483 0670dadd Michael Walle
};
484 0670dadd Michael Walle
485 0670dadd Michael Walle
static void milkymist_tmu2_register(void)
486 0670dadd Michael Walle
{
487 39bffca2 Anthony Liguori
    type_register_static(&milkymist_tmu2_info);
488 0670dadd Michael Walle
}
489 0670dadd Michael Walle
490 0670dadd Michael Walle
device_init(milkymist_tmu2_register)