-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
103 lines (84 loc) · 2.25 KB
/
main.c
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
/*
Simple Virtual Machine. Based on https://en.wikibooks.org/wiki/Creating_a_Virtual_Machine/Register_VM_in_C
Written 2019, Mayank Shouche.
This work is licensed under the Creative Commons Attribution 4.0 International License.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define NUM_REGS 4
#define READ_SZ 255
unsigned int regs[NUM_REGS] = {0};
int pc = 0;
unsigned int* program = {0x1064, 0x11C8, 0x2201, 0x0000};
unsigned int* memory;
int opcode = 0;
int reg1 = 0;
int reg2 = 0;
int reg3 = 0;
int immediateFull = 0;
int immediateHalf = 0;
bool status = true; //VM runs until status flag turns to zero
int fetch(void);
void getProgram(char* filename, unsigned int* program);
void decodeInstruction(int instruction);
void showRegs(void);
int main(int argc, char* argv[])
{
//getProgram(argv[1], program);
while (status)
{
decodeInstruction(fetch());
switch(opcode)
{
case 0:
printf("halt\n");
status = false;
break;
case 1:
printf("loadi r%d #%d\n", reg1, immediateFull);
regs[reg1] = immediateFull;
break;
case 2:
printf("add r%d r%d r%d\n", reg1, reg2, reg3);
regs[reg1] = regs[reg2] + regs[reg3];
break;
case 3:
printf("addi r%d r%d #%d\n", reg1, reg2, immediateHalf);
regs[reg1] = regs[reg2] + immediateHalf;
break;
}
showRegs();
}
}
int fetch(void)
{
return program[pc++];
}
void getProgram(char* filename, unsigned int* program)
{
FILE* fp;
fp = fopen(filename, "r");
char buffer[READ_SZ];
int index = 0;
while (fgets(buffer, READ_SZ, fp))
{
program[index] = malloc(sizeof(unsigned int)); //allocate space for each instruction read in
program[index++] = strtol(buffer, NULL, 16); //convert the string values from the file to hex ints
}
}
void decodeInstruction(int instruction)
{
opcode = (instruction & 0xF000 ) >> 12; //get first byte of instruction
reg1 = (instruction & 0x0F00) >> 8; //second byte
reg2 = (instruction & 0x00F0) >> 4; //third byte
reg3 = (instruction & 0x000F); //fourth...
immediateFull = (instruction & 0x00FF); //lower half
immediateHalf = (instruction & 0x000F); //LSB
}
void showRegs(void)
{
printf("reg0 = %d\nreg1 = %d\nreg2 = %d\nreg3 = %d\n", regs[0], regs[1], regs[2], regs[3]);
}