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://www.milkymist.org/socdoc/pfpu.pdf
26 #include "hw/sysbus.h"
29 #include "qemu/error-report.h"
32 /* #define TRACE_EXEC */
57 CTL_START_BUSY = (1<<0),
104 #define GPR_BEGIN 0x100
105 #define GPR_END 0x17f
106 #define MICROCODE_BEGIN 0x200
107 #define MICROCODE_END 0x3ff
108 #define MICROCODE_WORDS 2048
110 #define REINTERPRET_CAST(type, val) (*((type *)&(val)))
113 static const char *opcode_to_str[] = {
114 "NOP", "FADD", "FSUB", "FMUL", "FABS", "F2I", "I2F", "VECTOUT",
115 "SIN", "COS", "ABOVE", "EQUAL", "COPY", "IF", "TSIGN", "QUAKE",
119 struct MilkymistPFPUState {
121 MemoryRegion regs_region;
122 CharDriverState *chr;
125 uint32_t regs[R_MAX];
126 uint32_t gp_regs[128];
127 uint32_t microcode[MICROCODE_WORDS];
129 int output_queue_pos;
130 uint32_t output_queue[MAX_LATENCY];
132 typedef struct MilkymistPFPUState MilkymistPFPUState;
135 get_dma_address(uint32_t base, uint32_t x, uint32_t y)
137 return base + 8 * (128 * y + x);
141 output_queue_insert(MilkymistPFPUState *s, uint32_t val, int pos)
143 s->output_queue[(s->output_queue_pos + pos) % MAX_LATENCY] = val;
146 static inline uint32_t
147 output_queue_remove(MilkymistPFPUState *s)
149 return s->output_queue[s->output_queue_pos];
153 output_queue_advance(MilkymistPFPUState *s)
155 s->output_queue[s->output_queue_pos] = 0;
156 s->output_queue_pos = (s->output_queue_pos + 1) % MAX_LATENCY;
159 static int pfpu_decode_insn(MilkymistPFPUState *s)
161 uint32_t pc = s->regs[R_PC];
162 uint32_t insn = s->microcode[pc];
163 uint32_t reg_a = (insn >> 18) & 0x7f;
164 uint32_t reg_b = (insn >> 11) & 0x7f;
165 uint32_t op = (insn >> 7) & 0xf;
166 uint32_t reg_d = insn & 0x7f;
175 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
176 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
178 r = REINTERPRET_CAST(uint32_t, t);
179 latency = LATENCY_FADD;
180 D_EXEC(qemu_log("ADD a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
184 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
185 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
187 r = REINTERPRET_CAST(uint32_t, t);
188 latency = LATENCY_FSUB;
189 D_EXEC(qemu_log("SUB a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
193 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
194 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
196 r = REINTERPRET_CAST(uint32_t, t);
197 latency = LATENCY_FMUL;
198 D_EXEC(qemu_log("MUL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
202 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
204 r = REINTERPRET_CAST(uint32_t, t);
205 latency = LATENCY_FABS;
206 D_EXEC(qemu_log("ABS a=%f t=%f, r=%08x\n", a, t, r));
210 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
212 r = REINTERPRET_CAST(uint32_t, t);
213 latency = LATENCY_F2I;
214 D_EXEC(qemu_log("F2I a=%f t=%d, r=%08x\n", a, t, r));
218 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
220 r = REINTERPRET_CAST(uint32_t, t);
221 latency = LATENCY_I2F;
222 D_EXEC(qemu_log("I2F a=%08x t=%f, r=%08x\n", a, t, r));
226 uint32_t a = cpu_to_be32(s->gp_regs[reg_a]);
227 uint32_t b = cpu_to_be32(s->gp_regs[reg_b]);
229 get_dma_address(s->regs[R_MESHBASE],
230 s->gp_regs[GPR_X], s->gp_regs[GPR_Y]);
231 cpu_physical_memory_write(dma_ptr, &a, 4);
232 cpu_physical_memory_write(dma_ptr + 4, &b, 4);
233 s->regs[R_LASTDMA] = dma_ptr + 4;
234 D_EXEC(qemu_log("VECTOUT a=%08x b=%08x dma=%08x\n", a, b, dma_ptr));
235 trace_milkymist_pfpu_vectout(a, b, dma_ptr);
239 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
240 float t = sinf(a * (1.0f / (M_PI * 4096.0f)));
241 r = REINTERPRET_CAST(uint32_t, t);
242 latency = LATENCY_SIN;
243 D_EXEC(qemu_log("SIN a=%d t=%f, r=%08x\n", a, t, r));
247 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
248 float t = cosf(a * (1.0f / (M_PI * 4096.0f)));
249 r = REINTERPRET_CAST(uint32_t, t);
250 latency = LATENCY_COS;
251 D_EXEC(qemu_log("COS a=%d t=%f, r=%08x\n", a, t, r));
255 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
256 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
257 float t = (a > b) ? 1.0f : 0.0f;
258 r = REINTERPRET_CAST(uint32_t, t);
259 latency = LATENCY_ABOVE;
260 D_EXEC(qemu_log("ABOVE a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
264 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
265 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
266 float t = (a == b) ? 1.0f : 0.0f;
267 r = REINTERPRET_CAST(uint32_t, t);
268 latency = LATENCY_EQUAL;
269 D_EXEC(qemu_log("EQUAL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
273 r = s->gp_regs[reg_a];
274 latency = LATENCY_COPY;
275 D_EXEC(qemu_log("COPY"));
279 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
280 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
281 uint32_t f = s->gp_regs[GPR_FLAGS];
282 float t = (f != 0) ? a : b;
283 r = REINTERPRET_CAST(uint32_t, t);
284 latency = LATENCY_IF;
285 D_EXEC(qemu_log("IF f=%u a=%f b=%f t=%f, r=%08x\n", f, a, b, t, r));
289 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
290 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
291 float t = (b < 0) ? -a : a;
292 r = REINTERPRET_CAST(uint32_t, t);
293 latency = LATENCY_TSIGN;
294 D_EXEC(qemu_log("TSIGN a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
298 uint32_t a = s->gp_regs[reg_a];
299 r = 0x5f3759df - (a >> 1);
300 latency = LATENCY_QUAKE;
301 D_EXEC(qemu_log("QUAKE a=%d r=%08x\n", a, r));
305 error_report("milkymist_pfpu: unknown opcode %d", op);
310 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d>\n",
311 s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
312 s->regs[R_PC] + latency));
314 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d> -> R%03d\n",
315 s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
316 s->regs[R_PC] + latency, reg_d));
319 if (op == OP_VECTOUT) {
323 /* store output for this cycle */
325 uint32_t val = output_queue_remove(s);
326 D_EXEC(qemu_log("R%03d <- 0x%08x\n", reg_d, val));
327 s->gp_regs[reg_d] = val;
330 output_queue_advance(s);
332 /* store op output */
334 output_queue_insert(s, r, latency-1);
343 static void pfpu_start(MilkymistPFPUState *s)
348 for (y = 0; y <= s->regs[R_VMESHLAST]; y++) {
349 for (x = 0; x <= s->regs[R_HMESHLAST]; x++) {
350 D_EXEC(qemu_log("\nprocessing x=%d y=%d\n", x, y));
352 /* set current position */
353 s->gp_regs[GPR_X] = x;
354 s->gp_regs[GPR_Y] = y;
356 /* run microcode on this position */
358 while (pfpu_decode_insn(s)) {
359 /* decode at most MICROCODE_WORDS instructions */
360 if (i++ >= MICROCODE_WORDS) {
361 error_report("milkymist_pfpu: too many instructions "
362 "executed in microcode. No VECTOUT?");
367 /* reset pc for next run */
372 s->regs[R_VERTICES] = x * y;
374 trace_milkymist_pfpu_pulse_irq();
375 qemu_irq_pulse(s->irq);
378 static inline int get_microcode_address(MilkymistPFPUState *s, uint32_t addr)
380 return (512 * s->regs[R_CODEPAGE]) + addr - MICROCODE_BEGIN;
383 static uint64_t pfpu_read(void *opaque, hwaddr addr,
386 MilkymistPFPUState *s = opaque;
405 case GPR_BEGIN ... GPR_END:
406 r = s->gp_regs[addr - GPR_BEGIN];
408 case MICROCODE_BEGIN ... MICROCODE_END:
409 r = s->microcode[get_microcode_address(s, addr)];
413 error_report("milkymist_pfpu: read access to unknown register 0x"
414 TARGET_FMT_plx, addr << 2);
418 trace_milkymist_pfpu_memory_read(addr << 2, r);
423 static void pfpu_write(void *opaque, hwaddr addr, uint64_t value,
426 MilkymistPFPUState *s = opaque;
428 trace_milkymist_pfpu_memory_write(addr, value);
433 if (value & CTL_START_BUSY) {
448 s->regs[addr] = value;
450 case GPR_BEGIN ... GPR_END:
451 s->gp_regs[addr - GPR_BEGIN] = value;
453 case MICROCODE_BEGIN ... MICROCODE_END:
454 s->microcode[get_microcode_address(s, addr)] = value;
458 error_report("milkymist_pfpu: write access to unknown register 0x"
459 TARGET_FMT_plx, addr << 2);
464 static const MemoryRegionOps pfpu_mmio_ops = {
468 .min_access_size = 4,
469 .max_access_size = 4,
471 .endianness = DEVICE_NATIVE_ENDIAN,
474 static void milkymist_pfpu_reset(DeviceState *d)
476 MilkymistPFPUState *s = container_of(d, MilkymistPFPUState, busdev.qdev);
479 for (i = 0; i < R_MAX; i++) {
482 for (i = 0; i < 128; i++) {
485 for (i = 0; i < MICROCODE_WORDS; i++) {
488 s->output_queue_pos = 0;
489 for (i = 0; i < MAX_LATENCY; i++) {
490 s->output_queue[i] = 0;
494 static int milkymist_pfpu_init(SysBusDevice *dev)
496 MilkymistPFPUState *s = FROM_SYSBUS(typeof(*s), dev);
498 sysbus_init_irq(dev, &s->irq);
500 memory_region_init_io(&s->regs_region, OBJECT(dev), &pfpu_mmio_ops, s,
501 "milkymist-pfpu", MICROCODE_END * 4);
502 sysbus_init_mmio(dev, &s->regs_region);
507 static const VMStateDescription vmstate_milkymist_pfpu = {
508 .name = "milkymist-pfpu",
510 .minimum_version_id = 1,
511 .minimum_version_id_old = 1,
512 .fields = (VMStateField[]) {
513 VMSTATE_UINT32_ARRAY(regs, MilkymistPFPUState, R_MAX),
514 VMSTATE_UINT32_ARRAY(gp_regs, MilkymistPFPUState, 128),
515 VMSTATE_UINT32_ARRAY(microcode, MilkymistPFPUState, MICROCODE_WORDS),
516 VMSTATE_INT32(output_queue_pos, MilkymistPFPUState),
517 VMSTATE_UINT32_ARRAY(output_queue, MilkymistPFPUState, MAX_LATENCY),
518 VMSTATE_END_OF_LIST()
522 static void milkymist_pfpu_class_init(ObjectClass *klass, void *data)
524 DeviceClass *dc = DEVICE_CLASS(klass);
525 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
527 k->init = milkymist_pfpu_init;
528 dc->reset = milkymist_pfpu_reset;
529 dc->vmsd = &vmstate_milkymist_pfpu;
532 static const TypeInfo milkymist_pfpu_info = {
533 .name = "milkymist-pfpu",
534 .parent = TYPE_SYS_BUS_DEVICE,
535 .instance_size = sizeof(MilkymistPFPUState),
536 .class_init = milkymist_pfpu_class_init,
539 static void milkymist_pfpu_register_types(void)
541 type_register_static(&milkymist_pfpu_info);
544 type_init(milkymist_pfpu_register_types)