Retro Insectivores: Finding and Eliminating Bugs in NES Development

Sept. 4, 2019
protect

If you have ever engaged in programming, then you know about bugs. If bugs didn't bother us, the development process would be faster and more enjoyable. But these bugs are just waiting for the moment to corrupt our code and spoil our timelines and creative flows. Fortunately, there are many tools and strategies for eliminating bugs - even for retro programmers.

Debugging Tools

One of  the best ways to debug your code is to use a debugger. Some of the FCEUX and Mesen emulators have a built-in debugger which can interrupt program execution at any time in order to check the code for operability.

FCEUX Debugger

It is worth saying that this way is more suitable for advanced programmers who use assembly language. But since we are newbies, we will use the C language (cc65). Of course, the compiler will play by its own rules, and it will be difficult for us to navigate the machine code compiled from the C language.

FCEUX Hex Editor

Suppose we need to watch for some variable or array. Add the following parameter to your linker option (ld65): -Ln labels.txt

When the project is compiled, you will find the file labels.txt in your project folder. Just open it with any text viewer and look for the name of the variable you need to watch.

(Note: if you declare a static variable, it will not be included in this list. Therefore use unsigned char playerX; instead of static unsigned char playerX)

Debugger Labels

Now we know the address of the required variable. Not bad. Let's find it in the debugger. Start your ROM with the FCEUX emulator. In the “Debug menu”, click on the “Hex Editor” item, in the window that opens, press ctrl + g, and enter the address of your variable:

Find Tool

Click OK and the cursor will be moved to the address where the variable is located. Let's look at this:

Meating Hex Editor

This can be useful to check if the array is filled correctly, or to watch changes in specific variables. It also makes you feel a bit Big Brotherish, surveilling your code like this.

Be sure to check the FCEUX emulator Debug menu for other useful tools, like PPU Viewer, Name table Viewer, and much more.

Streamlining the Debug Process

What if you don’t want to run the debugger each time you check for a variable? An advanced method is to write a subroutine that will display any value on the screen. Let's try to use the score in the HUD to display the player's position on the Y axis:

Meating Macro

Works like a charm! 

Doug Fraker, a retro coder and owner of the nesdoug blog, provides a similar method for using an on-screen visualization for debugging purposes. The following subroutine creates a grey line on the screen which visually indicates CPU usage:

// void gray_line(void);

// For debugging. Insert at the end of the game loop, to see how much frame is left.

// Will print a gray line on the screen. Distance to the bottom = how much is left.

// No line, possibly means that you are in v-blank.

 

_gray_line:

 

     

If you have ever engaged in programming, then you know about bugs. If bugs didn't bother us, the development process would be faster and more enjoyable. But these bugs are just waiting for the moment to corrupt our code and spoil our timelines and creative flows. Fortunately, there are many tools and strategies for eliminating bugs - even for retro programmers.

Debugging Tools

One of  the best ways to debug your code is to use a debugger. Some of the FCEUX and Mesen emulators have a built-in debugger which can interrupt program execution at any time in order to check the code for operability.

FCEUX Debugger

It is worth saying that this way is more suitable for advanced programmers who use assembly language. But since we are newbies, we will use the C language (cc65). Of course, the compiler will play by its own rules, and it will be difficult for us to navigate the machine code compiled from the C language.

FCEUX Hex Editor

Suppose we need to watch for some variable or array. Add the following parameter to your linker option (ld65): -Ln labels.txt

When the project is compiled, you will find the file labels.txt in your project folder. Just open it with any text viewer and look for the name of the variable you need to watch.

(Note: if you declare a static variable, it will not be included in this list. Therefore use unsigned char playerX; instead of static unsigned char playerX)

Debugger Labels

Now we know the address of the required variable. Not bad. Let's find it in the debugger. Start your ROM with the FCEUX emulator. In the “Debug menu”, click on the “Hex Editor” item, in the window that opens, press ctrl + g, and enter the address of your variable:

Find Tool

Click OK and the cursor will be moved to the address where the variable is located. Let's look at this:

Meating Hex Editor

This can be useful to check if the array is filled correctly, or to watch changes in specific variables. It also makes you feel a bit Big Brotherish, surveilling your code like this.

Be sure to check the FCEUX emulator Debug menu for other useful tools, like PPU Viewer, Name table Viewer, and much more.

Streamlining the Debug Process

What if you don’t want to run the debugger each time you check for a variable? An advanced method is to write a subroutine that will display any value on the screen. Let's try to use the score in the HUD to display the player's position on the Y axis:

Meating Macro

Works like a charm! 

Doug Fraker, a retro coder and owner of the nesdoug blog, provides a similar method for using an on-screen visualization for debugging purposes. The following subroutine creates a grey line on the screen which visually indicates CPU usage:

// void gray_line(void);

// For debugging. Insert at the end of the game loop, to see how much frame is left.

// Will print a gray line on the screen. Distance to the bottom = how much is left.

// No line, possibly means that you are in v-blank.

 

_gray_line:

 

If you have ever engaged in programming, then you know about bugs. If bugs didn't bother us, the development process would be faster and more enjoyable. But these bugs are just waiting for the moment to corrupt our code and spoil our timelines and creative flows. Fortunately, there are many tools and strategies for eliminating bugs - even for retro programmers.

Debugging Tools

One of  the best ways to debug your code is to use a debugger. Some of the FCEUX and Mesen emulators have a built-in debugger which can interrupt program execution at any time in order to check the code for operability.

FCEUX Debugger

It is worth saying that this way is more suitable for advanced programmers who use assembly language. But since we are newbies, we will use the C language (cc65). Of course, the compiler will play by its own rules, and it will be difficult for us to navigate the machine code compiled from the C language.

FCEUX Hex Editor

Suppose we need to watch for some variable or array. Add the following parameter to your linker option (ld65): -Ln labels.txt

When the project is compiled, you will find the file labels.txt in your project folder. Just open it with any text viewer and look for the name of the variable you need to watch.

(Note: if you declare a static variable, it will not be included in this list. Therefore use unsigned char playerX; instead of static unsigned char playerX)

Debugger Labels

Now we know the address of the required variable. Not bad. Let's find it in the debugger. Start your ROM with the FCEUX emulator. In the “Debug menu”, click on the “Hex Editor” item, in the window that opens, press ctrl + g, and enter the address of your variable:

Find Tool

Click OK and the cursor will be moved to the address where the variable is located. Let's look at this:

Meating Hex Editor

This can be useful to check if the array is filled correctly, or to watch changes in specific variables. It also makes you feel a bit Big Brotherish, surveilling your code like this.

Be sure to check the FCEUX emulator Debug menu for other useful tools, like PPU Viewer, Name table Viewer, and much more.

Streamlining the Debug Process

What if you don’t want to run the debugger each time you check for a variable? An advanced method is to write a subroutine that will display any value on the screen. Let's try to use the score in the HUD to display the player's position on the Y axis:

Meating Macro

Works like a charm! 

Doug Fraker, a retro coder and owner of the nesdoug blog, provides a similar method for using an on-screen visualization for debugging purposes. The following subroutine creates a grey line on the screen which visually indicates CPU usage:

// void gray_line(void);

// For debugging. Insert at the end of the game loop, to see how much frame is left.

// Will print a gray line on the screen. Distance to the bottom = how much is left.

// No line, possibly means that you are in v-blank.

 

_gray_line:

 

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>