Skip to content

RemZapCypher/vm-from-scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Virtual Machine from Scratch

This project was made around December, 2024 and is a simple virtual machine (VM) implemented in C. It provides a basic understanding of how virtual machines work by simulating a simplified computer architecture.

Overview

The VM has its own memory, registers, and a limited instruction set. It can load and execute a program written in its assembly-like language(yup had to re-invent assembly).

Key Components

  • tankvm.h: Header file containing definitions for data structures and function prototypes.
    • VM struct: Represents the virtual machine, including:
      • CPU struct: Represents the CPU, containing registers.
      • Memory : A byte array representing the VM's memory.
      • b: Break address, indicating the end of the program in memory.
    • Registers: 16-bit registers (ax, bx, cx, dx, sp, ip) within the s_register struct.
    • Opcode enum: Defines the instruction set (e.g., mov, nop, hlt).
    • Instruction struct: Represents a single instruction with an opcode and arguments.
    • instrmap: A lookup table mapping opcodes to instruction sizes.
  • tankvm.c: Source file containing the implementation of the VM's functionality.
    • virtualmachine(): Allocates and initializes a new VM struct.
    • exampleprogram(): Creates a sample program in VM's memory (hardcoded).
    • execute(): The main execution loop that fetches, decodes, and executes instructions.
    • execinstr(): Executes a single instruction based on its opcode.
    • __mov(): Implements the mov instruction (move value into a register).
    • map(): Returns the size of an instruction based on its opcode.
    • error(): Handles errors like segmentation faults or halt instructions.
    • Utility functions: copy(), zero(), printhex().
    • main(): Entry point that creates a VM, loads the example program, executes it, and prints memory contents.
  • Makefile: Defines the build process.

Instruction Set

The VM supports the following instructions:

  • mov: Move a value into a register.
  • nop: No operation.
  • hlt: Halt the VM.

How it Works

  1. Initialization: The main function creates a VM instance using virtualmachine().
  2. Program Loading: The exampleprogram() function creates a simple, hardcoded program in the VM's memory.
  3. Execution: The execute() function fetches instructions, decodes them, and executes them in a loop.
  4. Termination: The program halts when a hlt instruction is encountered.

Building

To build the project, run:

make

Then go ahead and execute:

./tankvm

Limitations:

  • Limited instruction set.
  • Hardcoded program.
  • No input/output.
  • Limited memory (65KB).

Future Enhancements:

  • Implement more instructions.
  • Allow loading programs from a file.
  • Add input/output capabilities.
  • Improve error handling.

About

A Virtual Machine implementation written in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published