-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathwrapper.c
197 lines (176 loc) · 3.95 KB
/
wrapper.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
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
/* Copyright (C) 2016 Jeremiah Orians
* This file is part of stage0.
*
* stage0 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* stage0 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stage0. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vm.h"
#define DEBUG true
extern uint32_t performance_counter;
static struct lilith* Globalvm;
int Memory_Size;
void unpack_byte(uint8_t a, char* c);
/* Load program tape into Memory */
size_t load_program(struct lilith* vm, char* name)
{
FILE* program;
program = fopen(name, "r");
/* Figure out how much we need to load */
fseek(program, 0, SEEK_END);
size_t end = ftell(program);
rewind(program);
/* Load the entire tape into memory */
fread(vm->memory, 1, end, program);
fclose(program);
return end;
}
void execute_vm(struct lilith* vm)
{
struct Instruction* current;
current = calloc(1, sizeof(struct Instruction));
read_instruction(vm, current);
eval_instruction(vm, current);
free(current);
return;
}
void initialize_lilith(unsigned int size, unsigned int posix)
{
POSIX_MODE = posix;
tape_01_name = "tape_01";
tape_02_name = "tape_02";
struct lilith* vm;
vm = create_vm(size);
Globalvm = vm;
Memory_Size = size;
}
void load_lilith(char* name)
{
load_program(Globalvm, name);
}
unsigned int step_lilith()
{
if(!Globalvm->halted)
{
execute_vm(Globalvm);
}
return Globalvm->ip;
}
unsigned int get_register(unsigned int reg)
{
unsigned int i;
switch(reg)
{
case 0 ... 15:
{
i = Globalvm->reg[reg];
break;
}
case 16: /* Get PC */
{
i = Globalvm->ip;
break;
}
case 17: /* Instruction count */
{
i = performance_counter;
break;
}
default:
{
fprintf(stderr, "What have you done????\n");
exit(EXIT_FAILURE);
}
}
return i;
}
void set_register(unsigned int reg, unsigned int value)
{
switch(reg)
{
case 0 ... 15:
{
Globalvm->reg[reg] = value;
break;
}
case 16: /* Set PC */
{
Globalvm->ip = value;
break;
}
case 17: /* No one mess with the Instruction count */
{
fprintf(stderr, "Don't be a moron!!!!\n");
break;
}
default:
{
fprintf(stderr, "What are you trying to do????\n");
exit(EXIT_FAILURE);
}
}
}
void set_memory(unsigned int address, unsigned char value)
{
outside_of_world(Globalvm, address, "Address outside of World");
Globalvm->memory[address] = value;
}
unsigned char get_byte(unsigned int add)
{
outside_of_world(Globalvm, add, "Address outside of World");
return Globalvm->memory[add];
}
void insert_address(char* p, uint32_t value)
{
char* segment;
segment = p;
unpack_byte((value >> 24), segment);
segment = segment + 2;
unpack_byte((value >> 16)%256, segment);
segment = segment + 2;
unpack_byte((value >> 8)%256, segment);
segment = segment + 2;
unpack_byte((value%256), segment);
}
void process_Memory_Row(char* p, uint32_t addr)
{
char* segment = p;
strncpy(segment, "<tr>\n<td>", 10);
segment = segment + 9;
insert_address(segment, addr);
segment = segment + 8;
strncpy(segment, "</td>", 6);
segment = segment + 5;
int i;
for(i = 0; i < 16; i = i + 1)
{
strncpy(segment, "<td>", 5);
segment = segment + 4;
unpack_byte(Globalvm->memory[i + addr], segment);
segment = segment + 2;
strncpy(segment, "</td>", 6);
segment = segment + 5;
}
strncpy(segment, "\n</tr>\n", 8);
}
char* get_memory(unsigned int start)
{
char* result = calloc(4096 * 205 + 1, sizeof(char));
int i, point;
point = 0;
for(i = 0; i < 4096; i = i + 16)
{
process_Memory_Row(result + point, start + i);
point = point + 205;
}
return result;
}