Code Monkey home page Code Monkey logo

cpp-virtual-machine's Introduction

Virtual Machine C++

A virtual machine / CPU emulator written in C++. It contains a VM, assembler, and debugger.

VM screenshot

CPU

The CPU contains four general purpose registers, named AX, BX, CX, and DX. There is a stack pointer SP which indicates the top of the stack. There is a base pointer BP which is the bottom of the stack. The stack grows upwards, to lower memory. Finally there is the instruction pointer IP.

Name Value Bits Description
AX 1 16 Register A
BX 2 16 Register B
CX 4 16 Register C
DX 8 16 Register D
SP 16 16 Stack pointer
SB 32 16 Stack base
BP 64 16 Base pointer (consider replacing with flags)
IP 128 16 Instruction pointer

The following flags are used in the virtual machine.

Name Value Description
C 1 Carry
Z 2 Zero
I 4 Interrupt
B 8 Break
O 16 Overflow
E 32 Equal
L 64 Less than
G 128 Greater than

Memory

Memory has the following layout:

  • In the constructor of the virtual machine the amount of memory is currently set to 8kb.
  • The address range is 0x0000 - 0xffff.
  • The VGA is mapped to address 0x2000 in memory.
    • Size is set at 76x20 so 1520 bytes, range 0x2000-0x25f0.
  • At the moment, a ROM is loaded at address 0x0000.
  • The stack is located at the end of memory.

Instructions

The virtual machine support the following opcodes.

Including files

Before assembling, other assembly files should be included. They are annotated with the @include directive.

%include("file.asm)

Debugger

  • Press F5 to halt.
  • Enter run to start (shorthand: r).
  • Enter reset to reset.
  • Enter step to execute one instruction (shorthand: s).
  • Enter exit to exit.
  • Enter mem addr to change memory view (shorthand: m).
  • Enter set_reg reg val to set a register.
  • Enter set_mem addr val to set a memory address.
  • Enter cursor addr to set the write cursor at that address (shorthand: c).
  • Enter assemble opcode arg1 arg2 (arg3) to assemble those instruction and write them to the write cursor addres.
  • Enter load file to load a binary file at the write cursor.
  • Enter reload to reload the file at the write cursor.
  • Enter b addr to toggle a breakpoint (not added yet).

Todo

  • Write an assembler in Python.
    • Allow including of asm files with %include("file.asm").
    • Implement labels for reference, defined with label:, and address is placed for label.
  • Generate better disassembler argument formatting.
  • Load a binary from a file.
  • Debugger command: load_rom, to load a binary into the VM.
  • Debugger command: reload, to reload the last load rom, for easier dev
  • Debugger command: reg = value, set the value of a register.
  • Debugger command: [mem] = value, set the value of a memory address.
  • Debugger breakpoints.
  • Expand the instruction set:
    • Arithmetic: ADD, SUB, MUL, DIV.
    • Logical operators: AND, OR, NOT, XOR, SHL, SHR.
    • Stack: POP, PUSH.
    • Functions: CALL, RET, but these can be rewritten in terms of POP, PUSH, and JMP.
  • Interrupts:
    • Keyboard input?

cpp-virtual-machine's People

Contributors

darkeclipz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.