-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDartExecutor.c
112 lines (99 loc) · 2.86 KB
/
DartExecutor.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
//
// DartExecutor.c
// DART4iOS
//
// Created by Will Stafford on 6/1/14.
// Copyright (c) 2014 iWill LLC. All rights reserved.
//
#include <stdio.h>
#include <memory.h>
#include "Dart.h"
void setXARGS(char **argsPtr, unit count) {
setSymbol(dart_sym("XAC"), count);
int x;
for (x = 0; x < count; x++) {
setSymbolWithOffset(dart_sym("XARG"), x, loadXString(argsPtr[x]));
}
}
unit loadBinary(unit *code, unit totalLen) {
unit test = sector_alloc(totalLen);
//regs[0] = sector_alloc(0xFF);
writeBytesWithLengthToAddress(code, totalLen, test);
return test;
}
unit executeCode(unit startAddress) {
*programCounter = startAddress;
prgmoff = startAddress;
//addressForNextExec += prgmoff;
command_return_code retCode = NO_ERROR;
while (!dart_kill_exe) {
argtype types[2];
unit execAddr = *programCounter;
types[0] = (argtype)ram[execAddr+3];
types[1] = (argtype)ram[execAddr+4];
unit args[2];
args[0] = ram[execAddr+1];
args[1] = ram[execAddr+2];
if (types[0] & ARG_RELATIVE) {
args[0] += execAddr+1;
} if (types[1] & ARG_RELATIVE) {
args[1] += execAddr+2;
}
unit cmdidx = ram[execAddr];
//printf("%s %lld, %lld -- %x, %x\n", commands[cmdidx].name, args[0], args[1], types[0], types[1]);
int r;
for (r = 0; r < 2; r++) {
unit idcode = (types[r] & 0xff0000)>>16;
if (types[r] & ARG_ADDRESS_OF_REG) {
args[r] = mapped[idcode-1].address+args[r];
} else if (types[r] & ARG_REG) {
args[r] = ram[mapped[idcode-1].address+args[r]];
}
}
//printf("%lld -- %llx:%llx:%llx:%x:%x\n", *programCounter, cmdidx, args[0], args[1], types[0], types[1]);
if (types[0] & ARG_VALUE_AT_ADDRESS) {
args[0] = ram[args[0]];
}
types[0] = ARG_NUM;
if (types[1] & ARG_VALUE_AT_ADDRESS) {
args[1] = ram[args[1]];
}
types[1] = ARG_NUM;
*programCounter = execAddr+5;
if (cmdidx < 0) {
dartDelegate.error(NULL, GENERAL_ERROR);
}
//printf("\n%s %lld, %lld -- %x, %x\n", commands[cmdidx].name, args[0], args[1], types[0], types[1]);
retCode = commands[cmdidx].exec(args, types);
dart_kill_exe = retCode == EXIT;
if (dart_kill_exe) {
continue;
}
if (retCode != NO_ERROR) {
dart_sendErrorToDelegate(retCode);
}
}
if (dart_kill_exe) {
dart_kill_exe = 0;
}
if (retCode == EXIT) {
retCode = NO_ERROR;
}
sector_free(prgmoff);
dart_sendFinishToDelegate(retCode);
return retCode;
}
unit bootFile(const char **args, int count) {
load_commands();
if (!dartInitiated) {
dartInit();
dartInitiated = 1;
}
unit startAddress = loadBinary(dart_bootloader, dart_BL_size);
if (startAddress == OUT_OF_MEMORY_ERROR) {
printf("Cannot load code, out of memory.\n");
return OUT_OF_MEMORY_ERROR;
}
setXARGS(args, count);
return executeCode(startAddress);
}