Debian Codegraph @

src/ftohex.c

raw symbols SCIP
1 /*
2 the DASM macro assembler (aka small systems cross assembler)
3
4 Copyright (c) 1988-2002 by Matthew Dillon.
5 Copyright (c) 1995 by Olaf "Rhialto" Seibert.
6 Copyright (c) 2003-2008 by Andrew Davie.
7 Copyright (c) 2008 by Peter H. Froehlich.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 /*
25 * FTOHEX.C
26 *
27 * FTOHEX format infile [outfile]
28 *
29 * format: format used when assembling (asm705/asm65)
30 * 1,2,3 -generate straight hex file
31 *
32 * Note that int and char are not used as Lattice C on IBM PCs uses
33 * 16 bit ints and unsigned chars.
34 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #define PERLINE 16
40
41 void exiterr(const char *str);
42 void convert(int format, FILE *in, FILE *out);
43 unsigned int getwlh(FILE *in);
44 void puth(unsigned char c, FILE *out);
45
46 int
47 main(int ac, char **av)
48 {
49 int format;
50 FILE *infile;
51 FILE *outfile;
52
53 if (ac < 3) {
54 puts("FTOHEX format infile [outfile]");
55 puts("format 1 = DEFAULT, 2 = RAS, or 3 = RAW");
56 puts("Copyright (c) 1988-2008 by various authors (see file AUTHORS).");
57 exit(1);
58 }
59 format = atoi(av[1]);
60 if (format < 1 || format > 3)
61 exiterr("specify infile format 1, 2, or 3");
62 infile = fopen(av[2], "r");
63 if (infile == NULL)
64 exiterr("unable to open input file");
65 outfile = (av[3]) ? fopen(av[3], "w") : stdout;
66 if (outfile == NULL)
67 exiterr("unable to open output file");
68 convert(format, infile, outfile);
69 fclose(infile);
70 fclose(outfile);
71
72 return 0;
73 }
74
75 void
76 exiterr(const char *str)
77 {
78 fputs(str, stderr);
79 fputs("\n", stderr);
80 exit(1);
81 }
82
83 /*
84 * Formats:
85 *
86 * 1: origin (word:lsb,msb) + data
87 * 2: origin (word:lsb,msb) + length (word:lsb,msb) + data (repeat)
88 * 3: data
89 *
90 * Hex output:
91 *
92 * :lloooo00(ll bytes hex code)cc ll=# of bytes
93 * oooo=origin
94 * cc=invert of checksum all codes
95 */
96
97 void
98 convert(int format, FILE *in, FILE *out)
99 {
100 unsigned int org = 0;
101 unsigned int idx;
102 long len;
103 unsigned char buf[256];
104
105 if (format < 3)
106 org = getwlh(in);
107 if (format == 2) {
108 len = getwlh(in);
109 } else {
110 long begin = ftell(in);
111 fseek(in, 0, SEEK_END);
112 len = ftell(in) - begin;
113 fseek(in, begin, 0);
114 }
115 for (;;) {
116 while (len > 0) {
117 register unsigned char chk;
118 register unsigned int i;
119
120 idx = (len > PERLINE) ? PERLINE : len;
121 fread(buf, idx, 1, in);
122 putc(':', out);
123 puth(idx, out);
124 puth(org >> 8, out);
125 puth(org & 0xFF, out);
126 putc('0', out);
127 putc('0', out);
128 chk = idx + (org >> 8) + (org & 0xFF);
129 for (i = 0; i < idx; ++i) {
130 chk += buf[i];
131 puth(buf[i], out);
132 }
133 puth((unsigned char)-chk, out);
134 putc('\r', out);
135 putc('\n', out);
136 len -= idx;
137 org += idx;
138 }
139 if (format == 2) {
140 org = getwlh(in);
141 if (feof(in))
142 break;
143 len = getwlh(in);
144 } else {
145 break;
146 }
147 }
148 fprintf(out, ":00000001FF\r\n");
149 }
150
151 unsigned int getwlh(FILE *in)
152 {
153 unsigned int result;
154
155 result = getc(in);
156 result += getc(in) << 8;
157 return result;
158 }
159
160 void
161 puth(unsigned char c, FILE *out)
162 {
163 static char dig[] = { "0123456789ABCDEF" };
164 putc(dig[c>>4], out);
165 putc(dig[c&15], out);
166 }
167