yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
mock_memory.h
Go to the documentation of this file.
1#ifndef YAZE_TEST_MOCK_MOCK_MEMORY_H
2#define YAZE_TEST_MOCK_MOCK_MEMORY_H
3
4#include <gmock/gmock.h>
5#include <gtest/gtest.h>
6
7#include "app/emu/cpu/clock.h"
8#include "app/emu/cpu/cpu.h"
10
11namespace yaze {
12namespace app {
13namespace emu {
14namespace memory {
15
19class MockClock : public Clock {
20 public:
21 MOCK_METHOD(void, UpdateClock, (double delta), (override));
22 MOCK_METHOD(unsigned long long, GetCycleCount, (), (const, override));
23 MOCK_METHOD(void, ResetAccumulatedTime, (), (override));
24 MOCK_METHOD(void, SetFrequency, (float new_frequency), (override));
25 MOCK_METHOD(float, GetFrequency, (), (const, override));
26};
27
45class MockMemory : public Memory {
46 public:
47 MOCK_CONST_METHOD1(ReadByte, uint8_t(uint32_t address));
48 MOCK_CONST_METHOD1(ReadWord, uint16_t(uint32_t address));
49 MOCK_CONST_METHOD1(ReadWordLong, uint32_t(uint32_t address));
50 MOCK_METHOD(std::vector<uint8_t>, ReadByteVector,
51 (uint32_t address, uint16_t length), (const, override));
52
53 MOCK_METHOD2(WriteByte, void(uint32_t address, uint8_t value));
54 MOCK_METHOD2(WriteWord, void(uint32_t address, uint16_t value));
55 MOCK_METHOD2(WriteLong, void(uint32_t address, uint32_t value));
56
57 MOCK_METHOD1(PushByte, void(uint8_t value));
58 MOCK_METHOD0(PopByte, uint8_t());
59 MOCK_METHOD1(PushWord, void(uint16_t value));
60 MOCK_METHOD0(PopWord, uint16_t());
61 MOCK_METHOD1(PushLong, void(uint32_t value));
62 MOCK_METHOD0(PopLong, uint32_t());
63
64 MOCK_CONST_METHOD0(SP, uint16_t());
65 MOCK_METHOD1(SetSP, void(uint16_t value));
66
67 MOCK_METHOD1(SetMemory, void(const std::vector<uint8_t>& data));
68 MOCK_METHOD1(LoadData, void(const std::vector<uint8_t>& data));
69
71
72 MOCK_CONST_METHOD1(at, uint8_t(int i));
73 uint8_t operator[](int i) const override { return memory_[i]; }
74
81 MOCK_METHOD1(set_pal_timing, void(bool value));
85 MOCK_METHOD1(set_h_pos, void(uint16_t value));
86 MOCK_METHOD1(set_v_pos, void(uint16_t value));
87 MOCK_METHOD1(set_open_bus, void(uint8_t value));
89
90 void SetMemoryContents(const std::vector<uint8_t>& data) {
91 if (data.size() > memory_.size()) {
92 memory_.resize(data.size());
93 }
94 std::copy(data.begin(), data.end(), memory_.begin());
95 }
96
97 void SetMemoryContents(const std::vector<uint16_t>& data) {
98 if (data.size() > memory_.size()) {
99 memory_.resize(data.size());
100 }
101 int i = 0;
102 for (const auto& each : data) {
103 memory_[i] = each & 0xFF;
104 memory_[i + 1] = (each >> 8) & 0xFF;
105 i += 2;
106 }
107 }
108
109 void InsertMemory(const uint64_t address, const std::vector<uint8_t>& data) {
110 if (address > memory_.size()) {
111 memory_.resize(address + data.size());
112 }
113
114 int i = 0;
115 for (const auto& each : data) {
116 memory_[address + i] = each;
117 i++;
118 }
119 }
120
121 // 16MB = 0x1000000
122 // 02MB = 0x200000
123 void Initialize(const std::vector<uint8_t>& romData) {
124 // 16 MB, simplifying the memory layout for testing
125 memory_.resize(0x1000000);
126
127 // Clear memory
128 std::fill(memory_.begin(), memory_.end(), 0);
129
130 // Load ROM data into mock memory
131 size_t romSize = romData.size();
132 size_t romAddress = 0;
133 const size_t ROM_CHUNK_SIZE = 0x8000; // 32 KB
134 for (size_t bank = 0x00; bank <= 0xBF; bank += 0x80) {
135 for (size_t offset = 0x8000; offset <= 0xFFFF; offset += ROM_CHUNK_SIZE) {
136 if (romAddress < romSize) {
137 std::copy(romData.begin() + romAddress,
138 romData.begin() + romAddress + ROM_CHUNK_SIZE,
139 memory_.begin() + (bank << 16) + offset);
140 romAddress += ROM_CHUNK_SIZE;
141 }
142 }
143 }
144 }
145
146 void Init() {
147 ON_CALL(*this, ReadByte(::testing::_))
148 .WillByDefault(
149 [this](uint32_t address) { return memory_.at(address); });
150 ON_CALL(*this, ReadWord(::testing::_))
151 .WillByDefault([this](uint32_t address) {
152 return static_cast<uint16_t>(memory_.at(address)) |
153 (static_cast<uint16_t>(memory_.at(address + 1)) << 8);
154 });
155 ON_CALL(*this, ReadWordLong(::testing::_))
156 .WillByDefault([this](uint32_t address) {
157 return static_cast<uint32_t>(memory_.at(address)) |
158 (static_cast<uint32_t>(memory_.at(address + 1)) << 8) |
159 (static_cast<uint32_t>(memory_.at(address + 2)) << 16);
160 });
161 ON_CALL(*this, ReadByteVector(::testing::_, ::testing::_))
162 .WillByDefault([this](uint32_t address, uint16_t length) {
163 std::vector<uint8_t> data;
164 for (int i = 0; i < length; i++) {
165 data.push_back(memory_.at(address + i));
166 }
167 return data;
168 });
169 ON_CALL(*this, WriteByte(::testing::_, ::testing::_))
170 .WillByDefault([this](uint32_t address, uint8_t value) {
171 memory_[address] = value;
172 });
173 ON_CALL(*this, WriteWord(::testing::_, ::testing::_))
174 .WillByDefault([this](uint32_t address, uint16_t value) {
175 memory_[address] = value & 0xFF;
176 memory_[address + 1] = (value >> 8) & 0xFF;
177 });
178 ON_CALL(*this, PushByte(::testing::_)).WillByDefault([this](uint8_t value) {
179 memory_.at(SP_--) = value;
180 });
181 ON_CALL(*this, PopByte()).WillByDefault([this]() {
182 uint8_t value = memory_.at(SP_);
183 this->SetSP(SP_ + 1);
184 return value;
185 });
186 ON_CALL(*this, PushWord(::testing::_))
187 .WillByDefault([this](uint16_t value) {
188 memory_.at(SP_) = value & 0xFF;
189 memory_.at(SP_ + 1) = (value >> 8) & 0xFF;
190 this->SetSP(SP_ - 2);
191 });
192 ON_CALL(*this, PopWord()).WillByDefault([this]() {
193 uint16_t value = static_cast<uint16_t>(memory_.at(SP_)) |
194 (static_cast<uint16_t>(memory_.at(SP_ + 1)) << 8);
195 this->SetSP(SP_ + 2);
196 return value;
197 });
198 ON_CALL(*this, PushLong(::testing::_))
199 .WillByDefault([this](uint32_t value) {
200 memory_.at(SP_) = value & 0xFF;
201 memory_.at(SP_ + 1) = (value >> 8) & 0xFF;
202 memory_.at(SP_ + 2) = (value >> 16) & 0xFF;
203 });
204 ON_CALL(*this, PopLong()).WillByDefault([this]() {
205 uint32_t value = static_cast<uint32_t>(memory_.at(SP_)) |
206 (static_cast<uint32_t>(memory_.at(SP_ + 1)) << 8) |
207 (static_cast<uint32_t>(memory_.at(SP_ + 2)) << 16);
208 this->SetSP(SP_ + 3);
209 return value;
210 });
211 ON_CALL(*this, SP()).WillByDefault([this]() { return SP_; });
212 ON_CALL(*this, SetSP(::testing::_)).WillByDefault([this](uint16_t value) {
213 SP_ = value;
214 });
215 ON_CALL(*this, ClearMemory()).WillByDefault([this]() {
216 memory_.resize(64000, 0x00);
217 });
218 }
219
220 std::vector<uint8_t> memory_;
221 uint16_t SP_ = 0x01FF;
222};
223
224} // namespace memory
225} // namespace emu
226} // namespace app
227} // namespace yaze
228
229#endif // YAZE_TEST_MOCK_MOCK_MEMORY_H
virtual float GetFrequency() const =0
virtual void SetFrequency(float new_frequency)=0
virtual void UpdateClock(double delta)=0
virtual void ResetAccumulatedTime()=0
virtual unsigned long long GetCycleCount() const =0
Memory interface.
Definition memory.h:48
virtual std::vector< uint8_t > ReadByteVector(uint32_t address, uint16_t length) const =0
virtual void PushLong(uint32_t value)=0
virtual void WriteLong(uint32_t address, uint32_t value)=0
virtual void set_pal_timing(bool value)=0
virtual void PushByte(uint8_t value)=0
virtual uint16_t SP() const =0
virtual void set_v_pos(uint16_t value)=0
virtual void SetSP(uint16_t value)=0
virtual void PushWord(uint16_t value)=0
virtual auto v_pos() const -> uint16_t=0
virtual void WriteByte(uint32_t address, uint8_t value)=0
virtual void set_hdma_run_requested(bool value)=0
virtual uint8_t at(int i) const =0
virtual uint8_t open_bus() const =0
virtual void WriteWord(uint32_t address, uint16_t value)=0
virtual void set_h_pos(uint16_t value)=0
virtual void run_hdma_request()=0
virtual bool hdma_run_requested() const =0
virtual uint8_t ReadByte(uint32_t address) const =0
virtual auto h_pos() const -> uint16_t=0
virtual void set_hdma_init_requested(bool value)=0
virtual uint32_t PopLong()=0
virtual uint16_t ReadWord(uint32_t address) const =0
virtual uint32_t ReadWordLong(uint32_t address) const =0
virtual void init_hdma_request()=0
virtual uint8_t PopByte()=0
virtual uint16_t PopWord()=0
virtual auto pal_timing() const -> bool=0
virtual void set_open_bus(uint8_t value)=0
virtual bool hdma_init_requested() const =0
Mock CPU class for testing.
Definition mock_memory.h:19
MOCK_METHOD(void, UpdateClock,(double delta),(override))
MOCK_METHOD(void, SetFrequency,(float new_frequency),(override))
MOCK_METHOD(void, ResetAccumulatedTime,(),(override))
MOCK_METHOD(float, GetFrequency,(),(const, override))
MOCK_METHOD(unsigned long long, GetCycleCount,(),(const, override))
A mock implementation of the Memory class.
Definition mock_memory.h:45
std::vector< uint8_t > memory_
MOCK_METHOD0(PopByte, uint8_t())
MOCK_METHOD0(PopLong, uint32_t())
MOCK_METHOD1(set_pal_timing, void(bool value))
void SetMemoryContents(const std::vector< uint8_t > &data)
Definition mock_memory.h:90
MOCK_METHOD1(set_open_bus, void(uint8_t value))
MOCK_METHOD1(set_v_pos, void(uint16_t value))
MOCK_METHOD2(WriteWord, void(uint32_t address, uint16_t value))
MOCK_CONST_METHOD0(open_bus, uint8_t())
MOCK_CONST_METHOD1(at, uint8_t(int i))
MOCK_METHOD1(PushLong, void(uint32_t value))
MOCK_METHOD2(WriteLong, void(uint32_t address, uint32_t value))
MOCK_METHOD1(LoadData, void(const std::vector< uint8_t > &data))
MOCK_METHOD1(set_hdma_init_requested, void(bool value))
MOCK_METHOD1(PushWord, void(uint16_t value))
void SetMemoryContents(const std::vector< uint16_t > &data)
Definition mock_memory.h:97
MOCK_CONST_METHOD0(h_pos, uint16_t())
MOCK_CONST_METHOD0(v_pos, uint16_t())
MOCK_METHOD1(SetMemory, void(const std::vector< uint8_t > &data))
MOCK_CONST_METHOD0(hdma_init_requested, bool())
MOCK_METHOD0(init_hdma_request, void())
void Initialize(const std::vector< uint8_t > &romData)
MOCK_METHOD2(WriteByte, void(uint32_t address, uint8_t value))
MOCK_METHOD(std::vector< uint8_t >, ReadByteVector,(uint32_t address, uint16_t length),(const, override))
MOCK_CONST_METHOD1(ReadWord, uint16_t(uint32_t address))
MOCK_CONST_METHOD1(ReadWordLong, uint32_t(uint32_t address))
uint8_t operator[](int i) const override
Definition mock_memory.h:73
MOCK_METHOD0(PopWord, uint16_t())
MOCK_METHOD1(SetSP, void(uint16_t value))
MOCK_METHOD1(set_h_pos, void(uint16_t value))
MOCK_CONST_METHOD0(pal_timing, bool())
MOCK_METHOD1(PushByte, void(uint8_t value))
void InsertMemory(const uint64_t address, const std::vector< uint8_t > &data)
MOCK_METHOD0(ClearMemory, void())
MOCK_CONST_METHOD1(ReadByte, uint8_t(uint32_t address))
MOCK_CONST_METHOD0(hdma_run_requested, bool())
MOCK_METHOD1(set_hdma_run_requested, void(bool value))
MOCK_METHOD0(run_hdma_request, void())
Definition common.cc:22