/******************************************************************************\ ** ** This file is part of the Hades GBA Emulator, and is made available under ** the terms of the GNU General Public License version 2. ** ** Copyright (C) 2021-2026 - The Hades Authors ** \******************************************************************************/ /* ** Some of those tests were inspired by NBA's hardware tests and mGBA's technical blog. ** ** References: ** - https://github.com/nba-emu/hw-test/ ** - https://mgba.io/2020/01/25/infinite-loop-holy-grail/ */ #include #include #include #include #include #include #include /* ** Check that DMA's reads are latched. */ IWRAM_CODE bool dma_latch_test_1( void ) { bool success; u32 data; u32 res; u32 unused; data = 0xCAFEBABE; res = 0; unused = 0; // Fill DMA0's latch with 0xCAFEBABE REG_DMA0SAD = (u32)&data; REG_DMA0DAD = (u32)&unused; REG_DMA0CNT = DMA_ENABLE | DMA32 | 1; while (REG_DMA0CNT & DMA_ENABLE); // Read from invalid memory (< 0x02000000) using DMA0 should read the last // latched value REG_DMA0SAD = 0x0; REG_DMA0DAD = (u32)&res; REG_DMA0CNT = DMA_ENABLE | DMA32 | 1; while (REG_DMA0CNT & DMA_ENABLE); if (res == 0xCAFEBABE) { printf("DMA LATCH 1: PASS\n"); success = true; } else { printf("DMA LATCH 1: FAIL\n"); printf(" 0x%08lx != 0x%08lx\n", res, (u32)0xCAFEBABE); success = false; } REG_DMA0SAD = 0; REG_DMA0DAD = 0; REG_DMA0CNT = 0; return success; } /* ** Check that each DMA has its own latch, different from each other. */ IWRAM_CODE bool dma_latch_test_2( void ) { bool success; u32 data_dma0; u32 data_dma1; u32 unused; u32 res; data_dma0 = 0x1BADF00D; data_dma1 = 0x2BADCAFE; unused = 0; res = 0; // Fill DMA1's latch with 0x2BADCAFE REG_DMA1SAD = (u32)&data_dma1; REG_DMA1DAD = (u32)&unused; REG_DMA1CNT = DMA_ENABLE | DMA32 | 1; while (REG_DMA1CNT & DMA_ENABLE); // Fill DMA0's latch with 0x1BADFOOD REG_DMA0SAD = (u32)&data_dma0; REG_DMA0DAD = (u32)&unused; REG_DMA0CNT = DMA_ENABLE | DMA32 | 1; while (REG_DMA0CNT & DMA_ENABLE); // Read from invalid memory (< 0x02000000) using DMA1 should read the value // of that DMA's latch. // Therefore, the latch isn't shared among DMAs. REG_DMA1SAD = 0x0; REG_DMA1DAD = (u32)&res; REG_DMA1CNT = DMA_ENABLE | DMA32 | 1; while (REG_DMA1CNT & DMA_ENABLE); if (res == 0x2BADCAFE) { printf("DMA LATCH 2: PASS\n"); success = true; } else { printf("DMA LATCH 2: FAIL\n"); printf(" 0x%08lx != 0x%08lx\n", res, (u32)0x2BADCAFE); success = false; } REG_DMA0SAD = 0; REG_DMA0DAD = 0; REG_DMA0CNT = 0; REG_DMA1SAD = 0; REG_DMA1DAD = 0; REG_DMA1CNT = 0; return success; } /* ** Check that the first access of a DMA, if invalid, reads the last prefetched opcode. */ IWRAM_CODE bool dma_latch_test_3( void ) { bool success; u32 data; u32 unused; u32 res; data = 0xDEADBEEF; unused = 0; res = 0; // Fill DMA0's latch with 0xDEADBEEF. REG_DMA0SAD = (u32)&data; REG_DMA0DAD = (u32)&unused; REG_DMA0CNT = DMA_ENABLE | DMA32 | 1; while (REG_DMA0CNT & DMA_ENABLE); // The first read using DMA0, if from invalid memory (>= 0x02000000), should read what's // inside the memory bus, aka the last prefetched opcode and not the value latched above. REG_DMA0SAD = 0x04000FF0; REG_DMA0DAD = (u32)&res; REG_DMA0CNT = DMA_ENABLE | DMA32 | 1; while (REG_DMA0CNT & DMA_ENABLE); if (res == 0xe3530000) { printf("DMA LATCH 3: PASS\n"); success = true; } else { printf("DMA LATCH 3: FAIL\n"); printf(" 0x%08lx != 0x%08lx\n", res, 0xe3530000ul); success = false; } REG_DMA0SAD = 0; REG_DMA0DAD = 0; REG_DMA0CNT = 0; return success; } /* ** Check that the first invalid access that follows a DMA reads the latest value read by that DMA. ** ** This is kind-of a reproduction of the famous "Hello Kitty" bug. ** ** Reference: ** - https://mgba.io/2020/01/25/infinite-loop-holy-grail/ */ IWRAM_CODE bool dma_latch_test_4( void ) { bool success; u32 data; u32 value; value = 0; data = 0xFEEDC0DE; REG_SOUNDCNT_X |= SNDSTAT_ENABLE; REG_FIFO_A = 0; // Setup DMA1 to copy 0xFEEDC0DE to REG_FIFO_A every time Timer 0 overflows. REG_DMA1SAD = (u32)&data; REG_DMA1DAD = (u32)®_FIFO_A; REG_DMA1CNT = DMA_ENABLE | DMA_SPECIAL | DMA_SRC_FIXED | DMA_DST_FIXED | DMA32 | DMA_REPEAT | 1; __asm__ volatile( // Set r1 to REG_TM0CNT "ldr r1, =#0x04000100\n" // Set r2 to point to invalid memory "ldr r2, =#0x04000FF0\n" // Set the reload value to 0xFFFF "mov r0, %[reload]\n" "strh r0, [r1]\n" // Enable the timer "mov r0, #0x80\n" "strh r0, [r1, #0x2]\n" // Wait for the timer to overflow, right when the DMA is about to start "nop\n" "nop\n" "nop\n" "nop\n" // TODO: Investigate the difference in real hardware compared to Hades/NBA with/without this extra nop "nop\n" // Read from invalid memory, hoping the DMA left our desired value // in the memory bus. "ldr r0, [r2]\n" "mov %[value], r0\n" : [value]"=r"(value) : [reload]"r"(0xFFFF) : "r0", "r1", "r2" ); if (value == 0xFEEDC0DE) { printf("DMA LATCH 4: PASS\n"); success = true; } else { printf( "DMA LATCH 4: FAIL\n"); printf(" 0x%08lx != 0x%08lx\n", value, 0xFEEDC0DEul); success = false; } REG_TM0CNT_L = 0; REG_TM0CNT_H = 0; REG_DMA1SAD = 0; REG_DMA1DAD = 0; REG_DMA1CNT = 0; return success; } IWRAM_CODE int main( void ) { size_t nb_tests; u16 nb_tests_passed; u16 i; irqInit(); consoleDemoInit(); irqEnable(IRQ_VBLANK); printf("DMA Tests\n"); printf(" Latch & Open Bus\n\n"); bool (* const tests[])(void) = { dma_latch_test_1, dma_latch_test_2, dma_latch_test_3, dma_latch_test_4, }; nb_tests = sizeof(tests) / sizeof(tests[0]); nb_tests_passed = 0; for (i = 0; i < nb_tests; ++i) { nb_tests_passed += tests[i](); } printf("\n"); printf("Total: %u/%zu\n", nb_tests_passed, nb_tests); while (true) { VBlankIntrWait(); } return (0); }