This repository contains a fully functional 32-bit CPU simulator built entirely from scratch in Java. Developed as a semester-long progressive project, this simulator models the fundamental hardware components of a modern computer. It operates at the bit level, avoiding native Java math, and accurately simulates memory hierarchies, CPU pipelines, and an assembly language translator.
- Implemented custom
Bit,Word16, andWord32classes using Java booleans for internal storage. - Replicated low-level data manipulation required for the rest of the simulator.
- Built a custom Adder, Multiplier, and Shifter using two's complement bitwise operations without native Java math.
- Built an ALU that integrates the math components and updates result flags like "less" and "equal".
- Simulated DRAM accessed via pointers with read and write functionality.
- Supported loading 32-character binary string text files directly into simulated memory.
- Created a custom assembler to parse string-based assembly instructions into 16-bit binary opcodes and formats.
- Merged parsed 16-bit lines into 32-bit instructions ready for memory loading.
- Developed the core CPU pipeline featuring fetch, decode, execute, and store stages.
- Included a Program Counter (PC), 32 discrete registers, and stack memory handling for call/return operations.
- Implemented an Instruction Cache (1 line, 8 words) and a 4-line L2 Cache.
- Tracked execution time via simulated clock cycles, accurately accounting for 10-cycle cache hits and 300+ cycle main memory latency.
The simulator's core functionality—including bits, words, ALU logic, and the assembler—is validated through comprehensive unit testing.
Additionally, the processor includes foundational assembly (.asm) benchmark scripts designed to verify the execution pipeline and memory access patterns. These test programs execute arithmetic loops and memory loads, logging simulated cycle counts to demonstrate the functional integration and relative performance differences between the Instruction Cache, L2 Cache, and main memory.
- Clone the repository.
- Compile all
.javafiles. - Run the main processor class and pass in the path to any of the provided test files.
Ahmed Al-Mashraie