| 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 PERLINEcxx . . $ `src/ftohex.c:39:9`! 16 |
| 40 | |
| 41 | void exiterrcxx . . $ exiterr(85c52e162fed56f9).(const char *str); |
| 42 | void convertcxx . . $ convert(aa82a8eb9dfcaf33).(int format, FILEcxx . . $ FILE# *in, FILEcxx . . $ FILE# *out); |
| 43 | unsigned int getwlhcxx . . $ getwlh(9eef1996c458a0e7).(FILEcxx . . $ FILE# *in); |
| 44 | void puthcxx . . $ puth(7d580649b6eaea43).(unsigned char c, FILEcxx . . $ FILE# *out); |
| 45 | |
| 46 | int |
| 47 | maincxx . . $ main(afcd3168fc1188a4).(int ac, char **av) |
| 48 | { |
| 49 | int format; |
| 50 | FILEcxx . . $ FILE# *infile; |
| 51 | FILEcxx . . $ FILE# *outfile; |
| 52 | |
| 53 | if (ac < 3) { |
| 54 | putscxx . . $ puts(5c4306d8553b3696).("FTOHEX format infile [outfile]"); |
| 55 | putscxx . . $ puts(5c4306d8553b3696).("format 1 = DEFAULT, 2 = RAS, or 3 = RAW"); |
| 56 | putscxx . . $ puts(5c4306d8553b3696).("Copyright (c) 1988-2008 by various authors (see file AUTHORS)."); |
| 57 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 58 | } |
| 59 | format = atoicxx . . $ atoi(5c4306d8553b3696).(av[1]); |
| 60 | if (format < 1 || format > 3) |
| 61 | exiterrcxx . . $ exiterr(85c52e162fed56f9).("specify infile format 1, 2, or 3"); |
| 62 | infile = fopencxx . . $ fopen(82ec0787cd37e0a3).(av[2], "r"); |
| 63 | if (infile == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 64 | exiterrcxx . . $ exiterr(85c52e162fed56f9).("unable to open input file"); |
| 65 | outfile = (av[3]) ? fopencxx . . $ fopen(82ec0787cd37e0a3).(av[3], "w") : stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!; |
| 66 | if (outfile == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 67 | exiterrcxx . . $ exiterr(85c52e162fed56f9).("unable to open output file"); |
| 68 | convertcxx . . $ convert(aa82a8eb9dfcaf33).(format, infile, outfile); |
| 69 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(infile); |
| 70 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(outfile); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | exiterrcxx . . $ exiterr(85c52e162fed56f9).(const char *str) |
| 77 | { |
| 78 | fputscxx . . $ fputs(874fa6a5e59ab9bd).(str, stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!); |
| 79 | fputscxx . . $ fputs(874fa6a5e59ab9bd).("\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!); |
| 80 | exitcxx . . $ exit(3ca292c338df9bb3).(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 | convertcxx . . $ convert(aa82a8eb9dfcaf33).(int format, FILEcxx . . $ FILE# *in, FILEcxx . . $ 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 = getwlhcxx . . $ getwlh(9eef1996c458a0e7).(in); |
| 107 | if (format == 2) { |
| 108 | len = getwlhcxx . . $ getwlh(9eef1996c458a0e7).(in); |
| 109 | } else { |
| 110 | long begin = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(in); |
| 111 | fseekcxx . . $ fseek(543fae30d249f6b1).(in, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!); |
| 112 | len = ftellcxx . . $ ftell(1dcafcd8ef4c6e6c).(in) - begin; |
| 113 | fseekcxx . . $ fseek(543fae30d249f6b1).(in, begin, 0); |
| 114 | } |
| 115 | for (;;) { |
| 116 | while (len > 0) { |
| 117 | register unsigned char chk; |
| 118 | register unsigned int i; |
| 119 | |
| 120 | idx = (len > PERLINEcxx . . $ `src/ftohex.c:39:9`!) ? PERLINEcxx . . $ `src/ftohex.c:39:9`! : len; |
| 121 | freadcxx . . $ fread(738fc8ac1d711e58).(buf, idx, 1, in); |
| 122 | putccxx . . $ putc(b1cd03d6c470d1bd).(':', out); |
| 123 | puthcxx . . $ puth(7d580649b6eaea43).(idx, out); |
| 124 | puthcxx . . $ puth(7d580649b6eaea43).(org >> 8, out); |
| 125 | puthcxx . . $ puth(7d580649b6eaea43).(org & 0xFF, out); |
| 126 | putccxx . . $ putc(b1cd03d6c470d1bd).('0', out); |
| 127 | putccxx . . $ putc(b1cd03d6c470d1bd).('0', out); |
| 128 | chk = idx + (org >> 8) + (org & 0xFF); |
| 129 | for (i = 0; i < idx; ++i) { |
| 130 | chk += buf[i]; |
| 131 | puthcxx . . $ puth(7d580649b6eaea43).(buf[i], out); |
| 132 | } |
| 133 | puthcxx . . $ puth(7d580649b6eaea43).((unsigned char)-chk, out); |
| 134 | putccxx . . $ putc(b1cd03d6c470d1bd).('\r', out); |
| 135 | putccxx . . $ putc(b1cd03d6c470d1bd).('\n', out); |
| 136 | len -= idx; |
| 137 | org += idx; |
| 138 | } |
| 139 | if (format == 2) { |
| 140 | org = getwlhcxx . . $ getwlh(9eef1996c458a0e7).(in); |
| 141 | if (feofcxx . . $ feof(89bc1efcdac0ca22).(in)) |
| 142 | break; |
| 143 | len = getwlhcxx . . $ getwlh(9eef1996c458a0e7).(in); |
| 144 | } else { |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(out, ":00000001FF\r\n"); |
| 149 | } |
| 150 | |
| 151 | unsigned int getwlhcxx . . $ getwlh(9eef1996c458a0e7).(FILEcxx . . $ FILE# *in) |
| 152 | { |
| 153 | unsigned int result; |
| 154 | |
| 155 | result = getccxx . . $ getc(89bc1efcdac0ca22).(in); |
| 156 | result += getccxx . . $ getc(89bc1efcdac0ca22).(in) << 8; |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | puthcxx . . $ puth(7d580649b6eaea43).(unsigned char c, FILEcxx . . $ FILE# *out) |
| 162 | { |
| 163 | static char dig[] = { "0123456789ABCDEF" }; |
| 164 | putccxx . . $ putc(b1cd03d6c470d1bd).(dig[c>>4], out); |
| 165 | putccxx . . $ putc(b1cd03d6c470d1bd).(dig[c&15], out); |
| 166 | } |
| 167 |