-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulator.cpp
194 lines (132 loc) · 4.46 KB
/
simulator.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
#include "simulator.h"
#include "ui_simulator.h"
#include "logic.h"
#include<string>
#include<QFile>
#include<QTextStream>
#include<QMessageBox>
simulator::simulator(QWidget *parent) : QWidget(parent),ui(new Ui::simulator)
{
ui->setupUi(this);
}
simulator::~simulator()
{
delete ui;
}
// reads data from file runned by iverilog and copy the data into the regfile and datamem plaintext
void simulator::get_data()
{
QFile file ("D:\\vResults/1.txt");
file.open(QIODevice::ReadOnly);
/*if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}*/
QTextStream textStream (&file);
QString line = textStream.readAll();
file.close();
ui->reg_file->setPlainText(line);
//endreg
QFile memory ("D:\\vResults/2.txt");
memory.open(QIODevice::ReadOnly);
/*if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}*/
QTextStream textStream2 (&memory);
QString line2 = textStream2.readAll();
memory.close();
ui->data_memory->setPlainText(line2);
}
//---------------------------------
// run verilog file
/*void run_verilog ()
{
system("cd C:\\Modeltech_pe_edu_10.4a\\examples & vsim -c fMips -do commands.txt" );
}*/
//takes the binary code and call iverilog to run the code , then compare the results with the
//expected output
void simulator::compare_results()
{
QString res;
QString num = ui->num_testcases->toPlainText();
int n = num.toInt();
for (int i = 1 ; i<=n; i++)
{
QString s1 = QString::number(i) + ".txt";
QString s2 = QString::number(i+10) + ".txt";
//get instruction from txt file convert it to bin ,run verilog,dlete file contents
get_inst(s1);
//compare files with each other
//reg result
QFile file_reg ("D:\\vResults/1.txt");
file_reg.open(QIODevice::ReadOnly);
/*if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}*/
QTextStream textStream1 (&file_reg);
QString line1 = textStream1.readAll();
file_reg.close();
//----
//expected reg result
QFile expected_reg ("D:\\expected_Res/" + s1);
expected_reg.open(QIODevice::ReadOnly);
/* if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}*/
QTextStream textStream2 (&expected_reg);
QString line2 = textStream2.readAll();
expected_reg.close();
//-----
//mem verilog result
QFile file_mem ("D:\\vResults/2.txt");
file_mem.open(QIODevice::ReadOnly);
/*if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}*/
QTextStream textStream3 (&file_mem);
QString line3 = textStream3.readAll();
file_mem.close();
//----
//mem expected result
QFile expected_mem ("D:\\expected_Res/" + s2);
expected_mem.open(QIODevice::ReadOnly);
/*if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}*/
QTextStream textStream4 (&expected_mem);
QString line4 = textStream4.readAll();
expected_mem.close();
//------
if((line1 == line2) && (line3 == line4))
{
res+="Test case "+ QString::number(i) + " : Accepted!\n";
}
else {
res+="Test case "+ QString::number(i) + " : NO\n";
}
}
ui->testcase->setTextColor(QColor("blue"));
ui->testcase->setPlainText(res);
}
void simulator::on_run_clicked()
{
QString code = ui->code_textedit->toPlainText();
QFile file ("D:\\testCase/1.txt");
QStringList strList = code.split(QRegExp("[\n]"),QString::SkipEmptyParts);
if (file.open(QIODevice::ReadWrite))
{
QTextStream stream(&file);
for(auto i : strList){
QString convertedString= to_binary(i);
stream<< convertedString<<endl;
}
}
// save_binarycode(code);
run_verilog();
//delete file contets to be reused
file.resize(0);
get_data(); // get data from files runed by iverilog
}
void simulator::on_pushButton_clicked()
{
compare_results();
}