home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / emulate / x_comp / 68hc11 / util.c < prev   
C/C++ Source or Header  |  1986-11-01  |  5KB  |  285 lines

  1. /*
  2.  *      fatal --- fatal error handler
  3.  */
  4. fatal(str)
  5. char    *str;
  6. {
  7.  printf("%s\n",str);
  8.  exit(-1);
  9. }
  10.  
  11. /*
  12.  *      error --- error in a line
  13.  *                      print line number and error
  14.  */
  15. error(str)
  16. char    *str;
  17. {
  18.  if(N_files > 1)
  19.   printf("%s,",Gargv[Cfn]);
  20.  printf("%d: ",Line_num);
  21.  printf("%s\n",str);
  22.  Err_count++;
  23. }
  24. /*
  25.  *      warn --- trivial error in a line
  26.  *                      print line number and error
  27.  */
  28. warn(str)
  29. char    *str;
  30. {
  31.  if(N_files > 1)
  32.   printf("%s,",Gargv[Cfn]);
  33.  printf("%d: ",Line_num);
  34.  printf("Warning --- %s\n",str);
  35. }
  36.  
  37.  
  38. /*
  39.  *      delim --- check if character is a delimiter
  40.  */
  41. delim(c)
  42. char    c;
  43. {
  44.  if( any(c," \t\n"))
  45.   return(YES);
  46.  return(NO);
  47. }
  48.  
  49. /*
  50.  *      skip_white --- move pointer to next non-whitespace char
  51.  */
  52. char *skip_white(ptr)
  53. char    *ptr;
  54. {
  55.  while(*ptr==BLANK || *ptr==TAB)
  56.   ptr++;
  57.  return(ptr);
  58. }
  59.  
  60. /*
  61.  *      eword --- emit a word to code file
  62.  */
  63. eword(wd)
  64. int     wd;
  65. {
  66.  emit(hibyte(wd));
  67.  emit(lobyte(wd));
  68. }
  69.  
  70. /*
  71.  *      emit --- emit a byte to code file
  72.  */
  73. emit(byte)
  74. {
  75. #ifdef DEBUG
  76.  printf("%2x @ %4x\n",byte,Pc);
  77. #endif
  78.  if(Pass==1){
  79.   Pc++;
  80.   return(YES);
  81.   }
  82.  if(P_total < P_LIMIT)
  83.   P_bytes[P_total++] = byte;
  84.  E_bytes[E_total++] = byte;
  85.  Pc++;
  86.  if(E_total == E_LIMIT)
  87.   f_record();
  88. }
  89.  
  90. /*
  91.  *      f_record --- flush record out in `S1' format
  92.  */
  93. f_record()
  94. {
  95.  int     i;
  96.  int     chksum;
  97.  
  98.  if(Pass == 1)
  99.   return;
  100.  if(E_total==0){
  101.   E_pc = Pc;
  102.   return;
  103.   }
  104.  chksum =  E_total+3;    /* total bytes in this record */
  105.  chksum += lobyte(E_pc);
  106.  chksum += E_pc>>8;
  107.  fprintf(Objfil,"S1");   /* record header preamble */
  108.  hexout(E_total+3);      /* byte count +3 */
  109.  hexout(E_pc>>8);        /* high byte of PC */
  110.  hexout(lobyte(E_pc)); /* low byte of PC */
  111.  for(i=0;i<E_total;i++){
  112.   chksum += lobyte(E_bytes[i]);
  113.   hexout(lobyte(E_bytes[i])); /* data byte */
  114.   }
  115.  chksum =~ chksum;       /* one's complement */
  116.  hexout(lobyte(chksum)); /* checksum */
  117.  fprintf(Objfil,"\n");
  118.  E_pc = Pc;
  119.  E_total = 0;
  120. }
  121.  
  122. char    *hexstr = { "0123456789ABCDEF" } ;
  123.  
  124. hexout(byte)
  125. int     byte;
  126. {
  127.  char hi,lo;
  128.  
  129.  byte = lobyte(byte);
  130.  fprintf(Objfil,"%c%c",hexstr[byte>>4],hexstr[byte&017]);
  131. }
  132.  
  133. /*
  134.  *      print_line --- pretty print input line
  135.  */
  136. print_line()
  137. {
  138.  int     i;
  139.  register char *ptr;
  140.  
  141.  if(P_total || P_force)
  142.   printf("%04x",Old_pc);
  143.  else
  144.   printf("    ");
  145.  
  146.  for(i=0;i<P_total && i<6;i++)
  147.   printf(" %02x",lobyte(P_bytes[i]));
  148.  for(;i<6;i++)
  149.   printf("   ");
  150.  printf("  ");
  151.  
  152.  if(Cflag){
  153.   if(Cycles)
  154.    printf("[%2d -%8ld]  ",Cycles,Ctotal);
  155.   else
  156.    printf("                ");
  157.   }
  158.  ptr = Line;
  159.  while( *ptr != '\n' )   /* just echo the line back out */
  160.   putchar(*ptr++);
  161.  for(;i<P_total;i++){
  162.   if( i%6 == 0 )
  163.    printf("\n    ");
  164.   printf(" %02x",lobyte(P_bytes[i]));
  165.   }
  166.  printf("\n");
  167. }
  168.  
  169. /*
  170.  *      any --- does str contain c?
  171.  */
  172. any(c,str)
  173. char    c;
  174. char    *str;
  175. {
  176.  while(*str != EOS)
  177.   if(*str++ == c)
  178.    return(YES);
  179.  return(NO);
  180. }
  181.  
  182. /*
  183.  *      mapdn --- convert A-Z to a-z
  184.  */
  185. char mapdn(c)
  186. char c;
  187. {
  188.  if( c >= 'A' && c <= 'Z')
  189.   return((char)(c+040));
  190.  return(c);
  191. }
  192.  
  193. /*
  194.  *      lobyte --- return low byte of an int
  195.  */
  196. lobyte(i)
  197. int i;
  198. {
  199.  return(i&0xFF);
  200. }
  201. /*
  202.  *      hibyte --- return high byte of an int
  203.  */
  204. hibyte(i)
  205. int i;
  206. {
  207.  return((i>>8)&0xFF);
  208. }
  209.  
  210. /*
  211.  *      head --- is str2 the head of str1?
  212.  */
  213. head(str1,str2)
  214. char *str1,*str2;
  215. {
  216.  while( *str1 != EOS && *str2 != EOS){
  217.   if( *str1 != *str2 )break;
  218.   str1++;
  219.   str2++;
  220.   }
  221.  if(*str1 == *str2)return(YES);
  222.  if(*str2==EOS)
  223.   if( any(*str1," \t\n,+-];*") )return(YES);
  224.  return(NO);
  225. }
  226.  
  227. /*
  228.  *      alpha --- is character a legal letter
  229.  */
  230. alpha(c)
  231. char c;
  232. {
  233.  if( c<= 'z' && c>= 'a' )return(YES);
  234.  if( c<= 'Z' && c>= 'A' )return(YES);
  235.  if( c== '_' )return(YES);
  236.  if( c== '.' )return(YES);
  237.  return(NO);
  238. }
  239. /*
  240.  *      alphan --- is character a legal letter or digit
  241.  */
  242. alphan(c)
  243. char c;
  244. {
  245.  if( alpha(c) )return(YES);
  246.  if( c<= '9' && c>= '0' )return(YES);
  247.  if( c == '$' )return(YES);      /* allow imbedded $ */
  248.  return(NO);
  249. }
  250.  
  251. /*
  252.  * white --- is character whitespace?
  253.  */
  254. white(c)
  255. char c;
  256. {
  257.  if( c == TAB || c == BLANK || c == '\n' )return(YES);
  258.  return(NO);
  259. }
  260.  
  261. /*
  262.  * alloc --- allocate memory
  263.  */
  264. char *
  265. alloc(nbytes)
  266. int nbytes;
  267. {
  268.  char *malloc();
  269.  
  270.  return(malloc(nbytes));
  271. }
  272.  
  273. /*
  274.  * strcmp --- string compare
  275.  */
  276. strcmp(s,t)
  277. char *s;
  278. char *t;
  279. {
  280.  for(; *s == *t; s++,t++)
  281.   if( *s == '\0')
  282.    return(0);
  283.  return(*s - *t);
  284. }
  285.