-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirchual-Machine.h
More file actions
210 lines (179 loc) · 4.29 KB
/
Virchual-Machine.h
File metadata and controls
210 lines (179 loc) · 4.29 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
// #include <birchutils.h> // Removed dependency
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#pragma GCC diagnostic push
#define NoErr 0x00 /* 00 00 */
#define SysHlt 0x01 /* 00 01 */
#define ErrMem 0x02 /* 00 10 */
#define ErrSegv 0x04 /* 01 00 */
#define ErrInstr 0x08 /* 10 00 */
typedef unsigned char int8;
typedef unsigned short int int16;
typedef unsigned int int32;
typedef unsigned long long int int64;
#define $1 (int8 *)
#define $2 (int16)
#define $4 (int32)
#define $8 (int64)
#define $c (char *)
#define $i (int)
#define $ax ->c.r.ax
#define $bx ->c.r.bx
#define $cx ->c.r.cx
#define $dx ->c.r.dx
#define $sp ->c.r.sp
#define $ip ->c.r.ip
#define $flags ->c.r.flags
#define equal(x) (!!((x $flags & 0x08) >>3))
#define gt(x) (!!((x $flags & 0x04) >>2))
#define higher(x) (!!((x $flags & 0x02) >>1))
#define lower(x) (!!(x $flags & 0x01))
#define segfault(x) error((x), ErrSegv)
/*
16 bit
AX 16bit
- AL 8bit
- AH 8bit
BX
CX
DX
SP
IP
FLAGS
1010
^----
Zero flag
Carry flag
CMP AX,0x05
65 KB memory
(Serial COM port)
(Floppy drive)
*/
typedef unsigned short int Reg;
struct s_registers {
Reg ax;
Reg bx;
Reg cx;
Reg dx;
Reg sp;
Reg ip;
Reg flags;
/* P1 E - Equal flag
P2 G - Greater-than flag
P3 H - Higher part of reg
P4 L - Lower part of reg */
};
typedef struct s_registers Registers;
struct s_cpu {
Registers r;
};
typedef struct s_cpu CPU;
/*
mov ax,0x05 // (0x01 OR 0x02)
// 0000 0011 = mov
// 0000 0000
// 0000 0101 = 0x05
*/
enum e_opcode {
nop = 0x01,
hlt = 0x02,
mov = 0x08, /* 0x08 - 0x0f*/
ste = 0x10,
cle = 0x11,
stg = 0x12,
clg = 0x13,
sth = 0x14,
clh = 0x15,
stl = 0x16,
cll = 0x17,
/*
reserved = 0x18,
reserved = 0x19
*/
push = 0x1a,
pop = 0x1b
};
typedef enum e_opcode Opcode;
struct s_instrmap {
Opcode o;
int8 s;
};
typedef struct s_instrmap IM;
typedef int16 Args;
struct s_instruction {
Opcode o;
Args a[]; /* 0-2 bytes */
};
typedef struct s_instruction Instruction;
typedef unsigned char Errorcode;
typedef int8 Memory[((int16)(-1))];
typedef int8 Program;
struct s_vm {
CPU c;
Memory m;
int16 b; /* break line */
};
typedef struct s_vm VM;
typedef Memory *Stack;
static IM instrmap[] = {
{ nop, 0x01 },
{ hlt, 0x01 },
{ mov, 0x03 },
{0x09,0x03},{0x0a,0x03},{0x0b,0x03},{0x0c,0x03},
{0x0d,0x03},{0x0e,0x03},{0x0f,0x03},
{ ste, 0x01 },
{ stg, 0x01 },
{ stl, 0x01 },
{ sth, 0x01 },
{ cle, 0x01 },
{ clg, 0x01 },
{ cll, 0x01 },
{ clh, 0x01 },
{push, 0x03 },
{ pop, 0x03 }
};
#define IMs (sizeof(instrmap) / sizeof(struct s_instrmap))
void __ste(VM*,Opcode,Args,Args);
void __stg(VM*,Opcode,Args,Args);
void __sth(VM*,Opcode,Args,Args);
void __stl(VM*,Opcode,Args,Args);
void __cle(VM*,Opcode,Args,Args);
void __clg(VM*,Opcode,Args,Args);
void __clh(VM*,Opcode,Args,Args);
void __cll(VM*,Opcode,Args,Args);
void __push(VM*,Opcode,Args,Args);
void __pop(VM*,Opcode,Args,Args);
void __mov(VM*,Opcode,Args,Args);
// Utility functions
static inline void zero(void *ptr, size_t size) {
memset(ptr, 0, size);
}
static inline void copy(void *dst, void *src, size_t size) {
memcpy(dst, src, size);
}
void error(VM*,Errorcode);
void execinstr(VM*,Program*);
void execute(VM*);
Program *i(Instruction*);
Instruction *i0(Opcode);
Instruction *i1(Opcode,Args);
Instruction *i2(Opcode,Args,Args);
// Program *exampleprogram(VM*);
Program *exampleprogram(VM *vm, ...);
void printhex(void *mem, int len, char sep);
int8 map(Opcode);
VM *virtualmachine(void);
int main(int,char**);