-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopenVCBAssembler.cpp
More file actions
184 lines (164 loc) · 4.98 KB
/
openVCBAssembler.cpp
File metadata and controls
184 lines (164 loc) · 4.98 KB
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
#include "openVCB.h"
#include "openVCBExpr.h"
#include <fstream>
namespace openVCB {
using namespace std;
using namespace glm;
bool prefix(const char* str, const char* pre) {
return strncmp(pre, str, strlen(pre)) == 0;
}
string getNext(char* buff, int& pos) {
while (buff[pos] == ' ' || buff[pos] == '\t') pos++;
char* ptr = strchr(buff + pos, ' ');
char* tab = strchr(buff + pos, '\t');
if (ptr == nullptr || (tab != nullptr && tab < ptr))
ptr = tab;
if (!ptr) {
string res = string(buff + pos);
pos = -1;
return res;
}
else {
string res = string(buff + pos, ptr);
while (*ptr == ' ' || *ptr == '\t') ptr++;
pos = ptr - buff;
return res;
}
}
string getNextLine(char* buff, int& pos, int& lineNum) {
while (buff[pos] && (buff[pos] == ' ' || buff[pos] == '\t' || buff[pos] == ';' || buff[pos] == '\n')) {
pos++;
if (buff[pos] == '\n') lineNum++;
}
int start = pos;
int end = -1;
while (buff[pos] && buff[pos] != '\n' && buff[pos] != ';') {
if (end < 0 && buff[pos] == '#') end = pos;
pos++;
}
if (buff[pos] == '\n') lineNum++;
if (end < 0) end = pos;
return string(buff + start, buff + end);
}
void Project::assembleVmem(char* err) {
if (!vmem) return;
lineNumbers.clear();
char errBuff[512];
// printf("%s\n", assembly.c_str());
// Scan through everything once to obtain values for labels
assemblySymbols.clear();
int loc = 1;
int lineLoc = 0;
int lineNum = 0;
char* asmBuffer = (char*)assembly.c_str();
while (lineLoc != assembly.size()) {
// Read a line in.
string line = getNextLine(asmBuffer, lineLoc, lineNum);
if (line.size() == 0) continue;
char* buff = (char*)line.c_str();
// printf("Line: %s\n", buff);
// Parse this stuff
if (prefix(buff, "symbol") || prefix(buff, "resymb")) {}
else if (prefix(buff, "unsymb")) {}
else if (prefix(buff, "unpoint")) {}
else if (prefix(buff, "pointer") || prefix(buff, "repoint")) {
int k = 7;
string label = getNext(buff, k);
string addr = getNext(buff, k);
if (addr == "inline") loc++;
}
else if (buff[0] == '@') {
int k = 1;
string label = getNext(buff, k);
assemblySymbols[label] = loc;
}
else if (prefix(buff, "bookmark")) {}
else if (prefix(buff, "sub_bookmark")) {}
else loc++;
}
// Scan through everything again to assemble
loc = 1;
lineLoc = 0;
lineNum = 0;
while (lineLoc != assembly.size()) {
int lNum = lineNum;
// Read a line in.
string line = getNextLine(asmBuffer, lineLoc, lineNum);
if (line.size() == 0) continue;
char* buff = (char*)line.c_str();
errBuff[0] = 0;
// printf("Line: %s\n", buff);
// Parse this stuff
if (prefix(buff, "symbol") || prefix(buff, "resymb")) {
int k = 6;
string label = getNext(buff, k);
auto val = evalExpr(buff + k, assemblySymbols, errBuff);
assemblySymbols[label] = val;
}
else if (prefix(buff, "unsymb")) {
int k = 6;
string label = getNext(buff, k);
assemblySymbols.erase(label);
}
else if (prefix(buff, "pointer") || prefix(buff, "repoint")) {
int k = 7;
string label = getNext(buff, k);
string addr = getNext(buff, k);
auto addrVal = addr == "inline" ? loc++ : evalExpr(addr.c_str(), assemblySymbols, errBuff);
auto val = evalExpr(buff + k, assemblySymbols, errBuff);
addrVal = addrVal % vmemSize;
assemblySymbols[label] = addrVal;
vmem[addrVal] = val;
lineNumbers[addrVal] = lNum;
}
else if (prefix(buff, "unpoint")) {
int k = 7;
string label = getNext(buff, k);
assemblySymbols.erase(label);
}
else if (buff[0] == '@') {}
else if (prefix(buff, "bookmark")) {}
else if (prefix(buff, "sub_bookmark")) {}
else {
auto val = evalExpr(buff, assemblySymbols, errBuff);
// printf("%s (0x%08x=0x%08x)\n", buff, loc, val);
auto addrVal = (loc++) % vmemSize;
vmem[addrVal] = val;
lineNumbers[addrVal] = lNum;
}
if (*errBuff)
sprintf(err, "line %d: %s", lineNum, errBuff);
if (loc >= vmemSize) {
if (err) strcpy_s(err, 256, "VMem exceeded.");
break;
}
}
// Set VMem latch ids
for (int i = 0; i < vmAddr.numBits; i++) {
ivec2 pos = vmAddr.pos + i * vmAddr.stride;
vmAddr.gids[i] = indexImage[pos.x + pos.y * width];
if (vmAddr.gids[i] == -1 ||
setOff((Logic)states[vmAddr.gids[i]].logic) != Logic::LatchOff) {
printf("error: No address latch at VMem position %d %d\n", pos.x, pos.y);
exit(-1);
}
}
for (int i = 0; i < vmData.numBits; i++) {
ivec2 pos = vmData.pos + i * vmData.stride;
vmData.gids[i] = indexImage[pos.x + pos.y * width];
if (vmAddr.gids[i] == -1 ||
setOff((Logic)states[vmData.gids[i]].logic) != Logic::LatchOff) {
printf("error: No data latch at VMem position %d %d\n", pos.x, pos.y);
exit(-1);
}
}
// dumpVMemToText("vmemDump.bin");
}
void Project::dumpVMemToText(string p) {
FILE* file;
fopen_s(&file, p.c_str(), "w");
for (size_t i = 0; i < vmemSize; i++)
fprintf(file, "a: 0x%08x = 0x%08x\n", i, vmem[i]);
fclose(file);
}
}