Mad Pascal: Installing It and Writing Your First Game
Mad Pascal is a Turbo Pascal-style compiler for the Atari 8-bit, written by Tomasz Biela (Tebe). You write Pascal on a PC; the compiler mp turns it into 6502 assembly source, and the MADS assembler turns that into an Atari executable. The result is far faster than any BASIC and much easier to organise than assembly. It is also one of the most active modern Atari tools, with new releases several times a year.
This guide gets the compiler working, explains the parts of the language that behave differently on an 8-bit machine, and then builds Snake, a complete game in about 150 lines. For the project's history and feature list, see the Mad Pascal entry in the software section.
What you need: a Windows, Linux or macOS computer, the Mad Pascal repository from GitHub (tebe6502/Mad-Pascal), MADS (tebe6502/Mad-Assembler), and an emulator. Everything here was tested with Mad Pascal 1.7.9 and MADS 2.1.7, running the output in atari800.
1. Installing
The repository contains the compiler source (src), the units (lib), the runtime assembly routines (base), many samples (samples/a8), and a ready-built Windows compiler in bin/windows/mp.exe.
- Windows: use
bin\windows\mp.exeand a Windows build of MADS. - Linux and macOS: build the compiler with Free Pascal. The README gives the command, run in the
srcdirectory:fpc -MDelphi -vh -O3 mp.pas. MADS is also written in Free Pascal and builds the same way. If you already have Wine, the Windowsmp.exeruns under it too: that is how the examples in this guide were compiled, alongside a native Linux MADS.
Either way, you end up with two programs, mp and mads, and two directories that both commands must be told about: lib (Pascal units) and base (assembly runtime).
2. The two-step build
program hello;
var i: byte;
begin
for i := 1 to 5 do writeln('Hello from Mad Pascal ', i);
repeat until false;
end.
mp hello.pas -ipath:<MadPascal>/lib
mads hello.a65 -x -i:<MadPascal>/base -o:hello.xex
The first command writes hello.a65. The second assembles it. -x tells MADS to leave out runtime procedures that the program never calls, and -i: points to the base directory. This program becomes a 353-byte executable that prints five lines. Run it in an emulator, for example atari800 -xl -nobasic hello.xex, or from DOS on a real machine. The repeat until false loop keeps the screen visible. Without it, the program would return to DOS or the emulator's boot screen as soon as it finished.
Put the two commands in a batch file or shell script. The README shows a Windows batch file that also starts Altirra with the result, and links to set-ups for the WUDSN IDE (Eclipse) and the Geany editor.
3. Pascal on a 6502: what to know first
Mad Pascal follows Turbo Pascal and Free Pascal closely, but some details are tuned for the hardware:
- Know your types.
Byteis 8 bits,Word16 bits, andIntegeris 32 bits (SizeOf(integer)returns 4). On a 6502, 32-bit maths is slow, so useByteandWordwherever the range allows.ShortIntandSmallIntare the signed 8- and 16-bit types. - Bytes wrap. Adding 10 to a byte holding 250 gives 4, with no error. Snake uses this deliberately.
- Variables are static. There is no dynamic memory allocation. Arrays have a zero lower bound and behave like pointers to their first element, as in C.
- Functions return through
Result, as in Delphi:Result := Peek(addr);. - Hardware is a variable away.
absoluteplaces a variable at a fixed address. Theatariunit already declares the common OS and hardware locations this way (color4,savmsc,sdmctl,hposp0,hitclr,rtclokand more), and thejoystickunit declaresstick0,strig0, the paddles and constants such asjoy_left.Poke,Peek,DPokeandDPeekare built in. - Units mirror Turbo Pascal.
crtgivesClrScr,GotoXY(1-based, like Turbo Pascal),Write,KeyPressed,Delay,CursorOffand aSound(channel, freq, dist, vol)procedure for POKEY.graphgivesInitGraph,Line,Circleand friends on the OS graphics modes.pmghandles players and missiles, andcmc,mptandrmtplay music from the classic Atari trackers. - Assembly is always at hand. A procedure marked
assemblercan contain anasm ... endblock, and units can link binary data through{$r file.rc}resource files.
4. Snake, step by step
The game uses the GRAPHICS 0 text screen that every Atari program starts with. A wall of inverse spaces runs round the edge. The snake is a chain of ball characters, and hearts appear at random. Each heart scores a point and makes the snake longer, and every fifth heart makes it faster. Hitting the wall or yourself ends the game.
The screen as an array of bytes
const
W = 40; { characters per screen line }
WALL = $80; { internal screen codes: inverse space, }
BALL = $54; { ball (ATASCII Ctrl-T) }
HEART = $40; { and heart (ATASCII Ctrl-,) }
procedure PutAt(ofs: word; c: byte);
begin
Poke(scr + ofs, c);
end;
function GetAt(ofs: word): byte;
begin
Result := Peek(scr + ofs);
end;
scr is set once from savmsc, the OS pointer to screen memory. Each screen position is then a single number, row * 40 + column, and the game reads and writes characters with Peek and Poke. Screen memory holds internal character codes, not ATASCII. Graphics characters typed with CTRL move up by 64, so ATASCII 20 (CTRL-T, a ball) is stored as $54, and the heart (ATASCII 0) is $40. Adding $80 to any code shows it in inverse video, so $80 is a solid block.
Reading the screen is also the collision test. Anything that isn't a blank (0) where the head is about to go is either a heart or death.
A ring buffer for the body
var
body: array [0..255] of word; { screen offsets of the snake: a ring buffer }
head, tail: byte; { byte indexes wrap from 255 to 0 by themselves }
The snake is a queue: each step adds a new head position and, unless the snake is growing, removes the oldest tail position. body stores screen offsets. head and tail are bytes, so inc(head) after 255 gives 0, and the array works as a circular buffer with no extra code. The snake can be up to 256 segments long.
The main loop
while alive do begin
Pause(delay);
NoSound;
Pause(n) waits for n vertical blanks. delay starts at 6 frames per step, about ten steps a second on NTSC, and decreases down to 2 as the score rises. NoSound cuts off the previous step's sound effect, so each effect lasts exactly one step.
Steering with case
s := stick0;
case s of
joy_up: if dir <> DOWN then dir := UP;
joy_down: if dir <> UP then dir := DOWN;
joy_left: if dir <> RIGHT then dir := LEFT;
joy_right: if dir <> LEFT then dir := RIGHT;
end;
case dir of
UP: dec(pos, W);
DOWN: inc(pos, W);
LEFT: dec(pos);
RIGHT: inc(pos);
end;
stick0 is the OS shadow of joystick 0, with the same values as BASIC's STICK(0). The joystick unit names them, so the code reads joy_up rather than 14. A snake can't reverse into itself, so each turn is refused if it points straight back. Diagonals match none of the four cases and are ignored. Moving is just adding or subtracting 1 (one column) or 40 (one row) from the screen offset.
Eating, growing and dying
c := GetAt(pos);
if c = HEART then begin
inc(score); inc(grow, 3);
if (score mod 5 = 0) and (delay > 2) then dec(delay);
Sound(0, 60, 10, 8);
NewFood;
Status;
end else
if c <> 0 then alive := false;
if alive then begin
inc(head);
body[head] := pos;
PutAt(pos, BALL);
if grow > 0 then
dec(grow)
else begin
PutAt(body[tail], 0);
inc(tail);
end;
end;
grow counts how many more steps the tail should stay where it is. A new game starts with 4 and each heart adds 3, so the snake grows smoothly rather than jumping in length.
NewFood picks random positions inside the walls until it finds a blank one. Random(21) returns 0 to 20. On the Atari the runtime takes its random numbers from POKEY, so there is no seed to set.
procedure NewFood;
var ofs: word;
begin
repeat
ofs := word(2 + Random(21)) * W + 1 + Random(38);
until GetAt(ofs) = 0;
PutAt(ofs, HEART);
end;
The word(...) cast makes it clear that the multiplication needs 16 bits. (In tests Mad Pascal widened a byte times 40 correctly even without it, but being explicit costs nothing.)
Text with crt
Status, Title and the game-over message use GotoXY and Write, exactly as in Turbo Pascal. CursorOff hides the cursor, and the program sets color2, color4 and color1 from the atari unit for a black screen with light grey text.
5. The complete program
Save this as snake.pas, then build it with the two commands from section 2. It compiles to a 2,181-byte executable.
program Snake;
{ SNAKE - a small game in Mad Pascal }
uses crt, atari, joystick;
const
W = 40; { characters per screen line }
WALL = $80; { internal screen codes: inverse space, }
BALL = $54; { ball (ATASCII Ctrl-T) }
HEART = $40; { and heart (ATASCII Ctrl-,) }
UP = 0; RIGHT = 1; DOWN = 2; LEFT = 3;
var
body: array [0..255] of word; { screen offsets of the snake: a ring buffer }
head, tail: byte; { byte indexes wrap from 255 to 0 by themselves }
dir, grow, delay: byte;
pos, scr, score, best: word;
alive: boolean;
procedure PutAt(ofs: word; c: byte);
begin
Poke(scr + ofs, c);
end;
function GetAt(ofs: word): byte;
begin
Result := Peek(scr + ofs);
end;
procedure Status;
begin
GotoXY(3, 1);
Write('SCORE ', score, ' BEST ', best, ' ');
end;
procedure DrawCourt;
var i: byte;
begin
ClrScr;
for i := 0 to W - 1 do begin
PutAt(W + i, WALL);
PutAt(23 * W + i, WALL);
end;
for i := 2 to 22 do begin
PutAt(word(i) * W, WALL);
PutAt(word(i) * W + W - 1, WALL);
end;
end;
procedure NewFood;
var ofs: word;
begin
repeat
ofs := word(2 + Random(21)) * W + 1 + Random(38);
until GetAt(ofs) = 0;
PutAt(ofs, HEART);
end;
procedure WaitFire;
begin
repeat until strig0 = 1;
repeat until strig0 = 0;
end;
procedure Title;
begin
ClrScr;
GotoXY(17, 6); Write('S N A K E');
GotoXY(10, 10); Write('Eat the hearts, avoid');
GotoXY(10, 11); Write('the walls and yourself.');
GotoXY(14, 15); Write('Best score: ', best);
GotoXY(15, 20); Write('PRESS FIRE');
WaitFire;
end;
procedure Play;
var s, c: byte;
begin
DrawCourt;
head := 0; tail := 0;
pos := 12 * W + 20;
body[0] := pos;
PutAt(pos, BALL);
dir := RIGHT; grow := 4; score := 0; delay := 6;
alive := true;
NewFood;
Status;
while alive do begin
Pause(delay);
NoSound;
s := stick0;
case s of
joy_up: if dir <> DOWN then dir := UP;
joy_down: if dir <> UP then dir := DOWN;
joy_left: if dir <> RIGHT then dir := LEFT;
joy_right: if dir <> LEFT then dir := RIGHT;
end;
case dir of
UP: dec(pos, W);
DOWN: inc(pos, W);
LEFT: dec(pos);
RIGHT: inc(pos);
end;
c := GetAt(pos);
if c = HEART then begin
inc(score); inc(grow, 3);
if (score mod 5 = 0) and (delay > 2) then dec(delay);
Sound(0, 60, 10, 8);
NewFood;
Status;
end else
if c <> 0 then alive := false;
if alive then begin
inc(head);
body[head] := pos;
PutAt(pos, BALL);
if grow > 0 then
dec(grow)
else begin
PutAt(body[tail], 0);
inc(tail);
end;
end;
end;
Sound(0, 200, 8, 12);
if score > best then best := score;
GotoXY(16, 12); Write(' GAME OVER ');
Pause(60);
NoSound;
WaitFire;
end;
begin
CursorOff;
color2 := 0; color4 := 0; color1 := $0C;
scr := savmsc;
best := 0;
repeat
Title;
Play;
until false;
end.
6. Where to go next
- Sprites: the
pmgunit (InitPM,MoveP,ColorPM,SizeP) sets up player/missile graphics. Try replacing the heart with a player that flashes. - Music: the
rmtunit plays Raster Music Tracker songs. Export the song and its "features" file from RMT, link both through an.rcresource file, and call the player from your game loop.samples/a8/sound/rmtshows the whole set-up. - Interrupts: a procedure declared
assembler; interrupt;can be installed withSetIntVec(iVBL, @proc).samples/a8/interrupt/vbl.paschanges the background colour every frame this way. - Graphics: the
graphunit opens OS graphics modes withInitGraph. The samples include display list, DLI, GTIA 10-colour and VBXE examples. - Libraries: bocianu's
blibs(bundled in the repository) adds units for display lists, player/missile graphics, high-score handling and FujiNet SIO/TCP access, among others. - Finished games: the "Games in Mad Pascal" topic on AtariAge collects complete titles with source, the best way to learn idioms.
Troubleshooting
mpcan't find a unit (crt,atari…): pass-ipath:with the full path of thelibdirectory, or runmpfrom the Mad Pascal folder.- MADS reports errors in the generated
.a65: your MADS is older than the compiler expects. Update both together. - Unexpected numbers: check the types.
Integeris 32-bit and slow, aBytesilently wraps past 255, and aWordcan't go below 0. - The program flashes and disappears: it ended. Add
repeat until KeyPressed;or an endless loop at the end. - Screen characters look wrong: you POKEd ATASCII codes into screen memory. Convert to internal codes, or use
Writefor text.
Further reading
- Mad Pascal documentation (English and Polish, linked from the repository README), with the language reference and every unit.
- The repository's
samples/a8directory: hundreds of small, working programs. - On this site: the Mad Pascal software entry, MADS (Mad-Assembler): A Practical Guide, Writing a Game in FastBasic, Step by Step, Writing a Sprite Game in C with cc65, Step by Step, ATASCII and Keyboard Codes.
Credits
- Picture: Snake, the example game in this guide, compiled with Mad Pascal 1.7.9 and running in atari800. — Program authors; screenshot by the site
- Mad-Pascal GitHub repository — README (features, build and usage commands), lib/ units, samples/a8 — GitHub project authors
- Mad Pascal documentation (English) — tebe6502.github.io
- Mad-Assembler repository README (MADS build with Free Pascal) — GitHub project authors
- AtariAge forum — Mad Pascal (main thread, from 2015) — AtariAge forum members, 2015
- AtariAge forum — Games in Mad Pascal — AtariAge forum members
- GitHub releases list for tebe6502/Mad-Pascal (release cadence 2025-2026) — GitHub project authors, 2025
