Bringing Swift to the Apple II - YKM's Corner on the Web
English (en)
简体中文 (zh)
Maker, Coder, Private Pilot, Retrocomputing Enthusiast
Swift is the modern programming language behind many apps that run on modern Apple platforms. I thought it would be fun to bring a small taste of it back to Apple’s early days, the Apple II. It was Apple’s first mass-market series of machines, initially released in 1977 with a 1 MHz MOS 6502 CPU.
I built SwiftII , a Swift-flavored mini development environment for the original Apple II to a //e and up.
To be honest, I built it with heavy AI assistance in my spare time. I will be candid later how it helped because that workflow was as interesting to me as the retrocomputing work itself.
One big caveat, this is not modern Swift. It could never be. The Swift full standard library will not fit on this machine. SwiftII is a deliberate subset, much closer in spirit to Embedded Swift than the SDK meant for modern platforms like iOS or macOS.
My goal was to fit as much of Swift into the Apple II as I can while still reading like Swift on sight. If you know Swift, you should be able to read a SwiftII-compatible program and immediately understand it.
Other than the interpreter, I want my user to still have a relatively good user/developer experience. Because of this, I also had to build a launcher, file selector and text editor due to limitations I encountered on the Apple II.
Contents
This post is quite long, so here are the main sections for you to jump straight to:
The App
Motivation
Target Hardware
The language
The main constraint: 40,704 bytes
Development setup
How the language works
No floating point at all
Two families of binary
“What you type” vs “what you see” vs “what is stored”
Typing Swift on a 1977 keyboard for II and II+
Text output on the Apple II
Developing Swift on the machine itself
The trouble with memory banking
Why this project ships as 8x disks
Testing too many machines
How it was built with AI
The documentation as the AI’s memory
Try it
Conclusion
The App
Boot the appropriate disk and you land in a launcher. You can select an interactive REPL, a file browser that runs .swift programs straight from the disk, a full-screen editor, all self-contained on one bootable floppy.
Here is a demo video of the app running on my original Apple II Plus.
It goes through booting the system, running some sample programs and usage of the file selector, editor, REPL and compiler. The compiler is on a separate disk from the REPL.
The original II Plus keyboard has no lowercase and no \ key, so the program is typed with digraphs. ??/ becomes \ which SwiftII interprets as canonical Swift. More on that quirk later.
Here are some sample screenshots:
The boot launcher menu
The full-screen editor (//e, 80-column)
The file browser with a live code preview
xsnake, a playable light-cycle game
All 16 lo-res colours via gr / color / vlin
xgrdemo, a five-scene lo-res colour graphics showcase
If you are too excited to continue reading and want to dive into the code and disk images, here is the GitHub repo https://github.com/yeokm1/swiftii.
Motivation
I recently restored an Apple II Plus that was generously donated to me. I wondered what modern abilities I can make it do.
Years ago, I wrote a DOS ChatGPT client and a Slack client for Windows 3.1. Those projects were about squeezing a modern network service into a vintage machine. This time I wanted to know if I can do the same for a modern programming language, one from Apple itself no less!
The inspiration for this app is Apple Pascal. Back in 1979, Apple Pascal brought the UCSD p-System to the Apple II. Rather than compile Pascal to native 6502 machine code, it compiled to bytecode that ran on a virtual machine similar to how Java does it.
I wanted to do the same for Swift. The compiler emits bytecode and a virtual machine executes it. Almost half a century apart, both languages reach the 6502 by avoiding native 6502 code generation.
I also wanted a REPL too if some users want to test their code in an interactive manner.
Target Hardware
The baseline target is the original 1977 Apple II with appropriate hardware upgrades applied. If SwiftII runs here, it runs on pretty much any future Apple II.
The machine in the photo above is a 1979 Apple II Plus, an incremental iteration over the original Apple II with features like Applesoft BASIC, more RAM and disk Autostart. Otherwise, it is largely similar to the original Apple II.
My II Plus has the following high-level specifications:
MOS 6502 CPU 1 MHz
48 KB main RAM
Clone Saturn 128K RAM card (backward compatible as 16K Language Card)
Clone Videx Videoterm 80-column card
Yellowstone universal disk controller card (can emulate original Disk II mode)
Floppy Emu to emulate Disk II drives
More details on my own Apple II Plus setup are documented here:...