/////////////////////////////////////////////////////////////////////////////// // Includes /////////////////////////////////////////////////////////////////////////////// #define _GNU_SOURCE #include #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////// // Typedefs /////////////////////////////////////////////////////////////////////////////// typedef unsigned int _int16; typedef unsigned long _int32; typedef unsigned char _byte; /////////////////////////////////////////////////////////////////////////////// // Constants /////////////////////////////////////////////////////////////////////////////// #define VERSION "1.02" #define DEFAULT_PORT 1 #define DEFAULT_SPEED 115200 /////////////////////////////////////////////////////////////////////////////// // Debugging functions /////////////////////////////////////////////////////////////////////////////// // printf() for debugging void odprintf(const char *format, ...) { char buf[4096], *p = buf; va_list args; va_start(args, format); p += _vsnprintf(p, sizeof buf - 1, format, args); va_end(args); while (p > buf && isspace(p[-1])) *--p = '\0'; *p++ = '\r'; *p++ = '\n'; *p = '\0'; OutputDebugString(buf); } #ifdef DEBUG #define LOG(x) odprintf("%s:%s[%d]: %s", __FILE__, __PRETTY_FUNCTION__, __LINE__, x); #else #define LOG(x) #endif /////////////////////////////////////////////////////////////////////////////// // Serial port driver functions /////////////////////////////////////////////////////////////////////////////// static HANDLE hCom; int serial_Init(int comNumber, int baudRate) { DCB dcb; COMMTIMEOUTS CommTimeOuts; char comName[32]; // generate the device name sprintf(comName, "COM%d", comNumber); // open the port if ((hCom = CreateFile(comName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { // error opening port return 1; } CommTimeOuts.ReadIntervalTimeout = MAXDWORD; CommTimeOuts.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 1000; CommTimeOuts.WriteTotalTimeoutMultiplier = 0; CommTimeOuts.WriteTotalTimeoutConstant = 1000; if ( !SetCommTimeouts( hCom, &CommTimeOuts )){ CloseHandle(hCom); return 2; // SetCommTimeouts error } dcb.DCBlength = sizeof ( DCB ) ; dcb.BaudRate = baudRate; dcb.fBinary = TRUE ; dcb.fParity = FALSE; dcb.fOutxCtsFlow = TRUE; // CTS output flow control dcb.fOutxDsrFlow = FALSE; // DSR output flow control dcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type dcb.fDsrSensitivity = FALSE; // DSR sensitivity dcb.fTXContinueOnXoff = 0; // XOFF continues Tx dcb.fOutX = FALSE; // XON/XOFF output flow control dcb.fInX = FALSE; // XON/XOFF input flow control dcb.fErrorChar = 0; // enable error replacement dcb.fNull = FALSE; // enable null stripping dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; // RTS flow control dcb.fAbortOnError = FALSE; // abort reads/writes on error dcb.XonLim = 2048; // transmit XON threshold dcb.XoffLim = 512; // transmit XOFF threshold dcb.ByteSize = 8; // number of bits/byte, 4-8 dcb.Parity = NOPARITY; // 0-4=no,odd,even,mark,space dcb.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 dcb.XonChar = 17; // Tx and Rx XON character dcb.XoffChar = 19; // Tx and Rx XOFF character dcb.ErrorChar = 0; // error replacement character dcb.EofChar = 0; // end of input character dcb.EvtChar = 0; // received event character if (!SetCommState(hCom, &dcb)){ CloseHandle(hCom); return 3; // SetCommState error } return 0; } void serial_Done(void) { CloseHandle(hCom); } /* Read one character when it reads a character, it returns 1 when there is no character to read, it returns 0 */ int serial_ReadChar(unsigned char *c) { DWORD length; if( !ReadFile(hCom, c, 1, &length, NULL) ) return 0; return length; } void serial_WriteChar(unsigned char c) { DWORD length; WriteFile(hCom, &c, 1, &length, NULL); } int serial_ReadBlock(unsigned char *c, size_t size) { DWORD length; if (!ReadFile(hCom, c, size, &length, NULL) ) return 0; return length; } void serial_WriteBlock(unsigned char *c, size_t size) { DWORD length; WriteFile(hCom, c, size, &length, NULL); } void serial_Flush(void) { PurgeComm(hCom, PURGE_RXCLEAR | PURGE_TXCLEAR); } /////////////////////////////////////////////////////////////////////////////// // Syntax error handler / help screen /////////////////////////////////////////////////////////////////////////////// static struct option long_opts[] = { {"input", required_argument, 0, 'i'}, {"device", required_argument, 0, 'd'}, {"port", required_argument, 0, 'p'}, {"speed", required_argument, 0, 's'} }; static char *short_opts = "i:d:p:s:"; void syntax(void) { printf("Syntax:\n"); printf("\tepromemu {--device=|-d} {--input=|-i}\n"); printf("\t {--port | -p} {--speed | -s}\n"); printf("\nWhere:\n"); printf("\t--device (or -d):\tSelect device type (see list below).\n"); printf("\t--input (or -i):\tSet input file name.\n"); printf("\t--port (or -p):\tSet COM port number (default 1 = COM1).\n"); printf("\t--speed (or -s):\tSet COM port baud rate (default 115200).\n"); printf("\n\n"); printf("Supported devices: 2764/64, 27128/128, 27256/256, 27512/512.\n"); } /////////////////////////////////////////////////////////////////////////////// // Entry point /////////////////////////////////////////////////////////////////////////////// int main(int argc, char **argv) { FILE *fin; // Input file unsigned char buffer[256]; // Data buffer unsigned char packet[512]; // Packet buffer unsigned long pktlen; // Packet length unsigned char checksum; // Checksum accumulator unsigned char retCode; // Return code / error code unsigned long i, count; // Loop variables unsigned long addr; // Current address DWORD timeA, timeB; // Time of start/finish double time; // Time taken to load ROM ///// Command line params char *filename = NULL; // Input filename int device = -1; // Device type unsigned long maxaddr; // Maximum address for the selected device int port = DEFAULT_PORT; // COM port number unsigned long speed = DEFAULT_SPEED; // Baud rate ///// Internal versions of the params unsigned char deviceid; // Device ID // Display the startup banner printf("EPROM emulator downloader ver. %s\n", VERSION); printf("Copyleft 2005, Philip Pemberton . All rights reversed.\n"); // Parse the command line while (TRUE) { int opt_index, c; c = getopt_long(argc, argv, short_opts, long_opts, &opt_index); if (c == -1) { break; } switch (c) { case 'i': // "-i" or "--input" --> set input filename filename = strdup(optarg); break; case 'd': // "-d" or "--device" --> set device type for (i=0; i set com port if (port != DEFAULT_PORT) { printf("Can't specify more than one baud rate.\n"); return 1; } for (i=0; i set com port baud rate if (speed != DEFAULT_SPEED) { printf("Can't specify more than one baud rate.\n"); return 1; } for (i=0; i> 8); checksum += (addr >> 8); packet[pktlen++] = (addr & 0xff); checksum += (addr & 0xff); packet[pktlen++] = count-1; checksum += count-1; for (i=0; i> 8); checksum += (addr >> 8); packet[pktlen++] = (addr & 0xff); checksum += (addr & 0xff); packet[pktlen++] = count-1; checksum += count-1; for (i=0; i