2 * QEMU model of the Milkymist programmable FPU.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
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"
30 #include "qemu/error-report.h"
33 /* #define TRACE_EXEC */
58 CTL_START_BUSY
= (1<<0),
105 #define GPR_BEGIN 0x100
106 #define GPR_END 0x17f
107 #define MICROCODE_BEGIN 0x200
108 #define MICROCODE_END 0x3ff
109 #define MICROCODE_WORDS 2048
111 #define REINTERPRET_CAST(type, val) (*((type *)&(val)))
114 static const char *opcode_to_str
[] = {
115 "NOP", "FADD", "FSUB", "FMUL", "FABS", "F2I", "I2F", "VECTOUT",
116 "SIN", "COS", "ABOVE", "EQUAL", "COPY", "IF", "TSIGN", "QUAKE",
120 #define TYPE_MILKYMIST_PFPU "milkymist-pfpu"
121 #define MILKYMIST_PFPU(obj) \
122 OBJECT_CHECK(MilkymistPFPUState, (obj), TYPE_MILKYMIST_PFPU)
124 struct MilkymistPFPUState
{
125 SysBusDevice parent_obj
;
127 MemoryRegion regs_region
;
131 uint32_t regs
[R_MAX
];
132 uint32_t gp_regs
[128];
133 uint32_t microcode
[MICROCODE_WORDS
];
135 int output_queue_pos
;
136 uint32_t output_queue
[MAX_LATENCY
];
138 typedef struct MilkymistPFPUState MilkymistPFPUState
;
140 static inline uint32_t
141 get_dma_address(uint32_t base
, uint32_t x
, uint32_t y
)
143 return base
+ 8 * (128 * y
+ x
);
147 output_queue_insert(MilkymistPFPUState
*s
, uint32_t val
, int pos
)
149 s
->output_queue
[(s
->output_queue_pos
+ pos
) % MAX_LATENCY
] = val
;
152 static inline uint32_t
153 output_queue_remove(MilkymistPFPUState
*s
)
155 return s
->output_queue
[s
->output_queue_pos
];
159 output_queue_advance(MilkymistPFPUState
*s
)
161 s
->output_queue
[s
->output_queue_pos
] = 0;
162 s
->output_queue_pos
= (s
->output_queue_pos
+ 1) % MAX_LATENCY
;
165 static int pfpu_decode_insn(MilkymistPFPUState
*s
)
167 uint32_t pc
= s
->regs
[R_PC
];
168 uint32_t insn
= s
->microcode
[pc
];
169 uint32_t reg_a
= (insn
>> 18) & 0x7f;
170 uint32_t reg_b
= (insn
>> 11) & 0x7f;
171 uint32_t op
= (insn
>> 7) & 0xf;
172 uint32_t reg_d
= insn
& 0x7f;
181 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
182 float b
= REINTERPRET_CAST(float, s
->gp_regs
[reg_b
]);
184 r
= REINTERPRET_CAST(uint32_t, t
);
185 latency
= LATENCY_FADD
;
186 D_EXEC(qemu_log("ADD a=%f b=%f t=%f, r=%08x\n", a
, b
, t
, r
));
190 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
191 float b
= REINTERPRET_CAST(float, s
->gp_regs
[reg_b
]);
193 r
= REINTERPRET_CAST(uint32_t, t
);
194 latency
= LATENCY_FSUB
;
195 D_EXEC(qemu_log("SUB a=%f b=%f t=%f, r=%08x\n", a
, b
, t
, r
));
199 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
200 float b
= REINTERPRET_CAST(float, s
->gp_regs
[reg_b
]);
202 r
= REINTERPRET_CAST(uint32_t, t
);
203 latency
= LATENCY_FMUL
;
204 D_EXEC(qemu_log("MUL a=%f b=%f t=%f, r=%08x\n", a
, b
, t
, r
));
208 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
210 r
= REINTERPRET_CAST(uint32_t, t
);
211 latency
= LATENCY_FABS
;
212 D_EXEC(qemu_log("ABS a=%f t=%f, r=%08x\n", a
, t
, r
));
216 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
218 r
= REINTERPRET_CAST(uint32_t, t
);
219 latency
= LATENCY_F2I
;
220 D_EXEC(qemu_log("F2I a=%f t=%d, r=%08x\n", a
, t
, r
));
224 int32_t a
= REINTERPRET_CAST(int32_t, s
->gp_regs
[reg_a
]);
226 r
= REINTERPRET_CAST(uint32_t, t
);
227 latency
= LATENCY_I2F
;
228 D_EXEC(qemu_log("I2F a=%08x t=%f, r=%08x\n", a
, t
, r
));
232 uint32_t a
= cpu_to_be32(s
->gp_regs
[reg_a
]);
233 uint32_t b
= cpu_to_be32(s
->gp_regs
[reg_b
]);
235 get_dma_address(s
->regs
[R_MESHBASE
],
236 s
->gp_regs
[GPR_X
], s
->gp_regs
[GPR_Y
]);
237 cpu_physical_memory_write(dma_ptr
, &a
, 4);
238 cpu_physical_memory_write(dma_ptr
+ 4, &b
, 4);
239 s
->regs
[R_LASTDMA
] = dma_ptr
+ 4;
240 D_EXEC(qemu_log("VECTOUT a=%08x b=%08x dma=%08x\n", a
, b
, dma_ptr
));
241 trace_milkymist_pfpu_vectout(a
, b
, dma_ptr
);
245 int32_t a
= REINTERPRET_CAST(int32_t, s
->gp_regs
[reg_a
]);
246 float t
= sinf(a
* (1.0f
/ (M_PI
* 4096.0f
)));
247 r
= REINTERPRET_CAST(uint32_t, t
);
248 latency
= LATENCY_SIN
;
249 D_EXEC(qemu_log("SIN a=%d t=%f, r=%08x\n", a
, t
, r
));
253 int32_t a
= REINTERPRET_CAST(int32_t, s
->gp_regs
[reg_a
]);
254 float t
= cosf(a
* (1.0f
/ (M_PI
* 4096.0f
)));
255 r
= REINTERPRET_CAST(uint32_t, t
);
256 latency
= LATENCY_COS
;
257 D_EXEC(qemu_log("COS a=%d t=%f, r=%08x\n", a
, t
, r
));
261 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
262 float b
= REINTERPRET_CAST(float, s
->gp_regs
[reg_b
]);
263 float t
= (a
> b
) ? 1.0f
: 0.0f
;
264 r
= REINTERPRET_CAST(uint32_t, t
);
265 latency
= LATENCY_ABOVE
;
266 D_EXEC(qemu_log("ABOVE a=%f b=%f t=%f, r=%08x\n", a
, b
, t
, r
));
270 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
271 float b
= REINTERPRET_CAST(float, s
->gp_regs
[reg_b
]);
272 float t
= (a
== b
) ? 1.0f
: 0.0f
;
273 r
= REINTERPRET_CAST(uint32_t, t
);
274 latency
= LATENCY_EQUAL
;
275 D_EXEC(qemu_log("EQUAL a=%f b=%f t=%f, r=%08x\n", a
, b
, t
, r
));
279 r
= s
->gp_regs
[reg_a
];
280 latency
= LATENCY_COPY
;
281 D_EXEC(qemu_log("COPY"));
285 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
286 float b
= REINTERPRET_CAST(float, s
->gp_regs
[reg_b
]);
287 uint32_t f
= s
->gp_regs
[GPR_FLAGS
];
288 float t
= (f
!= 0) ? a
: b
;
289 r
= REINTERPRET_CAST(uint32_t, t
);
290 latency
= LATENCY_IF
;
291 D_EXEC(qemu_log("IF f=%u a=%f b=%f t=%f, r=%08x\n", f
, a
, b
, t
, r
));
295 float a
= REINTERPRET_CAST(float, s
->gp_regs
[reg_a
]);
296 float b
= REINTERPRET_CAST(float, s
->gp_regs
[reg_b
]);
297 float t
= (b
< 0) ? -a
: a
;
298 r
= REINTERPRET_CAST(uint32_t, t
);
299 latency
= LATENCY_TSIGN
;
300 D_EXEC(qemu_log("TSIGN a=%f b=%f t=%f, r=%08x\n", a
, b
, t
, r
));
304 uint32_t a
= s
->gp_regs
[reg_a
];
305 r
= 0x5f3759df - (a
>> 1);
306 latency
= LATENCY_QUAKE
;
307 D_EXEC(qemu_log("QUAKE a=%d r=%08x\n", a
, r
));
311 error_report("milkymist_pfpu: unknown opcode %d", op
);
316 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d>\n",
317 s
->regs
[R_PC
], opcode_to_str
[op
], reg_a
, reg_b
, latency
,
318 s
->regs
[R_PC
] + latency
));
320 D_EXEC(qemu_log("%04d %8s R%03d, R%03d <L=%d, E=%04d> -> R%03d\n",
321 s
->regs
[R_PC
], opcode_to_str
[op
], reg_a
, reg_b
, latency
,
322 s
->regs
[R_PC
] + latency
, reg_d
));
325 if (op
== OP_VECTOUT
) {
329 /* store output for this cycle */
331 uint32_t val
= output_queue_remove(s
);
332 D_EXEC(qemu_log("R%03d <- 0x%08x\n", reg_d
, val
));
333 s
->gp_regs
[reg_d
] = val
;
336 output_queue_advance(s
);
338 /* store op output */
340 output_queue_insert(s
, r
, latency
-1);
349 static void pfpu_start(MilkymistPFPUState
*s
)
354 for (y
= 0; y
<= s
->regs
[R_VMESHLAST
]; y
++) {
355 for (x
= 0; x
<= s
->regs
[R_HMESHLAST
]; x
++) {
356 D_EXEC(qemu_log("\nprocessing x=%d y=%d\n", x
, y
));
358 /* set current position */
359 s
->gp_regs
[GPR_X
] = x
;
360 s
->gp_regs
[GPR_Y
] = y
;
362 /* run microcode on this position */
364 while (pfpu_decode_insn(s
)) {
365 /* decode at most MICROCODE_WORDS instructions */
366 if (++i
>= MICROCODE_WORDS
) {
367 error_report("milkymist_pfpu: too many instructions "
368 "executed in microcode. No VECTOUT?");
373 /* reset pc for next run */
378 s
->regs
[R_VERTICES
] = x
* y
;
380 trace_milkymist_pfpu_pulse_irq();
381 qemu_irq_pulse(s
->irq
);
384 static inline int get_microcode_address(MilkymistPFPUState
*s
, uint32_t addr
)
386 return (512 * s
->regs
[R_CODEPAGE
]) + addr
- MICROCODE_BEGIN
;
389 static uint64_t pfpu_read(void *opaque
, hwaddr addr
,
392 MilkymistPFPUState
*s
= opaque
;
411 case GPR_BEGIN
... GPR_END
:
412 r
= s
->gp_regs
[addr
- GPR_BEGIN
];
414 case MICROCODE_BEGIN
... MICROCODE_END
:
415 r
= s
->microcode
[get_microcode_address(s
, addr
)];
419 error_report("milkymist_pfpu: read access to unknown register 0x"
420 TARGET_FMT_plx
, addr
<< 2);
424 trace_milkymist_pfpu_memory_read(addr
<< 2, r
);
429 static void pfpu_write(void *opaque
, hwaddr addr
, uint64_t value
,
432 MilkymistPFPUState
*s
= opaque
;
434 trace_milkymist_pfpu_memory_write(addr
, value
);
439 if (value
& CTL_START_BUSY
) {
454 s
->regs
[addr
] = value
;
456 case GPR_BEGIN
... GPR_END
:
457 s
->gp_regs
[addr
- GPR_BEGIN
] = value
;
459 case MICROCODE_BEGIN
... MICROCODE_END
:
460 s
->microcode
[get_microcode_address(s
, addr
)] = value
;
464 error_report("milkymist_pfpu: write access to unknown register 0x"
465 TARGET_FMT_plx
, addr
<< 2);
470 static const MemoryRegionOps pfpu_mmio_ops
= {
474 .min_access_size
= 4,
475 .max_access_size
= 4,
477 .endianness
= DEVICE_NATIVE_ENDIAN
,
480 static void milkymist_pfpu_reset(DeviceState
*d
)
482 MilkymistPFPUState
*s
= MILKYMIST_PFPU(d
);
485 for (i
= 0; i
< R_MAX
; i
++) {
488 for (i
= 0; i
< 128; i
++) {
491 for (i
= 0; i
< MICROCODE_WORDS
; i
++) {
494 s
->output_queue_pos
= 0;
495 for (i
= 0; i
< MAX_LATENCY
; i
++) {
496 s
->output_queue
[i
] = 0;
500 static int milkymist_pfpu_init(SysBusDevice
*dev
)
502 MilkymistPFPUState
*s
= MILKYMIST_PFPU(dev
);
504 sysbus_init_irq(dev
, &s
->irq
);
506 memory_region_init_io(&s
->regs_region
, OBJECT(dev
), &pfpu_mmio_ops
, s
,
507 "milkymist-pfpu", MICROCODE_END
* 4);
508 sysbus_init_mmio(dev
, &s
->regs_region
);
513 static const VMStateDescription vmstate_milkymist_pfpu
= {
514 .name
= "milkymist-pfpu",
516 .minimum_version_id
= 1,
517 .fields
= (VMStateField
[]) {
518 VMSTATE_UINT32_ARRAY(regs
, MilkymistPFPUState
, R_MAX
),
519 VMSTATE_UINT32_ARRAY(gp_regs
, MilkymistPFPUState
, 128),
520 VMSTATE_UINT32_ARRAY(microcode
, MilkymistPFPUState
, MICROCODE_WORDS
),
521 VMSTATE_INT32(output_queue_pos
, MilkymistPFPUState
),
522 VMSTATE_UINT32_ARRAY(output_queue
, MilkymistPFPUState
, MAX_LATENCY
),
523 VMSTATE_END_OF_LIST()
527 static void milkymist_pfpu_class_init(ObjectClass
*klass
, void *data
)
529 DeviceClass
*dc
= DEVICE_CLASS(klass
);
530 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
532 k
->init
= milkymist_pfpu_init
;
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
)