;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; AccuracyCoin ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; This ROM is a collection of accuracy tests on an NROM cartridge. ; NOTE: While most of these tests are universal to all revisions of the NES board, CPU, and PPU, there are a handful of tests that are not going to pass on all revisions. ; To be more specific, these tests were designed for an RP2A03G APU/CPU, and an RP2C02G PPU. ; Additionally, if you run this ROM on your console with a flash cart, you might fail some tests. Notably, the open bus tests fail on an Everdrive N8 Pro. ; If you are looking for a specific test, consider CRTL + F searching for "TestPages:", as that's where the list of tests is. ; The format for the tests as they are stored in the ROM is: ; table "Name of test", $FF, Address_To_Store_Test_Results, Address_To_Jump_To_In_Order_To_Run_The_Test ; so to easily find the code for a test, you can search for the "Address_To_Jump_To_In_Order_To_Run_The_Test:" routine for a given test. ; NOTE: The NMI and IRQ vectors both point to RAM. This allows me to create tests that have different NMI/IRQ routines. ;;;; HEADER AND COMPILER STUFF ;;;; .inesprg 2 ; 2 banks .ineschr 1 ; .inesmap 0 ; mapper 0 = NROM .inesmir 0 ; background mirroring, horizontal ;;;; CONSTANTS ;;;; flag_c = $1 flag_z = $2 flag_i = $4 flag_d = $8 flag_v = $40 flag_n = $80 byte0 = $0 byte1 = $1 byte2 = $2 byte3 = $3 suitePointer = $5 dontSetPointer = $7 byte8 = $8 byte9 = $9 byteF = $F ErrorCode = $10 initialSubTest = $11 result_DMCDMASync_PreTest = $12 menuTabXPos = $14 menuCursorXPos = $15 menuCursorYPos = $16 menuHeight = $17 controller = $18 controller_New = $19 testAddressPointer = $1A TestResultPointer = $1E Test_UnOp_OperandTargetAddrLo = $20 Test_UnOp_OperandTargetAddrHi = $21 Test_UnOp_ValueAtAddressForTest = $22 Test_UnOp_A = $23 Test_UnOp_X = $24 Test_UnOp_Y = $25 Test_UnOp_FlagsInit = $26 Test_UnOp_SP = $27 Test_UnOp_ExpectedResultAddrLo = $28 Test_UnOp_ExpectedResultAddrHi = $29 Test_UnOp_ValueAtAddressResult = $2A Test_UnOp_CMP = $2B Test_UnOp_CPX = $2C Test_UnOp_CPY = $2D Test_UnOp_CM_Flags = $2E Test_UnOp_CPS = $2F Test_UnOp_IndirectPointerLo = $30 Test_UnOp_IndirectPointerHi = $31 Test_UnOp_CycleDelayPostDMA = $32 HighlightTextPrinted = $33 AutomateTestSuite = $34 RunningAllTests = $35 PostAllTestScreen = $36 PostAllTestTally = $37 PostAllPassTally = $38 PrintDecimalTensCheck = $39 result_VblankSync_PreTest = $3A DebugMode = $3B IncorrectReturnAddressOffset = $3C AllTestMenuTestNameOffsetLo = $3D AllTestMenuTestNameOffsetHi = $3E AllTestMenuTotalSkipped = $3F Reserved_41 = $41 ; Used in the Implied Dummy Reads. It's probably best we never actually use this. PostDMACyclesUntilTestInstruction = 13 Test_ZeroPageReserved = $50 ; through $5F Test_ZeroPageReserved2 = $60 ; through $6F (rarely used, but let's still avoid putting engine stuff here.) TESTHighlightTextCopy = $7A suiteAttributeCopy = $7E suitePointerList = $80 suiteExecPointerList = $A0 Reserved_C1 = $C1 ; Used in the Implied Dummy Reads. It's probably best we never actually use this. Reserved_C8 = $C8; For my "unofficial opcodes are correct length" tests, I use [Two-Byte-Opcode][INY], and then check the value of Y. Since INY is $C8, I'd like to avoid corrupting something stored in byte C8. Reserved_E1 = $E1 ; Used in the Implied Dummy Reads. It's probably best we never actually use this. Debug_EC = $EC ; This is used to see how far an emulator gets before hanging when loading the main menu. Copy_X2 = $ED ; These are exclusively used to keep registers from before RunTest from being modified during a test, so they can be restored after the test. Copy_Y2 = $EE ; ^ Copy_A2 = $EF ; ^ PPUCTRL_COPY = $F0 PPUMASK_COPY = $F1 Copy_SP = $FA Copy_SP2 = $FB Copy_Flags = $FC Copy_X = $FD Copy_Y = $FE Copy_A = $FF PowerOnRAM = $300 PowerOnVRAM = $320 PowerOnPalette = $340 PowerOnTest_PPUReset = $360 ; 1 byte. Pass/fail the PPU Reset flag test. PowerOn_A = $370 PowerOn_X = $371 PowerOn_Y = $372 PowerOn_SP = $373 PowerOn_P = $374 PowerOn_MagicNumber = $3F0 ;$400 to $4FF is where I store the results of the tests. result_Unimplemented = $0400 result_CPUInstr = $0401 result_RAMMirror = $0403 result_PPURegMirror = $0404 result_ROMnotWritable = $0405 result_DummyReads = $0406 result_DummyWrites = $0407 result_OpenBus = $0408 result_UnOp_SLO_03 = $409 result_UnOp_SLO_07 = $40A result_UnOp_SLO_0F = $40B result_UnOp_SLO_13 = $40C result_UnOp_SLO_17 = $40D result_UnOp_SLO_1B = $40E result_UnOp_SLO_1F = $40F result_UnOp_ANC_0B = $410 result_UnOp_ANC_2B = $411 result_UnOp_ASR_4B = $412 result_UnOp_ARR_6B = $413 result_UnOp_ANE_8B = $414 result_UnOp_LXA_AB = $415 result_UnOp_AXS_CB = $416 result_UnOp_SBC_EB = $417 result_UnOp_RLA_23 = $419 result_UnOp_RLA_27 = $41A result_UnOp_RLA_2F = $41B result_UnOp_RLA_33 = $41C result_UnOp_RLA_37 = $41D result_UnOp_RLA_3B = $41E result_UnOp_RLA_3F = $41F result_UnOp_SRE_43 = $420 result_UnOp_SRE_47 = $47F ; It's pretty funny, but I need address $421 to always be $00. (see Implied Dummy Reads where bit 5 of the opcode is set.) result_UnOp_SRE_4F = $422 result_UnOp_SRE_53 = $423 result_UnOp_SRE_57 = $424 result_UnOp_SRE_5B = $425 result_UnOp_SRE_5F = $426 result_UnOp_RRA_63 = $427 result_UnOp_RRA_67 = $428 result_UnOp_RRA_6F = $429 result_UnOp_RRA_73 = $42A result_UnOp_RRA_77 = $42B result_UnOp_RRA_7B = $42C result_UnOp_RRA_7F = $42D result_UnOp_SAX_83 = $42E result_UnOp_SAX_87 = $42F result_UnOp_SAX_8F = $430 result_UnOp_SAX_97 = $431 result_UnOp_LAX_A3 = $432 result_UnOp_LAX_A7 = $433 result_UnOp_LAX_AF = $434 result_UnOp_LAX_B3 = $435 result_UnOp_LAX_B7 = $436 result_UnOp_LAX_BF = $437 result_UnOp_DCP_C3 = $438 result_UnOp_DCP_C7 = $439 result_UnOp_DCP_CF = $43A result_UnOp_DCP_D3 = $43B result_UnOp_DCP_D7 = $43C result_UnOp_DCP_DB = $43D result_UnOp_DCP_DF = $43E result_UnOp_ISC_E3 = $43F result_UnOp_ISC_E7 = $440 result_UnOp_ISC_EF = $441 result_UnOp_ISC_F3 = $442 result_UnOp_ISC_F7 = $443 result_UnOp_ISC_FB = $444 result_UnOp_ISC_FF = $445 result_UnOp_SHA_93 = $446 result_UnOp_SHA_9F = $447 result_UnOp_SHS_9B = $448 result_UnOp_SHY_9C = $449 result_UnOp_SHX_9E = $44A result_UnOp_LAE_BB = $44B result_DMA_Plus_2007R = $44C result_ProgramCounter_Wraparound = $44D result_PPUIOOpenBus = $044E result_DMA_Plus_2007W = $44F result_VBlank_Beginning = $450 result_VBlank_End = $451 result_NMI_Control = $452 result_NMI_Timing = $453 result_NMI_Suppression = $454 result_NMI_VBL_End = $455 result_NMI_Disabled_VBL_Start = $456 result_Sprite0Hit_Behavior = $457 result_ArbitrarySpriteZero = $458 result_SprOverflow_Behavior = $459 result_MisalignedOAM_Behavior = $45A result_Address2004_Behavior = $45B result_APURegActivation = $45C result_DMA_Plus_4015R = $45D result_DMA_Plus_4016R = $45E result_ControllerStrobing = $45F result_InstructionTiming = $460 result_IFlagLatency = $461 result_NmiAndBrk = $462 result_NmiAndIrq = $463 result_RMW2007 = $464 result_APULengthCounter = $465 result_APULengthTable = $466 result_FrameCounterIRQ = $467 result_FrameCounter4Step = $468 result_FrameCounter5Step = $469 result_DeltaModulationChannel = $46A result_DMABusConflict = $46B result_DMA_Plus_OpenBus = $46C result_ImpliedDummyRead = $46D result_AddrMode_AbsIndex = $46E result_AddrMode_ZPgIndex = $46F result_AddrMode_Indirect = $470 result_AddrMode_IndIndeX = $471 result_AddrMode_IndIndeY = $472 result_AddrMode_Relative = $473 result_DecimalFlag = $474 result_BFlag = $475 result_PPUReadBuffer = $476 result_DMCDMAPlusOAMDMA = $477 result_ImplicitDMAAbort = $478 result_ExplicitDMAAbort = $479 result_ControllerClocking = $47A result_OAM_Corruption = $47B result_JSREdgeCases = $47C result_AllNOPs = $47D result_PaletteRAMQuirks = $47E ; 47F is used.If you add a new test, don't forget to skip that value. result_INC4014 = $480 result_AttributesAsTiles = $481 result_tRegisterQuirks = $482 result_StaleBGShiftRegisters = $483 result_Scanline0Sprites = $484 result_CHRROMIsNotWritable = $485 result_RenderingFlagBehavior = $486 result_BGSerialIn = $487 result_DMA_Plus_2002R = $488 result_SuddenlyResizeSprite = $489 result_Rendering2007Read = $48A result_BranchDummyRead = $48B result_2004_Stress = $48C result_2002FlagClearTiming = $48D result_2007_Stress = $48E result_StaleSpriteShiftRegs = $48F result_InternalDataBus = $490 result_ALERead = $491 result_HybridAddresses = $492 result_FrozenOAM2Inc = $493 result_MisalignedOAMDMA = $494 result_MisalignedOAM2Addr = $495 result_DMALandingOnWrite = $496 result_DMCReloadTiming = $497 result_DMCPlaybackRate = $498 result_DrawTest = $03FF ; page 3 omits the test from the all-test-result-table. ;$500 is dedicated to RAM needed for tests. ;$600 is dedicated to the IRQ routine ;$700 is dedicated to the NMI routine. ;;;; ASSEMBLY CODE ;;;; .org $8000 ; The open bus test needs to make sure an inaccurate emulation of open bus will fall into test code, so this function here is a fail condition of the open bus test. OpenBusTestFakedOpenBusBehavior: NOP ; An incorrect implementation of open bus might execute all the way to here from address $5000. NOP ; The two NOPs are for alignment, and this BRK takes the PC to some "test failed" handler. BRK ; Pushes 3 bytes to the stack, and moves the PC to the address determined by $FFFE, which is address $0600. CannotWriteToROM_01: .byte $01; This value is used in the "Cannot write to ROM" test. RESET: ; This ROM, despite the guidance of the NesDev Wiki's "startup code", writes a bunch of uninitialized registers, and reads uninitialized RAM. Intentionally. STA <$00 ; First thing we do at power on is store A to address $00 as a temporary place to hold it. This does not modify the CPU flags. PHP ; Push the processor flags... PLA ; ... and pull them off. AND #$CF ; Remove the B flag, and other garbage flag. STA <$01 ; And store this somewhere temporary. LDA PowerOn_MagicNumber ; Before we store these to the "test results", let's verify this is a cold boot and not a warm boot. CMP #$5A ; Assume a cold boot won't have this value here. BEQ RESET_SkipPowerOnTests ; If the value was $5A, skip storing stuff to RAM. LDA <$00 ; Okay cool, it's a cold boot. Let's start storing some stuff. Copy the value we set aside... STA PowerOn_A ; And store the value for use in TEST_PowerOnState_CPU_Registers STY PowerOn_Y ; Ditto for the Y register. STX PowerOn_X ; And the X register. TSX ; Let's fetch the stack pointer... STX PowerOn_SP ; And store it for that test. LDA <$01 ; We stored the flags here, so let's copy these... STA PowerOn_P ; And paste it in RAM. RESET_SkipPowerOnTests: SEI ; Time for some regular power on code. Fun fact: The I flag is already set at power on and when hitting reset. The CPU just does that. So this line isn't needed. CLD ; Disable the Decimal Flag. Who knows, maybe you hit reset in the middle of the Decimal Flag test. LDX #$EF ; Due to some tests modifying the stack pointer, it's convenient to put it at EF instead of FF. TXS ; This prevents some tests where the resulting stack pointer is 00 from pushing data, and overwriting the bottom of the stack. LDA #$40 STA $4017; Disable the APU Frame Counter IRQ. TEST_PPUResetFlag: ; All throughout this ROM, you will see me label the various tests like so: ; 3 semicolons, the error code that will appear if the test fails here, the name of the test in square brackets, then a description of what is being tested. ; Here's an example: ;;; Test 1 [PPU Reset Flag]: Are PPU Registers writable before the first pre-render line? ;;; ; They shouldn't be, as that's the job of the PPU Reset Flag! (NOTE: Not every console has this behavior!) ; Let's see if the PPU Reset flag exists. LDA #$27 STA $2006 ; "magic address" (Writing to $2006 twice will update the 'v' register of the PPU) LDA #$BF STA $2006 ; 'v' = $27BF LDA #$5A ; "magic number". All over this ROM, you will frequently see me using the value $5A for tests. That's 01011010 in binary, and I just assume that if something goes wrong, it won't stumble on that number by random chance. STA $2007 ; Okay, I'll be back in 2 frames to check on you... LDX #$FF ; We're going to stall for VBlank, increment X, then X=0, so we're going to stall until next VBlank yet again. LDA $2002 VblLoop: LDA $2002 ; This is PPU_STATUS. Bit 7 tells us if the PPU is currently in VBlank or not. BPL VblLoop ; So if bit 7 is 0 (we are not in VBlank) the Negative flag is not set, so "Branch of Plus" will be taken. INX ; X++ BEQ VblLoop ; If X is zero, we do this again. ; Now that the PPU is responsive, let's copy the resting values. LDA PowerOn_MagicNumber ; Check again if this is a cold or a warm boot. CMP #$5A BEQ PostResetFlagTest ; If this is a warm boot, skip copying the uninitialized RAM and VRAM. JSR Read32NametableBytes JSR ReadPaletteRAM ; Let's also see if the magic number was written to VRAM, to verify if the reset flag exists. ; It's worth noting that in its current state, this test fails on my console. I assume this has something to do with the flash cart I'm using. LDA #6 STA PowerOnTest_PPUReset ; set to FAIL (error code $1) by default. Overwrite with PASS if it passes. LDA #$27 STA $2006 LDA #$BF STA $2006 ; Set 'v' back to where we attempted to write our magic number. LDA $2007 ; load buffer LDA $2007 ; read buffer CMP #$5A BEQ PostResetFlagTest ; If A = $5A at this point, you fail the test since that means we wrote to VRAM before the PPU reset flag cleared. (Or uninitialized VRAM there was $5A?) ; The value of $5A was not written to VRAM, so the reset flag does exist! LDA #1 STA PowerOnTest_PPUReset ; Store a passing result here. ; I also indicate whenever a test is over with the following comment: ;; END OF TEST ;; PostResetFlagTest: JSR DisableRendering ; With uninitialized values from VRAM and Palette RAM copied for future reference, let's overwrite the palette and nametable. JSR SetUpDefaultPalette JSR ClearRAMExceptPage3 ; Page 3 holds a copy of uninitialized RAM, VRAM, Palette RAM... JSR VerifyJSRBehavior JSR ClearNametable LDA #$5A STA PowerOn_MagicNumber ; At this point, let's write out magic number to RAM, indicating that any reset after this point is a warm boot. ; So now if the reset button is pressed, we skip writing to the results of TEST_PowerOnState_CPU_Registers, and skip running the PPU reset flag test. ; I guess that means you could hit the reset button at any point before this to ruin the results of those tests. ; I'm not sure why you would do that though... ReloadMainMenu: ; There's an option to run every test in the ROM, and it draws a table of the results. This will run when exiting that screen with the table. ; If your emulator fails to reach the main menu of this ROM, check the value of address $EC. ; This can help inform you of specifically where your emulator hangs. INC 01 JSR ClearPage2 ; Page 2 is used for OAM. JSR OAMDMAWithPage2 LDA #0 STA 02 ; set up the NMI routine. JSR SetUpNMIRoutineForMainMenu LDA #0 STA $6000 ; An incorrect open bus implementation might end up executing address $6000, so let's initialize these 3 bytes to BRKs. STA $6001 ; Though I would prefer if this was a NES 2.0 cartridge without any PRG RAM, so writing here might do nothing anyway. STA $6002 ; There's still a good chance an emulator doesn't support NES 2.0 and just puts PRG RAM here anyway. INC 04 JSR WaitForVBlank INC 05 JSR TEST_VblankSync_PreTest; Initialize result_VblankSync_PreTest INC 06 JSR DMASync ; Initialize result_DMCDMASync_PreTest LDA #$FF STA 07 JSR LoadSuiteMenu ; Determine all the tests on the current page, and store the pointers in RAM. INC 08 JSR DrawPageNumber ; Draw the correct page number at the top of the screen. INC 09 JSR WaitForVBlank ; Stall until the PPU is in VBlank. JSR ResetScroll ; Set the ppu 'v' and 't' registers to $2000, and reset the fine scroll values as well. JSR EnableFullRendering; Enable rendering the background. JSR EnableNMI ; Enable the Non Maskable Interrupt. INC 0A ; If your emulator hangs here, you probably haven't implemented the NMI? InfiniteLoop: JMP InfiniteLoop ; This is the spinning loop while I wait for the NMI to occur. ;;;;;;;;;;;;;;;;;;;; VerifyJSRBehavior: ; Let's also verify that JSR is pushing the correct values to the stack. ; A handful of my subroutines pull off the values pushed by JSR, and use them to read data stored next to the JSR instruction. ; I need my code to still be able to load the menu even if the JSR return addresses are wrong. ; To verify this, I'll just put a JSR at address $0000, and jump there. LDA #$20 ; JSR STA <$00 LDA #Low(VerifyReturnAddressesAreCorrect) STA <$01 LDA #High(VerifyReturnAddressesAreCorrect) STA <$02 LDA #$60 ; RTS STA <$03 INC 03 JSR $0000 ; Verify return addresses pushed by JSR are correct. RTS ;;;;;;; .org $8100 ; Menu Data ; Here is how the pages of tests are organized. TableTable: .word Suite_CPUBehavior .word Suite_CPUInstructions .word Suite_UnofficialOps_SLO .word Suite_UnofficialOps_RLA .word Suite_UnofficialOps_SRE .word Suite_UnofficialOps_RRA .word Suite_UnofficialOps__AX .word Suite_UnofficialOps_DCP .word Suite_UnofficialOps_ISC .word Suite_UnofficialOps_SH_ .word Suite_UnofficialOps_Immediates .word Suite_CPUInterrupts .word Suite_DMATests .word Suite_APUTiming .word Suite_CPUBehavior2 .word Suite_PowerOnState .word Suite_PPUBehavior .word Suite_PPUTiming .word Suite_SpriteZeroHits .word Suite_PPUMisc .word Suite_AdvancedBGEval .word Suite_AdvancedSpriteEval EndTableTable: ; I'm not a huge fan of using macros in this ROM, since they make the ASM code look different than the compiled bytes, and thus harder to debug. ; This macro is just a series of bytes and words though, so it's not too hard to read. table .macro .byte \1 .byte \2 .word \3 .word \4 .endm ; This is just an absurd byte-saving measure where I substitute the word "absolute" for a single character in the ROM. tblf1 .macro .byte \1 .byte \2 .byte \3 .word \4 .word \5 .endm ; This is just an absurd byte-saving measure where I substitute the word "absolute" for a single character in the ROM, but then I need to add a ",X" afterwards. tblf2 .macro .byte \1 .byte \2 .byte \3 .byte \4 .word \5 .word \6 .endm str_Indirect = $F0 str_ZeroPage = $F1 str_Absolute = $F2 str_Immediate = $F3 str_UnofficialInstructions = $F4 TestPages: ; I just made this label for ease of searching. ;; CPU Behavior ;; Suite_CPUBehavior: .byte "CPU Behavior", $FF table "ROM is not writable", $FF, result_ROMnotWritable, TEST_ROMnotWritable table "RAM Mirroring", $FF, result_RAMMirror, TEST_RamMirroring table "PC Wraparound", $FF, result_ProgramCounter_Wraparound, Test_ProgramCounter_Wraparound table "The Decimal Flag", $FF, result_DecimalFlag, TEST_DecimalFlag table "The B Flag", $FF, result_BFlag, TEST_BFlag table "Dummy read cycles", $FF, result_DummyReads, TEST_DummyReads table "Dummy write cycles", $FF, result_DummyWrites, TEST_DummyWrites table "Open Bus", $FF, result_OpenBus, TEST_OpenBus table "All NOP instructions", $FF, result_AllNOPs, TEST_AllNOPs .byte $FF Suite_CPUInstructions: .byte "Addressing mode wraparound", $FF table "Absolute Indexed", $FF, result_AddrMode_AbsIndex, TEST_AddrMode_AbsIndex table "Zero Page Indexed", $FF, result_AddrMode_ZPgIndex, TEST_AddrMode_ZPgIndex table "Indirect", $FF, result_AddrMode_Indirect, TEST_AddrMode_Indirect table "Indirect, X", $FF, result_AddrMode_IndIndeX, TEST_AddrMode_IndIndeX table "Indirect, Y", $FF, result_AddrMode_IndIndeY, TEST_AddrMode_IndIndeY table "Relative", $FF, result_AddrMode_Relative, TEST_AddrMode_Relative .byte $FF ;; Unofficial Instructions: SLO ;; Suite_UnofficialOps_SLO: .byte str_UnofficialInstructions .byte "SLO", $FF tblf2 "$03 SLO", str_Indirect, ",X", $FF, result_UnOp_SLO_03, TEST_SLO_03 tblf1 "$07 SLO", str_ZeroPage, $FF, result_UnOp_SLO_07, TEST_SLO_07 tblf1 "$0F SLO", str_Absolute, $FF, result_UnOp_SLO_0F, TEST_SLO_0F tblf2 "$13 SLO", str_Indirect, ",Y", $FF, result_UnOp_SLO_13, TEST_SLO_13 tblf2 "$17 SLO", str_ZeroPage, ",X", $FF, result_UnOp_SLO_17, TEST_SLO_17 tblf2 "$1B SLO", str_Absolute, ",Y", $FF, result_UnOp_SLO_1B, TEST_SLO_1B tblf2 "$1F SLO", str_Absolute, ",X", $FF, result_UnOp_SLO_1F, TEST_SLO_1F .byte $FF ;; Unofficial Instructions: RLA ;; Suite_UnofficialOps_RLA: .byte str_UnofficialInstructions .byte "RLA", $FF tblf2 "$23 RLA", str_Indirect, ",X", $FF, result_UnOp_RLA_23, TEST_RLA_23 tblf1 "$27 RLA", str_ZeroPage, $FF, result_UnOp_RLA_27, TEST_RLA_27 tblf1 "$2F RLA", str_Absolute, $FF, result_UnOp_RLA_2F, TEST_RLA_2F tblf2 "$33 RLA", str_Indirect, ",Y", $FF, result_UnOp_RLA_33, TEST_RLA_33 tblf2 "$37 RLA", str_ZeroPage, ",X", $FF, result_UnOp_RLA_37, TEST_RLA_37 tblf2 "$3B RLA", str_Absolute, ",Y", $FF, result_UnOp_RLA_3B, TEST_RLA_3B tblf2 "$3F RLA", str_Absolute, ",X", $FF, result_UnOp_RLA_3F, TEST_RLA_3F .byte $FF ;; Unofficial Instructions: SRE ;; Suite_UnofficialOps_SRE: .byte str_UnofficialInstructions .byte "SRE", $FF tblf2 "$43 SRE", str_Indirect, ",X", $FF, result_UnOp_SRE_43, TEST_SRE_43 tblf1 "$47 SRE", str_ZeroPage, $FF, result_UnOp_SRE_47, TEST_SRE_47 tblf1 "$4F SRE", str_Absolute, $FF, result_UnOp_SRE_4F, TEST_SRE_4F tblf2 "$53 SRE", str_Indirect, ",Y", $FF, result_UnOp_SRE_53, TEST_SRE_53 tblf2 "$57 SRE", str_ZeroPage, ",X", $FF, result_UnOp_SRE_57, TEST_SRE_57 tblf2 "$5B SRE", str_Absolute, ",Y", $FF, result_UnOp_SRE_5B, TEST_SRE_5B tblf2 "$5F SRE", str_Absolute, ",X", $FF, result_UnOp_SRE_5F, TEST_SRE_5F .byte $FF ;; Unofficial Instructions: RRA ;; Suite_UnofficialOps_RRA: .byte str_UnofficialInstructions .byte "RRA", $FF tblf2 "$63 RRA", str_Indirect, ",X", $FF, result_UnOp_RRA_63, TEST_RRA_63 tblf1 "$67 RRA", str_ZeroPage, $FF, result_UnOp_RRA_67, TEST_RRA_67 tblf1 "$6F RRA", str_Absolute, $FF, result_UnOp_RRA_6F, TEST_RRA_6F tblf2 "$73 RRA", str_Indirect, ",Y", $FF, result_UnOp_RRA_73, TEST_RRA_73 tblf2 "$77 RRA", str_ZeroPage, ",X", $FF, result_UnOp_RRA_77, TEST_RRA_77 tblf2 "$7B RRA", str_Absolute, ",Y", $FF, result_UnOp_RRA_7B, TEST_RRA_7B tblf2 "$7F RRA", str_Absolute, ",X", $FF, result_UnOp_RRA_7F, TEST_RRA_7F .byte $FF ;; Unofficial Instructions: .AX ;; Suite_UnofficialOps__AX: .byte str_UnofficialInstructions .byte "*AX", $FF tblf2 "$83 SAX", str_Indirect, ",X", $FF, result_UnOp_SAX_83, TEST_SAX_83 tblf1 "$87 SAX", str_ZeroPage, $FF, result_UnOp_SAX_87, TEST_SAX_87 tblf1 "$8F SAX", str_Absolute, $FF, result_UnOp_SAX_8F, TEST_SAX_8F tblf2 "$97 SAX", str_ZeroPage, ",Y", $FF, result_UnOp_SAX_97, TEST_SAX_97 tblf2 "$A3 LAX", str_Indirect, ",X", $FF, result_UnOp_LAX_A3, TEST_LAX_A3 tblf1 "$A7 LAX", str_ZeroPage, $FF, result_UnOp_LAX_A7, TEST_LAX_A7 tblf1 "$AF LAX", str_Absolute, $FF, result_UnOp_LAX_AF, TEST_LAX_AF tblf2 "$B3 LAX", str_Indirect, ",Y", $FF, result_UnOp_LAX_B3, TEST_LAX_B3 tblf2 "$B7 LAX", str_ZeroPage, ",Y", $FF, result_UnOp_LAX_B7, TEST_LAX_B7 tblf2 "$BF LAX", str_Absolute, ",Y", $FF, result_UnOp_LAX_BF, TEST_LAX_BF .byte $FF ;; Unofficial Instructions: DCP ;; Suite_UnofficialOps_DCP: .byte str_UnofficialInstructions .byte "DCP", $FF tblf2 "$C3 DCP", str_Indirect, ",X", $FF, result_UnOp_DCP_C3, TEST_DCP_C3 tblf1 "$C7 DCP", str_ZeroPage, $FF, result_UnOp_DCP_C7, TEST_DCP_C7 tblf1 "$CF DCP", str_Absolute, $FF, result_UnOp_DCP_CF, TEST_DCP_CF tblf2 "$D3 DCP", str_Indirect, ",Y", $FF, result_UnOp_DCP_D3, TEST_DCP_D3 tblf2 "$D7 DCP", str_ZeroPage, ",X", $FF, result_UnOp_DCP_D7, TEST_DCP_D7 tblf2 "$DB DCP", str_Absolute, ",Y", $FF, result_UnOp_DCP_DB, TEST_DCP_DB tblf2 "$DF DCP", str_Absolute, ",X", $FF, result_UnOp_DCP_DF, TEST_DCP_DF .byte $FF ;; Unofficial Instructions: ISC ;; Suite_UnofficialOps_ISC: .byte str_UnofficialInstructions .byte "ISC", $FF tblf2 "$E3 ISC", str_Indirect, ",X", $FF, result_UnOp_ISC_E3, TEST_ISC_E3 tblf1 "$E7 ISC", str_ZeroPage, $FF, result_UnOp_ISC_E7, TEST_ISC_E7 tblf1 "$EF ISC", str_Absolute, $FF, result_UnOp_ISC_EF, TEST_ISC_EF tblf2 "$F3 ISC", str_Indirect, ",Y", $FF, result_UnOp_ISC_F3, TEST_ISC_F3 tblf2 "$F7 ISC", str_ZeroPage, ",X", $FF, result_UnOp_ISC_F7, TEST_ISC_F7 tblf2 "$FB ISC", str_Absolute, ",Y", $FF, result_UnOp_ISC_FB, TEST_ISC_FB tblf2 "$FF ISC", str_Absolute, ",X", $FF, result_UnOp_ISC_FF, TEST_ISC_FF .byte $FF ;; Unofficial Instructions: SH_ ;; Suite_UnofficialOps_SH_: .byte str_UnofficialInstructions .byte "SH*", $FF tblf2 "$93 SHA", str_Indirect, ",Y", $FF, result_UnOp_SHA_93, TEST_SHA_93 tblf2 "$9F SHA", str_Absolute, ",Y", $FF, result_UnOp_SHA_9F, TEST_SHA_9F tblf2 "$9B SHS", str_Absolute, ",Y", $FF, result_UnOp_SHS_9B, TEST_SHS_9B tblf2 "$9C SHY", str_Absolute, ",X", $FF, result_UnOp_SHY_9C, TEST_SHY_9C tblf2 "$9E SHX", str_Absolute, ",Y", $FF, result_UnOp_SHX_9E, TEST_SHX_9E tblf2 "$BB LAE", str_Absolute, ",Y", $FF, result_UnOp_LAE_BB, TEST_LAE_BB .byte $FF ;; Unofficial Instructions: The Immediate group ;; Suite_UnofficialOps_Immediates: .byte "Unofficial Immediates", $FF tblf1 "$0B ANC", str_Immediate, $FF, result_UnOp_ANC_0B, TEST_ANC_0B tblf1 "$2B ANC", str_Immediate, $FF, result_UnOp_ANC_2B, TEST_ANC_2B tblf1 "$4B ASR", str_Immediate, $FF, result_UnOp_ASR_4B, TEST_ASR_4B tblf1 "$6B ARR", str_Immediate, $FF, result_UnOp_ARR_6B, TEST_ARR_6B tblf1 "$8B ANE", str_Immediate, $FF, result_UnOp_ANE_8B, TEST_ANE_8B tblf1 "$AB LXA", str_Immediate, $FF, result_UnOp_LXA_AB, TEST_LXA_AB tblf1 "$CB AXS", str_Immediate, $FF, result_UnOp_AXS_CB, TEST_AXS_CB tblf1 "$EB SBC", str_Immediate, $FF, result_UnOp_SBC_EB, TEST_SBC_EB .byte $FF ;; CPU Interrupts ;; Suite_CPUInterrupts: .byte "CPU Interrupts", $FF table "Interrupt flag latency", $FF, result_IFlagLatency, TEST_IFlagLatency table "NMI Overlap BRK", $FF, result_NmiAndBrk, TEST_NmiAndBrk table "NMI Overlap IRQ", $FF, result_NmiAndIrq, TEST_NmiAndIrq .byte $FF ;; DMA Tests ;; Suite_DMATests: .byte "APU Registers and DMA tests", $FF table "DMA + Open Bus", $FF, result_DMA_Plus_OpenBus, TEST_DMA_Plus_OpenBus table "DMA + $2002 Read", $FF, result_DMA_Plus_2002R, TEST_DMA_Plus_2002R table "DMA + $2007 Read", $FF, result_DMA_Plus_2007R, TEST_DMA_Plus_2007R table "DMA + $2007 Write", $FF, result_DMA_Plus_2007W, TEST_DMA_Plus_2007W table "DMA + $4015 Read", $FF, result_DMA_Plus_4015R, TEST_DMA_Plus_4015R table "DMA + $4016 Read", $FF, result_DMA_Plus_4016R, TEST_DMA_Plus_4016R table "DMC DMA Bus Conflicts", $FF, result_DMABusConflict, TEST_DMABusConflict table "DMC DMA + OAM DMA", $FF, result_DMCDMAPlusOAMDMA, TEST_DMCDMAPlusOAMDMA table "Explicit DMA Abort", $FF, result_ExplicitDMAAbort, TEST_ExplicitDMAAbort table "Implicit DMA Abort", $FF, result_ImplicitDMAAbort, TEST_ImplicitDMAAbort .byte $FF ;; APU Tests ;; Suite_APUTiming: .byte "APU Tests", $FF table "Length Counter", $FF, result_APULengthCounter, TEST_APULengthCounter table "Length Table", $FF, result_APULengthTable, TEST_APULengthTable table "Frame Counter IRQ", $FF, result_FrameCounterIRQ, TEST_FrameCounterIRQ table "Frame Counter 4-step", $FF, result_FrameCounter4Step, TEST_FrameCounter4Step table "Frame Counter 5-step", $FF, result_FrameCounter5Step, TEST_FrameCounter5Step table "Delta Modulation Channel", $FF, result_DeltaModulationChannel, TEST_DeltaModulationChannel table "APU Register Activation", $FF, result_APURegActivation, TEST_APURegActivation table "Controller Strobing", $FF, result_ControllerStrobing, TEST_ControllerStrobing table "Controller Clocking", $FF, result_ControllerClocking, TEST_ControllerClocking .byte $FF ;; CPU Behavior 2 ;; Suite_CPUBehavior2: .byte "CPU Behavior 2", $FF table "Instruction Timing", $FF, result_InstructionTiming, TEST_InstructionTiming table "Implied Dummy Reads", $FF, result_ImpliedDummyRead, TEST_ImpliedDummyRead table "Branch Dummy Reads", $FF, result_BranchDummyRead, TEST_BranchDummyRead table "JSR Edge Cases", $FF, result_JSREdgeCases, TEST_JSREdgeCases table "Internal Data Bus", $FF, result_InternalDataBus, TEST_InternalDataBus table "DMA Landing on Write", $FF, result_DMALandingOnWrite, TEST_DMALandingOnWrite table "DMC Reload Timing", $FF, result_DMCReloadTiming, TEST_DMCReloadTiming table "DMC Playback Rate", $FF, result_DMCPlaybackRate, TEST_DMCPlaybackRate .byte $FF ;; Power On State ;; Suite_PowerOnState: .byte "Power On State", $FF table "PPU Reset Flag", $FF, result_DrawTest, TEST_PowerOnState_PPU_ResetFlag table "CPU RAM", $FF, result_DrawTest, TEST_PowerOnState_CPU_RAM table "CPU Registers", $FF, result_DrawTest, TEST_PowerOnState_CPU_Registers table "PPU RAM", $FF, result_DrawTest, TEST_PowerOnState_PPU_RAM table "Palette RAM", $FF, result_DrawTest, TEST_PowerOnState_PPU_Palette .byte $FF ;; PPU Behavior ;; Suite_PPUBehavior: .byte "PPU Behavior", $FF table "CHR ROM is not writable", $FF, result_CHRROMIsNotWritable, TEST_CHRROMIsNotWritable table "PPU Register Mirroring", $FF, result_PPURegMirror, TEST_PPURegMirroring table "PPU Register Open Bus", $FF, result_PPUIOOpenBus, TEST_PPU_IO_Open_Bus table "PPU Read Buffer", $FF, result_PPUReadBuffer, TEST_PPUReadBuffer table "Palette RAM Quirks", $FF, result_PaletteRAMQuirks, TEST_PaletteRAMQuirks .byte $FF ;; PPU VBL Timing ;; Suite_PPUTiming: .byte "PPU VBlank Timing", $FF table "VBlank beginning", $FF, result_VBlank_Beginning, TEST_VBlank_Beginning table "VBlank end", $FF, result_VBlank_End, TEST_VBlank_End table "NMI Control", $FF, result_NMI_Control, TEST_NMI_Control table "NMI Timing", $FF, result_NMI_Timing, TEST_NMI_Timing table "NMI Suppression", $FF, result_NMI_Suppression, TEST_NMI_Suppression table "NMI at VBlank end", $FF, result_NMI_VBL_End, TEST_NMI_VBL_End table "NMI disabled at VBlank", $FF, result_NMI_Disabled_VBL_Start, TEST_NMI_Disabled_VBL_Start .byte $FF ;; Sprite Zero Hits ;; Suite_SpriteZeroHits: .byte "Sprite Evaluation", $FF table "Sprite overflow behavior", $FF, result_SprOverflow_Behavior, TEST_SprOverflow_Behavior table "Sprite 0 Hit behavior", $FF, result_Sprite0Hit_Behavior, TEST_Sprite0Hit_Behavior table "$2002 flag timing", $FF, result_2002FlagClearTiming, TEST_2002FlagTiming table "Suddenly Resize Sprite", $FF, result_SuddenlyResizeSprite, TEST_SuddenlyResizeSprite table "Misaligned OAM DMA", $FF, result_MisalignedOAMDMA, TEST_MisalignedOAMDMA table "Arbitrary Sprite zero", $FF, result_ArbitrarySpriteZero, TEST_ArbitrarySpriteZero table "Misaligned OAM behavior", $FF, result_MisalignedOAM_Behavior, TEST_MisalignedOAM_Behavior table "OAM Corruption", $FF, result_OAM_Corruption, TEST_OAM_Corruption .byte $FF ;; PPU Misc ;; Suite_PPUMisc: .byte "PPU Misc.", $FF table "t Register Quirks", $FF, result_tRegisterQuirks, TEST_tRegisterQuirks table "Address $2004 behavior", $FF, result_Address2004_Behavior, TEST_Address2004_Behavior table "INC $4014", $FF, result_INC4014, TEST_INC4014 table "Rendering Flag Behavior", $FF, result_RenderingFlagBehavior, TEST_RenderingFlagBehavior table "$2007 read w/ rendering", $FF, result_Rendering2007Read, TEST_Rendering2007Read table "$2004 Stress Test", $FF, result_2004_Stress, TEST_2004_Stress table "$2007 Stress Test", $FF, result_2007_Stress, TEST_2007_Stress ;table "RMW $2007 Extra Write", $FF, result_RMW2007, TEST_RMW2007 ; Commented out for now. More research required. ;table "Palette Corruption", $FF, result_Unimplemented, DebugTest (I did not write a test for this, because it relies on a specific cpu/ppu clock alignment.) .byte $FF Suite_AdvancedBGEval: .byte "Advanced Background Evaluation", $FF table "Attributes As Tiles", $FF, result_AttributesAsTiles, TEST_AttributesAsTiles table "Stale BG Shift Registers", $FF, result_StaleBGShiftRegisters, TEST_StaleBGShiftRegisters table "BG Serial In", $FF, result_BGSerialIn, TEST_BGSerialIn table "ALE + Read", $FF, result_ALERead, TEST_ALERead table "Hybrid Addresses", $FF, result_HybridAddresses, TEST_HybridAddresses .byte $FF Suite_AdvancedSpriteEval: .byte "Advanced Sprite Evaluation", $FF table "Sprites On Scanline 0", $FF, result_Scanline0Sprites, TEST_Scanline0Sprites table "Stale Sprite Shift Regs", $FF, result_StaleSpriteShiftRegs, TEST_StaleSpriteShiftRegs table "Frozen OAM2 Increment", $FF, result_FrozenOAM2Inc, TEST_FrozenOAM2Inc table "Misaligned OAM2 Address", $FF, result_MisalignedOAM2Addr, TEST_MisalignedOAM2Addr .byte $FF ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; MORE ENGINE STUFF ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; I ran out of space in the engine section from $F000 - $FFFF, so I put some extra stuff here. AutomaticallyRunEveryTestInROM: ; This function is used to run every test in the ROM automatically. LDA #1 STA = $80 then we're reading from $600, X. BMI TEST_2007StressTest_Eval600 LDA $500, Y ; Read from the resulting data. JMP TEST_2007StressTest_EvalMerge ; jump ahead TEST_2007StressTest_Eval600: LDA $600, Y ; Read from the resulting data. TEST_2007StressTest_EvalMerge: CMP TEST_2007StressTest_Key, X ; Compare with the answer key. BNE TEST_2007StressTest_Fail ; If it's not a match, fail the test. INX ; Increment X for the next read from the answer key. TEST_2007StressTest_EvalSkip1: INY ; Increment Y for the next loop. CPX #$AA ; If X = $AA then we read all the bytes. BNE TEST_2007StressTest_EvalLoop1 ; Loop until X = $AA ;; END OF TEST ;; LDA #1 RTS ;;;;;;; TEST_2007StressTest_Fail: JMP TEST_Fail TEST_2007StressTest_TimingLoop: LDA #$18 ; 28004 STA $2006; 28008 LDA #$00 ; 28010 STA $2006; 28014 ; disable rendering LDA #0 ; 28016 STA $2001; 28018 JSR ClockslideFromWord ; .word 1779-12 RTS ;;;;;;; FAIL_InternalDataBus1: JMP TEST_Fail TEST_StaleSpriteShiftRegs: ;;; Test 1 [Stale Sprite Shift Registers]: Verify sprite zero hits are working. ;;; JSR VerifySpriteZeroHits ; We already have a subroutine to verify sprite zero hits are working properly. BEQ FAIL_InternalDataBus1 ; If they aren't fail the test. INC > 1 = $66 + Carry set ; ROR <$66 ; $22 >> 1 = $91 ; STA ($91),Y ; A=60 -> ($15, $40) ; ; How could this one go wrong? Well, if writing to $4015 does not update the data bus, this will run RTI instead of RTS. ; We can prep for this by pushing $A0 to the stack, so if an RTI occurs, we'll return to TEST_OpenBusA0A0 LDX #0 LDY #0 LDA #$CD STA <$56 ; $56 = #$CD LDA #$22 STA <$66 ; $66 = #$22 LDA <$91 ; Push these to the stack PHA LDA <$92 ; These are important bytes for the test selection menu, so we'll restore these after the test. PHA LDA #$15 STA <$91 LDA #$40 STA <$92 ; ($91) = $4015 LDA #$A0 PHA LDA #$60 ; A = the value of RTS JSR $5600 ; Jump to open bus to run this! ; and if we made it back here, the test worked! ; restore the values from $91 and $92 PLA ; pull off the $A0 for the RTI guardrails. TEST_OpenBus_PostTest8: PLA STA <$92 PLA STA <$91 TXA ; X = 0 if we ran RTS, X = 1 if we ran RTI. CPX #$01 BEQ TEST_Fail2 INC =5, the read cycle of the LDX instruction will be after VBlank begins. X=$80, (the VBlank flag is cleared, so...) Y=$00. ; -(The bits get rearranged, and this is stored at $50 as "01") ; Put the X register into bit 1, and the Y register into bit 2. TXA ASL A LDA #0 ROL A STA <$00 TYA ASL A LDA #0 ROL A ASL A ORA <$00 ; A should now be 0000 00XY, where X and Y are bit 7 of X and Y. STA <$00 PLA TAX LDA <$00 STA <$50,X INX CPX #$07 BNE TEST_VBlank_Beginning_Loop ; Address $50 should now look exactly like TEST_VBlank_Beginning_Expected_Results LDX #0 TEST_VBlank_Beginning_Loop2: LDA <$50,X CMP TEST_VBlank_Beginning_Expected_Results,X BNE TEST_Fail9 INX CPX #$03 ; since byte 2 in this list could depend on CPU/PPU clock alignment... BNE TEST_VBlank_Beginning_Loop2_SkipByte2 ; let's ignore it. INX TEST_VBlank_Beginning_Loop2_SkipByte2: CPX #$07 BNE TEST_VBlank_Beginning_Loop2 ;; END OF TEST ;; LDA #1 RTS ;;;;;;; TEST_Fail9: JSR EnableRendering_BG JMP TEST_Fail TEST_VBlank_Beginning_Expected_Results: ; $00 is also acceptable in the fourth byte (byte 3), depending on CPU/PPU clock alignment. .byte $02, $02, $02, $02, $00, $01, $01 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; TEST_VBlank_End: ;;; Test 1 [VBLank End]: Tests the timing of the $2002 VBlank flag ;;; ; Special thanks to blargg for figuring this stuff out. JSR DisableRendering LDX #0 TEST_VBlank_End_Loop: TXA JSR VblSync_Plus_A ; This next CPU cycle is synced with PPU cycle 1+A for this frame. ; VBlank ends in about 2273.333 CPU cycles. ; So let's stall for 2273-4 cycles, as this upcoming LDA takes 4 cycles. JSR ClockslideFromWord .word 2269 LDA $2002 ; Here's how this test works. ; When A=0, the LDA instruction occurs before VBlank ends. ; Every iteration of this loop, this test will run 1 PPU cycle closer to the end of VBlank than the previous iteration. ; Eventually, the VBlank flag is no longer set when the LDA instruction reads from $2002. (when A>=4) ; The bits are rearranged so the VBlank flag gets stored in bit 0, and this value is written to $50,X ASL A ; Shift VBlank flag into carry LDA #0 ; clear A ROL A ; Rotate carry into bit 0. STA <$50,X ; store in $50,X INX CPX #$07 BNE TEST_VBlank_End_Loop ; loop until X=7 ; Address $50 should now look exactly like TEST_VBlank_Beginning_Expected_Results LDX #0 TEST_VBlank_End_Loop2: LDA <$50,X CMP TEST_VBlank_End_Expected_Results,X BNE TEST_Fail9 INX CPX #$07 BNE TEST_VBlank_End_Loop2 ;; END OF TEST ;; LDA #1 RTS ;;;;;;; TEST_VBlank_End_Expected_Results: .byte $01, $01, $01, $01, $00, $00, $00 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; FAIL_NMI_Control1: JSR DisableNMI JMP TEST_Fail TEST_NMI_Control: ; Special thanks to blargg. I'm pretty much just doing what they did here. LDA #$E8 ; INX opcode STA $700 LDA #$40 ; RTI opcode STA $701 ;;; Test 1 [NMI Control]: The NMI should not occur when disabled. ;;; ; The NMI *should* already be disabled (and it being enabled during the "WaitForVBlank" that happened before the jump here would be problematic...) ; but let's test this anyway. LDX #0 JSR Clockslide_29780 ; Wait 1 frame. CPX #0 BNE FAIL_NMI_Control1 ; If the NMI occurs, it will run INX, ... RTI. That would increment X to 1, thus failing the test. INC 0, X<8) the Sprite Zero Hit occurs. ;;; JSR WaitForVBlank JSR InitializeSpriteZero ; YPos, CHR, Att, XPos .byte $00, $FC, $00, $01 ; Xpos = 1 STX $4014 ; OAM DMA (we want to keep OAM refreshed for these tests) JSR Clockslide_3000 ; Wait long enough for VBlank to be over, and a few scanlines to render. (Sprite Zero hit should occur, for some sprite zero is visible.) LDA $2002 ; Bit 6 should be set, since the sprite zero hit should have occurred. AND #$40 BEQ FAIL_Sprite0Hit_Behavior INC =239. ;;; JSR WaitForVBlank JSR InitializeSpriteZero ; YPos, CHR, Att, XPos .byte 239, $FC, $00, $08 JSR WaitForVBlank STX $4014 ; OAM DMA (we want to keep OAM refreshed for these tests) JSR Clockslide_29780 ; Wait an entire frame, since this sprite is at the bottom of the screen. (Sprite Zero hit should NOT occur.) JSR Clockslide_500 ; wait a few more scanlines just to be sure. LDA $2002 ; Bit 6 should NOT be set, since the sprite zero hit should not have occurred. AND #$40 BNE FAIL_Sprite0Hit_Behavior2 INC = 1 && PPU cycle <= 64: clear Secondary-OAM. ; PPU cycle >= 65 && PPU cycle <= 256: Sprite evaluation ; PPU cycle >= 257 && PPU cycle <= 320: Shift register initialization. (Every one of these cycles also clears PPUOAMAddress to 0.) ; ; Keep in mind, on scanline n, we're processing the OAM data in preparation for scanline n+1. ; (So the sprites drawn on, for example, scanline 6 were evaluated during the rendering of scanline 5.) ; ; Let's take a deep look into how Sprite Evaluation works on any given scanline. (PPU cycle >= 65 && PPU cycle <= 256) ; for odd ppu cycles: read index "PPUOAMAddress" of OAM. Let's call the value read "S" ; - "PPUOAMAddress" is the value set by writing to $2003. ; - for our example here, assume PPUOAMAddress is zero, reset during the shift register initialization from the previous scanline. ; for even cycles: Evaluate "S" and determine if this sprite should be drawn on the next scanline. ; - Depending on how the evaluation goes, modify PPUOAMAddress. Typically increment by 1 or 4. (actually "it's complicated", but that's for a different test.) ; Let's focus on the even cycles. ; First of all, if Secondary-OAM is full, the behavior is different. Let's focus on when Secondary-OAM is not full. ; STEP 1: Check that the Y position of this object is in range of this scanline. ; - The value checked here is "S", which was read in the previous PPU cycle. In this example, "S" should be index 0 of OAM, since PPUOAMAddress was cleared in the previous scanline. ; - If the current scanline number-"S" is positive, and less than 8 (or 16 if the sprites are using the 8 by 16 mode) then this object is in range for this scanline. ; - That previous sentence was just a verbose way of calculating "yes, this object should be rendered on the next scanline". ; - Now, if Secondary-OAM is not full (in this example it is still empty, so yeah- it's not full) we know this object will be in the next scanline, so add it to Secondary-OAM. ; - In addition to being added to Secondary-OAM, if this is PPU cycle 66 of a scanline, it is assumed that we are processing sprite zero, so raise a flag indicating sprite zero exists on the next scanline. ; STEP 2 to 4: Read "S" as the CHR data, attributes, and X position respectively. (each of these steps happen 2 ppu cycles after the previous step, since we need to read "S" from OAM again) ; - There's actually some wild stuff going on with the X position, but that's for a different test. ; ; So to recap, read the Y position, and on the following cycle, see if it's in range. If it is, and this is PPU cycle 66 of a given scanline, raise a flag indicating sprite zero exists on the next scanline. ; - this "flag" essentially says, Secondary-OAM index 0 is "Sprite Zero", as in, a Sprite Zero hit will occur if a "solid pixel" of "sprite zero" overlaps a "solid pixel" of the background. ; - which is, again, a really verbose way of saying "run a check for a sprite zero hit next scanline" if the value of "S" on ppu cycle 66 was in range for this scanline. ; ; Duh. If you've implemented sprite zero hits, you should be following along. ; Perhaps that "flag" isn't the exact way your emulator checks if a sprite is "sprite zero", but it will make sense in a moment why I'm phrasing it this way. ; ; What happens if you write to $2003 after the "Shift register initialization" and before "Sprite evaluation"? ; Well, PPUOAMAddress won't be $00 when sprite evaluation begins. ; So the first sprite processed on cycle 66 won't necessarily be index zero of OAM. ; But if "S" is in range of the scanline, and it's cycle 66, then the next scanline will consider Secondary-OAM index 0 as "sprite zero", even if it isn't OAM index 0. ; ; In other words, it *is* possible for an object that isn't OAM index 0 to trigger a sprite 0 hit! ; On a completely unrelated topic, writing to $2003 is all sorts of jank, and I need my test here to prevent that "jankiness" from ruining the results. ; This behavior appears to only happen on CPU/PPU clock alignment 3: ; writing to $2003 can copy 8 bytes of OAM from $20 to $27, and paste these values at: ; The old PPUOAMAddress & $F8 ; The new PPUOAMAddress & $F8 ; So we also want to the 8 values starting at $20 to match the 8 values we want at the new PPUOAMAddress ; Since this test is a doozy, I will comment every line and explain why I'm doing this. JSR ClearPage2 ; Let's clear page 2. I'm using page 2 for the OAM DMA, so OAM will be a copy of $200 - $2FF JSR WaitForVBlank ; Wait for VBlank. I'm going to disable rendering next, and I'd prefer if I waited for VBlank to do that. JSR DisableRendering ; Rendering is now disabled. Rendering needs to be disabled for the upcoming VblSync_Plus_A subroutine to work properly. LDX #32 ; Let's initialize Sprite 32 at screen coordinates ($08, $00) JSR InitializeSpriteX ; This subroutine reads the following 4 bytes, and adjusts the return address accordingly, so the following 4 bytes are not executed. .byte $00, $FC, $00, $08 ; Y Position, Pattern Table Index, Attributes, X position LDX #8 ; Let's also initialize Sprite 8 with the same values, so the $2003 corruption doesn't break anything. JSR InitializeSpriteX ; This subroutine reads the following 4 bytes, and adjusts the return address accordingly, so the following 4 bytes are not executed. .byte $00, $FC, $00, $08 ; Y Position, Pattern Table Index, Attributes, X position LDA #0 ; A=0, since this next subroutine syncs to PPU cycle A of VBlank, and I want to sync to cycle 0. JSR VblSync_Plus_A ; Sync the next CPU cycle to PPU cycle 0 of VBlank. (cycle 1 of scanline 241) ; The CPU is now at PPU cycle 0 of VBlank. ; Let's calculate how many CPU cycles remain until scanline 0 is being rendered. ; We're on dot 1 of scanline 241. The final dot before scanline 0 is dot 341 of scanline 261 ; There are 341 PPU cycles per scanline ; (341 * 21)-1 = 7160 PPU cycles until dot 0 of scanline 0. ; 3 PPU cycles per 1 CPU cycle. 7160/3 = 2386.66 CPU cycles. ; So let's count CPU cycles. We have 2386 cycles until dot 0, which is when we want to write to $2003 to update PPUOAMAddress LDA #02 ; (+2 CPU cycles) A = 2, so the OAM DMA will use page 2 STA $4014 ; (+518 CPU cycles) Run the OAM DMA with page 2. LDA #32*4 ; (+2 CPU cycles) Load A with 32*4 (128, or $80) which is the OAM address for the object we initialized. JSR EnableRendering ; (+30 CPU cycles) Enable rendering of both the background and sprites, so the sprite zero hit can occur. ; After setting up sprite 8 and running the OAM DMA, we have 1596 CPU cycles remaining before cycle 0 of scanline 0. JSR ClockslideFromWord .word 1830 ; (+1830 CPU cycles) This function just stalls for 1830 CPU cycles, so we should be slightly after cycle 0 of scanline 0. STA $2003 ; Store A ($80) at PPUOAMAddress. (and probably copy 8 instances of $FF from OAM[$00] to OAM[$20], which won't break anything) ; Now, the sprite evaluation will occur with sprite 8 getting processed first. ; Since this object is the first one processed, PPU cycle 66 will check if it is in range of the current scanline. ; and if it is (it is), it will be treated as sprite zero for the purposes of triggering a sprite zero hit, despite being sprite 8. JSR Clockslide_500 ; Wait a few scanline for this entire sprite to be drawn LDA $2002 ; Read PPUSTATUS AND #$40 ; mask away every bit except the Sprite Zero Hit flag. BEQ FAIL_ArbitrarySpriteZero; If bit 6 was zero, the sprite zero hit did not occur, thus failing the test. JSR Clockslide_29780 ; Let's wait an entire frame and check again to weed out potential false positives. LDA $2002 ; Read PPUSTATUS AND #$40 ; mask away every bit except the Sprite Zero Hit flag. BNE FAIL_ArbitrarySpriteZero; If bit 6 was non-zero, the sprite zero hit did occur, thus failing the test. INC > 2) + 20 TYA PHA TXA CLC ADC #$03 LSR A LSR A CLC ADC #$20 STA $2006 ; Low = VRegisterByXIndexLowLUT[X&3] TXA AND #$03 TAY LDA VRegisterByXIndexLowLUT,Y STA $2006 PLA TAY RTS ;;;;;;; ReadPPUADDRFromWord: ; Takes the two bytes after the JSR instruction and stores them in $2006. Then reads $2007 twice. STY <$FE JSR CopyReturnAddressToByte0 LDA $2002 LDY #0 LDA [$0000],Y STA $2006 INY LDA [$0000],Y STA $2006 INY JSR FixRTS LDY <$FE LDA $2007 LDA $2007 RTS ;;;;;;; SetPPUADDRFromWord: ; pretty much the same as ReadPPUADDRFromWord, but it doesn't run LDA $2007 twice at the end. STA <$FF STY <$FE JSR CopyReturnAddressToByte0 ;LDA $2002 ; Good practices would dictate that I read from $2002 here to reset the w register. ; However, there are rare situations where I wish to use this subroutine without clearing the vblank flag. LDY #0 LDA [$0000],Y STA $2006 INY LDA [$0000],Y STA $2006 INY JSR FixRTS LDY <$FE LDA <$FF RTS ;;;;;;; WriteToPPUADDRWithByte: ; Sets up v then writes n to it, where n is the third byte after the JSR STA <$FF STY <$FE JSR CopyReturnAddressToByte0 LDA $2002 LDY #0 WriteToPPUADDRWithByteLoop: LDA [$0000],Y CMP #$FF BEQ WriteToPPUADDRWithByteExit STA $2006 INY LDA [$0000],Y STA $2006 INY LDA [$0000],Y STA $2007 INY JMP WriteToPPUADDRWithByteLoop WriteToPPUADDRWithByteExit: INY JSR FixRTS LDY <$FE LDA <$FF RTS ;;;;;;; DoubleLDA2007: ; There are a few tests that need to read the contents of a PPU address. LDA $2007 ; and instead of actually writing out LDA $2007 twice (6 bytes) LDA $2007 ; you can just jump here instead. (3 bytes) RTS ;;;;;;; SetPPUReadBufferToA: ; Sets the value of the PPU Read buffer to A. PHA JSR SetPPUADDRFromWord .byte $2C, $00 STA $2007 JSR SetPPUADDRFromWord .byte $2C, $00 LDA $2007 PLA RTS ;;;;;;; PrepNMI_TimingTests: ; This is re-used in a handful of NMI timing tests. LDA #$C8 ; INY opcode STA $700 LDA #$40 ; RTI opcode STA $701 JSR DisableRendering LDX #0 RTS ;;;;;;; VRegisterByXIndexLowLUT: ; a look up table used in GetVRegisterByXIndexForMenu .byte $E1, $21, $61, $A1 AttributeNybbles: ; Attribute nybbles used in UpdateTESTAttributes .byte $F0, $0F AttributeNybblesInverse: ; Attribute nybbles used in UpdateTESTAttributes .byte $0F, $F0 AttributePaletteNybbles: ; Attribute nybbles used in UpdateTESTAttributes .byte $00, $55, $AA, $FF TestPassFailBlend: ; These are used in DrawTEST. index 0 of each of these spells "TEST". index 1 spells "PASS" and so on. .byte "TPF.DS" .byte "EAA.RK" .byte "SSI.AI" .byte "TSL.WP" NMI_Routine: ; This is the NMI routine for the main menu. JSR ReadController1 JSR MaskDpadConflicts LDA 50 cycles after this RTS, a DMA will occur. DMASync_50MinusACyclesRemaining: ; Sync the CPU and the DMA, such that the DMA runs exactly 50-A CPU cycles after the RTS instruction ends. JSR DMASync ; the DMA is in 400 cycles; JSR ClockslideFromWord .word 311 LDA 92 JSR Clockslide37_Plus_A ; 92 -> (56-A) RTS ; (56-A) -> (50-A) cycles after this RTS, a DMA will occur. ;;;;;;; ; A giant list of ClockSlides! ; "What's a clockslide?" ; It's just a subroutine that wastes a precise amount of CPU cycles. ; If you want to waste exactly n cycles, run JSR Clockslide_n ; (Clockslide_14 through Clockslide_50 are defined, and most larger clockslides are a combination of JSRs to those clockslides) Clockslide_100Minus12: ; This is very handy for the following clockslides I want to make. 100, 200, etc. JSR Clockslide_26 ;=32 JSR Clockslide_50 ;=80 RTS ;=100-12. Remember, JSR and RTS add 12 cycles, so to make clockslide 100, I just need to JSR somewhere with JSR Clockslide_100Minus12 ;;;;;;; Clockslide_50000: JSR Clockslide_10000 Clockslide_40000: JSR Clockslide_10000 Clockslide_30000: JSR Clockslide_10000 Clockslide_20000: JSR Clockslide_10000 Clockslide_10000: JSR Clockslide_1000 Clockslide_9000: JSR Clockslide_1000 Clockslide_8000: JSR Clockslide_1000 Clockslide_7000: JSR Clockslide_1000 Clockslide_6000: JSR Clockslide_1000 Clockslide_5000: JSR Clockslide_1000 Clockslide_4000: JSR Clockslide_1000 Clockslide_3000: JSR Clockslide_1000 Clockslide_2000: JSR Clockslide_1000 Clockslide_1000: JSR Clockslide_100 Clockslide_900: JSR Clockslide_100 Clockslide_800: JSR Clockslide_100 Clockslide_700: JSR Clockslide_100 Clockslide_600: JSR Clockslide_100 Clockslide_500: JSR Clockslide_100 Clockslide_400: JSR Clockslide_100 Clockslide_300: JSR Clockslide_100 Clockslide_200: JSR Clockslide_100 Clockslide_100: JSR Clockslide_100Minus12 ; Since JSR and RTS take 12 cycles, let's stall for exactly 100-12 cycles. RTS ;;;;;;; ;A frame has about 29780 cycles, so let's make a few around that number. ; I use these clockslides enough that it actually saves bytes to do this. ; NOTE: Don't optimize these using ClockslideFromWord. ; Some tests rely on an interrupt occurring during the clockslide and changing some registers, ; while ClockslideFromWord restores the registers at the end. Clockslide_29750: JSR Clockslide_100Minus12 JSR Clockslide_50 ;150 JSR Clockslide_600 ;750 JSR Clockslide_9000 ;9750 JSR Clockslide_20000;29750 RTS ;;;;;;; Clockslide_29780: JSR Clockslide_29750; 29762 JSR Clockslide_18 ; 29780 RTS ;;;;;;; Clockslide_4320: JSR ClockslideFromWord .word 4320-12 RTS ;;;;;;; Clockslide37_Plus_A:;+6 STA <$00 ; +3 LDA #$FF ; +2 STA <$01 ; +3 LDA #36 ; +2 SEC ; +2 SBC <$00 ; +3 STA <$00 ; +3 JMP [$0000] ; 5 + A + 6 ;;;;;;;;;;;;;;;;; Clockslide64_Minus_A:;+6 STA <$00 ; +3 LDA #$FF ; +2 STA <$01 ; +3 JMP [$0000] ; +50 - A ;;;;;;;;;;;;;;;;; WaitForVBLSpriteZeroHit: JSR WaitForVBlank ; Wait for vblank LDA $2002 ; Read PPUSTATUS AND #$40 ; Mask away everything except the sprite zero hit flag. RTS ; RTS, and run a BEQ or BNE afterwards. ;;;;;;; VerifySpriteZeroHits: ; Verify that sprite zero hits work in this emulator, and don't provide false-positives just because sprite zero is on this scanline, and parts of sprite zero overlap visible pixels. ; STEP ONE: Intentionally miss a sprite zero hit. JSR DisableRendering ; Disable rendering so the following can happen even out of vblank. JSR ClearNametable2_With24 ; Clear nametable 2 with tile $24 (empty tiles) JSR ClearPage2 ; Clear Page 2 with all $FFs JSR SetUpSpriteZero ; Prepare sprite zero with the following values: .byte $00, $C0, $03, $03 ; Single dot on scanline 4, X = 08 JSR PrintCHR ; Update nametable .word $2C00 ; And 8x8 box with a single pixel hole in it. (we're intentionally missing this sprite zero hit.) .byte $E1, $FF ; This will trigger the sprite zero hit. JSR OAMDMAWithPage2 ; OAM DMA with page 2 JSR WaitForVBlank ; Wait for vblank JSR EnableRendering ; Draw both the background and sprites. JSR WaitForVBLSpriteZeroHit; Wait for vblank and load A with $2002.6 BNE VerifySpriteZeroHits_F ; Fail the test if the sprite zero hit occurred. JSR PrintCHR ; Update nametable .word $2C00 ; Single dot to overlap the sprite. (we're intentionally hitting this one) .byte $E2, $FF ; This will trigger the sprite zero hit. JSR OAMDMAWithPage2 ; OAM DMA with page 2 JSR WaitForVBlank ; Wait for vblank JSR EnableRendering ; Draw both the background and sprites. JSR WaitForVBLSpriteZeroHit; Wait for vblank and load A with $2002.6 RTS ; and return. ;;;;;;; VerifySpriteZeroHits_F: LDA #0 RTS ;;;;;;; FAIL_MisalignedOAMDMA: JMP TEST_Fail TEST_MisalignedOAMDMA_OAM: .byte $07, $C0, $00, $80 TEST_MisalignedOAMDMA: ;;; Test 1 [Misaligned OAM DMA]: Verify sprite zero hits before running the actual test. ;;; JSR VerifySpriteZeroHits ; Use this subroutine to verify if sprite zero hits are working. BEQ FAIL_MisalignedOAMDMA; And if they aren't, fail the test. INC 406 ; the next DMA is at (432) cycles, so we have 406 cycles to go. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; VerifyReturnAddressesAreCorrect: TSX INX LDA $100, X ; $02 is the correct value to read here. A value of $03 is wrong. $04 is right out. ; Anyway, subtract by 2. SEC SBC #02 STA