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 #define TYPE_MILKYMIST_PFPU "milkymist-pfpu"
120 #define MILKYMIST_PFPU(obj) \
121 OBJECT_CHECK(MilkymistPFPUState, (obj), TYPE_MILKYMIST_PFPU)
123 struct MilkymistPFPUState {
124 SysBusDevice parent_obj;
126 MemoryRegion regs_region;
127 CharDriverState *chr;
130 uint32_t regs[R_MAX];
131 uint32_t gp_regs[128];
132 uint32_t microcode[MICROCODE_WORDS];
134 int output_queue_pos;
135 uint32_t output_queue[MAX_LATENCY];
137 typedef struct MilkymistPFPUState MilkymistPFPUState;
140 get_dma_address(uint32_t base, uint32_t x, uint32_t y)
142 return base + 8 * (128 * y + x);
146 output_queue_insert(MilkymistPFPUState *s, uint32_t val, int pos)
148 s->output_queue[(s->output_queue_pos + pos) % MAX_LATENCY] = val;
151 static inline uint32_t
152 output_queue_remove(MilkymistPFPUState *s)
154 return s->output_queue[s->output_queue_pos];
158 output_queue_advance(MilkymistPFPUState *s)
160 s->output_queue[s->output_queue_pos] = 0;
161 s->output_queue_pos = (s->output_queue_pos + 1) % MAX_LATENCY;
164 static int pfpu_decode_insn(MilkymistPFPUState *s)
166 uint32_t pc = s->regs[R_PC];
167 uint32_t insn = s->microcode[pc];
168 uint32_t reg_a = (insn >> 18) & 0x7f;
169 uint32_t reg_b = (insn >> 11) & 0x7f;
170 uint32_t op = (insn >> 7) & 0xf;
171 uint32_t reg_d = insn & 0x7f;
180 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
181 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
183 r = REINTERPRET_CAST(uint32_t, t);
184 latency = LATENCY_FADD;
185 D_EXEC(qemu_log("ADD a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
189 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
190 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
192 r = REINTERPRET_CAST(uint32_t, t);
193 latency = LATENCY_FSUB;
194 D_EXEC(qemu_log("SUB a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
198 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
199 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
201 r = REINTERPRET_CAST(uint32_t, t);
202 latency = LATENCY_FMUL;
203 D_EXEC(qemu_log("MUL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
207 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
209 r = REINTERPRET_CAST(uint32_t, t);
210 latency = LATENCY_FABS;
211 D_EXEC(qemu_log("ABS a=%f t=%f, r=%08x\n", a, t, r));
215 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
217 r = REINTERPRET_CAST(uint32_t, t);
218 latency = LATENCY_F2I;
219 D_EXEC(qemu_log("F2I a=%f t=%d, r=%08x\n", a, t, r));
223 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
225 r = REINTERPRET_CAST(uint32_t, t);
226 latency = LATENCY_I2F;
227 D_EXEC(qemu_log("I2F a=%08x t=%f, r=%08x\n", a, t, r));
231 uint32_t a = cpu_to_be32(s->gp_regs[reg_a]);
232 uint32_t b = cpu_to_be32(s->gp_regs[reg_b]);
234 get_dma_address(s->regs[R_MESHBASE],
235 s->gp_regs[GPR_X], s->gp_regs[GPR_Y]);
236 cpu_physical_memory_write(dma_ptr, &a, 4);
237 cpu_physical_memory_write(dma_ptr + 4, &b, 4);
238 s->regs[R_LASTDMA] = dma_ptr + 4;
239 D_EXEC(qemu_log("VECTOUT a=%08x b=%08x dma=%08x\n", a, b, dma_ptr));
240 trace_milkymist_pfpu_vectout(a, b, dma_ptr);
244 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
245 float t = sinf(a * (1.0f / (M_PI * 4096.0f)));
246 r = REINTERPRET_CAST(uint32_t, t);
247 latency = LATENCY_SIN;
248 D_EXEC(qemu_log("SIN a=%d t=%f, r=%08x\n", a, t, r));
252 int32_t a = REINTERPRET_CAST(int32_t, s->gp_regs[reg_a]);
253 float t = cosf(a * (1.0f / (M_PI * 4096.0f)));
254 r = REINTERPRET_CAST(uint32_t, t);
255 latency = LATENCY_COS;
256 D_EXEC(qemu_log("COS a=%d t=%f, r=%08x\n", a, t, r));
260 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
261 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
262 float t = (a > b) ? 1.0f : 0.0f;
263 r = REINTERPRET_CAST(uint32_t, t);
264 latency = LATENCY_ABOVE;
265 D_EXEC(qemu_log("ABOVE a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
269 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
270 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
271 float t = (a == b) ? 1.0f : 0.0f;
272 r = REINTERPRET_CAST(uint32_t, t);
273 latency = LATENCY_EQUAL;
274 D_EXEC(qemu_log("EQUAL a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
278 r = s->gp_regs[reg_a];
279 latency = LATENCY_COPY;
280 D_EXEC(qemu_log("COPY"));
284 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
285 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
286 uint32_t f = s->gp_regs[GPR_FLAGS];
287 float t = (f != 0) ? a : b;
288 r = REINTERPRET_CAST(uint32_t, t);
289 latency = LATENCY_IF;
290 D_EXEC(qemu_log("IF f=%u a=%f b=%f t=%f, r=%08x\n", f, a, b, t, r));
294 float a = REINTERPRET_CAST(float, s->gp_regs[reg_a]);
295 float b = REINTERPRET_CAST(float, s->gp_regs[reg_b]);
296 float t = (b < 0) ? -a : a;
297 r = REINTERPRET_CAST(uint32_t, t);
298 latency = LATENCY_TSIGN;
299 D_EXEC(qemu_log("TSIGN a=%f b=%f t=%f, r=%08x\n", a, b, t, r));
303 uint32_t a = s->gp_regs[reg_a];
304 r = 0x5f3759df - (a >> 1);
305 latency = LATENCY_QUAKE;
306 D_EXEC(qemu_log("QUAKE a=%d r=%08x\n", a, r));
310 error_report("milkymist_pfpu: unknown opcode %d", op);
315 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d>\n",
316 s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
317 s->regs[R_PC] + latency));
319 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d> -> R%03d\n",
320 s->regs[R_PC], opcode_to_str[op], reg_a, reg_b, latency,
321 s->regs[R_PC] + latency, reg_d));
324 if (op == OP_VECTOUT) {
328 /* store output for this cycle */
330 uint32_t val = output_queue_remove(s);
331 D_EXEC(qemu_log("R%03d <- 0x%08x\n", reg_d, val));
332 s->gp_regs[reg_d] = val;
335 output_queue_advance(s);
337 /* store op output */
339 output_queue_insert(s, r, latency-1);
348 static void pfpu_start(MilkymistPFPUState *s)
353 for (y = 0; y <= s->regs[R_VMESHLAST]; y++) {
354 for (x = 0; x <= s->regs[R_HMESHLAST]; x++) {
355 D_EXEC(qemu_log("\nprocessing x=%d y=%d\n", x, y));
357 /* set current position */
358 s->gp_regs[GPR_X] = x;
359 s->gp_regs[GPR_Y] = y;
361 /* run microcode on this position */
363 while (pfpu_decode_insn(s)) {
364 /* decode at most MICROCODE_WORDS instructions */
365 if (++i >= MICROCODE_WORDS) {
366 error_report("milkymist_pfpu: too many instructions "
367 "executed in microcode. No VECTOUT?");
372 /* reset pc for next run */
377 s->regs[R_VERTICES] = x * y;
379 trace_milkymist_pfpu_pulse_irq();
380 qemu_irq_pulse(s->irq);
383 static inline int get_microcode_address(MilkymistPFPUState *s, uint32_t addr)
385 return (512 * s->regs[R_CODEPAGE]) + addr - MICROCODE_BEGIN;
388 static uint64_t pfpu_read(void *opaque, hwaddr addr,
391 MilkymistPFPUState *s = opaque;
410 case GPR_BEGIN ... GPR_END:
411 r = s->gp_regs[addr - GPR_BEGIN];
413 case MICROCODE_BEGIN ... MICROCODE_END:
414 r = s->microcode[get_microcode_address(s, addr)];
418 error_report("milkymist_pfpu: read access to unknown register 0x"
419 TARGET_FMT_plx, addr << 2);
423 trace_milkymist_pfpu_memory_read(addr << 2, r);
428 static void pfpu_write(void *opaque, hwaddr addr, uint64_t value,
431 MilkymistPFPUState *s = opaque;
433 trace_milkymist_pfpu_memory_write(addr, value);
438 if (value & CTL_START_BUSY) {
453 s->regs[addr] = value;
455 case GPR_BEGIN ... GPR_END:
456 s->gp_regs[addr - GPR_BEGIN] = value;
458 case MICROCODE_BEGIN ... MICROCODE_END:
459 s->microcode[get_microcode_address(s, addr)] = value;
463 error_report("milkymist_pfpu: write access to unknown register 0x"
464 TARGET_FMT_plx, addr << 2);
469 static const MemoryRegionOps pfpu_mmio_ops = {
473 .min_access_size = 4,
474 .max_access_size = 4,
476 .endianness = DEVICE_NATIVE_ENDIAN,
479 static void milkymist_pfpu_reset(DeviceState *d)
481 MilkymistPFPUState *s = MILKYMIST_PFPU(d);
484 for (i = 0; i < R_MAX; i++) {
487 for (i = 0; i < 128; i++) {
490 for (i = 0; i < MICROCODE_WORDS; i++) {
493 s->output_queue_pos = 0;
494 for (i = 0; i < MAX_LATENCY; i++) {
495 s->output_queue[i] = 0;
499 static int milkymist_pfpu_init(SysBusDevice *dev)
501 MilkymistPFPUState *s = MILKYMIST_PFPU(dev);
503 sysbus_init_irq(dev, &s->irq);
505 memory_region_init_io(&s->regs_region, OBJECT(dev), &pfpu_mmio_ops, s,
506 "milkymist-pfpu", MICROCODE_END * 4);
507 sysbus_init_mmio(dev, &s->regs_region);
512 static const VMStateDescription vmstate_milkymist_pfpu = {
513 .name = "milkymist-pfpu",
515 .minimum_version_id = 1,
516 .fields = (VMStateField[]) {
517 VMSTATE_UINT32_ARRAY(regs, MilkymistPFPUState, R_MAX),
518 VMSTATE_UINT32_ARRAY(gp_regs, MilkymistPFPUState, 128),
519 VMSTATE_UINT32_ARRAY(microcode, MilkymistPFPUState, MICROCODE_WORDS),
520 VMSTATE_INT32(output_queue_pos, MilkymistPFPUState),
521 VMSTATE_UINT32_ARRAY(output_queue, MilkymistPFPUState, MAX_LATENCY),
522 VMSTATE_END_OF_LIST()
526 static void milkymist_pfpu_class_init(ObjectClass *klass, void *data)
528 DeviceClass *dc = DEVICE_CLASS(klass);
529 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
531 k->init = milkymist_pfpu_init;
532 dc->reset = milkymist_pfpu_reset;
533 dc->vmsd = &vmstate_milkymist_pfpu;
536 static const TypeInfo milkymist_pfpu_info = {
537 .name = TYPE_MILKYMIST_PFPU,
538 .parent = TYPE_SYS_BUS_DEVICE,
539 .instance_size = sizeof(MilkymistPFPUState),
540 .class_init = milkymist_pfpu_class_init,
543 static void milkymist_pfpu_register_types(void)
545 type_register_static(&milkymist_pfpu_info);
548 type_init(milkymist_pfpu_register_types)