Singapore-MIT GAMBIT Game Lab spacer
  CMS MIT
spacer New Entries Archives Links subheader placeholder
Updates
left edge
pinstripe
The Night Sky in Spacewar!

Expensive Planetarium Source

When we first started looking at the source code for Spacewar! we were struck by a strange section of code at the end, which was solely made up of repeated instructions along the lines of the code in the image on the right. A little bit of reflection and some background reading lead us to discover that this code was setting up a table of all the coordinates of the stars displayed in Spacewar!'s background. The code for doing this is a program in itself called Expensive Planetarium, written by Peter Samson, which displays a faithful recreation of the night sky as visible from MIT and was originally independent of Spacewar, but was later integrated into it. You can see the section of Expensive Planetarium that sets up the star field data on Wikipedia. Investigating this gives a bit of a taste of what unravelling Spacewar! is like.

Right up the top of the source listing is a comment that tells us Peter R. Samson wrote this for version 2b of Spacewar! The command 6077/ means the code from here down should be placed starting at that address in the PDP-1's memory. Next up is another comment telling us that Peter finished his code on March 13th in 1962 and a directive saying all the numbers from here on down should be interpreted as decimal, rather than the default of octal.

Now comes the definition of mark, which is a macro for converting the star coordinates into a format that makes them easier to process for displaying. Anywhere mark is called, the Macro preprocessor will replace the call inline with the code here, substituting in the arguments for the variables X and Y. Interestingly though, any arithmetic expressions or repeat statements are handled by the preprocessor, rather than being translated into code, so repeat 8, Y=Y+Y will multiply Y by 2^8, which is equivalent to bit-shifting it left by 8 bits, but will do this before run time. Wikipedia's listing actually has an error in the code; 8192-X and Y should be on two separate lines. These two lines are telling the preprocessor that 8192 (which is 2^13!) - X should be written at the current address in memory and Y should be written at the following address. In this way, the long list of mark commands that follows the macro's definition builds up a table of the positions of all the stars in the night sky visible from MIT.

Looking at this big list you might wonder why all the X coordinates are in the strange range of 0 to 8192 yet the Y coordinates can be both positive and negative. Also, why bit-shift the Y coordinates and subtract the X coordinates from 8192? It turns out that to answer that question you have to go beyond the code listing in Wiki and look at the Spacewar! source code itself. For keen PDP-1 hackers who are interested in looking at the source code, the macro in Spacewar! that does this is called dislis. Some reading, tracing code execution with pen and paper, and a bit of intuition revealed that Y is initially in the range -512 to 512 because the display of the PDP-1 had a resolution of 1024 by 1024 with its origin at center of the screen. The reason that it's bit-shifted by 8 before being stored in memory is because the display instruction for the PDP-1 looked in bits 0-9 of its accumulator and IO register for the X and Y coordinates respectively and words on the PDP-1 were 18 bits long, so the bit shift puts the Y value in the correct location for display.

The X coordinates are a bit more involved. Spacewar! keeps track of where the rightmost edge of the screen is in world space and moves it along in the game's main loop, which creates the effect of the star field drifting across the screen. dislis examines each X coordinate to see whether it would be in the viewable region of the sky represented in the screen, and then converts the X coordinate from world space to screen space, bit-shifting it in the process to prepare it for a display instruction. The subtraction and also the ordering of the stars by X coordinate performed when the table of stars is generated lets dislis detect when it has reached a star that is beyond the viewable region displayed on the screen and do an early abort of its scan through the table, saving a few cycles.

Lastly if you look back at the Wiki article you can see that the coordinates are separated out into blocks, which it turns out divide the stars into brightness categories. The PDP-1's display was monochrome, but the brightness of any given point could be controlled programatically, a feature which Spacewar! uses quite a lot.

Now that we've worked out how to interpret this star field data we're using it in the port of Spacewar! that we're working on here at GAMBIT. Our device's screen won't be the same size, but we now that we know how the coordinate system worked in the original it will be easy to convert it to make it work for our display. Our next big challenge is reverse engineering the display code for the spaceships in Spacewar! Our initial investigation has revealed that Spacewar! generates the display code programmatically at runtime, but exactly how is a mystery that we're having fun solving.

right edge
bottom curves