/** GameBoy Cartridge Tester *********************************/   
/**                                                         **/
/**                        testall.c                        **/
/**                                                         **/
/** This program written by Pascal Felber will test all of  **/
/** your cartridges finding those which fail CRC check.     **/
/**                                                         **/
/** Copyright (C) Pascal Felber 1996                        **/
/**     You are not allowed to distribute this software     **/
/**     commercially. Please, notify me, if you make any    **/
/**     changes to this file.                               **/
/*************************************************************/

#include <stdio.h>

unsigned char RAM[0x4000];

void main(int argc,char *argv[])
{
  FILE *s;
  unsigned checksum;
  int i, arg = 0;

  if(argc < 2) {
    fprintf(stderr, "Usage: %s file...\n", argv[0]);
    exit(1);
  }

  while(++arg < argc) {
    if(!(s = fopen(argv[arg], "rb"))) {
      perror("fopen");
      exit(1);
    }

    if(fread(RAM, 1, 0x4000, s) == 0x4000) {
      checksum = ((unsigned)RAM[0x014E]<<8)+RAM[0x014F];
      checksum += RAM[0x014E]+RAM[0x014F];
      do {
	for(i = 0; i < 0x4000; i++)
	  checksum -= RAM[i];
      } while(fread(RAM, 1, 0x4000, s) == 0x4000);
    } else {
      fprintf(stderr, "Error while reading %s\n", argv[arg]);
    }

    fclose(s);

    if(checksum & 0xFFFF)
      printf("%s: Checksum error\n", argv[arg]);
  }
}
