*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4
5 #include "tape_block.hh"
6 #include "data_block.hh"
7 #include "data_block_0.hh"
8
9 #include "silent_code.hh"
10 #include "hw_constants.hh"
11
12
13 tape_block * tape_block::gen_from_fd(int fd,
14 void(*input_start)(void *),
15 void (*input_stop)(void *),
16 void * start_stop_arg)
17 {
18 tape_block * res_block;
19 res_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
20 data_block * d_block;
21 data_block_0 * d0_block;
22
23 // Retype to data_block if possible
24 switch(res_block->get_type()){
25 case tape_block::TBT_DATA: // Make pointer a data block
26 d_block=new data_block(*res_block);
27 delete res_block;
28 res_block=d_block;
29 break;
30 default: // All other cases
31 return res_block;
32 }
33
34 // Retype again, if it's a data_block now
35
36 switch(res_block->get_type()){
37 case 00:
38 d0_block=new data_block_0(*d_block);
39 d_block=d0_block;
40 break;
41 // case 01: d_block=new data_block_1(*d_block); break;
42 // case 02: d_block=new data_block_2(*d_block); break;
43 // case 03: d_block=new data_block_3(*d_block); break;
44 // case 04: d_block=new data_block_4(*d_block); break;
45 // case 05: d_block=new data_block_5(*d_block); break;
46 // case 06: d_block=new data_block_6(*d_block); break;
47 // case 07: d_block=new data_block_7(*d_block); break;
48 default:
49 return res_block;
50 }
51 delete res_block;
52 res_block=d_block;
53
54 if (res_block->get_type()==0) switch(d0_block->get_subtype()){
55 // case 000: d0_block=new data_block_0_00(d0_block); break;
56 // case 001: d0_block=new data_block_0_01(d0_block); break;
57 // case 002: d0_block=new data_block_0_02(d0_block); break;
58 // case 003: d0_block=new data_block_0_03(d0_block); break;
59 // case 004: d0_block=new data_block_0_04(d0_block); break;
60 // case 010: d0_block=new data_block_0_10(d0_block); break;
61 // case 014: d0_block=new data_block_0_14(d0_block); break;
62 // case 024: d0_block=new data_block_0_24(d0_block); break;
63 // case 030: d0_block=new data_block_0_30(d0_block); break;
64 // case 054: d0_block=new data_block_0_54(d0_block); break;
65 // case 060: d0_block=new data_block_0_60(d0_block); break;
66 default:
67 return res_block;
68 }
69 delete res_block;
70 res_block=d0_block;
71
72 return res_block;
73 }
74
75 /*!
76 *\brief Default constructor.
77 */
78 tape_block::tape_block(){}
79
80
81 tape_block::tape_block(tape_block & org){
82 block_type=org.block_type;
83 init_state=org.init_state;
84 data_read=org.data_read;
85 raw_data=0;
86 raw_size=org.raw_size;
87 word_data=0;
88 word_size=org.word_size;
89 if (raw_size){
90 raw_data=(unsigned char *)malloc(raw_size);
91 memcpy(raw_data,org.raw_data,raw_size);
92 }
93 if (word_size){
94 word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
95 memcpy(word_data,org.word_data,word_size*sizeof(unsigned short));
96 }
97 }
98
99 /*!
100 *\brief Automatic constructor with file descriptor support
101 *
102 * This constructor is used to initialise the block read in the block's
103 * data via a file descriptor.\n
104 * This is done in the following way:
105 * - input_start() is called.
106 * - Data is read from fd. Stops on end of file or end of block
107 * or IO error. On EOF or IO error input_stop() is NOT called.
108 * - If there was no IO error the block's checksum is calculated
109 * and checked.
110 * - The block's type field is initialised.
111 * - input_stop() is called.
112 *
113 *\param fd A file descriptor where the read is taken from
114 *\param input_stop A pointer to a function called at the end of input
115 *\param input_start A pointer to a function called at the beginning
116 * of input
117 *\param start_stop_arg A pointer passed to input_start and input_stop().
118 */
119 tape_block::tape_block (int fd,
120 void(*input_start)(void *),
121 void (*input_stop)(void *),
122 void * start_stop_arg
123 )
124 {
125 unsigned char buffer;
126 int retval;
127 int eot_needed=EOT_LENGTH;
128 int eob_needed=EOB_LENGTH;
129 int block_complete=0;
130
131 block_type= TBT_DISCARD;
132 init_state= TBS_DEFAULT;
133 data_read = 0;
134 raw_data = 0;
135 raw_size = 0;
136 word_data = 0;
137 word_size = 0;
138
139 if (input_start) input_start (start_stop_arg);
140
141 /* Look for block start in input stream */
142 do {
143 retval=read(fd, &buffer, 1);
144 if (retval == 0){ // End of file
145 block_type=TBT_DISCARD;
146 init_state=TBS_EOF_LEGAL;
147 return;
148 }
149 if (retval == -1){ // Error reading from fd
150 block_type=TBT_DISCARD;
151 init_state=TBS_IOERR;
152 return;
153 }
154 data_read++;
155
156 /* Look for end ot tape sequence */
157 switch(eot_needed){
158 case 3:
159 if (buffer==EOT_SEQ_1) eot_needed--;
160 else eot_needed=EOT_LENGTH;
161 break;
162 case 2:
163 if (buffer==EOT_SEQ_2) eot_needed--;
164 else eot_needed=EOT_LENGTH;
165 break;
166 case 1:
167 if (buffer==EOT_SEQ_3){
168 raw_data=(unsigned char*)malloc(3);
169 raw_size=3;
170 raw_data[0]=EOT_SEQ_1;
171 raw_data[1]=EOT_SEQ_2;
172 raw_data[2]=EOT_SEQ_3;
173 block_type =TBT_EOT;
174 init_state =TBS_OK;
175 return;
176 }
177 else eot_needed=EOT_LENGTH;
178 break;
179 }
180 } while (buffer != (unsigned char) BLOCK_START_DELIMITER);
181
182
183 /* Now input the block data */
184 block_complete = 0;
185 do {
186 retval = read (fd, &buffer, 1);
187
188 if (retval == 0){ // End of file
189 block_type=TBT_DISCARD;
190 init_state=TBS_EOF_ILLEGAL;
191 return;
192 }
193 if (retval == -1){ // Error reading from fd
194 block_type=TBT_DISCARD;
195 init_state=TBS_IOERR;
196 return;
197 }
198 data_read++; // We have consumed a byte
199
200 /* sort in the new byte */
201 raw_size++;
202 raw_data=(unsigned char*)realloc(raw_data,raw_size);
203 raw_data[raw_size-1]=buffer;
204
205 /* Look for end ot block sequence */
206 switch(eob_needed){
207 case 2:
208 if (buffer==BLOCK_END_DELIMITER_1) eob_needed--;
209 else eob_needed=EOB_LENGTH;
210 break;
211 case 1:
212 if (buffer==BLOCK_END_DELIMITER_2){
213 raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data!
214 raw_data=(unsigned char*)realloc(raw_data,raw_size);
215 block_complete=1;
216 }
217 else eob_needed=EOB_LENGTH;
218 break;
219 }
220 }while (!block_complete);
221 if (input_stop) input_stop (start_stop_arg);
222
223 /* Now we have the data in. */
224
225 if (init_words()!=0){
226 init_state=TBS_CHECKSUM;
227 return;
228 }
229
230 if (test_checksum()!=0){
231 init_state=TBS_CHECKSUM;
232 return;
233 }
234
235 block_type=TBT_DATA;
236 init_state=TBS_OK;
237 }
238
239 /*!
240 *\brief Default destructor.
241 */
242 tape_block::~tape_block(){
243 free(raw_data);
244 free(word_data);
245 }
246
247 /*!
248 *\brief Query the block's state.
249 *
250 * If the state is not TBS_OK, the block must be discarded.
251 *\return The block's initialisation state
252 */
253 tape_block::tb_state_t tape_block::get_state(){
254 return init_state;
255 }
256
257
258 /*!
259 *\brief Get size of the internal raw data buffer
260 *\return The size of the internal raw data buffer
261 */
262 int tape_block::get_raw_size(){
263 return raw_size;
264 }
265
266 /*!
267 *\brief Access internal raw data buffer
268 *
269 * The raw data buffer contains all block data except the \ \
270 * block start delimiter and block end delimiter.
271 *\return Pointer to internal raw data buffer of the block
272 */
273 unsigned char * tape_block::get_raw_data(){
274 return raw_data;
275 }
276
277 /*!
278 *\brief Determine the block's type
279 *
280 * This routine returns the block's type.\\
281 *\retval TBT_DATA if it's a data block
282 *\retval TBT_EOT if it's an end of tape mark
283 *\retval TBR_DISCARD if the block is unusable
284 *
285 *\note This function is virtual. That means that if the tape block in
286 * question is of the subtype data_block which overwrites this method,
287 * it will return the block type contained in the upper 4 bits of the
288 * first data word! Try to avoid testing for TBT_DATA. \n
289 * Blocks generated by gen_from_fd should never have TBT_DATA.
290 */
291 int tape_block::get_type(){
292 return block_type;
293 }
294
295 /*!
296 *\brief Determine the block's subtype
297 *
298 * Some data block types have a subtype.
299 *\note This is to be used in derived classes.
300 *\return Always 0
301 */
302 int tape_block::get_subtype(){
303 return 0;
304 }
305
306
307 /*!
308 *\brief Initialize word representation of data
309 *\retval 0 on success
310 *\retval 1 on error
311 */
312 int tape_block::init_words(){
313 word_size=raw_size/3;
314 if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
315 word_data=(unsigned short *)malloc(raw_size*sizeof(unsigned short));
316 for (int pos=0; pos<word_size; pos++)
317 word_data[pos]=combine466(raw_data+3*pos);
318 return 0;
319 }
320
321 /*!
322 *\brief Calculate and check checksum
323 *\retval 0 on success
324 *\retval 1 on error
325 */
326 int tape_block::test_checksum(){
327 unsigned short sum=0;
328 for (int pos=0; pos<word_size-1;pos++)
329 sum^=word_data[pos];
330 if (sum==word_data[word_size-1]) return 0;
331 else return 1;
332 }