4bit CPU Emulator – A simple 4-bit processor emulator written in JavaScript. Run assembly code directly in your browser! Perfect for learning the basics of computer architecture and low-level programming.
JavaScriptMIT
4-bit CPU Emulator
Welcome to the 4-bit CPU Emulator! This project is a web-based emulator for a simple CPU that supports both 4-bit and 8-bit operations. It allows you to load, execute, and debug programs in a simulated environment with features like registers, flags, memory, and output LEDs.
This emulator is perfect for learning about low-level programming, CPU architecture, and how basic instructions are executed in a processor.
Features
4-bit and 8-bit Operation Modes: Switch between 4-bit and 8-bit modes for different levels of complexity.
Registers and Flags: Simulates CPU registers (AX, BX, CX, etc.) and flags (Zero, Carry, Overflow, etc.).
Memory Simulation: 256 bytes of memory for program storage and execution.
Instruction Set: Supports a variety of instructions, including arithmetic, logical, jump, and bitwise operations.
Program Control: Load, run, step through, and reset programs with intuitive controls.
Output LEDs: Visualize the output of the CPU using LEDs.
Interactive Debugging: View and update registers, flags, and memory in real-time.
This emulator runs directly in your browser. You can use it to load and execute programs written in a simple assembly-like language. The interface provides real-time feedback on the state of the CPU, including registers, flags, and memory.
Prerequisites
To run the emulator, you need:
A modern web browser (e.g., Chrome, Firefox, Edge).
Basic knowledge of assembly language and CPU architecture (optional but helpful).
Installation
Clone the repository to your local machine:
git clone https://github.com/mrFavoslav/4bit-cpu-emulator.git
cd 4bit-cpu-emulator
Open the cpu.html file in your browser to start the emulator.
Usage
Loading a Program
Write your program in the prog.ass file using the supported instruction set.
Click the Load Program button in the emulator interface to load the program into memory.
Running the Emulator
Run: Executes the program continuously until it halts or encounters an error.
Step: Executes the program one instruction at a time, allowing you to debug.
Stop: Halts the execution of the program.
Reset: Resets the CPU, memory, and program state.
Viewing the State
Registers: Displays the current values of all CPU registers.
Flags: Shows the status of the CPU flags (e.g., Zero, Carry).
Memory: Visualizes the contents of the 256-byte memory.
Output LEDs: Displays the output of the CPU in binary form using LEDs.
Instruction Set
Here’s the complete instruction set for the emulator:
Instruction Format
Each instruction consists of an opcode and optional operands. The format varies based on the instruction type.
Register operations can be 4-bit or 8-bit depending on MOP flag
All arithmetic and logical operations update the flags automatically
File Structure
4bit-cpu-emulator/
├── .gitignore # Files and directories to ignore in Git
├── LICENSE # License for the project
├── README.md # Project documentation
├── cpu.html # Main HTML file for the emulator interface
├── emulator.js # JavaScript implementation of the CPU emulator
├── package.json # Project metadata and dependencies
├── package-lock.json # Dependency lock file
├── prog.ass # Example program file (assembly-like language)
Contributing
Contributions are welcome! If you'd like to improve the emulator, fix bugs, or add new features, follow these steps:
Fork the repository.
Create a new branch for your feature or bugfix.
Commit your changes and push them to your fork.
Submit a pull request with a detailed description of your changes.
License
This project is licensed under the MIT License. You are free to use, modify, and distribute this project as long as you include the original license.