home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_300
/
363_01
/
object.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-17
|
6KB
|
197 lines
/***********************************************************************
*
* OBJECT.C
* Object File Routines for 68000 Assembler
*
* Function: initObj()
* Opens the specified object code file for writing. If the file cannot
* be opened, then the routine prints a message and exits.
*
* outputObj()
* Places the data whose size, value, and address are specified in the
* object code file. If the new data would cause the current S-record to
* exceed a certain length, or if the address of the current item doesn't
* follow immediately after the address of the previous item, then the
* current S-record is written to the file (using writeObj) and a new
* S-record is started, beginning with the specified data.
*
* writeObj()
* Writes the current S-record to the object code file. The record length
* and checksum fields are filled in before the S-record is written. If
* an error occurs during the writing, the routine prints a message and
* exits.
*
* finishObj()
* Flushes the S-record buffer by writing out the data in it (using
* writeObj), if any, then writes a termination S-record and closes the
* object code file. If an error occurs during this write, the routine
* prints a messge and exits.
*
* Usage: initObj(char *name)
*
* int outputObj(long newAddr, int data, int size)
*
* int writeObj(void)
*
* finishint Obj(void)
*
* Author: Paul McKee
* ECE492 North Carolina State University, 12/13/86
* Modified A.E. Romer 17 March 1991
* Version 1.0
************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include "asm.h"
/* Define the maximum number of bytes (address, data,
and checksum) that can be in one S-record */
#define SRECSIZE 36
extern char line[256];
extern FILE *objFile;
static char sRecord[80], *objPtr;
static char byteCount, checksum, lineFlag;
static long objAddr;
static char objErrorMsg[] = "Error writing to object file\n";
int initObj(char *name)
{
objFile = fopen(name, "w");
if (!objFile)
{
puts("Can't open object file");
exit(0);
}
/* Output S-record file header */
/* fputs("Here comes an S-record...\n", objFile); */
fputs("S004000020DB\n", objFile);
lineFlag = FALSE;
return NORMAL;
}
int outputObj(long newAddr, long data, int size)
{
/* If the new data doesn't follow the previous data, or if the S-record
would be too long, then write out this S-record and start a new one */
if ((lineFlag && (newAddr != objAddr)) || (byteCount + size > SRECSIZE))
{
writeObj();
lineFlag = FALSE;
}
/* If no S-record is already being assembled, then start making one */
if (!lineFlag)
{
if ((newAddr & 0xFFFF) == newAddr)
{
sprintf(sRecord, "S1 %04X", newAddr);
byteCount = 2;
}
else if ((newAddr & 0xFFFFFF) == newAddr)
{
sprintf(sRecord, "S2 %06X", newAddr);
byteCount = 3;
}
else
{
sprintf(sRecord, "S3 %08lX", newAddr);
byteCount = 4;
}
objPtr = sRecord + 4 + byteCount*2;
checksum = (char) (checkValue(newAddr) & 0xff);
objAddr = newAddr;
lineFlag = TRUE;
}
/* Add the new data to the S-record */
switch (size)
{
case BYTE : data &= 0xFF;
sprintf(objPtr, "%02X", data);
byteCount++;
checksum += (char) (data & 0xff);
break;
case WORD : data &= 0xFFFF;
sprintf(objPtr, "%04X", data);
byteCount += 2;
checksum += (char) (checkValue(data) & 0xff);
break;
case LONG : sprintf(objPtr, "%08lX", data);
byteCount += 4;
checksum += (char) (checkValue(data) & 0xff);
break;
default : printf("outputObj: INVALID SIZE CODE!\n");
exit(0);
}
objPtr += size*2;
objAddr += (long) size;
return NORMAL;
}
long checkValue(long data)
{
return (data + (data >> 8) + (data >> 16) + (data >> 24)) & 0xFF;
}
int writeObj(void)
{
char recLen[3];
/* Fill in the record length
* (including the checksum in the record length */
sprintf(recLen, "%02X", ++byteCount);
strncpy(sRecord+2, recLen, 2);
/* Add the checksum
* (including in the checksum the record length) */
checksum += byteCount;
sprintf(objPtr, "%02X\n", (~checksum & 0xFF));
/* Output the S-record to the object file */
/* fputs("Here comes an S-record...\n", objFile); */
fputs(sRecord, objFile);
if ferror(objFile)
{
fputs(objErrorMsg, stderr);
exit(0);
}
return NORMAL;
}
int finishObj(void)
{
/* Write out the last real S-record, if present */
if (lineFlag)
writeObj();
/* Write out a termination S-record and close the file*/
fputs("S9030000FC\n", objFile);
if ferror(objFile)
{
fputs(objErrorMsg, stderr);
exit(0);
}
fclose(objFile);
return NORMAL;
}