Commit | Line | Data |
---|---|---|
437b3ba8 | 1 | #include <stdio.h> |
2 | #include <unistd.h> | |
3 | #include <stdlib.h> | |
4 | #include <fcntl.h> | |
5 | #include <stdarg.h> | |
6 | #include <sys/select.h> | |
7 | #include "hw_types.h" | |
8 | #include "hw_helpers.h" | |
d34afc1e PH |
9 | #include "tape.h" |
10 | #include "process.h" | |
11 | #include "split.h" | |
12 | ||
437b3ba8 | 13 | #define XON 0x11 |
14 | #define XOFF 0x13 | |
15 | #define CHAR_START 0x81 | |
16 | // 0x93 | |
17 | #define CHAR_END1 0223 | |
18 | #define CHAR_END2 0223 | |
19 | #define CHAR_END3 0223 | |
20 | #define MAX_BLOCK_SIZE 500 | |
21 | ||
22 | //#define DEBUG | |
036c9298 | 23 | #define DEBUG_CHECKSUM |
24 | #define DEBUG_STRUCTURE | |
25 | #define DEBUG_COMBINE | |
437b3ba8 | 26 | //#define IGNORE_CHECKSUM |
27 | #define HARDCORE_DEBUG | |
28 | /********************************************************/ | |
29 | void output (char *string, int length); | |
30 | datablock *get_block (); | |
31 | ||
32 | /********************************************************/ | |
33 | char ends[3] = { CHAR_END1, CHAR_END2, CHAR_END3 }; | |
34 | int errflag = 0; // Global error flag | |
35 | int tapemode = 0; | |
36 | int inputfd; | |
37 | int ignore = 0; | |
38 | int dosplit = 0; | |
39 | int splitnum = 0; | |
77ce3d6a | 40 | int silent = 0; |
0413f345 | 41 | int read_pos = -5; |
42 | int block_pos= -5; | |
437b3ba8 | 43 | /********************************************************/ |
44 | ||
45 | ||
46 | /********************************************************/ | |
47 | void dump_data (void *data, int count) | |
48 | { | |
49 | int fd = open ("dump", O_RDWR | O_CREAT | O_APPEND, 0666); | |
50 | write (fd, "***", 3); | |
51 | write (fd, data, count); | |
52 | close (fd); | |
53 | } | |
54 | ||
55 | /********************************************************/ | |
56 | void endwarn (int ret) | |
57 | { | |
58 | if (ret < 1) { | |
59 | err ("Unexpected problem in input"); | |
60 | exit (3); | |
61 | } | |
62 | } | |
63 | ||
64 | /********************************************************/ | |
65 | /* Small output things */ | |
66 | void err (char *string) | |
67 | { | |
68 | printf ("Error: --> %s\n", string); | |
69 | errflag++; | |
70 | } | |
71 | ||
72 | void msg (char *string) | |
73 | { | |
74 | #ifdef DEBUG | |
75 | printf ("** %s\n", string); | |
76 | #endif | |
77 | } | |
78 | ||
79 | void msgf (char *format, ...) | |
80 | { | |
81 | va_list ag; | |
82 | va_start (ag, format); | |
83 | char ms[100]; | |
84 | vsprintf (ms, format, ag); | |
85 | msg (ms); | |
86 | va_end (ag); | |
87 | } | |
88 | ||
89 | void errf (char *format, ...) | |
90 | { | |
91 | va_list ag; | |
92 | va_start (ag, format); | |
93 | char ms[100]; | |
94 | vsprintf (ms, format, ag); | |
95 | err (ms); | |
96 | va_end (ag); | |
97 | } | |
98 | ||
99 | void say (char *string) | |
100 | { | |
77ce3d6a | 101 | if (!silent) printf ("%s\n", string); |
437b3ba8 | 102 | } |
103 | ||
104 | void sayf (char *format, ...) | |
105 | { | |
106 | va_list ag; | |
107 | va_start (ag, format); | |
108 | char ms[100]; | |
109 | vsprintf (ms, format, ag); | |
110 | say (ms); | |
111 | va_end (ag); | |
112 | } | |
113 | ||
114 | ||
115 | /********************************************************/ | |
116 | /********************************************************/ | |
117 | ||
118 | /********************************************************/ | |
119 | /*Block input routines*/ | |
120 | ||
121 | unsigned char decode (unsigned char in) | |
122 | { | |
123 | if (in & 64) | |
124 | switch (in) { | |
125 | case 0174: | |
126 | return 05; | |
127 | break; | |
128 | case 0374: | |
129 | return 045; | |
130 | break; | |
131 | case 0175: | |
132 | return 012; | |
133 | break; | |
134 | case 0375: | |
135 | return 052; | |
136 | break; | |
137 | case 0176: | |
138 | return 021; | |
139 | break; | |
140 | case 0376: | |
141 | return 061; | |
142 | break; | |
143 | case 0177: | |
144 | return 023; | |
145 | break; | |
146 | case 0377: | |
147 | return 063; | |
148 | break; | |
149 | default: | |
150 | err ("decode(): Impossible case!!!\n"); | |
151 | return 0; | |
152 | break; | |
153 | } else { | |
154 | if (in & 128) | |
155 | in |= 32; | |
156 | return in; | |
157 | } | |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
162 | /********************************************************/ | |
163 | /********************************************************/ | |
164 | ||
165 | void combine466 (unsigned char *raw, unsigned char *data) | |
166 | { | |
167 | hw16 *result = (hw16 *) data; | |
168 | ||
169 | // result->tape.low = 077; | |
170 | unsigned char r1, r2, r3; | |
171 | ||
172 | r1 = decode ((raw)[0]) & 017; | |
173 | r2 = decode ((raw)[1]) & 077; | |
174 | r3 = decode ((raw)[2]) & 077; | |
175 | ||
176 | result->tape.high = r1; | |
177 | result->tape.mid = r2; | |
178 | result->tape.low = r3; | |
179 | ||
180 | #ifdef DEBUG_COMBINE | |
036c9298 | 181 | msgf ("combine466 (data) : %02x %02x %02x",r1,r2,r3, 6); |
182 | // msgf ("combine466 (rawmem) : %s%s", bin2str (data[1], 8), | |
183 | // bin2str (data[0], 8)); | |
437b3ba8 | 184 | msgf ("combine466 (structure): %s", bin2str (result->value, 16)); |
185 | #endif | |
186 | } | |
187 | ||
188 | /*********************************************************************/ | |
189 | /*********************************************************************/ | |
190 | int checksum_error (hw16 * data, int size) | |
191 | { | |
192 | unsigned short int checksumme = 0; | |
193 | int c; | |
194 | for (c = 0; c < (size - 1); c++) { | |
195 | checksumme ^= data[c].value; | |
196 | #ifdef DEBUG_CHECKSUM | |
197 | msgf ("checksum_error(): Gelesen:%04x Berechnet:%04x", | |
198 | data[c + 1].value, checksumme); | |
199 | if (data[c + 1].value == checksumme) | |
200 | msg ("checksum_error (): Treffer."); | |
201 | #endif | |
202 | } | |
203 | #ifdef DEBUG_CHECKSUM | |
204 | msgf ("checksum_error(): Gelesen:%04x Berechnet:%04x", | |
205 | data[size - 1].value, checksumme); | |
206 | #endif | |
207 | if (data[size - 1].value != checksumme) | |
208 | return 1; | |
209 | return 0; | |
210 | } | |
211 | ||
212 | /********************************************************/ | |
213 | datablock *get_block() | |
214 | { | |
215 | int blk_size, round, ret, found, complete; | |
216 | unsigned char buffer[3]; | |
217 | hw16 *blk_data; | |
218 | unsigned char *raw_data; | |
219 | ||
220 | if (tapemode) | |
221 | tapestart (); | |
222 | do { | |
0413f345 | 223 | read_pos+=ret=read (inputfd, buffer, 1); |
224 | ||
437b3ba8 | 225 | #ifdef HARDCORE_DEBUG |
226 | msgf ("READ : (hex)%02x (bin)%s (dec)%3i (oct)%3o", | |
227 | buffer[0], bin2str (buffer[0], 8), buffer[0], buffer[0]); | |
228 | #endif | |
229 | if (ret < 1) { | |
230 | msg ("get_block(): Nichts mehr zu lesen"); | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | if (buffer[0]==0x83){ | |
235 | datablock *result = (datablock *) malloc (sizeof (datablock)); | |
236 | result->type = BT_STOP; | |
237 | result->data = NULL; | |
238 | result->raw_data = NULL; | |
239 | result->size = 0; | |
240 | return result; | |
241 | } | |
242 | ||
243 | } while (*buffer != (unsigned char) CHAR_START); | |
0413f345 | 244 | |
245 | block_pos=read_pos; | |
437b3ba8 | 246 | msg ("get_block(): Blockstart erkannt"); |
247 | ||
248 | // blk_data = (hw16 *) malloc (MAX_BLOCK_SIZE * sizeof (hw16)); | |
249 | blk_data=NULL; | |
250 | raw_data=NULL; | |
251 | round = 0; | |
252 | blk_size = 0; | |
253 | ||
254 | do { | |
255 | complete = 0; | |
256 | ret = read (inputfd, buffer + round, 1); | |
0413f345 | 257 | read_pos+=ret; |
437b3ba8 | 258 | #ifdef HARDCORE_DEBUG |
259 | static int lochzahl = 0; | |
260 | char *bin = bin2str (buffer[round], 8); | |
261 | int a; | |
262 | for (a = 0; a < 8; a++) | |
263 | if (bin[a] == '1') | |
264 | lochzahl++; | |
265 | msgf ("READ (round %i): (hex)%02x (bin)%s (dec)%3i " | |
266 | " (oct)%3o Lochzahl:%4i", | |
267 | round, buffer[round], bin, buffer[round], buffer[round], lochzahl); | |
268 | ||
269 | // msgf ("READ(round %i) (hex)%02x", round, buffer[round]); | |
270 | #endif | |
271 | endwarn (ret); | |
272 | round++; | |
273 | found = 0; | |
274 | if (buffer[round - 1] == CHAR_END1) | |
275 | found++; | |
276 | if (buffer[round - 1] == CHAR_END2) | |
277 | found++; | |
278 | ||
279 | if (found) { | |
280 | complete++; | |
281 | } else { | |
282 | if (round == 3) { | |
283 | //if (blk_size == MAX_BLOCK_SIZE) { | |
284 |