Skip to content

Instantly share code, notes, and snippets.

@asumagic
Created July 27, 2018 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asumagic/7791a9c32d69996da7d7ab783e1d9def to your computer and use it in GitHub Desktop.
Save asumagic/7791a9c32d69996da7d7ab783e1d9def to your computer and use it in GitHub Desktop.
// silly hack job to parse http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html into a list with hex and binary representations of opcodes, in order to find patterns.
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Syntax: ./parse [file]\n";
}
std::cout << std::hex << std::uppercase;
std::ifstream file{argv[1]};
std::string s = {std::istreambuf_iterator{file}, {}};
std::string_view pattern = "<td class=\"withborder\"";
std::string_view bad_pattern = "<td class=\"withborder\" bgcolor=\"#9f9f9f";
auto it = std::search(s.begin(), s.end(), pattern.begin(), pattern.end());
auto set_next_it = [&] { it = std::search(it + pattern.size(), s.end(), pattern.begin(), pattern.end()); };
unsigned row = 0, col = 0;
while (it != s.end())
{
if (std::string_view{&(*it), bad_pattern.size()} == bad_pattern)
{
set_next_it();
continue;
}
std::cout << "Opcode 0x" << row << col << " (0b";
int num = col | (row << 4);
for (int i = 7; i >= 0; --i)
{
std::cout << ((num >> i) & 0b1);
}
std::cout << "): ";
auto name_it = std::find(it, s.end(), '>') + 1;
auto name_ite = std::find(name_it, s.end(), '<');
std::string_view name(&(*name_it), std::distance(name_it, name_ite));
if (name != "&nbsp;")
{
std::cout << name;
}
else
{
std::cout << "** BAD **\n";
set_next_it();
continue;
}
std::string_view dur(&name_ite[4], std::distance(name_ite + 4, std::find(name_ite + 4, s.end(), '<')));
std::cout << "; " << dur[0] << " bytes; " << std::string_view(&dur[13], std::distance(dur.begin() + 13, dur.end())) << " cycles";
set_next_it();
if (col++ == 0xF)
{
++row;
col = 0;
}
std::cout << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment