Prince of Persia Source Code

My Apple II-unaware friend Jamie walked into my office this morning, surveyed the detritus of yesterday’s marathon source-code extraction, and asked “Good Lord, what happened here?!”

Tony and me at the moment of truth

I explained that the original Prince of Persia source code had just turned up after being lost for 22 years, and that two stalwart companions and I had dedicated most of the previous day and night to extracting it and posting it on github.

Jamie — who knows the term “source code” primarily as the title of the movie Jake Gyllenhaal did after Prince of Persia — digested my explanation; then, looking as confused as before, asked “Why?!?”

It was such a simple question, it stumped me for a moment. Why would I spend a whole day trying to recover data from some ancient floppy disks?

I said: “Because if we didn’t, it might have disappeared forever.”

Why source code?

POP source code recovered after 22 years

Non-programming analogy: Video game source code is a bit like the sheet music to a piano sonata that’s already been performed and recorded. One might reasonably ask: If you have the recording, what do you need the sheet music for?

You don’t, if all you want is to listen and enjoy the music. But to a pianist performing the piece, or a composer who wants to study it or arrange it for different instruments, the original score is valuable.

It’s possible, up to a point, to reverse-engineer new source code from a published video game, much as a capable musician can transcribe a musical score from listening to a performance. But in both cases, there’s no substitute for the original document as a direct line to the creator’s intentions and work process. As such, it has both practical and historical value, to the small subset of the game-playing/music-listening community that cares.

This is why I was so sorry to have lost the Prince of Persia source code, and happy to find it again.

Lost and found (Geek quotient = 9)

If you’ve read my 1980s game dev journals, you know that by the time Prince of Persia shipped in 1989, I was burned out on coding and seriously eager for the next chapter of my life to start. So I did what most programmers would do: I backed up my Apple II source code onto 3.5″ floppies, stuck it in a box, and promptly forgot about it.

Thirteen years later, when I looked for that box of source code again, I couldn’t find it. I was in Montreal with an amazing team making Prince of Persia: The Sands of Time. Lead programmers Dominic Couture and Claude Langlais had volunteered to port the original POP to the PlayStation 2, and slip it into our new game as an easter egg. (This was their idea of a fun respite from crunch time.) All they needed was the source code. But as much as we searched — from my garage to Broderbund’s archives to Doug Carlston’s basement — it was nowhere to be found.

Finally we tracked down Scott Shumway, who’d done the 1992 Mac port of POP. He didn’t have the Apple II source code either, but he did have the Mac source code. Dom and Claude made short work of porting it to the PS2, and Sands of Time got its easter egg. Everyone was happy.

On my return to LA, I dug deeper, and turned up a whole shoebox full of Apple II floppies, some dating back to high school. The source code to all my early games was in there: Deathbounce, Karateka. But no POP.

I didn’t need the source code for anything; and it wasn’t as if POP had been lost to history — vintage Apple II POP copies (and their disk images) were widely available — but still, it bothered me to think that something I’d spent years working on was just gone. I felt dumb for not having kept a copy.

This was eight years ago. I gave up the search and forgot about it.

Until two weeks ago, when my Dad shipped me a carton of my stuff he’d found cleaning out the closets of his New York apartment. Inside was the source code archive I’d mislaid in 1990.

Paper is forever

Here’s the thing about digital backups and magnetic storage media: They don’t last very long.

Try popping your old 1980s VHS and Hi-8 home movies into a player (if you can find one). Odds are at least some of them will be visibly degraded or downwright unplayable. Digital photos I burned onto DVD or backed up onto Zip disks or external hard drives just ten years ago are hit and miss — assuming I still have the hardware to read them.

Whereas my parents’ Super 8 home movies from the 1960s, and my grandparents’ photos from the 1930s, are still completely usable and will probably remain so fifty years from now.

Pretty much anything on paper or film, if you pop it in a cardboard box and forget about for a few decades, the people of the future will still be able to figure out what it is, or was. Not so with digital media. Operating systems and data formats change every few years, along with the size and shape of the thingy and the thing you need to plug it into. Skip a few updates in a row, and you’re quickly in the territory where special equipment and expertise are needed to recover your data. Add to that the fact that magnetic media degrade with time, a single hard knock or scratch can render a hard drive or floppy disk unreadable, and suddenly the analog media of the past start to look remarkably durable.

This is why, when I posted about finding the POP source code, digital archivist Jason Scott, Apple II collector Tony Diaz, Derek Moore, and the technical teams behind the DiscFerret and Kryoflux disk readers volunteered their time and effort to give us the best possible shot at a successful extraction.

Let me begin to count the ways I’ve been lucky with this: The box was found. The disks were intact. Prince of Persia and I happened to have a high enough public profile that people of Jason and Tony’s caliber (and dozens of others who contributed their expertise via IRC, skype and twitter from around the world) cared.

In the bigger picture, our timing was lucky. The 1980s and the Apple II are long enough ago to be of historical interest, yet recent enough that the people who put the data on the disks are still with us, and young enough to kind of remember how we did it. Roland Gustafsson, author of the special 18-sector RWTS routines that had made our disks super-efficient in 1988 (and unreadable to anyone but us), was able to get on IRC in 2012 and explain what he’d done to Discferret kids who weren’t born then.

For all these lucky reasons, our archaeological expedition was crowned with success.

From a preservationist point of view, the POP source code slipped through a window that is rapidly closing. Anyone who turns up a 1980s disk archive 20 or 30 years from now may be out of luck. Even if it’s something valuable that the world really cares about and is willing to invest time and money into extracting, it will probably be too late.

This is why it’s awesome that there are people out there working on digital preservation. Because now is the time.

Back up your backups

Jason suggests the following rule of thumb: If you have data you want to keep for posterity, follow the Russian doll approach. Back up your old 20GB hard drives into a folder on your new 200GB hard drive. Next year, back up your 200GB hard drive into a folder on your new 1TB hard drive. And so on into the future.

As for me, the past 48 hours have been a fun walk down memory lane. And have given me a renewed appreciation for paper, celluloid, and stone tablets.

(Postscript: For 6502 assembly-language aficionados, the Prince of Persia source code is now up on github, along with a README file that answers some frequently asked questions. I’ve been amazed and moved by the outpouring of interest in the #popsource saga — it literally crashed this website for several hours today.)

Now, I really need to get back to my day job of making up new stuff. I can only hope to have the same lucky, glorious headache of trying to recover some of it 20 years from now.

Further reading:

“Making of Prince of Persia” game development journal

25-year capsule history of Prince of Persia

POP source code on github

Código Fuente de Prince of Persia – ¡Posteado! (Spanish translation of this post)

Posted on Apr 17, 2012 in Blog, Featured, Games, Making Games, Old School, Prince of Persia | 49 comments

49 Comments

  1. 4-17-2012

    I am really glad you managed to extract the source-code! It must’ve been quite a party once you were all finished!

    Great post, good advice and the remark about the stone tablets made my day!

    I hope your week will continue to be successful!

  2. 4-18-2012

    What an incredible contribution to the history of programming. Thank you. :)

    (ps. Just a coincidence [I think] that the poster above me has a very similar name)

  3. 4-18-2012

    What a great story, thank you (I was 17 back then in ’89..)!

  4. 4-18-2012

    A few months ago I restored data from about 300 old C64 disks but unluckily some of the disks with my own source code and projects were probably the most used and were unreadable.

    Still, I did find a few projects I had completely forgotten about and it’s a great feeling seeing this stuff come alive again!

  5. 4-18-2012

    thanks for this wonderful story

  6. 4-18-2012

    From now on,your code will be eternity.Thanks for your great work and your impressive story!

  7. 4-18-2012

    WAIT… I think I got you beat! Six months ago, I github-ed a program I wrote for Ticketron (remember them? sorta like TicketMaster, but with a soul) in 1987-88.

    https://github.com/jamescurran/HonestIllusion/tree/master/PCT

  8. 4-18-2012

    Great post! Thank you.

  9. 4-18-2012

    Great advice: Back up your backups!

  10. 4-18-2012

    The trouble is that even though the source code might now be widespread, if GitHub goes down or is acquired or whatever – the archive will be lost. The current libraries storing piles of paper are not doing the job they should of organizing and archiving digital content.

    • 4-18-2012

      Not really — anyone who has cloned the repository to their hard drive has a copy of the source code.

  11. 4-18-2012

    Great story! Like my dad says, keep everything in three copies, and then copy those. Glad you found that treasure.

  12. 4-18-2012

    I see in the post that you have the Karateka source code, any chance of uploading it too to devour?

    • 4-18-2012

      I’ve been wondering if it’s possible (with mods) to hold the entire code of Karateka in memory on a 64K system so there’s no disk access. (on a 128K system it’s almost certainly possible.) That way I could make a file-loaded version.

      File-loaded versions of games were pretty popular back in the day – I collect them and usually compress them. On IRC a couple days ago I mentioned a project I did that put 7-9 games on a disk using Roland Gustafsson’s RDOS 3.3 and an executable-patcher for Commodores called Exomizer. (I think it can handle Apple directly now. I still use a format translator intermediary though.)

      Karateka appears to use page-flipping for flicker-free animation. That means it needs 16K for graphics. That space could be used for initial loading, and then the data moved up onto the bankswitched RAM. With the compression it doesn’t really matter if the program spills into DOS space so long as the *compressed* binary only needs about 35K. The game itself is just a 48K game and doesn’t need to use the extra 16K for its own code.

      If I’m right and Karateka is written in S-C ASSEMBLER (the program is mentioned in the PoP diaries e-book), I think can read that dialect without too much difficulty and easier than Merlin. I used a disassembly of the Apple ][+ BASIC in the S-C dialect as the basis for a form of the so-called "satan mode" (reasonable Apple ][ compatibility without locking the hardware into Apple ][ mode) on the Apple /// using CA65 and Ciderpress.

      • 4-18-2012

        Sorry, that should be “executable-packer”, not “executable-patcher”

      • 4-23-2012

        A file version is entirely possible, but it will still require its own loader. The game is too big to support traditional DOS. However, this kind of thing has been done before – see Black Bag’s version of Olympic Decathlon, in particular. It was a multi-loader, but with a custom loader, the data were placed in files and the loader interprets the file structure.

      • 4-24-2012

        Though could it be tweaked to only load once? If so, and if it compresses well enough in Exomizer, it’s still viable to make a single-load version. (And being as it could then be run from ProDOS-8, it could also be loaded and run from 3.5″ floppy or hard disk.)

  13. 4-18-2012

    Congratulations Jordan ! This is one of the happiest days of my life ! I was really really really hoping for the POP source code to be safe !

    Thank you, once again, for bringing Prince of Persia in our lives !

  14. 4-19-2012

    Woohoo! :D

  15. 4-19-2012

    I’d love to see some Apple II programmer get in there and make some sort of indie revivalist art-house romp as a sort of unofficial sequel. :)

    Perhaps a story of what the prince and princess get up to after she is rescued?

    With a 70′s funk music soundtrack…

    Muzboz

  16. 4-19-2012

    Hey Jordan – been reading through your POP source code – very nice. Thanks for posting it!

    I teach game programming at New England Institute of Technology in Rhode Island, and often send my students to your journals to get a perspective on what indie game development is like. Occasionally we’ll look at the Visual6502.org so they can get an understanding of how microprocessor chips actually work.

    BTW – I wrote in Apple II 6502 assembler between 1982-86 before I moved on to the Mac. I used to work for Sweet Microsystems (makers of the Mockingboard) and got to see all sorts of original commercial 6502 source code – like Will Harvey’s Music Construction Set among others. IIRC, most of the stuff was complete spaghetti. Your code is well laid out and very self-documenting.

    Question – is the source Merlin 8/16 assembler? Thanks!

    • 4-24-2012

      Dug out the old manuals – Yes – It’s Merlin 8/16.
      It’s been a long time.

      I’d like to do a mini-seminar on POP source the next Game Jam we hold.

  17. 4-19-2012

    What of this does Ubisoft own, and what do you own? Is it like Star Control 2 where The Ur-Quan Masters is the same thing, just renamed?

    • 4-20-2012

      That would be (IANAL) my understanding, at least.

  18. 4-20-2012

    Wiser words have never been spoken. I’ve been busily consolidating all my CD/DVD ROMs/Rs/RWs onto hard disk, and did so for the floppies and Zips a long time back (they actually went on to CD first … bad CDs that, once I could afford a properly huge HDD to copy stuff onto, meant I still had to hunt a few of the original floppies and also a working Zip drive). The stuff is remarkably friable, and I’ve also since learnt to keep three copies of everything – the original (in a box at the back of a dusty cupboard), a working copy (on the desk), and a safe archive (in a different building if at all possible). Unfortunately it’s a slow and expensive affair if you have any kind of serious collection of digital data, and you still end up with gaps :(

    And I really, really, really want to know what happened to my Atari ST disk image collection that took a couple months of painstaking work to extract, because that was better than 10 years ago now and I bet a number of the ones that were barely readable at the time are now completely dead.

    (Can Amiga drives read Atari floppies? I’ve now got a more functional A600 alongside my rather ratty ST, and it came with 300 discs of its own… plus an internal HD interface. If I can hunt out some software that’ll image floppies direct to an installed hard disk (the 4GB max partition size would be more than enough to hold both system’s software libraries) then it wouldn’t be too hard to set up a production line to read the lot of them over a year or so)

    Oddly enough, though – hard disks, and to a lesser extent floppies, seem to be about the most resilient form of computer data storage for long term purposes (after well-made tape… but before cheap consumer QIC drives and audio cassette). There are BBC disks from 30 years ago that still work, and a great many of the Atari and Amiga ones from 20-25 years past still function. I’ve a 1987 286 with a 40mb hard disk that’s still absolutely cherry… though it’s a rather low density option these days! You could probably fit the pertinent details of one person’s life onto it in order to give future generations a glimpse of the past, however, if you included the PC, self loading code, and suitable instructions on how to power it.

    • 4-20-2012

      PCs should be able to read ST floppies – they’re MS-DOS format. (Might have some trouble with very early floppies due to a bug in the TOS formatter.)

  19. 4-20-2012

    Very nice to hear this, Jordan. The last time I played this game (the DOS version) is 2001, when I was in the junior high school. Now, given those 6502 assemblers and simulators, do you know to build this game? I’d love to see it run once again.

  20. 4-20-2012

    This is a fascinating find – thanks for sharing it with the Internet!

    I don’t know about anyone else, but one of the things I was most surprised by (in the design document you also shared) was the revelation that all the levels were given names. I understand most of them (Cell and Tower are obvious, Quad… starts in a room with four doors, and after a bit of help I realized that “Wtless” was because of the “weightless”/floating potion) – but why did level 8 end up being called “329″?

  21. 4-21-2012

    Great find, and thank you for making this available! It’s always a joy to read old source code not only to see what made our beloved classics “tick” but also to learn what clever tricks were used with early hardware and, possibly, how to adapt those tricks to homebrew games and even games developed on current-gen systems. =)

    Are you still in touch with Scott Shumway? I grew up playing PoP on a Macintosh II and would be very interested in looking at the Mac source code. Maybe if Mr. Shumway is reading this… is there any possibility that code could be released?

  22. 4-23-2012

    Thanks for the fascinating post and sharing the code – this really is an eye opener.

    For me, this is a throwback to a time where games where made entirely by a single developer, as opposed to the huge teams used today. I am reading the POP journals, and here’s my take on it:
    http://blog.flexwiz.net/finding-inspiration-in-mechners-journals/

    I grew up playing 8-bit games on C64, Apple II and Amiga. Sometimes these games almost seem like a lost art.. it sure is nice to take a peek at the code.

  23. 4-23-2012

    I’ve not seen assembler instructions laid out so beautifully. Loved the way you deconstructed the entire game logic into such compact routines. I hate to imagine how many iterations of the code there must have been to make it this tight. A quick read of the technical information document and I can see how the whole things hangs together in flash – just wonderful. It brings a whole new level of insight to the game and provides the definitive answer to “just how did Jordan do that?” It’s good to see it in the public domain, it will be a great case study for future generations of techys to come.

  24. 4-24-2012

    Insanely Brilliant [tm] !

    Thanks for sharing, the story and the code, to all involved. POP was pretty much the first ‘Really Big’ game I played, for years. It awakened my interest in computers, coding, and computer graphics. I’m going to have so much fun playing with it again now, but in a techy way, for nostalgia’s sake :-)

    And regarding the stone tablets of backup (lol :) ) that’s one of the most important lessons people have to learn. Did so myself the hard way too. Backup backup backup and test the backup and backup some more! In future-proof ways. Do the ways change? Convert the backups to the new way! Test the backups! Backup some more.

  25. 4-24-2012

    I truly appreciate this article post. Awesome.

  26. 4-30-2012

    It’s more than packing the executable that’s required to one file a game.

    If there isn’t enough room to hold everything in memory you have two options.

    1) compress data in memory, and decompress the level data as needed. This is only possible if there’s room for one uncompressed level, and every other level compressed. This was done with the goonies. There was enough space left to crunch all of the levels and store them all in memory, and have enough space to uncompress the active level.

    2) the Interactive Flexible File Linking (IFFL) system. here you write a special loader, append the rest of the game to it, and have the game read from inside the file as needed. In this case, generally the game file is required to use contiguous sectors and tracks on the disk. Naturally a fastloader would be used to do this.

    • 5-1-2012

      I’m aware of the limitations of packing. I was wondering if it was possible to have the game only need, during the load process, the $0800-$BFFF area, where some of that code could be moved around into other parts of memory (including the language card). After that, if the file could be crunched such that it needs no higher address than about $9000 (could probably go up near $9600, really) before decompression, then that’s all that one needs.

      As for fast loading, I’ve often ditched DOS 3.3 altogether on my multigame disks – they often rely on Roland Gustafsson’s RDOS instead, which gives me a few more sectors because of lower overhead, as well as faster load due to a simpler filesystem and no possibility of fragmentation.

      I actually saw a single-load crack of Karateka on the C64. I don’t know if that would be doable on the Apple…but it might be.

  27. 5-15-2012

    Thanks for this great stuff.. Ilike POP all the time…

    Thanks once again..

  28. 5-19-2012

    Yes, it is THE PROBLEM! I have faced it, too. I was very happy when I found an old PC with 5″-disket drive! IMHO, there must be a standard for digital data (f.i., DVD in UTF8, with appropriate drives and drivers), useful in the far future.

  29. 6-5-2012

    Congratulations! POP was one of my all-time favorite games that I played when I was younger. I know what its like to recover such old archives; Several years back I had my own experience of attempting to restore a bunch of old floppies from my youth (for the TRS-80 Color Computer), and bit-rot had set in on many. With persistence and a bunch of trial-and-error, I managed to recover about 95% of it. I’ve gone with the “russian doll” approach since then for everything (along with having a separate RAID fileserver, and a backup system). Even so, I still have a small Apple IIe archive to recover (need to work on that soon), along with a somewhat larger Amiga archive.

  30. 6-8-2012

    This is very cool and im glad you guys were able to save this piece of gaming history!!! seriously!!

    any chance we can start a hunt for pitfall sc lol

  31. 7-4-2012

    Hey! Great post – PoP was my favourite game when i was young :)
    My colleague and I work in digital preservation and were wondering – can you share what you actually had to do to recover the source code?

    Thanks again!!

  32. 7-20-2012

    Congrats!
    Does it compile yet? ;)
    Let’s hope it isnt’ again lost in the next 20 years? :P

  33. 8-9-2012

    this is the game I have loved more. If I had the knowledge I would build another episode of this game with different levels,a more sofisticated IA of enemies but with the same graphic. I don’t like prince of persia 2 or 3 so much as the number 1.

    • 11-14-2012

      “prince of persia level editor” in Google gives you pretty much all you need for another episode.

  34. 11-27-2012

    Wow, awesome. I would like to port this directly to ARM :) for Open Pandora

  35. 2-2-2013

    Hey, I just bought an Apple iie platinum off E-bay.

    Going to learn 6502 assembly with it, and I’m looking for a copy of Merlin 8/16

    Got any ideas where I can find it? (Or any other macro assembler for the apple ii?)

    Thanks :)

  36. 3-18-2013

    Thank you Jordan! Your game was on of the best in 90-s! It have very good persons movement! PRINCE MEGAHIT ;)

  37. 6-15-2013

    I remember playing POP as a kid. Best game ever.

  38. 6-16-2013

    I had played POP long back, it’s still one of those games you like to remember along with Mario and Jungle.

  39. 4-22-2014

    This entity has the mechanica parts that are used to assemle the bicycles.
    Remember, sometimes thee most loved gifts are the ones that she can’t
    hold in her hands. In the ‘Driving on Thin Ice’ episode, the main events are
    1.

Leave a Comment