home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 148_01 / a99put.c < prev    next >
Text File  |  1987-09-28  |  5KB  |  239 lines

  1. /*
  2.     TMS9900/99105  Cross-Assembler  v. 1.0
  3.  
  4.     January, 1985
  5.  
  6.     Original 6800 version Copyright (c) 1980 William C. Colley, III.
  7.     Modified for the TMS9900/99105  Series by Alexander Cameron.
  8.  
  9. File:    a99put.c
  10.  
  11. List and hex output routines.
  12. */
  13.  
  14. /*  Get globals:  */
  15.  
  16. #include "a99.gbl"
  17.  
  18. /*
  19. Function to form the list output line and put it to
  20. the list device.  Routine also puts the line to the
  21. console in the event of an error.
  22. */
  23.  
  24. lineout()
  25. {
  26.     char tbuf[25], *tptr, *bptr, count;
  27.     setmem(tbuf,24,' ');
  28.     tbuf[24] = '\0';
  29.     tptr = tbuf;
  30.     *tptr++ = errcode;
  31.     tptr++;
  32.     if (hexflg != NOCODE) puthex4(address,&tptr);
  33.     else tptr += 4;
  34.     tptr += 3;
  35.     count = 0;
  36.     bptr = binbuf;
  37.     while (TRUE)
  38.     {
  39.         if (count == nbytes || count != 0 && count % 4 == 0)
  40.         {
  41.             putlin(tbuf,&lstbuf);
  42.             if (count > 4) putchr('\n',&lstbuf);
  43.             else putlin(linbuf,&lstbuf);
  44.             if (lstbuf.fd != CONO && errcode != ' ')
  45.             {
  46.                 puts(tbuf);
  47.                 if (count >= 5) putchar('\n');
  48.                 else
  49.                      {linbuf[28]='\n';
  50.                      linbuf[29]='\0';    
  51.                       puts(linbuf);
  52.                     }
  53.             }
  54.  
  55.             tptr = tbuf + 2;
  56.             puthex4(address,&tptr);
  57.             setmem(tptr,14,' ');
  58.             tptr += 3;
  59.         }
  60.         if (count == nbytes) return;
  61.         count++;
  62.         address++;
  63.         puthex2(*bptr++,&tptr);
  64.         if( count % 2 == 0)tptr++;
  65.     }
  66. }
  67.  
  68. /*
  69. Function to form the hex output line and put it to
  70. the hex output device.
  71. */
  72.  
  73. hexout()
  74. {
  75.     char count, *bptr;
  76.     switch (hexflg)
  77.     {
  78.         case PUTCODE:    bptr =binbuf;
  79.                 for (count = 1; count <= nbytes; count++)
  80.                 {
  81.                     puthex2(*bptr,&hxlnptr);
  82.                     chksum += *bptr++;
  83.                     if (++hxbytes == 16) flshhbf(pc+count);
  84.                 }
  85.  
  86.         case NOCODE:    return;
  87.  
  88.         case FLUSH:    flshhbf(pc);
  89.                 return;
  90.  
  91.         case NOMORE:    flshhbf(0);
  92.                 putlin(":0000000000\n\032",&hexbuf);
  93.                 flush(&hexbuf);
  94.                 return;
  95.     }
  96. }
  97.  
  98. /*
  99. Function to put a line of intel hex to the appropriate
  100. device and get a new line started.
  101. */
  102.  
  103. flshhbf(loadaddr)
  104. unsigned loadaddr;
  105. {
  106.     if (hxbytes != 0)
  107.     {
  108.         puthex2(-(chksum+hxbytes),&hxlnptr);
  109.         *hxlnptr++ = '\n';
  110.         *hxlnptr++ = '\0';
  111.         hxlnptr = hxlnbuf + 1;
  112.         puthex2(hxbytes,&hxlnptr);
  113.         putlin(hxlnbuf,&hexbuf);
  114.     }
  115.     hxbytes = 0;
  116.     hxlnptr = hxlnbuf;
  117.     *hxlnptr++ = ':';
  118.     hxlnptr += 2;
  119.     puthex4(loadaddr,&hxlnptr);
  120.     puthex2(0,&hxlnptr);
  121.     chksum = (loadaddr >> 8) + (loadaddr & 0xff);
  122. }
  123.  
  124. /*
  125. Function to put a 4-digit hex number into an output line.
  126. */
  127.  
  128. puthex4(number,lineptr)
  129. unsigned number;
  130. char **lineptr;
  131. {
  132.     puthex2(number>>8,lineptr);
  133.     puthex2(number,lineptr);
  134. }
  135.  
  136. /*
  137. Function to put a 2-digit hex number into an output line.
  138. */
  139.  
  140. puthex2(number,lineptr)
  141. char number, **lineptr;
  142. {
  143.     if ((**lineptr = (number >> 4) + '0') > '9') **lineptr += 7;
  144.     if ((*++*lineptr = (number & 0xf) + '0') > '9') **lineptr += 7;
  145.     ++(*lineptr);
  146. }
  147.  
  148. /*
  149. Function to put a decimal number into an output line.
  150. */
  151.  
  152. putdec(number,lineptr)
  153. unsigned number;
  154. char **lineptr;
  155. {
  156.     if (number == 0) return;
  157.     putdec(number/10,lineptr);
  158.     *(*lineptr)++ = number % 10 + '0';
  159. }
  160.  
  161. /*
  162. Function to move a line to a disk buffer.  The line is pointed to
  163. by line, and the disk buffer is specified by its disk I/O buffer
  164. structure dskbuf.
  165. */
  166.  
  167. putlin(line,dskbuf)
  168. char *line;
  169. struct diskbuf *dskbuf;
  170. {
  171.     while (*line != '\0') putchr(*line++,dskbuf);
  172. }
  173.  
  174. /*
  175. Function to put a character into a disk buffer.  The character
  176. is sent in char, and the disk buffer is specified by the address
  177. of its structure.  Newline characters (LF's) are converted to
  178. CR/LF pairs.
  179. */
  180.  
  181. putchr(byte,dskbuf)
  182. char byte;
  183. struct diskbuf *dskbuf;
  184. {    
  185.     char c;
  186.     byte &= 0x7f;
  187.     if(kbhit())
  188.     {
  189.         c = getchar();
  190.         if(c == CTLC) wipeout("\n");
  191.         if(c == CTLS) while(kbhit() == 0) getchar();
  192.     }
  193.     switch (dskbuf -> fd)
  194.     {
  195.         case CONO:    if (byte != CPMEOF) putchar(byte);
  196.  
  197.         case NOFILE:    return;
  198.  
  199.         case LST:    if (byte != CPMEOF)
  200.                 {
  201.                     if (byte == '\n') bdos(LISTOUT,'\r');
  202.                     bdos(LISTOUT,byte);
  203.                 }
  204.                 else putchr('\n',dskbuf);
  205.                 return;
  206.  
  207.         default:    if(dskbuf->fd >= 20){printf("fd=%u\n",dskbuf->fd);return;}
  208.                 if(byte == '\n') putchr('\r',dskbuf);
  209.                 if (dskbuf -> pointr >= dskbuf -> space + (NSECT * 128))
  210.                 {    if (write(dskbuf->fd,dskbuf->space,NSECT) == -1)
  211.                         {
  212.                         printf("\nDisk write error ++%s\n",errmsg(errno()));
  213.                         wipeout("\n");
  214.                         }
  215.                     dskbuf -> pointr = dskbuf -> space;
  216.                 }
  217.                 *(dskbuf -> pointr)++ = byte;
  218.                 return; 
  219.                 
  220.     }
  221. }
  222.  
  223. /*
  224. Function to flush a disk buffer.
  225. */
  226.  
  227. flush(dskbuf)
  228. struct diskbuf *dskbuf;
  229. {
  230.     int t;
  231.     if (dskbuf -> fd < LODISK) return;
  232.     t = dskbuf -> pointr - dskbuf -> space;
  233.     t = (t % 128 == 0) ? t / 128 : t / 128 + 1;
  234.     while (dskbuf -> pointr < &dskbuf -> space[NSECT * 128]) *(dskbuf -> pointr)++ = 0;
  235.     if (write(dskbuf->fd,dskbuf->space,t) == -1) wipeout("\nDisk write error in flush.\n");
  236.     if (close(dskbuf->fd) == -1) wipeout("\nError closing file.\n");
  237. }
  238.  
  239.