Tokenize/De-tokenize Commodore Basic Programs Using petcat

  /     /   Commodore     Programming     Retro    

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.

Related Articles

Changing Screen Dimensions on the Commodore VIC-20

To make the most of the limited amount of memory on the VIC-20, we can increase and decrease the screen size depending on our program's priorities and what we want to achieve. If we increase the size ...   Read More

Moving the Picture Origin on the Commodore VIC-20

The VIC-20's VIC chip provides a simple yet flexible video display and one of the features that can be quite useful is the ability to alter the picture origin on the screen. This feature allows us to ...   Read More

40 Columns in Basic on the Commodore VIC-20

There are a number of programs that allow you to use 40 columns of text from Basic on a Commodore VIC-20. This can be useful as by default the Vic's screen is 22 columns by 23 rows. They are supplied...   Read More

Adding a Basic Stub to a VIC-20 Assembly Language Program

To make machine language programs more friendly it is 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 ...   Read More

Creating a TTY Simulator in Assembly Language on the VIC-20

The Vicmon machine language cartridge is an excellent tool for exploring the Vic-20. This article and its associated video will show you how to use it to create a simple program that will change the n...   Read More

Sign up to get new articles straight to your inbox.

Delivered by FeedBurner

Comments

blog comments powered by Disqus