#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <utility>

#define bits std::bitset<sizeof(uint16_t) * 8>

void getbs(std::ifstream& f, uint16_t& d)
{
	f.read((char*)&d, 2);
	d=(d&0xff00)>>8|d<<8;
}
		   
int main(int argc, char** argv)
{
	std::ifstream f(argv[1], std::ifstream::in|std::ifstream::binary);

	uint16_t last, diff, temp = 0;
	f.seekg(2, std::ios::beg);
	getbs(f, last);
	
	bool coalesce = false;
	uint16_t start, end;
	
	uint16_t test_val = 0;
	
	while (f.good()) {
		static int x = 1;
		f.seekg(64*x+2, std::ios::beg);
		getbs(f, temp);
		
		if (x == 1) {
			test_val = temp;
			std::cout << "testing 0x" << std::hex << test_val << std::endl;
		}
		
		diff = temp - last;
		
		if (diff != 1) {
			if (diff == 0) {
				if (!coalesce) {
					start = x-1;
					coalesce = true;
				}
				goto no_diff;
			}
			else if (coalesce) {
				end = x-1;
				std::cout << std::hex << start << " to " << end << "\t0" << std::endl;
				coalesce = false;
			}
			
			std::cout << std::hex << x-1
			<< "\t" << std::hex << diff
			<< "\t" << bits(diff);
			
			if ((diff & test_val) == test_val)
				std::cout << "\t" << bits(diff & ~test_val);
			
			std::cout << std::endl;
		}
	no_diff:
		last = temp;
		x++;
	}
}
