Пример #1
0
uint32 readnum(const char *s)
{
	uint32 n=0;
	if((*s == '0') && (*(s+1) == 'x')) return readhex(s+2);
	//while(isdigit(*s)) {
	while(*s) {
		n = n*10 + (*s - '0');
		s++;
	}
	return n;
}
Пример #2
0
/**
 * Read a hexfile
 */
char * readHexfile(const char * filename, int flashsize, unsigned long * lastaddr) {
    char * data = malloc(flashsize);
    FILE *fp;
    int len;
    unsigned char line[512];
    unsigned long addr = 0;

    if(data == NULL) {
        printf("Memory allocation error!\n");
        return NULL;
    }

    memset( data, 0xFF, flashsize );

    if(NULL == (fp = fopen(filename, "r"))) {
        printf("File \"%s\" open failed!\n\n", filename);
        free(data);
        exit(0);
    }

    printf("Reading %s... ", filename);


    // reading file to "data"
    while((len = readhex(fp, &addr, line)) >= 0) {
        if(len) {
            if( addr + len > flashsize ) {
                fclose(fp);
                free(data);
                printf("Hex-file too large for target!\n");
                return NULL;
            }
            memcpy(data+addr, line, len);
            addr += len;

            if(*lastaddr < addr-1) {
                *lastaddr = addr-1;
            }
// This doesn't appear to do anything, and probably incorrect due to
// addr += len; above, introducing an off-by-one error
            addr++;
        }
    }

    fclose(fp);

    printf("File read.\n");
    return data;
}
Пример #3
0
//-------------------------------------------------------------------
int main ( int argc, char *argv[] )
{
    FILE *fp;
    unsigned int ra;

    if(argc<2)
    {
        printf("hex file not specified\n");
        return(1);
    }
    fp=fopen(argv[1],"rt");
    if(fp==NULL)
    {
        printf("error opening file [%s]\n",argv[1]);
        return(1);
    }
    if(readhex(fp)) return(1);
    fclose(fp);

    maxadd++;
    maxadd>>=1;

    fp=fopen("blinker.h","wt");
    if(fp==NULL) return(1);
    fprintf(fp,"\n");
    fprintf(fp,"\n");
    fprintf(fp,"#define ROM_LENGTH 0x%04X\n",maxadd);
    fprintf(fp,"\nconst unsigned short rom[ROM_LENGTH]=\n{\n");
    for(ra=0;ra<maxadd;ra++)
    {
        fprintf(fp," 0x%04X, //0x%08X\n",rom[ra],ra);
    }
    fprintf(fp,"};");
    fprintf(fp,"\n");
    fprintf(fp,"\n");
    fclose(fp);



    return(0);
}
Пример #4
0
/*^
void writehex(char str[],char out[])  
{   
    int i;
    char buf[10];
    out[0]=0;
    for (i = 0; i < 8; i++)
   {
           sprintf(buf,"%02x", str[i] & 0377);
           strcat(out,buf);
   }
}
*/
int ConvierteCaracteresWeb(char szData[])
{
    int i;
    char *ini,*fin;
    char szTmp[MAX_BUFFER],szTmp1[1024];
    char szHex[5],szOut[2];
    
    ini=szData;
    //printf("INI=%s\n\r",ini);
    fin=(char *)strchr(ini,'%');
    if (fin==NULL) return 1;
    memset(szTmp,0,sizeof(szTmp));
    do
    {
	//Copiamos el buffer antes...
        memcpy(szTmp1,ini,fin-ini); szTmp1[fin-ini]=0;
	strcat(szTmp,szTmp1);
	//printf("szTmp=%s\n\r",szTmp);
	//OBtenemos el hex...
	memcpy(szHex,fin+1,2); szHex[2]=0;
	readhex(szHex,szOut);
	strncat(szTmp,szOut,1);
    	//printf("szHex=%s %c\n\r",szHex,szOut[0]);
	//printf("szTmp=%s\n\r",szTmp);
	ini=fin+3;
    	fin=(char *)strchr(ini,'%');
    	if (fin==NULL) 
	{
		memcpy(szTmp1,ini,strlen(ini)); szTmp1[strlen(ini)]=0;
		strcat(szTmp,szTmp1);
		//printf("szTmp=%s\n\r",szTmp);
		sprintf(szData,"%s",szTmp);
		//printf("szData=%s\n\r",szData);
		return 1;
	}
    } while(1);

}
/* Parse /proc/self/maps.  For each map entry, call
   record_mapping, passing it, in this order:

      start address in memory
      length
      page protections (using the VKI_PROT_* flags)
      mapped file device and inode
      offset in file, or zero if no file
      filename, zero terminated, or NULL if no file

   So the sig of the called fn might be

      void (*record_mapping)( Addr start, SizeT size, UInt prot,
			      UInt dev, UInt info,
                              ULong foffset, UChar* filename )

   Note that the supplied filename is transiently stored; record_mapping 
   should make a copy if it wants to keep it.

   Nb: it is important that this function does not alter the contents of
       procmap_buf!
*/
void VG_(parse_procselfmaps) (
   void (*record_mapping)( Addr addr, SizeT len, UInt prot,
			   UInt dev, UInt ino, ULong foff, const UChar* filename )
   )
{
   Int    i, j, i_eol;
   Addr   start, endPlusOne;
   UChar* filename;
   UChar  rr, ww, xx, pp, ch, tmp;
   UInt	  ino, prot;
   UWord  foffset, maj, min;

   read_procselfmaps();

   tl_assert( '\0' != procmap_buf[0] && 0 != buf_n_tot);

   if (0)
      VG_(message)(Vg_DebugMsg, "raw:\n%s", procmap_buf );

   /* Ok, it's safely aboard.  Parse the entries. */
   i = 0;
   while (True) {
      if (i >= buf_n_tot) break;

      /* Read (without fscanf :) the pattern %16x-%16x %c%c%c%c %16x %2x:%2x %d */
      j = readhex(&procmap_buf[i], &start);
      if (j > 0) i += j; else goto syntaxerror;
      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == '-') i += j; else goto syntaxerror;
      j = readhex(&procmap_buf[i], &endPlusOne);
      if (j > 0) i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == ' ') i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &rr);
      if (j == 1 && (rr == 'r' || rr == '-')) i += j; else goto syntaxerror;
      j = readchar(&procmap_buf[i], &ww);
      if (j == 1 && (ww == 'w' || ww == '-')) i += j; else goto syntaxerror;
      j = readchar(&procmap_buf[i], &xx);
      if (j == 1 && (xx == 'x' || xx == '-')) i += j; else goto syntaxerror;
      /* This field is the shared/private flag */
      j = readchar(&procmap_buf[i], &pp);
      if (j == 1 && (pp == 'p' || pp == '-' || pp == 's')) 
                                              i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == ' ') i += j; else goto syntaxerror;

      j = readhex(&procmap_buf[i], &foffset);
      if (j > 0) i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == ' ') i += j; else goto syntaxerror;

      j = readhex(&procmap_buf[i], &maj);
      if (j > 0) i += j; else goto syntaxerror;
      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == ':') i += j; else goto syntaxerror;
      j = readhex(&procmap_buf[i], &min);
      if (j > 0) i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == ' ') i += j; else goto syntaxerror;

      j = readdec(&procmap_buf[i], &ino);
      if (j > 0) i += j; else goto syntaxerror;
 
      goto read_line_ok;

    syntaxerror:
      VG_(message)(Vg_UserMsg, "FATAL: syntax error reading /proc/self/maps");
      { Int k;
        VG_(printf)("last 50 chars: '");
        for (k = i-50; k <= i; k++) VG_(printf)("%c", procmap_buf[k]);
        VG_(printf)("'\n");
      }
      VG_(exit)(1);

    read_line_ok:

      /* Try and find the name of the file mapped to this segment, if
         it exists. */
      while (procmap_buf[i] != '\n' && i < buf_n_tot-1) i++;
      i_eol = i;
      i--;
      while (!VG_(isspace)(procmap_buf[i]) && i >= 0) i--;
      i++;
      if (i < i_eol-1 && procmap_buf[i] == '/') {
         /* Minor hack: put a '\0' at the filename end for the call to
            'record_mapping', then restore the old char with 'tmp'. */
         filename = &procmap_buf[i];
         tmp = filename[i_eol - i];
         filename[i_eol - i] = '\0';
      } else {
	 tmp = 0;
         filename = NULL;
         foffset = 0;
      }

      prot = 0;
      if (rr == 'r') prot |= VKI_PROT_READ;
      if (ww == 'w') prot |= VKI_PROT_WRITE;
      if (xx == 'x') prot |= VKI_PROT_EXEC;

      //if (start < VG_(valgrind_last))
      (*record_mapping) ( start, endPlusOne-start, 
                          prot, maj * 256 + min, ino,
                          foffset, filename );

      if ('\0' != tmp) {
         filename[i_eol - i] = tmp;
      }

      i = i_eol + 1;
   }
}
Пример #6
0
void VG_(read_procselfmaps) (
   void (*record_mapping)( Addr, UInt, Char, Char, Char, UInt, UChar* )
)
{
   Int    i, j, n_tot, n_chunk, fd, i_eol;
   Addr   start, endPlusOne;
   UChar* filename;
   UInt   foffset;
   UChar  rr, ww, xx, pp, ch;

   /* Read the initial memory mapping from the /proc filesystem. */
   fd = VG_(open_read) ( "/proc/self/maps" );
   if (fd == -1) {
      VG_(message)(Vg_UserMsg, "FATAL: can't open /proc/self/maps");
      VG_(exit)(1);
   }
   n_tot = 0;
   do {
      n_chunk = VG_(read) ( fd, &procmap_buf[n_tot], M_PROCMAP_BUF - n_tot );
      n_tot += n_chunk;
   } while ( n_chunk > 0 && n_tot < M_PROCMAP_BUF );
   VG_(close)(fd);
   if (n_tot >= M_PROCMAP_BUF-5) {
      VG_(message)(Vg_UserMsg, "FATAL: M_PROCMAP_BUF is too small; "
                               "increase it and recompile");
       VG_(exit)(1);
   }
   if (n_tot == 0) {
      VG_(message)(Vg_UserMsg, "FATAL: I/O error on /proc/self/maps" );
       VG_(exit)(1);
   }
   procmap_buf[n_tot] = 0;
   if (0)
      VG_(message)(Vg_DebugMsg, "raw:\n%s", procmap_buf );

   /* Ok, it's safely aboard.  Parse the entries. */

   i = 0;
   while (True) {
      if (i >= n_tot) break;

      /* Read (without fscanf :) the pattern %8x-%8x %c%c%c%c %8x */
      j = readhex(&procmap_buf[i], &start);
      if (j > 0) i += j; else goto syntaxerror;
      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == '-') i += j; else goto syntaxerror;
      j = readhex(&procmap_buf[i], &endPlusOne);
      if (j > 0) i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == ' ') i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &rr);
      if (j == 1 && (rr == 'r' || rr == '-')) i += j; else goto syntaxerror;
      j = readchar(&procmap_buf[i], &ww);
      if (j == 1 && (ww == 'w' || ww == '-')) i += j; else goto syntaxerror;
      j = readchar(&procmap_buf[i], &xx);
      if (j == 1 && (xx == 'x' || xx == '-')) i += j; else goto syntaxerror;
      /* I haven't a clue what this last field means. */
      j = readchar(&procmap_buf[i], &pp);
      if (j == 1 && (pp == 'p' || pp == '-' || pp == 's')) 
                                              i += j; else goto syntaxerror;

      j = readchar(&procmap_buf[i], &ch);
      if (j == 1 && ch == ' ') i += j; else goto syntaxerror;

      j = readhex(&procmap_buf[i], &foffset);
      if (j > 0) i += j; else goto syntaxerror;
      
      goto read_line_ok;

    syntaxerror:
      VG_(message)(Vg_UserMsg, "FATAL: syntax error reading /proc/self/maps");
      { Int k;
        VG_(printf)("last 50 chars: `");
        for (k = i-50; k <= i; k++) VG_(printf)("%c", procmap_buf[k]);
        VG_(printf)("'\n");
      }
       VG_(exit)(1);

    read_line_ok:
      /* Try and find the name of the file mapped to this segment, if
         it exists. */
      while (procmap_buf[i] != '\n' && i < M_PROCMAP_BUF-1) i++;
      i_eol = i;
      i--;
      while (!VG_(isspace)(procmap_buf[i]) && i >= 0) i--;
      i++;
      if (i < i_eol-1 && procmap_buf[i] == '/') {
         filename = &procmap_buf[i];
         filename[i_eol - i] = '\0';
      } else {
         filename = NULL;
         foffset = 0;
      }

      (*record_mapping) ( start, endPlusOne-start, 
                          rr, ww, xx, 
                          foffset, filename );

      i = i_eol + 1;
   }
}
Пример #7
0
void
mem_handler(REQUEST* request, RESPONSE* response)
{
  char arg[16];
  uint32_t addr, val;
  int success = 1;
  int ret;

  ret = rest_get_query_variable(request, "addr", arg, 16);
  if (ret) {
    addr = readhex(arg);
    ret = rest_get_query_variable(request, "sz", arg, 16);
    if (ret) {
      if (arg[0] == '1') {
        ret = rest_get_query_variable(request, "val", arg, 16);
        if (ret) {
          val = readhex(arg);
          *(uint8_t*)addr = (uint8_t)val;
          sprintf(temp, "OK\n");
        } else {
          sprintf(temp, "0x%02x\n", *(uint8_t*)addr);
        }
      } else if (arg[0] == '2') {
        ret = rest_get_query_variable(request, "val", arg, 16);
        if (ret) {
          val = readhex(arg);
          *(uint16_t*)addr = (uint16_t)val;
          sprintf(temp, "OK\n");
        } else {
          sprintf(temp, "0x%0hx\n", *(uint16_t*)addr);
        }
      } else {
        ret = rest_get_query_variable(request, "val", arg, 16);
        if (ret) {
          val = readhex(arg);
          *(uint32_t*)addr = (uint32_t)val;
          sprintf(temp, "OK\n");
        } else {
          sprintf(temp, "0x%0x\n", *(uint32_t*)addr);
        }
      }
    } else {
      ret = rest_get_query_variable(request, "val", arg, 16);
      if (ret) {
        val = readhex(arg);
        *(uint32_t*)addr = (uint32_t)val;
        sprintf(temp, "OK\n");
      } else {
        sprintf(temp, "0x%0x\n", *(uint32_t*)addr);
      }
    }
  } else {
    success = 0;
  }

  if (success) {
    rest_set_header_content_type(response, TEXT_PLAIN);
    rest_set_response_payload(response, (uint8_t*)temp, strlen(temp));
  } else {
    rest_set_response_status(response, BAD_REQUEST_400);
  }
}
Пример #8
0
//-----------------------------------------------------------------------------
int main ( int argc, char *argv[] )
{
    unsigned int ra;
    unsigned int addr;
    unsigned int inst;
    unsigned int rd,rs1,rs2,simm,dest;
    FILE *fp;

    if(argc<2)
    {
        fprintf(stderr,".hex file not specified\n");
        return(1);
    }

    memset(mem,0xFF,sizeof(mem));
    memset(mem_hit,0x00,sizeof(mem_hit));

    fp=fopen(argv[1],"rt");
    if(fp==NULL)
    {
        fprintf(stderr,"Error opening file [%s]\n",argv[1]);
        return(1);
    }
    if(readhex(fp)) return(1);
    fclose(fp);

    for(addr=0;addr<=RAMMASK;addr++)
    {
        if(mem_hit[addr])
        {
            inst=mem[addr];
            printf("0x%04X: 0x%04X ",addr,inst);
            switch(inst&0xF000)
            {
                case 0x0000:
                {
                    printf("br");
                    if(inst&0x0800) printf("n");
                    if(inst&0x0400) printf("z");
                    if(inst&0x0200) printf("p");
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf(" 0x%04X",dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x1000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    if(inst&0x20)
                    {
                        simm=sext5(inst);
                        printf("add r%u,r%u,#0x%04X",rd,rs1,simm);
                    }
                    else
                    {
                        rs2=(inst>>0)&7;
                        printf("add r%u,r%u,r%u",rd,rs1,rs2);
                    }
                    break;
                }
                case 0x2000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("ld r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x3000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("st r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x4000:
                {
                    if(inst&0x0800)
                    {
                        simm=sext11(inst);
                        dest=(addr+1+simm)&0xFFFF;
                        printf("jsr 0x%04X",dest);
                        if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                        else            printf(" ; +%u",simm);
                    }
                    else
                    {
                        rd=(inst>>6)&7;
                        printf("jsrr r%u",rd);
                    }
                    break;
                }
                case 0x5000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    if(inst&0x20)
                    {
                        simm=sext5(inst);
                        printf("and r%u,r%u,#0x%04X",rd,rs1,simm);
                    }
                    else
                    {
                        rs2=(inst>>0)&7;
                        printf("and r%u,r%u,r%u",rd,rs1,rs2);
                    }
                    break;
                }
                case 0x6000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    simm=sext6(inst);
                    printf("ldr r%u,r%u,#0x%04X",rd,rs1,simm);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x7000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    simm=sext6(inst);
                    printf("str r%u,r%u,#0x%04X",rd,rs1,simm);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x8000:
                {
                    printf("rti");
                    break;
                }
                case 0x9000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    printf("not r%u,r%u",rd,rs1);
                    break;
                }
                case 0xA000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("ldi r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xB000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("sti r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xC000:
                {
                    printf("ret");
                    break;
                }
                case 0xD000:
                {
                    printf("undefined");
                    break;
                }
                case 0xE000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("lea r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xF000:
                {
                    rd=inst&0xFF;
                    printf("trap 0x%02X",rd);
                    break;
                }
            }


            printf("\n");
        }
    }



    return(0);
}
Пример #9
0
int main(int argc, char *argv[])
{
    unsigned int lasttick;
    unsigned int tick;
    unsigned int addr;
    unsigned int mask;
    unsigned int simhalt;
    unsigned int did_reset;

    FILE *fp;

//    Verilated::commandArgs(argc, argv);

    if(argc<2)
    {
        fprintf(stderr,".hex file not specified\n");
        return(1);
    }
    fp=fopen(argv[1],"rt");
    if(fp==NULL)
    {
        fprintf(stderr,"Error opening file [%s]\n",argv[1]);
        return(1);
    }
    if(readhex(fp)) return(1);
    fclose(fp);


#if VM_TRACE
    Verilated::traceEverOn(true);
#endif

    top = new Va23_core;

#if VM_TRACE
    trace = new VerilatedVcdC;
    top->trace(trace, 99);
    trace->open("test.vcd");
#endif

    top->i_system_rdy = 0;
    simhalt=0;
    did_reset=0;
    tick=0;
    lasttick=tick;
    while (!Verilated::gotFinish())
    {

        top->i_wb_dat=0;
        top->i_wb_ack=0;

        tick++;
        if(tick<lasttick) printf("tick rollover\n");
        lasttick=tick;

        if(did_reset)
        {
            if(top->o_wb_cyc)
            {
                if(top->o_wb_sel)
                {
                    addr=top->o_wb_adr;
                    if(addr<=ROMMASK)
                    {
                        if(top->o_wb_we)
                        {
                            printf("Error trying to write to rom 0x%08X\n",addr);
                        }
                        else
                        {
                            top->i_wb_dat=rom[addr>>2];
                        }
                    }
                    else if((addr>=RAMBASE)&&(addr<=(RAMBASE+RAMMASK)))
                    {
                        addr&=RAMMASK;
                        if(top->o_wb_we)
                        {
                            //write ram
                            if((tick&1)==0)
                            {
                                if(top->o_wb_sel==0x0F)
                                {
                                    //all lanes on, just write
                                    ram[addr>>2]=top->o_wb_dat;
                                    //printf("write ram[0x%X]=0x%08X\n",addr,ram[addr>>2]);
                                }
                                else
                                {
                                    //read-modify-write
                                    mask=0;
                                    if(top->o_wb_sel&1) mask|=0x000000FF;
                                    if(top->o_wb_sel&2) mask|=0x0000FF00;
                                    if(top->o_wb_sel&4) mask|=0x00FF0000;
                                    if(top->o_wb_sel&8) mask|=0xFF000000;
                                    ram[addr>>2]&=~mask;
                                    ram[addr>>2]|=top->o_wb_dat&mask;
                                    //printf("write ram[0x%X]=0x%08X\n",addr,ram[addr>>2]);
                                }
                            }
                        }
                        else
                        {
Пример #10
0
//-----------------------------------------------------------------------------
int main ( int argc, char *argv[] )
{
  //struct termios newsettings;

    if(argc<2)
    {
        printf(".hex file not specified\n");
        return(1);
    }
    fp=fopen(argv[1],"rt");
    if(fp==NULL)
    {
        printf("error opening file %s\n",argv[1]);
        return(1);
    }
    if(readhex()) return(1);
    fclose(fp);

    pages=maxadd>>8;

        printf("pages %u maxadd %u\n",pages,maxadd);



    if(ser_open())
    {
        printf("ser_open() failed\n");
        return(1);
    }
    printf("port opened\n");


    if(get_sync())
    {
        ser_close();
        printf("\n\n");
        return(1);
    }

        sdata[0]=0x41;
        sdata[1]=0x81;
        sdata[2]=0x20;
        ser_senddata(sdata,3);
        while(1)
        {
            rb=ser_copystring(data);
            if(rb==3) break;
        }
        ser_dump(rb);
        showstring(rb);

        sdata[0]=0x41;
        sdata[1]=0x82;
        sdata[2]=0x20;
        ser_senddata(sdata,3);
        while(1)
        {
            rb=ser_copystring(data);
            if(rb==3) break;
        }
        ser_dump(rb);
        showstring(rb);


        sdata[0]=0x50;
        sdata[1]=0x20;
        ser_senddata(sdata,2);
        while(1)
        {
            rb=ser_copystring(data);
            if(rb==2) break;
        }
        ser_dump(rb);
        showstring(rb);

        if((data[0]==0x14)&&(data[1]==0x10))
        {
        }
        else
        {
            ser_close();
            printf("\n\n");
            return(1);
        }


        for(page=0;page<=maxadd;page+=256)
        {
            printf("set address 0x%04X\n",page);
            sdata[0]=0x55;
            sdata[1]=(page>>1)&0xFF;
            sdata[2]=(page>>9)&0xFF;
            sdata[3]=0x20;
            ser_senddata(sdata,4);
            while(1)
            {
                rb=ser_copystring(data);
                if(rb==2) break;
            }
            ser_dump(rb);
            showstring(rb);

            printf("write page\n");
            sdata[0]=0x64;
            sdata[1]=0x01;
            sdata[2]=0x00;
            sdata[3]='F';
            for(ra=0;ra<256;ra++) sdata[4+ra]=memory[ra+page];
            sdata[4+ra]=0x20;
            ser_senddata(sdata,ra+5);
            while(1)
            {
                rb=ser_copystring(data);
                if(rb==2) break;
            }
            ser_dump(rb);
            showstring(rb);

        }


        sdata[0]=0x51;
        sdata[1]=0x20;
        ser_senddata(sdata,2);
        while(1)
        {
            rb=ser_copystring(data);
            if(rb==2) break;
        }
        ser_dump(rb);
        showstring(rb);

        if((data[0]==0x14)&&(data[1]==0x10))
        {
        }
        else
        {
            ser_close();
            printf("\n\n");
            return(1);
        }



  ser_close();
  printf("\n\n");
  return(0);
}
Пример #11
0
int main(int argc,char*argv[])
{
 BYTE       b, o;
 WORD       i, ih, crc;
 struct tm  stm;
 BYTE       vs[57]; //58-1
 WORD       tabcrc[256];
 BYTE       fbuff[0x1e000];
 BYTE       ebuff[4096];
 BYTE       header[0x80];
 LONGWORD   adr;
 FILE*      f;

 printf("ZX EVO project:  HEX to BIN + CRC + Header\n");
 if (argc<5) { printf("usage: make_fw <HexFileName> <EepFileName> <OutputFileName> <VersionFileName>\n"); return 2; }

 header[0]='Z';
 header[1]='X';
 header[2]='E';
 header[3]='V';
 header[4]='O';
 header[5]=0x1a;
 for (ih=0x06; ih<0x80; ih++) header[ih]=0;
 ih=6;
 o=0;
 if (argc==6)
  {
   strncpy(s1,argv[5],1);
   if (s1[0]=='o') o=0x80;
  }

 strncpy(s1,argv[4],255);
 f=fopen(s1,"rt");
 vs[0]=0;
 if (f)
  {
   fgets(vs,56,f);
   fclose(f);
  }
 for (i=0; i<57; i++)
  if ( (vs[i]<0x20) || (vs[i]>=0x80) )
   {
    vs[i]=0;
    break;
   }
 i=strlen(vs);
 if (!i) strcpy(vs, "No info");

 strcpy(&header[ih], vs);
 ih=strlen(header);

 strncpy(s1,argv[1],255);
 printf("Open file %s... ",s1);
 f=fopen(s1,"rt");
 if (!f) { printf("Can't open file\n"); return 1; }
 printf("Read... ");
 readhex(f,fbuff,0x1e000);
 fclose(f);
 printf("Close.\n");
 if (err) { printf("Total %u error(s)!\n",err); return 3; }

 strncpy(s1,argv[2],255);
 printf("Open file %s... ",s1);
 f=fopen(s1,"rt");
 if (!f)
   printf("Can't open file\n");
  else
  {
   printf("Read... ");
   readhex(f,ebuff,4096);
   fclose(f);
   printf("Close.\n");
   if (err) { printf("Total %u error(s)!\n",err); return 3; }
  }

 // comments place
 {
  time_t tt;
  tt=time(NULL);
  memcpy(&stm,localtime(&tt),sizeof(stm));
 }
 i=(WORD)(((stm.tm_year-100)&0x3f)<<9) | (((stm.tm_mon+1)&0x0f)<<5) | (stm.tm_mday&0x1f);
 header[0x003f]=fbuff[0x1dffd]=(i>>8)&0x7f|o;
 header[0x003e]=fbuff[0x1dffc]=i&0xff;

 strncpy(&fbuff[0x1dff0], vs, 12);

 for (i=0;i<256;i++)
 {
  crc=i<<8;
  b=8;
  do
  {
   if (crc&0x8000)
    crc=(crc<<1)^0x1021;
   else
    crc<<=1;
   b--;
  }
  while ((b)&&(crc));
  tabcrc[i]=crc;
 }

 crc=0xffff;
 for (adr=0;adr<0x1dffe;adr++) crc=tabcrc[(crc>>8)^fbuff[adr]]^(crc<<8);
 fbuff[0x1dffe]=crc>>8;
 fbuff[0x1dfff]=crc&0xff;

 makebitmap(fbuff,&header[0x40],0x1e000);
 makebitmap(ebuff,&header[0x7c],4096);

 crc=0x0000;
 for (i=0;i<0x7e;i++) crc=tabcrc[(crc>>8)^header[i]]^(crc<<8);
 header[0x7e]=crc>>8;
 header[0x7f]=crc&0xff;

 strncpy(target,argv[3],255);
 f=fopen(target,"wb");
 if (!f) { printf("Can't create output file!\n"); return 1; }
 fwrite(header,1,0x80,f);
 adr=0;
 do
 {
  b=0xff;
  for (i=0;i<256;i++) b&=fbuff[adr++];
  if (b!=0xff) fwrite(&fbuff[adr-256],256,1,f);
 }
 while (adr<0x1e000);
 adr=0;
 do
 {
  b=0xff;
  for (i=0;i<256;i++) b&=ebuff[adr++];
  if (b!=0xff) fwrite(&ebuff[adr-256],256,1,f);
 }
 while (adr<4096);
 fclose(f);
 printf("Created file %s\n",target);
 return 0;
}