Subversion Repositories pentevo

Rev

Rev 66 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef unsigned char BYTE;
  6. typedef unsigned short WORD;
  7. typedef unsigned long LONGWORD;
  8.  
  9. //-----------------------------------------------------------------------------
  10.  
  11. int main(int argc,char*argv[])
  12. {
  13.  BYTE       h[]="0123456789ABCDEF";
  14.  BYTE       b0, b1, l, az;
  15.  LONGWORD   i, z;
  16.  BYTE       s[256];
  17.  BYTE       buff[0x10000];
  18.  FILE*      f;
  19.  
  20.  printf("BinnaryFile_To_AVRAssmFile Converter\n");
  21.  if (argc==1) { printf("usage: bin2avr <FileName> [0]\n"); return 2; }
  22.  az=0;
  23.  if (argc==3)
  24.  {
  25.   strncpy(s,argv[2],1);
  26.   if (s[0]=='0') az=1;
  27.  }
  28.  strncpy(s,argv[1],255);
  29.  f=fopen(s,"rb");
  30.  if (!f) { printf("Can't open file %s!\n",s); return 1; }
  31.  
  32.  for (i=0;i<0x10000;i++) buff[i]=0x00;
  33.  z=fread(buff,1,0x10000,f);
  34.  fclose(f);
  35.  if (!z) { printf("Can't read file %s!\n",s); return 1; }
  36.  
  37.  l=strlen(s);
  38.  b0=1;
  39.  do
  40.   b0++;
  41.  while ((l>b0) && (b0<4) && (s[l-b0]!='.'));
  42.  if (s[l-b0]=='.')
  43.    strcpy(&s[l-b0],".inc");
  44.   else
  45.    strcat(s,".inc");
  46.  
  47.  f=fopen(s,"wt");
  48.  if (!f) { printf("Can't create output file!\n"); return 1; }
  49.  
  50.  z+=az;
  51.  i=0;
  52.  l=0;
  53.  do
  54.  {
  55.   if (!l) fputs("        .DW     ",f);
  56.   b0=buff[i++];
  57.   b1=buff[i++];
  58.   fputc('$',f);
  59.   fputc(h[b1>>4],f);
  60.   fputc(h[b1&0x0f],f);
  61.   fputc(h[b0>>4],f);
  62.   fputc(h[b0&0x0f],f);
  63.   if (l==7)
  64.     fputc('\n',f);
  65.    else
  66.     if (i<z) fputc(',',f);
  67.   l++; l&=0x07;
  68.  }
  69.  while (i<z);
  70.  fputc('\n',f);
  71.  fclose(f);
  72.  printf("Created file %s\n",s);
  73.  return 0;
  74. }
  75.