Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created October 11, 2019 15:29
Show Gist options
  • Save untodesu/08f728d4caed4eebd8c682dabed438fe to your computer and use it in GitHub Desktop.
Save untodesu/08f728d4caed4eebd8c682dabed438fe to your computer and use it in GitHub Desktop.
//The V16 Virtual Machine
//Licensed under the "BSD-2-Clause" License
#ifndef _V16_H
#define _V16_H
//Standard C Header Files
#include <stdint.h>
#include <stddef.h>
//V16 Macro Definitions
#define V16_MAXRAM 0x10000 //V16 Can address 128KiB of RAM (Address words!)
#define V16_REGISTERS 0x08 //V16 have 8 registers with direct access
#define V16_OADDR(o) ((o >> 2) & 1) //Operand mode ADDR bit
#define V16_OREG(o) ((o >> 1) & 1) //Operand mode REG bit
#define V16_OCONST(0) (o & 1) //Operand mode CONST bit
//Register aliases
//There are 8 registers and three of them are two-purposed
enum {
REG_R0 = 0x00,
};
//Instruction structure
//Looks like PDP-11 instruction but with different purposes
//Instead of PDP11's 'B' bit and then 3-bit opcode, opcode there is a single 4-bit value
struct instr_s {
uint8_t opcode : 4; //Yes it is.
uint8_t dst_mode : 3; //DESTINATION: Operand mode
uint8_t dst_reg : 3; //DESTINATION: Register
uint8_t src_mode : 3; //SOURCE: Operand mode
uint8_t src_reg : 3; //SOURCE: Register
uint16_t dst_const; //DESTINATION: Optional constant
uint16_t src_const; //SOURCE: Optional constant
};
typedef struct instr_s instr_t;
//VM Structure
struct vm_s {
uint16_t ram[V16_MAXRAM];
uint16_t reg[V16_REGISTERS];
int status;
};
typedef struct vm_s vm_t;
//VM Structure/Functions
vm_t * v16_delete(void); //Create new VM
void v16_delete(vm_t *vm); //Delete existing VM
uint16_t v16_get_word(vm_t *vm); //Get word at (R7/IP)++
void v16_start_at(vm_t *vm, uint16_t at); //Start at address
void v16_set_value(vm_t *vm, instr_t *i, uint16_t v); //Set value to dst operand
void v16_parse_instr(vm_t *vm, instr_t *i); //Parse instruction
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment