Skip to content

Instantly share code, notes, and snippets.

@daeken
Created August 3, 2012 17:31
Show Gist options
  • Save daeken/3249823 to your computer and use it in GitHub Desktop.
Save daeken/3249823 to your computer and use it in GitHub Desktop.
#include <EEPROM.h>
#define CONSERVATIVE
int ioPin = 3;
#define BUFSIZE 200
unsigned char buf[BUFSIZE];
#define pullLow() pinMode(ioPin, OUTPUT)
#define pullHigh() pinMode(ioPin, INPUT)
unsigned char dbits[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 1, 1, 0, 0, 0, 0};
unsigned char bits[][144] = {
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 1, 0,
0, 1,
1, 0, 0, 0, 1, 0, 0, 0, 1,
1, 0, 1, 0, 0, 1, 0, 1, 1,
1, 1, 0, 0, 0, 0, 1, 1, 1,
0, 0, 0, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0}
};
unsigned char bval;
void wentLow() {
bval = 1;
}
int readData(unsigned long int addr) {
for(int i = 0; i < 8; ++i)
dbits[50 + 9 + i] = (addr >> i) & 1;
for(int i = 0; i < 8; ++i)
dbits[50 + i] = (addr >> (i + 8)) & 1;
for(int i = 0; i < 8; ++i)
dbits[50 + 9 + 9 + i] = ((addr >> i) ^ (addr >> (i + 8)) ^ (0x1D >> i)) & 1;
for(int i = 0; i < sizeof(dbits); ++i) {
if(dbits[i] == 0) {
pullLow();
delayMicroseconds(16);
pullHigh();
delayMicroseconds(190);
} else {
pullLow();
delayMicroseconds(16);
pullHigh();
delayMicroseconds(56);
pullLow();
delayMicroseconds(16);
pullHigh();
delayMicroseconds(112);
}
}
pullLow();
delayMicroseconds(16);
pullHigh();
bval = 0;
attachInterrupt(1, wentLow, FALLING);
unsigned int i = 0;
while(digitalRead(ioPin) == HIGH && i++ < 32767) {
//delayMicroseconds(20);
}
if(i == 32767)
return false;
delayMicroseconds(20);
int hit = 0;
for(int i = 0; i < 165; ++i) {
buf[i] = 0;
pullLow();
delayMicroseconds(8);
pullHigh();
bval = 0;
delayMicroseconds(184);
buf[i] = bval;
hit |= bval;
}
return hit;
}
int getByte(int off) {
int i = 0, val = 0;
for(; i < 8; ++i)
val = (val << 1) | buf[13 + off * 9 + 7 - i];
return val;
}
void open() {
pinMode(ioPin, OUTPUT);
digitalWrite(ioPin, LOW);
pinMode(ioPin, INPUT);
digitalWrite(ioPin, LOW);
if(!readData(0x110))
return;
for(int i = 0; i < 4; ++i) {
unsigned char val = 0;
for(int j = 0; j < 8; ++j)
val = (val << 1) | buf[22 + i*9 + 7 - j];
EEPROM.write(i, val);
}
for(int i = 0; i < 32+3; ++i)
bits[0][50+i] = buf[22+i];
for(int i = 0; i < 8; ++i) {
bits[0][86+i] = bits[0][50+i] ^ bits[0][50+9+i] ^ bits[0][50+18+i] ^ bits[0][50+27+i];
}
bits[0][86] ^= 1;
bits[0][87] ^= 0;
bits[0][88] ^= 1;
bits[0][89] ^= 1;
bits[0][90] ^= 1;
bits[0][91] ^= 0;
bits[0][92] ^= 1;
bits[0][93] ^= 1;
for(int i = 0; i < 4; ++i) {
readData(0x412C + (i << 4));
for(int j = 0; j < 4; ++j) {
for(int x = 0; x < 4; ++x)
EEPROM.write(4 + 3 + (i << 4) + (j << 2) + x, getByte((j << 2) + x));
}
}
readData(0x124);
for(int i = 0; i < 3; ++i)
EEPROM.write(4 + i, getByte(i));
#ifdef CONSERVATIVE
delay(100);
#endif
for(int j = 0; j < 11; ++j) {
for(int i = 0; i < sizeof(bits[j]); ++i) {
if(bits[j][i] == 0) {
pullLow();
delayMicroseconds(16);
pullHigh();
delayMicroseconds(190);
} else {
pullLow();
delayMicroseconds(16);
pullHigh();
delayMicroseconds(56);
pullLow();
delayMicroseconds(16);
pullHigh();
delayMicroseconds(112);
}
}
#ifdef CONSERVATIVE
delayMicroseconds(2700);
#else
delayMicroseconds(500);
#endif
}
}
void setup() {
Serial.begin(115200);
dump();
}
void dump() {
while(Serial.available())
Serial.read();
for(int i = 0; i < 4; ++i) {
int val = EEPROM.read(i);
if(val < 16)
Serial.print('0');
Serial.print(val, HEX);
}
Serial.print('\n');
for(int i = 0; i < 3; ++i) {
int val = EEPROM.read(4 + i);
if(val < 16)
Serial.print('0');
Serial.print(val, HEX);
}
Serial.print('\n');
for(int i = 0; i < 64; ++i) {
int val = EEPROM.read(4 + 3 + i);
if(val < 16)
Serial.print('0');
Serial.print(val, HEX);
if((i & 3) == 3)
Serial.print('\n');
}
Serial.print('\n');
/*for(unsigned long int i = 0x4100; i < 0x4200UL; i += 16) {
if(!readData(i))
return;
Serial.print(i, HEX);
Serial.print(" | ");
for(int j = 0; j < 16; ++j) {
int val = getByte(j);
if(val < 16)
Serial.print('0');
Serial.print(val, HEX);
Serial.print(' ');
if(j == 7)
Serial.print(' ');
}
Serial.print('\n');
}*/
}
void loop() {
if(Serial.available())
dump();
open();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment