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.

OptionVersionComputers
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.

MnemonicExplanation
{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

Feedback/Discuss

Related Articles

Getting the Address of BASIC Variables on the VIC-20

Getting the address of a BASIC variable can be useful if you want to pass data to a machine code routine or want to access the bytes of a variable directly to improve speed and reduce garbage collectio...   Read More

Saving and Loading Memory on the VIC-20

Saving and loading memory is quite easy on the VIC-20 once you know how. However, it isn't obvious how to do this and therefore this article will present a few simple ways of doing it from BASIC and A...   Read More

Basic Line Storage on the VIC-20

BASIC programs are stored in memory using a simple structure that we can investigate and manipulate. This article will show how they are stored and contains a BASIC program to go through each line of ...   Read More

Storing Machine Code in REM Statements on the VIC-20

BASIC programs often contain machine code routines but they take up quite a lot of space in BASIC. An interesting way to reduce the amount of space that they take is to store the machine code in REM s...   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