Ben Eater's 8 bit breadboard computer emulator in C++

Overview

TinyE8 - Ben Eater's 8 Bit CPU Emulator

TinyE8 emulates Ben Eater's 8 bit breadboard CPU. Implemented all the Instructions except JC and JZ, I need to watch the whole playlist before implementing these two instructions..

Here are some screenshots:

screenshot

screenshot1

Source code is extensively commented, so that it is readable.

Instruction set

 -------------------------------------------------------------------------------------------------------------------------------
| 0000 - NOP --> No Operation
| 0001 - LDA --> Load contents of a memory address XXXX into A register
| 0010 - ADD --> Load contents of a memory address XXXX into B register, then performs A+B and stores the result in A register
| 0011 - SUB --> Load contents of a memory address XXXX into B register, then performs A-B and stores the result in A register
| 0100 - STA --> Store contents of A register at memory address XXXX
| 0101 - LDI --> Load 4 bit immediate value into A register
| 0110 - JMP --> Unconditional jump: sets program counter to XXXX and executes from there
| 0111 - JC  --> Jump if carry: sets program counter to XXXX when carry flag is set and executes from there
| 1000 - JZ  --> Jump if zero: sets program counter to XXXX when zero flag is set and executes from there
| 1110 - OUT --> Output contents of A register to 7 segment display, in our case, we'll print it on console
| 1111 - HLT --> Halts the execution
 -------------------------------------------------------------------------------------------------------------------------------

In the mean time feel free to fork, implement these instructions and start a pull request. I'll verify changes and merge it to the main branch.

Example: Add 7 and 3

 -----------------------------------------------------------------------------------------------------------
| LDA 0000	;0000 is the memory address of 7 in the RAM. 7 is present at 0000
| ADD 0001	;0001 is the memory address 0f 3 in the RAM. 3 is present at 0001	
| OUT XXXX	;we don't care about XXXX, all we want is whether the last 4 bits are OUT instruction's or not
| HLT XXXX	;similarly, for HLT, we only check the last 4 bits	
|
| ;In Memory or RAM, the values are stored as follows
|	
| ;at 0000 we store 7 or in C syntax, we can represent it as memory[0] = 7;
| ;similarly at 0001, we store 3, or memory[1] = 3;
| ;The output should be 10
 ----------------------------------------------------------------------------------------------------------- 

Implemented Instructions:

  • NOP
  • LDA
  • ADD
  • SUB
  • STA
  • LDI
  • JMP - ah, fixed it
  • JC - implemented
  • JZ - implemented
  • OUT
  • HLT

Loading your own program

Loading your own program from .bin file

Create .asm file and write your assembly program in it. The program should contain atleast 16 lines and each line should consist of 1 or 2 instructions

For example: If you want to write a program that adds 7 and 3 and then subtracts 2 from it, it should something like this

LDA 8
ADD 9
SUB 10
OUT 0
NOP
NOP
NOP
NOP
7
3
2
NOP
NOP
NOP
NOP
NOP

At line 0, we can see LDA 8, this means that, it loads value from address 8 into A register. This address 8 is at line 8 which has a value of 7. So, it Loads 7 into A register.

At line 1, we have ADD 9, this will store contents from address 9 into B register, then adds this value to contents of A register, So, the final value of A register will be 10 and B register will be 3.

At line 2, we have SUB 10, this will store contents from address 10 int B register, then subtracts this value from contents of A register, the final value of A register will be 8 and B register will be 2

At line 3, we have OUT 0, this will OUTPUT the contents of A register on to the console.

Rest of the lines contain NOP instructions.

At line 8, there is 7, this value will be used when line 0 gets executed. At line 9, there is 3, this value will be used when line 1 gets executed. At line 10, there is 2, this value will be used when line 3 gets executed.

In this manner, you have to write your assembly program. And make sure that there are no extra spaces other than spaces between instruction and memory addresses

Once, you write your assembly program, save it. For example, if you have saved it with a name of add.asm, then run the following command to generate .bin file.

python3 eas.py add.asm -o add.bin

This will generate a .bin file. You can run this using the emulator with the following command

./main add.bin 1 where 1 will print the debug info and 0 will omit it.

Building

Clone the repo using git clone https://github.com/0xhh/TinyE8.git and then cd TinyE8.

If you are on Linux, make sure you have g++ working. Then run sh build.sh from the terminal. It will create main binary file. Run it using ./main .

If you are on Windows, install C++ compiler using MinGW and then run the following command on the CMD or Powershell

g++ -c src/*.cpp -std=c++14 -g -Wall -I include && g++ *.o -o ./main.exe and then run main.exe file from CMD or Powershell

TODO

  • JC - Implement jump if carry
  • JZ - Implement jump if zero
  • load program from .bin file
  • did I miss something?

Contributing

Feel free to fork and contribute to this repo. Make sure that your code is readable and add comments wherever they are needed.

You might also like...
8.4.1 untether (for 32-bit iOS)

daibutsu 8.4.1 untether (for 32-bit iOS) exploit A dyld exploit that overrides the MISValidateSignature in libmis.dylib (CVE-2015-7079) OSUnserialize

KePOS is a 64-bit operating system. Design and implement your own operating system
KePOS is a 64-bit operating system. Design and implement your own operating system

KePOS is a 64-bit operating system. The purpose of this system is to combine the theoretical knowledge and practice of the operating system, and to deepen the understanding of the operating system.

A hobbyist 64-bit kernel written in C!
A hobbyist 64-bit kernel written in C!

Discitix Kernel A hobbyist kernel written in C! About Discitix is a kernel being written to wander in the world of low level! Discitix aims to be POSI

An 8-bit minicomputer with a fully custom architecture
An 8-bit minicomputer with a fully custom architecture

JDH-8 An fully custom 8-bit minicomputer with a unique architecture and the express goal of playing PONG. The program above is running in the emulator

RLibm for 32-bit representations (float and posit32)

RLIBM-32 RLIBM-32 is both a library that provides correctly rounded result for all inputs and a collection of tools used to generate the correct polyn

🎨 HiColor is a program for converting images to 15- and 16-bit RGB color
🎨 HiColor is a program for converting images to 15- and 16-bit RGB color

HiColor is a program for converting images to 15- and 16-bit RGB color, the color depth of old display modes known as “high color”. In 15-bit mode images have 5 bits for each of red, green, and blue, and the last bit is reserved. In 16-bit mode green, the color the human eye is generally most sensitive to, gets 6 bits.

seg's WONDELFUL, INCREDIBLE and POWERFUL 16-bit CPU (:

seg's WONDELFUL, INCREDIBLE and POWERFUL 16-bit CPU (: Not really ): This is just a tiny emulated ISA and CPU with an assembler, a simple debugg

CERBERUS 2080™, the amazing multi-processor 8-bit microcomputer
CERBERUS 2080™, the amazing multi-processor 8-bit microcomputer

CERBERUS 2080™ CERBERUS 2080™, the amazing multi-processor 8-bit microcomputer: a fully open-source project available for anyone to peruse, build, mod

ThatOS64 is for the youtube series on 64-Bit Kernel Development pre-loaded by the EFI
ThatOS64 is for the youtube series on 64-Bit Kernel Development pre-loaded by the EFI

Step by Step Tutorials on how to code a 64-Bit loader and kernel for OS Development NOTES Starting with CODE5, the resolution from the EFI file sets t

A program for converting instructions from Ben Eater's 8 Bit computer design to binary.

8Bit-Assembler A program for converting instructions from Ben Eater's 8 Bit computer design to binary. We used C because is used for it's mid-level-ne

Marek Borik 2 Nov 21, 2021
TinyE8 - Ben Eater's 8 Bit CPU Emulator

TinyE8 - Ben Eater's 8 Bit CPU Emulator TinyE8 emulates Ben Eater's 8 bit breadboard CPU. Implemented all the Instructions except JC and JZ, I need to

null 67 Nov 10, 2022
A video input (V4L2) to NDI converter that works with Raspberry Pi (32-bit and 64-bit), and Intel/AMD CPUs

V4L2 to NDI V4L2 to NDI is an application that connects to attached V4L2 devices and encodes the video signal into NDI It uses the NDI library, allowi

Luke Plassman 49 Nov 12, 2022
RISCAL is a 32-bit reduced instruction-set computer (RISC) designed for learning and research purposes. It is named after my dog, Rascal.

RISCAL CPU RISCAL is a 32-bit custom instruction set architecture virtual machine. It is intended to be used for learning/research purposes. In a nuts

null 3 Dec 23, 2021
PLP Project Programming Language | Programming for projects and computer science and research on computer and programming.

PLPv2b PLP Project Programming Language Programming Language for projects and computer science and research on computer and programming. What is PLP L

PLP Language 5 Aug 20, 2022
CSC404: Computer Graphics [CG] & CSL402: Computer Graphics Lab [CG Lab]

COMPUTER-GRAPHICS-AND-COMPUTER-GRAPHICS-LAB CSC404: CG & CSL402: CG LAB [SEMESTER IV] Syllabus CG - Reference Books THE WALL MEGA SATISH - AUTHOR CG C

AMEY THAKUR 7 Apr 28, 2022
A simple CHIP-8 emulator made for the purpose of studying computer organization, mainly how emulation does work.

CHIP8EMU A simple CHIP-8 emulator made for the purpose of studying computer organization, mainly how emulation does work. It was written in just a few

Patrick Cardoso 1 Nov 9, 2021
Emulator of soviet chess computer, WIP 🚧

elektronika-im01 - emulator of soviet chess computer "Электроника ИМ-01" is a Soviet era chess computer. Produced on "Svetlana" (ПАО «Светлана») since

Nikita Zimin 2 Dec 27, 2021
Minimalistic text-based 1-bit music tracker

1bitr 1bitr ("One Bitter" or "The Bitter One") is a minimalistic text-based music tracker. It only supports 1-bit audio playback and encourages users

Serge Zaitsev 57 Nov 22, 2022