• Stars
    star
    287
  • Rank 144,232 (Top 3 %)
  • Language Slash
  • License
    The Unlicense
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A homebrew 16-bit CPU with a homebrew Unix-like-ish operating system.

SCAMP CPU

SCAMP is my homebrew 16-bit CPU. It stands for something like "Simple Computing and Arithmetic Microcoded Processor".

The CPU is very primitive. It doesn't support interrupts, and has no MMU, and no concept of privilege levels. It currently runs at 1 MHz, above that the CompactFlash interface falls over. It runs a homebrew operating system, with a homebrew programming language and compiler, and can self-host the kernel and all of the system utilities. The environment tastes a bit like an early Unix, but works like CP/M. I used the computer to complete almost all of Advent of Code 2021.

My Advent of Code solutions are here: https://github.com/jes/aoc2021

I have some blog posts here: https://incoherency.co.uk/blog/tags/cpu.html and a web-based emulator available here: https://incoherency.co.uk/scamp/

Here are some pictures:

And here's a video with some explanation and an example session on real hardware:

Architecture

It is a 16-bit CPU. The bus is 16-bit, registers are 16-bit, addresses are 16-bit, and memory contents are 16-bit. The upper 8 bits of an instruction select the opcode, and the lower 8 bits are available for small immediate values. There is no support for: cache, interrupts, virtual memory, DMA, privilege rings, floating point, and ~anything else that is not strictly necessary.

Here is a diagram of the architecture:

For more information, see doc/UCODE.md and doc/ISA.md.

Try it out

The easiest way to try it out is to use the web-based emulator at https://incoherency.co.uk/scamp/

If you want to run it locally, first you'll need to build everything. Try make -j in the root directory of this repository. It probably won't work on the first attempt because it works with multiple different Makefiles that have annoying dependencies. Just run make -j twice or three times. If that doesn't do the trick, run make in sys/ first, and then run it in .. Once you get to the point that it is only complaining about emcc, you're done, unless you also want to build the web-based emulator.

Having built everything, you can go into kernel/ and start it in the emulator with ./run:

~/scamp-cpu/kernel $ ./run
make: 'os.disk' is up to date.
SCAMP boot...
.......................................................OK
starting kernel...
loading init...
      ____   ____    _    __  __ ____   __
     / ___| / ___|  / \  |  \/  |  _ \ / /__  ___
     \___ \| |     / _ \ | |\/| | |_) / / _ \/ __|
      ___) | |___ / ___ \| |  | |  __/ / (_) \__ \
     |____/ \____/_/   \_\_|  |_|_| /_/ \___/|___/

$

If you know how to use Unix you'll probably understand how to use it. If you know how to use CP/M you'll probably understand how it works.

If you want, you can write SLANG programs using kilo and compile them with slc. You've probably never written SLANG before. You can learn how to use it by reading programs under /src (or sys/ in the repo). It's mostly like C but with fewer features and less safety.

How it's made

I did these tasks, but working on several steps at a time (i.e. I didn't wait until after I had built the entire computer before thinking about microcode or the operating system):

  1. Create the CPU in Verilog, with a testbench for each part.
  2. Replace the raw Verilog with Verilog that only uses 74xx-compatible primitives (e.g. https://github.com/TimRudy/ice-chips-verilog), but still passes the testbenches.
  3. Convert the 74xx-Verilog into KiCad schematics (by hand).
  4. Order the PCBs and components, solder up the PCBs, build a card cage, build a wooden case.
  5. Settle on the instruction set and write the microcode (see doc/table.html, available online at https://incoherency.co.uk/interest/table.html).
  6. Write a bootloader, kernel, shell, text editor, compiler, and various system utilities.

Next steps

I suspect the computer will never be "finished", but some things I still want to work on are:

Hardware

  • build a hardware multiplication card (and maybe division?)
  • build a hardware RNG

Low-level software

  • make library blob self-hosting (compile within SCAMP/os)
  • write an strace-like debugging tool
  • write a DDT-like debugging tool
  • better facility for memory profiling (mainly to stop kilo and asm from running out of memory on large inputs)
  • atexit()
  • static initialisation of globals from constants (e.g. var x = 5 at global scope should not take any code to initialise)
  • "const" variables (e.g. const x = 5 should lead to x being replaced with the constant value 5 where possible)

Other software

  • write a big-fixed-point arithmetic library
  • fixed-point mathematics: pow,tan,asin,acos,atan,atan2
  • write a FORTH implementation (should it run on bare metal or under SCAMP/os?)
  • write a Z-machine interpreter and port some text adventures
  • write some games (pong? breakout? tetris?)

If you happen to want to work on any of the above, I'd be happy to help you!

Resources

I thoroughly recommend the Nand2Tetris course. https://nand2tetris.org/

If you want to do the exercises from Nand2Tetris without learning what a hardware-description language is, and without going through all the lectures, you can play https://nandgame.com/

Ben Eater's videos on 8-bit CPU design are excellent and heavily influenced the design of my CPU.

I found the YouTube playlist for Nicolas Laurent's compiler class quite helpful in writing the parser for the compiler.

The Build Your Own Text Editor is a fantastic tutorial that walks you through implementing antirez's kilo editor.

Balazs Markus's 8250 UART example is helpful.

Daniel Tufvesson has a post that is a good starting point for understanding the 8-bit CompactFlash interface.

PickledDog has a description of an improvement to the RC2014 CompactFlash card that might be helpful. Also Bill Shen's comment on the RC2014 mailing list.

Michał Cierniak has a good blog post about CompactFlash + 8255 interface card with lots of detail on how his CompactFlash interface works.

James Sharman is building an interesting 8-bit pipelined CPU, and has good videos with lots of explanation on how it works.

SCAMP is featured on the Homebrew CPU ring which also features many other interesting homebrew CPU designs.

Contact

You can email me on [email protected] or read my blog: [https://incoherency.co.uk/].

More Repositories

1

hardbin

Encrypted pastebin using IPFS
JavaScript
251
star
2

nightdrive

Night-time driving simulation
JavaScript
177
star
3

chess-steg

Steganography in chess games
JavaScript
85
star
4

cntdn

Solvers for the countdown letter and number games.
C
68
star
5

seasonal-css

Seasonal colours for any web page
HTML
63
star
6

meshmill

The world's greatest open source 3D CAM software. (Maybe one day.)
JavaScript
38
star
7

stegoseed

Steganographically hide Bitcoin wallet seeds
HTML
30
star
8

autopatzer

Automatic chess board
JavaScript
27
star
9

cpmhttpd

A basic web server for CP/M
C
13
star
10

eldood

Date coordination for social events
JavaScript
12
star
11

pngcam

Pngcam takes in a heightmap and gives out Gcode to run a CNC machine.
G-code
9
star
12

ring-signatures

Javascript ring signatures tool
JavaScript
8
star
13

jescpu

Silly toy CPU project
Verilog
7
star
14

fifoirc

Creates a fifo that can be written to, producing text in an IRC channel.
C
7
star
15

jetstream

Live-streaming without the bullshit: no signup, no accounts, no pricing. Based on WebRTC and Janus.
JavaScript
6
star
16

psion-arduino

Psion Organiser II interface over USB using an Arduino
C++
6
star
17

muxirc

IRC multiplexer, allowing several client programs to share a nick on IRC.
C
5
star
18

base58

Javascript base58 encode/decode tool
JavaScript
5
star
19

countdown-game

Countdown game
JavaScript
5
star
20

ttgen

Truth-table generator for boolean logic expressions.
C
5
star
21

chess-puzzle

Single-player chess variant
JavaScript
4
star
22

collapseos-doc

Collapse OS documentation browser
Perl
4
star
23

isopath

Web-based implementation of Isopath, a zero-sum boardgame invented by pocket83
JavaScript
4
star
24

fuelsafe

Inline fuel sensor
C++
3
star
25

serve

A simple HTTP server supporting CGI scripts and gzip compression.
C
3
star
26

android-countdown-solver

Countdown Solver for Android
Java
3
star
27

aoc2021

Advent of Code 2021 on my homemade 16-bit CPU SCAMP
Slash
3
star
28

zoe

Zoe Opponent Engine - an xboard-compatible chess engine
C
3
star
29

revsafe

Arduino rev-limiter for my racing lawnmower.
C++
3
star
30

drl2gcode

Convert Excellion drill files (such as those produced by KiCad) into G-Code for a CNC machine.
Python
3
star
31

chess-clock

HTML
3
star
32

aoc2022

Advent of Code 2022 on SCAMP
Slash
3
star
33

timetable-parser

Convert a University of Bath course timetable page into an iCalendar file.
Perl
3
star
34

pikontroll

Controlling my PiKon telescope
Python
2
star
35

keyswitch-tester

Automatic Arduino-based keyboard switch tester
Perl
2
star
36

ricochet-group

Group chat for Ricochet IM
Go
2
star
37

blotto

A program to run round-robin Colonel Blotto tournaments, and one to generate random teams.
C
2
star
38

hideimage

Image steganography tool
HTML
2
star
39

emph

Emphasise matching lines with colour.
Perl
2
star
40

dnstweak

Quick local DNS spoofing tool
Go
2
star
41

jfind

Indexed filesystem search with inotify to detect changes.
C
2
star
42

packominoes

Program to search for skewed pentomino packing puzzles
Perl
1
star
43

nsp

Network Statistics Protocol client and server.
C
1
star
44

bnbf

Brainfuck interpreter with bignum cells and various runtime options.
C
1
star
45

stlwrap

Wrap an STL file around a cylinder
Rust
1
star
46

ngindock

Zero-downtime Docker container deployments with nginx.
Perl
1
star
47

stlcat

Concatenate STL files
Rust
1
star
48

submerge

Subscribe to YouTube channels without telling Google
Perl
1
star
49

3d-metal-printing

Notes and information about 3d metal printing
JavaScript
1
star
50

am

Alphametic puzzle solver.
C
1
star
51

guess-whom

Social online "Guess Who" game
Perl
1
star
52

aoc2020

Advent of Code 2020
Perl
1
star
53

triangle-strip-encoding

Encoding data in triangle strips
JavaScript
1
star
54

ricochet-web

Web interface for Ricochet
JavaScript
1
star
55

bip39tool

Command-line tool to interact with BIP39 mnemonics
Python
1
star
56

aoc2023

Advent of Code 2023 on SCAMP
Slash
1
star
57

bauch

Bauch-style Lego Artwork Generator
JavaScript
1
star
58

counter

Create arbitrary counters
Perl
1
star
59

anagram

Anagram deputy
JavaScript
1
star
60

pugsender

G-code sender that gets out of the way
Go
1
star
61

oscillating-engine

Oscillating engine simulator
JavaScript
1
star
62

deadbeat-escapement

Deadbeat escapement simulator
JavaScript
1
star
63

formpacker

JavaScript
1
star
64

audio2image

Convert audio files to image files
Perl
1
star