Developing modern software for vintage hardware is a masterclass in constraint-driven engineering. The original Sony PSP features a 333MHz CPU and only 32MB of available RAM. Creating a native EPUB book reader that handles large files, renders clean anti-aliased text, and preserves battery life required strict, low-level optimization.
The Rendering Engine: Custom Text Layouts on SDL2
I used C++ and SDL2 for the core graphics layer. Because rendering full EPUB layouts on-the-fly is too CPU-heavy for the PSP, I implemented a custom text layout compiler. It parses the XHTML structure, extracts formatting, calculates line breaks based on the screen resolution (480x272), and caches the parsed token stream in memory. This avoids expensive re-layouts during page flips.
Memory Constrained Architecture
To fit books with hundreds of pages into memory, I built a segmented buffer. Instead of holding the entire document structure in RAM, we only cache the active chapter's token stream and pre-compile the adjacent page images. All resource allocations use custom memory pools to prevent RAM fragmentation, which is a common cause of crashes on embedded systems.
Custom Memory Allocators to Avoid Fragmentation
During long reading sessions, default malloc and free calls fragment the PSP's scarce address space, leading to out-of-memory crashes. To solve this, I wrote a custom Arena Allocator that is reset completely between chapter loads. Since all paragraph nodes, word tokens, and temporary layout coordinates are allocated sequentially and freed in a single operation when the user switches chapters, memory fragmentation is effectively reduced to zero.
Double-Buffered Page Layout Pre-Rendering
To eliminate any visible rendering latency, I implemented an off-screen double-buffering queue. While the user is reading page N, a background low-priority CPU thread compiles layout calculations and rasterizes glyphs for pages N-1 and N+1 into a private off-screen render buffer. When the user clicks the trigger to turn the page, the graphics engine simply swaps the hardware display pointers, rendering the transitions instantly without blocking the interface.
- Off-Screen Framebuffers: Utilizing the PSP's internal graphics processor VRAM directly for off-screen page layout caching.
- Tokenized Layout Engines: I pre-tokenize CSS formatting properties to minimize runtime string matching and CPU calculations.
Optimizing Image Decoding with Hardware Assembly Helpers
EPUB files frequently contain inline illustrations. Loading and decoding full-scale PNG images with standard libraries is slow on a 333MHz CPU. I bypassed libpng and wrote a custom lightweight image decoder that utilizes the PSP's Vector Floating Point Unit (VFPU) to process pixel color conversions in parallel. This optimization cut image decoding times from 500ms down to 75ms.
Dynamic CPU Scaling for Battery Life
- CPU Governor: The PSP CPU can scale from 19MHz to 333MHz. I programmatically scale the clock down to 111MHz when the user is reading static text, and scale up to 333MHz only during page transitions.
- TATE Orientation: rotating the viewport 90 degrees allows users to hold the console vertically, increasing text line capacity.
- Custom Font Caching: caching glyphs as texture sub-images to minimize SDL GPU rendering calls.
“Programming under severe constraints forces you to think about every CPU cycle and byte allocation, returning to the fundamentals of software craftsmanship.”
“When memory is measured in kilobytes, high-level abstractions dissolve and you are left with the beautiful simplicity of pointers and registers.”
Manual memory pools require strict lifetime checks: failing to track pointers correctly on PSP leads to hard hardware exceptions (bus errors) rather than simple developer console warnings.
The resulting reader launches in under 2 seconds and extends PSP battery life by over 30% compared to standard homebrew readers.
Danya Yudin (Sabraman)
Designer & Frontend Engineer
Design-led frontend and product developer specializing in web applications, interface design, and Telegram systems.