src/gb/test/rtc.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/test/suite.h"
7
8#include <mgba/core/core.h>
9#include <mgba/gb/core.h>
10#include <mgba/internal/gb/gb.h>
11#include <mgba/internal/gb/mbc.h>
12#include <mgba-util/vfs.h>
13
14struct GBRTCTest {
15 struct mRTCSource d;
16 struct mCore* core;
17 struct VFile* fakeSave;
18 time_t nextTime;
19};
20
21static void _sampleRtc(struct GB* gb) {
22 gb->memory.rtcLastLatch = 0;
23 GBStore8(gb->cpu, 0x6000, 0);
24 GBStore8(gb->cpu, 0x6000, 1);
25}
26
27static void _sampleRtcKeepLatch(struct GB* gb) {
28 GBStore8(gb->cpu, 0x6000, 0);
29 GBStore8(gb->cpu, 0x6000, 1);
30}
31
32static time_t _testTime(struct mRTCSource* source) {
33 struct GBRTCTest* test = (struct GBRTCTest*) source;
34 return test->nextTime;
35}
36
37M_TEST_SUITE_SETUP(GBRTC) {
38 struct GBRTCTest* test = malloc(sizeof(*test));
39 if (!test) {
40 return -1;
41 }
42 test->core = GBCoreCreate();
43 test->d.sample = NULL;
44 test->d.unixTime = _testTime;
45 test->nextTime = 0;
46 if (!test->core) {
47 *state = NULL;
48 return -1;
49 }
50 test->core->init(test->core);
51 mCoreInitConfig(test->core, NULL);
52 struct VFile* vf = VFileMemChunk(NULL, 2048);
53 GBSynthesizeROM(vf);
54 test->core->loadROM(test->core, vf);
55 mCoreSetRTC(test->core, &test->d);
56
57 test->fakeSave = VFileMemChunk(NULL, 0);
58 test->core->loadSave(test->core, test->fakeSave);
59
60 struct GB* gb = test->core->board;
61 gb->memory.mbcType = GB_MBC3_RTC;
62
63 *state = test;
64 return 0;
65}
66
67M_TEST_SUITE_TEARDOWN(GBRTC) {
68 if (!*state) {
69 return 0;
70 }
71 struct GBRTCTest* test = *state;
72 test->core->deinit(test->core);
73 free(test);
74 return 0;
75}
76
77M_TEST_DEFINE(create) {
78 struct GBRTCTest* test = *state;
79 test->core->reset(test->core);
80 struct GB* gb = test->core->board;
81
82 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
83 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
84 assert_int_equal(gb->memory.mbcType, GB_MBC3_RTC);
85}
86
87M_TEST_DEFINE(tickSecond) {
88 struct GBRTCTest* test = *state;
89 test->nextTime = 0;
90 test->core->reset(test->core);
91 struct GB* gb = test->core->board;
92 test->nextTime = 1;
93
94 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
95 memset(gb->memory.rtcRegs, 0, sizeof(expected));
96
97 gb->memory.rtcRegs[0] = 0;
98 expected[0] = 1;
99 _sampleRtc(gb);
100 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
101
102 gb->memory.rtcRegs[0] = 1;
103 expected[0] = 2;
104 _sampleRtc(gb);
105 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
106
107 gb->memory.rtcRegs[0] = 59;
108 gb->memory.rtcRegs[1] = 0;
109 expected[0] = 0;
110 expected[1] = 1;
111 _sampleRtc(gb);
112 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
113}
114
115M_TEST_DEFINE(tick30Seconds) {
116 struct GBRTCTest* test = *state;
117 test->nextTime = 0;
118 test->core->reset(test->core);
119 struct GB* gb = test->core->board;
120 test->nextTime = 30;
121
122 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
123 memset(gb->memory.rtcRegs, 0, sizeof(expected));
124
125 gb->memory.rtcRegs[0] = 0;
126 expected[0] = 30;
127 _sampleRtc(gb);
128 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
129
130 gb->memory.rtcRegs[0] = 1;
131 expected[0] = 31;
132 _sampleRtc(gb);
133 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
134
135 gb->memory.rtcRegs[0] = 30;
136 gb->memory.rtcRegs[1] = 0;
137 expected[0] = 0;
138 expected[1] = 1;
139 _sampleRtc(gb);
140 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
141
142 gb->memory.rtcRegs[0] = 59;
143 gb->memory.rtcRegs[1] = 0;
144 expected[0] = 29;
145 expected[1] = 1;
146 _sampleRtc(gb);
147 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
148}
149
150M_TEST_DEFINE(tickMinute) {
151 struct GBRTCTest* test = *state;
152 test->nextTime = 0;
153 test->core->reset(test->core);
154 struct GB* gb = test->core->board;
155 test->nextTime = 60;
156
157 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
158 memset(gb->memory.rtcRegs, 0, sizeof(expected));
159
160 gb->memory.rtcRegs[0] = 0;
161 gb->memory.rtcRegs[1] = 0;
162 expected[0] = 0;
163 expected[1] = 1;
164 _sampleRtc(gb);
165 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
166
167 gb->memory.rtcRegs[0] = 1;
168 gb->memory.rtcRegs[1] = 0;
169 expected[0] = 1;
170 expected[1] = 1;
171 _sampleRtc(gb);
172 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
173
174 gb->memory.rtcRegs[0] = 0;
175 gb->memory.rtcRegs[1] = 59;
176 gb->memory.rtcRegs[2] = 0;
177 expected[0] = 0;
178 expected[1] = 0;
179 expected[2] = 1;
180 _sampleRtc(gb);
181 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
182
183 gb->memory.rtcRegs[0] = 1;
184 gb->memory.rtcRegs[1] = 59;
185 gb->memory.rtcRegs[2] = 0;
186 expected[0] = 1;
187 expected[1] = 0;
188 expected[2] = 1;
189 _sampleRtc(gb);
190 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
191}
192
193M_TEST_DEFINE(tick90Seconds) {
194 struct GBRTCTest* test = *state;
195 test->nextTime = 0;
196 test->core->reset(test->core);
197 struct GB* gb = test->core->board;
198 test->nextTime = 90;
199
200 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
201 memset(gb->memory.rtcRegs, 0, sizeof(expected));
202
203 gb->memory.rtcRegs[0] = 0;
204 gb->memory.rtcRegs[1] = 0;
205 expected[0] = 30;
206 expected[1] = 1;
207 _sampleRtc(gb);
208 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
209
210 gb->memory.rtcRegs[0] = 1;
211 gb->memory.rtcRegs[1] = 0;
212 expected[0] = 31;
213 expected[1] = 1;
214 _sampleRtc(gb);
215 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
216
217 gb->memory.rtcRegs[0] = 30;
218 gb->memory.rtcRegs[1] = 0;
219 expected[0] = 0;
220 expected[1] = 2;
221 _sampleRtc(gb);
222 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
223
224 gb->memory.rtcRegs[0] = 59;
225 gb->memory.rtcRegs[1] = 0;
226 expected[0] = 29;
227 expected[1] = 2;
228 _sampleRtc(gb);
229 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
230
231 gb->memory.rtcRegs[0] = 0;
232 gb->memory.rtcRegs[1] = 59;
233 gb->memory.rtcRegs[2] = 0;
234 expected[0] = 30;
235 expected[1] = 0;
236 expected[2] = 1;
237 _sampleRtc(gb);
238 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
239
240 gb->memory.rtcRegs[0] = 1;
241 gb->memory.rtcRegs[1] = 59;
242 gb->memory.rtcRegs[2] = 0;
243 expected[0] = 31;
244 expected[1] = 0;
245 expected[2] = 1;
246 _sampleRtc(gb);
247 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
248}
249
250M_TEST_DEFINE(tick30Minutes) {
251 struct GBRTCTest* test = *state;
252 test->nextTime = 0;
253 test->core->reset(test->core);
254 struct GB* gb = test->core->board;
255 test->nextTime = 1800;
256
257 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
258 memset(gb->memory.rtcRegs, 0, sizeof(expected));
259
260 gb->memory.rtcRegs[0] = 0;
261 gb->memory.rtcRegs[1] = 0;
262 expected[0] = 0;
263 expected[1] = 30;
264 _sampleRtc(gb);
265 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
266
267 gb->memory.rtcRegs[0] = 1;
268 gb->memory.rtcRegs[1] = 0;
269 expected[0] = 1;
270 expected[1] = 30;
271 _sampleRtc(gb);
272 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
273
274 gb->memory.rtcRegs[0] = 0;
275 gb->memory.rtcRegs[1] = 59;
276 gb->memory.rtcRegs[2] = 0;
277 expected[0] = 0;
278 expected[1] = 29;
279 expected[2] = 1;
280 _sampleRtc(gb);
281 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
282
283 gb->memory.rtcRegs[0] = 1;
284 gb->memory.rtcRegs[1] = 59;
285 gb->memory.rtcRegs[2] = 0;
286 expected[0] = 1;
287 expected[1] = 29;
288 expected[2] = 1;
289 _sampleRtc(gb);
290 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
291}
292
293M_TEST_DEFINE(tickHour) {
294 struct GBRTCTest* test = *state;
295 test->nextTime = 0;
296 test->core->reset(test->core);
297 struct GB* gb = test->core->board;
298 test->nextTime = 3600;
299
300 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
301 memset(gb->memory.rtcRegs, 0, sizeof(expected));
302
303 gb->memory.rtcRegs[0] = 0;
304 gb->memory.rtcRegs[1] = 0;
305 gb->memory.rtcRegs[2] = 0;
306 expected[0] = 0;
307 expected[1] = 0;
308 expected[2] = 1;
309 _sampleRtc(gb);
310 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
311
312 gb->memory.rtcRegs[0] = 1;
313 gb->memory.rtcRegs[1] = 0;
314 gb->memory.rtcRegs[2] = 0;
315 expected[0] = 1;
316 expected[1] = 0;
317 expected[2] = 1;
318 _sampleRtc(gb);
319 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
320
321 gb->memory.rtcRegs[0] = 0;
322 gb->memory.rtcRegs[1] = 1;
323 gb->memory.rtcRegs[2] = 0;
324 expected[0] = 0;
325 expected[1] = 1;
326 expected[2] = 1;
327 _sampleRtc(gb);
328 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
329
330 gb->memory.rtcRegs[0] = 0;
331 gb->memory.rtcRegs[1] = 0;
332 gb->memory.rtcRegs[2] = 1;
333 expected[0] = 0;
334 expected[1] = 0;
335 expected[2] = 2;
336 _sampleRtc(gb);
337 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
338
339 gb->memory.rtcRegs[0] = 0;
340 gb->memory.rtcRegs[1] = 0;
341 gb->memory.rtcRegs[2] = 23;
342 gb->memory.rtcRegs[3] = 0;
343 expected[0] = 0;
344 expected[1] = 0;
345 expected[2] = 0;
346 expected[3] = 1;
347 _sampleRtc(gb);
348 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
349}
350
351M_TEST_DEFINE(tick12Hours) {
352 struct GBRTCTest* test = *state;
353 test->nextTime = 0;
354 test->core->reset(test->core);
355 struct GB* gb = test->core->board;
356 test->nextTime = 3600 * 12;
357
358 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
359 memset(gb->memory.rtcRegs, 0, sizeof(expected));
360
361 gb->memory.rtcRegs[0] = 0;
362 gb->memory.rtcRegs[1] = 0;
363 gb->memory.rtcRegs[2] = 0;
364 expected[0] = 0;
365 expected[1] = 0;
366 expected[2] = 12;
367 _sampleRtc(gb);
368 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
369
370 gb->memory.rtcRegs[0] = 1;
371 gb->memory.rtcRegs[1] = 0;
372 gb->memory.rtcRegs[2] = 0;
373 expected[0] = 1;
374 expected[1] = 0;
375 expected[2] = 12;
376 _sampleRtc(gb);
377 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
378
379 gb->memory.rtcRegs[0] = 0;
380 gb->memory.rtcRegs[1] = 1;
381 gb->memory.rtcRegs[2] = 0;
382 expected[0] = 0;
383 expected[1] = 1;
384 expected[2] = 12;
385 _sampleRtc(gb);
386 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
387
388 gb->memory.rtcRegs[0] = 1;
389 gb->memory.rtcRegs[1] = 1;
390 gb->memory.rtcRegs[2] = 0;
391 expected[0] = 1;
392 expected[1] = 1;
393 expected[2] = 12;
394 _sampleRtc(gb);
395 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
396
397 gb->memory.rtcRegs[0] = 0;
398 gb->memory.rtcRegs[1] = 0;
399 gb->memory.rtcRegs[2] = 12;
400 gb->memory.rtcRegs[3] = 0;
401 expected[0] = 0;
402 expected[1] = 0;
403 expected[2] = 0;
404 expected[3] = 1;
405 _sampleRtc(gb);
406 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
407
408 gb->memory.rtcRegs[0] = 0;
409 gb->memory.rtcRegs[1] = 0;
410 gb->memory.rtcRegs[2] = 23;
411 gb->memory.rtcRegs[3] = 0;
412 expected[0] = 0;
413 expected[1] = 0;
414 expected[2] = 11;
415 expected[3] = 1;
416 _sampleRtc(gb);
417 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
418}
419
420M_TEST_DEFINE(tickDay) {
421 struct GBRTCTest* test = *state;
422 test->nextTime = 0;
423 test->core->reset(test->core);
424 struct GB* gb = test->core->board;
425 test->nextTime = 3600 * 24;
426
427 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
428 memset(gb->memory.rtcRegs, 0, sizeof(expected));
429
430 gb->memory.rtcRegs[0] = 0;
431 gb->memory.rtcRegs[1] = 0;
432 gb->memory.rtcRegs[2] = 0;
433 gb->memory.rtcRegs[3] = 0;
434 expected[0] = 0;
435 expected[1] = 0;
436 expected[2] = 0;
437 expected[3] = 1;
438 _sampleRtc(gb);
439 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
440
441 gb->memory.rtcRegs[0] = 1;
442 gb->memory.rtcRegs[1] = 0;
443 gb->memory.rtcRegs[2] = 0;
444 gb->memory.rtcRegs[3] = 0;
445 expected[0] = 1;
446 expected[1] = 0;
447 expected[2] = 0;
448 expected[3] = 1;
449 _sampleRtc(gb);
450 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
451
452 gb->memory.rtcRegs[0] = 0;
453 gb->memory.rtcRegs[1] = 1;
454 gb->memory.rtcRegs[2] = 0;
455 gb->memory.rtcRegs[3] = 0;
456 expected[0] = 0;
457 expected[1] = 1;
458 expected[2] = 0;
459 expected[3] = 1;
460 _sampleRtc(gb);
461 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
462
463 gb->memory.rtcRegs[0] = 1;
464 gb->memory.rtcRegs[1] = 1;
465 gb->memory.rtcRegs[2] = 0;
466 gb->memory.rtcRegs[3] = 0;
467 expected[0] = 1;
468 expected[1] = 1;
469 expected[2] = 0;
470 expected[3] = 1;
471 _sampleRtc(gb);
472 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
473
474 gb->memory.rtcRegs[0] = 0;
475 gb->memory.rtcRegs[1] = 0;
476 gb->memory.rtcRegs[2] = 12;
477 gb->memory.rtcRegs[3] = 0;
478 expected[0] = 0;
479 expected[1] = 0;
480 expected[2] = 12;
481 expected[3] = 1;
482 _sampleRtc(gb);
483 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
484
485 gb->memory.rtcRegs[0] = 0;
486 gb->memory.rtcRegs[1] = 0;
487 gb->memory.rtcRegs[2] = 23;
488 gb->memory.rtcRegs[3] = 0;
489 expected[0] = 0;
490 expected[1] = 0;
491 expected[2] = 23;
492 expected[3] = 1;
493 _sampleRtc(gb);
494 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
495
496 gb->memory.rtcRegs[0] = 0;
497 gb->memory.rtcRegs[1] = 0;
498 gb->memory.rtcRegs[2] = 0;
499 gb->memory.rtcRegs[3] = 1;
500 expected[0] = 0;
501 expected[1] = 0;
502 expected[2] = 0;
503 expected[3] = 2;
504 _sampleRtc(gb);
505 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
506
507 gb->memory.rtcRegs[0] = 0;
508 gb->memory.rtcRegs[1] = 0;
509 gb->memory.rtcRegs[2] = 1;
510 gb->memory.rtcRegs[3] = 1;
511 expected[0] = 0;
512 expected[1] = 0;
513 expected[2] = 1;
514 expected[3] = 2;
515 _sampleRtc(gb);
516 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
517}
518
519M_TEST_DEFINE(wideTickDay) {
520 struct GBRTCTest* test = *state;
521 test->nextTime = 0;
522 test->core->reset(test->core);
523 struct GB* gb = test->core->board;
524 test->nextTime = 3600 * 24 * 2001;
525
526 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
527 memset(gb->memory.rtcRegs, 0, sizeof(expected));
528
529 gb->memory.rtcRegs[0] = 0;
530 gb->memory.rtcRegs[1] = 0;
531 gb->memory.rtcRegs[2] = 0;
532 gb->memory.rtcRegs[3] = 0;
533 expected[0] = 0;
534 expected[1] = 0;
535 expected[2] = 0;
536 expected[3] = 1;
537 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
538 _sampleRtcKeepLatch(gb);
539 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
540
541 gb->memory.rtcRegs[0] = 1;
542 gb->memory.rtcRegs[1] = 0;
543 gb->memory.rtcRegs[2] = 0;
544 gb->memory.rtcRegs[3] = 0;
545 expected[0] = 1;
546 expected[1] = 0;
547 expected[2] = 0;
548 expected[3] = 1;
549 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
550 _sampleRtcKeepLatch(gb);
551 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
552
553 gb->memory.rtcRegs[0] = 0;
554 gb->memory.rtcRegs[1] = 1;
555 gb->memory.rtcRegs[2] = 0;
556 gb->memory.rtcRegs[3] = 0;
557 expected[0] = 0;
558 expected[1] = 1;
559 expected[2] = 0;
560 expected[3] = 1;
561 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
562 _sampleRtcKeepLatch(gb);
563 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
564
565 gb->memory.rtcRegs[0] = 1;
566 gb->memory.rtcRegs[1] = 1;
567 gb->memory.rtcRegs[2] = 0;
568 gb->memory.rtcRegs[3] = 0;
569 expected[0] = 1;
570 expected[1] = 1;
571 expected[2] = 0;
572 expected[3] = 1;
573 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
574 _sampleRtcKeepLatch(gb);
575 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
576
577 gb->memory.rtcRegs[0] = 0;
578 gb->memory.rtcRegs[1] = 0;
579 gb->memory.rtcRegs[2] = 12;
580 gb->memory.rtcRegs[3] = 0;
581 expected[0] = 0;
582 expected[1] = 0;
583 expected[2] = 12;
584 expected[3] = 1;
585 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
586 _sampleRtcKeepLatch(gb);
587 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
588
589 gb->memory.rtcRegs[0] = 0;
590 gb->memory.rtcRegs[1] = 0;
591 gb->memory.rtcRegs[2] = 23;
592 gb->memory.rtcRegs[3] = 0;
593 expected[0] = 0;
594 expected[1] = 0;
595 expected[2] = 23;
596 expected[3] = 1;
597 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
598 _sampleRtcKeepLatch(gb);
599 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
600
601 gb->memory.rtcRegs[0] = 0;
602 gb->memory.rtcRegs[1] = 0;
603 gb->memory.rtcRegs[2] = 0;
604 gb->memory.rtcRegs[3] = 1;
605 expected[0] = 0;
606 expected[1] = 0;
607 expected[2] = 0;
608 expected[3] = 2;
609 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
610 _sampleRtcKeepLatch(gb);
611 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
612
613 gb->memory.rtcRegs[0] = 0;
614 gb->memory.rtcRegs[1] = 0;
615 gb->memory.rtcRegs[2] = 1;
616 gb->memory.rtcRegs[3] = 1;
617 expected[0] = 0;
618 expected[1] = 0;
619 expected[2] = 1;
620 expected[3] = 2;
621 gb->memory.rtcLastLatch = 3600 * 24 * 2000;
622 _sampleRtcKeepLatch(gb);
623 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
624}
625
626M_TEST_DEFINE(rolloverSecond) {
627 struct GBRTCTest* test = *state;
628 test->nextTime = 0;
629 test->core->reset(test->core);
630 struct GB* gb = test->core->board;
631 test->nextTime = 1;
632
633 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
634 memset(gb->memory.rtcRegs, 0, sizeof(expected));
635
636 gb->memory.rtcRegs[0] = 59;
637 gb->memory.rtcRegs[1] = 0;
638 expected[0] = 0;
639 expected[1] = 1;
640 _sampleRtc(gb);
641 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
642
643 gb->memory.rtcRegs[0] = 59;
644 gb->memory.rtcRegs[1] = 59;
645 gb->memory.rtcRegs[2] = 0;
646 expected[0] = 0;
647 expected[1] = 0;
648 expected[2] = 1;
649 _sampleRtc(gb);
650 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
651
652 gb->memory.rtcRegs[0] = 59;
653 gb->memory.rtcRegs[1] = 59;
654 gb->memory.rtcRegs[2] = 23;
655 gb->memory.rtcRegs[3] = 0;
656 expected[0] = 0;
657 expected[1] = 0;
658 expected[2] = 0;
659 expected[3] = 1;
660 _sampleRtc(gb);
661 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
662
663 gb->memory.rtcRegs[0] = 59;
664 gb->memory.rtcRegs[1] = 59;
665 gb->memory.rtcRegs[2] = 23;
666 gb->memory.rtcRegs[3] = 0xFF;
667 gb->memory.rtcRegs[4] = 0;
668 expected[0] = 0;
669 expected[1] = 0;
670 expected[2] = 0;
671 expected[3] = 0;
672 expected[4] = 1;
673 _sampleRtc(gb);
674 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
675
676 gb->memory.rtcRegs[0] = 59;
677 gb->memory.rtcRegs[1] = 59;
678 gb->memory.rtcRegs[2] = 23;
679 gb->memory.rtcRegs[3] = 0xFF;
680 gb->memory.rtcRegs[4] = 1;
681 expected[0] = 0;
682 expected[1] = 0;
683 expected[2] = 0;
684 expected[3] = 0;
685 expected[4] = 0x80;
686 _sampleRtc(gb);
687 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
688}
689
690M_TEST_DEFINE(roundtrip0) {
691 struct GBRTCTest* test = *state;
692 test->nextTime = 0;
693 test->fakeSave->truncate(test->fakeSave, 0);
694 test->core->reset(test->core);
695 struct GB* gb = test->core->board;
696
697 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
698 memset(gb->memory.rtcRegs, 0, sizeof(expected));
699 GBMBCRTCWrite(gb);
700
701 GBMBCRTCRead(gb);
702 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
703
704 GBMBCRTCRead(gb);
705 _sampleRtcKeepLatch(gb);
706 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
707
708 GBMBCRTCWrite(gb);
709 _sampleRtcKeepLatch(gb);
710 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
711 GBMBCRTCRead(gb);
712 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
713
714 test->nextTime = 3600;
715 expected[2] = 1;
716 GBMBCRTCWrite(gb);
717 _sampleRtcKeepLatch(gb);
718 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
719 GBMBCRTCRead(gb);
720 _sampleRtcKeepLatch(gb);
721 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
722
723 GBMBCRTCWrite(gb);
724 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
725 GBMBCRTCRead(gb);
726 _sampleRtcKeepLatch(gb);
727 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
728
729 test->nextTime = 3600 * 2;
730 expected[2] = 2;
731 GBMBCRTCWrite(gb);
732 _sampleRtcKeepLatch(gb);
733 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
734 GBMBCRTCRead(gb);
735 _sampleRtcKeepLatch(gb);
736 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
737}
738
739M_TEST_DEFINE(roundtripSecond) {
740 struct GBRTCTest* test = *state;
741 test->nextTime = 0;
742 test->fakeSave->truncate(test->fakeSave, 0);
743 test->core->reset(test->core);
744 struct GB* gb = test->core->board;
745
746 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
747 memset(gb->memory.rtcRegs, 0, sizeof(expected));
748 GBMBCRTCWrite(gb);
749
750 test->nextTime = 1;
751 GBMBCRTCRead(gb);
752 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
753
754 expected[0] = 1;
755 _sampleRtcKeepLatch(gb);
756 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
757
758 GBMBCRTCWrite(gb);
759
760 test->nextTime = 2;
761 GBMBCRTCRead(gb);
762 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
763 _sampleRtcKeepLatch(gb);
764 expected[0] = 2;
765 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
766
767 test->core->reset(test->core);
768 expected[0] = 1;
769 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
770 _sampleRtcKeepLatch(gb);
771 expected[0] = 2;
772 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
773
774 test->core->reset(test->core);
775 expected[0] = 1;
776 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
777 GBMBCRTCRead(gb);
778 _sampleRtcKeepLatch(gb);
779 expected[0] = 2;
780 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
781
782 test->nextTime = 3;
783 test->core->reset(test->core);
784 expected[0] = 1;
785 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
786 _sampleRtcKeepLatch(gb);
787 expected[0] = 3;
788 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
789
790 test->nextTime = 1;
791 test->core->reset(test->core);
792 expected[0] = 1;
793 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
794 test->nextTime = 4;
795 _sampleRtcKeepLatch(gb);
796 expected[0] = 4;
797 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
798}
799
800M_TEST_DEFINE(roundtripDay) {
801 struct GBRTCTest* test = *state;
802 test->nextTime = 0;
803 test->fakeSave->truncate(test->fakeSave, 0);
804 test->core->reset(test->core);
805 struct GB* gb = test->core->board;
806
807 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
808 memset(gb->memory.rtcRegs, 0, sizeof(expected));
809 GBMBCRTCWrite(gb);
810 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
811
812 test->nextTime = 3600 * 24;
813 GBMBCRTCRead(gb);
814 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
815 expected[0] = 0;
816 expected[1] = 0;
817 expected[2] = 0;
818 expected[3] = 1;
819 _sampleRtcKeepLatch(gb);
820 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
821
822 test->nextTime = 3600 * 24 * 2;
823 expected[0] = 0;
824 expected[1] = 0;
825 expected[2] = 0;
826 expected[3] = 2;
827 GBMBCRTCRead(gb);
828 _sampleRtcKeepLatch(gb);
829 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
830
831 GBMBCRTCWrite(gb);
832
833 test->nextTime = 3600 * 24 * 2;
834 expected[0] = 0;
835 expected[1] = 0;
836 expected[2] = 0;
837 expected[3] = 2;
838 GBMBCRTCRead(gb);
839 _sampleRtcKeepLatch(gb);
840 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
841
842 test->nextTime = 3600 * 24 * 3;
843 expected[0] = 0;
844 expected[1] = 0;
845 expected[2] = 0;
846 expected[3] = 3;
847 GBMBCRTCRead(gb);
848 _sampleRtcKeepLatch(gb);
849 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
850
851 test->nextTime = 3600 * 24 * 2;
852 test->core->reset(test->core);
853 expected[0] = 0;
854 expected[1] = 0;
855 expected[2] = 0;
856 expected[3] = 2;
857 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
858 _sampleRtcKeepLatch(gb);
859 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
860
861 test->nextTime = 3600 * 24 * 3;
862 test->core->reset(test->core);
863 expected[0] = 0;
864 expected[1] = 0;
865 expected[2] = 0;
866 expected[3] = 2;
867 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
868 expected[0] = 0;
869 expected[1] = 0;
870 expected[2] = 0;
871 expected[3] = 3;
872 _sampleRtcKeepLatch(gb);
873 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
874}
875
876M_TEST_DEFINE(roundtripHuge) {
877 struct GBRTCTest* test = *state;
878 test->nextTime = 1500000000;
879 test->fakeSave->truncate(test->fakeSave, 0);
880 test->core->reset(test->core);
881 struct GB* gb = test->core->board;
882
883 uint8_t expected[sizeof(gb->memory.rtcRegs)] = { 0, 0, 0, 0, 0 };
884 memset(gb->memory.rtcRegs, 0, sizeof(expected));
885 GBMBCRTCWrite(gb);
886 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
887 _sampleRtcKeepLatch(gb);
888 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
889
890 test->nextTime = 1500000000 + 3600 * 24;
891 GBMBCRTCRead(gb);
892 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
893 expected[0] = 0;
894 expected[1] = 0;
895 expected[2] = 0;
896 expected[3] = 1;
897 _sampleRtcKeepLatch(gb);
898 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
899
900 test->nextTime = 1500000000 + 3600 * 24 * 2;
901 expected[0] = 0;
902 expected[1] = 0;
903 expected[2] = 0;
904 expected[3] = 2;
905 GBMBCRTCRead(gb);
906 _sampleRtcKeepLatch(gb);
907 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
908
909 GBMBCRTCWrite(gb);
910
911 test->nextTime = 1500000000 + 3600 * 24 * 2;
912 expected[0] = 0;
913 expected[1] = 0;
914 expected[2] = 0;
915 expected[3] = 2;
916 GBMBCRTCRead(gb);
917 _sampleRtcKeepLatch(gb);
918 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
919
920 test->nextTime = 1500000000 + 3600 * 24 * 3;
921 expected[0] = 0;
922 expected[1] = 0;
923 expected[2] = 0;
924 expected[3] = 3;
925 GBMBCRTCRead(gb);
926 _sampleRtcKeepLatch(gb);
927 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
928
929 test->nextTime = 1500000000 + 3600 * 24 * 2;
930 test->core->reset(test->core);
931 expected[0] = 0;
932 expected[1] = 0;
933 expected[2] = 0;
934 expected[3] = 2;
935 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
936 _sampleRtcKeepLatch(gb);
937 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
938
939 test->nextTime = 1500000000 + 3600 * 24 * 3;
940 test->core->reset(test->core);
941 expected[0] = 0;
942 expected[1] = 0;
943 expected[2] = 0;
944 expected[3] = 2;
945 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
946 expected[0] = 0;
947 expected[1] = 0;
948 expected[2] = 0;
949 expected[3] = 3;
950 _sampleRtcKeepLatch(gb);
951 assert_memory_equal(gb->memory.rtcRegs, expected, sizeof(expected));
952}
953
954M_TEST_SUITE_DEFINE_SETUP_TEARDOWN(GBRTC,
955 cmocka_unit_test(create),
956 cmocka_unit_test(tickSecond),
957 cmocka_unit_test(tick30Seconds),
958 cmocka_unit_test(tickMinute),
959 cmocka_unit_test(tick90Seconds),
960 cmocka_unit_test(tick30Minutes),
961 cmocka_unit_test(tickHour),
962 cmocka_unit_test(tick12Hours),
963 cmocka_unit_test(tickDay),
964 cmocka_unit_test(wideTickDay),
965 cmocka_unit_test(rolloverSecond),
966 cmocka_unit_test(roundtrip0),
967 cmocka_unit_test(roundtripSecond),
968 cmocka_unit_test(roundtripDay),
969 cmocka_unit_test(roundtripHuge))