GitHub - djhworld/simple-computer: the scott CPU from "But How Do It Know?&...
source link: https://github.com/djhworld/simple-computer
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Whilst reading But How Do It Know? by J. Clark Scott I felt compelled to write something to simulate the computer the book describes.
Starting from NAND gates, and moving up through to registers, RAM, the ALU, the control unit and adding I/O support, I eventually ended up with a fully functional machine.
All the components of the system are based on logic gates and the way they are connected together via the system bus.
- at least on my machine
- the book describes an 8-bit CPU for simplicity but I wanted more RAM and there is only one system bus
- 65K RAM
- 240x160 screen resolution
- 4x 16-bit registers (
- Interrupts, so you have to write awful polling code
- The book does shortly describe how to extend the system to support interrupts but would involve a lot more wiring
- Stack pointer register + stack + stack manipulation instructions so nested
CALLinstructions won't work and registers may be left in an inconsistent state
- Hard drive
- Subtract instruction
- Floating point math (lol)
- Everything else you could think of from a modern CPU
- No Meltdown/SPECTRE risk
- Can easily overwrite any portion of memory without any protective mode getting in the way
- Currently incapable of accessing the internet
- I can see how you might write a simple networking I/O adapter, although I'd imagine it would be tedious writing the assembly to get bytes in and out of it ?
InstructionsInstruction Type Description Example
LOAD Ra, RbMachine Load value of memory address in register A into register B
LOAD R1, R2
STORE Ra, RbMachine Store value of register B into memory address in register A
STORE R3, R1
DATA Ra, <VALUE>Machine Put
<VALUE>into register A.
<VALUE>can either be a symbol, prefixed with
%LINE-X) or a numeric value (e.g.
DATA R3, %KEYCODE
JR RaMachine Jump to instruction in memory address in register A
JMP <LABEL>Machine Jump to instruction in memory address for
JMP[CAEZ]+ <LABEL>Machine Jump to instruction in memory address for
<LABEL>if flags register for any combination of
CLFMachine Clear contents of flags register
IN <MODE>, RaMachine Request input from IO device to Register A
IN Data, R3
OUT <MODE>, RaMachine Send output to IO device for register A
OUT Addr, R2
ADD Ra, RbMachine 16 bit addition of two registers
ADD R0, R2
SHR RaMachine Shift right register A
SHL RaMachine Shift left register A
NOT RaMachine Bitwise NOT on register A
AND Ra, RbMachine Bitwise AND on two registers
AND R2, R3
OR Ra, RbMachine Bitwise OR on two registers
OR R0, R1
XOR Ra, RbMachine Bitwise XOR on two registers
XOR R1, R0
CMP Ra, RbMachine Compare register A and register B (will set flags register)
CMP R1, R2
CALL <LABEL>Pseudo Call a subroutine. This will jump to the subroutine, on completion, the subroutine should jump back and continue from the next instruction. Note: there is no stack functionality here so all registers may be in a different state at the end of the subroutine.
The following I/O devices are supported by the computer.Device Address Keyboard
There is no memory management unit or protected areas of memory.
However the assembler and simulator will start executing user code from offset
Machine code can be written in text and assembled using a crude assembler I wrote.
See assembler for more information.
- go 1.12+
- GLFW 3.2+
There are some unit tests that take 30-45 seconds to run through, by running
The computer can be run using the wrapper tool I wrote that utilises GLFW for I/O functionality.
Example of running the
./bin/simulator -bin _programs/brush.bin
You can see some example programs I wrote under _programs/, note the ASM code I wrote for these is very bad and I lost my sanity a bit when writing them.
I'm taking myself on a journey, a hardware journey you might say. I want to understand how computers work at a lower level but not quite low enough for the physics/digital electronics side of things.
Just enough to see all the pieces of the system interacting. I remember doing a lot of this stuff in school but I'd say my education seemed to focus on the concepts (Von-Neumann architecture, fetch-decode-execute) rather than the actual construction of a CPU.
This simple computer is the start of that journey, it's actually been a very rewarding little project.
I hope to move onto playing around with X86/ARM/RISC-V next although I suspect it will be quite a leap (of faith)
Aggregate valuable and interesting links.
Joyk means Joy of geeK