Timendus / chip8-test-suite

A collection of ROM images with tests that will aid you in developing your own CHIP-8, SUPER-CHIP or XO-CHIP interpreter (or "emulator")
GNU General Public License v3.0
362 stars 7 forks source link

Test for timer range #13

Closed ivan-pi closed 1 year ago

ivan-pi commented 1 year ago

First of all, thank you very much for these incredible tests!

I ran into the following issue with my CHIP-8 implementation. The timers were accidentally signed integers, meaning their range was from -128 to 127.

int8_t delay_timer;

    // DFX15:
    delay_timer = V[x]:
    pc += 2;

Since the timers were only decremented when delay_timer > 0, my interpreter would freeze any time V[x] contained a value larger than 127. (This happened for instance with the the 5-quirks.ch8 test.)

I'm not sure what would be the right way to catch this?

ivan-pi commented 1 year ago

I guess the signed int could also work (assuming wraparound behaviour), as long as the comparison is performed correctly:

if ((uint8_t) delay_timer > 0U) 
    delay_timer--;

I suppose the timer could be represented with anything really, but it needs to be able to perform up to 2^8 = 256 ticks before it stops changing.

Timendus commented 1 year ago

Yes, interesting issue. And you're welcome! 😄

I'm not sure if this is a class of problem that many people struggle with. I haven't seen it happen often. I'm also not entirely sure how we would test this in a friendly way.

Do you have any suggestions?