all repos — mgba @ a330df2f1fc617254e793702ebb5e3ed869af804

mGBA Game Boy Advance Emulator

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