Skip to content

About

A port of SDL Sopwith to the Casio Loopy game console

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

25 Commits

Folders and files

Repository files navigation

Loopy Sopwith

A port of SDL Sopwith to the Casio Loopy, the 1995 Japanese console built around a 16 MHz SH-1.

Sopwith is a side-scrolling biplane game from 1984, in which you bomb the enemy's buildings while fighting off their aircraft. This port runs on real hardware and under emulation.

Title screen Gameplay A printed high-score sticker on the console

Version 1.4, based on SDL Sopwith 2.9.0. Ported by Derek Quenneville techknight@techknight.com.

Controls

Action Button
Accelerate R
Decelerate L
Pull up Left
Pull down Right
Flip plane C
Fire gun A
Drop bomb B
Fly home D
Sound on/off Start

In menus: Up/Down move the cursor, A or Start selects, B goes back. The settings menu adds C to reset all settings and D to reset the high score table. On the title menu, R opens sticker printing — see below.

Setting a high score opens a name picker: Up/Down change the letter, A moves to the next one and saves after the third, B steps back.

Start toggles the sound during play, as the original's S key did — there is no setting for it, and it takes effect immediately. Up and Down have no function during play, and missiles and starbursts are not available in this build.

Sound

Sound is new in 1.2 and should be treated as a first attempt. It works, and it covers everything the original made a noise about — the engine, gunfire, the bomb whistle, a falling plane, hits, explosions and the "Wild Blue Yonder" title tune — but the voices were chosen by ear against a synth whose instrument list is undocumented, and there is plenty of room to do better.

It sounds considerably better on real hardware than under LoopyMSE. The emulator's synthesiser is a reverse-engineering of the real chip rather than a copy of it, so timbres, envelopes and the balance between voices all differ, and the emulator is harsher. Judge it on hardware; if you only have the emulator, expect it to be rougher than the console is.

If you would rather not have it, press Start during play.

Sticker printing

New in 1.4: the Loopy's built-in seal printer can print a high score. Press R on the title menu (the boxed printer icon on the title screen is the reminder), pick a pilot from the TOP PILOTS table with Up/Down and A, and a preview of the sticker appears: the pilot's initials and score, their medals and ribbons at full size, and the player's plane flying over the title screen's own terrain toward the ox. A prints it, B backs out.

Printing takes several seconds, and the console is fully occupied for the duration — the screen holds the sticker and the music pauses, resuming when the print is done. Standard XS-11 seal cassettes are supported (the four-up XS-14 reports itself as the same type and prints the same image as four small stickers). The XS-31 VHS-spine cassette is refused, as are a missing cassette, a jam, and an overheated print head, each with its own message; nothing is retried without another press of A.

Under LoopyMSE the print comes out as a print_*.png next to the ROM instead of on paper. On hardware, everything above is confirmed on a real printer and real seals.

Running

Download the ROMs from the Releases page, or build them yourself (below).

File Format
LoopySopwith.bin Native big-endian. Flash carts, LoopyMSE.
LoopySopwith-byteswapped.bin Byteswapped (16-bit words), for MAME and other tooling that expects that layout.

Both are 108 KB with the cartridge header checksum applied.

On hardware, the recommended flash cart is the Floopy Drive. For emulation, use LoopyMSE:

LoopyMSE.exe LoopySopwith.bin --bios <bios> --sound_bios <sound-bios>

The BIOS paths can also be set in loopymse.ini next to the executable, and -v enables the peripheral logging (serial, timers, VDP) that the capture harness relies on.

The Casio Loopy BIOS is not included here and is not needed to build.

Settings and high scores are saved to the cartridge's battery-backed SRAM.

Building

Requires a SuperH cross-compiler, plus python3 (ROM checksum) and dd (byteswapped image). The toolchain is Linux-only; on Windows, build in WSL2.

./scripts/setup-toolchain.sh    # installs the Wonderful Toolchain into ~/wonderful
./scripts/build.sh              # finds the toolchain and runs make

Or drive make directly:

make                                        # toolchain in /opt/wonderful
make WONDERFUL_TOOLCHAIN=$HOME/wonderful    # installed somewhere else

make TRACE=1 additionally enables boot checkpoint tracing (see platform/lp_platform.h), which writes marker values to a scratch VDP register that emulators log — useful when there is no working display yet.

scripts/capture.ps1 launches LoopyMSE with -v, optionally drives it with synthesised keypresses, and collects the emulator's native F10 screenshots. Point it at your own LoopyMSE install.

Layout

boot/       SH-1 startup, vector table, crt0   (from loopy-homebrew-template)
include/    Loopy hardware headers             (from loopy-homebrew-template)
tools/      Linker scripts, ROM checksum fixer (from loopy-homebrew-template)
src/        Sopwith game code, largely unmodified
platform/   The Loopy backend, replacing Sopwith's SDL layer
scripts/    Toolchain setup, build wrapper, emulator capture harness

How the port works

Sopwith's platform layer is small and well isolated: the game renders 4-colour (CGA) pixels into an 8-bit software framebuffer and calls a handful of Vid_* functions. That maps onto the Loopy almost directly, so nearly all the work is in platform/; src/ is upstream code with a small number of changes.

Video. The game draws into a 256x200 buffer in main RAM, which Vid_Update blits into the VDP's 8bpp bitmap layer once per field. The layer is configured 256 pixels wide so the buffer maps 1:1 onto VRAM and the blit is a single linear copy. Drawing into RAM rather than straight into VRAM matters because Sopwith's renderer is XOR-based — it reads back every pixel it touches, and read-modify-write over the VDP bus is far slower than against RAM. It also means the per-frame Vid_ClearBuf stays a plain memset, which is most of why drawing directly into VRAM would not actually save anything: the clear alone is the same 51,200 bytes the blit moves. Only palette entries 0-3 are used; the SDL build's palette table was carried over and converted to RGB555.

Page flipping. The bitmap layer has no base-address register — BM_SCROLLX and BM_SCROLLY are the only way to move the scanout origin, so a page flip is a scroll write rather than a pointer swap. BM_MODE_8BPP_SHARED presents VRAM as a 256x512 canvas, which is the entire 128 KB, so scrolling down by 256 rows selects between two 256x256 pages at byte 0 and byte 0x10000. Both were always there; nothing about the pixel format had to change to use them, and in particular 4bpp — the obvious way to make two pages fit — is not needed and would only add a nibble read-modify-write to every pixel the renderer touches.

This is what makes the presentation tear-free. Before it, Vid_Update waited for vblank and then started the copy, which cannot possibly finish inside the ~39-line blanking window — so it ran through active display, overwriting the exact memory the VDP was scanning out. Now the copy targets the page that is not on screen, and only the flip happens at vblank: one 16-bit store to BM_SCROLLY[0]. It has to be inside the blanking window, because the VDP re-reads that register every scanline and nothing latches it; a write that lands mid-frame is a raster split, not a flip.

Double buffering normally forces dirty-rectangle tracking, since the page coming back round is two frames stale. Not here — every Vid_Update blits the whole RAM buffer, so whichever page it targets is completely overwritten. That holds for the callers that draw incrementally (menus, text, high scores) too: they accumulate into the persistent RAM buffer and then blit all of it.

The blit. The SH7021's on-chip DMAC moves the frame, at roughly 7.5 MB/s against about 2 MB/s for a CPU copy. The configuration is the one the sibling LoopyDOOM port verified on hardware — 16-bit transfers to match the VDP's data bus, burst mode, auto-request — with one difference that matters. DOOM issues a burst per row because its rows are narrower than the VRAM stride; here the layout is 1:1 and the whole frame could go as one 25,600-word burst, which would be a mistake. Burst mode holds the bus until the count expires, so the CPU is stalled and interrupts cannot land, and one whole-frame burst is around 6.8 ms — three missed ticks of the 500 Hz ITU1 handler that is the MIDI byte clock and the gamepad sampler both. It is split into eight-row bursts (~0.27 ms) instead, comfortably inside one tick. make LP_BLIT_CPU=1 restores the CPU copy if the DMAC ever needs to be ruled out.

Per-pixel drawing. Vid_PlotPixel and Vid_XorPixel are static inline in src/video.h rather than functions in video.c. They are the innermost thing the game does, there is no LTO in the build to recover a cross-TU call, and the callers run out of uncached cart ROM. Two callers were not pixel plotting at all but rectangle fills written a pixel at a time — clrprmpt() at 11,264 calls and colorscreen() at 46,336, the latter once per frame while an ox is splatted. Both now go through Vid_Box, which is a memset per row.

Profiling. make PROFILE=1 puts a frame-time breakdown on screen: draw, vblank wait, blit, and the last title-screen redraw. Resolution is one 2 ms ITU tick, so spans are accumulated over 64 frames and divided at the end; reading TCNT for the sub-tick remainder would be finer but LoopyMSE asserts on any 16-bit TCNT read (writes are emulated). Note that the blit figure reads as 0.0 ms under emulation — LoopyMSE performs a DMA transfer instantaneously inside the register write — and that the emulator does not model the cart ROM fetch penalty either. Both numbers only mean something on hardware.

Screen size. Sopwith is a 320x200 game and the Loopy displays 256x224. SCR_WDTH is set to 256 and the 200-line playfield kept as-is (MAX_Y and the status bar layout both depend on it), centred vertically. The narrower screen means 32 text columns instead of 40, so the title screen, menus and the beginner's help overlay have Loopy-specific layouts.

Timing. Time comes from an SH7021 ITU channel, free-running and cleared on compare match every 2 ms, with the handler counting ticks. This started out as a simple count of video fields, which was wrong in a way that took a stopwatch to catch: such a clock only advances when something waits for vblank, so all the time spent drawing a frame is invisible to it. The game paced its 10 Hz tic against that clock, so every tic cost 100 ms of waiting plus the render time, and the game ran about 1.75x slower than intended — while an on-screen frame counter, measured against the same clock, insisted everything was fine.

Channel 1 specifically: ITU0 is the BIOS's, for BGM. Channels 2-4 have their interrupt priorities in IPRD, which older LoopyMSE builds did not implement (current ones do), so channel 1 was the only choice that worked everywhere and is proven on hardware. The 2 ms period is the sound backend's requirement, not the clock's — see below; the gamepad is still sampled at the 100 Hz it was tuned for, on every fifth tick.

Sound. New in 1.2, and a first attempt — the mapping below is sound engineering, but the choice of instruments is guesswork against an undocumented voice list, and anyone with a better ear will improve on it. The notes here are meant to make that easy.

Sopwith's audio model is a PC speaker: it asks for a square wave at an arbitrary frequency. The Loopy's is a NEC uPD937 wavetable synth that only speaks MIDI, over the SH-1's second serial port at 31250 baud — the BIOS handshake wakes it up, and after that the port is ours. platform/lp_sound.c converts each requested frequency into a MIDI note, with a table of the 61 notes the chip plays at true pitch rather than a division the SH-1 cannot do cheaply.

The interesting half is below 65 Hz. The synth folds notes that low back up by octaves instead of clamping them, so Sopwith's 19–42 Hz engine and hit sounds cannot be played as notes at all — asked for a rumble, the chip returns a tune. They are reproduced as a retriggered percussive pulse train at the same rate instead, which is what a 20 Hz square wave on a PC speaker really is, and where the engine's putt-putt comes from in the first place. That is also why the ITU tick is 2 ms: at 10 ms, three of the plane's speeds quantise to the same pulse rate and the throttle stops being audible. The tick doubles as the MIDI byte clock, which lets the interrupt handler write one byte per tick and never wait on TDRE — a wait that cannot work under emulation anyway.

Three of the chip's four channels are used, and they are not interchangeable. Channel 0 is the only one that bypasses the volume sliders, so it carries the music; channel 3 is the only one with a slider to itself, so it carries the engine and can be turned down without taking everything else with it; gunfire gets channel 1 of its own, because the frequency alone cannot identify it — a falling plane passes through the machine gun's exactly once on its way down — so swsound.c labels each request as it is made.

Volume is the chip's weakest point: no per-note velocity, no volume controller, and two shared sliders whose quietest setting is about 63%. The engine level is therefore built out of what does work. Its one reliable ingredient is a gate — each strike can be released after a few ticks instead of ringing until the next one, cutting into the volume envelope before it has finished rising, which at 20 Hz saves 50 ms of decay per hit. Each level also carries a note-on velocity and a volume controller; the emulator's synth ignores both, but the real chip may not, and they cost four bytes to try.

The instrument list is undocumented and not General MIDI, and the emulator's copy of it is a reverse-engineering, so the three voices and the level were chosen by ear on hardware — which is also why the result sounds noticeably better there than under LoopyMSE. The shortlists they were chosen from are still in platform/lp_sound.c so the comparison can be redone without hunting for the numbers again: make SNDDEFS='-DLPSND_TONE_VOICE=1 -DLPSND_ENGINE_LEVEL=3'.

The title music is pumped from inside the screen redraw as well as from the menu loop. A title-screen redraw used to be upwards of 100 ms on hardware — some 16,000 single-pixel calls out of uncached cart ROM — which is nearly two sound ticks, and without those extra pumps the anthem holds a note for twice its length and then rushes the backlog, heard as a note played too long followed by a missing one. Inlining the plot and turning clrprmpt() into a memset took two thirds of those calls out (see Per-pixel drawing above), so the pumps inside the fills themselves are gone; the ones around the rest of the redraw stay, and PROFILE=1 reports the redraw time as T so the margin can be checked rather than assumed. For the same reason the menu redraws only when its background actually changes rather than on a 250 ms timer, which was firing about thirty redraws for every one that painted anything different. Neither shows up under emulation, which does not model the ROM fetch penalty.

Persistence. Settings and the high score table go into the cartridge's battery-backed SRAM instead of config files. Blocks are tagged with a magic value and a length, so a fresh cart — or a build whose tables changed size — reads as empty rather than as garbage.

Status bar. The minimap is 158 pixels wide for the built-in level, which does not fit beside the gauges at their original position on a 256-pixel screen. The map is right-aligned against the screen edge and the gauges moved with it, which leaves the bar as: score at x 0-47, medals at 49-74, ribbons at 50-73, gauges at 78-93, map at 98-255.

Medals and ribbons were dropped from the in-game bar for a while on the grounds that the gauges needed the space. They did not — that was an arithmetic error, and the two were never in contact. They are drawn during play again, with three pixels between the last medal and the first gauge. Worth knowing if the layout is ever touched again: the medal extent is the worst case over every award order, and dispmedals() has to stay after dispscore(), whose Vid_Box clears x 0-79 across the whole bar every frame. The Medals setting still controls the high score table as well.

Dropped. Network play (no transport), custom mission files (no filesystem and no command line), missiles and starbursts, and the key-bindings menu. That last one is deliberate: it waits for a keypress to assign, and with no keyboard there would be no way out of that screen.

Notes for anyone extending this

Three things cost real debugging time and are worth knowing about.

The VDP struct must be volatile. Without it the compiler merges two adjacent 16-bit register stores into one 32-bit store, and the VDP decodes register writes by access width — a merged write is a different operation entirely. This shows up as an emulator assert, or nothing at all on hardware.

picolibc's default printf is a cut-down variant in this toolchain: it ignores field widths and drops %s, so every formatted string came out one character long, which looked like a rendering bug rather than a libc one. platform/lp_printf.c supplies the printf family instead; picolibc still provides string/memory routines and malloc.

A clock derived from the frame loop lies about time. The port originally counted video fields, so Timer_GetMS() only moved when something waited for vblank. That caused two bugs a long way apart: a busy-wait spinning on Timer_GetMS() in the end-of-level countdown deadlocked outright, and the game's own pacing ran ~1.75x slow because render time was never counted. Both are fixed by the hardware clock, but the general lesson stands — do not measure a system with a clock that only ticks when that system is idle. Note that an FPS counter divided by such a clock cannot detect the problem: the error cancels, and it always reports the expected number.

Credits and licensing

The Casio Loopy port is by Derek Quenneville techknight@techknight.com, written with substantial help from Claude Code. The hardware debugging, design decisions and testing on real hardware are mine.

Sopwith is by BMB Compuscience and David L. Clark (1984), and has been maintained since 2001 by Simon Howard, Jesse Smith and contributors. The SH-1 startup code, hardware headers and linker scripts come from Kasami's loopy-homebrew-template.

This project as a whole is under the GNU GPL, version 2 or later (COPYING.md). The vendored template files are under the zlib licence (LICENSE-loopy-template.txt).

See NOTICE.md for the full provenance breakdown and the list of modified files.

About

A port of SDL Sopwith to the Casio Loopy game console

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages