5 #include "tape_block.hh"
6 #include "data_block.hh"
8 #include "silent_code.hh"
9 #include "hw_constants.hh"
11 tape_block::tape_block()
15 tape_block::tape_block(tape_block
& org
){
16 block_type
=org
.block_type
;
17 init_state
=org
.init_state
;
18 data_read
=org
.data_read
;
20 raw_size
=org
.raw_size
;
22 word_size
=org
.word_size
;
24 raw_data
=(unsigned char *)malloc(raw_size
);
25 memcpy(raw_data
,org
.raw_data
,raw_size
);
28 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
29 memcpy(word_data
,org
.word_data
,word_size
*sizeof(unsigned short));
33 tape_block::tape_block (int fd
,
34 void(*input_start
)(void *),
35 void (*input_stop
)(void *),
41 int eot_needed
=EOT_LENGTH
;
42 int eob_needed
=EOB_LENGTH
;
45 block_type
= TBT_DISCARD
;
46 init_state
= TBS_DEFAULT
;
53 if (input_start
) input_start (start_stop_arg
);
55 /* Look for block start in input stream */
57 retval
=read(fd
, &buffer
, 1);
58 if (retval
== 0){ // End of file
59 block_type
=TBT_DISCARD
;
60 init_state
=TBS_EOF_LEGAL
;
63 if (retval
== -1){ // Error reading from fd
64 block_type
=TBT_DISCARD
;
70 /* Look for end ot tape sequence */
73 if (buffer
==EOT_SEQ_1
) eot_needed
--;
74 else eot_needed
=EOT_LENGTH
;
77 if (buffer
==EOT_SEQ_2
) eot_needed
--;
78 else eot_needed
=EOT_LENGTH
;
81 if (buffer
==EOT_SEQ_3
){
82 raw_data
=(unsigned char*)malloc(3);
84 raw_data
[0]=EOT_SEQ_1
;
85 raw_data
[1]=EOT_SEQ_2
;
86 raw_data
[2]=EOT_SEQ_3
;
91 else eot_needed
=EOT_LENGTH
;
94 } while (buffer
!= (unsigned char) BLOCK_START_DELIMITER
);
97 /* Now input the block data */
100 retval
= read (fd
, &buffer
, 1);
102 if (retval
== 0){ // End of file
103 block_type
=TBT_DISCARD
;
104 init_state
=TBS_EOF_ILLEGAL
;
107 if (retval
== -1){ // Error reading from fd
108 block_type
=TBT_DISCARD
;
109 init_state
=TBS_IOERR
;
112 data_read
++; // We have consumed a byte
114 /* sort in the new byte */
116 raw_data
=(unsigned char*)realloc(raw_data
,raw_size
);
117 raw_data
[raw_size
-1]=buffer
;
119 /* Look for end ot block sequence */
122 if (buffer
==BLOCK_END_DELIMITER_1
) eob_needed
--;
123 else eob_needed
=EOB_LENGTH
;
126 if (buffer
==BLOCK_END_DELIMITER_2
){
127 raw_size
-=EOB_LENGTH
; // Don't want the delimiter(s) in the data!
128 raw_data
=(unsigned char*)realloc(raw_data
,raw_size
);
131 else eob_needed
=EOB_LENGTH
;
134 }while (!block_complete
);
135 if (input_stop
) input_stop (start_stop_arg
);
137 /* Now we have the data in. */
139 if (init_words()!=0){
140 init_state
=TBS_CHECKSUM
;
144 if (test_checksum()!=0){
145 init_state
=TBS_CHECKSUM
;
153 tape_block::~tape_block(){
158 tape_block::tb_state_t
tape_block::get_state(){
162 int tape_block::get_raw_size(){
166 unsigned char * tape_block::get_raw_data(){
170 int tape_block::get_type(){
171 int result
=block_type
;
172 if (block_type
==TBT_DATA
){
173 data_block
* mb
=new data_block(*this);
174 result
=mb
->get_type();
179 int tape_block::get_subtype(){
181 if (block_type
==TBT_DATA
){
182 data_block
* mb
=new data_block(*this);
183 result
=mb
->get_subtype();
188 int tape_block::init_words(){
189 word_size
=raw_size
/3;
190 if (word_size
<MINIMUM_DATA_BLOCK_WORDSIZE
) return 1; // Block too short!
191 word_data
=(unsigned short *)malloc(raw_size
*sizeof(unsigned short));
192 for (int pos
=0; pos
<word_size
; pos
++)
193 word_data
[pos
]=combine466(raw_data
+3*pos
);
197 int tape_block::test_checksum(){
198 unsigned short sum
=0;
199 for (int pos
=0; pos
<word_size
-1;pos
++)
200 // sum^=word_data[pos];
203 // printf("cs:%04x\n",word_data[word_size-1]);
204 if (sum
==word_data
[word_size
-1]) return 0;