-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.h
86 lines (74 loc) · 1.36 KB
/
vm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef VM_H_
#define VM_H_
#include <stdint.h>
#include <stdio.h>
//opcodes definition
typedef enum{
END = 0x0, NOP,
PUSHI, PUSHL, PUSHR,
POP,
LDR, STR,
MOVRI, MOVRL, MOVRR,
ADDRI, ADDRL, ADDRR,
SUBRI, SUBRL, SUBRR,
MULRI, MULRL, MULRR,
DIVRI, DIVRL, DIVRR,
IMULRI, IMULRL, IMULRR,
IDIVRI, IDIVRL, IDIVRR,
CMPRI, CMPRL, CMPRR,
JMP, JNZ, JZ, JG, JGE, JL, JLE, JA, JAE, JB, JBE,
NOT,
ANDRI, ANDRL, ANDRR,
ORRI, ORRL, ORRR,
XORRI, XORRL, XORRR
} OPCODE;
/*
R0 - ACCUMULATOR
R1 - ACCUMULTOR
FL - FLAGS
PC - PROGRAM COUNTER
BP - BASE POINTER
SP - STACK POINTER
*/
typedef enum{
R0,
R1,
R2,
R3,
R4,
R5,
R6,
R7,
R8,
R9,
R10,
R11,
FL,
PC,
BP,
SP,
REGS_NUMBER
} REGISTER;
typedef enum{
OF,
ZF,
NF,
CF
} FLAG;
typedef struct vm{
uint32_t memory[0xffff];
uint32_t registers[REGS_NUMBER];
int running;
} vm_t;
vm_t* vm_create();
void vm_destroy(vm_t* vm);
void vm_clear_flag(vm_t* vm, FLAG flag);
int vm_read_flag(const vm_t* vm, FLAG flag);
void vm_set_flag(vm_t* vm, FLAG flag);
void print_vm_details(vm_t* vm);
void read_program_from_file(vm_t* vm, const char* path);
void read_program_to_mem(vm_t* vm, FILE* f);
void vm_start(vm_t* vm);
void vm_run(vm_t* vm);
void vm_cpu_cycle(vm_t* vm);
#endif