Skip to content →

testing our cpu

After we have successfully implemented the CPU, it’s time to test it, to make sure, it is running as it is supposed to.

For this, there are ROMs out there, that will test each individual opcode for correct behavior. I went with the popular ROMs from blargg. The ROMs are mirrored here for example.

We want to use the 'cpu_instrs' ROMS.
Make sure, for now, to use the individual ROMs, as the main makes use of memory banking (MBC), something that we will implement later, and can not use for now.

If you are wondering, how to get any output from the test ROMs, without any PPU, which could display graphics, there is a solution to that. Blargg was kind enough, to write the characters to the game link port address, so we can pull the results and print them ourselves.

//    blarggs test - serial output
if (memory[0xff02] == 0x81) {
    char c = memory[0xff01];
    printf("%c", c);
    memory[0xff02] = 0x0;

When successfully completing a test, it will print “successful” to the console, otherwise it will print out some additional information about the error on hand.

Often, errors in instructions may be because of setting the flags wrong, so let’s clarify the flags for a moment:

Z - (zero) is set, if the current operation results in a zero
N - (subtract) is set, if the current operation is a subtraction
H - (half-carry) is set, if there is a carry from the low nibble to the high nibble
C - (carry) is set, if there is a carry 

There are special cases, depending on some opcodes. For example, the Carry and the Half-Carry flag also function as (Half-)Borrow flags, for subtraction operations.

For us to be able to pass all of blargg’s tests, and calling our CPU complete (for now), we need to know about how interrupts and timers work, because the will make an important part of the CPU.


Leave a Reply