Statistics
| Branch: | Revision:

root / hw / milkymist-tmu2.c @ 69bf405b

History | View | Annotate | Download (13 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 0670dadd Michael Walle
} __attribute__((packed));
77 0670dadd Michael Walle
78 0670dadd Michael Walle
struct MilkymistTMU2State {
79 0670dadd Michael Walle
    SysBusDevice busdev;
80 0670dadd Michael Walle
    CharDriverState *chr;
81 0670dadd Michael Walle
    qemu_irq irq;
82 0670dadd Michael Walle
83 0670dadd Michael Walle
    uint32_t regs[R_MAX];
84 0670dadd Michael Walle
85 0670dadd Michael Walle
    Display *dpy;
86 0670dadd Michael Walle
    GLXFBConfig glx_fb_config;
87 0670dadd Michael Walle
    GLXContext glx_context;
88 0670dadd Michael Walle
};
89 0670dadd Michael Walle
typedef struct MilkymistTMU2State MilkymistTMU2State;
90 0670dadd Michael Walle
91 0670dadd Michael Walle
static const int glx_fbconfig_attr[] = {
92 0670dadd Michael Walle
    GLX_GREEN_SIZE, 5,
93 0670dadd Michael Walle
    GLX_GREEN_SIZE, 6,
94 0670dadd Michael Walle
    GLX_BLUE_SIZE, 5,
95 0670dadd Michael Walle
    None
96 0670dadd Michael Walle
};
97 0670dadd Michael Walle
98 0670dadd Michael Walle
static int tmu2_glx_init(MilkymistTMU2State *s)
99 0670dadd Michael Walle
{
100 0670dadd Michael Walle
    GLXFBConfig *configs;
101 0670dadd Michael Walle
    int nelements;
102 0670dadd Michael Walle
103 0670dadd Michael Walle
    s->dpy = XOpenDisplay(NULL); /* FIXME: call XCloseDisplay() */
104 0670dadd Michael Walle
    if (s->dpy == NULL) {
105 0670dadd Michael Walle
        return 1;
106 0670dadd Michael Walle
    }
107 0670dadd Michael Walle
108 0670dadd Michael Walle
    configs = glXChooseFBConfig(s->dpy, 0, glx_fbconfig_attr, &nelements);
109 0670dadd Michael Walle
    if (configs == NULL) {
110 0670dadd Michael Walle
        return 1;
111 0670dadd Michael Walle
    }
112 0670dadd Michael Walle
113 0670dadd Michael Walle
    s->glx_fb_config = *configs;
114 0670dadd Michael Walle
    XFree(configs);
115 0670dadd Michael Walle
116 0670dadd Michael Walle
    /* FIXME: call glXDestroyContext() */
117 0670dadd Michael Walle
    s->glx_context = glXCreateNewContext(s->dpy, s->glx_fb_config,
118 0670dadd Michael Walle
            GLX_RGBA_TYPE, NULL, 1);
119 0670dadd Michael Walle
    if (s->glx_context == NULL) {
120 0670dadd Michael Walle
        return 1;
121 0670dadd Michael Walle
    }
122 0670dadd Michael Walle
123 0670dadd Michael Walle
    return 0;
124 0670dadd Michael Walle
}
125 0670dadd Michael Walle
126 0670dadd Michael Walle
static void tmu2_gl_map(struct vertex *mesh, int texhres, int texvres,
127 0670dadd Michael Walle
        int hmeshlast, int vmeshlast, int ho, int vo, int sw, int sh)
128 0670dadd Michael Walle
{
129 0670dadd Michael Walle
    int x, y;
130 0670dadd Michael Walle
    int x0, y0, x1, y1;
131 0670dadd Michael Walle
    int u0, v0, u1, v1, u2, v2, u3, v3;
132 0670dadd Michael Walle
    double xscale = 1.0 / ((double)(64 * texhres));
133 0670dadd Michael Walle
    double yscale = 1.0 / ((double)(64 * texvres));
134 0670dadd Michael Walle
135 0670dadd Michael Walle
    glLoadIdentity();
136 0670dadd Michael Walle
    glTranslatef(ho, vo, 0);
137 0670dadd Michael Walle
    glEnable(GL_TEXTURE_2D);
138 0670dadd Michael Walle
    glBegin(GL_QUADS);
139 0670dadd Michael Walle
140 0670dadd Michael Walle
    for (y = 0; y < vmeshlast; y++) {
141 0670dadd Michael Walle
        y0 = y * sh;
142 0670dadd Michael Walle
        y1 = y0 + sh;
143 0670dadd Michael Walle
        for (x = 0; x < hmeshlast; x++) {
144 0670dadd Michael Walle
            x0 = x * sw;
145 0670dadd Michael Walle
            x1 = x0 + sw;
146 0670dadd Michael Walle
147 0670dadd Michael Walle
            u0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].x);
148 0670dadd Michael Walle
            v0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].y);
149 0670dadd Michael Walle
            u1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].x);
150 0670dadd Michael Walle
            v1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].y);
151 0670dadd Michael Walle
            u2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].x);
152 0670dadd Michael Walle
            v2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].y);
153 0670dadd Michael Walle
            u3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].x);
154 0670dadd Michael Walle
            v3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].y);
155 0670dadd Michael Walle
156 0670dadd Michael Walle
            glTexCoord2d(((double)u0) * xscale, ((double)v0) * yscale);
157 0670dadd Michael Walle
            glVertex3i(x0, y0, 0);
158 0670dadd Michael Walle
            glTexCoord2d(((double)u1) * xscale, ((double)v1) * yscale);
159 0670dadd Michael Walle
            glVertex3i(x1, y0, 0);
160 0670dadd Michael Walle
            glTexCoord2d(((double)u2) * xscale, ((double)v2) * yscale);
161 0670dadd Michael Walle
            glVertex3i(x1, y1, 0);
162 0670dadd Michael Walle
            glTexCoord2d(((double)u3) * xscale, ((double)v3) * yscale);
163 0670dadd Michael Walle
            glVertex3i(x0, y1, 0);
164 0670dadd Michael Walle
        }
165 0670dadd Michael Walle
    }
166 0670dadd Michael Walle
167 0670dadd Michael Walle
    glEnd();
168 0670dadd Michael Walle
}
169 0670dadd Michael Walle
170 0670dadd Michael Walle
static void tmu2_start(MilkymistTMU2State *s)
171 0670dadd Michael Walle
{
172 0670dadd Michael Walle
    int pbuffer_attrib[6] = {
173 0670dadd Michael Walle
        GLX_PBUFFER_WIDTH,
174 0670dadd Michael Walle
        0,
175 0670dadd Michael Walle
        GLX_PBUFFER_HEIGHT,
176 0670dadd Michael Walle
        0,
177 0670dadd Michael Walle
        GLX_PRESERVED_CONTENTS,
178 0670dadd Michael Walle
        True
179 0670dadd Michael Walle
    };
180 0670dadd Michael Walle
181 0670dadd Michael Walle
    GLXPbuffer pbuffer;
182 0670dadd Michael Walle
    GLuint texture;
183 0670dadd Michael Walle
    void *fb;
184 0670dadd Michael Walle
    target_phys_addr_t fb_len;
185 0670dadd Michael Walle
    void *mesh;
186 0670dadd Michael Walle
    target_phys_addr_t mesh_len;
187 0670dadd Michael Walle
    float m;
188 0670dadd Michael Walle
189 0670dadd Michael Walle
    trace_milkymist_tmu2_start();
190 0670dadd Michael Walle
191 0670dadd Michael Walle
    /* Create and set up a suitable OpenGL context */
192 0670dadd Michael Walle
    pbuffer_attrib[1] = s->regs[R_DSTHRES];
193 0670dadd Michael Walle
    pbuffer_attrib[3] = s->regs[R_DSTVRES];
194 0670dadd Michael Walle
    pbuffer = glXCreatePbuffer(s->dpy, s->glx_fb_config, pbuffer_attrib);
195 0670dadd Michael Walle
    glXMakeContextCurrent(s->dpy, pbuffer, pbuffer, s->glx_context);
196 0670dadd Michael Walle
197 0670dadd Michael Walle
    /* Fixup endianness. TODO: would it work on BE hosts? */
198 0670dadd Michael Walle
    glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
199 0670dadd Michael Walle
    glPixelStorei(GL_PACK_SWAP_BYTES, 1);
200 0670dadd Michael Walle
201 0670dadd Michael Walle
    /* Row alignment */
202 0670dadd Michael Walle
    glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
203 0670dadd Michael Walle
    glPixelStorei(GL_PACK_ALIGNMENT, 2);
204 0670dadd Michael Walle
205 0670dadd Michael Walle
    /* Read the QEMU source framebuffer into an OpenGL texture */
206 0670dadd Michael Walle
    glGenTextures(1, &texture);
207 0670dadd Michael Walle
    glBindTexture(GL_TEXTURE_2D, texture);
208 0670dadd Michael Walle
    fb_len = 2*s->regs[R_TEXHRES]*s->regs[R_TEXVRES];
209 0670dadd Michael Walle
    fb = cpu_physical_memory_map(s->regs[R_TEXFBUF], &fb_len, 0);
210 0670dadd Michael Walle
    if (fb == NULL) {
211 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
212 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
213 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
214 0670dadd Michael Walle
        return;
215 0670dadd Michael Walle
    }
216 0670dadd Michael Walle
    glTexImage2D(GL_TEXTURE_2D, 0, 3, s->regs[R_TEXHRES], s->regs[R_TEXVRES],
217 0670dadd Michael Walle
            0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
218 0670dadd Michael Walle
    cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
219 0670dadd Michael Walle
220 0670dadd Michael Walle
    /* Set up texturing options */
221 0670dadd Michael Walle
    /* WARNING:
222 0670dadd Michael Walle
     * Many cases of TMU2 masking are not supported by OpenGL.
223 0670dadd Michael Walle
     * We only implement the most common ones:
224 0670dadd Michael Walle
     *  - full bilinear filtering vs. nearest texel
225 0670dadd Michael Walle
     *  - texture clamping vs. texture wrapping
226 0670dadd Michael Walle
     */
227 0670dadd Michael Walle
    if ((s->regs[R_TEXHMASK] & 0x3f) > 0x20) {
228 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
229 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
230 0670dadd Michael Walle
    } else {
231 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233 0670dadd Michael Walle
    }
234 0670dadd Michael Walle
    if ((s->regs[R_TEXHMASK] >> 6) & s->regs[R_TEXHRES]) {
235 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
236 0670dadd Michael Walle
    } else {
237 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
238 0670dadd Michael Walle
    }
239 0670dadd Michael Walle
    if ((s->regs[R_TEXVMASK] >> 6) & s->regs[R_TEXVRES]) {
240 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
241 0670dadd Michael Walle
    } else {
242 0670dadd Michael Walle
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
243 0670dadd Michael Walle
    }
244 0670dadd Michael Walle
245 0670dadd Michael Walle
    /* Translucency and decay */
246 0670dadd Michael Walle
    glEnable(GL_BLEND);
247 0670dadd Michael Walle
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
248 0670dadd Michael Walle
    m = (float)(s->regs[R_BRIGHTNESS] + 1) / 64.0f;
249 0670dadd Michael Walle
    glColor4f(m, m, m, (float)(s->regs[R_ALPHA] + 1) / 64.0f);
250 0670dadd Michael Walle
251 0670dadd Michael Walle
    /* Read the QEMU dest. framebuffer into the OpenGL framebuffer */
252 0670dadd Michael Walle
    fb_len = 2 * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
253 0670dadd Michael Walle
    fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 0);
254 0670dadd Michael Walle
    if (fb == NULL) {
255 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
256 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
257 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
258 0670dadd Michael Walle
        return;
259 0670dadd Michael Walle
    }
260 0670dadd Michael Walle
261 0670dadd Michael Walle
    glDrawPixels(s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
262 0670dadd Michael Walle
            GL_UNSIGNED_SHORT_5_6_5, fb);
263 0670dadd Michael Walle
    cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
264 0670dadd Michael Walle
    glViewport(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES]);
265 0670dadd Michael Walle
    glMatrixMode(GL_PROJECTION);
266 0670dadd Michael Walle
    glLoadIdentity();
267 0670dadd Michael Walle
    glOrtho(0.0, s->regs[R_DSTHRES], 0.0, s->regs[R_DSTVRES], -1.0, 1.0);
268 0670dadd Michael Walle
    glMatrixMode(GL_MODELVIEW);
269 0670dadd Michael Walle
270 0670dadd Michael Walle
    /* Map the texture */
271 0670dadd Michael Walle
    mesh_len = MESH_MAXSIZE*MESH_MAXSIZE*sizeof(struct vertex);
272 0670dadd Michael Walle
    mesh = cpu_physical_memory_map(s->regs[R_VERTICESADDR], &mesh_len, 0);
273 0670dadd Michael Walle
    if (mesh == NULL) {
274 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
275 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
276 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
277 0670dadd Michael Walle
        return;
278 0670dadd Michael Walle
    }
279 0670dadd Michael Walle
280 0670dadd Michael Walle
    tmu2_gl_map((struct vertex *)mesh,
281 0670dadd Michael Walle
        s->regs[R_TEXHRES], s->regs[R_TEXVRES],
282 0670dadd Michael Walle
        s->regs[R_HMESHLAST], s->regs[R_VMESHLAST],
283 0670dadd Michael Walle
        s->regs[R_DSTHOFFSET], s->regs[R_DSTVOFFSET],
284 0670dadd Michael Walle
        s->regs[R_DSTSQUAREW], s->regs[R_DSTSQUAREH]);
285 0670dadd Michael Walle
    cpu_physical_memory_unmap(mesh, mesh_len, 0, mesh_len);
286 0670dadd Michael Walle
287 0670dadd Michael Walle
    /* Write back the OpenGL framebuffer to the QEMU framebuffer */
288 0670dadd Michael Walle
    fb_len = 2 * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
289 0670dadd Michael Walle
    fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 1);
290 0670dadd Michael Walle
    if (fb == NULL) {
291 0670dadd Michael Walle
        glDeleteTextures(1, &texture);
292 0670dadd Michael Walle
        glXMakeContextCurrent(s->dpy, None, None, NULL);
293 0670dadd Michael Walle
        glXDestroyPbuffer(s->dpy, pbuffer);
294 0670dadd Michael Walle
        return;
295 0670dadd Michael Walle
    }
296 0670dadd Michael Walle
297 0670dadd Michael Walle
    glReadPixels(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
298 0670dadd Michael Walle
            GL_UNSIGNED_SHORT_5_6_5, fb);
299 0670dadd Michael Walle
    cpu_physical_memory_unmap(fb, fb_len, 1, fb_len);
300 0670dadd Michael Walle
301 0670dadd Michael Walle
    /* Free OpenGL allocs */
302 0670dadd Michael Walle
    glDeleteTextures(1, &texture);
303 0670dadd Michael Walle
    glXMakeContextCurrent(s->dpy, None, None, NULL);
304 0670dadd Michael Walle
    glXDestroyPbuffer(s->dpy, pbuffer);
305 0670dadd Michael Walle
306 0670dadd Michael Walle
    s->regs[R_CTL] &= ~CTL_START_BUSY;
307 0670dadd Michael Walle
308 0670dadd Michael Walle
    trace_milkymist_tmu2_pulse_irq();
309 0670dadd Michael Walle
    qemu_irq_pulse(s->irq);
310 0670dadd Michael Walle
}
311 0670dadd Michael Walle
312 0670dadd Michael Walle
static uint32_t tmu2_read(void *opaque, target_phys_addr_t addr)
313 0670dadd Michael Walle
{
314 0670dadd Michael Walle
    MilkymistTMU2State *s = opaque;
315 0670dadd Michael Walle
    uint32_t r = 0;
316 0670dadd Michael Walle
317 0670dadd Michael Walle
    addr >>= 2;
318 0670dadd Michael Walle
    switch (addr) {
319 0670dadd Michael Walle
    case R_CTL:
320 0670dadd Michael Walle
    case R_HMESHLAST:
321 0670dadd Michael Walle
    case R_VMESHLAST:
322 0670dadd Michael Walle
    case R_BRIGHTNESS:
323 0670dadd Michael Walle
    case R_CHROMAKEY:
324 0670dadd Michael Walle
    case R_VERTICESADDR:
325 0670dadd Michael Walle
    case R_TEXFBUF:
326 0670dadd Michael Walle
    case R_TEXHRES:
327 0670dadd Michael Walle
    case R_TEXVRES:
328 0670dadd Michael Walle
    case R_TEXHMASK:
329 0670dadd Michael Walle
    case R_TEXVMASK:
330 0670dadd Michael Walle
    case R_DSTFBUF:
331 0670dadd Michael Walle
    case R_DSTHRES:
332 0670dadd Michael Walle
    case R_DSTVRES:
333 0670dadd Michael Walle
    case R_DSTHOFFSET:
334 0670dadd Michael Walle
    case R_DSTVOFFSET:
335 0670dadd Michael Walle
    case R_DSTSQUAREW:
336 0670dadd Michael Walle
    case R_DSTSQUAREH:
337 0670dadd Michael Walle
    case R_ALPHA:
338 0670dadd Michael Walle
        r = s->regs[addr];
339 0670dadd Michael Walle
        break;
340 0670dadd Michael Walle
341 0670dadd Michael Walle
    default:
342 0670dadd Michael Walle
        error_report("milkymist_tmu2: read access to unknown register 0x"
343 0670dadd Michael Walle
                TARGET_FMT_plx, addr << 2);
344 0670dadd Michael Walle
        break;
345 0670dadd Michael Walle
    }
346 0670dadd Michael Walle
347 0670dadd Michael Walle
    trace_milkymist_tmu2_memory_read(addr << 2, r);
348 0670dadd Michael Walle
349 0670dadd Michael Walle
    return r;
350 0670dadd Michael Walle
}
351 0670dadd Michael Walle
352 0670dadd Michael Walle
static void tmu2_check_registers(MilkymistTMU2State *s)
353 0670dadd Michael Walle
{
354 0670dadd Michael Walle
    if (s->regs[R_BRIGHTNESS] > MAX_BRIGHTNESS) {
355 0670dadd Michael Walle
        error_report("milkymist_tmu2: max brightness is %d\n", MAX_BRIGHTNESS);
356 0670dadd Michael Walle
    }
357 0670dadd Michael Walle
358 0670dadd Michael Walle
    if (s->regs[R_ALPHA] > MAX_ALPHA) {
359 0670dadd Michael Walle
        error_report("milkymist_tmu2: max alpha is %d\n", MAX_ALPHA);
360 0670dadd Michael Walle
    }
361 0670dadd Michael Walle
362 0670dadd Michael Walle
    if (s->regs[R_VERTICESADDR] & 0x07) {
363 0670dadd Michael Walle
        error_report("milkymist_tmu2: vertex mesh address has to be 64-bit "
364 0670dadd Michael Walle
                "aligned\n");
365 0670dadd Michael Walle
    }
366 0670dadd Michael Walle
367 0670dadd Michael Walle
    if (s->regs[R_TEXFBUF] & 0x01) {
368 0670dadd Michael Walle
        error_report("milkymist_tmu2: texture buffer address has to be "
369 0670dadd Michael Walle
                "16-bit aligned\n");
370 0670dadd Michael Walle
    }
371 0670dadd Michael Walle
}
372 0670dadd Michael Walle
373 0670dadd Michael Walle
static void tmu2_write(void *opaque, target_phys_addr_t addr, uint32_t value)
374 0670dadd Michael Walle
{
375 0670dadd Michael Walle
    MilkymistTMU2State *s = opaque;
376 0670dadd Michael Walle
377 0670dadd Michael Walle
    trace_milkymist_tmu2_memory_write(addr, value);
378 0670dadd Michael Walle
379 0670dadd Michael Walle
    addr >>= 2;
380 0670dadd Michael Walle
    switch (addr) {
381 0670dadd Michael Walle
    case R_CTL:
382 0670dadd Michael Walle
        s->regs[addr] = value;
383 0670dadd Michael Walle
        if (value & CTL_START_BUSY) {
384 0670dadd Michael Walle
            tmu2_start(s);
385 0670dadd Michael Walle
        }
386 0670dadd Michael Walle
        break;
387 0670dadd Michael Walle
    case R_BRIGHTNESS:
388 0670dadd Michael Walle
    case R_HMESHLAST:
389 0670dadd Michael Walle
    case R_VMESHLAST:
390 0670dadd Michael Walle
    case R_CHROMAKEY:
391 0670dadd Michael Walle
    case R_VERTICESADDR:
392 0670dadd Michael Walle
    case R_TEXFBUF:
393 0670dadd Michael Walle
    case R_TEXHRES:
394 0670dadd Michael Walle
    case R_TEXVRES:
395 0670dadd Michael Walle
    case R_TEXHMASK:
396 0670dadd Michael Walle
    case R_TEXVMASK:
397 0670dadd Michael Walle
    case R_DSTFBUF:
398 0670dadd Michael Walle
    case R_DSTHRES:
399 0670dadd Michael Walle
    case R_DSTVRES:
400 0670dadd Michael Walle
    case R_DSTHOFFSET:
401 0670dadd Michael Walle
    case R_DSTVOFFSET:
402 0670dadd Michael Walle
    case R_DSTSQUAREW:
403 0670dadd Michael Walle
    case R_DSTSQUAREH:
404 0670dadd Michael Walle
    case R_ALPHA:
405 0670dadd Michael Walle
        s->regs[addr] = value;
406 0670dadd Michael Walle
        break;
407 0670dadd Michael Walle
408 0670dadd Michael Walle
    default:
409 0670dadd Michael Walle
        error_report("milkymist_tmu2: write access to unknown register 0x"
410 0670dadd Michael Walle
                TARGET_FMT_plx, addr << 2);
411 0670dadd Michael Walle
        break;
412 0670dadd Michael Walle
    }
413 0670dadd Michael Walle
414 0670dadd Michael Walle
    tmu2_check_registers(s);
415 0670dadd Michael Walle
}
416 0670dadd Michael Walle
417 0670dadd Michael Walle
static CPUReadMemoryFunc * const tmu2_read_fn[] = {
418 0670dadd Michael Walle
    NULL,
419 0670dadd Michael Walle
    NULL,
420 0670dadd Michael Walle
    &tmu2_read,
421 0670dadd Michael Walle
};
422 0670dadd Michael Walle
423 0670dadd Michael Walle
static CPUWriteMemoryFunc * const tmu2_write_fn[] = {
424 0670dadd Michael Walle
    NULL,
425 0670dadd Michael Walle
    NULL,
426 0670dadd Michael Walle
    &tmu2_write,
427 0670dadd Michael Walle
};
428 0670dadd Michael Walle
429 0670dadd Michael Walle
static void milkymist_tmu2_reset(DeviceState *d)
430 0670dadd Michael Walle
{
431 0670dadd Michael Walle
    MilkymistTMU2State *s = container_of(d, MilkymistTMU2State, busdev.qdev);
432 0670dadd Michael Walle
    int i;
433 0670dadd Michael Walle
434 0670dadd Michael Walle
    for (i = 0; i < R_MAX; i++) {
435 0670dadd Michael Walle
        s->regs[i] = 0;
436 0670dadd Michael Walle
    }
437 0670dadd Michael Walle
}
438 0670dadd Michael Walle
439 0670dadd Michael Walle
static int milkymist_tmu2_init(SysBusDevice *dev)
440 0670dadd Michael Walle
{
441 0670dadd Michael Walle
    MilkymistTMU2State *s = FROM_SYSBUS(typeof(*s), dev);
442 0670dadd Michael Walle
    int tmu2_regs;
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 0670dadd Michael Walle
    tmu2_regs = cpu_register_io_memory(tmu2_read_fn, tmu2_write_fn, s,
451 0670dadd Michael Walle
            DEVICE_NATIVE_ENDIAN);
452 0670dadd Michael Walle
    sysbus_init_mmio(dev, R_MAX * 4, tmu2_regs);
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 0670dadd Michael Walle
static SysBusDeviceInfo milkymist_tmu2_info = {
469 0670dadd Michael Walle
    .init = milkymist_tmu2_init,
470 0670dadd Michael Walle
    .qdev.name  = "milkymist-tmu2",
471 0670dadd Michael Walle
    .qdev.size  = sizeof(MilkymistTMU2State),
472 0670dadd Michael Walle
    .qdev.vmsd  = &vmstate_milkymist_tmu2,
473 0670dadd Michael Walle
    .qdev.reset = milkymist_tmu2_reset,
474 0670dadd Michael Walle
};
475 0670dadd Michael Walle
476 0670dadd Michael Walle
static void milkymist_tmu2_register(void)
477 0670dadd Michael Walle
{
478 0670dadd Michael Walle
    sysbus_register_withprop(&milkymist_tmu2_info);
479 0670dadd Michael Walle
}
480 0670dadd Michael Walle
481 0670dadd Michael Walle
device_init(milkymist_tmu2_register)