coastline 0.0.4
Loading...
Searching...
No Matches
mipscpu.h
Go to the documentation of this file.
1#pragma once
2
11
12#include <cstdint>
13#include <functional>
14#include <span>
15#include <string>
16#include <unordered_map>
17#include <vector>
18
19namespace coastline {
20
22enum MipsRegister : int {
24 MIPS_V0 = 2,
25 MIPS_V1 = 3,
26 MIPS_A0 = 4,
27 MIPS_A1 = 5,
28 MIPS_A2 = 6,
29 MIPS_A3 = 7,
30 MIPS_SP = 29,
31 MIPS_RA = 31,
32};
33
35class MipsCpu {
36public:
43 enum class HookResult {
46 };
47
50
53
55 using DeviceWrite = std::function<void(std::uint32_t address, int size, std::uint64_t value)>;
56
62 explicit MipsCpu(std::size_t ramSize);
63
71
78 void SetHook(std::uint32_t address, Hook hook);
79
88
95 void SetIdle(std::function<void(MipsCpu &)> idle);
96
103 bool Run(std::uint64_t budget);
104
106 void Stop();
107
114
120 void Jump(std::uint32_t address);
121
123 const std::string &Fault() const {
124 return m_fault;
125 }
126
134
142
150
158
165 void Write8(std::uint32_t address, std::uint8_t value);
166
173 void Write16(std::uint32_t address, std::uint16_t value);
174
181 void Write32(std::uint32_t address, std::uint32_t value);
182
189 void Write64(std::uint32_t address, std::uint64_t value);
190
197 float ReadFloat(std::uint32_t address);
198
206
229
232 return instructions / 2 + idleTicks;
233 }
234
237 return static_cast<std::uint32_t>(Ticks());
238 }
239
240private:
241 void Step();
242 void Execute(std::uint32_t instruction, std::uint32_t address);
243 void Special(std::uint32_t instruction);
244 void RegImm(std::uint32_t instruction, std::uint32_t address);
245 void Cop0(std::uint32_t instruction);
246 void Cop1(std::uint32_t instruction, std::uint32_t address);
247 void FloatOperation(std::uint32_t instruction);
248 void Branch(bool taken, std::uint32_t target, bool likely);
249 void SetFault(const std::string &fault);
250 std::uint8_t *RamAt(std::uint32_t address, int size);
251 std::uint64_t ReadDevice(std::uint32_t address, int size);
252 void WriteDevice(std::uint32_t address, int size, std::uint64_t value);
253 float GetSingle(int reg) const;
254 double GetDouble(int reg) const;
255 void SetSingle(int reg, float value);
256 void SetDouble(int reg, double value);
257 std::uint64_t GetLong(int reg) const;
258 void SetLong(int reg, std::uint64_t value);
259
263 DeviceRead m_deviceRead;
264 DeviceWrite m_deviceWrite;
265 std::function<void(MipsCpu &)> m_idle;
266 std::string m_fault;
267 bool m_stopped = false;
268 std::uint32_t m_current = 0;
269};
270
271} // namespace coastline
The VR4300 processor and the RDRAM it runs from.
Definition mipscpu.h:35
MipsCpu(std::size_t ramSize)
Make a processor with zeroed RDRAM.
std::uint32_t fcr31
The control and status register of the floating-point unit.
Definition mipscpu.h:218
void Write32(std::uint32_t address, std::uint32_t value)
Write a big-endian word of memory.
void SetHook(std::uint32_t address, Hook hook)
Set the routine called before the instruction at an address of RDRAM.
std::uint32_t Argument(int index)
Read an argument of the routine a hook is on, from a0 to a3 and then from the stack.
std::uint16_t Read16(std::uint32_t address)
Read a big-endian half-word of memory.
std::uint32_t Read32(std::uint32_t address)
Read a big-endian word of memory.
std::uint64_t lo
The low word of a product or the quotient of a division.
Definition mipscpu.h:214
void SetDevices(DeviceRead read, DeviceWrite write)
Set the routines the registers of the console are read and written through.
void SetRom(std::span< const std::uint8_t > rom)
Set the cartridge image the physical addresses from 0x10000000 read.
std::uint64_t instructions
The instructions run.
Definition mipscpu.h:226
std::vector< std::uint8_t > ram
The RDRAM.
Definition mipscpu.h:208
void Write16(std::uint32_t address, std::uint16_t value)
Write a big-endian half-word of memory.
std::uint32_t npc
The address of the instruction after it, the target of a branch taken.
Definition mipscpu.h:224
std::function< void(std::uint32_t address, int size, std::uint64_t value)> DeviceWrite
A write to the registers of the console: the physical address, the size, and the value.
Definition mipscpu.h:55
void Return(std::uint32_t value)
Return from the routine a hook is on, as its last instruction would.
const std::string & Fault() const
Definition mipscpu.h:123
void Write8(std::uint32_t address, std::uint8_t value)
Write a byte of memory.
void Write64(std::uint32_t address, std::uint64_t value)
Write a big-endian double word of memory.
std::uint64_t Ticks() const
Definition mipscpu.h:231
HookResult
What a hook did.
Definition mipscpu.h:43
@ Jumped
Definition mipscpu.h:45
@ Continue
Definition mipscpu.h:44
std::uint32_t cop0[32]
The registers of the system control processor.
Definition mipscpu.h:220
std::uint32_t Count() const
Definition mipscpu.h:236
std::uint64_t hi
The high word of a product or the remainder of a division.
Definition mipscpu.h:212
float ReadFloat(std::uint32_t address)
Read a single-precision value of memory.
void Jump(std::uint32_t address)
Set the program counter, with no branch pending.
void SetIdle(std::function< void(MipsCpu &)> idle)
Set the routine called when the processor spins on a branch to itself, as an idle thread does.
std::function< std::uint64_t(std::uint32_t address, int size)> DeviceRead
A read of the registers of the console: the physical address and the size in bytes.
Definition mipscpu.h:52
void Stop()
Stop Run() before the next instruction.
std::uint8_t Read8(std::uint32_t address)
Read a byte of memory, through the map of the console.
std::uint64_t Read64(std::uint32_t address)
Read a big-endian double word of memory.
std::function< HookResult(MipsCpu &)> Hook
A routine called before the instruction at the address it is set on.
Definition mipscpu.h:49
std::uint64_t idleTicks
The ticks of Count added to those of the instructions, as time spent idle.
Definition mipscpu.h:228
std::uint32_t pc
The address of the next instruction.
Definition mipscpu.h:222
bool Run(std::uint64_t budget)
Run instructions until Stop() or a fault.
std::uint32_t fpr[32]
The floating-point registers, as 32-bit halves.
Definition mipscpu.h:216
std::uint64_t gpr[32]
The general registers.
Definition mipscpu.h:210
Definition assetimport.h:11
MipsRegister
The numbers of the general registers the calling convention gives a role.
Definition mipscpu.h:22
@ MIPS_A3
Definition mipscpu.h:29
@ MIPS_ZERO
Definition mipscpu.h:23
@ MIPS_V1
Definition mipscpu.h:25
@ MIPS_A0
Definition mipscpu.h:26
@ MIPS_V0
Definition mipscpu.h:24
@ MIPS_A2
Definition mipscpu.h:28
@ MIPS_RA
Definition mipscpu.h:31
@ MIPS_A1
Definition mipscpu.h:27
@ MIPS_SP
Definition mipscpu.h:30