dingusppc/cpu/ppc/test/testdisasm.cpp

124 lines
3.4 KiB
C++
Raw Normal View History

/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
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 3 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, see <https://www.gnu.org/licenses/>.
*/
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};
2023-12-02 02:05:56 +00:00
ctx.instr_addr = (uint32_t)stoul(tokens[0], NULL, 16);
ctx.instr_code = (uint32_t)stoul(tokens[1], NULL, 16);
2020-02-12 21:58:24 +00:00
/* build disassembly string out of comma-separated parts */
ostringstream idisasm;
/* put instruction mnemonic padded with trailing spaces */
idisasm << tokens[2];
2023-12-02 02:05:56 +00:00
idisasm << setw(8 - (int)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;
}