gianlucag / mos6502

A fast & simple MOS 6502 CPU emulator written in C++
MIT License
286 stars 56 forks source link

Fix cycle count integer overflow #7

Closed MacDue closed 5 years ago

MacDue commented 5 years ago

This PR removes the cycles member from the mos6502 class and replaces it's use in mos6502::Run with a simple decrement of the parameter n.

This is for the simple reason that cycles overflows pretty quickly (a few mins) causing the emulation to halt (example below).

uint32_t start = cycles; // cycles near 2^32 (say 2^32 -n +1)
while(start + n /* overflow! start + n is now less than cycles*/ > cycles && !illegalOpcode)
/* no code executed*/

The fix is:

while(n-- > 0 && !illegalOpcode)

Which does the same as before with no risk of overflow.

P.s. There's also a bunch of white-space corrections thrown in for free by my editor.

gianlucag commented 5 years ago

very good. thank you!