by grishka · 3y ago · 0 comments
While most of the games I tested are playable, I couldn't get SBC and ADC instructions to work correctly for all cases. I especially have no idea whatsoever how they're supposed to affect the carry flags. Test ROMs aren't very helpful either because the ones I used just ran the instruction with all possible combinations of input parameters, checksummed the results, compared the checksum to the expected value, and said "duh, your thing is wrong". And Googling around wasn't any more helpful. Since most of my childhood games ran fine anyway, I gave up. I'd still like to fix it eventually though.
That said, writing a video game system emulator is a tedious but rewarding experience. You just move bytes around and do an unhealthy amount of bit operations, and a game comes out. IMO every software developer should try writing an emulator at some point.
SBC:
result = byte1 - byte2 - carry
carry = result < 0
return result & 0xff
ADC:
result = byte1 + byte2 + carry
carry = result > 0xff
return result & 0xff
Source - my implementation, passes tests. Look for SBC & ADC in
https://github.com/trekawek/coffee-gb/blob/master/src/main/j...ZEXALL and derivatives are pretty good test suites. I think it checksums results but it does have lots of tiny tests to exercise particular instructions and operands. You'll still need to single-step through all the code that runs the generated test suite and prints the results. I know there's a port to a Sega Master System rom (ZEXSMS), dunno about the Gameboy.