VOGONS


First post, by DrH

User metadata
Rank Newbie
Rank
Newbie

Happy new year all,

I'm developing a Chip-8 emulator just to fun and learning. This is my first one, so I'm learning a lot in the process.
Recently I was able to run the first test rom [1] where the author says that we should run it for 39 cycles otherwise we would get into an infinite loop.
My question is, how do we know we finished the execution of a rom? Checking the Chip-8 opcode table [2] it seems that there is not "halt" like opcode.

I'm looking forward to hearing your thoughts on this.

Thank you.

[1] https://github.com/Timendus/chip8-test-suite? … 8-splash-screen
[2] https://en.wikipedia.org/wiki/CHIP-8#Opcode_table

Reply 1 of 6, by Deunan

User metadata
Rank Oldbie
Rank
Oldbie

So first of all that infinite loop is expected behaviour. It means your emulator is correctly calculating and executing a jump. As for your question, the answer is you don't know. Either you have some metadata about the ROM (that is, the size of the code in that particular ROM) or you detect invalid opcodes if it so happens that empty ROM cells end up being interpreted as one. Or, rather than metadata, you could accept any ROM sizes and simply figure out the end of code based on file size.

Of course ROM file size method is not going to work for programs that end with infinite loop. On the other hand you could also detect just that - any jump to current address is sort of a HALT. Not a true one, the CPU is still fetching the opcode but for the program execution the result is the same.

Reply 2 of 6, by DrH

User metadata
Rank Newbie
Rank
Newbie

Thanks @Deunan for the reply.

After posting I did some more search and I confirm what you said that we don't know when it ends.
One interesting solution that I found is to check PC regularly, and if it points to the same location more than X (5, 10,...) times, it can be considered as an infinite loop state.

Reply 3 of 6, by Deunan

User metadata
Rank Oldbie
Rank
Oldbie
DrH wrote on 2025-01-03, 17:40:

One interesting solution that I found is to check PC regularly, and if it points to the same location more than X (5, 10,...) times, it can be considered as an infinite loop state.

The problem with this approach is it can be triggered by a long delay loop (say, to show a splash screen) if you happen to sample PC at the wrong times. It's less likely on architectures that require more than 1 instruction for decrementing a register and conditional jump (and even less likely with merely 8-bit registers) but not impossible. Also the sampling and detection will take some time, the more the less false positives but also longer delay. IMO it's better to detect a jump to current address while decoding the jump instruction. If jump target == PC then HALT, that's how I would do it.

Reply 5 of 6, by DrH

User metadata
Rank Newbie
Rank
Newbie

In case other people search for the same info, I just found something that might clarify: https://www.reddit.com/r/EmuDev/comments/k7a6 … _exitterminate/
Indeed, Chip-8 doesn't have a halt or exit execution. However, Super Chip-8 and other variants do have it through the op code 0x00FD.

Reply 6 of 6, by nocash

User metadata
Rank Newbie
Rank
Newbie

A halt opcode - on cpus that do support it - is causing the cpu to wait until an interrupt occurs (often reducing the cpu power consumtion during the wait).
That said, halt has nothing to do with exiting or terminating the program.