5 #include "tape_block.hh"
6 #include "data_block.hh"
7 #include "data_block_0.hh"
9 #include "silent_code.hh"
10 #include "hw_constants.hh"
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
)
18 tape_block
* res_block
;
19 res_block
=new tape_block(fd
,input_start
,input_stop
,start_stop_arg
);
21 data_block_0
* d0_block
;
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
);
30 default: // All other cases
34 // Retype again, if it's a data_block now
36 switch(res_block
->get_type()){
38 d0_block
=new data_block_0(*d_block
);
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;
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;
76 *\brief Default constructor.
78 tape_block::tape_block(){}
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
;
86 raw_size
=org
.raw_size
;
88 word_size
=org
.word_size
;
90 raw_data
=(unsigned char *)malloc(raw_size
);
91 memcpy(raw_data
,org
.raw_data
,raw_size
);
94 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
95 memcpy(word_data
,org
.word_data
,word_size
*sizeof(unsigned short));
100 *\brief Automatic constructor with file descriptor support
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
110 * - The block's type field is initialised.
111 * - input_stop() is called.
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
117 *\param start_stop_arg A pointer passed to input_start and input_stop().
119 tape_block::tape_block (int fd
,
120 void(*input_start
)(void *),
121 void (*input_stop
)(void *),
122 void * start_stop_arg
125 unsigned char buffer
;
127 int eot_needed
=EOT_LENGTH
;
128 int eob_needed
=EOB_LENGTH
;
129 int block_complete
=0;
131 block_type
= TBT_DISCARD
;
132 init_state
= TBS_DEFAULT
;
139 if (input_start
) input_start (start_stop_arg
);
141 /* Look for block start in input stream */
143 retval
=read(fd
, &buffer
, 1);
144 if (retval
== 0){ // End of file
145 block_type
=TBT_DISCARD
;
146 init_state
=TBS_EOF_LEGAL
;
149 if (retval
== -1){ // Error reading from fd
150 block_type
=TBT_DISCARD
;
151 init_state
=TBS_IOERR
;
156 /* Look for end ot tape sequence */
159 if (buffer
==EOT_SEQ_1
) eot_needed
--;
160 else eot_needed
=EOT_LENGTH
;
163 if (buffer
==EOT_SEQ_2
) eot_needed
--;
164 else eot_needed
=EOT_LENGTH
;
167 if (buffer
==EOT_SEQ_3
){
168 raw_data
=(unsigned char*)malloc(3);
170 raw_data
[0]=EOT_SEQ_1
;
171 raw_data
[1]=EOT_SEQ_2
;
172 raw_data
[2]=EOT_SEQ_3
;
177 else eot_needed
=EOT_LENGTH
;
180 } while (buffer
!= (unsigned char) BLOCK_START_DELIMITER
);
183 /* Now input the block data */
186 retval
= read (fd
, &buffer
, 1);
188 if (retval
== 0){ // End of file
189 block_type
=TBT_DISCARD
;
190 init_state
=TBS_EOF_ILLEGAL
;
193 if (retval
== -1){ // Error reading from fd
194 block_type
=TBT_DISCARD
;
195 init_state
=TBS_IOERR
;
198 data_read
++; // We have consumed a byte
200 /* sort in the new byte */
202 raw_data
=(unsigned char*)realloc(raw_data
,raw_size
);
203 raw_data
[raw_size
-1]=buffer
;
205 /* Look for end ot block sequence */
208 if (buffer
==BLOCK_END_DELIMITER_1
) eob_needed
--;
209 else eob_needed
=EOB_LENGTH
;
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
);
217 else eob_needed
=EOB_LENGTH
;
220 }while (!block_complete
);
221 if (input_stop
) input_stop (start_stop_arg
);
223 /* Now we have the data in. */
225 if (init_words()!=0){
226 init_state
=TBS_CHECKSUM
;
230 if (test_checksum()!=0){
231 init_state
=TBS_CHECKSUM
;
240 *\brief Default destructor.
242 tape_block::~tape_block(){
248 *\brief Query the block's state.
250 * If the state is not TBS_OK, the block must be discarded.
251 *\return The block's initialisation state
253 tape_block::tb_state_t
tape_block::get_state(){
259 *\brief Get size of the internal raw data buffer
260 *\return The size of the internal raw data buffer
262 int tape_block::get_raw_size(){
267 *\brief Access internal raw data buffer
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
273 unsigned char * tape_block::get_raw_data(){
278 *\brief Determine the block's type
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
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.
291 int tape_block::get_type(){
296 *\brief Determine the block's subtype
298 * Some data block types have a subtype.
299 *\note This is to be used in derived classes.
302 int tape_block::get_subtype(){
308 *\brief Initialize word representation of data
309 *\retval 0 on success
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
);
322 *\brief Calculate and check checksum
323 *\retval 0 on success
326 int tape_block::test_checksum(){
327 unsigned short sum
=0;
328 for (int pos
=0; pos
<word_size
-1;pos
++)
330 if (sum
==word_data
[word_size
-1]) return 0;