home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / BEAGLE / RND_INST.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  8KB  |  282 lines

  1.  
  2. /*-----------------------------------------------------------------------*/
  3. /* 15-6-92 a new tool to randomize imap files for tierra                 */
  4. /* Tierra Simulator V3.13: Copyright (c) 1992 Dan Pirone & Virtual Life  */
  5. /*-----------------------------------------------------------------------*/
  6.  
  7. /* #define _DBG_    */
  8.  
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include "license.h"
  12. #include "tierra.h"
  13. #include "declare.h"
  14. #ifdef __TURBOC__
  15. #define random rand
  16. #define srandom srand
  17. #endif /* __TURBOC__ */
  18. /*-----------------------------------------------------------------------*/
  19. typedef struct 
  20.    {
  21.    I32s op;
  22.    I8s  mnem[84];
  23.    } I_set;
  24.  
  25. I_set *InstSet_In, *InstSet_Out; 
  26.  
  27.      /* # of instructions, dyanmic on size of Infile */
  28. I32s t_i_num , PLACE_1 = -1L, InstNum = INSTNUM;  
  29. FILE *infile = stdin, *outfile = stdout; 
  30. I8s data[120];
  31.  
  32. I8s *usage = "syntax - mix-o-inst infile outfile [op1 op2 hamm_dist]\n\
  33.         infile, outfile can be '-' for stdin, stdout\n\
  34.         op1, op2 are mnemonics, \n\
  35.         & hamm_dist is the desired Hamming distance between them\n";
  36. /*-----------------------------------------------------------------------*/
  37. int FEGetch()
  38. {
  39. #ifdef __TURBOC__
  40.     return printf("\n"),getch();
  41. #else
  42.     char data[84],c;
  43.     fgets(data,84,stdin);
  44.     sscanf(data,"%c",&c);
  45.     return c;
  46. #endif
  47. }
  48.  
  49. /*-----------------------------------------------------------------------*/
  50. void FlipBit(seed, bit )
  51.     I32s *seed, bit;
  52. {
  53.     if ((*seed) & (ONE << bit))        /* if bit in seed in on */
  54.         (*seed) &= (~(ONE << bit));
  55.     else
  56.         (*seed) |= (ONE << bit);
  57. }
  58. /*-----------------------------------------------------------------------*/
  59. void init_uparams(size)
  60. I32s size;
  61. {
  62. I32s i;
  63.  
  64. InstSet_In  = (I_set*) calloc(size, sizeof(I_set));
  65. InstSet_Out = (I_set*) calloc(size, sizeof(I_set));
  66.  
  67. if(!InstSet_In || !InstSet_Out)
  68.    {
  69.    fprintf(stderr,"Problem getting memory for %ld elements!\n",size);
  70.    exit(-666);
  71.    }
  72. for(i =0;i < size; i++)
  73.      {
  74.      InstSet_Out[i].op = -1L;
  75.      InstSet_Out[i].mnem[0] = '\0';
  76.      }
  77.  
  78. }
  79. /*-----------------------------------------------------------------------*/
  80. I32s place_rest_of_inst(i_left)
  81. I32s i_left;
  82. {
  83. I32s t_op_dist,
  84.      op_p,
  85.      t_p,in_p
  86.      ;
  87.  
  88. while(i_left > 0)
  89.    {
  90.    t_p= t_op_dist =  random() % i_left+1;
  91. #ifdef _DBG_
  92.      for(in_p=0;in_p < InstNum; in_p++)
  93.       fprintf(outfile,"%ld %ld %ld %ld %s\n",in_p,i_left,
  94.               t_op_dist,InstSet_Out[in_p].op, InstSet_Out[in_p].mnem);
  95. #endif
  96.    if ( PLACE_1 < 0L)
  97.       {
  98.       for(in_p=0;in_p < InstNum  ; in_p++)
  99.          if (InstSet_In[in_p].op > -1L) break;
  100.       }
  101.     else in_p = PLACE_1;
  102.    if(in_p == InstNum)
  103.       {
  104.       fprintf(stderr,"Problem, all input placed ??? \n");
  105.       exit(-666);
  106.       }
  107.    t_p= t_op_dist;
  108.    for(op_p = 0; op_p < InstNum;op_p++ )
  109.       {
  110.       if ((InstSet_Out[op_p].op < 0L) && (--t_p < 1L)) 
  111.            {
  112.            InstSet_Out[op_p] = InstSet_In[in_p]; 
  113.            InstSet_Out[op_p].op = op_p;
  114.            InstSet_In[in_p].op = -1L;
  115.            i_left--;
  116.            if(PLACE_1 > -1L) return op_p;
  117.            break;
  118.            }
  119.       }
  120.  
  121.    }
  122. return op_p;        /* for hamming dist stuff in place first */
  123. }
  124.  
  125. /*-----------------------------------------------------------------------*/
  126. I32s read_in_inst(in_name)
  127. I8s *in_name;
  128. {
  129.     I32s inum, op;
  130.     int  rest;
  131.  
  132.     if ((!in_name) || ((in_name[0] != '-') && 
  133.         ((infile = fopen(in_name,"r")) == NULL)))
  134.     {   fprintf(stderr,"Problem opening infile %s",in_name);
  135.         exit(-666);
  136.     }
  137.  
  138.     inum = 0L;
  139.     fgets(data, 84, infile);
  140.     while (strlen(data) > 3)
  141.     {   if (fgets(data, 84, infile) == NULL)
  142.             break;
  143.         inum++;
  144.     }
  145.     rewind(infile);
  146.     init_uparams(inum);        /* allocate space for maps */
  147.  
  148.     fgets(data, 84, infile);
  149.     while (strlen(data) > 3)
  150.     {   rest = op = -1;
  151.         data[119] = '\0';        /* just in case */
  152.         if ((( sscanf(data,"%*[^x]x%lx%*[^\"]%n",&op,&rest)) < 1)||((op <0)
  153.             ||(op >INSTNUM ) || (rest < 1L))) 
  154.         {   fprintf(stderr,"bad IMapFile line: %s", data);
  155.             exit(-667);
  156.         }
  157.         InstSet_In[op].op = op;
  158.         strcpy(InstSet_In[op].mnem,&data[rest]);
  159.         if (fgets(data, 84, infile) == NULL)
  160.             break;
  161.     }
  162.     fclose(infile);
  163.     return inum;
  164. }
  165. /*-----------------------------------------------------------------------*/
  166. void place_first_inst(i_num,argc,argv)
  167. I32s *i_num;
  168. int argc;
  169. char *argv[];
  170. {
  171.     I32s op_p, i, l, op1, op2, h_dist, u_b_left,  bits[255];
  172.     I32s t_bit_dist, bit_p, bit_dist;
  173.  
  174.     if ((*argv[2] != '-') && ((outfile = fopen(argv[2],"w")) == NULL))
  175.     {    fprintf(stderr,"Problem opening outfile %s",argv[2]);
  176.          exit(-666);
  177.     }
  178.     if(!argc)
  179.     {   op_p = random() % (*i_num)--;
  180.         InstSet_Out[op_p]= InstSet_In[op_p];
  181.         InstSet_In[op_p].op = -1L;
  182.         PLACE_1 = -1L;
  183.     }
  184.     else
  185.     {   if (argc%3) 
  186.         {   fprintf(stderr,"Problem incomplete op1 op2 ham triplet!\n");
  187.             exit(-666);
  188.         }
  189.         for(i= 3; i < argc; i++)
  190.         {   
  191.             for(op1= 0; op1 < InstNum; op1++)
  192.             {   if(!strncmp(&(InstSet_In[op1].mnem[1]), argv[i],
  193.                     strlen(argv[i])))
  194.                     break;
  195.             } 
  196.             if(op1==InstNum) 
  197.             {   fprintf(stderr,"Problem op1 %8s not in %s!\n",argv[i],
  198.                     (*argv[1] == '-')?"stdin": argv[1]);
  199.                 exit(-666);
  200.             }
  201.             i++;
  202.             for(op2= 0; op2 < InstNum; op2++)
  203.             {   if(!strncmp(&(InstSet_In[op2].mnem[1]), argv[i],
  204.                     strlen(argv[i])))
  205.                     break;
  206.             } 
  207.             if(op2==InstNum) 
  208.             {   fprintf(stderr,"Problem op2 %8s not in %s!\n",argv[i],
  209.                     (*argv[1] == '-')?"stdin": argv[1]);
  210.                 exit(-666);
  211.             }
  212.             i++;
  213.             if((sscanf(argv[i],"%ld", &h_dist) < 1)
  214.                 || (h_dist < 1 || h_dist >= InstNum))
  215.             {   fprintf(stderr,
  216.                     "Problem bad magic value on hamming dist %8s !\n",
  217.                     argv[i]);
  218.                 exit(-666);
  219.             }
  220.             for (l=0; l < InstNum; bits[l++] = -1L) ;   /*init bits */
  221.             u_b_left = h_dist;
  222.             /* place first one in uniqe location of Out */
  223.             /* InstSet_Out[op1] = InstSet_In[op1]; */
  224.             /* copy Out[op1].op to In[op2].op */
  225.             PLACE_1 = InstSet_In[op1].op;
  226.             InstSet_In[op2].op = place_rest_of_inst((*i_num));
  227.             /* place second one in uniqe location of Out, accordin to mutated
  228.                op op2, if not unique bitch and quit ! */
  229.  
  230.             while(u_b_left > 0)
  231.             {   t_bit_dist =  random() % INSTBITNUM;
  232.                 for(bit_p = 0; bit_p < INSTBITNUM; bit_p++)
  233.                 {   if ((bits[bit_p] < 0L) && (--t_bit_dist < 0L))
  234.                     {   FlipBit(&(InstSet_In[op2].op) , bit_p);
  235.                         bits[bit_p] = 1L;
  236.                         u_b_left --;
  237.                         break;
  238.                     }
  239.                 }
  240.             }
  241.             if (InstSet_Out[InstSet_In[op2].op].op > -1L)
  242.             {   fprintf(stderr,
  243.                     "Problem Hamming conflict at %s %s %s !\nTry again...\n",
  244.                     argv[i-2],argv[i-1],argv[i]);
  245.                 exit(-666);
  246.             }
  247.             else
  248.             {   InstSet_Out[InstSet_In[op2].op] = InstSet_In[op2];
  249.                 InstSet_In[op2].op = -1L;
  250.                 (*i_num) -= 2; /* one for each part of the pair */
  251.             }
  252.         }
  253.         PLACE_1 = -1L;
  254.     }
  255. }
  256.  
  257. /*-----------------------------------------------------------------------*/
  258. int main (argc,argv)
  259. int argc;
  260. char *argv[];
  261. {
  262.     I32s i, seed;
  263.     if (argc < 3) {perror(usage);exit(-1);}
  264.  
  265.     seed = (I32s) time(NULL);
  266.     srandom(seed);
  267.     seed = random();
  268.     for (i = random() % 256; i >= 0; i--)
  269.         seed = random();
  270.     srandom(seed);
  271.     random();
  272.  
  273.     InstNum = t_i_num = read_in_inst(argv[1]);
  274.     place_first_inst(&t_i_num ,(argc>3)?argc:0,argv);
  275.     place_rest_of_inst(t_i_num);
  276.     for(i=0;i < InstNum; i++)
  277.         fprintf(outfile,
  278.             "    {0x%02lx, %s",InstSet_Out[i].op,InstSet_Out[i].mnem);
  279.     return 0;
  280. }
  281. /*-----------------------------------------------------------------------*/
  282.