Skip to content

Repository files navigation

🌐 Akdeniz University - Department of Computer Engineering

🖥️ CSE206 - Computer Organization

📌 Assignment #1: CPU Emulator

📝 Prepared by: Yahya Efe Kuruçay

🌐 Website: efekurucay.com

📅 Last Update: 11/05/2025

Student ID20220808005

🚀 Project Description

⚡ Submission Note

This assignment reflects a comprehensive development effort. Collaboration with my roommate, Burak Yalçın (Student ID: 20220808069), was crucial. Our discussions refined our understanding of:

  • Addressing Modes
  • Program Counter (PC) Increment Logic
  • Cache Interaction

📂 Project Structure

FileDescription
Memory.javaSimulates 64 KB main memory with byte-level read/write operations.
Cache.javaImplements a direct-mapped cache with 16 bytes (8 blocks, 2 bytes each).
CPUEmulator.javaManages CPU logic, including fetch-decode-execute cycle.
Main.javaApplication entry point; loads and executes the program.

📝 System Specifications & Implementation Details

🔧 CPU Registers

RegisterDescription
Program Counter (PC)Stores the next instruction address.
Accumulator (AC)16-bit register for arithmetic/logical operations.
Comparison FlagSet to -1, 0, or 1 by comparison instructions.

📊 Main Memory

SpecificationDetails
Capacity65,536 bytes (64 KB)
AddressabilityByte-addressable
EndiannessLittle-endian (16-bit values)

🔠 Instruction Format

PropertyDetails
Size16-bit
Structure4-bit opcode + 12-bit operand
StorageEach instruction occupies 2 bytes

🚀 Program Loading

  • Loaded into main memory starting at the loadAddress from config.txt.
  • Each 16-bit instruction occupies 2 consecutive bytes.

🔎 Addressing Modes

ModeDescription
ImmediateOperand contains the direct value (for LOAD, ADD, SUB, MUL).
RelativeEffective address is loadAddress + operand.
Absolute(For PC/Jumps) loadAddress + operand * 2.

📈 Cache Implementation Details

PropertyValue
Cache Size16 bytes
Block Size2 bytes
Number of Blocks8 (16 bytes / 2 bytes/block)
MappingDirect-Mapped

✅ Policies

PolicyDescription
Write-ThroughWrites immediately update main memory.
Write-AllocateOn write miss, block fetched from memory before writing.

📐 Address Breakdown

FieldSizePurpose
Offset1 bitSelects the byte within a 2-byte block.
Index3 bitsSelects one of 8 cache blocks.
Tag12 bitsVerifies cache hit.

⚡ Cache Accesses

  • All data memory operations (LOADM, STOREM, etc.) pass through the cache.
  • Instruction fetches also utilize the cache, contributing to access stats.

🚧 Development Challenges & Resolution

⚡ Addressing Mode Ambiguity

IssueSolution
Interpreting "Absolute (for PC/Jumps)"Treated jump operands as relative instruction indices.

🔄 PC Increment and Fetch Logic

Standard BehaviorAdjusted Behavior
PC += 2 after each instructionFetches counted as cache accesses for hit/miss stats.

✅ Final Implementation Approach

  1. Program Loading: Directly into the Memory object.
  2. Instruction Fetching: Via cache, affecting hit/miss ratio.
  3. PC Management: Increments by 2, except for jumps.
  4. Jump Calculation:loadAddress + operand * 2.
  5. Memory Operations: Accessed through cache.

🔧 Debugging and Notes

  • Extensive use of System.out.println for tracing execution.
  • Memory operand instructions (LOADM, STOREM, etc.) operate on single bytes.
  • Little-endian byte ordering for 16-bit instructions.
  • Named constants (e.g., VALID, TAG in Cache.java) for clarity.

📌 Sample Program Execution and Output

ConfigurationValue
Program Load Address0x2000
Initial PC0x2000
Final AC Value210
Cache Hit Ratio72.88%

📦 Submission Files

FileDescription
README.mdThis file (documentation).
CPUEmulator.javaCore CPU emulation logic.
Cache.javaDirect-mapped cache implementation.
Memory.javaMain memory simulation (64 KB).
Main.javaApplication entry point.
program.txtOriginal sample program.
config.txtConfiguration file.
program2.txtCustom factorial test program (for verification).

Releases

Packages

Contributors

Languages