-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler.cpp
388 lines (379 loc) · 6.95 KB
/
assembler.cpp
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
// Assembler.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
/*
MOV Ri(from) Rj(to)
LDI Ri(address of internal register) value
ILDI R(Rx) value
LOAD Ri(address of external ram from which to read) Xj(Address of internal register to which to write)
ST Ri(read address of internal register) Rj(write address of register in external ram)
INC Ri (increment value in internal register)
DEC Ri (increment value in internal register)
BRNZ location (Branch if not zero)
BRNN location (Branch if not negative)
BRNO location (Branch if not overflow)
BRNU location (Branch if not underflow (shift right underflow) )
( Following alu operations: read from Ri and Rj and store result in Rk)
ADD Ri Rj Rk
SUB Ri Rj Rk
AND Ri Rj Rk
OR Ri Rj Rk
SLEFT Ri Rj Rk
SRIGHT Ri Rj Rk
END (end the program)
*/
fstream fout, fin;
void load(string &res) {
string op1;
int op2;
int i, ctr;
res.append("0001");
fin >> op1;
res.push_back('_');
if (op1.size() == 2)
ctr = op1[1] - '0';
else
ctr = (op1[1] - '0') * 10 + op1[2] - '0';
i = 7;
while (i >= 0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
res.push_back('1');
}
else
res.push_back('0');
i--;
}
res.push_back('_');
fin >> op2;
i = 7;
ctr = op2;
while(i>=0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
res.push_back('1');
}
else
res.push_back('0');
i--;
}
}
void move(string &res) {
string op;
int i, ctr;
res.append("0000");
fin >> op;
res.push_back('_');
if (op.size() == 2)
ctr = op[1] - '0';
else
ctr = (op[1] - '0') * 10 + op[2] - '0';
i = 7;
while (i >= 0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
res.push_back('1');
}
else
res.push_back('0');
i--;
}
fin >> op;
res.push_back('_');
if (op.size() == 2)
ctr = op[1] - '0';
else
ctr = (op[1] - '0') * 10 + op[2] - '0';
i = 7;
while (i >= 0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
res.push_back('1');
}
else
res.push_back('0');
i--;
}
}
void alu(string &res)
{
string op;
int i, ctr;
fin >> op;
res.push_back('_');
if (op.size() == 2)
ctr = op[1] - '0';
else
ctr = (op[1] - '0') * 10 + op[2] - '0';
i = 7;
while (i >= 0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
res.push_back('1');
}
else
res.push_back('0');
i--;
}
fin >> op;
res.push_back('_');
if (op.size() == 2)
ctr = op[1] - '0';
else
ctr = (op[1] - '0') * 10 + op[2] - '0';
i = 7;
while (i >= 0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
res.push_back('1');
}
else
res.push_back('0');
i--;
}
fin >> op;
res.push_back('_');
if (op.size() == 2)
ctr = op[1] - '0';
else
ctr = (op[1] - '0') * 10 + op[2] - '0';
i = 7;
while (i >= 0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
res.push_back('1');
}
else
res.push_back('0');
i--;
}
}
void inc_dec(string &res)
{
string op,buf;
int i, ctr;
fin >> op;
res.push_back('_');
if (op.size() == 2)
ctr = op[1] - '0';
else
ctr = (op[1] - '0') * 10 + op[2] - '0';
i = 7;
while (i >= 0)
{
if (ctr - pow(2, i) >= 0)
{
ctr -= pow(2, i);
buf.push_back('1');
}
else
buf.push_back('0');
i--;
}
res.append(buf);
res.push_back('_');
res.append("00011111");
res.push_back('_');
res.append(buf);
}
void main()
{
string s,op1;
int num,ctr,ctr1,k,j;
fout.open("./progmemory.v", fstream::in | fstream::out | fstream::trunc);
fin.open("./input.txt", fstream::in | fstream::out | fstream::app);
int i = 0,n;
bool cmd = false;
vector<string> res;
vector<pair<string, int> > stack;
while (fin >> s)
{
string ctr;
if (s.compare("LDI") == 0)
{
load(ctr);
ctr.append("_00000000");
ctr.push_back(';');
}
else if (s.compare("ILDI") == 0)
{
load(ctr);
ctr[2] = '1';
ctr.append("_00000000");
ctr.push_back(';');
}
else if (s.compare("INC") == 0)
{
ctr.append("0001_00011111_00000001_00000000;");
res.push_back(ctr);
i++;
ctr = "";
ctr.append("1000");
inc_dec(ctr);
ctr.push_back(';');
}
else if (s.compare("DEC") == 0)
{
ctr.append("0001_00011111_00000001_00000000;");
res.push_back(ctr);
i++;
ctr = "";
ctr.append("1001");
inc_dec(ctr);
ctr.push_back(';');
}
else if (s.compare("MOV") == 0)
{
move(ctr);
ctr.append("_00000000");
ctr.push_back(';');
}
else if (s.compare("ST") == 0)
{
move(ctr);
ctr[0] = ctr[1] = ctr[3] = '0';
ctr[2] = '1';
ctr.append("_00000000");
ctr.push_back(';');
}
else if (s[0] == 'B')
{
ctr.append("01");
if (s[3] == 'Z')
ctr.append("10");
else if (s[3] == 'N')
ctr.append("01");
else if (s[3] == 'O')
ctr.append("00");
else if (s[3] == 'U')
ctr.append("11");
fin >> op1;
ctr.push_back('_');
k = 7;
j = i + 1;
while (k >= 0)
{
if (j - pow(2, k) >= 0)
{
j -= pow(2, k);
ctr.push_back('1');
}
else
ctr.push_back('0');
k--;
}
bool ans = false;
for (j = 0; j < stack.size(); j++) {
if (stack[j].first.compare(op1) == 0) {
ctr.push_back('_');
k = 7;
ans = true;
while (k >= 0)
{
if (stack[j].second - pow(2, k) >= 0)
{
stack[j].second -= pow(2, k);
ctr.push_back('1');
}
else
ctr.push_back('0');
k--;
}
}
}
if (!ans)
{
cout << "Tag doesn't exist in the program" << endl;
exit(0);
}
ctr.append("_00000000");
ctr.push_back(';');
}
else if (s.compare("END") == 0)
{
ctr.append("1111_11111111_11111111_11111111;");
res.push_back(ctr);
break;
}
else if (s.compare("ADD") == 0)
{
ctr.append("1000");
alu(ctr);
ctr.push_back(';');
}
else if (s.compare("SUB") == 0)
{
ctr.append("1001");
alu(ctr);
ctr.push_back(';');
}
else if (s.compare("AND") == 0)
{
ctr.append("1010");
alu(ctr);
ctr.push_back(';');
}
else if (s.compare("OR") == 0)
{
ctr.append("1011");
alu(ctr);
ctr.push_back(';');
}
else if (s.compare("SLEFT") == 0)
{
ctr.append("1100");
alu(ctr);
ctr.push_back(';');
}
else if (s.compare("SRIGHT") == 0)
{
ctr.append("1101");
alu(ctr);
ctr.push_back(';');
}
else if (s.compare("LOAD") == 0)
{
move(ctr);
ctr[0] = '1'; ctr[1] = '1'; ctr[2] = '1'; ctr[3] = '0';
ctr.append("_00000000");
ctr.push_back(';');
}
else {
s.pop_back();
stack.push_back(make_pair(s, i));
i--;
}
i++;
if(!ctr.empty())
res.push_back(ctr);
}
fout << "`timescale 1ns / 1ps \n module progmemory(clk, pc, inst); \n reg[27:0] pmemory[0:31]; output reg[27:0] inst; input wire[4:0]pc; input wire clk; initial begin " << endl;
for (i = 0; i < res.size(); i++)
{
fout<<"pmemory["<<i<<"]=28'b"<<res[i]<<endl;
}
fout << "end \n always @(*) begin \n inst = pmemory[pc]; end \n endmodule " << endl;
fout.close();
fin.close();
_getch;
}