FreeCalypso > hg > freecalypso-hwlab
annotate simtool/hexread.c @ 106:66f04193a906
fc-simtool: pb-dump: forgot newline at the end of record
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 25 Jan 2021 06:03:58 +0000 |
parents | 454ff8bd0b83 |
children | 4aaf722ab933 |
rev | line source |
---|---|
101
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the function for reading hex files, |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * to be used in the implementation of manual write commands. |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <ctype.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <string.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <strings.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdio.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdlib.h> |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 decode_hex_digit(c) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 if (c >= '0' && c <= '9') |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 return(c - '0'); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 if (c >= 'A' && c <= 'F') |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 return(c - 'A' + 10); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 if (c >= 'a' && c <= 'f') |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 return(c - 'a' + 10); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 read_hex_data_file(filename, databuf) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 char *filename; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 u_char *databuf; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 FILE *inf; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 unsigned count; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 int c, c2; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 inf = fopen(filename, "r"); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (!inf) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 perror(filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 for (count = 0; ; count++) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 do |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 c = getc(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 while (isspace(c)); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 if (c < 0) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 break; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (!isxdigit(c)) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 inv_input: fprintf(stderr, "%s: invalid hex file input\n", |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 c2 = getc(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (!isxdigit(c2)) |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 goto inv_input; |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 if (count >= 255) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 fprintf(stderr, "%s: hex input data is too long\n", |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 databuf[count] = (decode_hex_digit(c) << 4) | |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 decode_hex_digit(c2); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 fclose(inf); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (!count) { |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 fprintf(stderr, "%s: no hex data input found\n", filename); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 return(-1); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 return(count); |
454ff8bd0b83
fc-simtool: update-bin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } |