Statistics
| Branch: | Revision:

root / hw / misc / milkymist-pfpu.c @ a8aec295

History | View | Annotate | Download (14.3 kB)

1 5ee18b9c Michael Walle
/*
2 5ee18b9c Michael Walle
 *  QEMU model of the Milkymist programmable FPU.
3 5ee18b9c Michael Walle
 *
4 5ee18b9c Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 5ee18b9c Michael Walle
 *
6 5ee18b9c Michael Walle
 * This library is free software; you can redistribute it and/or
7 5ee18b9c Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 5ee18b9c Michael Walle
 * License as published by the Free Software Foundation; either
9 5ee18b9c Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 5ee18b9c Michael Walle
 *
11 5ee18b9c Michael Walle
 * This library is distributed in the hope that it will be useful,
12 5ee18b9c Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 5ee18b9c Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 5ee18b9c Michael Walle
 * Lesser General Public License for more details.
15 5ee18b9c Michael Walle
 *
16 5ee18b9c Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 5ee18b9c Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 5ee18b9c Michael Walle
 *
19 5ee18b9c Michael Walle
 *
20 5ee18b9c Michael Walle
 * Specification available at:
21 5ee18b9c Michael Walle
 *   http://www.milkymist.org/socdoc/pfpu.pdf
22 5ee18b9c Michael Walle
 *
23 5ee18b9c Michael Walle
 */
24 5ee18b9c Michael Walle
25 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
26 83c9f4ca Paolo Bonzini
#include "hw/sysbus.h"
27 5ee18b9c Michael Walle
#include "trace.h"
28 1de7afc9 Paolo Bonzini
#include "qemu/log.h"
29 1de7afc9 Paolo Bonzini
#include "qemu/error-report.h"
30 5ee18b9c Michael Walle
#include <math.h>
31 5ee18b9c Michael Walle
32 5ee18b9c Michael Walle
/* #define TRACE_EXEC */
33 5ee18b9c Michael Walle
34 5ee18b9c Michael Walle
#ifdef TRACE_EXEC
35 5ee18b9c Michael Walle
#    define D_EXEC(x) x
36 5ee18b9c Michael Walle
#else
37 5ee18b9c Michael Walle
#    define D_EXEC(x)
38 5ee18b9c Michael Walle
#endif
39 5ee18b9c Michael Walle
40 5ee18b9c Michael Walle
enum {
41 5ee18b9c Michael Walle
    R_CTL = 0,
42 5ee18b9c Michael Walle
    R_MESHBASE,
43 5ee18b9c Michael Walle
    R_HMESHLAST,
44 5ee18b9c Michael Walle
    R_VMESHLAST,
45 5ee18b9c Michael Walle
    R_CODEPAGE,
46 5ee18b9c Michael Walle
    R_VERTICES,
47 5ee18b9c Michael Walle
    R_COLLISIONS,
48 5ee18b9c Michael Walle
    R_STRAYWRITES,
49 5ee18b9c Michael Walle
    R_LASTDMA,
50 5ee18b9c Michael Walle
    R_PC,
51 5ee18b9c Michael Walle
    R_DREGBASE,
52 5ee18b9c Michael Walle
    R_CODEBASE,
53 5ee18b9c Michael Walle
    R_MAX
54 5ee18b9c Michael Walle
};
55 5ee18b9c Michael Walle
56 5ee18b9c Michael Walle
enum {
57 5ee18b9c Michael Walle
    CTL_START_BUSY = (1<<0),
58 5ee18b9c Michael Walle
};
59 5ee18b9c Michael Walle
60 5ee18b9c Michael Walle
enum {
61 5ee18b9c Michael Walle
    OP_NOP = 0,
62 5ee18b9c Michael Walle
    OP_FADD,
63 5ee18b9c Michael Walle
    OP_FSUB,
64 5ee18b9c Michael Walle
    OP_FMUL,
65 5ee18b9c Michael Walle
    OP_FABS,
66 5ee18b9c Michael Walle
    OP_F2I,
67 5ee18b9c Michael Walle
    OP_I2F,
68 5ee18b9c Michael Walle
    OP_VECTOUT,
69 5ee18b9c Michael Walle
    OP_SIN,
70 5ee18b9c Michael Walle
    OP_COS,
71 5ee18b9c Michael Walle
    OP_ABOVE,
72 5ee18b9c Michael Walle
    OP_EQUAL,
73 5ee18b9c Michael Walle
    OP_COPY,
74 5ee18b9c Michael Walle
    OP_IF,
75 5ee18b9c Michael Walle
    OP_TSIGN,
76 5ee18b9c Michael Walle
    OP_QUAKE,
77 5ee18b9c Michael Walle
};
78 5ee18b9c Michael Walle
79 5ee18b9c Michael Walle
enum {
80 5ee18b9c Michael Walle
    GPR_X = 0,
81 5ee18b9c Michael Walle
    GPR_Y = 1,
82 5ee18b9c Michael Walle
    GPR_FLAGS = 2,
83 5ee18b9c Michael Walle
};
84 5ee18b9c Michael Walle
85 5ee18b9c Michael Walle
enum {
86 5ee18b9c Michael Walle
    LATENCY_FADD = 5,
87 5ee18b9c Michael Walle
    LATENCY_FSUB = 5,
88 5ee18b9c Michael Walle
    LATENCY_FMUL = 7,
89 5ee18b9c Michael Walle
    LATENCY_FABS = 2,
90 5ee18b9c Michael Walle
    LATENCY_F2I = 2,
91 5ee18b9c Michael Walle
    LATENCY_I2F = 3,
92 5ee18b9c Michael Walle
    LATENCY_VECTOUT = 0,
93 5ee18b9c Michael Walle
    LATENCY_SIN = 4,
94 5ee18b9c Michael Walle
    LATENCY_COS = 4,
95 5ee18b9c Michael Walle
    LATENCY_ABOVE = 2,
96 5ee18b9c Michael Walle
    LATENCY_EQUAL = 2,
97 5ee18b9c Michael Walle
    LATENCY_COPY = 2,
98 5ee18b9c Michael Walle
    LATENCY_IF = 2,
99 5ee18b9c Michael Walle
    LATENCY_TSIGN = 2,
100 5ee18b9c Michael Walle
    LATENCY_QUAKE = 2,
101 5ee18b9c Michael Walle
    MAX_LATENCY = 7
102 5ee18b9c Michael Walle
};
103 5ee18b9c Michael Walle
104 5ee18b9c Michael Walle
#define GPR_BEGIN       0x100
105 5ee18b9c Michael Walle
#define GPR_END         0x17f
106 5ee18b9c Michael Walle
#define MICROCODE_BEGIN 0x200
107 5ee18b9c Michael Walle
#define MICROCODE_END   0x3ff
108 5ee18b9c Michael Walle
#define MICROCODE_WORDS 2048
109 5ee18b9c Michael Walle
110 5ee18b9c Michael Walle
#define REINTERPRET_CAST(type, val) (*((type *)&(val)))
111 5ee18b9c Michael Walle
112 5ee18b9c Michael Walle
#ifdef TRACE_EXEC
113 5ee18b9c Michael Walle
static const char *opcode_to_str[] = {
114 5ee18b9c Michael Walle
    "NOP", "FADD", "FSUB", "FMUL", "FABS", "F2I", "I2F", "VECTOUT",
115 5ee18b9c Michael Walle
    "SIN", "COS", "ABOVE", "EQUAL", "COPY", "IF", "TSIGN", "QUAKE",
116 5ee18b9c Michael Walle
};
117 5ee18b9c Michael Walle
#endif
118 5ee18b9c Michael Walle
119 5ee18b9c Michael Walle
struct MilkymistPFPUState {
120 5ee18b9c Michael Walle
    SysBusDevice busdev;
121 d46ccfce Michael Walle
    MemoryRegion regs_region;
122 5ee18b9c Michael Walle
    CharDriverState *chr;
123 5ee18b9c Michael Walle
    qemu_irq irq;
124 5ee18b9c Michael Walle
125 5ee18b9c Michael Walle
    uint32_t regs[R_MAX];
126 5ee18b9c Michael Walle
    uint32_t gp_regs[128];
127 5ee18b9c Michael Walle
    uint32_t microcode[MICROCODE_WORDS];
128 5ee18b9c Michael Walle
129 5ee18b9c Michael Walle
    int output_queue_pos;
130 5ee18b9c Michael Walle
    uint32_t output_queue[MAX_LATENCY];
131 5ee18b9c Michael Walle
};
132 5ee18b9c Michael Walle
typedef struct MilkymistPFPUState MilkymistPFPUState;
133 5ee18b9c Michael Walle
134 a8170e5e Avi Kivity
static inline hwaddr
135 5ee18b9c Michael Walle
get_dma_address(uint32_t base, uint32_t x, uint32_t y)
136 5ee18b9c Michael Walle
{
137 5ee18b9c Michael Walle
    return base + 8 * (128 * y + x);
138 5ee18b9c Michael Walle
}
139 5ee18b9c Michael Walle
140 5ee18b9c Michael Walle
static inline void
141 5ee18b9c Michael Walle
output_queue_insert(MilkymistPFPUState *s, uint32_t val, int pos)
142 5ee18b9c Michael Walle
{
143 5ee18b9c Michael Walle
    s->output_queue[(s->output_queue_pos + pos) % MAX_LATENCY] = val;
144 5ee18b9c Michael Walle
}
145 5ee18b9c Michael Walle
146 5ee18b9c Michael Walle
static inline uint32_t
147 5ee18b9c Michael Walle
output_queue_remove(MilkymistPFPUState *s)
148 5ee18b9c Michael Walle
{
149 5ee18b9c Michael Walle
    return s->output_queue[s->output_queue_pos];
150 5ee18b9c Michael Walle
}
151 5ee18b9c Michael Walle
152 5ee18b9c Michael Walle
static inline void
153 5ee18b9c Michael Walle
output_queue_advance(MilkymistPFPUState *s)
154 5ee18b9c Michael Walle
{
155 5ee18b9c Michael Walle
    s->output_queue[s->output_queue_pos] = 0;
156 5ee18b9c Michael Walle
    s->output_queue_pos = (s->output_queue_pos + 1) % MAX_LATENCY;
157 5ee18b9c Michael Walle
}
158 5ee18b9c Michael Walle
159 5ee18b9c Michael Walle
static int pfpu_decode_insn(MilkymistPFPUState *s)
160 5ee18b9c Michael Walle
{
161 5ee18b9c Michael Walle
    uint32_t pc = s->regs[R_PC];
162 5ee18b9c Michael Walle
    uint32_t insn = s->microcode[pc];
163 5ee18b9c Michael Walle
    uint32_t reg_a = (insn >> 18) & 0x7f;
164 5ee18b9c Michael Walle
    uint32_t reg_b = (insn >> 11) & 0x7f;
165 5ee18b9c Michael Walle
    uint32_t op = (insn >> 7) & 0xf;
166 5ee18b9c Michael Walle
    uint32_t reg_d = insn & 0x7f;
167 7f7454ec Anthony Liguori
    uint32_t r = 0;
168 5ee18b9c Michael Walle
    int latency = 0;
169 5ee18b9c Michael Walle
170 5ee18b9c Michael Walle
    switch (op) {
171 5ee18b9c Michael Walle
    case OP_NOP:
172 5ee18b9c Michael Walle
        break;
173 5ee18b9c Michael Walle
    case OP_FADD:
174 5ee18b9c Michael Walle
    {
175 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
176 5ee18b9c Michael Walle
        float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
177 5ee18b9c Michael Walle
        float t = a + b;
178 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
179 5ee18b9c Michael Walle
        latency = LATENCY_FADD;
180 5ee18b9c Michael Walle
        D_EXEC(qemu_log("ADD a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
181 5ee18b9c Michael Walle
    } break;
182 5ee18b9c Michael Walle
    case OP_FSUB:
183 5ee18b9c Michael Walle
    {
184 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
185 5ee18b9c Michael Walle
        float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
186 5ee18b9c Michael Walle
        float t = a - b;
187 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
188 5ee18b9c Michael Walle
        latency = LATENCY_FSUB;
189 5ee18b9c Michael Walle
        D_EXEC(qemu_log("SUB a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
190 5ee18b9c Michael Walle
    } break;
191 5ee18b9c Michael Walle
    case OP_FMUL:
192 5ee18b9c Michael Walle
    {
193 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
194 5ee18b9c Michael Walle
        float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
195 5ee18b9c Michael Walle
        float t = a * b;
196 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
197 5ee18b9c Michael Walle
        latency = LATENCY_FMUL;
198 5ee18b9c Michael Walle
        D_EXEC(qemu_log("MUL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
199 5ee18b9c Michael Walle
    } break;
200 5ee18b9c Michael Walle
    case OP_FABS:
201 5ee18b9c Michael Walle
    {
202 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
203 5ee18b9c Michael Walle
        float t = fabsf(a);
204 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
205 5ee18b9c Michael Walle
        latency = LATENCY_FABS;
206 5ee18b9c Michael Walle
        D_EXEC(qemu_log("ABS a=%f t=%f, r=%08x\n", a, t, r));
207 5ee18b9c Michael Walle
    } break;
208 5ee18b9c Michael Walle
    case OP_F2I:
209 5ee18b9c Michael Walle
    {
210 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
211 5ee18b9c Michael Walle
        int32_t t = a;
212 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
213 5ee18b9c Michael Walle
        latency = LATENCY_F2I;
214 5ee18b9c Michael Walle
        D_EXEC(qemu_log("F2I a=%f t=%d, r=%08x\n", a, t, r));
215 5ee18b9c Michael Walle
    } break;
216 5ee18b9c Michael Walle
    case OP_I2F:
217 5ee18b9c Michael Walle
    {
218 5ee18b9c Michael Walle
        int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
219 5ee18b9c Michael Walle
        float t = a;
220 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
221 5ee18b9c Michael Walle
        latency = LATENCY_I2F;
222 5ee18b9c Michael Walle
        D_EXEC(qemu_log("I2F a=%08x t=%f, r=%08x\n", a, t, r));
223 5ee18b9c Michael Walle
    } break;
224 5ee18b9c Michael Walle
    case OP_VECTOUT:
225 5ee18b9c Michael Walle
    {
226 5ee18b9c Michael Walle
        uint32_t a = cpu_to_be32(s->gp_regs[reg_a]);
227 5ee18b9c Michael Walle
        uint32_t b = cpu_to_be32(s->gp_regs[reg_b]);
228 a8170e5e Avi Kivity
        hwaddr dma_ptr =
229 5ee18b9c Michael Walle
            get_dma_address(s->regs[R_MESHBASE],
230 5ee18b9c Michael Walle
                    s->gp_regs[GPR_X], s->gp_regs[GPR_Y]);
231 e1fe50dc Stefan Weil
        cpu_physical_memory_write(dma_ptr, &a, 4);
232 e1fe50dc Stefan Weil
        cpu_physical_memory_write(dma_ptr + 4, &b, 4);
233 5ee18b9c Michael Walle
        s->regs[R_LASTDMA] = dma_ptr + 4;
234 5ee18b9c Michael Walle
        D_EXEC(qemu_log("VECTOUT a=%08x b=%08x dma=%08x\n", a, b, dma_ptr));
235 5ee18b9c Michael Walle
        trace_milkymist_pfpu_vectout(a, b, dma_ptr);
236 5ee18b9c Michael Walle
    } break;
237 5ee18b9c Michael Walle
    case OP_SIN:
238 5ee18b9c Michael Walle
    {
239 5ee18b9c Michael Walle
        int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
240 5ee18b9c Michael Walle
        float t = sinf(a * (1.0f / (M_PI * 4096.0f)));
241 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
242 5ee18b9c Michael Walle
        latency = LATENCY_SIN;
243 5ee18b9c Michael Walle
        D_EXEC(qemu_log("SIN a=%d t=%f, r=%08x\n", a, t, r));
244 5ee18b9c Michael Walle
    } break;
245 5ee18b9c Michael Walle
    case OP_COS:
246 5ee18b9c Michael Walle
    {
247 5ee18b9c Michael Walle
        int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
248 5ee18b9c Michael Walle
        float t = cosf(a * (1.0f / (M_PI * 4096.0f)));
249 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
250 5ee18b9c Michael Walle
        latency = LATENCY_COS;
251 5ee18b9c Michael Walle
        D_EXEC(qemu_log("COS a=%d t=%f, r=%08x\n", a, t, r));
252 5ee18b9c Michael Walle
    } break;
253 5ee18b9c Michael Walle
    case OP_ABOVE:
254 5ee18b9c Michael Walle
    {
255 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
256 5ee18b9c Michael Walle
        float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
257 5ee18b9c Michael Walle
        float t = (a > b) ? 1.0f : 0.0f;
258 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
259 5ee18b9c Michael Walle
        latency = LATENCY_ABOVE;
260 5ee18b9c Michael Walle
        D_EXEC(qemu_log("ABOVE a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
261 5ee18b9c Michael Walle
    } break;
262 5ee18b9c Michael Walle
    case OP_EQUAL:
263 5ee18b9c Michael Walle
    {
264 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
265 5ee18b9c Michael Walle
        float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
266 5ee18b9c Michael Walle
        float t = (a == b) ? 1.0f : 0.0f;
267 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
268 5ee18b9c Michael Walle
        latency = LATENCY_EQUAL;
269 5ee18b9c Michael Walle
        D_EXEC(qemu_log("EQUAL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
270 5ee18b9c Michael Walle
    } break;
271 5ee18b9c Michael Walle
    case OP_COPY:
272 5ee18b9c Michael Walle
    {
273 5ee18b9c Michael Walle
        r = s->gp_regs[reg_a];
274 5ee18b9c Michael Walle
        latency = LATENCY_COPY;
275 5ee18b9c Michael Walle
        D_EXEC(qemu_log("COPY"));
276 5ee18b9c Michael Walle
    } break;
277 5ee18b9c Michael Walle
    case OP_IF:
278 5ee18b9c Michael Walle
    {
279 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
280 5ee18b9c Michael Walle
        float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
281 5ee18b9c Michael Walle
        uint32_t f = s->gp_regs[GPR_FLAGS];
282 5ee18b9c Michael Walle
        float t = (f != 0) ? a : b;
283 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
284 5ee18b9c Michael Walle
        latency = LATENCY_IF;
285 5ee18b9c Michael Walle
        D_EXEC(qemu_log("IF f=%u a=%f b=%f t=%f, r=%08x\n", f, a, b, t, r));
286 5ee18b9c Michael Walle
    } break;
287 5ee18b9c Michael Walle
    case OP_TSIGN:
288 5ee18b9c Michael Walle
    {
289 5ee18b9c Michael Walle
        float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
290 5ee18b9c Michael Walle
        float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
291 5ee18b9c Michael Walle
        float t = (b < 0) ? -a : a;
292 5ee18b9c Michael Walle
        r = REINTERPRET_CAST(uint32_t, t);
293 5ee18b9c Michael Walle
        latency = LATENCY_TSIGN;
294 5ee18b9c Michael Walle
        D_EXEC(qemu_log("TSIGN a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
295 5ee18b9c Michael Walle
    } break;
296 5ee18b9c Michael Walle
    case OP_QUAKE:
297 5ee18b9c Michael Walle
    {
298 5ee18b9c Michael Walle
        uint32_t a = s->gp_regs[reg_a];
299 5ee18b9c Michael Walle
        r = 0x5f3759df - (a >> 1);
300 5ee18b9c Michael Walle
        latency = LATENCY_QUAKE;
301 5ee18b9c Michael Walle
        D_EXEC(qemu_log("QUAKE a=%d r=%08x\n", a, r));
302 5ee18b9c Michael Walle
    } break;
303 5ee18b9c Michael Walle
304 5ee18b9c Michael Walle
    default:
305 6daf194d Markus Armbruster
        error_report("milkymist_pfpu: unknown opcode %d", op);
306 5ee18b9c Michael Walle
        break;
307 5ee18b9c Michael Walle
    }
308 5ee18b9c Michael Walle
309 5ee18b9c Michael Walle
    if (!reg_d) {
310 5ee18b9c Michael Walle
        D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d>\n",
311 5ee18b9c Michael Walle
                    s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
312 5ee18b9c Michael Walle
                    s->regs[R_PC] + latency));
313 5ee18b9c Michael Walle
    } else {
314 5ee18b9c Michael Walle
        D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d> -> R%03d\n",
315 5ee18b9c Michael Walle
                    s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
316 5ee18b9c Michael Walle
                    s->regs[R_PC] + latency, reg_d));
317 5ee18b9c Michael Walle
    }
318 5ee18b9c Michael Walle
319 5ee18b9c Michael Walle
    if (op == OP_VECTOUT) {
320 5ee18b9c Michael Walle
        return 0;
321 5ee18b9c Michael Walle
    }
322 5ee18b9c Michael Walle
323 5ee18b9c Michael Walle
    /* store output for this cycle */
324 5ee18b9c Michael Walle
    if (reg_d) {
325 5ee18b9c Michael Walle
        uint32_t val = output_queue_remove(s);
326 5ee18b9c Michael Walle
        D_EXEC(qemu_log("R%03d <- 0x%08x\n", reg_d, val));
327 5ee18b9c Michael Walle
        s->gp_regs[reg_d] = val;
328 5ee18b9c Michael Walle
    }
329 5ee18b9c Michael Walle
330 5ee18b9c Michael Walle
    output_queue_advance(s);
331 5ee18b9c Michael Walle
332 5ee18b9c Michael Walle
    /* store op output */
333 5ee18b9c Michael Walle
    if (op != OP_NOP) {
334 5ee18b9c Michael Walle
        output_queue_insert(s, r, latency-1);
335 5ee18b9c Michael Walle
    }
336 5ee18b9c Michael Walle
337 5ee18b9c Michael Walle
    /* advance PC */
338 5ee18b9c Michael Walle
    s->regs[R_PC]++;
339 5ee18b9c Michael Walle
340 5ee18b9c Michael Walle
    return 1;
341 5ee18b9c Michael Walle
};
342 5ee18b9c Michael Walle
343 5ee18b9c Michael Walle
static void pfpu_start(MilkymistPFPUState *s)
344 5ee18b9c Michael Walle
{
345 5ee18b9c Michael Walle
    int x, y;
346 5ee18b9c Michael Walle
    int i;
347 5ee18b9c Michael Walle
348 5ee18b9c Michael Walle
    for (y = 0; y <= s->regs[R_VMESHLAST]; y++) {
349 5ee18b9c Michael Walle
        for (x = 0; x <= s->regs[R_HMESHLAST]; x++) {
350 5ee18b9c Michael Walle
            D_EXEC(qemu_log("\nprocessing x=%d y=%d\n", x, y));
351 5ee18b9c Michael Walle
352 5ee18b9c Michael Walle
            /* set current position */
353 5ee18b9c Michael Walle
            s->gp_regs[GPR_X] = x;
354 5ee18b9c Michael Walle
            s->gp_regs[GPR_Y] = y;
355 5ee18b9c Michael Walle
356 5ee18b9c Michael Walle
            /* run microcode on this position */
357 5ee18b9c Michael Walle
            i = 0;
358 5ee18b9c Michael Walle
            while (pfpu_decode_insn(s)) {
359 5ee18b9c Michael Walle
                /* decode at most MICROCODE_WORDS instructions */
360 5ee18b9c Michael Walle
                if (i++ >= MICROCODE_WORDS) {
361 5ee18b9c Michael Walle
                    error_report("milkymist_pfpu: too many instructions "
362 6daf194d Markus Armbruster
                            "executed in microcode. No VECTOUT?");
363 5ee18b9c Michael Walle
                    break;
364 5ee18b9c Michael Walle
                }
365 5ee18b9c Michael Walle
            }
366 5ee18b9c Michael Walle
367 5ee18b9c Michael Walle
            /* reset pc for next run */
368 5ee18b9c Michael Walle
            s->regs[R_PC] = 0;
369 5ee18b9c Michael Walle
        }
370 5ee18b9c Michael Walle
    }
371 5ee18b9c Michael Walle
372 5ee18b9c Michael Walle
    s->regs[R_VERTICES] = x * y;
373 5ee18b9c Michael Walle
374 5ee18b9c Michael Walle
    trace_milkymist_pfpu_pulse_irq();
375 5ee18b9c Michael Walle
    qemu_irq_pulse(s->irq);
376 5ee18b9c Michael Walle
}
377 5ee18b9c Michael Walle
378 5ee18b9c Michael Walle
static inline int get_microcode_address(MilkymistPFPUState *s, uint32_t addr)
379 5ee18b9c Michael Walle
{
380 5ee18b9c Michael Walle
    return (512 * s->regs[R_CODEPAGE]) + addr - MICROCODE_BEGIN;
381 5ee18b9c Michael Walle
}
382 5ee18b9c Michael Walle
383 a8170e5e Avi Kivity
static uint64_t pfpu_read(void *opaque, hwaddr addr,
384 d46ccfce Michael Walle
                          unsigned size)
385 5ee18b9c Michael Walle
{
386 5ee18b9c Michael Walle
    MilkymistPFPUState *s = opaque;
387 5ee18b9c Michael Walle
    uint32_t r = 0;
388 5ee18b9c Michael Walle
389 5ee18b9c Michael Walle
    addr >>= 2;
390 5ee18b9c Michael Walle
    switch (addr) {
391 5ee18b9c Michael Walle
    case R_CTL:
392 5ee18b9c Michael Walle
    case R_MESHBASE:
393 5ee18b9c Michael Walle
    case R_HMESHLAST:
394 5ee18b9c Michael Walle
    case R_VMESHLAST:
395 5ee18b9c Michael Walle
    case R_CODEPAGE:
396 5ee18b9c Michael Walle
    case R_VERTICES:
397 5ee18b9c Michael Walle
    case R_COLLISIONS:
398 5ee18b9c Michael Walle
    case R_STRAYWRITES:
399 5ee18b9c Michael Walle
    case R_LASTDMA:
400 5ee18b9c Michael Walle
    case R_PC:
401 5ee18b9c Michael Walle
    case R_DREGBASE:
402 5ee18b9c Michael Walle
    case R_CODEBASE:
403 5ee18b9c Michael Walle
        r = s->regs[addr];
404 5ee18b9c Michael Walle
        break;
405 5ee18b9c Michael Walle
    case GPR_BEGIN ... GPR_END:
406 5ee18b9c Michael Walle
        r = s->gp_regs[addr - GPR_BEGIN];
407 5ee18b9c Michael Walle
        break;
408 5ee18b9c Michael Walle
    case MICROCODE_BEGIN ...  MICROCODE_END:
409 5ee18b9c Michael Walle
        r = s->microcode[get_microcode_address(s, addr)];
410 5ee18b9c Michael Walle
        break;
411 5ee18b9c Michael Walle
412 5ee18b9c Michael Walle
    default:
413 5ee18b9c Michael Walle
        error_report("milkymist_pfpu: read access to unknown register 0x"
414 5ee18b9c Michael Walle
                TARGET_FMT_plx, addr << 2);
415 5ee18b9c Michael Walle
        break;
416 5ee18b9c Michael Walle
    }
417 5ee18b9c Michael Walle
418 5ee18b9c Michael Walle
    trace_milkymist_pfpu_memory_read(addr << 2, r);
419 5ee18b9c Michael Walle
420 5ee18b9c Michael Walle
    return r;
421 5ee18b9c Michael Walle
}
422 5ee18b9c Michael Walle
423 a8170e5e Avi Kivity
static void pfpu_write(void *opaque, hwaddr addr, uint64_t value,
424 d46ccfce Michael Walle
                       unsigned size)
425 5ee18b9c Michael Walle
{
426 5ee18b9c Michael Walle
    MilkymistPFPUState *s = opaque;
427 5ee18b9c Michael Walle
428 5ee18b9c Michael Walle
    trace_milkymist_pfpu_memory_write(addr, value);
429 5ee18b9c Michael Walle
430 5ee18b9c Michael Walle
    addr >>= 2;
431 5ee18b9c Michael Walle
    switch (addr) {
432 5ee18b9c Michael Walle
    case R_CTL:
433 5ee18b9c Michael Walle
        if (value & CTL_START_BUSY) {
434 5ee18b9c Michael Walle
            pfpu_start(s);
435 5ee18b9c Michael Walle
        }
436 5ee18b9c Michael Walle
        break;
437 5ee18b9c Michael Walle
    case R_MESHBASE:
438 5ee18b9c Michael Walle
    case R_HMESHLAST:
439 5ee18b9c Michael Walle
    case R_VMESHLAST:
440 5ee18b9c Michael Walle
    case R_CODEPAGE:
441 5ee18b9c Michael Walle
    case R_VERTICES:
442 5ee18b9c Michael Walle
    case R_COLLISIONS:
443 5ee18b9c Michael Walle
    case R_STRAYWRITES:
444 5ee18b9c Michael Walle
    case R_LASTDMA:
445 5ee18b9c Michael Walle
    case R_PC:
446 5ee18b9c Michael Walle
    case R_DREGBASE:
447 5ee18b9c Michael Walle
    case R_CODEBASE:
448 5ee18b9c Michael Walle
        s->regs[addr] = value;
449 5ee18b9c Michael Walle
        break;
450 5ee18b9c Michael Walle
    case GPR_BEGIN ...  GPR_END:
451 5ee18b9c Michael Walle
        s->gp_regs[addr - GPR_BEGIN] = value;
452 5ee18b9c Michael Walle
        break;
453 5ee18b9c Michael Walle
    case MICROCODE_BEGIN ...  MICROCODE_END:
454 5ee18b9c Michael Walle
        s->microcode[get_microcode_address(s, addr)] = value;
455 5ee18b9c Michael Walle
        break;
456 5ee18b9c Michael Walle
457 5ee18b9c Michael Walle
    default:
458 5ee18b9c Michael Walle
        error_report("milkymist_pfpu: write access to unknown register 0x"
459 5ee18b9c Michael Walle
                TARGET_FMT_plx, addr << 2);
460 5ee18b9c Michael Walle
        break;
461 5ee18b9c Michael Walle
    }
462 5ee18b9c Michael Walle
}
463 5ee18b9c Michael Walle
464 d46ccfce Michael Walle
static const MemoryRegionOps pfpu_mmio_ops = {
465 d46ccfce Michael Walle
    .read = pfpu_read,
466 d46ccfce Michael Walle
    .write = pfpu_write,
467 d46ccfce Michael Walle
    .valid = {
468 d46ccfce Michael Walle
        .min_access_size = 4,
469 d46ccfce Michael Walle
        .max_access_size = 4,
470 d46ccfce Michael Walle
    },
471 d46ccfce Michael Walle
    .endianness = DEVICE_NATIVE_ENDIAN,
472 5ee18b9c Michael Walle
};
473 5ee18b9c Michael Walle
474 5ee18b9c Michael Walle
static void milkymist_pfpu_reset(DeviceState *d)
475 5ee18b9c Michael Walle
{
476 5ee18b9c Michael Walle
    MilkymistPFPUState *s = container_of(d, MilkymistPFPUState, busdev.qdev);
477 5ee18b9c Michael Walle
    int i;
478 5ee18b9c Michael Walle
479 5ee18b9c Michael Walle
    for (i = 0; i < R_MAX; i++) {
480 5ee18b9c Michael Walle
        s->regs[i] = 0;
481 5ee18b9c Michael Walle
    }
482 5ee18b9c Michael Walle
    for (i = 0; i < 128; i++) {
483 5ee18b9c Michael Walle
        s->gp_regs[i] = 0;
484 5ee18b9c Michael Walle
    }
485 5ee18b9c Michael Walle
    for (i = 0; i < MICROCODE_WORDS; i++) {
486 5ee18b9c Michael Walle
        s->microcode[i] = 0;
487 5ee18b9c Michael Walle
    }
488 5ee18b9c Michael Walle
    s->output_queue_pos = 0;
489 5ee18b9c Michael Walle
    for (i = 0; i < MAX_LATENCY; i++) {
490 5ee18b9c Michael Walle
        s->output_queue[i] = 0;
491 5ee18b9c Michael Walle
    }
492 5ee18b9c Michael Walle
}
493 5ee18b9c Michael Walle
494 5ee18b9c Michael Walle
static int milkymist_pfpu_init(SysBusDevice *dev)
495 5ee18b9c Michael Walle
{
496 5ee18b9c Michael Walle
    MilkymistPFPUState *s = FROM_SYSBUS(typeof(*s), dev);
497 5ee18b9c Michael Walle
498 5ee18b9c Michael Walle
    sysbus_init_irq(dev, &s->irq);
499 5ee18b9c Michael Walle
500 d46ccfce Michael Walle
    memory_region_init_io(&s->regs_region, &pfpu_mmio_ops, s,
501 d46ccfce Michael Walle
            "milkymist-pfpu", MICROCODE_END * 4);
502 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->regs_region);
503 5ee18b9c Michael Walle
504 5ee18b9c Michael Walle
    return 0;
505 5ee18b9c Michael Walle
}
506 5ee18b9c Michael Walle
507 5ee18b9c Michael Walle
static const VMStateDescription vmstate_milkymist_pfpu = {
508 5ee18b9c Michael Walle
    .name = "milkymist-pfpu",
509 5ee18b9c Michael Walle
    .version_id = 1,
510 5ee18b9c Michael Walle
    .minimum_version_id = 1,
511 5ee18b9c Michael Walle
    .minimum_version_id_old = 1,
512 5ee18b9c Michael Walle
    .fields      = (VMStateField[]) {
513 5ee18b9c Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistPFPUState, R_MAX),
514 5ee18b9c Michael Walle
        VMSTATE_UINT32_ARRAY(gp_regs, MilkymistPFPUState, 128),
515 5ee18b9c Michael Walle
        VMSTATE_UINT32_ARRAY(microcode, MilkymistPFPUState, MICROCODE_WORDS),
516 5ee18b9c Michael Walle
        VMSTATE_INT32(output_queue_pos, MilkymistPFPUState),
517 5ee18b9c Michael Walle
        VMSTATE_UINT32_ARRAY(output_queue, MilkymistPFPUState, MAX_LATENCY),
518 5ee18b9c Michael Walle
        VMSTATE_END_OF_LIST()
519 5ee18b9c Michael Walle
    }
520 5ee18b9c Michael Walle
};
521 5ee18b9c Michael Walle
522 999e12bb Anthony Liguori
static void milkymist_pfpu_class_init(ObjectClass *klass, void *data)
523 999e12bb Anthony Liguori
{
524 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
525 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
526 999e12bb Anthony Liguori
527 999e12bb Anthony Liguori
    k->init = milkymist_pfpu_init;
528 39bffca2 Anthony Liguori
    dc->reset = milkymist_pfpu_reset;
529 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_milkymist_pfpu;
530 999e12bb Anthony Liguori
}
531 999e12bb Anthony Liguori
532 8c43a6f0 Andreas Färber
static const TypeInfo milkymist_pfpu_info = {
533 39bffca2 Anthony Liguori
    .name          = "milkymist-pfpu",
534 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
535 39bffca2 Anthony Liguori
    .instance_size = sizeof(MilkymistPFPUState),
536 39bffca2 Anthony Liguori
    .class_init    = milkymist_pfpu_class_init,
537 5ee18b9c Michael Walle
};
538 5ee18b9c Michael Walle
539 83f7d43a Andreas Färber
static void milkymist_pfpu_register_types(void)
540 5ee18b9c Michael Walle
{
541 39bffca2 Anthony Liguori
    type_register_static(&milkymist_pfpu_info);
542 5ee18b9c Michael Walle
}
543 5ee18b9c Michael Walle
544 83f7d43a Andreas Färber
type_init(milkymist_pfpu_register_types)