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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
|
#include "instruction.h"
#include "prog.h"
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <fstream>
#include <bitset>
#include <algorithm>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
Program::Program()
{
dumpRegsCalled = false;
}
Program::Program(std::string fname)
{
std::cout << "WARNING: make sure that the executable \"smc_parser\" exists in the working directory" << std::endl;
std::string correctFileName = "./smc_parser " + fname + " < " + fname + " >/dev/null";
if (system(correctFileName.c_str()) != 0)
{
std::cerr << "Error parsing SMC program, abort." << std::endl;
exit(-1);
}
// Read instruction binary data from fname.inst
std::ifstream binFile (fname + ".bin", std::ios::in | std::ios::binary);
int no_insts = 0;
// Read no of insts
binFile.read((char*) &no_insts, 4);
Inst insts[no_insts];
// Read all insts
binFile.read((char*) insts, sizeof(Inst)*no_insts);
for(int i = 0 ; i < no_insts ; i++)
{
program.push_back(insts[i]);
spc++;
}
binFile.close();
// Read branch label positions from fname.meta
std::ifstream metaFile (fname + ".meta", std::ios::in);
std::string line;
bool parseLabels = true;
while(getline(metaFile,line))
{
if(parseLabels)
{
if (line == "-")
{
parseLabels = false;
continue;
}
std::string label = line.substr(0,line.find(" "));
std::string target = line.substr(line.find(" ")+1,line.length());
(labels)[label] = stoi(target);
}
else
{
std::string target = line.substr(0,line.find(" "));
std::string label = line.substr(line.find(" ")+1,line.length());
(branches)[stoi(target)] = label;
}
}
metaFile.close();
}
void Program::add_label(std::string name)
{
if(labels.count(name))
std::cerr << "Trying to add label " << name << " multiple times!" << std::endl;
(labels)[name] = spc;
}
void Program::add_wait(int wait_cycles)
{
for(; wait_cycles > 0 ; wait_cycles--)
{
minprogram.push_back(SMC_NOP());
}
}
void Program::add_mininst(Mininst mi, int wait_after)
{
minprogram.push_back(mi);
for ( ; wait_after>0 ; wait_after--)
minprogram.push_back(SMC_NOP());
// if (verbose) {
// std::cout << "Added new mininst with wait." << std::endl;
// for(auto& mininst : minprogram){
// std::cout << std::hex << mininst << " ";
// }
// std::cout << std::endl;
// }
}
void Program::pack_minprogram()
{
// if (verbose) std::cout << "Packing minprogram" << std::endl;
while(minprogram.size() >= 4)
{
this->add_inst(__pack_mininsts(minprogram[0], minprogram[1], minprogram[2], minprogram[3]));
minprogram.erase(minprogram.begin(), minprogram.begin()+4);
}
switch (minprogram.size())
{
case 0:
return;
case 1:
this->add_inst(__pack_mininsts(minprogram[0], SMC_NOP(), SMC_NOP(), SMC_NOP()));
break;
case 2:
this->add_inst(__pack_mininsts(minprogram[0], minprogram[1], SMC_NOP(), SMC_NOP()));
break;
case 3:
this->add_inst(__pack_mininsts(minprogram[0], minprogram[1], minprogram[2], SMC_NOP()));
break;
}
// std::cerr << "WARNING: Number of mininsts is not multiple of four. "
// << "Appending "<< 4-minprogram.size() << " extra NOPs for alignment."
// << std::endl;
minprogram.erase(minprogram.begin(), minprogram.end());
}
void Program::add_inst(Inst i)
{
program.push_back(i);
if (is_load(i))
this->add_inst(SMC_NOP(), SMC_NOP(), SMC_NOP(), SMC_NOP());
spc += 1;
}
void Program::add_inst(Mininst m1, Mininst m2, Mininst m3, Mininst m4)
{
Inst i = (uint64_t) m4 << 48 |
(uint64_t) m3 << 32 |
(uint64_t) m2 << 16 |
m1;
program.push_back(i);
spc += 1;
}
void Program::add_branch(BR_TYPE bt, int rs1, int rs2, std::string tgt)
{
Inst place_holder = 2;
switch(bt)
{
case BR_TYPE::BL:
place_holder = SMC_BL(rs1, rs2, 0);
break;
case BR_TYPE::BEQ:
place_holder = SMC_BEQ(rs1, rs2, 0);
break;
case BR_TYPE::JUMP:
place_holder = SMC_JUMP(0);
break;
}
(branches)[spc] = tgt;
add_inst(place_holder);
}
void Program::add_below(const Program &p)
{
// move instructions from p to the end of
// this program
program.insert(program.end(), p.program.begin(), p.program.end());
// update branch targets and branch instruction pcs
for ( const auto &branch : (p.branches) )
((this->branches))[branch.first + this -> spc] = branch.second;
for ( const auto &label : (p.labels) )
((this->labels))[label.first] = label.second + this -> spc;
// update pc
this -> spc += p.spc;
}
Inst* Program::get_inst_array()
{
linear_analysis();
insert_generated();
// printf("Inserted auto-generated info instructions\n");
preprocess_branches();
// printf("Assigned branch targets to labels\n");
//pretty_print();
int bytes = size();
Inst* arr = (Inst*) malloc(bytes);
int ind = 0;
for (auto & element : (program))
arr[ind++] = element;
return arr;
}
Inst* Program::get_insts()
{
int bytes = size();
Inst* arr = (Inst*) malloc(bytes);
int ind = 0;
for (auto & element : (program))
arr[ind++] = element;
return arr;
}
int Program::size()
{
return program.size() * 8;
}
void Program::preprocess_branches()
{
for ( const auto &branch : (branches) ) {
Inst br = (program)[branch.first];
int lbl = (labels)[branch.second];
// printf("branch addr: %d branch label: %s lbl_adr: %d is_cond: %d\n", branch.first, branch.second.c_str(),
// lbl,is_conditional(br));
assert(is_branch(br) &&
"preprocess_branches(): expected a branch but got something else");
uint64_t target_b = ((uint64_t)lbl) << 8;
uint64_t target_j = (uint64_t) lbl;
br |= is_conditional(br) ? target_b : target_j;
(program)[branch.first] = br;
}
}
/**
* Currently only goes through the whole program
* to see if any constraint is violated
*/
void Program::linear_analysis()
{
bool inSequence = false;
uint seqPc = 0;
int readCtr = 0;
for (uint pc = 0 ; pc < program.size() ; pc++)
{
Inst cur_inst = (program)[pc];
if(!inSequence)
{
if(is_ddr(cur_inst))
{
//printf("Program::linear_analysis(): enter segment at pc: %d\n", pc);
inSequence = true;
seqPc = pc;
readCtr = is_ddr_read(cur_inst);
}
}else
{
// we see a non-ddr inst,
// can commit whatever info we want
// related to the previous segment
if(!is_ddr(cur_inst) && !is_sleep(cur_inst))
{
//printf("Program::linear_analysis(): exit segment at pc: %d with %d reads \n", pc, readCtr);
inSequence = false;
// TODO either directly insert into the vector
// or keep these records elsewhere
if(readCtr > 1024)
{
// TODO Add code that will warn user violently here.
// maybe even an assertion?
std::cerr << "WARNING: SoftMC won't be able to run your program timing-critically!" << std::endl;
}else if(readCtr > 0)
{
// Insert info-encapsulating packet into program
Inst warn_pipeline = SMC_INFO(readCtr);
(warnings)[seqPc] = warn_pipeline;
}
}else
{
readCtr += is_ddr_read(cur_inst);
}
}
}
}
void Program::insert_generated()
{
// WARNING this assumes that map keys are iterated
// in ascending order
std::vector<uint> warn_keys;
std::vector<Inst> warn_values;
for ( const auto &warn : (this->warnings))
{
warn_keys.push_back(warn.first);
warn_values.push_back(warn.second);
}
while (warn_keys.size()>0)
{
// new branch and label mappings
uint warn_key = warn_keys.front();
Inst warn_value = warn_values.front();
warn_keys.erase(warn_keys.begin());
warn_values.erase(warn_values.begin());
std::map<std::string, uint> n_labels;
std::map<uint, std::string> n_branches;
program.insert(program.begin() + warn_key, warn_value);
for ( const auto &branch : (this->branches) )
{
// branch target > inserted instruction pc
if((labels)[branch.second] > warn_key)
{
(n_labels)[branch.second] = (labels)[branch.second] + 1;
}else
{
(n_labels)[branch.second] = (labels)[branch.second];
}
// branching instruction pc > inserted instruction pc
if(branch.first >= warn_key)
{
(n_branches)[branch.first + 1] = branch.second;
}else
{
(n_branches)[branch.first] = branch.second;
}
}
for (uint i = 0 ; i < warn_keys.size() ; i++)
warn_keys[i] += 1;
labels = n_labels;
branches = n_branches;
}
}
void Program::pretty_print()
{
for (uint pc = 0 ; pc < program.size() ; pc++)
{
for ( const auto &label : (labels) )
{
if(label.second == pc)
std::cout << label.first << std::endl;
}
printf("%04d: ", pc);
decode_inst((program)[pc]);
for ( const auto &branch : (branches) )
{
if(branch.first == pc)
std::cout << " (TARGET LABEL: " << branch.second << ")";
}
printf("\n");
}
}
void Program::bin_dump()
{
for (uint pc = 0 ; pc < program.size() ; pc++)
{
for ( const auto &label : (labels) )
{
if(label.second == pc)
std::cout << label.first << std::endl;
}
std::cout << "PC: " << pc << " Inst: " << std::hex << (program)[pc] << std::dec << std::endl;
for ( const auto &branch : (branches) )
{
if(branch.first == pc)
std::cout << branch.second;
}
printf("\n");
}
}
void Program::save_bin(const std::string &fname)
{
// Write instruction binary data to fname.inst
std::ofstream binFile (fname + ".bin", std::ios::out | std::ios::binary);
Inst* insts = this -> get_insts();
int no_insts = spc;
binFile.write ((char*)&no_insts, sizeof(no_insts));
binFile.write ((char*) insts, sizeof(Inst)*no_insts);
binFile.close();
// Write branch label positions to fname.meta
std::ofstream metaFile (fname + ".meta", std::ios::out);
for ( const auto &label : (labels) )
metaFile << label.first << " " << label.second << "\n";
metaFile << "-" << "\n";
for ( const auto &branch : (branches) )
metaFile << branch.first << " " << branch.second << "\n";
metaFile.close();
}
void Program::save_coe_here(const std::string &prj_dir)
{
std::ofstream coeFile ("program.coe", std::ios::out | std::ios::binary);
uint64_t* insts = (uint64_t*) this -> get_inst_array();
coeFile << "memory_initialization_radix=2;\n";
coeFile << "memory_initialization_vector=\n";
for(uint i = 0 ; i < program.size() ; i++){
coeFile << std::bitset<64>(insts[i]);
if( i == (program.size()) -1){
coeFile << ";";
}else{
coeFile << ",\n";
}
}
coeFile.close();
}
void Program::save_coe(const std::string &prj_dir)
{
std::ofstream coeFile (prj_dir + "/VU095.sim/sim_1/behav/xsim/simmem.coe", std::ios::out | std::ios::binary);
std::ofstream coeFile2 (prj_dir + "/coe/simmem.coe", std::ios::out | std::ios::binary);
std::ofstream mifFile (prj_dir + "/VU095.sim/sim_1/behav/xsim/instr_blk_mem_sim.mif", std::ios::out | std::ios::binary);
uint64_t* insts = (uint64_t*) this -> get_inst_array();
coeFile << "memory_initialization_radix=2;\n";
coeFile2 << "memory_initialization_radix=2;\n";
coeFile << "memory_initialization_vector=\n";
coeFile2 << "memory_initialization_vector=\n";
for(uint i = 0 ; i < program.size() ; i++){
coeFile << std::bitset<64>(insts[i]);
coeFile2 << std::bitset<64>(insts[i]);
mifFile << std::bitset<64>(insts[i])<<"\n";
if( i == (program.size()) -1){
coeFile << ";";
coeFile2 << ";";
}else{
coeFile << ",\n";
coeFile2 << ",\n";
}
}
coeFile.close();
coeFile2.close();
mifFile.close();
}
void Program::dump_registers()
{
if(dumpRegsCalled)
{
std::cerr << "You can use your trump card (dump_register) only once per program!" << std::endl;
exit(1);
}
dumpRegsCalled = true;
this -> add_inst(SMC_LI(15,13)); // BAR
this -> add_inst(SMC_LI(0,14)); // RAR
this -> add_inst(SMC_LI(0,15)); // CAR
this -> add_inst(__pack_mininsts(SMC_PRE(13,0,1), // PRECHARGE ALL
SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_ACT(13,0,14,0), // ACT B15 R0
SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_WRITE(13,0,15,0,0,0), // WRITE WDATA CONTENT
SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_READ(13,0,15,0,0,0), // READ WDATA CONTENT
SMC_NOP(),SMC_NOP(),SMC_NOP()));
for (int i = 0 ; i < 16 ; i++) // SWAP WDATA WITH REG VALUES
this -> add_inst(SMC_LDWD(i,i));
this -> add_inst(__pack_mininsts(SMC_WRITE(13,0,15,0,0,0), // WRITE WDATA CONTENT
SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_READ(13,0,15,0,0,0), // READ WDATA CONTENT
SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_NOP(),SMC_NOP(),SMC_NOP(),SMC_NOP()));
this -> add_inst(__pack_mininsts(SMC_PRE(13,0,1), // PRECHARGE ALL
SMC_NOP(),SMC_NOP(),SMC_NOP()));
}
bool Program::isDumpRegsCalled()
{
return dumpRegsCalled;
}
void Program::debug(const std::string &prj_dir, bool first)
{
int pid = fork();
if(pid == 0){
std::string source = prj_dir + "/source.tcl";
std::string project = prj_dir + "/VU095.xpr";
std::string arg;
int fd = open("/dev/null", O_WRONLY);
dup2(fd, 1);
if(first)
arg = "first";
else
arg = "debug";
execl("/opt/Xilinx/Vivado/2018.2/bin/vivado",
"vivado","-mode","tcl","-nojournal","-applog","-log","/tmp/asmc.log",
"-source", source.c_str(), project.c_str(),"-tclargs", arg.c_str(), NULL);
}else if(pid > 0){
std::cout << "Debugger starting...\n";
mkfifo("/tmp/fifo_1", 0666);
int f1 = open("/tmp/fifo_1", O_WRONLY);
mkfifo("/tmp/fifo_2", 0666);
int f2 = open("/tmp/fifo_2", O_RDONLY);
char buf[100];
[[maybe_unused]] int rv;
std::vector<std::string> vios;
std::string line, start, end, vio;
start = "sim_tb_top.mem_model_x8.memModels_Ri1[0].memModel1[0].ddr4_model.always_diff_ck.if_diff_ck:VIOLATION:";
end = "sim_tb_top.mem_model_x8.memModels_Ri1[0].memModel1[1].ddr4_model.always_diff_ck.if_diff_ck:VIOLATION:";
std::string msg, com, param,param2;
std::vector<std::string> commands {"reg", "mem", "time", "step", "run",
"until", "exit", "btwn","stat"};
int state = 0;
while(state != 5){
switch (state){
case 0:
std::cout << "Enter command: ";
std::cin >> com;
if(std::find(commands.begin(), commands.end(), com) != commands.end()){
if(com == "step" || com == "exit" || com == "time" || com == "stat"){
msg = com + "_\n";
}else if(com == "btwn"){
std::cin >> param;
std::cin >> param2;
msg = com + "_" + param + "_" + param2 + "\n";
}else{
std::cin >> param;
msg = com + "_" + param + "\n";
}
state = 1;
}else{
std::cout << "Please enter a valid command...\n";
}
break;
case 1:
rv = write( f1, (char*)msg.c_str(), msg.size());
memset(buf, 0, sizeof(buf));
while(read(f2, buf, sizeof(buf)) == 0);
state = 2;
break;
case 2:
if(com == "reg" || com == "mem")
std::cout << com << " " << param << ": " << buf;
else if(com == "btwn")
std::cout << "Time between "<< param << "-" << param2 << ": " << buf;
else if(com == "time")
std::cout << "Current time: " << buf;
else if(com == "stat")
std::cout << "Stats:\n" << buf;
else if(com == "exit"){
state = 5;
break;
}
state = 3;
break;
case 3:
std::ifstream logfile ("/tmp/asmc.log");
while (std::getline(logfile, line))
{
if(line.find(start) != std::string::npos)
{
vio = line;
if(std::find(vios.begin(), vios.end(), vio) != vios.end())
continue;
std::cout << line.substr(91) << "\n";
while (std::getline(logfile, line))
{
if (line.find(end) != std::string::npos)
break;
std::cout << line << "\n";
}
vios.insert(vios.begin(), vio);
}
}
logfile.close();
state = 0;
break;
}
}
close(f1);
close(f2);
wait(NULL);
remove("/tmp/fifo_1");
remove("/tmp/fifo_2");
remove("/tmp/asmc.log");
std::cout << "Exiting debugger.\n";
}
}
void Program::debug(const std::string &prj_dir, const std::string &filename)
{
int pid = fork();
if(pid == 0){
std::string source = prj_dir + "/source.tcl";
std::string project = prj_dir + "/VU095.xpr";
int fd = open("/dev/null", O_WRONLY);
dup2(fd, 1);
execl("/opt/Xilinx/Vivado/2018.2/bin/vivado",
"vivado","-mode","tcl","-nojournal","-applog","-log","/tmp/asmc.log",
"-source", source.c_str(), project.c_str(),"-tclargs", "update", NULL);
}else if(pid > 0){
std::string path, line, param, value, time_path, temp_path;
path = prj_dir.substr(0, prj_dir.find("VU095")) + "phy_ddr4_ex/imports/";
time_path = path + "timing_tasks.sv";
temp_path = path + "temp.sv";
std::ifstream timing_tasks (time_path);
std::ifstream param_file (filename);
std::ofstream temp_file (temp_path);
std::map <std::string, std::string> params;
while (std::getline(param_file, line)){
param = line.substr(0, line.find("="));
param = param.substr(param.find_first_not_of(" \n\r\t\f\v"),param.find_last_not_of(" \n\r\t\f\v") + 1);
param = " SetTSArray (i" + param;
value = line.substr(line.find("=") + 1);
value = value.substr(value.find_first_not_of(" \n\r\t\f\v"),value.find_last_not_of(" \n\r\t\f\v") + 1);
value = std::string((7- value.size()), ' ') + value;
params[param] = value;
}
param_file.close();
while (std::getline(timing_tasks, line)){
if(line.find(" SetTSArray (i") == std::string::npos){
temp_file << line << "\n";
continue;
}
for(const auto & param : params){
if(line.find(param.first) != std::string::npos){
line = line.replace(35, 7, param.second);
break;
}
}
temp_file << line << "\n";
}
timing_tasks.close();
temp_file.close();
remove(time_path.c_str());
rename(temp_path.c_str(), time_path.c_str());
wait(NULL);
std::cout << "Timing parameters are updated...\n";
}
}
|