openMSX
Dasm_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2
3#include "Dasm.hh"
4
5#include <iostream>
6#include <set>
7
8using namespace openmsx;
9
10TEST_CASE("instructionLength, dasm")
11{
12 std::set<std::string> allInstructions;
13 std::array<uint8_t, 4> opcode = {0, 0, 0, 0};
14 std::string dasmString;
15
16 unsigned count = 0;
17 bool end = false;
18 while (!end) {
19 ++count;
20
21 auto instrLen = instructionLength(opcode);
22 REQUIRE(instrLen);
23 REQUIRE(1 <= *instrLen);
24 REQUIRE(*instrLen <= 4);
25
26 dasmString.clear();
27 auto dasmLen = dasm(std::span(opcode.data(), *instrLen), 0x1234, dasmString);
28 CHECK(dasmLen == *instrLen);
29
30 // check for duplicate instructions
31 auto [it, inserted] = allInstructions.insert(dasmString);
32 CHECK(inserted);
33
34 if (dasmLen != *instrLen || !inserted) {
35 std::string info;
36 for (auto i : xrange(*instrLen)) {
37 strAppend(info, ' ', hex_string<2>(opcode[i]));
38 }
39 std::cout << "opcode:" << info << " (" << dasmString << ")\n";
40 }
41
42 // goto next instruction
43 unsigned idx = *instrLen - 1;
44 while (true) {
45 if (opcode[idx] == 255) {
46 opcode[idx] = 0;
47 if (idx == 0) {
48 end = true;
49 break;
50 }
51 --idx;
52 } else {
53 ++opcode[idx];
54 break;
55 }
56 }
57 }
58 REQUIRE(count == 2'904'196); // found experimentally
59}
TEST_CASE("instructionLength, dasm")
Definition Dasm_test.cc:10
CHECK(m3==m3)
This file implemented 3 utility functions:
Definition Autofire.cc:11
std::optional< unsigned > instructionLength(std::span< const uint8_t > bin)
Calculate the length of the instruction at the given address.
Definition Dasm.cc:26
unsigned dasm(std::span< const uint8_t > opcode, uint16_t pc, std::string &dest, function_ref< void(std::string &, uint16_t)> appendAddr)
Disassemble.
Definition Dasm.cc:38
void strAppend(std::string &result, Ts &&...ts)
Definition strCat.hh:752
constexpr auto xrange(T e)
Definition xrange.hh:132
constexpr auto end(const zstring_view &x)