#include <eeprom.h>

#define SR PB4

unsigned char data;

/*unsigned char data0;
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char data4;
unsigned char data5;
unsigned char data6;
unsigned char data7;

unsigned char data8;
unsigned char data9;
unsigned char data10;
unsigned char data11;
unsigned char data12;
unsigned char data13;
unsigned char data14;
unsigned char data15;

unsigned char data16;
unsigned char data17;
unsigned char data18;
unsigned char data19;
unsigned char data20;
unsigned char data21;
unsigned char data22;
unsigned char data23;

unsigned char data24;
unsigned char data25;
unsigned char data26;
unsigned char data27;
unsigned char data28;
unsigned char data29;
unsigned char data30;
unsigned char data31;*/

void MEMRD(unsigned char h, unsigned char l)
{
	ROMRD(h, l);
	data = SPIRD();
	SPI_PORT |= (1<<ROM);
	
	SPIWR(data);
	SPI_PORT |= (1<<SR);
	_delay_us(1);
	SPI_PORT &= ~(1<<SR);
}

int main(void)
{
	SPI_Init();
	SPI_PORT |= (1<<ROM);
	SPI_PORT &= ~(1<<SR);
	
	DDRD |= (1<<PIND5);

	while(1)
	{
		data = ROMRDSR();
		SPIWR(data);
		SPI_PORT |= (1<<SR);
		_delay_us(1);
		SPI_PORT &= ~(1<<SR);
		
		PORTD ^= (1<<PIND5);		// 0bxxxx0000
		_delay_ms(2000);
		
		SPI_PORT &= ~(1<<ROM);
		SPIWR(WREN);
		_delay_us(1);
		SPI_PORT |= (1<<ROM);
		_delay_ms(5);
		
		PORTD ^= (1<<PIND5);
		_delay_ms(2000);
		
		data = ROMRDSR();
		SPIWR(data);
		SPI_PORT |= (1<<SR);
		_delay_us(1);
		SPI_PORT &= ~(1<<SR);
		
		PORTD ^= (1<<PIND5);		// 0bxxxx0010
		_delay_ms(2000);
		
		SPI_PORT |= (1<<ROM);
		SPIWR(WRDI);
		_delay_us(1);
		SPI_PORT &= ~(1<<ROM);
		_delay_ms(5);
		
		PORTD ^= (1<<PIND5);
		_delay_ms(2000);
		
		
	}
	
	/*ROMWR(0x00, 0x00);
		SPIWR(0x01);
		SPIWR(0x02);
		SPIWR(0x03);
		SPIWR(0x04);
		SPIWR(0x05);
		SPIWR(0x06);
		SPIWR(0x07);
		SPIWR(0x08);
		
		SPIWR(0x10);
		SPIWR(0x20);
		SPIWR(0x30);
		SPIWR(0x40);
		SPIWR(0x50);
		SPIWR(0x60);
		SPIWR(0x70);
		SPIWR(0x80);
		
		SPIWR(0x01);
		SPIWR(0x02);
		SPIWR(0x04);
		SPIWR(0x08);
		SPIWR(0x10);
		SPIWR(0x20);
		SPIWR(0x40);
		SPIWR(0x80);
		
		SPIWR(0x11);
		SPIWR(0x22);
		SPIWR(0x33);
		SPIWR(0x44);
		SPIWR(0x55);
		SPIWR(0x66);
		SPIWR(0x77);
		SPIWR(0x88);
		SPI_PORT |= (1<<ROM);
		_delay_ms(100);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x00);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x01);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x02);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x03);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x04);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x05);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x06);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x07);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x1F);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x20);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x0A);
		_delay_ms(1000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x0B);
		_delay_ms(2000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x0C);
		_delay_ms(2000);
		PORTD ^= (1<<PIND5);
		
		MEMRD(0x00, 0x00);
		_delay_ms(2000);
		PORTD ^= (1<<PIND5);*/

}