-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDartRam.c
97 lines (85 loc) · 1.91 KB
/
DartRam.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
//
// DartRam.c
// DART4iOS
//
// Created by Will Stafford on 5/28/14.
// Copyright (c) 2014 iWill LLC. All rights reserved.
//
#include <stdio.h>
#include "Dart.h"
int createRam() {
int x;
for (x = 0; x < SECTOR_COUNT-1; x++) {
sectors[x].offset = 0;
sectors[x].size = 0;
}
stackIdx = sector_alloc(0xfff);
for (x = 0; x < mapIndex; x++) {
mapped[x].address = sector_alloc(mapped[x].size);
if (mapped[x].address == -1) {
dart_sendErrorToDelegate(OUT_OF_MEMORY_ERROR);
}
}
if (ram) {
return 1;
}
return 0;
}
unit sector_alloc(unit size) {
unit x;
for (x = 0; x < SECTOR_COUNT-1; x++) {
unit availableOffset = -1;
availableOffset = sectors[x].offset+sectors[x].size;
if (sectors[x].size == 0) {
if (x > 0) {
availableOffset = sectors[x-1].offset+sectors[x-1].size;
}
if (sectors[x+1].size == 0 || sectors[x+1].offset > availableOffset+size) {
sectors[x].offset = availableOffset;
sectors[x].size = size;
return availableOffset;
}
}
}
return -1;
}
void sector_free(unit address) {
unit x;
for (x = 0; x < SECTOR_COUNT-1; x++) {
if (sectors[x].offset == address) {
sectors[x].size = 0;
}
}
}
void writeUTF8StringToAddress(char *utf8, unit addr) {
unit x = 0;
while (utf8[x] != '\0') {
ram[addr+x] = utf8[x];
x++;
}
}
void writeByteToAddress(byte arg1, unit addr) {
ram[addr] = arg1;
}
byte readByteAtAddress(address addr) {
return ram[addr];
}
void writeBytesWithLengthToAddress(unit *bytes, unit length, unit addr) {
unsigned int itr = 0;
while (itr < length) {
ram[addr+itr] = bytes[itr];
itr++;
}
}
void readBytesWithLengthFromAddressIntoArray(unit length, unit addr, unit **arry) {
unsigned int itr = 0;
while (itr < length) {
(*arry)[itr] = ram[addr+itr];
itr++;
}
}
unit loadXString(char *string) {
unit addr = sector_alloc((unit)strlen(string)+1);
writeUTF8StringToAddress(string, addr);
return addr;
}