WolPSX
Loading...
Searching...
No Matches
cpu_utils.cpp
1#include <iostream>
2#include <sstream>
3#include <core/cpu/cpu.hpp>
4
11{
12 ir = ir_next;
13 ir_next = read32(pc);
15 pc += 4;
16}
17
26{
27 if(lookup_op.find(ins.opcode()) != lookup_op.end())
28 {
29 (this->*lookup_op[ins.opcode()])();
30 return;
31 }
32 //throw unhandled instruction error
33 std::stringstream ss;
34 ss << "Unhandled instruction: " << std::hex << ir;
35 throw std::runtime_error(ss.str());
36}
37
44void CPU::branch(uint32_t offset)
45{
46 pc -= 4; //undo pc increment to point to next instruction
47 uint32_t multiplied = offset << 2;
48 if((multiplied & 0x80000000) != (offset & 0x80000000))
49 {
50 std::cout << "MISTAKE!";
51 }
52 pc += multiplied;
53}
54
61void CPU::set_reg(uint8_t reg, uint32_t data)
62{
63 // gpreg_out[reg] = data;
64 // gpreg_out[0] = 0; // $zero register
65 load_queue.push(RegisterLoad(reg, data));
66}
67
74uint32_t CPU::get_reg(uint8_t reg)
75{
76 // return gpreg_in[reg];
77 return regs[reg];
78}
79
86{
87 uint8_t num_loads = load_queue.size();
88 for(int i = 0; i < num_loads; i++)
89 {
90 RegisterLoad load = load_queue.front();
91 load_queue.pop();
92 load.delay--;
93 if(load.delay == 0xffffffff)
94 regs[load.reg] = load.data;
95 else
96 load_queue.push(load);
97 }
98 regs[0] = 0; // $zero register
99}
100
107{
108 std::cout << "Registers:\n";
109 for(int i = 0; i < 32; i++)
110 {
111 std::cout << "R" << i << ": " << std::hex << regs[i] << "\t";
112 if(i % 4 == 3)
113 std::cout << "\n";
114 }
115 std::cout << "HI: " << std::hex << hi << "\tLO: " << std::hex << lo << "\n";
116
117}
std::map< uint8_t, void(CPU::*)()> lookup_op
Lookup table for instructions.
Definition cpu.hpp:293
uint32_t read32(uint32_t addr)
Read a 32 bit word from the bus.
Definition cpu_rw.cpp:13
uint32_t hi
HI register.
Definition cpu.hpp:219
void load_next_ins()
Load the next instruction into the instruction register.
Definition cpu_utils.cpp:10
void branch(uint32_t offset)
Branches to the given offset.
Definition cpu_utils.cpp:44
uint32_t ir
Instruction register.
Definition cpu.hpp:194
uint32_t ir_next
Instruction immediately after the current instruction in the memory.
Definition cpu.hpp:200
Instruction ins
Instruction in the form of the Instruction structure.
Definition cpu.hpp:206
void load_regs()
Loads the registers from the load queue.
Definition cpu_utils.cpp:85
uint32_t regs[32]
General purpose registers.
Definition cpu.hpp:212
uint32_t pc
Program counter.
Definition cpu.hpp:188
void show_regs()
Shows the values of the registers.
void decode_and_execute()
Decodes and executes the instruction in the instruction register.
Definition cpu_utils.cpp:25
void set_reg(uint8_t reg, uint32_t data)
Sets the value of the given register from the general purpose registers.
Definition cpu_utils.cpp:61
uint32_t get_reg(uint8_t reg)
Gets the value of the given register from the general purpose registers.
Definition cpu_utils.cpp:74
std::queue< RegisterLoad > load_queue
Queue to store the loads to the general purpose registers.
Definition cpu.hpp:182
uint32_t lo
LO register.
Definition cpu.hpp:226
Structure to access different parts of an instruction by value.
Definition cpu.hpp:16
uint32_t opcode()
Opcode of the instruction. Size: 6 bits [31-26].
Definition cpu.hpp:37
Struture to store details of loads to the general purpose registers.
Definition cpu.hpp:96
uint32_t delay
Delay in clock cycles.
Definition cpu.hpp:113
uint32_t reg
Register ID.
Definition cpu.hpp:101
uint32_t data
Data to be loaded.
Definition cpu.hpp:107