#include "interface.h" FILE* fropen(char *filename) { FILE *fh; if ((fh = fopen(filename,"rb"))==NULL) { printf("Cannot read input file: %s", filename); _exit(0); } return(fh); } FILE* fwopen(char *filename) { FILE *fh; if ((fh = fopen(filename,"wb"))==NULL) { printf("Cannot write output file: %s\n", filename); _exit(0); } return(fh); } void makeCommand(char c, int a, int d, unsigned char* command) { // packet format: |command (1B)|address (4B)|size (4B)| command[0] = c; command[1] = (a>>24) & 0xFF; command[2] = (a>>16) & 0xFF; command[3] = (a>>8) & 0xFF; command[4] = a & 0xFF; command[5] = (d>>24) & 0xFF; command[6] = (d>>16) & 0xFF; command[7] = (d>>8) & 0xFF; command[8] = d & 0xFF; } int toInt( unsigned char *inBuf ) { int b1 = inBuf[0]<<24 | inBuf[1]<<16 | inBuf[2]<<8 | inBuf[3]; return b1; } inline int getFinishStatus(CSerial2 *s) { unsigned char inBuf[4]; unsigned char command[9]; char c = 'F'; int a = 0; int d = 0; makeCommand(c,a,d,command); s->SendData(command, 9); s->ReadDataBlocking(inBuf,4, 0); int b1 = toInt( inBuf ); return b1; } void startExecution(CSerial2 *s) { unsigned char command[9]; char c = 'S'; int a = 0; int d = 0; makeCommand(c,a,d,command); printf("Sending command 'start'.\n"); s->SendData(command, 9); } void waitForFPGA(CSerial2 *s) { unsigned char inBuf[4]; unsigned char command[9]; char c = 'F'; int a = 0; int d = 0; makeCommand(c,a,d,command); int b1 = 0; printf("\nWaiting the system to finish execution... ( )"); while( !b1 ) { s->SendData(command, 9); s->ReadDataBlocking(inBuf,4, 0); b1 = toInt( inBuf ); }; printf("\b\bdone)\n"); printf("Execution finished.\n\n"); } int getExecutionTime(CSerial2 *s) { unsigned char inBuf[4]; unsigned char command[9]; char c = 'T'; int a = 0; int d = 0; makeCommand(c,a,d,command); printf("Sending command 'get execution time'.\n"); s->SendData(command, 9); printf("Receiving time... ( )"); s->ReadDataBlocking(inBuf,4, 0); printf("\b\bdone)\n"); int b1 = toInt( inBuf ); return( b1 ); } void exitFPGA(CSerial2 *s) { unsigned char inBuf[4]; unsigned char command[9]; char c = 'Q'; int a = 0; int d = 0; makeCommand(c,a,d,command); printf("Sending command 'exit'.\n"); s->SendData(command, 9); printf("Receiving acknowledge... ( )"); s->ReadDataBlocking(inBuf,1, 0); if( inBuf[0]=='E' ) { printf("\b\bdone)\n"); // printf("Acnowledge received.\n"); } printf("\n"); } void writeBytes(unsigned char *outBuf, int nBytes, int address, CSerial2 *s) { unsigned char *pBuf = outBuf; unsigned char command[9]; int iAllBytes; char c = 'w'; makeCommand(c,address,nBytes,command); printf("Sending command 'write'.\n"); s->SendData(command, 9); printf("\nSending %d bytes to address 0x%X: \n", nBytes, address); iAllBytes = nBytes; unsigned int sBytes = 1000; while( nBytes != 0 ) { if( nBytes < 1000 ) { sBytes = nBytes; } else if( nBytes < 0 ) { sBytes = abs(nBytes); } sBytes = s->SendData(pBuf, sBytes); pBuf += sBytes; nBytes = nBytes - sBytes; printf("\rUploading %08d bytes : %08d bytes left... ", iAllBytes, nBytes); fflush(stdout); } printf("(done)\n"); } void readBytes(unsigned char *inBuf, int nBytes, int address, CSerial2 *s) { unsigned char command[9]; char c = 'r'; makeCommand(c,address,nBytes,command); printf("Sending command 'read'.\n"); s->SendData(command, 9); printf("Receiving %d bytes from address 0x%X:\n", nBytes, address); fflush(stdout); s->ReadDataBlocking(inBuf,nBytes, 1); printf("(done)\n"); } void writeInt32(int *outBuf, int words, int address, CSerial2 *s) { unsigned char *pBuf = (unsigned char *)outBuf; unsigned char command[9]; char c = 'W'; int nBytes = words*4; makeCommand(c,address,nBytes,command); printf("Sending command 'write'.\n"); s->SendData(command, 9); printf("Sending %d bytes to address 0x%X... ( )", nBytes, address); char symb[4] = {'\\', '|', '/', '-'}; int ctr = 0; unsigned int sBytes = 1000; int sentBytes = 0; while( nBytes != 0 ) { if( nBytes < 1000 ) { sBytes = nBytes; } else if( nBytes < 0 ) { sBytes = abs(nBytes); } sentBytes = s->SendData(pBuf, sBytes); printf("\b\b%c)", symb[ctr++]); if( ctr==4 ) ctr=0; pBuf += sBytes; nBytes = nBytes - sBytes; } printf("\b\bdone)\n"); } void readInt32(int *inBuf, int words, int address, CSerial2 *s) { unsigned char command[9]; char c = 'R'; int nBytes = words*4; makeCommand(c,address,nBytes,command); printf("Sending command 'read'.\n"); s->SendData(command, 9); printf("Receiving %d bytes from address 0x%X...\n\n", nBytes, address); s->ReadDataBlocking((unsigned char *)inBuf,nBytes, 0); } void writeRegister(int value, int address, CSerial2 *s) { unsigned char inBuf[4]; inBuf[0] = value >> 24 & 0xFF; // MSByte inBuf[1] = value >> 16 & 0xFF; inBuf[2] = value >> 8 & 0xFF; inBuf[3] = value & 0xFF; // LSByte unsigned char command[9]; char c = 'A'; int a = 0x0a000008; // write the parameter register in host_interface int d = 4; makeCommand(c,a,d,command); printf("Sending command 'write' to a 32-bit register.\n"); s->SendData(command, 9); // printf("Sending the value = 0x%X%X%X%X\n", inBuf[0],inBuf[1],inBuf[2],inBuf[3]); printf("Sending value = %d.\n", value); s->SendData(inBuf, 4); } int readRegister(int address, CSerial2 *s) { unsigned char inBuf[4]; unsigned char command[9]; makeCommand('Z',address,4,command); printf("Sending command 'read register'.\n"); s->SendData(command, 9); printf("Receiving the register value...\n"); s->ReadDataBlocking(inBuf, 4, 0); int b1 = toInt( inBuf ); return( b1 ); }