read_eeprom.c

#include <asm/io.h>
#include <stdio.h>


delay ()
{
  int i;
  for (i = 0; i < 3000; ++i)
    {
      i++;
      i--;
    }
}

int
get_d ()
{
  int q;

  delay ();
  q = (inb (0x379) & 0x80) ? 0 : 1;
//printf("R     D %d\n",q);
  return q;
}

set_cd (int c, int d)
{
  int o = 0;

  if (c)
    o |= 1;
  if (!d)
    o |= 4;

//printf("W C %d D %d\n",o&1,(o& 4)?0:1);

  outb (o, 0x378);
  delay ();
}


start ()
{
  set_cd (1, 1);
  set_cd (1, 0);
  set_cd (0, 0);
}


send_bit (int i)
{
//printf("Sending %d",i?1:0);
  set_cd (0, i);
  set_cd (1, i);
  set_cd (0, i);
}

stop ()
{
  set_cd (0, 0);
  set_cd (1, 0);
  set_cd (1, 1);
}

int
read_bit ()
{
  int r;
  set_cd (0, 1);
  set_cd (1, 1);
  r = get_d ();
  set_cd (0, 1);

  return r;
}


int
send_7bits (int b)
{
  int c = 0x40;

  while (c)
    {
      send_bit (c & b);
      c >>= 1;
    }

  return read_bit ();
}

int
send_byte (int b)
{
  int c = 0x80;

  while (c)
    {
      send_bit (c & b);
      c >>= 1;
    }

  return read_bit ();
}

int
read_byte ()
{
  int i;
  int r = 0;
  for (i = 0; i < 8; ++i)
    {
      r <<= 1;
      r |= read_bit ();
    }

  read_bit ();
  return r;
}


int
read_address (int u, int a)
{
  int r;
  start ();
  send_byte (0xa0 | (u << 1) | 0);
  send_byte (a);
  start ();
  send_byte (0xa0 | (u << 1) | 1);
  r = read_byte ();
  stop ();
  usleep (1000);

  return r;
}


main ()
{
  unsigned char d;
  unsigned char nd;
  int wd;
  int a;
  FILE *f;
  iopl (3);

  f = fopen ("eeprom.dat", "w");

  set_cd (1, 1);
  usleep (200000);

  start ();

  nd = read_address (0, 0);
  for (a = 0; a < 128; ++a)
    {
      char c;
      d = nd;
      nd = read_address (0, a + 1);

      if ((d > 31) && (d < 127))
        c = d;
      else
        c = '.';

      fwrite (&d, 1, 1, f);

      printf ("%04x:", a);
      if (a & 1)
        {
          printf ("             ");
        }
      else
        {
          wd = nd;
          wd <<= 8;
          wd += d;
          printf (" 0x%4x %5d", wd, wd);
        }

      printf ("  0x%02x %3d '%c'\n", d, d, c);
    }

  fclose (f);
}