-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassembler.cpp
More file actions
222 lines (207 loc) · 8.23 KB
/
assembler.cpp
File metadata and controls
222 lines (207 loc) · 8.23 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
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
#include <bits/stdc++.h>
using namespace std;
map<string,string> binaryRamInstructions{
//{"instruction_name", bitset<4>(string("0000"))},
{"loadA", "0"},
{"storeA", "1"},
{"addA", "2"},
{"subA", "3"},
{"inA", "4"},
{"outA", "5"}
};
map<string,string> binaryRomInstructions{
{"jpos", "6"},
{"jneg", "7"},
{"jz", "8"},
{"jnz", "9"},
{"jmp", "A"},
{"halt", "B"}
};
int variables_number = 0;
vector<string> tokenize(string line) {
vector<string> tokens;
string aux;
stringstream stream(line);
while(getline(stream, aux, ' '))
tokens.push_back(aux);
return tokens;
}
void configureData(map<string, pair<bitset<6>,int>> *variables, vector<string> tokens) {
int int_value = 0;
if(tokens.size() == 2)
int_value = atoi((tokens[1]).c_str());
bitset<6> direction(variables_number);
pair<bitset<6>,int> value;
value.first = direction;
value.second = int_value;
(*variables)[tokens[0]] = value;
variables_number += 1;
}
void configureProgram(vector<pair<string,string>> *instructions, vector<string> tokens) {
pair<string, string> instruction;
instruction.first = tokens[0];
if(tokens.size() == 2) instruction.second = tokens[1];
(*instructions).push_back(instruction);
}
void configureProgramReferences(map<string, bitset<6>> *references, vector<string> tokens) {
(*references)[tokens[0]] = bitset<6>(atoi(tokens[1].c_str()));
}
void writeRam(ofstream *file, map<string,pair<bitset<6>,int>> *variables){
map<string,pair<bitset<6>,int>>::iterator it = (*variables).begin();
stringstream init_value;
(*file) << "(\n";
for(int i = 0; i < variables_number; i++){
if((it->second).second != 0) {
init_value.str("");
init_value << hex << uppercase << bitset<4>((it->second).second).to_ulong();
(*file) << (it->second).first.to_ulong() << " => X\"" << init_value.str() << "\",\n";
}
it++;
}
(*file) << "others => X\"0\"";
(*file) << "\n)\n";
}
void writeRom(ofstream *file, map<string,pair<bitset<6>,int>> *variables, map<string, bitset<6>> *references, vector<pair<string,string>> *instructions){
string binary_option;
bitset<8> operand;
stringstream operand_stream;
(*file) << "(\n";
for(int i = 0; i < (*instructions).size(); i++){
operand_stream.str("");
if((*instructions)[i].first[0] == 'j' || (*instructions)[i].first[0] == 'h'){
binary_option = binaryRomInstructions[(*instructions)[i].first];
operand = (*instructions)[i].second != "" ? bitset<8>((*references)[(*instructions)[i].second].to_ulong()) : bitset<8>();
}else{
binary_option = binaryRamInstructions[(*instructions)[i].first];
operand = (*instructions)[i].second != "" ? bitset<8>((*variables)[(*instructions)[i].second].first.to_ulong()): bitset<8>();
}
operand_stream << hex << uppercase << operand.to_ulong();
(*file) << i << " => X\"" << binary_option << ((operand.to_ulong() < 16) ? "0" : "") << operand_stream.str() << "\",\n";
}
(*file) << "others => X\"000\"";
(*file) << "\n)\n";
}
void writeProgram(string fileName, map<string,pair<bitset<6>,int>> *variables, map<string, bitset<6>> *references, vector<pair<string,string>> *instructions) {
ofstream file;
stringstream sstream(fileName);
string aux;
getline(sstream, aux, '.');
file.open(aux + ".as");
file << ".data\n";
writeRam(&file, variables);
file << "\n.code\n";
writeRom(&file, variables, references, instructions);
file.close();
}
void writeArduinoRam(ofstream *file, map<string,pair<bitset<6>,int>> *variables){
map<string,pair<bitset<6>,int>>::iterator it = (*variables).begin();
stringstream init_value;
(*file) << "short ram[64] = {\n";
for(int i = 0; i < variables_number; i++){
if((it->second).second != 0) {
init_value.str("");
init_value << hex << uppercase << bitset<4>((it->second).second).to_ulong();
(*file) << "0x" << init_value.str() << ((i == variables_number - 1) ? "" : ",") << "\n";
} else {
(*file) << "0x0" << ((i == variables_number - 1) ? "" : ",") << + "\n";
}
it++;
}
(*file) << "};\n";
}
void writeArduinoRom(ofstream *file, map<string,pair<bitset<6>,int>> *variables, map<string, bitset<6>> *references, vector<pair<string,string>> *instructions){
string binary_option;
bitset<8> operand;
stringstream operand_stream;
(*file) << "short rom[64] = {\n";
for(int i = 0; i < (*instructions).size(); i++){
operand_stream.str("");
if((*instructions)[i].first[0] == 'j' || (*instructions)[i].first[0] == 'h'){
binary_option = binaryRomInstructions[(*instructions)[i].first];
operand = (*instructions)[i].second != "" ? bitset<8>((*references)[(*instructions)[i].second].to_ulong()) : bitset<8>();
}else{
binary_option = binaryRamInstructions[(*instructions)[i].first];
operand = (*instructions)[i].second != "" ? bitset<8>((*variables)[(*instructions)[i].second].first.to_ulong()): bitset<8>();
}
operand_stream << hex << uppercase << operand.to_ulong();
(*file) << "0x" << binary_option << ((operand.to_ulong() < 16) ? "0" : "") << operand_stream.str() << ((i == (*instructions).size() - 1) ? "" : ",") << "\n";
}
(*file) << "};\n";
}
void writeArduinoProgram(string fileName, map<string,pair<bitset<6>,int>> *variables, map<string, bitset<6>> *references, vector<pair<string,string>> *instructions) {
ofstream file;
stringstream sstream(fileName);
string aux;
getline(sstream, aux, '.');
file.open(aux + ".asr");
writeArduinoRom(&file, variables, references, instructions);
writeArduinoRam(&file, variables);
file << "byte instructions = " << (*instructions).size() << ";\n";
file << "byte variables = " << variables_number << ";\n";
file.close();
}
int main(int argc, char** argv) {
map<string,pair<bitset<6>,int>> variables;
map<string,bitset<6>> references;
vector<pair<string,string>> instructions;
vector<string> tokens;
string line;
string fileName(argv[1]);
cout << "file name is : " + fileName + "\n";
ifstream programFile;
programFile.open(fileName);
if(programFile.good()){
getline(programFile, line);
if(line == ".data"){
cout << "parsing data\n";
while(line != ""){
getline(programFile, line);
if(line != "")
configureData(&variables, tokenize(line));
}
cout << "data parsed\n";
while (line == "")
getline(programFile, line);
if(line == ".references"){
cout << "parsing references\n";
while(line != ""){
getline(programFile, line);
if(line != "")
configureProgramReferences(&references, tokenize(line));
}
cout << "references parsed\n";
while (line == "")
getline(programFile, line);
}else{
cout << "there is no .references section\n";
}
if(line == ".code") {
cout << "parsing code\n";
while(!programFile.eof()){
getline(programFile, line);
if(line != "")
configureProgram(&instructions, tokenize(line));
}
cout << "code parsed\n";
}else{
cout << "there is no .code section\n";
}
if(!argv[2]){
writeProgram(fileName, &variables, &references, &instructions);
}else{
if(/*argv[2] == "arduino"*/true) {
writeArduinoProgram(fileName, &variables, &references, &instructions);
} else {
cout << "there is no second argument like " << argv[2] << "\n";
cout << "Try with \'arduino\' as a second command or nothing to assemble for vhdl by default\n";
}
}
cout << "done\n";
}else{
cout << "there is no .data section\n";
}
programFile.close();
}else{
cout << "cannot read the file\n";
}
}