Skip to content

ahmash11/CPU-Simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java 32-bit CPU Simulator

Overview

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.

Architecture & Features

Core Data Types

  • Implemented custom Bit, Word16, and Word32 classes using Java booleans for internal storage.
  • Replicated low-level data manipulation required for the rest of the simulator.

Arithmetic Logic Unit (ALU)

  • 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".

Memory Management

  • Simulated DRAM accessed via pointers with read and write functionality.
  • Supported loading 32-character binary string text files directly into simulated memory.

Custom Assembler

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

Processor Pipeline

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

Cache Hierarchy & Performance

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

Testing and Benchmarking

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.

How to Run

  1. Clone the repository.
  2. Compile all .java files.
  3. Run the main processor class and pass in the path to any of the provided test files.

Author

Ahmed Al-Mashraie

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages