The Fantástico Laberinto box from The Boy Who Opened Boxes.
In 1998, my first year at the Instituto Tecnológico de Morelia, the final project for Programming I was supposed to be a program. I handed in a game: Fantástico Laberinto (“Fantastic Labyrinth”, Flab for short), with a title screen, a pencil-sketch menu, mouse and keyboard control, combat, saving and loading, and a user manual nobody had asked for. It took the better part of a semester of demos before a few professors accepted I had written it. Much of that convincing happened after hours, in the office of my professor, Mónica Blancas, where I sat with her going through the source and explaining a bit of everything.
It was my first shot at a real game, simple as it is, and it was a labor. It took night after night of figuring out how sprites actually work, mostly from one book, André LaMothe’s Tricks of the Game Programming Gurus, and I learned a lot from it. And every time something finally worked, I couldn’t go to sleep. It ended up as one C file of 3,189 lines, and this year I read every one of them again.

A planet with three moons
Section titled “A planet with three moons”The manual tells the story, and I wrote the story before the game had much of one. On Tuuron, a small planet with three moons and few oceans, life depends on a stone of power fed by four elemental gems. The legend says that whoever pries the gems loose for the first time, with a pure heart, may ask for anything in exchange for putting them back. A false wizard named Hantrox pried them loose wanting more than one wish, and found each gem cursed in its own way. The green one weighed 35 kilos by the middle of the labyrinth. The blue one slipped through his fingers at the 356th step. The red one grew too hot to hold before he managed 67. The white one vanished into the air at 47, and fell back on his head two minutes later. Furious, he spellbound everyone inside the labyrinth into guardians and died without ever finding a cure. You arrive with three years of the planet’s life left, to put the gems back.
I made the art together with my friend from high school, Cesar Romero, who drew by hand and was really good at it: the title’s green stone letters over a castle, the sepia sketches behind the menu, the creatures, the forest.

The manual is earnest the way only a first manual can be. It lists the system requirements (a 486, MS-DOS 5, 525K of free conventional memory, a VESA card with 512K of video memory), walks you through an installer, and admits that “the help button is disabled in this version”. Its troubleshooting section ends with my favorite entry. Problem: I cannot finish the game. Answer: You are not trying hard enough. Solution: Play Fantástico Laberinto longer.
My first players were my brothers, Jesús and Daniel, who tested it by playing it while I was still building it. Daniel, the youngest, played the most (he always had a thing for video games and screens), and the game’s last words are for him. Put the four gems back, and the screen says “El juego ha sido terminado Exitosamente!!! Daniel es chido” (“The game has been finished successfully!!! Daniel is cool”).
Five banks per screen
Section titled “Five banks per screen”The game runs in VESA mode 101h, 640 by 480 pixels in 256 colors. That is 300K of pixels,
and a real-mode DOS program sees the video card through a 64K window, so every screen is
drawn in five pieces, switching the card’s bank between them by hand.
A screen row is 640 bytes, which doesn’t divide 64K evenly, so some rows start in one bank and end in the next. The part I still like best is how it finds a pixel without multiplying. At startup it builds a table with the bank and offset where every row begins, and the pixel’s address is then one add for the column and one add-with-carry for the bank: the carry is the row crossing into the next bank.
The palette is laid out in brightness ramps, so lighting is arithmetic. A dark room is the same tiles with a number subtracted from every color, the font is shaded into a gradient by adding the row, frames are beveled by adding to whatever is under them, and enemies are recolored the way NES games did it, by adding a random offset to their colors.
Under that, the game takes over the machine. It replaces the keyboard interrupt (INT 9)
with its own, so it can see every key held at once, and the clock interrupt (INT 1Ch) to
pace itself, and it fades the palette in and out through the VGA’s color registers. Its art
lives in raw 640 by 480 dumps with the palette appended, and creatures move without flicker
because each move restores only the pixels its next frame won’t cover.


The banner on Doom
Section titled “The banner on Doom”All that art had a price. In real mode a program gets 640K of memory, minus whatever DOS and the drivers take, and the graphics ate nearly all of it: the back buffer alone is 300K, more than half of everything the game allocates. There was nothing left for sound, or for most of what I wanted to add next. That is why it has none.
The fix had been in front of me all along. Every time I started Doom I saw a small banner, DOS/4GW, and wondered what it was. I didn’t have a telephone line yet, so there was no Internet to ask. When I finally could, I learned that id had built Doom with Watcom C and a DOS extender, Rational Systems’ DOS/4GW, which runs a program in protected mode with all the memory the machine has. I was thrilled, and a little stung, because I had paid for that 640K ceiling in this game and in nearly everything else I had written.
Nobody at school had Watcom, and most of the people I asked had never heard of it. From the day I got it, I wanted it everywhere, which is why Emu6802 runs in protected mode. It came a little late. Most of my school projects were done by then, and Watcom itself was on its way out.
Reading it again
Section titled “Reading it again”Some of it I would still sign: the bank table, the palette, the flicker-free moves. Then there is the rest.
The bank table itself is allocated with farmalloc(960), for 480 rows of two entries each.
But the entries are ints, so the table needs 1,920 bytes. I counted entries, not bytes, and
C was happy to let me. It never crashed because the overrun lands at the start of the next
block, the sprite sheet, whose first frame is a blank I had reserved “for future expansion”.
Twenty-eight years of pure luck.
The screenshot key, F12, runs the whole capture (gray the screen, write a BMP through DOS, fade back in over 64 steps) inside the keyboard interrupt handler. Press it while a frame is being copied to the screen and a band of the picture comes out as garbage, and DOS was never meant to be called from in there at all. Today the handler would set a flag, and the main loop would do the work. Back then I had never seen anyone else’s interrupt handler: I knew how to install one, and not how little it should do, something that took me many more years to learn, the hard way.
Every room starts with “Espere por favor… Dibujando cuarto…” (“Please wait… drawing
room…”), and the source says why. PutSprite works out which kind of tile it is drawing
with about 45 if (tipo == N) tests for each of a tile’s 1,024 pixels, around 40 million
comparisons per room, when deciding once per tile would have made the message pointless. You
can see how it grew, one more if for each new kind of tile. The map’s address formula,
(cuarto<<9)+(cuarto<<6), a room number times 576 spelled as shifts because I had read that
multiplying was slow, appears 66 times instead of living in one small function.
There is even a bug you can feel in play. Enter makes the hero attack a neighbor, searching the eight cells around him, starting with the one beside the direction he faces. The wrap-around in that search skips exactly one cell, the one he is facing. Enter hits anyone standing next to you, except the enemy you are looking at.
And a few things only the source tells. The code that keeps two creatures out of the same
cell explains that two sprites in one place “violates the Pauli exclusion principle”. The
help function behind that disabled button, Ayuda(), is empty. Holding Tab leaves whatever
you are fighting with one point of energy, a cheat that never made it into the manual. Most of
what is wrong here comes from the same place: it was my first program longer than a few
hundred lines, written alone and at night, and nobody read it but me.
Against 1998 instead of today, the picture shifts. The books I learned from, LaMothe’s included, mostly taught 320 by 200 in 256 colors, a mode where the whole screen fits in one 64K window and none of the bank juggling exists. Going to 640 by 480 meant working past the book, and the bank table is where I did it. The 640K wall is the other side of that coin: it was already avoidable, and not only with Watcom. DJGPP, a free 32-bit C compiler for DOS, was out, and so was Allegro, a free game library built on it. I didn’t know either existed. What I was missing that year wasn’t skill, it was a telephone line.
One instruction
Section titled “One instruction”To run it today in DOSBox, the game needed exactly one
fix. Right after switching to mode 101h, it checks that the card can switch banks by calling
VESA function 4F05h, without ever loading DX, the register that says which bank. The VESA
BIOSes of 1998 shrugged at whatever happened to be there. DOSBox does not, and the game quit
at startup claiming my video card wasn’t supported.
The source fix is one mov. The executable got the same fix as a patch of the same size, since
the Borland compiler that built it is long gone: mov bx,0 became xor bx,bx followed by
cwd, an instruction that fills DX with the sign of AX, which at that moment holds
4F05h, a positive number. So DX comes out zero, three bytes for three bytes. I think the
kid who wrote it would have liked that trick.
The game, its source, the manual (in Spanish) and the fix are on GitHub, at Kronuz/Flab, and on its project page.