This Java program is a simple CPU emulator that simulates the execution of instructions stored in a ROM (Read-Only Memory) using a CPU that interacts with both the ROM and a RAM (Random Access Memory). The program reads instructions from a text file, processes them through the CPU, and outputs the results.
The program consists of the following main classes:
Main
: The entry point of the program.CPU
: The central processing unit that fetches, decodes, and executes instructions.ALU
: The arithmetic logic unit that performs operations based on control bits.ROM
: The read-only memory that stores instructions.RAM
: The random access memory that stores data.
- Ensure you have Java installed on your system.
- Create a file named
rom.txt
that contains the instructions for the CPU.
- Compile the Java files:
javac Main.java
- Run the compiled program:
java Main
The rom.txt
file should contain instructions in a format that the CPU can understand. Each line represents one instruction, and each character in the line is a binary digit (0 or 1). The length of each line should not exceed the number of columns defined in the ROM
class (16 in this case).
The Main
class initializes the ROM and RAM, loads instructions from the rom.txt
file into the ROM, and creates a CPU instance to execute these instructions.
public class Main {
public static void main(String[] args) {
ROM rom = new ROM(8, 16);
short sizeram = 32767;
RAM ram = new RAM(sizeram);
rom.loadRomFromFile("rom.txt");
CPU cpu = new CPU(rom, ram);
cpu.executeInstructions();
System.out.print("finish");
}
}
The CPU
class is responsible for executing instructions stored in the ROM. It uses the ALU
class for arithmetic and logic operations and interacts with the RAM
to store and retrieve data.
The ALU
(Arithmetic Logic Unit) performs operations based on the control bits set by the CPU. It supports operations such as addition, bitwise AND, negation, and zeroing inputs.
The ROM
class stores the instructions loaded from the rom.txt
file. It provides methods to retrieve instructions and convert them from binary to integer.
The RAM
class simulates a simple memory module. It allows setting and getting values at specific addresses and printing the first few elements for debugging purposes.
1110101010101010
0001101010101010
...
This file should contain binary instructions, each line representing one instruction.
go register A
ALU
registerA: 32767
registerD: 0
ram: 0
finish
- The program prints the contents of registers and RAM after executing each instruction, which helps in debugging and understanding the instruction flow.
- The
handleRegisters
method in theCPU
class manages updates to the registers and RAM based on the decoded instruction. - The
jump
method in theCPU
class handles conditional and unconditional jumps based on the ALU output.
This emulator provides a basic framework for understanding how a CPU fetches, decodes, and executes instructions, and it can be extended with additional features and more complex instruction sets.