Sir Box-a-Lot: an 8-bit Sokoban clone

Introduction

Sir Box-a-Lot is a handheld, hackable 8-bit Sokoban clone implemented from scratch for the retro-modern AVR Dx series microcontroller. The hardware costs around $50 to assemble. The implementation features:

The assembled circuit looks like this:

Note that you don't have to use a custom PCB; plans are provided for assembly on a generic perfboard (image). You also don't need to stick to Sokoban: you can use the circuit to develop any other game that's playable with a joystick and two auxiliary buttons.

Downloadable archive

Click on the button below to download an offline archive containing detailed schematics, source code, parts list, assembly instructions, and Gerber files for PCB manufacturing. If you're on Windows, you will need a tool like 7-Zip to extract the contents.

The archive contains the following files:

Sourcing / building PCBs

The most expedient approach is to build the circuit by hand on a 7x8 cm perfboard based on the supplied schematics:

For a more professional look, you can send gerber-files.zip (see above) to a PCB vendor such as JLCPCB, OSH Park, or Digi-Key Red. The result should look something like this:

In the latter case, expect to pay $20-$40 for the boards and to wait 1-2 weeks for the order to arrive. Since there's usually a 3-5 board minimum and given that shipping costs are essentially fixed, it's best to split the order with some friends.

If taking the custom PCB route, default manufacturing settings are fine. You can choose a different board solder mask color or thickness. I recommend paying for lead-free HASL or ENIG finish, as the board will be handled extensively.

Bill of materials

In addition to a standard soldering workshop, the following parts and supplies are necessary to assemble the circuit:

The UPDI connector is optional: for one-time programming, you can simply jam some breadboard jumper wires into the plated holes on the PCB.

If building on a perfboard, you will probably need a TQFP-48 / QFP-48 breakout board for the MCU (example); and a SOIC-8 one for EEPROM (example).

Component availability & substitutions

All electronic components should be nominally available from Mouser and Digi-Key; your total for the order should be around $50. That said, especially given the ongoing supply chain issues, here are some suggestions for substitutes:

A "commercial" version of this circuit would probably feature a couple of changes. First, to reduce costs, the OLED module could be replaced with a $15 version without a bezel or a breakout board (link); in this case, an additional chip - maybe AP3032KTR - would be needed to generate 14 V for the LEDs. Another modification would be reverse polarity protection - perhaps an n-channel MOSFET such as DMG2302UK on the GND side.

Assembly walkthrough

Please be realistic about your soldering skills and seek help if needed. The project isn't particularly complex, but it requires soldering a single TQFP-48 chip with 0.5 mm lead spacing. If you have no experience with fine-pitch surface mount chips, you will mess up your first attempt:

With this disclaimer out of the way, here's the recommended sequence of assembly steps:

  1. Solder both SMD chips, noting their orientation. The MCU has a recessed circle near pin 1. EEPROM has a chamfered long edge on the side with pin 1. Etched text on the package is not indicative of mounting direction.

  2. Solder 2x10 pin header onto the back of the OLED module. Don't solder it to the main PCB yet, and don't remove the protective film until you're done with all the remaining assembly steps.

  3. Trim, strip, and tin battery holder wires to allow attachment to nearby pads on the back of the PCB. You can allow some slack, just keep the wires short enough so that they don't snag.

  4. Use hot melt glue or silicone caulk to attach battery holder to the rectangular region marked in the rear of the PCB. Allow glue to set.

  5. Grab the speaker with tweezers, with the recessed side pointing up, then carefully apply cyanoacrylate ("instant") glue only to the bezel. Keep the glue off the membrane. Place the speaker in the marked position on the front of the PCB, membrane facing down and wires pointing toward the capacitors. Press gently and allow the glue to set.

  6. Route speaker wires through nearby mechanical holes on the board, trim and strip the leads to allow attachment to pads on the back. Again, some slack is OK, just keep them short enough to avoid snags.

  7. Neatly solder battery wires and speaker wires to pads.

  8. Mount and solder switches, joystick, UPDI connector, and capacitors. Trim leads with flush cutters when done. Observe the polarity for large caps. Longer lead is positive; printed marking on top or on the side is on the negative side.

    (If you want no pointy protrusions on the back of the PCB, the trick is to trim all leads flush with the board before soldering. With good soldering technique, this should leave the board silk smooth.)

  9. Optionally clean up soldering flux residues with isopropyl alcohol and an unwanted toothbrush, then apply conformal coating to the exposed solder joints.

  10. Install the OLED module using four short PCB standoffs. If you don't have any suitable spacers, you can use a gob of hot melt glue underneath for support. Either way, make sure that the header is sticking through.

  11. Solder the OLED header to the board.

  12. Fit the joystick cap. You will need to gently shave the sides of the joystick with a sharp knife or a small file. Press the cap into place; if it's not snug, apply a touch of cyanoacrylate glue, but make sure that the glue doesn't drip down the shaft. It's safer to hold the board upside down for this.

Here's a clip showing most of the assembly steps for the AVR128DB28 variant:

Compiling the source (optional)

AVR toolchain is required to build the source code; this can be the Microchip Studio IDE on Windows, or avr-gcc on Linux or MacOS X.

If you're on Windows, download the latest Microchip Studio, then go to File > Open > Project / Solution and select sir-box-a-lot2.cproj from this package. Click on the Debug drop-down in the toolbar and select Release. If you're using the AVR128DB48 MCU, you can then hit F7 to compile. If you're using AVR128DA48, press Alt-F7, go to the Device tab, and click Change Device... first.

On Linux or MacOS X, please download the latest avr-gcc / gcc-avr and avr-libc packages for your system, as the chips are relatively new. The command to compile the source code is:

avr-gcc -O3 -mmcu=avr128db48 sir-box-a-lot2.c -o sir-box-a-lot2.elf

Substitute avr128db48 with avr128da48 if using the DA series chip.

If you get an error about a missing device-specs/specs-avr128db48 file, you can download and unzip this device pack from Microchip (despite the extension, it's just a regular zip file). After that, find the device-specs/ directory on your system (probably somewhere in /usr) and copy the following files from the archive you just downloaded:

./gcc/dev/avr128db48/device-specs/specs-avr128db64
./gcc/dev/avr128da48/device-specs/specs-avr128da64

Next, find a directory on your system containing libatxmega64d4.a (again, likely in /usr); and copy the following files from the DFP to that location:

./gcc/dev/avr128db48/avrxmega4/crtavr128db48.o
./gcc/dev/avr128da48/avrxmega4/crtavr128da48.o
./gcc/dev/avr128db48/avrxmega4/libavr128db48.a
./gcc/dev/avr128da48/avrxmega4/libavr128da48.a

Finally, find a directory on your system containing iox64d4.h (possible location as before); and copy the following files to there:

./include/avr/ioavr64db48.h
./include/avr/ioavr64da48.h

If you did this correctly, you should now be able to compile the source code.

If you have no experience or no luck with the AVR toolchain, you can also use prebuilt binaries included with this archive (binaries/*.elf) and skip directly to the programming step.

Device programming

An UPDI-capable programmer is required to send code to the chip. The canonical option is ATMEL-ICE, but it is moderately pricey (~$150, link). Cheaper options include PICkit 4 or MPLAB SNAP (see below); as well as cheap third-party clones such as this.

The programmer can be operated via Microchip Studio on Windows, or via tools such as avrdude or pymcuprog on Linux and MacOS X. If you have Microchip Studio installed and if you're using ATMEL-ICE, you can just open cmd.exe, go to the directory containing sir-box-a-lot2.elf, and paste this command:

"C:\Program Files (x86)\Atmel\studio\7.0\atbackend\atprogram.exe" -t atmelice -i updi -d avr128db48 program -f sir-box-a-lot2.elf

Again, substitute avr128db48 for avr128da48 if using the DA series chip.

Alternatively, you can access the programming UI from Microchip Studio via Ctrl+Shift+P, select AVR128DB48 or AVR128DA48 as the MCU, and go to the Memories tab to select an ELF binary.

If using avrdude on Linux, the usual command line for ATMEL-ICE is:

avrdude -p avr128db48 -c atmelice_updi -U flash:w:sir-box-a-lot2.elf

If you get an error along the lines of "avrdude was compiled without USB or HIDAPI support", you should get angry at the maintainers of your Linux distro, uninstall the package, and build the tool by hand.

In regard to PICkit and SNAP programmers, I have no first-hand experience, but Rafael Martins notes:

"You can use a PicKit4 or SNAP AVR mode to send code to AVRs these days. They are cheaper than ATMEL-ICE [...] To enable AVR mode: plug the device, open MPLab IPE / IDE / Microchip Studio and try to run any operation in an AVR device (something like reading signature is enough). just doing that will change the tool to AVR mode, and now [...] either avrdude or pymcuprog will work just fine."

During programming, you might want to optionally configure the brown-out detector (BOD) fuse on the MCU. BOD can be set to 1.9V. Without BOD turned on, the display may appear glitchy when the battery is dead. BOD makes shutdown more graceful.

Troubleshooting

Here's a quick checklist for common circuit issues:

Game tips & tricks

Here's some useful advice for players:

Further reading

This article discusses some of the software challenges involved in designing an earlier prototype of Sir Box-a-Lot.

If you'd like to learn more about the SPI EEPROM interface, it is very similar to the SRAM interface discussed in this introductory article.

For more information about the OLED interface and the custom font used by Sir Box-a-Lot, follow this link.

If you're interested in an introduction to custom PCB design, click here.

Finally, some of my earlier 8-bit games can be found here. You might also want to check out my later project, Bob the Cat.

Bug reports, patches, questions...

If you have any questions, suggestions, or feedback, contact me at lcamtuf@coredump.cx. For other projects, you can also follow me on Mastodon or Twitter, or subscribe on Substack. And if you end up building the circuit, please send me a photo!

Your lucky number is: 24215484