-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressed.cpp
More file actions
559 lines (468 loc) · 15.4 KB
/
compressed.cpp
File metadata and controls
559 lines (468 loc) · 15.4 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "compressed.h"
#include <iostream>
#include <string>
#include <cstring>
#include <istream>
#include <bits/stdc++.h>
using namespace std;
string CLW (unsigned int ComInsWord) // compressed lw
{
string AssemblyInstruction;
unsigned int rd, rs, offset;
rd = ((ComInsWord >> 2 ) & 0x0007) + 8 ;
rs = ((ComInsWord >> 7 ) & 0x0007) + 8 ;
offset = ((((ComInsWord >> 6)& 0x1))|(((ComInsWord >> 10) & 0x7 ) << 1)|(((ComInsWord >> 5 ) &0x1 ) << 4)) *4; // check offset calculations
AssemblyInstruction = "lw\t\t\tx" + to_string(rd) + ", " + to_string(offset) + "(x" + to_string(rs) + ")";
// cout << "lw\tx" << rd << ", " << offset << "(x" << rs << ")\n"; //lw x!, num(x!)
return AssemblyInstruction;
}
string CSW (unsigned int ComInsWord) // compressed sw
{
string AssemblyInstruction;
unsigned int rs1, rs2, offset;
rs2 = ((ComInsWord >> 2 ) & 0x0003)+ 8;
rs1 = ((ComInsWord >> 7 ) & 0x0007)+ 8;
offset = (((ComInsWord >> 6)& 0x1)|(((ComInsWord >> 10) & 0x7 ) << 1)|(((ComInsWord >> 5 ) &0x1 ) << 4)); // check offset calculations
AssemblyInstruction = "sw\t\t\tx" + to_string(rs2) + ", " + to_string (offset) + "(x" + to_string(rs1) + ")";
// cout << "sw\t" << "x" << rs2 << ", " << offset << "(x" << rs1 << ")\n"; // sw x!, num(x!)
return AssemblyInstruction;
}
string CADDI4SPN (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rd, Imm;
rd = ((ComInsWord >> 2) & 0x0007)+ 8;
Imm = (((ComInsWord >> 6) & 0x0001) | (((ComInsWord >> 5) & 0x0001) << 1) | (((ComInsWord >> 11) & 0x0003) << 2) | (((ComInsWord >> 7) & 0x000F) << 4))*4;
if (!Imm) return "Error in offset of ADDI14SPN";
AssemblyInstruction = "addi\t\tx" + to_string(rd) + ", sp, " + to_string(Imm);
return AssemblyInstruction;
}
string QuadrantZero (unsigned int ComInsWord) // opcode zero
{
unsigned int fun3;
if (ComInsWord == 0) return "Illegal Instructions!";
fun3 = (ComInsWord >> 13) & 0x0007;
switch (fun3)
{
case 0:
{
return CADDI4SPN(ComInsWord);
}
case 2:
{
return CLW(ComInsWord);
break;
}
case 6:
{
return CSW(ComInsWord);
break;
}
default:
{
return "Unsupported Instruction!";
break;
}
}
return "errorr";
}
/////////////////////////////////////////////////////////////////////////
string CSLLI (unsigned int ComInsWord) //compressed shoft ledt logical (immediate)
{
string AssemblyInstruction;
unsigned int rd, Imm;
rd = (ComInsWord >> 7 ) & 0x1F;
Imm = ((ComInsWord >> 2) & 0x001F) | (((ComInsWord >> 12) & 0x0001) << 5);
if (!rd)
// cout << "Error in the instruction!!";
AssemblyInstruction = "Error in loading rd in CSLLI!!";
else
// cout << "slli\tx" << rd << ", x" << rd << ", " << Imm << endl; // slli x!, x!, Imm
AssemblyInstruction = "slli\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", " + to_string(Imm);
return AssemblyInstruction;
}
string CJR_MV (unsigned int ComInsWord)
{
unsigned int rs, rd;
string AssemblyInstruction;
rs = ((ComInsWord >> 2) & 0x1F);
rd = ((ComInsWord >> 7) & 0x1F);
if (rs)
{
// MV
// cout << "add\tx" << rd << ", x0, x" << rs << endl; // add rd, x0, rs
AssemblyInstruction = "add\t\t\tx" + to_string(rd) + ", x0, x" + to_string(rs);
}
else
{
// JR
// cout << "jalr\tx0, x" << rd << ", 0\n"; // jalr x0, rs, 0
AssemblyInstruction = "jalr\t\tx0, x" + to_string(rd) + ", 0";
}
return AssemblyInstruction;
}
string CLWSP (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rd, offset;
rd = ((ComInsWord >> 7 ) & 0x1F);
offset = (((ComInsWord >> 4) & 0x0007)|(((ComInsWord >> 12) & 0x1) << 3)|(((ComInsWord >> 2 ) & 0x3) << 4))*4; //shift by 2 or not ? x4
// cout << "lw\tx" << rd << ", " << offset << "(x2)\n"; //lw x!, num(x2)
AssemblyInstruction = "lw\t\t\tx" + to_string(rd) + ", " + to_string(offset) + "(sp)";
return AssemblyInstruction;
}
string CSWSP (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rs2, offset;
rs2 = ((ComInsWord >> 2 ) & 0x1F);
offset = (((ComInsWord >> 9) & 0x0F) |(((ComInsWord >> 7) & 0x03) << 4))*4; //shift by 2 or not ? x4
// cout << "sw\t" << "x" << rs2 << ", " << offset << "(x2)\n"; // sw x!, num(x!)
AssemblyInstruction = "sw\t\t\tx" + to_string(rs2) + ", " + to_string(offset) + "(sp)";
return AssemblyInstruction;
}
string CJALR_ADD_EBREAK (unsigned int ComInsWord)
{
unsigned int rs, rd;
string AssemblyInstruction;
rs = ((ComInsWord >> 2) & 0x1F);
rd = ((ComInsWord >> 7) & 0x1F);
if (rs == 0 & rd ==0)
{
AssemblyInstruction = "ebreak";
}
else if (rs)
{
// MV
// cout << "add\tx" << rd << ", x" << rd << ", x" << rs << endl; // add rd, rd, rs
AssemblyInstruction = "add\t\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", x" + to_string(rs);
}
else
{
// JR
// cout << "jalr\tx1, x" << rd << ", 0\n"; // jalr x1, rd, 0
AssemblyInstruction = "jalr\t\tx1, x" + to_string(rd) + ", 0";
}
return AssemblyInstruction;
}
string QuadrantTwo (unsigned int ComInsWord) // opcode two
{
unsigned int fun3, Bit12;
fun3 = (ComInsWord >> 13) & 0x0007;
switch (fun3)
{
case 0:
{
return CSLLI(ComInsWord);
break;
}
case 2:
{
return CLWSP(ComInsWord);
break;
}
case 4:
{
Bit12 = (ComInsWord >>12) & 0x0001;
if (Bit12)
return CJALR_ADD_EBREAK(ComInsWord);
else
return CJR_MV(ComInsWord);
break;
}
case 6:
{
return CSWSP(ComInsWord);
break;
}
default:
{
// cout << "Unsupported Instruction.\n";
return "Unsupported Instruction.";
break;
}
}
return "errorr";
}
///////////////////////////////////////////////////////////////////////
// how to get negative offset
string CJ (unsigned int ComInsWord, unsigned int pc)
{
signed int offset;
string AssemblyInstruction;
offset = (((ComInsWord >> 3 ) & 0x0007 )) | (((ComInsWord >> 11) & 0x0001) << 3) | (((ComInsWord >> 2) & 0x0001) << 4) | (((ComInsWord >> 7) & 0x0001) << 5) | (((ComInsWord >> 6) & 0x0001) << 6) | (((ComInsWord >> 9) & 0x0003) << 7) | (((ComInsWord >> 8 ) & 0x0001) << 9)| (((ComInsWord >> 12) & 0x0001) << 10);
if (((ComInsWord >> 12) & 0x0001))
{
offset = offset ^ 0x07FF;
offset = (offset + 1) * (-1) ;
}
// cout << "jal\tx1, " << hex << offset;
offset = offset * 2 +pc;
stringstream ss;
ss << "jal\t\t\tx0, 0x" << hex << offset;
AssemblyInstruction = ss.str();
return AssemblyInstruction;
}
string CJAL (unsigned int ComInsWord, unsigned int pc)
{
signed int offset;
string AssemblyInstruction;
offset = (((ComInsWord >> 3 ) & 0x0007 )) | (((ComInsWord >> 11) & 0x0001) << 3) | (((ComInsWord >> 2) & 0x0001) << 4) | (((ComInsWord >> 7) & 0x0001) << 5) | (((ComInsWord >> 6) & 0x0001) << 6) | (((ComInsWord >> 9) & 0x0003) << 7) | (((ComInsWord >> 8 ) & 0x0001) << 9)| (((ComInsWord >> 12) & 0x0001) << 10);
if (((ComInsWord >> 12) & 0x0001))
{
offset = offset ^ 0x07FF;
offset = (offset + 1) * (-1) ;
}
// cout << "jal\tx1, " << hex << offset;
offset = offset * 2 +pc;
stringstream ss;
ss << "jal\t\t\tx1, 0x" << hex << offset;
AssemblyInstruction = ss.str();
return AssemblyInstruction;
}
string CBEQZ (unsigned int ComInsWord, unsigned int pc)
{
string AssemblyInstruction;
unsigned int rs1;
signed int offset;
signed int result;
rs1 = ((ComInsWord >> 7) & 0x0007)+ 8;
if (((ComInsWord >> 12 ) & 0x0001))
{
offset =(((ComInsWord >> 3) & 0x0003) |(((ComInsWord >> 10) & 0x0003) << 2) | (((ComInsWord >> 2 ) & 0x0001) << 4) | (((ComInsWord >> 5) & 0x0003) << 5) | (((ComInsWord >> 12 ) & 0x0001) << 7)); //not multiplied by 2
offset = offset ^ 0x0ff;
offset = (offset + 1) * -1;
// cout << "neg:" << offset << endl << result << endl;
}
else
{
offset =(((ComInsWord >> 3) & 0x0003) |(((ComInsWord >> 10) & 0x0003) << 2) | (((ComInsWord >> 2 ) & 0x0001) << 4) | (((ComInsWord >> 5) & 0x0003) << 5) | (((ComInsWord >> 12 ) & 0x0001) << 7)); //not multiplied by 2
// cout << "pos:" <<offset << endl;
}
offset = offset*2 + pc;
// cout << "pc" << pc << endl;
// cout << "new: " << offset << endl;
stringstream ss;
ss << "beq\t\tx" << to_string(rs1) << ", x0, 0x" << hex << offset;
AssemblyInstruction = ss.str();
return AssemblyInstruction;
}
string CBNEZ (unsigned int ComInsWord, unsigned int pc)
{
string AssemblyInstruction;
unsigned int rs1;
signed int offset;
rs1 = ((ComInsWord >> 7) & 0x0007)+ 8;
if (((ComInsWord >> 12 ) & 0x0001))
{
offset =(((ComInsWord >> 3) & 0x0003) |(((ComInsWord >> 10) & 0x0003) << 2) | (((ComInsWord >> 2 ) & 0x0001) << 4) | (((ComInsWord >> 5) & 0x0003) << 5) | (((ComInsWord >> 12 ) & 0x0001) << 7)); //not multiplied by 2
offset = offset ^ 0x0ff;
offset = (offset + 1) * -1;
}
else
{
offset =(((ComInsWord >> 3) & 0x0003) |(((ComInsWord >> 10) & 0x0003) << 2) | (((ComInsWord >> 2 ) & 0x0001) << 4) | (((ComInsWord >> 5) & 0x0003) << 5) | (((ComInsWord >> 12 ) & 0x0001) << 7)); //not multiplied by 2
}
offset = offset*2 + pc;
stringstream ss;
ss << "bne\t\tx" << to_string(rs1) << ", x0, 0x" << hex << offset;
AssemblyInstruction = ss.str();
return AssemblyInstruction;
}
string CLI (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rd;
signed int Imm;
rd = ((ComInsWord >> 7) & 0x001F);
if(!rd)
{
return "Unsupported Instruction!!";
}
Imm = (((ComInsWord >> 2) & 0x000F ) | ((ComInsWord >> 12) & 0x0001) << 4);
if (((ComInsWord >> 12) & 0x0001))
{
Imm = Imm ^ 0x1f;
Imm++;
Imm = Imm * (-1);
}
AssemblyInstruction = "addi\t\tx" + to_string(rd) + ", x0, " + to_string(Imm);
return AssemblyInstruction;
}
string CLUI_ADDI16SP (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rd;
signed int Imm;
rd = ((ComInsWord >> 7) & 0x001F);
if(!rd )
{
return "Unsupported Instruction!!";
}
else if (rd == 2)
{
// ADDI16SP
Imm = (((ComInsWord >> 6) & 0x0001 ) | (((ComInsWord >> 2) & 0x0001) << 1 ) | (((ComInsWord >> 5) & 0x0001) << 2 ) | (((ComInsWord >> 3) & 0x0003) << 3 ) | (((ComInsWord >> 12) & 0x0001) << 5 )); // << 4
if ((ComInsWord >> 12) & 0x0001)
{
Imm = Imm ^ 0x3f;
Imm++;
Imm = Imm * (-1);
}
Imm *= 16;
AssemblyInstruction = "addi\t\tsp, sp, " + to_string(Imm);
}
else
{
// LUI
Imm = (((ComInsWord >> 2) & 0x001F ) | ((ComInsWord >> 12) & 0x0001) << 5);
if ((ComInsWord >> 12) & 0x0001)
{
Imm = Imm ^ 0x3f;
Imm++;
Imm = Imm * (-1);
}
Imm = Imm * 4096;
AssemblyInstruction = "lui\t\tx" + to_string(rd) + ", x0, " + to_string(Imm);
}
return AssemblyInstruction;
}
string CADDI_NOP (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rd;
signed int Imm;
rd = ((ComInsWord >> 7) & 0x001F);
Imm = (((ComInsWord >> 2) & 0x000F ) | ((ComInsWord >> 12) & 0x0001) << 4);
if (((ComInsWord >> 12) & 0x0001))
{
Imm = Imm ^ 0x1f;
Imm++;
Imm = Imm * (-1);
}
if ((rd == 0) & (Imm == 0))
{
AssemblyInstruction = "addi\t\tx0, x0, 0";
}
else
{
AssemblyInstruction = "addi\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", " + to_string(Imm);
}
return AssemblyInstruction;
}
string CSRLI_SRAI_ANDI (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rd, Imm, fun2;
signed int ImmAnd;
Imm = (((ComInsWord >> 2) & 0x1F) | (((ComInsWord >> 12) & 0x1) << 5));
ImmAnd = (((ComInsWord >> 2) & 0x1F) | (((ComInsWord >> 12) & 0x1) << 5));
rd = ((ComInsWord >> 7) & 0x7)+ 8;
fun2 = (ComInsWord >> 10) & 0x3;
if (fun2 == 0)
{
AssemblyInstruction = "srli\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", " + to_string(Imm);
}
else if (fun2 == 1)
{
AssemblyInstruction = "srai\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", " + to_string(ImmAnd);
}
else if (fun2 == 2)
{
if ((ComInsWord >> 12) & 0x1)
{
ImmAnd = ImmAnd ^ 0x3f;
ImmAnd++;
ImmAnd = ImmAnd * (-1);
}
AssemblyInstruction = "andi\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", " + to_string(ImmAnd);
}
else
AssemblyInstruction = "Instruction not supported!";
return AssemblyInstruction;
}
string CAND_OR_XOR_SUB (unsigned int ComInsWord)
{
string AssemblyInstruction;
unsigned int rd, rs, func;
rd = ((ComInsWord >> 7) & 0x7)+ 8;
rs = ((ComInsWord >> 2) & 0x7)+ 8;
func = (ComInsWord >> 5) & 0x3;
if (func == 0)
{
AssemblyInstruction = "sub\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", x" + to_string(rs);
}
else if (func == 1)
{
AssemblyInstruction = "xor\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", x" + to_string(rs);
}
else if (func == 2)
{
AssemblyInstruction = "or\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", x" + to_string(rs);
}
else if (func == 3)
{
AssemblyInstruction = "and\t\tx" + to_string(rd) + ", x" + to_string(rd) + ", x" + to_string(rs);
}
else
AssemblyInstruction = "Instruction not supported!";
return AssemblyInstruction;
}
string QuadrantOne (unsigned int ComInsWord, unsigned int pc) // opcode two
{
string AssemblyInstruction;
unsigned int func, func2;
func = (ComInsWord >> 13) & 0x7;
func2 = (ComInsWord >> 10) & 0x3;
switch (func)
{
case 0:
{
AssemblyInstruction = CADDI_NOP (ComInsWord);
break;
}
case 1:
{
AssemblyInstruction = CJAL (ComInsWord, pc);
break;
}
case 2:
{
AssemblyInstruction = CLI (ComInsWord);
break;
}
case 3:
{
AssemblyInstruction = CLUI_ADDI16SP (ComInsWord);
break;
}
case 4:
{
if (func2 == 3)
{
AssemblyInstruction = CAND_OR_XOR_SUB (ComInsWord);
}
else
{
AssemblyInstruction = CSRLI_SRAI_ANDI (ComInsWord);
}
break;
}
case 5:
{
AssemblyInstruction = CJ (ComInsWord, pc);
break;
}
case 6:
{
AssemblyInstruction = CBEQZ (ComInsWord, pc);
break;
}
case 7:
{
AssemblyInstruction = CBNEZ (ComInsWord, pc);
break;
}
}
return AssemblyInstruction;
}