
Prince Of Persia Code Review - glaze
http://www.fabiensanglard.net/prince_of_persia/index.php
======
jasonlotito
Just for reference, these are two books that were from the journal of Jordan
Mechner written while writing Prince of Persia and Karateka. They are
enjoyable reads, and I highly recommend them.

[http://www.amazon.com/Making-Prince-Persia-
Journals-1985/dp/...](http://www.amazon.com/Making-Prince-Persia-
Journals-1985/dp/1468093657/ref=sr_1_1?s=books&ie=UTF8&qid=1371822194&sr=1-1&keywords=prince+of+persia)

[http://www.amazon.com/The-Making-Karateka-
Journals-1982-1985...](http://www.amazon.com/The-Making-Karateka-
Journals-1982-1985/dp/1480297232/ref=pd_bxgy_b_img_y)

~~~
VikingCoder
Do they look good on Kindle, or is the physical book vastly better?

~~~
ikonst
I don't have the physical one, but I can't imagine what I could be missing
out. If anything, you can keep reading it on your phone if you don't have your
e-book with you etc.

~~~
VikingCoder
Sometimes books with diagrams, pictures, source code, look bad on an e-book.

------
glurgh
I love reading Sanglard's stuff I hope he's encouraged by the comments on his
site to complete this - the bulk of it so far is on details of the Apple ][
architecture. It'd be great if he decides to take apart the actual game,
although even with commented assembly source, it's no trivial amount of work.

~~~
mhd
The C64 reverse engineering covers that a bit. At least regarding general data
structures, as opposed to in-depth Apple II hackery (you get some C64 hackery
instead, of course).

[http://popc64.blogspot.com](http://popc64.blogspot.com)

~~~
glurgh
Ok, that is the craziest (in a good way) thing I've seen in some time. I'd
have thought PoP had been ported to the C64 at the time - Karateka was ported
just about everything. So in 2011, starting with the Apple source, disassembly
dumps, graphics from a free version, looking at the PC port and god knows what
else, someone ported it to the C64. Amazing.

EDIT: Oh, and apparently the Apple ][ source hadn't been found again yet,
making the thing even more bonkers. Mechner's comments here:

[http://popc64.blogspot.com/2011/10/prince-of-persia-for-
comm...](http://popc64.blogspot.com/2011/10/prince-of-persia-for-
commodore-64128.html?showComment=1318790546501#c8564553486263557064)

~~~
mhd
If I remember correctly, the ported game does need a memory extension, which
would've been a no-go back then. Wonder how much you would have to cripple the
ported effort to fit in in there, if at all possible with the same approach...

~~~
glurgh
Yep, it looks like it requires a 128KB Apple //e and up. In the comments of
the blog, Mechner mentions he couldn't get anyone interested in porting it to
the C64 at the time as the system was considered too old. I guess by '89, 128
KB Commodores weren't considered that interesting of a market.

The original must have needed all the memory it could get its hands on - looks
like it doesn't use the Apple //'s 'double high res' modes, for instance.

Forum thread of Sanglard trying to sort the thing out, with the help of some
6502-heads is also entertaining:

[http://forum.6502.org/viewtopic.php?f=2&t=2223](http://forum.6502.org/viewtopic.php?f=2&t=2223)

Almost makes you want to combine the two actually hacker-ish posts of the last
two days - the lightweight block ciphers and this and burn a weekend
implementing SIMON in 6502 assembly. Almost.

------
superasn
Love how most games were mainly a one man show back in the 80s. Jordan
Mechner, John Carmack could be used as aliases for the games. Btw, I read a
very similar article about Doom / sometime ago about the Magical Square Root
Implementation In Quake III, another interesting read[1]

[1]
[http://www.codemaestro.com/reviews/9](http://www.codemaestro.com/reviews/9)

~~~
goostavos
That was interesting! Has he ever said how he came up with the number?

~~~
darth_aardvark
It's posted pretty regularly on HN. [http://blog.dave.io/2011/10/0x5f3759df-
true-magic-number/](http://blog.dave.io/2011/10/0x5f3759df-true-magic-number/)

------
coldcode
Having written a VT100 terminal emulator in 6502 assembly back in those days I
remember it well. But games had to be more clever as the memory was so tight.
At least in my case the only fun part was showing 80 columns of data on a 40
column screen (if the user had no 80 column video card). Today we can focus
much more on just writing code instead of engaging in daily hackery.

------
t1m
This is a great walkthrough, and a real trip back to the good old days. I
learned programming on the Apple ][ and, like many started in Basic then moved
to 6502 assembly.

I am not sure how other teenagers made this awkward transition, but I did it
by writing a tiny assembler in Basic. It was fairly easy because Basic had
PEEK (get contents of address), POKE (set contents) and CALL (jump to address)
that everyone was used to using to get anything useful done. I think I just
got tired of hand assembling programs into POKE calls.

I remember getting a thin wire-bound book with a detailed disassembly (with
comments!) of the ROMs and DOS as a birthday present. I think one of the thing
that inspired programmers on the Apple ][ was the amazing system code that Woz
had written. He set a very high bar for assembly language programmers. His
code was economical, fast, smart, and tiny. He demonstrated how to squeeze the
most out of limited resources.

~~~
to3m
I learned to program 6502 on the BBC Micro. It had a built-in assembler as
part of its BASIC. Very useful! Assembly language instructions got written to
memory according to the value of a specific variable, and there was a somewhat
neat mechanism for doing 2 passes (to fix up forward references). It was quite
flexible, and gave you all the power of the inbuilt BASIC - which was of
course awful, though excellent by the standards of the day - for writing
macros.

More programming languages should have included something like this, I think.
Maybe by now we'd have runtime code generation as a common primitive,
something like LuaJIT's dynasm
([http://luajit.org/dynasm.html](http://luajit.org/dynasm.html)), only with
GC, so you could generate custom little routines at runtime.

(As it is, what do we get? gcc-style inline assembly language. Progress? My
arse.)

------
uxp100
I see mentioned that the PC port did not go very well. I've never played PoP
on my Apple ][, only the DOS port. Am I missing something by only having
played the PC version?

(Maybe Color? I don't remember if it was B&W because of my VGA card or because
the PC version just didn't have color.)

~~~
nsxwolf
The PC version had color. It was my first version as well, and to me is the
definitive port. I didn't like the cartoony Macintosh version that became the
standard for all the 16 bit ports. The new art muted the fluid animations IMO.

------
rpicard
He also has an interesting Doom 3 code review:
[http://fabiensanglard.net/doom3/index.php](http://fabiensanglard.net/doom3/index.php)

------
StacyC
I loved PoP, played it a LOT when I was in college. Great memories.

