?? eeprom.c
字號:
#include "driver.h"#include "eeprom.h"#include "savegame.h"#include <stdarg.h>#include <stdio.h>#include <string.h>#define VERBOSE 0#define SERIAL_BUFFER_LENGTH 40#define MEMORY_SIZE 1024static struct EEPROM_interface *intf;static int serial_count;static char serial_buffer[SERIAL_BUFFER_LENGTH];static unsigned char eeprom_data[MEMORY_SIZE];static int eeprom_data_bits;static int eeprom_read_address;static int eeprom_clock_count;static int latch,reset_line,clock_line,sending;static int locked;static int reset_delay;void logerror(const char *text, ...){#ifdef RAINE_DEBUG va_list arg; va_start(arg, text); vfprintf(stderr, text, arg); va_end(arg);#endif }void EEPROM_init(struct EEPROM_interface *interface){ int size = (1 << interface->address_bits) * interface->data_bits / 8; intf = interface; if (size > MEMORY_SIZE) { // usrintf_showmessage("EEPROM larger than eeprom.c allows"); return; } memset(eeprom_data,0xff,size); serial_count = 0; latch = 0; reset_line = ASSERT_LINE; clock_line = ASSERT_LINE; sending = 0; if (intf->cmd_unlock) locked = 1; else locked = 0; // The following line should include this as a normal eeprom... Hope. add_eeprom(eeprom_data,size,1);}static void EEPROM_write(int bit){#if VERBOSElogerror("EEPROM write bit %d\n",bit);#endif if (serial_count >= SERIAL_BUFFER_LENGTH-1) { logerror("error: EEPROM serial buffer overflow\n"); return; } serial_buffer[serial_count++] = (bit ? '1' : '0'); serial_buffer[serial_count] = 0; /* nul terminate so we can treat it as a string */ if (intf->cmd_read && (unsigned int)serial_count == (strlen(intf->cmd_read) + intf->address_bits) && !strncmp(serial_buffer,intf->cmd_read,strlen(intf->cmd_read))) { int i,address; address = 0; for (i = 0;i < intf->address_bits;i++) { address <<= 1; if (serial_buffer[i + strlen(intf->cmd_read)] == '1') address |= 1; } if (intf->data_bits == 16) eeprom_data_bits = (eeprom_data[2*address+0] << 8) + eeprom_data[2*address+1]; else eeprom_data_bits = eeprom_data[address]; eeprom_read_address = address; eeprom_clock_count = 0; sending = 1; serial_count = 0; logerror("EEPROM read %04x from address %02x\n",eeprom_data_bits,address); } else if (intf->cmd_erase && (unsigned int)serial_count == (strlen(intf->cmd_erase) + intf->address_bits) && !strncmp(serial_buffer,intf->cmd_erase,strlen(intf->cmd_erase))) { int i,address; address = 0; for (i = 0;i < intf->address_bits;i++) { address <<= 1; if (serial_buffer[i + strlen(intf->cmd_erase)] == '1') address |= 1; } logerror("EEPROM erase address %02x\n",address); if (locked == 0) { if (intf->data_bits == 16) { eeprom_data[2*address+0] = 0x00; eeprom_data[2*address+1] = 0x00; } else eeprom_data[address] = 0x00; } elselogerror("Error: EEPROM is locked\n"); serial_count = 0; } else if (intf->cmd_write && (unsigned int)serial_count == (strlen(intf->cmd_write) + intf->address_bits + intf->data_bits) && !strncmp(serial_buffer,intf->cmd_write,strlen(intf->cmd_write))) { int i,address,data; address = 0; for (i = 0;i < intf->address_bits;i++) { address <<= 1; if (serial_buffer[i + strlen(intf->cmd_write)] == '1') address |= 1; } data = 0; for (i = 0;i < intf->data_bits;i++) { data <<= 1; if (serial_buffer[i + strlen(intf->cmd_write) + intf->address_bits] == '1') data |= 1; } logerror("EEPROM write %04x to address %02x\n",data,address); if (locked == 0) { if (intf->data_bits == 16) { eeprom_data[2*address+0] = data >> 8; eeprom_data[2*address+1] = data & 0xff; } else eeprom_data[address] = data; } else logerror("Error: EEPROM is locked\n"); serial_count = 0; } else if (intf->cmd_lock && (unsigned int)serial_count == strlen(intf->cmd_lock) && !strncmp(serial_buffer,intf->cmd_lock,strlen(intf->cmd_lock))) { logerror("EEPROM lock\n"); locked = 1; serial_count = 0; } else if (intf->cmd_unlock && (unsigned int)serial_count == strlen(intf->cmd_unlock) && !strncmp(serial_buffer,intf->cmd_unlock,strlen(intf->cmd_unlock))) { logerror("EEPROM unlock\n"); locked = 0; serial_count = 0; }}static void EEPROM_reset(void){if (serial_count) logerror("EEPROM reset, buffer = %s\n",serial_buffer); serial_count = 0; sending = 0; reset_delay = 5; /* delay a little before returning setting data to 1 (needed by wbeachvl) */}void EEPROM_write_bit(int bit){#if VERBOSElogerror("write bit %d\n",bit);#endif latch = bit;}int EEPROM_read_bit(void){ int res; if (sending) res = (eeprom_data_bits >> intf->data_bits) & 1; else { if (reset_delay > 0) { /* this is needed by wbeachvl */ reset_delay--; res = 0; } else res = 1; }#if VERBOSElogerror("read bit %d\n",res);#endif return res;}void EEPROM_set_cs_line(int state){#if VERBOSElogerror("set reset line %d\n",state);#endif reset_line = state; if (reset_line != CLEAR_LINE) EEPROM_reset();}void EEPROM_set_clock_line(int state){#if VERBOSElogerror("set clock line %d\n",state);#endif if (state == PULSE_LINE || (clock_line == CLEAR_LINE && state != CLEAR_LINE)) { if (reset_line == CLEAR_LINE) { if (sending) { if (eeprom_clock_count == intf->data_bits && intf->enable_multi_read) { eeprom_read_address = (eeprom_read_address + 1) & ((1 << intf->address_bits) - 1); if (intf->data_bits == 16) eeprom_data_bits = (eeprom_data[2*eeprom_read_address+0] << 8) + eeprom_data[2*eeprom_read_address+1]; else eeprom_data_bits = eeprom_data[eeprom_read_address]; eeprom_clock_count = 0;logerror("EEPROM read %04x from address %02x\n",eeprom_data_bits,eeprom_read_address); } eeprom_data_bits = (eeprom_data_bits << 1) | 1; eeprom_clock_count++; } else EEPROM_write(latch); } } clock_line = state;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -