2 * QEMU model of the Milkymist programmable FPU.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Specification available at:
21 * http://milkymist.walle.cc/socdoc/pfpu.pdf
25 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
28 #include "migration/vmstate.h"
31 #include "qemu/module.h"
32 #include "qemu/error-report.h"
35 /* #define TRACE_EXEC */
60 CTL_START_BUSY = (1<<0),
107 #define GPR_BEGIN 0x100
108 #define GPR_END 0x17f
109 #define MICROCODE_BEGIN 0x200
110 #define MICROCODE_END 0x3ff
111 #define MICROCODE_WORDS 2048
113 #define REINTERPRET_CAST(type, val) (*((type *)&(val)))
116 static const char *opcode_to_str[] = {
117 "NOP", "FADD", "FSUB", "FMUL", "FABS", "F2I", "I2F", "VECTOUT",
118 "SIN", "COS", "ABOVE", "EQUAL", "COPY", "IF", "TSIGN", "QUAKE",
122 #define TYPE_MILKYMIST_PFPU "milkymist-pfpu"
123 #define MILKYMIST_PFPU(obj) \
124 OBJECT_CHECK(MilkymistPFPUState, (obj), TYPE_MILKYMIST_PFPU)
126 struct MilkymistPFPUState {
127 SysBusDevice parent_obj;
129 MemoryRegion regs_region;
133 uint32_t regs[R_MAX];
134 uint32_t gp_regs[128];
135 uint32_t microcode[MICROCODE_WORDS];
137 int output_queue_pos;
138 uint32_t output_queue[MAX_LATENCY];
140 typedef struct MilkymistPFPUState MilkymistPFPUState;
142 static inline uint32_t
143 get_dma_address(uint32_t base, uint32_t x, uint32_t y)
145 return base + 8 * (128 * y + x);
149 output_queue_insert(MilkymistPFPUState *s, uint32_t val, int pos)
151 s->output_queue[(s->output_queue_pos + pos) % MAX_LATENCY] = val;
154 static inline uint32_t
155 output_queue_remove(MilkymistPFPUState *s)
157 return s->output_queue[s->output_queue_pos];
161 output_queue_advance(MilkymistPFPUState *s)
163 s->output_queue[s->output_queue_pos] = 0;
164 s->output_queue_pos = (s->output_queue_pos + 1) % MAX_LATENCY;
167 static int pfpu_decode_insn(MilkymistPFPUState *s)
169 uint32_t pc = s->regs[R_PC];
170 uint32_t insn = s->microcode[pc];
171 uint32_t reg_a = (insn >> 18) & 0x7f;
172 uint32_t reg_b = (insn >> 11) & 0x7f;
173 uint32_t op = (insn >> 7) & 0xf;
174 uint32_t reg_d = insn & 0x7f;
183 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
184 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
186 r = REINTERPRET_CAST(uint32_t, t);
187 latency = LATENCY_FADD;
188 D_EXEC(qemu_log("ADD a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
192 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
193 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
195 r = REINTERPRET_CAST(uint32_t, t);
196 latency = LATENCY_FSUB;
197 D_EXEC(qemu_log("SUB a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
201 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
202 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
204 r = REINTERPRET_CAST(uint32_t, t);
205 latency = LATENCY_FMUL;
206 D_EXEC(qemu_log("MUL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
210 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
212 r = REINTERPRET_CAST(uint32_t, t);
213 latency = LATENCY_FABS;
214 D_EXEC(qemu_log("ABS a=%f t=%f, r=%08x\n", a, t, r));
218 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
220 r = REINTERPRET_CAST(uint32_t, t);
221 latency = LATENCY_F2I;
222 D_EXEC(qemu_log("F2I a=%f t=%d, r=%08x\n", a, t, r));
226 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
228 r = REINTERPRET_CAST(uint32_t, t);
229 latency = LATENCY_I2F;
230 D_EXEC(qemu_log("I2F a=%08x t=%f, r=%08x\n", a, t, r));
234 uint32_t a = cpu_to_be32(s->gp_regs[reg_a]);
235 uint32_t b = cpu_to_be32(s->gp_regs[reg_b]);
237 get_dma_address(s->regs[R_MESHBASE],
238 s->gp_regs[GPR_X], s->gp_regs[GPR_Y]);
239 cpu_physical_memory_write(dma_ptr, &a, 4);
240 cpu_physical_memory_write(dma_ptr + 4, &b, 4);
241 s->regs[R_LASTDMA] = dma_ptr + 4;
242 D_EXEC(qemu_log("VECTOUT a=%08x b=%08x dma=%08x\n", a, b, dma_ptr));
243 trace_milkymist_pfpu_vectout(a, b, dma_ptr);
247 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
248 float t = sinf(a * (1.0f / (M_PI * 4096.0f)));
249 r = REINTERPRET_CAST(uint32_t, t);
250 latency = LATENCY_SIN;
251 D_EXEC(qemu_log("SIN a=%d t=%f, r=%08x\n", a, t, r));
255 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
256 float t = cosf(a * (1.0f / (M_PI * 4096.0f)));
257 r = REINTERPRET_CAST(uint32_t, t);
258 latency = LATENCY_COS;
259 D_EXEC(qemu_log("COS a=%d t=%f, r=%08x\n", a, t, r));
263 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
264 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
265 float t = (a > b) ? 1.0f : 0.0f;
266 r = REINTERPRET_CAST(uint32_t, t);
267 latency = LATENCY_ABOVE;
268 D_EXEC(qemu_log("ABOVE a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
272 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
273 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
274 float t = (a == b) ? 1.0f : 0.0f;
275 r = REINTERPRET_CAST(uint32_t, t);
276 latency = LATENCY_EQUAL;
277 D_EXEC(qemu_log("EQUAL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
281 r = s->gp_regs[reg_a];
282 latency = LATENCY_COPY;
283 D_EXEC(qemu_log("COPY"));
287 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
288 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
289 uint32_t f = s->gp_regs[GPR_FLAGS];
290 float t = (f != 0) ? a : b;
291 r = REINTERPRET_CAST(uint32_t, t);
292 latency = LATENCY_IF;
293 D_EXEC(qemu_log("IF f=%u a=%f b=%f t=%f, r=%08x\n", f, a, b, t, r));
297 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
298 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
299 float t = (b < 0) ? -a : a;
300 r = REINTERPRET_CAST(uint32_t, t);
301 latency = LATENCY_TSIGN;
302 D_EXEC(qemu_log("TSIGN a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
306 uint32_t a = s->gp_regs[reg_a];
307 r = 0x5f3759df - (a >> 1);
308 latency = LATENCY_QUAKE;
309 D_EXEC(qemu_log("QUAKE a=%d r=%08x\n", a, r));
313 error_report("milkymist_pfpu: unknown opcode %d", op);
318 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d>\n",
319 s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
320 s->regs[R_PC] + latency));
322 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d> -> R%03d\n",
323 s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
324 s->regs[R_PC] + latency, reg_d));
327 if (op == OP_VECTOUT) {
331 /* store output for this cycle */
333 uint32_t val = output_queue_remove(s);
334 D_EXEC(qemu_log("R%03d <- 0x%08x\n", reg_d, val));
335 s->gp_regs[reg_d] = val;
338 output_queue_advance(s);
340 /* store op output */
342 output_queue_insert(s, r, latency-1);
351 static void pfpu_start(MilkymistPFPUState *s)
356 for (y = 0; y <= s->regs[R_VMESHLAST]; y++) {
357 for (x = 0; x <= s->regs[R_HMESHLAST]; x++) {
358 D_EXEC(qemu_log("\nprocessing x=%d y=%d\n", x, y));
360 /* set current position */
361 s->gp_regs[GPR_X] = x;
362 s->gp_regs[GPR_Y] = y;
364 /* run microcode on this position */
366 while (pfpu_decode_insn(s)) {
367 /* decode at most MICROCODE_WORDS instructions */
368 if (++i >= MICROCODE_WORDS) {
369 error_report("milkymist_pfpu: too many instructions "
370 "executed in microcode. No VECTOUT?");
375 /* reset pc for next run */
380 s->regs[R_VERTICES] = x * y;
382 trace_milkymist_pfpu_pulse_irq();
383 qemu_irq_pulse(s->irq);
386 static inline int get_microcode_address(MilkymistPFPUState *s, uint32_t addr)
388 return (512 * s->regs[R_CODEPAGE]) + addr - MICROCODE_BEGIN;
391 static uint64_t pfpu_read(void *opaque, hwaddr addr,
394 MilkymistPFPUState *s = opaque;
413 case GPR_BEGIN ... GPR_END:
414 r = s->gp_regs[addr - GPR_BEGIN];
416 case MICROCODE_BEGIN ... MICROCODE_END:
417 r = s->microcode[get_microcode_address(s, addr)];
421 error_report("milkymist_pfpu: read access to unknown register 0x"
422 TARGET_FMT_plx, addr << 2);
426 trace_milkymist_pfpu_memory_read(addr << 2, r);
431 static void pfpu_write(void *opaque, hwaddr addr, uint64_t value,
434 MilkymistPFPUState *s = opaque;
436 trace_milkymist_pfpu_memory_write(addr, value);
441 if (value & CTL_START_BUSY) {
456 s->regs[addr] = value;
458 case GPR_BEGIN ... GPR_END:
459 s->gp_regs[addr - GPR_BEGIN] = value;
461 case MICROCODE_BEGIN ... MICROCODE_END:
462 s->microcode[get_microcode_address(s, addr)] = value;
466 error_report("milkymist_pfpu: write access to unknown register 0x"
467 TARGET_FMT_plx, addr << 2);
472 static const MemoryRegionOps pfpu_mmio_ops = {
476 .min_access_size = 4,
477 .max_access_size = 4,
479 .endianness = DEVICE_NATIVE_ENDIAN,
482 static void milkymist_pfpu_reset(DeviceState *d)
484 MilkymistPFPUState *s = MILKYMIST_PFPU(d);
487 for (i = 0; i < R_MAX; i++) {
490 for (i = 0; i < 128; i++) {
493 for (i = 0; i < MICROCODE_WORDS; i++) {
496 s->output_queue_pos = 0;
497 for (i = 0; i < MAX_LATENCY; i++) {
498 s->output_queue[i] = 0;
502 static void milkymist_pfpu_realize(DeviceState *dev, Error **errp)
504 MilkymistPFPUState *s = MILKYMIST_PFPU(dev);
505 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
507 sysbus_init_irq(sbd, &s->irq);
509 memory_region_init_io(&s->regs_region, OBJECT(dev), &pfpu_mmio_ops, s,
510 "milkymist-pfpu", MICROCODE_END * 4);
511 sysbus_init_mmio(sbd, &s->regs_region);
514 static const VMStateDescription vmstate_milkymist_pfpu = {
515 .name = "milkymist-pfpu",
517 .minimum_version_id = 1,
518 .fields = (VMStateField[]) {
519 VMSTATE_UINT32_ARRAY(regs, MilkymistPFPUState, R_MAX),
520 VMSTATE_UINT32_ARRAY(gp_regs, MilkymistPFPUState, 128),
521 VMSTATE_UINT32_ARRAY(microcode, MilkymistPFPUState, MICROCODE_WORDS),
522 VMSTATE_INT32(output_queue_pos, MilkymistPFPUState),
523 VMSTATE_UINT32_ARRAY(output_queue, MilkymistPFPUState, MAX_LATENCY),
524 VMSTATE_END_OF_LIST()
528 static void milkymist_pfpu_class_init(ObjectClass *klass, void *data)
530 DeviceClass *dc = DEVICE_CLASS(klass);
532 dc->realize = milkymist_pfpu_realize;
533 dc->reset = milkymist_pfpu_reset;
534 dc->vmsd = &vmstate_milkymist_pfpu;
537 static const TypeInfo milkymist_pfpu_info = {
538 .name = TYPE_MILKYMIST_PFPU,
539 .parent = TYPE_SYS_BUS_DEVICE,
540 .instance_size = sizeof(MilkymistPFPUState),
541 .class_init = milkymist_pfpu_class_init,
544 static void milkymist_pfpu_register_types(void)
546 type_register_static(&milkymist_pfpu_info);
549 type_init(milkymist_pfpu_register_types)