dingusppc/cpu/ppc/test/testdisasm.cpp

103 lines
2.5 KiB
C++
Raw Normal View History

2020-05-12 18:55:45 +00:00
#include "../ppcdisasm.h"
2020-02-12 21:58:24 +00:00
#include <fstream>
#include <iomanip>
2020-05-12 18:55:45 +00:00
#include <iostream>
#include <sstream>
2020-02-12 21:58:24 +00:00
#include <string>
2020-05-12 18:55:45 +00:00
#include <vector>
2020-02-12 21:58:24 +00:00
using namespace std;
/** testing vehicle */
2020-05-12 18:55:45 +00:00
static vector<PPCDisasmContext> read_test_data() {
string line, token;
int i, lineno;
2020-02-12 21:58:24 +00:00
PPCDisasmContext ctx;
vector<PPCDisasmContext> tstvec;
2020-05-12 18:55:45 +00:00
ifstream tfstream("ppcdisasmtest.csv");
2020-02-12 21:58:24 +00:00
if (!tfstream.is_open()) {
cout << "Could not open tests CSV file. Exiting..." << endl;
return tstvec;
}
lineno = 0;
2020-05-12 18:55:45 +00:00
while (getline(tfstream, line)) {
2020-02-12 21:58:24 +00:00
lineno++;
if (line.empty() || !line.rfind("#", 0))
2020-05-12 18:55:45 +00:00
continue; // skip empty/comment lines
2020-02-12 21:58:24 +00:00
istringstream lnstream(line);
vector<string> tokens;
2020-05-12 18:55:45 +00:00
while (getline(lnstream, token, ',')) {
// cout << "Token: " << token << endl;
2020-02-12 21:58:24 +00:00
tokens.push_back(token);
}
if (tokens.size() < 3) {
cout << "Too few values in line " << lineno << ". Skipping..." << endl;
continue;
}
2020-05-12 18:55:45 +00:00
ctx = {0};
2020-02-12 21:58:24 +00:00
ctx.instr_addr = stoul(tokens[0], NULL, 16);
ctx.instr_code = stoul(tokens[1], NULL, 16);
/* build disassembly string out of comma-separated parts */
ostringstream idisasm;
/* put instruction mnemonic padded with trailing spaces */
idisasm << tokens[2];
idisasm << setw(8 - tokens[2].length()) << "";
2020-02-12 21:58:24 +00:00
/* now add comma-separated operands */
for (i = 3; i < tokens.size(); i++) {
if (i > 3)
idisasm << ", ";
idisasm << tokens[i];
}
ctx.instr_str = idisasm.str();
2020-05-12 18:55:45 +00:00
// cout << idisasm.str() << endl;
2020-02-12 21:58:24 +00:00
tstvec.push_back(ctx);
}
return tstvec;
}
2020-05-12 18:55:45 +00:00
int test_ppc_disasm() {
2020-02-12 21:58:24 +00:00
int i, nfailed;
PPCDisasmContext ctx;
vector<PPCDisasmContext> testdata = read_test_data();
cout << "Imported " << testdata.size() << " test instructions." << endl;
nfailed = 0;
for (i = 0; i < testdata.size(); i++) {
2020-05-12 18:55:45 +00:00
ctx = {0};
2020-02-12 21:58:24 +00:00
ctx.instr_addr = testdata[i].instr_addr;
ctx.instr_code = testdata[i].instr_code;
ctx.simplified = true;
std::string disas = disassemble_single(&ctx);
if (disas != testdata[i].instr_str) {
2020-05-12 18:55:45 +00:00
cout << "Mismatch found, expected={" << testdata[i].instr_str << "}, got={" << disas
<< "}" << endl;
2020-02-12 21:58:24 +00:00
nfailed++;
}
}
2020-05-12 18:55:45 +00:00
cout << "Tested " << testdata.size() << " instructions. Failed: " << nfailed << "." << endl;
2020-02-12 21:58:24 +00:00
return 0;
}