Experience the inner workings of computing with this 16-bit CPU emulator in Kotlin. It includes a basic assembler and supports a comprehensive fetch-decode-execute cycle, making it an excellent educational tool for understanding CPU architecture and assembly language.
The kotlin-cpu project is a comprehensive 16-bit CPU emulator implemented in Kotlin, designed for educational purposes. This emulator models the essential components of a CPU including registers, RAM, and an execution loop, while also providing a simplistic assembler that transforms assembly code into machine-executable binary.
CPU Architecture: The emulator features a fully functional 16-bit CPU core, which supports a resource configuration including:
R0 to R7)Instruction Set Architecture (ISA): The implementation includes an ISA executor that decodes 16-bit instructions and executes various operations. The available instruction formats include:
Arithmetic Logic Unit (ALU): Implements a variety of operations encompassing arithmetic, bitwise, and shift functionalities. Example operations include addition, subtraction, logical AND, and more.
Assembler Pipeline: The assembler processes assembly files (.kasm) through several stages:
The instruction encoding is structured as a 16-bit layout with distinct opcodes and operand formats. Several types of operations (such as ADD, SUB, LOAD, etc.) are encoded into the instruction set, accompanied by immediate value processing that supports signed extensions.
The emulator effectively handles flags that impact the behavior of subsequent commands and maintain CPU state, enhancing its functionality in executing control flow instructions.
Several sample programs illustrate the capabilities of the emulator, showcasing:
alu.kasmmemory.kasmbeq.kasm and bne.kasmcall.kasmThe entry point for executing the emulator is found in src/main/kotlin/Main.kt. Users can modify the specified KASM file to run different sample programs, enabling a hands-on approach to learning about CPU operation and assembly language.
For further understanding of how the CPU operates, refer to an insightful series of blog posts:
No comments yet.
Sign in to be the first to comment.