home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CROSSASM / 68ASMSIM.ZIP / asmsrc / object.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-07  |  4.8 KB  |  198 lines

  1. /***********************************************************************
  2.  *
  3.  *        OBJECT.C
  4.  *        Object File Routines for 68000 Assembler
  5.  *
  6.  *    Function: initObj()
  7.  *        Opens the specified object code file for writing. If
  8.  *        the file cannot be opened, then the routine prints a
  9.  *        message and exits. 
  10.  *
  11.  *        outputObj()
  12.  *        Places the data whose size, value, and address are
  13.  *        specified in the object code file. If the new data
  14.  *        would cause the current S-record to exceed a certain
  15.  *        length, or if the address of the current item doesn't
  16.  *        follow immediately after the address of the previous
  17.  *        item, then the current S-record is written to the file
  18.  *        (using writeObj) and a new S-record is started,
  19.  *        beginning with the specified data. 
  20.  *
  21.  *        writeObj()
  22.  *        Writes the current S-record to the object code file.
  23.  *        The record length and checksum fields are filled in
  24.  *        before the S-record is written. If an error occurs
  25.  *        during the writing, the routine prints a message and
  26.  *        exits. 
  27.  *
  28.  *        finishObj()
  29.  *        Flushes the S-record buffer by writing out the data in
  30.  *        it (using writeObj), if any, then writes a termination
  31.  *        S-record and closes the object code file. If an error 
  32.  *        occurs during this write, the routine prints a messge 
  33.  *        and exits.
  34.  *
  35.  *     Usage: initObj(name)
  36.  *        char *name;
  37.  *
  38.  *        outputObj(newAddr, data, size)
  39.  *        int data, size;
  40.  *
  41.  *        writeObj()
  42.  *
  43.  *        finishObj()
  44.  *        
  45.  *      Author: Paul McKee
  46.  *        ECE492    North Carolina State University
  47.  *
  48.  *        Date:    12/13/86
  49.  *
  50.  ************************************************************************/
  51.  
  52.  
  53. #include <stdio.h>
  54. #include <ctype.h>
  55. #include "asm.h"
  56.  
  57. /* Define the maximum number of bytes (address, data, 
  58.    and checksum) that can be in one S-record */
  59. #define SRECSIZE  36    
  60.  
  61. extern char line[256];
  62. extern FILE *objFile;
  63.  
  64. static char sRecord[80], *objPtr;
  65. static char byteCount, checksum, lineFlag;
  66. static long    objAddr;
  67. static char objErrorMsg[] = "Error writing to object file\n";
  68.  
  69. int    initObj(name)
  70. char *name;
  71. {
  72.  
  73.     objFile = fopen(name, "w");
  74.     if (!objFile) {    
  75.         puts("Can't open object file");
  76.         exit(0);
  77.         }
  78.     /* Output S-record file header */
  79. /*    fputs("Here comes an S-record...\n", objFile); */
  80.     fputs("S004000020DB\n", objFile);
  81.     lineFlag = FALSE;
  82.  
  83.     return NORMAL;
  84.  
  85. }
  86.  
  87.  
  88. int    outputObj(newAddr, data, size)
  89. long    newAddr, data;
  90. int    size;
  91. {
  92.     /* If the new data doesn't follow the previous data, or if the S-record
  93.        would be too long, then write out this S-record and start a new one */
  94.     if ((lineFlag && (newAddr != objAddr)) || (byteCount + size > SRECSIZE))
  95.         {
  96.         writeObj();
  97.         lineFlag = FALSE;
  98.         }
  99.  
  100.     /* If no S-record is already being assembled, then start making one */
  101.     if (!lineFlag) {
  102.         if ((newAddr & 0xFFFF) == newAddr) {
  103.             sprintf(sRecord, "S1  %04X", newAddr);
  104.             byteCount = 2;
  105.             }
  106.         else if ((newAddr & 0xFFFFFF) == newAddr) {
  107.             sprintf(sRecord, "S2  %06X", newAddr);
  108.             byteCount = 3;
  109.             }
  110.         else {
  111.             sprintf(sRecord, "S3  %08lX", newAddr);
  112.             byteCount = 4;
  113.             }
  114.         objPtr = sRecord + 4 + byteCount*2;
  115.         checksum = (char) (checkValue(newAddr) & 0xff);
  116.         objAddr = newAddr;
  117.         lineFlag = TRUE;
  118.         }
  119.     
  120.     /* Add the new data to the S-record */
  121.     switch (size) {
  122.         case BYTE : data &= 0xFF;
  123.                         sprintf(objPtr, "%02X", data);
  124.                         byteCount++;
  125.                         checksum += (char) (data & 0xff);
  126.                         break;
  127.         case WORD : data &= 0xFFFF;
  128.                         sprintf(objPtr, "%04X", data); 
  129.                         byteCount += 2;
  130.                         checksum += (char) (checkValue(data) & 0xff);
  131.                         break;
  132.         case LONG : sprintf(objPtr, "%08lX", data);
  133.                         byteCount += 4;
  134.                         checksum += (char) (checkValue(data) & 0xff);
  135.                         break;
  136.         default   : printf("outputObj: INVALID SIZE CODE!\n");
  137.                         exit(0);
  138.         }
  139.     objPtr += size*2;
  140.     objAddr += (long) size;
  141.  
  142.     return NORMAL;
  143.  
  144. }    
  145.     
  146.  
  147.  
  148. long    checkValue(data)
  149. long    data;
  150. {
  151.     return (data + (data >> 8) + (data >> 16) + (data >> 24)) & 0xFF;
  152. }
  153.  
  154.  
  155. int    writeObj()
  156. {
  157. char recLen[3];
  158.  
  159.     /* Fill in the record length (including the checksum in the record length */
  160.     sprintf(recLen, "%02X", ++byteCount);
  161.     strncpy(sRecord+2, recLen, 2);
  162.     
  163.     /* Add the checksum (including in the checksum the record length) */
  164.     checksum += byteCount;
  165.     sprintf(objPtr, "%02X\n", (~checksum & 0xFF));
  166.     
  167.     /* Output the S-record to the object file */
  168. /*    fputs("Here comes an S-record...\n", objFile); */
  169.     fputs(sRecord, objFile);
  170.     if ferror(objFile) {
  171.         fputs(objErrorMsg, stderr);
  172.         exit(0);
  173.         }
  174.  
  175.     return NORMAL;
  176.  
  177. }
  178.  
  179.  
  180. int    finishObj()
  181. {
  182.     /* Write out the last real S-record, if present */
  183.     if (lineFlag)
  184.         writeObj();
  185.  
  186.     /* Write out a termination S-record and close the file*/
  187.     fputs("S9030000FC\n", objFile);
  188.     if ferror(objFile) {
  189.         fputs(objErrorMsg, stderr);
  190.         exit(0);
  191.         }
  192.     fclose(objFile);
  193.  
  194.     return NORMAL;
  195.  
  196. }    
  197.  
  198.