Skip to content

AT95BL/CPU-Emulator-in-Java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Processor Architecture Emulator

Project Assignment #1 – Processor Architecture

This project implements an emulator for a custom processor architecture, adhering to principles of object-oriented programming (OOP), SOLID principles, and best practices for readable code. The correctness of all implemented features is tested using unit tests.


πŸ“‹ Project Overview

This emulator models a simple processor with the following components:

  • Four 64-bit general-purpose registers
  • Program Counter (PC)
  • 64-bit address space, where each memory address holds 1 byte

The instruction set supports:

  1. Arithmetic Operations:

    • ADD – Addition
    • SUB – Subtraction
    • MUL – Multiplication
    • DIV – Division
  2. Bitwise Logical Operations:

    • AND, OR, NOT, XOR
  3. Data Transfer Instructions:

    • MOV – Supports both direct and indirect addressing
  4. Control Flow Instructions (Unconditional & Conditional Branching):

    • JMP, JE, JNE, JGE, JL, CMP
  5. I/O Instructions:

    • Input from the keyboard into a register
    • Output from a register to the screen
  6. Processor Halt Instruction:

    • HALT – Stops the emulator execution

πŸ›  Features

  1. Efficient Memory Usage

    • Full access to the guest address space while minimizing host memory usage.
  2. Cache Memory Simulation

    • Configurable cache levels (e.g., L1, L2, L3).
    • Set cache size, associativity, and line size per level.
    • Track cache hit and miss rates for memory accesses.
    • Support for cache replacement algorithms:
      • LRU (Least Recently Used)
      • Optimal (BΓ©lΓ‘dy) Algorithm
  3. Unit Tests

    • All core functionalities are tested with unit tests to ensure correctness.

πŸš€ How to Use

  1. Clone the Repository:

    git clone https://github.com/your-username/processor-emulator.git
    cd processor-emulator
  2. Compile and Run the Program:

    javac -d bin src/*.java
    java -cp bin Main
  3. Configure the Emulator:

    • Set cache parameters (number of levels, size, associativity, line size).
    • Choose a cache replacement algorithm (LRU or Optimal).
  4. Run Example Programs:

    • Sample programs demonstrating arithmetic, logic, branching, and I/O instructions are included.

πŸ—‚ Project Structure

processor-emulator/
β”‚
β”œβ”€β”€ src/                     # Source code
β”‚   β”œβ”€β”€ core/                # Processor and memory models
β”‚   β”œβ”€β”€ instructions/        # Instruction implementations (arithmetic, logic, branching)
β”‚   β”œβ”€β”€ io/                  # Input/Output handling
β”‚   β”œβ”€β”€ cache/               # Cache memory simulation
β”‚   β”œβ”€β”€ tests/               # Unit tests for the emulator
β”‚   └── Main.java            # Entry point for the emulator
β”‚
β”œβ”€β”€ examples/                # Sample programs for the emulator
β”œβ”€β”€ README.md                # Project documentation
└── .gitignore               # Ignored files and directories

βš™οΈ Configuration Options

  • Cache Levels: Specify the number of cache levels (e.g., 1 to 3).
  • Cache Size per Level: Set the size of each cache level in bytes.
  • Cache Line Size: Define the size of a cache line.
  • Associativity: Configure direct, set-associative, or fully associative caches.
  • Replacement Algorithms:
    • LRU – Evicts the least recently used line.
    • Optimal – Uses the BΓ©lΓ‘dy algorithm for optimal line eviction.

πŸ“ Example Usage

Here’s a simple example program that uses arithmetic and branching instructions:

MOV R1, 10    ; Load 10 into register R1  
MOV R2, 20    ; Load 20 into register R2  
ADD R3, R1, R2  ; Add R1 and R2, store result in R3  
CMP R3, 30    ; Compare R3 with 30  
JE END        ; If R3 == 30, jump to END  
HALT          ; Stop the emulator  
END:          ; Label for the end of the program  
MOV R4, 'H'   ; Store character 'H' in R4  
OUT R4        ; Print 'H' to the screen  

πŸ“Š Cache Performance Metrics

The emulator provides detailed statistics on cache performance, including:

  • Cache hit rate: Percentage of memory accesses served from the cache.
  • Cache miss rate: Percentage of memory accesses that result in a cache miss.

πŸ§ͺ Unit Testing

Unit tests ensure the correctness of:

  • Arithmetic and logic operations
  • Data transfers and branching instructions
  • Cache functionality and replacement algorithms

Run all unit tests using:

java -cp bin org.junit.runner.JUnitCore tests.ProcessorTest

πŸ›‘ Best Practices

  • Follow OOP and SOLID principles.
  • Ensure readable code by following naming conventions and proper formatting.
  • Avoid code duplication and aim for efficient performance.

πŸ’‘ Demonstration

To demonstrate the functionality of the emulator:

  • Load a sample program from the examples/ folder.
  • Configure the cache settings before execution.
  • Observe the cache performance metrics and program output on the console.

πŸ›‘ License

This project is developed as part of the Arhitektura računara course.


πŸ‘₯ Contributors

  • Andrej TroΕΎiΔ‡ – Student at Elektrotehnički fakultet, Banja Luka

About

CPU Emulator Simulation in Java Programming Language.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages