Tank Battalion Debugging Writeup...

You need an actual VIC.

Moderator: Moderators

Post Reply
User avatar
beamrider
Vic 20 Scientist
Posts: 1448
Joined: Sun Oct 17, 2010 2:28 pm
Location: UK

Tank Battalion Debugging Writeup...

Post by beamrider »

I spent a fair amount of time debugging Tank Battalion over the Xmas break as Bills442 had found one of those pesky bugs that manifests itself only after the program has been running for a while.

It proved difficult to track down as the crashes seemed random, so I suspected memory was being corrupted somewhere along the line.

I modified the program to auto-play similar to the attract mode but with sound enabled and the player invincible so I could let it run for extended periods in VICE under warp mode simulating real play.

I also downloaded a copy of the nightly VICE builds that have the "CPU history" feature enabled and let it run overnight. As suspected it had broken into the monitor the following morning with a CPU-jam and when I compared against the comiler putput I could see a corruption had occured. However, this patten seemed not to be consistent each time that I changed the code, it would crash in a different place. I was struggling to debug the compiled code too. I tried disabling optimisation without success.

Next step was to re-organise the code, making all of the variables "const" where possible (something I should have done to begin with) and then various sections of the code could be marked as "read-only". This still compiled without any issues, so it must be a run-time issue.

I produced a Windows power-shell script (below) to read the ".map" file produced by the compiler and generate a file that could be loaded into vice to protect all areas of memory that should be read only. This could be loaded into vice using the command - pb "moncmd.txt". This had to be done manually otherwise it would fire as the program was loading. With this in place I re-ran overnight and found that plotting routine was sometimes overwriting areas of memory where the code was located. This gave me a clue as to the problem and I augmented the program with some debug assert functions to check various data structures were correct and with this in place found than a look-up array for screen addresses was being overrun and resulting in random writes to memory.

It was then a fairly simple matter to fix up the code to ensure that this didn't occur the problem seemed to get fixed (fingers crossed). I confirmed this by letting two copies run overnight in VICE for 15 hours at warp speed without any issues...

The simple powershell script I wrote to generate the vice playback file is below..

Code: Select all

$input_path = '.\obj\TankBattalion.map'
$output_file = ‘moncmd.txt’
$content = Get-Content $input_path
$processing = $FALSE;
$tokens = "LOWRAM","RSYNC","RODATA","CODE";

del  $output_file;

foreach ($line in $content)
{
    if ($line.StartsWith("Segment list:"))
    {
        $processing = $TRUE;
    }

    if ($processing)
    {
        $parts = $line -split "\s+";
        if ($tokens.Contains($parts[0]))
        {
            $outLine = "watch store " + $parts[1] + " "  + $parts[2];
            $outLine | out-file -Encoding ascii -Append $output_file;
        }
    }
    
    if ($line.StartsWith("Exports list:"))
    {
        break;
    }
}
Post Reply