[3ae31e9] | 1 | /*
|
---|
| 2 | ============================================================================
|
---|
| 3 | msrec.c -- Motorola S-record output functions
|
---|
| 4 | Version 6 -- 1987-07-10 -- D.N. Lynx Crowe
|
---|
| 5 | (c) Copyright 1987 -- D.N. Lynx Crowe
|
---|
| 6 |
|
---|
| 7 | This file contains functions which will output a buffer as a group
|
---|
| 8 | of Motorola S-records. These functions will output type 2
|
---|
| 9 | S-records for the data (3 byte addresses). Output is to a
|
---|
| 10 | specified file, which must be open when the function is called, and
|
---|
| 11 | which will be left open by the function. The last record will be
|
---|
| 12 | a type 9 S-record.
|
---|
| 13 |
|
---|
| 14 | void Output a buffer as S-records
|
---|
| 15 | msrec(fp, adr, len, buf)
|
---|
| 16 | FILE *fp; file pointer for output
|
---|
| 17 | long adr; beginning address for S-records
|
---|
| 18 | long len; length of data in buffer
|
---|
| 19 | char *buf; buffer address
|
---|
| 20 |
|
---|
| 21 | void Output a single S-record
|
---|
| 22 | outrec(fp, adr, len, buf)
|
---|
| 23 | FILE *fp; file pointer for output
|
---|
| 24 | long adr; beginning address for S-record
|
---|
| 25 | int len; length of data in record
|
---|
| 26 | char *buf; buffer address of record
|
---|
| 27 |
|
---|
| 28 | void Output a byte as 2 ASCII hex characters
|
---|
| 29 | outhex(fp, val)
|
---|
| 30 | FILE *fp; file pointer for output
|
---|
| 31 | unsigned int val; byte to be output
|
---|
| 32 | ============================================================================
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #define TESTER 0 /* define non-zero to get a test program */
|
---|
| 36 |
|
---|
| 37 | #define VOID
|
---|
| 38 |
|
---|
| 39 | #include "stdio.h"
|
---|
| 40 |
|
---|
| 41 | static int csum; /* checsum for current record */
|
---|
| 42 | static char hexdig[] = "0123456789ABCDEF"; /* hex table */
|
---|
| 43 |
|
---|
| 44 | #define RECLEN 32 /* 32 bytes per S-record */
|
---|
| 45 |
|
---|
| 46 | #define GE >=
|
---|
| 47 |
|
---|
| 48 | /* |
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 | /*
|
---|
| 52 | ============================================================================
|
---|
| 53 | outhex(fp, val) -- output a byte in ASCII hex
|
---|
| 54 |
|
---|
| 55 | Outputs the byte val on file fp in ASCII hex and updates
|
---|
| 56 | the accumulated checksum csum.
|
---|
| 57 | ============================================================================
|
---|
| 58 | */
|
---|
| 59 |
|
---|
| 60 | VOID
|
---|
| 61 | outhex(fp, val)
|
---|
| 62 | FILE *fp; /* file pointer for output */
|
---|
| 63 | unsigned int val; /* byte to be output */
|
---|
| 64 | {
|
---|
| 65 | fputc(hexdig[(val >> 4) & 0x0F], fp);
|
---|
| 66 | fputc(hexdig[val & 0x0F], fp);
|
---|
| 67 | csum += (val & 0x0FF);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /* |
---|
| 71 |
|
---|
| 72 | */
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | ============================================================================
|
---|
| 76 | outrec(fp, adr, len, buf) -- output a Motorola S-record
|
---|
| 77 |
|
---|
| 78 | Outputs len bytes from buffer buf to file fp with S-record
|
---|
| 79 | address adr.
|
---|
| 80 | ============================================================================
|
---|
| 81 | */
|
---|
| 82 |
|
---|
| 83 | VOID
|
---|
| 84 | outrec(fp, adr, len, buf)
|
---|
| 85 | FILE *fp; /* file pointer for output */
|
---|
| 86 | long adr; /* beginning address for S-record */
|
---|
| 87 | register int len; /* length of data in record */
|
---|
| 88 | register char *buf; /* buffer address of record */
|
---|
| 89 | {
|
---|
| 90 | register int i;
|
---|
| 91 |
|
---|
| 92 | csum = 0; /* zero the checksum */
|
---|
| 93 | fprintf(fp, "S2"); /* record header */
|
---|
| 94 | outhex(fp, (unsigned int)(len+4)); /* record length */
|
---|
| 95 |
|
---|
| 96 | /* record address */
|
---|
| 97 |
|
---|
| 98 | outhex(fp, (unsigned int)((adr >> 16) & 0x0FFL));
|
---|
| 99 | outhex(fp, (unsigned int)((adr >> 8) & 0x0FFL));
|
---|
| 100 | outhex(fp, (unsigned int)(adr & 0x0FFL));
|
---|
| 101 |
|
---|
| 102 | /* data */
|
---|
| 103 |
|
---|
| 104 | for (i = 0; i < len; i++)
|
---|
| 105 | outhex(fp, (unsigned int)*buf++);
|
---|
| 106 |
|
---|
| 107 | outhex(fp, (~csum) & 0x0FFL); /* checksum */
|
---|
| 108 | fprintf(fp, "\n"); /* CR/LF */
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /* |
---|
| 112 | */
|
---|
| 113 |
|
---|
| 114 | /*
|
---|
| 115 | ============================================================================
|
---|
| 116 | msrec(fp, adr, len, buf) -- output data in Motorola S-record format
|
---|
| 117 |
|
---|
| 118 | Outputs 'len' bytes of data from buffer 'buf' to file pointer 'fp'
|
---|
| 119 | with an initial S-record address of 'adr'.
|
---|
| 120 | ============================================================================
|
---|
| 121 | */
|
---|
| 122 |
|
---|
| 123 | VOID
|
---|
| 124 | msrec(fp, recadr, len, rp)
|
---|
| 125 | FILE *fp; /* file pointer for output */
|
---|
| 126 | register long recadr; /* beginning address for S-records */
|
---|
| 127 | register long len; /* length of data in buffer */
|
---|
| 128 | register char *rp; /* buffer address */
|
---|
| 129 | {
|
---|
| 130 | register int ilen;
|
---|
| 131 |
|
---|
| 132 | while (len) { /* while there's data ... */
|
---|
| 133 |
|
---|
| 134 | if (len GE RECLEN) { /* full record */
|
---|
| 135 |
|
---|
| 136 | outrec(fp, recadr, RECLEN, rp);
|
---|
| 137 | len -= RECLEN;
|
---|
| 138 | rp += RECLEN;
|
---|
| 139 | recadr += RECLEN;
|
---|
| 140 |
|
---|
| 141 | } else { /* final short record */
|
---|
| 142 |
|
---|
| 143 | ilen = len;
|
---|
| 144 | outrec(fp, recadr, ilen ,rp);
|
---|
| 145 | len = 0;
|
---|
| 146 |
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | fprintf(fp, "S904000000FB\n"); /* end of S-records */
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /* |
---|
| 154 | */
|
---|
| 155 |
|
---|
| 156 | #if TESTER
|
---|
| 157 |
|
---|
| 158 | /*
|
---|
| 159 | ============================================================================
|
---|
| 160 | test routine for msrec
|
---|
| 161 | ============================================================================
|
---|
| 162 | */
|
---|
| 163 |
|
---|
| 164 | main()
|
---|
| 165 | {
|
---|
| 166 | printf("Test of msrec\nShort record of 16 bytes\n\n");
|
---|
| 167 | msrec(stdout, 0L, 16L, &hexdig);
|
---|
| 168 |
|
---|
| 169 | printf("\nRecord of 256 bytes\n\n");
|
---|
| 170 | msrec(stdout, 0x0FC0000L, 256L, (char *)0x0FC0000L);
|
---|
| 171 |
|
---|
| 172 | printf("\nRecord of 45 bytes\n\n");
|
---|
| 173 | msrec(stdout, 0x0FC0000L, 45L, (char *)0x0FC0000L);
|
---|
| 174 |
|
---|
| 175 | printf("\nEnd of test\n");
|
---|
| 176 | fclose(stdout);
|
---|
| 177 |
|
---|
| 178 | exit(0);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | #endif
|
---|