1 Star 1 Fork 0

flanche / simple-computer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Simple Computer

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.

For a write up about this project, see my blog post about it here https://djharper.dev/post/2019/05/21/i-dont-know-how-cpus-work-so-i-simulated-one-in-code/

text writer

Specs

  • ~0.006mhz
    • at least on my machine
  • 16-bit
    • 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 (R0, R1, R2, R3)

Missing features

  • 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 CALL instructions won't work and registers may be left in an inconsistent state
  • Hard drive
  • Subtract instruction
  • MOV instruction
  • Floating point math (lol)
  • Everything else you could think of from a modern CPU

Bonus features

  • 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 🤔

Instructions

Instruction Type Description Example
LOAD Ra, Rb Machine Load value of memory address in register A into register B LOAD R1, R2
STORE Ra, Rb Machine 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 % (e.g. %LINE-X) or a numeric value (e.g. 0x00F2 or 23) DATA R3, %KEYCODE
JR Ra Machine Jump to instruction in memory address in register A JR R2
JMP <LABEL> Machine Jump to instruction in memory address for <LABEL> JMP startloop
JMP[CAEZ]+ <LABEL> Machine Jump to instruction in memory address for <LABEL> if flags register for any combination of CAEZ is true JMPEZ endloop
CLF Machine Clear contents of flags register CLF
IN <MODE>, Ra Machine Request input from IO device to Register A IN Data, R3
OUT <MODE>, Ra Machine Send output to IO device for register A OUT Addr, R2
ADD Ra, Rb Machine 16 bit addition of two registers ADD R0, R2
SHR Ra Machine Shift right register A SHR R0
SHL Ra Machine Shift left register A SHL R0
NOT Ra Machine Bitwise NOT on register A NOT R2
AND Ra, Rb Machine Bitwise AND on two registers AND R2, R3
OR Ra, Rb Machine Bitwise OR on two registers OR R0, R1
XOR Ra, Rb Machine Bitwise XOR on two registers XOR R1, R0
CMP Ra, Rb Machine 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. CALL pollKeyboard

I/O devices

The following I/O devices are supported by the computer.

Device Address
Keyboard 0x000F
Display 0x0007

Memory layout

There is no memory management unit or protected areas of memory.

However the assembler and simulator will start executing user code from offset 0x0500

Assembler

Machine code can be written in text and assembled using a crude assembler I wrote.

See assembler for more information.

Compiler

@realkompot made an awesome compiler https://github.com/realkompot/llvm-project-scott-cpu for this using LLVM that produces working binaries to run on the simulator, check out the cool little snake game example https://github.com/realkompot/llvm-project-scott-cpu/tree/scott-cpu/_scott-cpu

Building

Requirements

  • go 1.12+
  • GLFW 3.2+

Building:

make

There are some unit tests that take 30-45 seconds to run through, by running

make test

Running

The computer can be run using the wrapper tool I wrote that utilises GLFW for I/O functionality.

Example of running the brush.bin program

./bin/simulator -bin _programs/brush.bin

Example programs

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.

Why bother?

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)

空文件

简介

暂无描述 展开 收起
Go 等 3 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/flanche/simple-computer.git
git@gitee.com:flanche/simple-computer.git
flanche
simple-computer
simple-computer
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891