source: buchla-68k/orig/GEMDOS/MSREC.C@ 66b48e7

Last change on this file since 66b48e7 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Imported original source code.

  • Property mode set to 100755
File size: 4.6 KB
Line 
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
41static int csum; /* checsum for current record */
42static 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
60VOID
61outhex(fp, val)
62FILE *fp; /* file pointer for output */
63unsigned 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
83VOID
84outrec(fp, adr, len, buf)
85FILE *fp; /* file pointer for output */
86long adr; /* beginning address for S-record */
87register int len; /* length of data in record */
88register 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
123VOID
124msrec(fp, recadr, len, rp)
125FILE *fp; /* file pointer for output */
126register long recadr; /* beginning address for S-records */
127register long len; /* length of data in buffer */
128register 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
164main()
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
Note: See TracBrowser for help on using the repository browser.