Tokenize/De-tokenize Commodore Basic Programs Using petcat

petcat is a utility provided with the VICE Commodore emulator that you can use to convert Basic source code contained in ASCII text files to .PRG files or vice versa. It is also able to convert ASCII text files to and from PETSCII .SEQ files. This makes it a great utility for cross-development purposes.

Convert Basic Source Code in an ASCII Text File to a .PRG File

If the input file is Basic source code stored as an ASCII file then petcat will tokenize this to create a .PRG file as its output. Blank lines will be ignored, but the Basic keywords should be in lowercase because of they way that Commodore Basic stores them. If, for example, you used lI in your Basic source code it would be converted to list.

In the following example we specify tokenizing with Basic v7.0 using the -w70 switch and the output file with -o program.prg. The -- switch tells petcat to stop processing command line switches and the file to convert is program.bas at the end.

$ petcat -w70 -o program.prg -- program.bas

Convert a .PRG File to an ASCII Text File

If the input file is a .PRG file then petcat will de-tokenize this to create an ASCII text file as its output.

In the following example we specify de-tokenizing Basic v2.0 using the -2 switch and the output file with -o program.bas. The -- switch tells petcat to stop processing command line switches and the file to convert is program.prg at the end.

$ petcat -2 -o program.bas -- program.prg

Basic Versions

petcat supports many different flavours of Basic that came as standard with 8-bit Commodore computers as well many extended and alternative Basics. Further options are listed in the VICE manual. Below are the options used to specify the standard flavours of Basic. These are used after the -w switch if you want to tokenize and without if you want to de-tokenize.

1pBasic v1.0PET
2Basic v2.0C64/VIC20/PET
3Basic v3.5C16/C116/Plus/4
40Basic v4.0PET/CBM2
70Basic v7.0C128

Unprintable / Special Characters

Commodore Basic programs can contain characters that aren't printable in ASCII, so petcat uses a set of mnemonics to encode these. Below are some of the most commonly used.

{blk}Colour: black
{wht}Colour: white
{red}Colour: red
{cyn}Colour: cyan
{pur}Colour: purple
{grn}Colour: green
{blu}Colour: blue
{yel}Colour: yellow
{orng}Colour: orange
{brn}Colour: brown
{lred}Colour: light red
{lgrn}Colour: light green
{lblu}Colour: light blue
{gry1}Colour: grey1
{gry2}Colour: grey2
{gry3}Colour: grey3
{rvon}Control: reverse on
{rvof}Control: reverse off
{clr}Control: clear screen
{home}Control: home
{inst}Control: insert
{del}Control: delete
{up}Cursor: up
{down}Cursor: down
{left}Cursor: left
{rght}Cursor: right

Example Using the Mnemonics

The following example clears the screen using the {clr} mnemonic and then prints a line of black text using {blk} followed by a line of red text using {red}, finally it finishes the program by changing the text colour to blue using {blu}.

10 print "{clr}"
20 print "{blk}this text is black"
30 print "{red}this text is red"
40 print "{blu}"

Autostarting a .PRG in VICE

VICE allows you to autostart a .PRG file by putting it on the command-line. You may need to use the -basicload switch (depending on how AutostartBasicLoad is set in your config) to load it using ,8 instead of ,8,1. The following will autostart the basic program, program.prg, using VICE's VIC-20 emulator.

$ xvic -basicload program.prg &

Video Demonstrating petcat

You can see petcat being used in the following video:

Creative Commons License
Tokenize/De-tokenize Commodore Basic Programs Using petcat by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Share This Post


Sign up to get new articles straight to your inbox.

Delivered by FeedBurner

Related Articles

SUBLEQ on the Commodore VIC-20

I have created a SUBLEQ Virtual Machine for the Commodore VIC-20. SUBLEQ is a computer architecture that has only one instruction: SUBLEQ. The instruction stands for SUbtract and Branch if Less than ...   Read More

Sweet 16 (The 6502 Dream Machine) Ported to the VIC-20

Sweet 16 was created by Steve Wozniak to reduce code size and make it easier to handle 16-bit pointers and arithmetic for his Apple Integer BASIC. He wrote it around 1977 and referred to it in an arti...   Read More

Position Independent Code (6502) on the Commodore VIC-20

If we are writing 6502 machine code and want to to create a routine or program that can be placed in any location then we have to create Position Independent Code (PIC) or make the code relocatable. H...   Read More

Adding Basic Stubs to Assembly Language on the Commodore VIC-20

To make machine language programs more friendly it's nice to add a Basic stub which contains a line with a SYS statement to start the code. This is easy to do on the VIC-20 and the process gives you a...   Read More

Hand Assembling to Machine Code on the Commodore VIC-20

I quite enjoy designing machine language routines on paper and then hand assembling them. For many people this would have been their only option until they got a more advanced machine language monitor...   Read More