-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.c
395 lines (300 loc) · 6.45 KB
/
emulator.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
void*
die(const char *fmt, ...)
{
va_list args;
va_start(args,fmt);
vfprintf(stderr,fmt,args);
fprintf(stderr,"\n");
va_end(args);
exit(1);
}
#define OPCODE(x) //printf(x"\n");
#define MAX_DEVICE 4
typedef unsigned char uint8_t;
typedef struct {
int16_t mem_range[2];
char name[12];
void (*write)(int16_t address, uint8_t data);
uint8_t (*read)(int16_t address);
} device;
// Remember, addresses are 16bit and datapaths are 8bits
// The operand is 16bits for 16bit addressing
typedef struct {
uint8_t memory[65536];
int16_t pc;
uint8_t accumulator;
uint8_t status;
/*
* Status Register
*
* Lowest
* 0 - Zero Flag
* 1 - Carry Flag
* 2 - Sign Flag
* 3 - Overflow flag
* 4 - Parity Fla
*/
uint8_t ir;
int device_count;
device devices[MAX_DEVICE];
} machine;
device*
device_from_address(machine *m, int16_t address)
{
device *d;
d = &m->devices[0];
if(d){
if( (address >= d->mem_range[0]) &&
(address <= d->mem_range[1])) {
return d;
}
}
return 0;
}
void
write_memory(machine *m,int16_t address, uint8_t data)
{
if(address >= 65536) {
die("Seg Fault!\n");
}
device *d;
d = device_from_address(m,address);
if(d) {
d->write(address,data);
}
m->memory[address] = data;
}
uint8_t
read_memory(machine *m, int16_t address)
{
if(address >= 65536) {
die("Seg fault!");
}
device *d;
d = device_from_address(m,address);
if(d) {
return d->read(address);
}
return m->memory[address];
}
void
load_program(machine *m, uint8_t *data,int length)
{
if(!data) {
die("No program, halting!");
}
memcpy(&m->memory,data,length);
}
void
print_machine_status(machine *m)
{
//printf("\rAccumulator: D:%u H:%02X Carry: %u\n", m->accumulator, m->accumulator, (m->status >> 1) & 1);
}
void
register_device(machine *m, char *d_name, int d_index,
int16_t m_low, int16_t m_high,
void (*write)(int16_t address, uint8_t data),
uint8_t (*read)(int16_t address))
{
if(d_index > MAX_DEVICE) {
die("device index out of range");
}
device *d = &m->devices[d_index];
d->mem_range[0] = m_low;
d->mem_range[1] = m_high;
strcpy(d->name,d_name);
d->write = write;
d->read = read;
printf("Device registered, Name:%s, Low:%0X, High:%0X\n",d->name,d->mem_range[0],d->mem_range[1]);
m->device_count++;
}
// TODO: Update flags on Cyclo
// TODO: Implement Sub with carry
// TODO: Allow simulated clock speed
void
run(machine *m)
{
m->accumulator = 0;
m->status = 0;
m->pc = 0x0;
int running = 1;
uint8_t opcode,oplow,ophigh;
int16_t operand;
uint8_t opvalue;
while(running)
{
// 3 Cycle Fetch
opcode = read_memory(m,m->pc++);
ophigh = read_memory(m,m->pc++);
oplow = read_memory(m,m->pc++);
operand = (ophigh << 8) + oplow;
opvalue = read_memory(m,operand);
switch(opcode){
case 0x00:
if((m->accumulator + opvalue) > 255){
m->status |= (1 << 1);
m->accumulator &= opvalue;
} else {
m->accumulator += opvalue;
if(m->accumulator == 0){
m->status |= 1;
}
}
OPCODE("ADD");
break;
// Should we unset the carry flag after?
case 0x01:
OPCODE("ADC")
m->accumulator += opvalue + ( (m->status >> 1) & 1 );
break;
case 0x02:
m->accumulator -= opvalue;
OPCODE("SUB")
break;
case 0x03:
OPCODE("SBC")
break;
case 0x08:
m->accumulator = oplow;
OPCODE("LDI")
break;
case 0x07:
m->accumulator = ~m->accumulator;
OPCODE("NOT")
break;
case 0x04:
OPCODE("AND")
m->accumulator &= opvalue;
break;
case 0x05:
OPCODE("OR")
m->accumulator |= opvalue;
break;
case 0x06:
OPCODE("XOR")
m->accumulator ^= opvalue;
break;
case 0x09:
OPCODE("LDM")
m->accumulator = opvalue;
break;
case 0x0A:
OPCODE("STM")
write_memory(m,operand,m->accumulator);
//printf("Storing memory: Addr: %04X Value: %04X\n",operand,m->accumulator);
break;
case 0x0B:
OPCODE("JMP")
m->pc = operand;
break;
case 0x0C:
OPCODE("JPI")
m->pc = opvalue;
break;
case 0x0D:
OPCODE("JPZ")
if( m->status & 1 ) {
m->pc = operand;
}
break;
case 0x0E:
OPCODE("JPM")
if( (m->accumulator >> 8) & 1 ) {
m->pc = operand;
}
break;
case 0x0F:
OPCODE("JPC")
if( (m->status >> 1) & 1 ) {
m->pc = operand;
}
break;
case 0x10:
running = 0;
OPCODE("HLT");
break;
case 0x11:
OPCODE("JE")
if( m->accumulator == opvalue ) {
}
break;
case 0x12:
{
OPCODE("CMP")
// Set the carry flag, this is wrong
int8_t tmp;
tmp = (uint8_t) (m->accumulator - opvalue );
if(tmp == 0){
m->status |= 1;
} else {
m->status &= 0;
}
}
break;
}
print_machine_status(m);
//usleep(1000); // 1MHz/1000 = 1Khz
}
}
void
load_file(machine *m,char *path)
{
if(strlen(path) <= 0) {
die("load_file: no path to load");
}
FILE *file;
char *buffer;
long length;
file = fopen(path,"rb");
if(!file) {
die("load_file: %s file doesn't exist",path);
}
fseek(file,0,SEEK_END);
length = ftell(file);
fseek(file,0,SEEK_SET);
buffer = malloc(length+1);
fread(buffer, 1,length, file);
fclose(file);
memcpy(&m->memory,buffer,length);
}
void
dump_memory(machine *m)
{
int i;
for(i=0; i < 200; i++){
if((i % 0x10)==0){
printf("\n %04X | ",i);
}
printf("%02X ",m->memory[i]);
}
}
// TODO: Have video memory
void
video_write(int16_t address, uint8_t data)
{
//printf("Video written: %0X\n",data);
putchar(data);
}
uint8_t
video_read(int16_t address)
{
}
int
main(int argc, char **argv)
{
if(argc < 2){
die("no program specified");
}
machine m;
char *buffer;
printf("Loading program %s\n",argv[1]);
load_file(&m,argv[1]);
free(buffer);
register_device(&m,"video",0,0xA000,0xA7FF,video_write,video_read);
run(&m);
}