yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
spc700_test.cc
Go to the documentation of this file.
2
3#include <gmock/gmock-nice-strict.h>
4#include <gmock/gmock.h>
5#include <gtest/gtest.h>
6
7namespace yaze {
8namespace test {
9namespace emu {
10
11using testing::_;
12using testing::Return;
16
20class MockAudioRam : public AudioRam {
21 public:
22 MOCK_METHOD(void, reset, (), (override));
23 MOCK_METHOD(uint8_t, read, (uint16_t address), (const, override));
24 MOCK_METHOD(uint8_t&, mutable_read, (uint16_t address), (override));
25 MOCK_METHOD(void, write, (uint16_t address, uint8_t value), (override));
26
27 void SetupMemory(uint16_t address, const std::vector<uint8_t>& values) {
28 if (address > internal_audio_ram_.size()) {
29 internal_audio_ram_.resize(address + values.size());
30 }
31 int i = 0;
32 for (const auto& each : values) {
33 internal_audio_ram_[address + i] = each;
34 i++;
35 }
36 }
37
38 void SetUp() {
39 // internal_audio_ram_.resize(0x10000); // 64 K (0x10000)
40 // std::fill(internal_audio_ram_.begin(), internal_audio_ram_.end(), 0);
41 ON_CALL(*this, read(_)).WillByDefault([this](uint16_t address) {
42 return internal_audio_ram_[address];
43 });
44 ON_CALL(*this, mutable_read(_))
45 .WillByDefault([this](uint16_t address) -> uint8_t& {
46 return internal_audio_ram_[address];
47 });
48 ON_CALL(*this, write(_, _))
49 .WillByDefault([this](uint16_t address, uint8_t value) {
50 internal_audio_ram_[address] = value;
51 });
52 ON_CALL(*this, reset()).WillByDefault([this]() {
53 std::fill(internal_audio_ram_.begin(), internal_audio_ram_.end(), 0);
54 });
55 }
56
57 std::vector<uint8_t> internal_audio_ram_ = std::vector<uint8_t>(0x10000, 0);
58};
59
63class Spc700Test : public ::testing::Test {
64 public:
65 Spc700Test() = default;
66 void SetUp() override {
67 // Set up the mock
68 audioRAM.SetUp();
69
70 // Set the Spc700 to bank 01
71 spc700.PC = 0x0100;
72 }
73
74 testing::StrictMock<MockAudioRam> audioRAM;
77};
78
79// ========================================================
80// 8-bit Move Memory to Register
81
82TEST_F(Spc700Test, MOV_A_Immediate) {
83 // MOV A, imm
84 uint8_t opcode = 0xE8;
85 uint8_t immediate_value = 0x5A;
86 audioRAM.SetupMemory(0x0100, {opcode, immediate_value});
87
88 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
89
90 spc700.ExecuteInstructions(opcode);
91
92 EXPECT_EQ(spc700.A, immediate_value);
93 EXPECT_EQ(spc700.PSW.Z, 0);
94 EXPECT_EQ(spc700.PSW.N, 0);
95}
96
97TEST_F(Spc700Test, MOV_A_X) {
98 // MOV A, X
99 uint8_t opcode = 0x7D;
100 spc700.X = 0x5A;
101
102 spc700.ExecuteInstructions(opcode);
103
104 EXPECT_EQ(spc700.A, spc700.X);
105 EXPECT_EQ(spc700.PSW.Z, 0);
106 EXPECT_EQ(spc700.PSW.N, 0);
107}
108
110 // MOV A, Y
111 uint8_t opcode = 0xDD;
112 spc700.Y = 0x5A;
113
114 spc700.ExecuteInstructions(opcode);
115
116 EXPECT_EQ(spc700.A, spc700.Y);
117 EXPECT_EQ(spc700.PSW.Z, 0);
118 EXPECT_EQ(spc700.PSW.N, 0);
119}
120
121TEST_F(Spc700Test, MOV_A_dp) {
122 // MOV A, dp
123 uint8_t opcode = 0xE4;
124 uint8_t dp_value = 0x5A;
125 audioRAM.SetupMemory(0x005A, {0x42});
126 audioRAM.SetupMemory(0x0100, {opcode, dp_value});
127
128 EXPECT_CALL(audioRAM, read(_))
129 .WillOnce(Return(dp_value))
130 .WillOnce(Return(0x42));
131
132 spc700.ExecuteInstructions(opcode);
133
134 EXPECT_EQ(spc700.A, 0x42);
135 EXPECT_EQ(spc700.PSW.Z, 0);
136 EXPECT_EQ(spc700.PSW.N, 0);
137}
138
139TEST_F(Spc700Test, MOV_A_dp_plus_x) {
140 // MOV A, dp+X
141 uint8_t opcode = 0xF4;
142 uint8_t dp_value = 0x5A;
143 spc700.X = 0x01;
144 audioRAM.SetupMemory(0x005B, {0x42});
145 audioRAM.SetupMemory(0x0100, {opcode, dp_value});
146
147 EXPECT_CALL(audioRAM, read(_))
148 .WillOnce(Return(dp_value + spc700.X))
149 .WillOnce(Return(0x42));
150
151 spc700.ExecuteInstructions(opcode);
152
153 EXPECT_EQ(spc700.A, 0x42);
154 EXPECT_EQ(spc700.PSW.Z, 0);
155 EXPECT_EQ(spc700.PSW.N, 0);
156}
157
158TEST_F(Spc700Test, MOV_A_dp_indirect_plus_y) {
159 // MOV A, [dp]+Y
160 uint8_t opcode = 0xF7;
161 uint8_t dp_value = 0x5A;
162 spc700.Y = 0x01;
163 audioRAM.SetupMemory(0x005A, {0x00, 0x42});
164 audioRAM.SetupMemory(0x0100, {opcode, dp_value});
165 audioRAM.SetupMemory(0x4201, {0x69});
166
167 EXPECT_CALL(audioRAM, read(_))
168 .WillOnce(Return(dp_value))
169 .WillOnce(Return(0x4200))
170 .WillOnce(Return(0x69));
171
172 spc700.ExecuteInstructions(opcode);
173
174 EXPECT_EQ(spc700.A, 0x69);
175 EXPECT_EQ(spc700.PSW.Z, 0);
176 EXPECT_EQ(spc700.PSW.N, 0);
177}
178
179TEST_F(Spc700Test, MOV_A_dp_plus_x_indirect) {
180 // MOV A, [dp+X]
181 uint8_t opcode = 0xE7;
182 uint8_t dp_value = 0x5A;
183 spc700.X = 0x01;
184 audioRAM.SetupMemory(0x005B, {0x00, 0x42});
185 audioRAM.SetupMemory(0x0100, {opcode, dp_value});
186 audioRAM.SetupMemory(0x4200, {0x69});
187
188 EXPECT_CALL(audioRAM, read(_))
189 .WillOnce(Return(dp_value + 1))
190 .WillOnce(Return(0x4200))
191 .WillOnce(Return(0x69));
192
193 spc700.ExecuteInstructions(opcode);
194
195 EXPECT_EQ(spc700.A, 0x69);
196 EXPECT_EQ(spc700.PSW.Z, 0);
197 EXPECT_EQ(spc700.PSW.N, 0);
198}
199
200TEST_F(Spc700Test, MOV_A_abs) {
201 // MOV A, !abs
202 uint8_t opcode = 0xE5;
203 uint16_t abs_addr = 0x1234;
204 uint8_t abs_value = 0x5A;
205
206 EXPECT_CALL(audioRAM, read(_))
207 .WillOnce(Return(abs_addr & 0xFF)) // Low byte
208 .WillOnce(Return(abs_addr >> 8)); // High byte
209
210 EXPECT_CALL(audioRAM, read(abs_addr)).WillOnce(Return(abs_value));
211
212 spc700.ExecuteInstructions(opcode);
213
214 EXPECT_EQ(spc700.A, abs_value);
215 EXPECT_EQ(spc700.PSW.Z, 0);
216 EXPECT_EQ(spc700.PSW.N, 0);
217}
218
219// ============================================================================
220// 8-bit Move Register to Memory
221
222TEST_F(Spc700Test, MOV_Immediate) {
223 // MOV A, imm
224 uint8_t opcode = 0xE8;
225 uint8_t immediate_value = 0x5A;
226
227 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
228
229 spc700.ExecuteInstructions(opcode);
230
231 EXPECT_EQ(spc700.A, immediate_value);
232 EXPECT_EQ(spc700.PSW.Z, 0);
233 EXPECT_EQ(spc700.PSW.N, 0);
234}
235
236// ============================================================================
237
238TEST_F(Spc700Test, NOP_DoesNothing) {
239 // NOP opcode
240 uint8_t opcode = 0x00;
241
242 uint16_t initialPC = spc700.PC;
243 spc700.ExecuteInstructions(opcode);
244
245 // PC should increment by 1, no other changes
246 EXPECT_EQ(spc700.PC, initialPC + 1);
247 // Add checks for other registers if needed
248}
249
250TEST_F(Spc700Test, ADC_A_Immediate) {
251 // ADC A, #imm
252 uint8_t opcode = 0x88;
253 uint8_t immediate_value = 0x10;
254 spc700.A = 0x01;
255 spc700.PSW.C = 1; // Assume carry is set
256
257 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
258
259 spc700.ExecuteInstructions(opcode);
260
261 // Verify A, and flags
262 EXPECT_EQ(spc700.A, 0x12); // 0x01 + 0x10 + 1 (carry)
263 // Check for other flags (Z, C, etc.) based on the result
264}
265
266TEST_F(Spc700Test, BEQ_BranchesIfZeroFlagSet) {
267 // BEQ rel
268 uint8_t opcode = 0xF0;
269 int8_t offset = 0x05;
270 spc700.PSW.Z = 1; // Set Zero flag
271
272 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
273
274 uint16_t initialPC = spc700.PC + 1;
275 spc700.ExecuteInstructions(opcode);
276
277 EXPECT_EQ(spc700.PC, initialPC + offset);
278}
279
280TEST_F(Spc700Test, STA_Absolute) {
281 // STA !abs
282 uint8_t opcode = 0x85;
283 uint16_t abs_addr = 0x1234;
284 spc700.A = 0x80;
285
286 // Set up the mock to return the address for the absolute addressing
287 EXPECT_CALL(audioRAM, read(_))
288 .WillOnce(Return(abs_addr & 0xFF)) // Low byte
289 .WillOnce(Return(abs_addr >> 8)); // High byte
290
291 spc700.ExecuteInstructions(opcode);
292}
293
294TEST_F(Spc700Test, ExecuteADCWithImmediate) {
295 // ADC A, imm
296 uint8_t opcode = 0x88; // Replace with opcode for ADC A, imm
297 uint8_t immediate_value = 0x10;
298 spc700.A = 0x15;
299
300 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
301
302 spc700.ExecuteInstructions(opcode);
303
304 EXPECT_EQ(spc700.A, 0x25); // 0x15 + 0x10
305 EXPECT_EQ(spc700.PSW.Z, 0);
306 EXPECT_EQ(spc700.PSW.N, 0);
307 EXPECT_EQ(spc700.PSW.C, 0);
308}
309
310TEST_F(Spc700Test, ExecuteBRA) {
311 // BRA
312 uint8_t opcode = 0x2F;
313 int8_t offset = 0x05;
314
315 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
316
317 // rel() moves the PC forward one after read
318 uint16_t initialPC = spc700.PC + 1;
319 spc700.ExecuteInstructions(opcode);
320
321 EXPECT_EQ(spc700.PC, initialPC + offset);
322}
323
324TEST_F(Spc700Test, ReadFromAudioRAM) {
325 uint16_t address = 0x1234;
326 uint8_t expected_value = 0x5A;
327
328 EXPECT_CALL(audioRAM, read(address)).WillOnce(Return(expected_value));
329
330 uint8_t value = spc700.read(address);
331 EXPECT_EQ(value, expected_value);
332}
333
334TEST_F(Spc700Test, WriteToAudioRAM) {
335 uint16_t address = 0x1234;
336 uint8_t value = 0x5A;
337
338 EXPECT_CALL(audioRAM, write(address, value));
339
340 spc700.write(address, value);
341}
342
343TEST_F(Spc700Test, ExecuteANDWithImmediate) {
344 // AND A, imm
345 uint8_t opcode = 0x28;
346 uint8_t immediate_value = 0x0F;
347 spc700.A = 0x5A; // 0101 1010
348
349 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
350
351 spc700.ExecuteInstructions(opcode);
352
353 EXPECT_EQ(spc700.A, 0x0A); // 0101 1010 & 0000 1111 = 0000 1010
354 EXPECT_EQ(spc700.PSW.Z, 0);
355 EXPECT_EQ(spc700.PSW.N, 0);
356}
357
358TEST_F(Spc700Test, ExecuteORWithImmediate) {
359 // OR A, imm
360 uint8_t opcode = 0x08;
361 uint8_t immediate_value = 0x0F;
362 spc700.A = 0xA0; // 1010 0000
363
364 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
365
366 spc700.ExecuteInstructions(opcode);
367
368 EXPECT_EQ(spc700.A, 0xAF); // 1010 0000 | 0000 1111 = 1010 1111
369 EXPECT_EQ(spc700.PSW.Z, 0);
370 // EXPECT_EQ(spc700.PSW.N, 1);
371}
372
373TEST_F(Spc700Test, ExecuteEORWithImmediate) {
374 // EOR A, imm
375 uint8_t opcode = 0x48;
376 uint8_t immediate_value = 0x5A;
377 spc700.A = 0x5A; // 0101 1010
378
379 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(immediate_value));
380
381 spc700.ExecuteInstructions(opcode);
382
383 EXPECT_EQ(spc700.A, 0x00); // 0101 1010 ^ 0101 1010 = 0000 0000
384 EXPECT_EQ(spc700.PSW.Z, 1);
385 EXPECT_EQ(spc700.PSW.N, 0);
386}
387
388TEST_F(Spc700Test, ExecuteINC) {
389 // INC A
390 uint8_t opcode = 0xBC;
391 spc700.A = 0xFF;
392
393 spc700.ExecuteInstructions(opcode);
394
395 EXPECT_EQ(spc700.A, 0x00);
396 EXPECT_EQ(spc700.PSW.Z, 1);
397 EXPECT_EQ(spc700.PSW.N, 0);
398}
399
400TEST_F(Spc700Test, ExecuteDEC) {
401 // DEC A
402 uint8_t opcode = 0x9C;
403 spc700.A = 0x01;
404
405 spc700.ExecuteInstructions(opcode);
406
407 EXPECT_EQ(spc700.A, 0x00);
408 EXPECT_EQ(spc700.PSW.Z, 1);
409 EXPECT_EQ(spc700.PSW.N, 0);
410}
411
412TEST_F(Spc700Test, ExecuteBNEWhenNotEqual) {
413 // BNE
414 uint8_t opcode = 0xD0;
415 int8_t offset = 0x05;
416 spc700.PSW.Z = 0;
417
418 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
419
420 uint16_t initialPC = spc700.PC + 1;
421 spc700.ExecuteInstructions(opcode);
422
423 EXPECT_EQ(spc700.PC, initialPC + offset);
424}
425
426TEST_F(Spc700Test, ExecuteBNEWhenEqual) {
427 // BNE
428 uint8_t opcode = 0xD0;
429 int8_t offset = 0x05;
430 spc700.PSW.Z = 1;
431
432 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
433
434 uint16_t initialPC = spc700.PC;
435 spc700.ExecuteInstructions(opcode);
436
437 EXPECT_EQ(spc700.PC, initialPC + 1); // +1 because of reading the offset
438}
439
440TEST_F(Spc700Test, ExecuteBEQWhenEqual) {
441 // BEQ
442 uint8_t opcode = 0xF0;
443 int8_t offset = 0x05;
444 spc700.PSW.Z = 1;
445
446 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
447
448 uint16_t initialPC = spc700.PC + 1;
449 spc700.ExecuteInstructions(opcode);
450
451 EXPECT_EQ(spc700.PC, initialPC + offset);
452}
453
454TEST_F(Spc700Test, ExecuteBEQWhenNotEqual) {
455 // BEQ
456 uint8_t opcode = 0xF0;
457 int8_t offset = 0x05;
458 spc700.PSW.Z = 0;
459
460 EXPECT_CALL(audioRAM, read(_)).WillOnce(Return(offset));
461
462 uint16_t initialPC = spc700.PC;
463 spc700.ExecuteInstructions(opcode);
464
465 EXPECT_EQ(spc700.PC, initialPC + 1); // +1 because of reading the offset
466}
467
468TEST_F(Spc700Test, BootIplRomOk) {
469 // Boot the IPL ROM
470 // spc700.BootIplRom();
471 // EXPECT_EQ(spc700.PC, 0xFFC1 + 0x3F);
472}
473
474} // namespace emu
475} // namespace test
476} // namespace yaze
AudioRam is an interface for the Audio RAM used by the SPC700.
Definition spc700.h:18
virtual uint8_t & mutable_read(uint16_t address)=0
virtual void write(uint16_t address, uint8_t value)=0
virtual uint8_t read(uint16_t address) const =0
The Spc700 class represents the SPC700 processor.
Definition spc700.h:72
void ExecuteInstructions(uint8_t opcode)
Definition spc700.cc:58
MockAudioRam is a mock class for the AudioRam class.
std::vector< uint8_t > internal_audio_ram_
MOCK_METHOD(void, reset,(),(override))
void SetupMemory(uint16_t address, const std::vector< uint8_t > &values)
MOCK_METHOD(uint8_t &, mutable_read,(uint16_t address),(override))
MOCK_METHOD(uint8_t, read,(uint16_t address),(const, override))
MOCK_METHOD(void, write,(uint16_t address, uint8_t value),(override))
testing::StrictMock< MockAudioRam > audioRAM
TEST_F(CpuTest, AsmParserTokenizerOk)
Definition cpu_test.cc:47
Definition common.cc:22