Pascal P4 for the Propeller Chip

AiChip Industries are developing a 'reference design' for running Pascal P4 derived bytecode on the Propeller Chip.

Original Parallax Forum thread : here


The AiChip_P4_XXX.spin program interprets P4 bytecode held in hub memory. That bytecode is produced by AiChip's PrettyP4 linker from the P4 p-code generated by a P4 compiler.

The P4 compiler written by Urs Ammann, Kesav Nori and Christian Jacobi, and described in the book "Pascal Implementation" by Steven Pemberton and Martin Daniels, produces P4 p-code. P4 p-code was designed for execution on an imaginary Stack Computer ( the "SC" as described by Wirth ) and was initially implemented on a CDC 6000. P4 is the latest of a series of P compilers and supersedes PL/0 and Pascal-S.

The P4 compiler is a pre-compiled executable for Windows XP command line use provided by Scott A Moore. Source code of the compiler ( itself written in Pascal ) is available for download.

The PrettyP4 linker takes the P4 p-code and translates it into an equivalent P4 bytecode to be interpreted while performing a considerable amount of compression on the original P4 p-code which ( for the Propeller Chip ) is notionally designed for a completely 32-bit architecture. Compression primarily reduces the size of data in the constant pool ( strings and runtime bounds checking information ) but also compresses a number of P4 p-codes to allow for efficient storing of various operand sizes, bytes, words or long. The rather reader-unfriendly format of the P4 p-code file ( file.p4 ) is converted to a much more reader-friendly form ( file.p4a and file.p4b ) and a complete listing of the linked and assembled P4 bytcode is produced ( file.lst ).

PrettyP4 is P4 Virtual Machine aware so can also reduce the number of P4 opcodes required where the Virtual Machine code is the same regardless of data type. This reduces the number of opcodes and speeds up Virtual machine execution.

The PrettyP4 linker is provided by AiChip Industries and runs as a command line executable. The PrettyP4 linker is currently a 16-bit MS-DOS program and thus only supports 8-dot-3 filenames.

Once the P4 compiler and PrettyP4 linker have executed, two files ( Spin-P4.bin and Spin-P4.spin ) will have been created which the AiChip_P4_XXX.spin includes with itself. When the AiChip_P4_XXX. spin program is compiled and executed the P4 bytcode will be interpreted and the Pascal program will be executed.

Development Lifecycle

1) Create the pascal source code - file.pas
2) Compile using pcom.exe - produces file.p4 and file.err
3) Run PrettyP4 on file.err - reports any compilation errors
4) Run PrettyP4 on file.p4 - produces a more readable file.p4a
5) Run PrettyP4 on file.p4a - reformats the source for assembly as file.p4b
6) Run PrettyP4 on file.p4b - produces an assembly listing as file.lst
7) Run PrettyP4 on file.lst - produces the executable bytecode as file.bin
8) Load AiChip_P4_XXX.spin and execute

Language Support

The P4 Pascal compiler is a complete compiler in its own right, prmimarily designed to be able to cmpile itself it was never intended to support the full features of standard Pascal but does offer a considerably close approach to that.

The most significant absence is any 'string' type. Strings can only be implemented as arrays of characters.

Other omissions of an unmodified P4 Pascal from Standard Pascal include -

No Procedures or functions as parameters.
No Inter-procedure goto.
Only files of type "text" can be used.
Only predefined ( input, output, prr and prd ) files can be used.
Mark and Release replaces Dispose.
Curly bracket comments {} are not implemented.
The predeclared identifiers maxint, text, round, page, dispose are not implemented.
The Reset, Rewrite, Pack and Unpack are not implemented.
No undiscriminated variant records.
No output of boolean types.
No output of reals in "fixed" format.
No Set constructors using subranges ('0'..'9').

Primitive Types

The following types are supported -

Boolean - 1-bit ( value 0 or 1 )
Char - 8-bit ( value 0 to 255 )
Integer - 16-bit or 32-bit
Real - 16-bit or 32-bit ( not implemented yet )
Set - 16-bit or 32-bit ( 16 or 32 set members )

The P4 Virtual Machine will run with either a 16-bit or 32-bit stack which is selected at runtime. The same code will run regardless of which size is selected and out of range values will be trapped when using 16-bit and 32-bits is required. If the Pascal program does never exceeds the 16-bit limitation it will run equally well on a 16-bit platform as it will on 32-bit. Using real numbers on a 16-bit machine will howver cause considerable errors of numeric resoluton.

Input and Output

Four I/O streams are provided for -

Input - PS/2 keyboard
Output - TV Display
Prd - "Propeller Debugging", serial out on TX, P30
Prr - "Propeller Receive", serial in on RX, P31

These can be defined in the program heading of the Pascal source code -
program mypascalfile(input,output,prr,prd);
var ch : char;
  writeln(output,'Hello TV');
  writeln(prd,'Hello Serial Out');
  writeln(output,'Typed : ', ch );
  writeln(prd,'Echo : ', ch );

The I/O streams can be redefined or redirected by modifying the P4 Virtual Machine.

Future Plans

Because the P4 Compiler, PrettyP4 and Virtual Machine are all available as source code, it is possible to enhance the P4 Pascal language to support Propeller specific extensions.

No enhancements or extensions have been planned so far.

Development Tools

Pascal to P4 P-Code Compiler

This is the command line compiler which takes Pascal source code and emits the P4 p-code. The compiler works under Windows XP but not under Windows 98SE.

Executable and Source download from Scott A Moore's site : here


Executable and Source are included in the download available from the Parallax Forum.

Bytecode Interpreter

Source code is included in the download available from the Parallax Forum.

Other Compilers

If the P4 Pascal source code is changed to add more functionality to the compiler, it must itself be re-compiled to produce a newer version of the P4 Compiler executable. This requires a third-party Pascal compiler as the P4 Pascal compiler appears to be unable to compile itself these days and the complexities of self-compiling or self-interpreting the p-code of the compiler is too complex to consider as a practical methodology. Unfortunately it seems there are few Pascal compilers which can compile the P4 Compiler source "as is" and thus some modification to the source will be required to get it working. The IP Pascal compiler from Scott A Moore is claimed to compile the P4 Compiler source "as is" but is a non-free commercial product and the demo version too restricted to do so. Alternative compilers which would be worth considering are -

BACI - Ben-Ari Concurrent Interpreter. A compiler and interpreter written by M. Ben-Ari, based on the original Pascal compiler by Niklaus Wirth. A suite of programming tools orientated around p-code with concurrency and associated abilities added. More information : here

Blaster Master Pascal - A pascal compiler based on the QCC compiler from Quake, so everything should be almost like standard pascal. It's almost a mix of the best of both C and Pascal. More information : here

Borland - Borland Software Corporation have released their Turbo Pascal versions 1.0, 3.02 and 5.5 as "Antique Software" in their Software Museum for free download through their CodeGear Developer Network ( no registration required ). Download Turbo Pascal, Turbo C and Turbo C++ : here

Free Pascal - Free Pascal (aka FPK Pascal) is a 32 and 64 bit professional Pascal compiler. It is available for different processors: Intel x86, Amd64/x86_64, PowerPC, PowerPC64, Sparc, ARM. Released under the GNU General Public License. More information : here

GNU Pascal - A native and cross-compiler tool for a variety of platforms released under the GNU General Public License. More information : here

Pascal X - a complete Pascal (development) environment with a very fast compiler/interpreter for Windows 95/NT "console". More Information here


Pascal Implementation - by Steven Pemberton and Martin Daniels,

Published by Ellis Horwood, Chichester, UK
ISBN: 0-13-653-0311

Read online at Steven Pemberton's site : here

The Pascal User Manual and Report - by Kathleen Jensen and Niklaus Wirth

Published by Springer Verlag 1974, 1985, 1991
ISBN 0-387-97649-3
ISBN 0-540-97649-3

Details here


Compiler Executable and Source Code download :

P4 Virtual Machine interpreter and PrettyP4 Linker download from the Parallax Forum: