WolPSX
Loading...
Searching...
No Matches
ram.cpp
1#include <iostream>
2#include <sstream>
3
4#include "core/memory/ram.hpp"
5
13RAM::RAM(uint32_t size)
14{
15 data = std::vector<uint8_t>(size, 0xca);
16}
17
27uint32_t RAM::read32_cpu(uint32_t offset)
28{
29 if(offset >= data.size())
30 {
31 std::stringstream ss;
32 ss << "Size exceeded for read32_cpu (RAM): 0x" << std::hex << offset;
33 throw std::runtime_error(ss.str());
34 }
35 if(offset % 4 != 0)
36 {
37 std::stringstream ss;
38 ss << "Unaligned read32_cpu (RAM): 0x" << std::hex << offset;
39 throw std::runtime_error(ss.str());
40 }
41 return *(uint32_t*)&data[offset];
42}
43
53void RAM::write32_cpu(uint32_t offset, uint32_t data)
54{
55 if(offset >= this->data.size())
56 {
57 std::stringstream ss;
58 ss << "Size exceeded for write32_cpu (RAM): 0x" << std::hex << offset;
59 throw std::runtime_error(ss.str());
60 }
61 if(offset % 4 != 0)
62 {
63 std::stringstream ss;
64 ss << "Unaligned write32_cpu (RAM): 0x" << std::hex << offset;
65 throw std::runtime_error(ss.str());
66 }
67 *(uint32_t*)&this->data[offset] = data;
68}
69
78uint8_t RAM::read8_cpu(uint32_t offset)
79{
80 if(offset >= data.size())
81 {
82 std::stringstream ss;
83 ss << "Size exceeded for read8_cpu (RAM): 0x" << std::hex << offset;
84 throw std::runtime_error(ss.str());
85 }
86 return data[offset];
87}
88
97void RAM::write8_cpu(uint32_t offset, uint8_t data)
98{
99 if(offset >= this->data.size())
100 {
101 std::stringstream ss;
102 ss << "Size exceeded for write8_cpu (RAM): 0x" << std::hex << offset;
103 throw std::runtime_error(ss.str());
104 }
105 this->data[offset] = data;
106}
107
117uint16_t RAM::read16_cpu(uint32_t offset)
118{
119 if(offset >= data.size())
120 {
121 std::stringstream ss;
122 ss << "Size exceeded for read16_cpu (RAM): 0x" << std::hex << offset;
123 throw std::runtime_error(ss.str());
124 }
125 if(offset % 2 != 0)
126 {
127 std::stringstream ss;
128 ss << "Unaligned read16_cpu (RAM): 0x" << std::hex << offset;
129 throw std::runtime_error(ss.str());
130 }
131 return *(uint16_t*)&data[offset];
132}
133
143void RAM::write16_cpu(uint32_t offset, uint16_t data)
144{
145 if(offset >= this->data.size())
146 {
147 std::stringstream ss;
148 ss << "Size exceeded for write16_cpu (RAM): 0x" << std::hex << offset;
149 throw std::runtime_error(ss.str());
150 }
151 if(offset % 2 != 0)
152 {
153 std::stringstream ss;
154 ss << "Unaligned write16_cpu (RAM): 0x" << std::hex << offset;
155 throw std::runtime_error(ss.str());
156 }
157 *(uint16_t*)&this->data[offset] = data;
158}
void write8_cpu(uint32_t offset, uint8_t data)
Writes a byte to the RAM.
Definition ram.cpp:97
std::vector< uint8_t > data
Data of the RAM.
Definition ram.hpp:28
void write32_cpu(uint32_t offset, uint32_t data)
Writes a 32-bit word to the RAM.
Definition ram.cpp:53
RAM(uint32_t size)
Construct a new RAM:: RAM object.
Definition ram.cpp:13
void write16_cpu(uint32_t offset, uint16_t data)
Writes a 16-bit word to the RAM.
Definition ram.cpp:143
uint32_t read32_cpu(uint32_t offset)
Reads a 32-bit word from the RAM.
Definition ram.cpp:27
uint16_t read16_cpu(uint32_t offset)
Reads a 16-bit word from the RAM.
Definition ram.cpp:117
uint8_t read8_cpu(uint32_t offset)
Reads a byte from the RAM.
Definition ram.cpp:78