Studying the Fort Apocalypse Source Code
Fort Apocalypse is Steve Hales's helicopter game for Synapse Software: fly a rescue chopper through scrolling caverns, pick up prisoners, dodge tanks, robot choppers and lasers, and destroy the fort. Hales later published the original 6502 source of the cartridge version. Next to the reverse-engineered Star Raiders listing, it's the most instructive commercial Atari game source you can read, because it shows how a 1982 developer actually organised a 16 KB action game, quirks and copy protection included.
What you'll learn: what's in the source release and how its files fit together, how to read SynAssembler syntax, how the game splits work between the main loop, a deferred VBI and four chained DLIs, how the scrolling cavern and the Navatron radar are built, how the levels are packed into the ROM, and what the anti-tampering code does.
What you need: the source files (fort.s and fort1.s to fort8.s, plus fnt1.s and fnt2.s), a text editor, and some familiarity with display lists, fine scrolling and display list interrupts. The guides Fine Scrolling, Display List Interrupts and Vertical Blank Interrupts on this site cover the techniques themselves.
1. The release and its history
The header of fort.s reads "Fort Apocalypse ROM — By Steve Hales — Copyright September 1 1982 Synapse Software", and a dated comment just below says "FEBUARY 8, 1983". That fits the published history. Hales showed an early version at CES in 1983, and the first level was changed after players complained it was too hard. Synapse released the game in 1983 on cartridge, disk and cassette, and Electronic Fun reviewed it in August 1983. Later releases include a SynSoft edition in the UK (1984) and a Load'n Go! disk (1986). Hales also said the game sold more than 75,000 copies on the Atari 800, and that the Commodore 64 version was better known.
In an interview, Hales explained why the game has only two levels. He had planned around fifty, but the game had to fit a 16 KB cartridge. He spent nearly two weeks on compression and could still fit only two, with a few bytes to spare. That compression code is in the source (see section 6).
The source was published on GitHub; the copy described here is github.com/heyigor/FortApocalypse. Its README describes it as "mainly being released for historical reasons", says it "requires a copy of SynAssembler to assemble", and licenses it under Creative Commons Attribution-NonCommercial-NoDerivs 2.5. You may share it with credit, but not sell it or distribute modified versions. The README also documents files that aren't assembler source: four level files (LEVEL.1P and LEVEL.2P from before shipping, LEVEL.1N and LEVEL.2N as shipped) and recordings of six sound effects (chopper, cruise missile, fort explosion, hyper chamber, chopper missile and refuelling), captured by running only the sound code in an emulator.
2. File structure
The README gives each file's role:
| File | Contents |
|---|---|
fort.s |
Main file: equates, memory plan, zero page, includes |
fort1.s |
Game logic and setup (start-up, title, main loop, level unpacking) |
fort2.s |
Pods, cruise missiles, tanks, hyper chambers |
fort3.s |
The VBI driver, chopper logic, robot "brains" |
fort4.s |
Sprite positioning, joystick, map drawing (scrolling) |
fort5.s |
Sound, prisoners ("slaves"), fuel base, Navatron, DLIs |
fort6.s |
Display lists, chopper shapes, cartridge start code |
fort7.s |
Variables |
fort8.s |
RAM-based display list and status panel for the Navatron screen |
fnt1.s, fnt2.s |
The two character sets (alphabet, graphics) |
fort.s pulls in everything else with .IN "H1:FORT7.S" and so on, sets the object file with .TF "H1:FORT.OBJ", and ends by filling to $BFFA and laying down the cartridge trailer. A level map file has a 6-byte load header, five colour bytes, then one byte per character cell. The README describes the playfield as four screens of 40 characters wide.
3. Reading SynAssembler syntax
Every line starts with a line number, and the dialect differs from MAC/65 or MADS:
.EQis an equate,.ORsets the origin, and.BS nreserves n bytes. Several.ORblocks are used just to lay out variables in RAM, with no code emitted..HS 70708070is a hex string..DAemits words, and.DA #$44emits a single byte..ASand.ATemit text.LDA #LABELloads the low byte of an address, andLDA /LABELthe high byte..1,.2and so on are local labels, reused after every global label.BLTandBGEare used in place ofBCCandBCSafter comparisons.- An asterisk in the label column starts a comment line.
Once these are familiar, the code reads quickly, helped by long, descriptive labels such as MOVE.CRUISE.MISSILES.
4. Start-up: a diagnostic cartridge
The trailer at the end of fort.s is worth a close look:
02690 END.CART
02700 .BS $BFFA-*
02710 .DA CART.START
02720 .HS 00
02730 .DA #%10000100
02740 .DA CART.START
These are the standard cartridge bytes at $BFFA–$BFFF: start address, a zero "cartridge present" byte, the option byte, and the init address. The option byte has bit 2 set (start the cartridge) and bit 7 set, which marks a diagnostic cartridge. The OS then hands over control before initialising itself. CART.START in fort6.s therefore does the OS's job: it clears zero page, the hardware registers and RAM up to $4FFF, copies a 128-byte block into the top of the stack page, and then uses RTS, not JMP, to run through INIT.OS (which copies the target addresses of the OS jump-table entries at $E45F and $E462 into the immediate and deferred VBI vectors) and on to START. The return addresses are stored as .DA INIT.OS-1 and .DA START-1, the usual "minus one" for RTS dispatch. The same block contains the string "Steve Hales". When running the cartridge image in an emulator, the 400/800 OS-B ROM is the usual recommendation.
START (in fort1.s) sets SDMCTL to %00111110 for single-line player/missile graphics, PRIOR to $14, PMBASE to page 0 (the player stripes are at $0300–$07FF, like Star Raiders), copies the two fonts into RAM at $0800 and $0C00, and installs the first DLI.
5. Who does what: main loop, VBI and DLIs
The game splits its work in an interesting way. In fort1.s, MAIN waits for the next frame only when the game isn't being played:
02770 MAIN
02780 LDA MODE
02790 CMP #GO.MODE
02800 BEQ .2
02810 LDA FRAME
02820 .1 CMP FRAME
02830 BEQ .1
During play it runs flat out, calling MOVE.PODS, MOVE.TANKS, MOVE.CRUISE.MISSILES, MOVE.SLAVES, SET.SCANNER, CHECK.FUEL.BASE, CHECK.FORT and CHECK.LEVEL as often as it can. Anything that must happen exactly once per frame lives in the deferred VBI, VERTBLKD in fort3.s. It reads all the collision registers into two summary bytes (CHOPPER.COL and ROBOT.COL) and clears them. Then it calls DO.NUMBERS, DRAW.MAP (the scroll), UPDATE.CHOPPER, UPDATE.ROBOT.CHOPPER, and during play the robot, rockets, lasers, blocks, elevator, chopper physics, ROBOT.BRAINS and READ.STICK. It exits through the OS at VVBLKD.RET ($E462). The result is perfectly smooth motion for the player and enemies, while slower background objects such as pods and tanks update whenever the CPU has time.
Four DLIs, LINE1 to LINE4 in fort5.s, chain to each other by rewriting VDSLST, a technique explained in Display List Interrupts. They draw colour-ramp bars with WSYNC loops, reposition missiles and the robot chopper's players partway down the screen so they can be reused, and switch CHBASE to the graphics font for the cavern.
6. The scrolling cavern and the packed map
The playfield display list in fort8.s is built in RAM so that its addresses can be rewritten every frame:
00160 .DA #$74,0,#$74,0
...
00240 .DA #$54+$80,0
00250 .DA #$41,DSP.LST1
$74 is ANTIC mode 4 (four-colour characters) with LMS, vertical-scroll and horizontal-scroll bits set. The last line drops the vertical-scroll bit, which ends the scrolling region, and adds a DLI. In DRAW.MAP (fort4.s), the fine position goes into the hardware and a new address goes into every LMS:
03540 .4 LDA SX.F
03550 AND #3
03560 STA HSCROL
03570 LDA SY.F
03580 AND #7
03590 STA VSCROL
COMPUTE.MAP.ADR.I adds the coarse X position to the map base, and the loop that follows stores one LMS address per line, adding 1 to the high byte each time. Every map row is therefore 256 bytes apart, so moving down a row costs a single INC and the address calculation never needs a multiply. The map occupies $2800 bytes starting at $1103: 40 rows of 256 bytes.
The levels are stored packed in ROM and expanded into that RAM area by UNPACK in fort1.s. The scheme is a simple run-length code with a dictionary. If a byte appears in the short table CHR1 (the common cavern characters), the next byte is a repeat count. Any other byte is stored once. A second table, CHR2 ($00,$55,$AA,$FF, solid bit patterns), is selected by a flag; those values suit bitmap data such as the Navatron overview. PACK.ADR shows that level 2 starts $62B bytes into the packed map data.
7. The Navatron radar
The Navatron at the top of the screen is a 12-character-wide window of mode 4 text. Its "pixels" are drawn into the character set itself. SET.SCANNER picks a window into a pre-drawn overview bitmap (SCANNER, 40 bytes per row) based on the coarse scroll position. DO.LINE copies 12 bytes per row into 12 consecutive characters, stepping 8 bytes per character, for eight rows each of three text lines (S.LINE1 to S.LINE3, inside the first font). POS.IT appears to plot the moving objects on top.
An AtariAge member who rebuilt the cartridge from this source reported in 2018 that the Navatron's overview bitmap couldn't be generated from the published files. It appears to have been prepared offline from the level data, and his build copied it from an existing cartridge. The overview is stored packed in ROM at PACKED.SCAN, $8D34.
8. Protection: checksums and booby traps
Three routines, DO.CHECKSUM1 to DO.CHECKSUM3, add up regions of the ROM ($9000–$AFFF, and a page at $B980). If the total isn't what it should be, they execute a single byte:
05540 .4 .HS 12
$12 isn't a legal 6502 instruction. On the NMOS 6502 it locks up the processor, so a modified ROM simply freezes. Elsewhere, lines marked PROT write into the program itself, INC DRAW.MAP in SET.SCANNER and DEC MAIN+32 in DO.LEVEL.1. Run from a real cartridge, these writes go to ROM and do nothing. In a pirated copy running from RAM, they would corrupt the game's code. Check for traps like these before patching old code.
9. Assembling it today
The README says you need SynAssembler, the assembler Synapse used, and the include directives expect the files on the H1: device. Under an emulator, map H1: to the folder holding the .s files. Converting to a modern cross-assembler is mostly mechanical: strip the line numbers, rename the local labels, and translate .EQ, .OR, .BS, .HS, .DA and #// byte selection. The packed level and Navatron data must still come from somewhere, though. The 2018 rebuild worked only on a 400 or 800, needed only 16 KB, and was described by its author as "somewhat broken". Treat the source as something to read and learn from, and remember that the licence does not allow you to distribute modified versions.
Troubleshooting
- Labels like
.1seem defined many times. They're local to the preceding global label. LDA /CHR.SET1looks like division. In this dialect it means "high byte of".- Addresses don't match a cartridge dump. Check the
.ORblocks: many only describe RAM layout. The code itself starts atPROGRAM, $9221. - Your rebuilt ROM freezes at once. That's the checksum trap. Any change in the summed regions must be balanced, or the checks removed.
Further reading
- James Hague's interview with Steve Hales, on the making of Fort Apocalypse and Slime.
- Electronic Fun, August 1983: a contemporary (and unimpressed) review.
- De Re Atari (Atari, Inc., 1982), chapters on scrolling, display list interrupts and player/missile graphics.
- Mapping the Atari by Ian Chadwick (COMPUTE! Books): cartridge option byte ($BFFD) and the OS vectors.
- Atari Graphics and Arcade Game Design by Jeffrey Stanton and Dan Pinal (Arrays, 1984): fine scrolling in game engines.
- Studying the Star Raiders Source Code on this site: a reverse-engineered listing to compare against.
Credits
- Picture: Fort Apocalypse (Synapse Software). The panel and Navatron above the yellow bar and the scrolling cavern below it are separate parts of one display list. — In-game screenshot of Fort Apocalypse (Synapse Software) from AtariMania
- FortApocalypse (GitHub, heyigor) — README (file list, level format, sounds, CC BY-NC-ND 2.5 licence) — GitHub project authors
- FortApocalypse — fort.s (header, equates, memory plan, includes, cartridge trailer) — GitHub project authors
- FortApocalypse — fort1.s (START, MAIN, UNPACK, CHR1/CHR2, PACK.ADR, DO.LEVEL.1) — GitHub project authors
- FortApocalypse — fort3.s (VERTBLKD) — GitHub project authors
- FortApocalypse — fort4.s (DRAW.MAP, COMPUTE.MAP.ADR.I) — GitHub project authors
- FortApocalypse — fort5.s (SET.SCANNER, DO.LINE, POS.IT, DO.CHECKSUM1-3, LINE1-LINE4 DLIs) — GitHub project authors
- FortApocalypse — fort6.s, fort7.s, fort8.s (display lists, CART.START, BOOT.STUFF, INIT.OS, variables, Navatron panel) — GitHub project authors
- AtariMania — Fort Apocalypse (Synapse Software, 1983; cartridge/disk/tape; 'Load cartridge with OS-B'; Electronic Fun review Aug 1983) — Synapse Software cartridge box scan from AtariMania (ft_apokalypse_cart.jpg) (Box art © Synapse Software 1983 — used for identification/commentary), 1983
- AtariMania — Fort Apocalypse (SynSoft UK, 1984) — AtariMania, 1984
- AtariMania — Fort Apocalypse (Load'n Go!, 1986, LRG-0779) with James Hague interview excerpts — AtariMania, 1986
- AtariAge forum — Fort Apocalypse - the Navatron (2018) — AtariAge forum members, 2018
- Mapping the Atari — 49149 ($BFFD) cartridge option byte — AtariArchives.org
- Screenshot: AtariMania 8bit/screens/ft_apokalypse_2.gif — AtariMania
