

#include <stdio.h>

const char *opNames[] = { "?", "IO.WB", "IO.WW","IO.RB","IO.RW", "SER.W","SER.R","KYB" }; 

int main(int argc, char *argv[]){

	if(argc < 2){
		printf("%s [logFile]\n", argv[0]);
		return -1;
	}

	FILE *binFile = fopen(argv[1], "rb");

	int lastOp = 0, collectVal = 0;
	unsigned short port, lastPort, val, lastVal;

	while(!feof(binFile)){

		int op = fgetc(binFile);
		if(op >= 1 && op < 5){
			fread(&port, 2, 1, binFile);
			fread(&val, 2, 1, binFile);
			if(lastOp != op || lastPort != port){
				printf("\n%s(%x): ", opNames[op], port);
				printf("%02x ", val);
				collectVal = 1;
			}else{
				if(val == lastVal){
					collectVal++;
				}else{
					if(collectVal > 1){
						printf("x%i", collectVal);
					}
					printf(", %02x ", val);
				}

			}

			lastPort = port;
			lastVal = val;
		}else{
			val = fgetc(binFile);
			if(lastOp != op) 
                               printf("\n%s: ", opNames[op]);

                        printf("%c", val);
		}

		lastOp = op;
	}
	fclose(binFile);
}
