Saturday 23 December 2017

Custom VIC20 Kernal for Cartridge Testing

This is an old post, preserved for reference.
The products and services mentioned within are no longer available.

VIC20 cartridge production is now in full swing at The Future Was 8 Bit. With Cheese and Onion already released, and Spaceship Minus One available from today.
Testing these is an interesting challenge. The ROM chips are verified after programming, but it is important to test the final assembled units.
To test the Penultimate Cartridge, I wrote a test program which could be loaded from disk and would test the RAM and then cycle through all the ROM cartridge images and check they were all correct.
This works because when the VIC20 boots up, one of the first things it does is check for a bootable cartridge at address A000. If there is one, it runs it, so the Penultimate Cartridge menu is displayed.
All that needs to be done was to press F8 and the tester would be loaded from an attached SD2IEC drive.
Testing the Cheese and Onion cartridges is a little more complicated, as the game starts up as soon as the machine boots up, and runs the game. Yes, this is a test that it appears to work, and is a good start, but it would be better to get a checksum of the whole 32K of the game ROMs to be sure.
It didn't seem a good idea to modify the game code to add in a test feature, as that would need to be included and updated in all future releases. Another option was to modify the VIC20 hardware to swap around two of the ROM banks, to avoid the right ROM being present at A000 during boot, but that wasn't ideal as it meant cutting some tracks.
I have considered making a cartridge extended type thing, which plugs in and then has a slot to plug the test cartridge in, this would have some test code on it, and manipulate the ROM selectors on the test cartridge to check the ROMs. This might make an interesting project, but it probably a bit too much work.
A simpler option was to modify the kernal code, replacing a few instructions with NOPs, so it skipped the cartridge check on boot and when straight to the standard BASIC prompt. That was fairly easy to do in a hex editor, and burn a new kernal ROM for the VIC20. I used an adapter board, so it was possible to select the normal kernal ROM or the modified one.
On bootup, it then went straight to the ready prompt, and the test program could then be loaded and run. Couple of issues here. Firstly, it's a bit of a faff to have to type LOAD "cotester",8 and RUN each time. Secondly, even the cut down version of the Penultimate Cartridge tester was about 5K, so didn't fit in the unexpanded VIC20.
Briefly considering and rejecting using a multi cartridge setup with a 3K RAM expansion, or internally hacking in a 3K RAM upgrade, time for plan B. That involved building a new kernal. The idea being to integrate the test code directly into the kernal, so it would run on startup. I was hoping there would be enough space in the kernal ROM used by things that were not required that could be used to fit in the tester. That would mean it would run on power on, without the user needed to type anything, and wouldn't run the game from the cartridge (at least not until the end).
I could have found a complete kernal disassembly, then got it to build a binary identical ROM image, then start modifying it, but I didn't want to spend too long on this, so I sort of cheated a bit here. I have a command line program I wrote years ago which takes a binary file and generates asm include or C header files with a binary array of the data in the file. Assembling that file would generate a .bin file which matched the original. I could then change small sections and not have to worry about the other bits.
The first change was bypassing the startup code, so I replaced a section of the binary array with the code from the disassembly. Reassembled it to make sure it was still identical, then I could begin commenting out bits and making changes. I left in most of the initialisation code, apart from the **** CBM BASIC V2 **** message, and added my code at the point where it would have put up the READY prompt.
The VIC20 kernal ROM actually starts with the end of the BASIC ROM, so there is a few K of trigonometry routines, then some IO routines, then more trig functions. The IO routines are still required, but not the trig functions, so in the end about 3K of space I could use in two chunks. Not enough space for the 2K CRC table my original CRC code used, but I found some smaller on the fly CRC code which was still fairly fast and much smaller. That all fitted, with some space to add additional ROM checksums to the lookup table as more cartridges are added to the range. I see now I should have filled the space with FF so that I could add to the table without having to erase the EPROM, rather than 00.
Since I was writing this in raw assembler, the display is quite simple. I have used the same theme of a blue background, white text during testing as the Penultimate Cartridge tester.
As the Cheese and Onion cartridge uses two ROM chips, I have tested these separately, rather than generating an overall checksum, to deal with situations where there is an error on only one chip
Red background indicates a failure (or at least as close as you can get to red on a PAL VIC20). At this point, I jumped to the BASIC cold start code and brought up a ready prompt. In case anything useful could be done from BASIC.
Green background indicates success. If the test is successful, the user can press a key and it will start the game, in the same way it would on bootup.
And then it's packed up and shipped to the eagerly awaiting VIC20 owners.
Cheese and Onion and Spaceship Minus One are available to order now from The Future Was 8 bit.