Z80 Sans – a disassembler in a font (2024)
Z80 Sans is a fascinating OpenType font that ingeniously disassembles Z80 machine code by converting hexadecimal sequences into human-readable assembly instructions. This project highlights an incredibly creative and deep dive into the obscure capabilities of font technologies, pushing the boundaries of what's possible with glyph substitution and positioning. It's a prime example of a 'cursed font' that delights the Hacker News crowd with its sheer technical cleverness and unconventional approach to a low-level problem.
The Lowdown
The 'Z80 Sans' project introduces an OpenType font that performs Z80 instruction disassembly directly within text rendering, a truly novel and technically demanding endeavor. Instead of needing a separate tool, one can simply type hexadecimal Z80 opcodes, and the font automatically displays the corresponding assembly instructions.
Here are the core details of this impressive technical achievement:
- Core Functionality: The font converts sequences of lowercase hexadecimal characters into Z80 assembly mnemonics, operands, and addresses.
- Underlying Technology: This is accomplished through extensive and complex utilization of OpenType's Glyph Substitution Table (GSUB) and Glyph Positioning Table (GPOS).
- Design Challenges: The author tackled significant hurdles, including defining substitution rules for potentially hundreds of thousands of instruction combinations, handling out-of-order operands (where hex bytes appear in a different order than their disassembled representation), managing little-endian addresses, and correctly rendering signed two's-complement offsets.
- Programmatic Approach: Due to the immense complexity, a programmatic solution was essential. A recursive descent parser was used to generate all possible glyphs, evaluating expressions in encodings.
- Implementation Strategy: The author directly edited
.ttx(XML representation of OpenType fonts) files, circumventing the limitations of high-level.feaformat, and utilized contextual chaining rules for intricate scenarios like out-of-order operands and addresses. - Development Stack: The project relies on tools like FontForge, FontCustom, ImageMagick, and Python's fonttools for its construction process.
- Known Limitations: Some minor rendering glitches exist for specific instructions, and the author humorously describes the code quality as "CTF quality."
- Future Considerations: The author notes that scriptable FontForge features or font shapers might offer alternative, less constrained approaches for more complex instruction sets in future work.
Ultimately, Z80 Sans is a testament to creative problem-solving and deep technical mastery, showcasing an unexpected and highly specialized application of font technology to reverse engineering and low-level computing.