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
|
// SPDX-License-Identifier: GPL-2.0+
/*******************************************************************************
* QtMips - MIPS 32-bit Architecture Subset Simulator
*
* Implemented to support following courses:
*
* B35APO - Computer Architectures
* https://cw.fel.cvut.cz/wiki/courses/b35apo
*
* B4M35PAP - Advanced Computer Architectures
* https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start
*
* Copyright (c) 2017-2019 Karel Koci<cynerd@email.cz>
* Copyright (c) 2019 Pavel Pisa <pisa@cmp.felk.cvut.cz>
*
* Faculty of Electrical Engineering (http://www.fel.cvut.cz)
* Czech Technical University (http://www.cvut.cz/)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
******************************************************************************/
#include "reporter.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <typeinfo>
#include <qtmipsexception.h>
using namespace machine;
using namespace std;
Reporter::Reporter(QCoreApplication *app, QtMipsMachine *machine) : QObject() {
this->app = app;
this->machine = machine;
connect(machine, SIGNAL(program_exit()), this, SLOT(machine_exit()));
connect(machine, SIGNAL(program_trap(machine::QtMipsException&)), this, SLOT(machine_trap(machine::QtMipsException&)));
connect(machine->core(), SIGNAL(stop_on_exception_reached()), this, SLOT(machine_exception_reached()));
e_regs = false;
e_cache_stats = false;
e_cycles = false;
e_fail = (enum FailReason)0;
}
void Reporter::regs() {
e_regs = true;
}
void Reporter::cache_stats() {
e_cache_stats = true;
}
void Reporter::cycles() {
e_cycles = true;
}
void Reporter::expect_fail(enum FailReason reason) {
e_fail = (enum FailReason)(e_fail | reason);
}
void Reporter::add_dump_range(std::uint32_t start, std::uint32_t len, QString fname) {
dump_ranges.append({start, len, fname});
}
void Reporter::machine_exit() {
report();
if (e_fail != 0) {
cout << "Machine was expected to fail but it didn't." << endl;
app->exit(1);
} else
app->exit();
}
void Reporter::machine_exception_reached() {
ExceptionCause excause;
excause = machine->get_exception_cause();
switch (excause) {
case EXCAUSE_NONE:
cout << "Machine stopped on NONE exception." << endl;
break;
case EXCAUSE_INT:
cout << "Machine stopped on INT exception." << endl;
break;
case EXCAUSE_ADDRL:
cout << "Machine stopped on ADDRL exception." << endl;
break;
case EXCAUSE_ADDRS:
cout << "Machine stopped on ADDRS exception." << endl;
break;
case EXCAUSE_IBUS:
cout << "Machine stopped on IBUS exception." << endl;
break;
case EXCAUSE_DBUS:
cout << "Machine stopped on DBUS exception." << endl;
break;
case EXCAUSE_SYSCALL:
cout << "Machine stopped on SYSCALL exception." << endl;
break;
case EXCAUSE_OVERFLOW:
cout << "Machine stopped on OVERFLOW exception." << endl;
break;
case EXCAUSE_TRAP:
cout << "Machine stopped on TRAP exception." << endl;
break;
case EXCAUSE_HWBREAK:
cout << "Machine stopped on HWBREAK exception." << endl;
break;
default:
break;
}
report();
app->exit();
}
void Reporter::machine_trap(QtMipsException &e) {
report();
bool expected = false;
auto& etype = typeid(e);
if (etype == typeid(QtMipsExceptionUnsupportedInstruction))
expected = e_fail & FR_I;
else if (etype == typeid(QtMipsExceptionUnsupportedAluOperation))
expected = e_fail & FR_A;
else if (etype == typeid(QtMipsExceptionOverflow))
expected = e_fail & FR_O;
else if (etype == typeid(QtMipsExceptionUnalignedJump))
expected = e_fail & FR_J;
cout << "Machine trapped: " << e.msg(false).toStdString() << endl;
app->exit(expected ? 0 : 1);
}
static void out_hex(ostream &out, std::uint64_t val, int digits) {
std::ios_base::fmtflags saveflg(out.flags());
char prevfill = out.fill('0');
out.setf(ios::hex, ios::basefield);
out << setfill('0') << setw(digits) << val;
out.fill(prevfill);
out.flags(saveflg);
}
void Reporter::report() {
cout << dec;
if (e_regs) {
cout << "Machine state report:" << endl;
cout << "PC:0x";
out_hex(cout, machine->registers()->read_pc(), 8);
cout << endl;
for (int i = 0; i < 32; i++) {
cout << "R" << i << ":0x";
out_hex(cout, machine->registers()->read_gp(i), 8);
if (i != 31)
cout << " ";
else
cout << endl;
}
cout << "HI:0x";
out_hex(cout, machine->registers()->read_hi_lo(true), 8);
cout << " LO:0x";
out_hex(cout, machine->registers()->read_hi_lo(false), 8);
cout << endl;
for (int i = 1; i < Cop0State::COP0REGS_CNT; i++) {
cout << Cop0State::cop0reg_name((Cop0State::Cop0Registers)i).toLocal8Bit().data() << ":0x";
out_hex(cout, machine->cop0state()->read_cop0reg((Cop0State::Cop0Registers)i), 8);
if (i != Cop0State::COP0REGS_CNT - 1)
cout << " ";
else
cout << endl;
}
}
if (e_cache_stats) {
cout << "Cache statistics report:" << endl;
cout << "i-cache:reads:" << machine->cache_program()->memory_reads() << endl;
cout << "i-cache:hit:" << machine->cache_program()->hit() << endl;
cout << "i-cache:miss:" << machine->cache_program()->miss() << endl;
cout << "i-cache:hit-rate:" << machine->cache_program()->hit_rate() << endl;
cout << "i-cache:stalled-cycles:" << machine->cache_program()->stalled_cycles() << endl;
cout << "i-cache:improved-speed:" << machine->cache_program()->speed_improvement() << endl;
cout << "d-cache:reads:" << machine->cache_data()->memory_reads() << endl;
cout << "d-cache:writes:" << machine->cache_data()->memory_writes() << endl;
cout << "d-cache:hit:" << machine->cache_data()->hit() << endl;
cout << "d-cache:miss:" << machine->cache_data()->miss() << endl;
cout << "d-cache:hit-rate:" << machine->cache_data()->hit_rate() << endl;
cout << "d-cache:stalled-cycles:" << machine->cache_data()->stalled_cycles() << endl;
cout << "d-cache:improved-speed:" << machine->cache_data()->speed_improvement() << endl;
}
if (e_cycles) {
cout << "cycles:" << machine->core()->cycles() << endl;
cout << "stalls:" << machine->core()->stalls() << endl;
}
foreach (DumpRange range, dump_ranges) {
ofstream out;
out.open(range.fname.toLocal8Bit().data(), ios::out | ios::trunc);
std::int32_t start = range.start & ~3;
std::int32_t end = range.start + range.len;
if (end < start)
end = 0xffffffff;
for (std::int32_t addr = start; addr < end; addr += 4) {
out << "0x";
out_hex(out, machine->memory()->read_word(addr), 8);
out << endl;
}
out.close();
}
}
|