Skip to content

Instantly share code, notes, and snippets.

@TerrorBite
Created January 27, 2014 03:03
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 TerrorBite/8642679 to your computer and use it in GitHub Desktop.
Save TerrorBite/8642679 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class rom {
public:
rom ( ifstream & stream );
~rom();
void read ( ifstream & stream );
void dump ( ofstream & stream );
void setPage ( int p );
private:
unsigned char readByte ( ifstream & stream );
unsigned char conv ( unsigned char a );
unsigned char data[0x80 * 0x4000];
int pageNo;
};
rom::rom ( ifstream & stream ) {
memset ( data, 0xff, sizeof ( data ) );
read ( stream );
pageNo = 0;
}
rom::~rom() {
}
void rom::read ( ifstream & stream ) {
try {
while ( 1 ) {
stream.get(); // garbage
int len = readByte ( stream );
int addr = ( ( ( ( int ) readByte ( stream ) ) << 8 ) & 0xff00 ) | ( ( int ) readByte ( stream ) & 0xff );
int type = readByte ( stream );
if ( type == 0 ) {
for ( int i = 0; i < len; i++ ) {
data[pageNo * 0x4000 + (addr + i) % 0x4000] = readByte ( stream );
}
} else if ( type == 2 ) {
pageNo = ( ( ( ( int ) readByte ( stream ) ) << 8 ) & 0xff00 ) | ( ( int ) readByte ( stream ) & 0xff );
if(pageNo > 0x10)
{
pageNo += 0x60;
}
}
else if(type == 1)
{
cout << "END\n";
}
readByte ( stream );
stream.get(); // newline
if ( type == 2 )
cout << "Page " << pageNo << endl;
if ( type == 0x01 ) {
cout << "END\n";
break;
}
}
} catch ( unsigned char c ) {
cout << "Failed to read '" << c << "'\n";
}
}
unsigned char rom::conv ( unsigned char a ) {
if ( a >= 'A' && a <= 'F' ) {
a = a - 'A' + 0xA;
} else if ( a >= '0' && a <= '9' ) {
a -= '0';
} else {
throw a;
}
return a;
}
unsigned char rom::readByte ( ifstream & stream ) {
char a, b;
stream >> a >> b;
return ( conv ( a ) << 4 ) | conv ( b );
}
void rom::dump ( ofstream & stream ) {
stream.write ( (const char*)data, sizeof ( data ) );
}
void rom::setPage ( int p ) {
pageNo = p;
}
int main ( int argc, char ** argv ) {
ifstream in ( argv[1] );
rom p ( in );
in.close();
ofstream out ( argv[2] );
p.dump ( out );
out.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment