6 #include "tape_block.hh"
7 #include "data_block.hh"
8 #include "data_block_0.hh"
9 #include "data_block_0_0.hh"
10 #include "data_block_0_1.hh"
11 #include "data_block_0_2.hh"
12 #include "data_block_0_3.hh"
13 #include "data_block_0_4.hh"
14 #include "data_block_0_10.hh"
15 #include "data_block_0_14.hh"
16 #include "data_block_0_24.hh"
17 #include "data_block_0_44.hh"
18 #include "data_block_0_30.hh"
19 #include "data_block_0_54.hh"
20 #include "data_block_0_60.hh"
21 #include "data_block_0_50.hh"
22 #include "data_block_0_64.hh"
23 #include "data_block_1.hh"
24 #include "data_block_2.hh"
25 #include "data_block_3.hh"
26 #include "data_block_4.hh"
27 #include "data_block_5.hh"
28 #include "data_block_6.hh"
29 #include "data_block_7.hh"
31 #include "eot_block.hh"
32 #include "discard_block.hh"
33 #include "silent_code.hh"
34 #include "hw_constants.hh"
38 /***************************************************************/
42 tape_block::~tape_block(){
47 /***************************************************************/
49 *\brief Copy constructor.
51 tape_block::tape_block(tape_block
& org
){
55 /***************************************************************/
57 *\brief Assignment operator.
59 void tape_block::operator=(tape_block
&org
){
60 block_type
=org
.block_type
;
61 init_state
=org
.init_state
;
62 data_read
=org
.data_read
;
64 raw_size
=org
.raw_size
;
66 word_size
=org
.word_size
;
68 raw_data
=(unsigned char *)malloc(raw_size
);
69 memcpy(raw_data
,org
.raw_data
,raw_size
);
72 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
73 memcpy(word_data
,org
.word_data
,word_size
*sizeof(unsigned short));
77 /***************************************************************/
79 *\brief Query the block's state.
81 * If the state is not TBS_OK, the block must be discarded.
82 *\return The block's initialisation state
84 tape_block::tb_state_t
tape_block::get_state(){
88 /***************************************************************/
90 *\brief Determine the block's type
92 * This routine returns the block's type.\\
93 *\retval TBT_DATA if it's a data block
94 *\retval TBT_EOT if it's an end of tape mark
95 *\retval TBR_DISCARD if the block is unusable
97 *\note This function is virtual. That means that if the tape block in
98 * question is of the subtype data_block which overwrites this method,
99 * it will return the block type contained in the upper 4 bits of the
100 * first data word! Try to avoid testing for TBT_DATA. \n
101 * Blocks generated by gen_from_fd should never have TBT_DATA.
103 int tape_block::get_type(){
107 /***************************************************************/
109 *\brief Determine the block's subtype
111 * Some data block types have a subtype.
112 *\note This is a virtual method and to be used in derived classes.
115 int tape_block::get_subtype(){
119 /***************************************************************/
121 *\brief Get a cleartext description of the block.
122 *\return ldc style descriptive line for the block
124 vector
<string
> tape_block:: get_description(){
125 vector
<string
> result
;
126 result
.insert(result
.end(),
127 "***** "+get_typestring()+"Untyped tape block");
131 /***************************************************************/
133 *\brief Get size of the internal raw data buffer
134 *\return The size of the internal raw data buffer
136 int tape_block::get_raw_size(){
140 /***************************************************************/
142 *\brief Access internal raw data buffer
144 * The raw data buffer contains all block data except the \ \
145 * block start delimiter and block end delimiter.
146 *\return Pointer to internal raw data buffer of the block
148 unsigned char * tape_block::get_raw_data(){
152 /***************************************************************/
154 *\brief Static factory method with file descriptor support.
156 * This method creates a tape_block by using the private
157 * constructor with file descriptor support.\n
158 * It always returns a valid pointer, errors
159 * or exceptional conditions during creation are marked in the
161 * The factory method tries to determine the block's type and then
162 * looks for appropriate subclasses. If there's an appropriate
163 * subclass fitting the newly-created object, an
164 * object of the subclass will be created and a pointer to this
165 * object will be returned to the caller.\n
166 * This allows for easy use of the object using it's virtual
167 * interface methods or a cast of the pointer to a sub type pointer
168 * according to the type information, and then using the subtype's
169 * methods not already present in tape_block.\n
170 * Objects of the following subclasses are automagically generated:\n
196 *\return Pointer to an object of type tape_block or a subclass.
197 *\param fd A file descriptor where the read is taken from.
198 *\param input_start A pointer to a function
199 * called before reading data from fd.
200 *\param input_stop A pointer to a function
201 * called after reading data from fd.
203 *\param start_stop_arg A pointer passed to input_start
204 * and input_stop(). Can be anything.
206 tape_block
* tape_block::gen_from_fd(int fd
,
207 void(*input_start
)(void *),
208 void (*input_stop
)(void *),
209 void * start_stop_arg
)
211 tape_block
* n_tape_block
=0;
212 data_block
* n_data_block
=0;
213 data_block
* n_data_block_x
=0;
214 data_block_0
* n_data_block_0
=0;
215 data_block_0
* n_data_block_0_x
=0;
216 eot_block
* n_eot_block
=0;
217 discard_block
* n_discard_block
=0;
219 n_tape_block
=new tape_block(fd
,input_start
,input_stop
,start_stop_arg
);
221 // Retype to data_block, eot_block, discard_block - if possible
222 switch(n_tape_block
->get_type()){
223 case tape_block::TBT_DATA
:
224 n_data_block
= new data_block(*n_tape_block
);
226 case tape_block::TBT_EOT
:
227 n_eot_block
=new eot_block(*n_tape_block
);
230 case tape_block::TBT_DISCARD
:
231 n_discard_block
= new discard_block(*n_tape_block
);
233 return n_discard_block
;
239 // Now only data blocks are left
241 switch(n_data_block
->get_type()){
242 case 00: n_data_block_0
=new data_block_0(*n_data_block
); break;
243 case 01: n_data_block_x
=new data_block_1(*n_data_block
); break;
244 case 02: n_data_block_x
=new data_block_2(*n_data_block
); break;
245 case 03: n_data_block_x
=new data_block_3(*n_data_block
); break;
246 case 04: n_data_block_x
=new data_block_4(*n_data_block
); break;
247 case 05: n_data_block_x
=new data_block_5(*n_data_block
); break;
248 case 06: n_data_block_x
=new data_block_6(*n_data_block
); break;
249 case 07: n_data_block_x
=new data_block_7(*n_data_block
); break;
253 if (n_data_block_0
==0){ // So we must have found another one
255 return n_data_block_x
;
258 // Here only type 0 left
260 switch(n_data_block_0
->get_subtype()){
261 case 000: n_data_block_0_x
=new data_block_0_0(*n_data_block_0
); break;
262 case 001: n_data_block_0_x
=new data_block_0_1(*n_data_block_0
); break;
263 case 002: n_data_block_0_x
=new data_block_0_2(*n_data_block_0
); break;
264 case 003: n_data_block_0_x
=new data_block_0_3(*n_data_block_0
); break;
265 case 004: n_data_block_0_x
=new data_block_0_4(*n_data_block_0
); break;
266 case 010: n_data_block_0_x
=new data_block_0_10(*n_data_block_0
); break;
267 case 014: n_data_block_0_x
=new data_block_0_14(*n_data_block_0
); break;
268 case 024: n_data_block_0_x
=new data_block_0_24(*n_data_block_0
); break;
269 case 044: n_data_block_0_x
=new data_block_0_44(*n_data_block_0
); break;
270 case 030: n_data_block_0_x
=new data_block_0_30(*n_data_block_0
); break;
271 case 054: n_data_block_0_x
=new data_block_0_54(*n_data_block_0
); break;
272 case 060: n_data_block_0_x
=new data_block_0_60(*n_data_block_0
); break;
273 case 064: n_data_block_0_x
=new data_block_0_64(*n_data_block_0
); break;
274 case 050: n_data_block_0_x
=new data_block_0_50(*n_data_block_0
); break;
276 return n_data_block_0
;
278 delete n_data_block_0
;
279 return n_data_block_0_x
;
282 /***************************************************************/
284 *\brief Default constructor.
286 tape_block::tape_block(){}
288 /***************************************************************/
290 *\brief get a short type description string.
291 *\return A small string to be used in description methods.
293 * The string is always 19 characters long.\n
294 * An example is "(99-99) ".
296 string
tape_block::get_typestring(){
298 sprintf(buffer
,"(%o-%o) ",get_type(),get_subtype());
300 return string(buffer
);
303 /***************************************************************/
305 *\brief Automatic constructor with file descriptor support
307 * This constructor is used to initialise the block read in the block's
308 * data via a file descriptor.\n
309 * This is done in the following way:
310 * - input_start() is called.
311 * - Data is read from fd. Stops on end of file or end of block
312 * or IO error. On EOF or IO error input_stop() is NOT called.
313 * - If there was no IO error the block's checksum is calculated
315 * - The block's type field is initialised.
316 * - input_stop() is called.
318 *\param fd A file descriptor where the read is taken from
319 *\param input_stop A pointer to a function called at the end of input
320 *\param input_start A pointer to a function called at the beginning
322 *\param start_stop_arg A pointer passed to input_start and input_stop().
324 tape_block::tape_block (int fd
,
325 void(*input_start
)(void *),
326 void (*input_stop
)(void *),
327 void * start_stop_arg
330 unsigned char buffer
;
332 int eot_needed
=EOT_LENGTH
;
333 int eob_needed
=EOB_LENGTH
;
334 int block_complete
=0;
336 block_type
= TBT_DISCARD
;
337 init_state
= TBS_DEFAULT
;
344 if (input_start
) input_start (start_stop_arg
);
346 /* Look for block start in input stream */
348 retval
=read(fd
, &buffer
, 1);
349 if (retval
== 0){ // End of file
350 block_type
=TBT_DISCARD
;
351 init_state
=TBS_EOF_LEGAL
;
354 if (retval
== -1){ // Error reading from fd
355 block_type
=TBT_DISCARD
;
356 init_state
=TBS_IOERR
;
361 /* Look for end ot tape sequence */
364 if (buffer
==EOT_SEQ_1
) eot_needed
--;
365 else eot_needed
=EOT_LENGTH
;
368 if (buffer
==EOT_SEQ_2
) eot_needed
--;
369 else eot_needed
=EOT_LENGTH
;
372 if (buffer
==EOT_SEQ_3
){
373 raw_data
=(unsigned char*)malloc(3);
375 raw_data
[0]=EOT_SEQ_1
;
376 raw_data
[1]=EOT_SEQ_2
;
377 raw_data
[2]=EOT_SEQ_3
;
382 else eot_needed
=EOT_LENGTH
;
385 } while (buffer
!= (unsigned char) BLOCK_START_DELIMITER
);
388 /* Now input the block data */
391 retval
= read (fd
, &buffer
, 1);
393 if (retval
== 0){ // End of file
394 block_type
=TBT_DISCARD
;
395 init_state
=TBS_EOF_ILLEGAL
;
398 if (retval
== -1){ // Error reading from fd
399 block_type
=TBT_DISCARD
;
400 init_state
=TBS_IOERR
;
403 data_read
++; // We have consumed a byte
405 /* sort in the new byte */
407 raw_data
=(unsigned char*)realloc(raw_data
,raw_size
);
408 raw_data
[raw_size
-1]=buffer
;
410 /* Look for end ot block sequence */
413 if (buffer
==BLOCK_END_DELIMITER_1
) eob_needed
--;
414 else eob_needed
=EOB_LENGTH
;
417 if (buffer
==BLOCK_END_DELIMITER_2
){
418 raw_size
-=EOB_LENGTH
; // Don't want the delimiter(s) in the data!
419 raw_data
=(unsigned char*)realloc(raw_data
,raw_size
);
422 else eob_needed
=EOB_LENGTH
;
425 }while (!block_complete
);
426 if (input_stop
) input_stop (start_stop_arg
);
428 /* Now we have the data in. */
430 if (init_words()!=0){
431 init_state
=TBS_CHECKSUM
;
435 if (test_checksum()!=0){
436 init_state
=TBS_CHECKSUM
;
445 /***************************************************************/
447 *\brief Initialize word representation of data.
448 *\retval 0 on success.
451 int tape_block::init_words(){
452 word_size
=raw_size
/3;
453 if (word_size
<MINIMUM_DATA_BLOCK_WORDSIZE
) return 1; // Block too short!
454 word_data
=(unsigned short *)malloc(raw_size
*sizeof(unsigned short));
455 for (int pos
=0; pos
<word_size
; pos
++)
456 word_data
[pos
]=combine466(raw_data
+3*pos
);
460 /***************************************************************/
462 *\brief Calculate and check checksum.
463 *\retval 0 on success.
466 int tape_block::test_checksum(){
467 unsigned short sum
=0;
468 for (int pos
=0; pos
<word_size
-1;pos
++)
470 if (sum
==word_data
[word_size
-1]) return 0;