*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
index 81efc8ce8cbecdd467d6cd561071bcf5e0b459a4..8b29bc62d1a0008257d32712143d96379c9f9198 100644 (file)
@@ -4,14 +4,80 @@
 
 #include "tape_block.hh"
 #include "data_block.hh"
+#include "data_block_0.hh"
 
 #include "silent_code.hh"
 #include "hw_constants.hh"
 
-tape_block::tape_block()
+
+tape_block * tape_block::gen_from_fd(int  fd,
+                                    void(*input_start)(void *),
+                                    void (*input_stop)(void *),
+                                    void * start_stop_arg)
 {
+  tape_block * res_block;
+  res_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
+  data_block   * d_block;
+  data_block_0 * d0_block;
+  
+  // Retype to data_block if possible
+  switch(res_block->get_type()){
+  case tape_block::TBT_DATA:    // Make pointer a data block
+    d_block=new data_block(*res_block);
+    delete res_block;
+    res_block=d_block;
+    break;
+  default:          // All other cases
+    return res_block;
+  }
+  
+  // Retype again, if it's a data_block now
+  
+  switch(res_block->get_type()){
+  case 00: 
+    d0_block=new data_block_0(*d_block); 
+    d_block=d0_block;
+    break;
+//   case 01: d_block=new data_block_1(*d_block); break;
+//   case 02: d_block=new data_block_2(*d_block); break;
+//   case 03: d_block=new data_block_3(*d_block); break;
+//   case 04: d_block=new data_block_4(*d_block); break;
+//   case 05: d_block=new data_block_5(*d_block); break;
+//   case 06: d_block=new data_block_6(*d_block); break;
+//   case 07: d_block=new data_block_7(*d_block); break;
+  default:
+    return res_block;
+  }
+  delete res_block;
+  res_block=d_block;
+  
+  if (res_block->get_type()==0) switch(d0_block->get_subtype()){
+//   case 000: d0_block=new data_block_0_00(d0_block); break;
+//   case 001: d0_block=new data_block_0_01(d0_block); break;
+//   case 002: d0_block=new data_block_0_02(d0_block); break;
+//   case 003: d0_block=new data_block_0_03(d0_block); break;
+//   case 004: d0_block=new data_block_0_04(d0_block); break;
+//   case 010: d0_block=new data_block_0_10(d0_block); break;
+//   case 014: d0_block=new data_block_0_14(d0_block); break;
+//   case 024: d0_block=new data_block_0_24(d0_block); break;
+//   case 030: d0_block=new data_block_0_30(d0_block); break;
+//   case 054: d0_block=new data_block_0_54(d0_block); break;
+//   case 060: d0_block=new data_block_0_60(d0_block); break;
+  default:
+    return res_block;
+  }
+  delete res_block;
+  res_block=d0_block;
+  return res_block;
 }
 
+/*!
+ *\brief Default constructor.
+ */
+tape_block::tape_block(){}
+
+
 tape_block::tape_block(tape_block & org){
   block_type=org.block_type;
   init_state=org.init_state;
@@ -30,6 +96,26 @@ tape_block::tape_block(tape_block & org){
    }
 }
 
+/*!
+ *\brief Automatic constructor with file descriptor support
+ *
+ * This constructor is used to initialise the block read in the block's 
+ * data via a file descriptor.\n
+ * This is done in the following way:
+ * - input_start() is called.
+ * - Data is read from fd. Stops on end of file or end of block
+ * or IO error. On EOF or IO error input_stop() is NOT called.
+ * - If there was no IO error the block's checksum is calculated 
+ * and checked.
+ * - The block's type field is initialised.
+ * - input_stop() is called.
+ *
+ *\param fd A file descriptor where the read is taken from 
+ *\param input_stop A pointer to a function called at the end of input
+ *\param input_start A pointer to a function called at the beginning 
+ * of input
+ *\param start_stop_arg A pointer passed to input_start and input_stop(). 
+ */
 tape_block::tape_block (int  fd,
                        void(*input_start)(void *),
                        void (*input_stop)(void *),
@@ -150,41 +236,79 @@ tape_block::tape_block (int  fd,
   init_state=TBS_OK;
 }
 
+/*!
+ *\brief Default destructor.
+ */
 tape_block::~tape_block(){
   free(raw_data);
   free(word_data);
 }
 
+/*!
+ *\brief Query the block's state.
+ *
+ * If the state is not TBS_OK, the block must be discarded.
+ *\return The block's initialisation state
+ */
 tape_block::tb_state_t tape_block::get_state(){
   return init_state;
 }
 
+
+/*!
+ *\brief Get size of the internal raw data buffer
+ *\return The size of the internal raw data buffer
+ */
 int tape_block::get_raw_size(){
   return raw_size;
 }
 
+/*!
+ *\brief Access internal raw data buffer
+ *
+ * The raw data buffer contains all block data except the \    \
+ * block start delimiter and block end delimiter.
+ *\return Pointer to internal raw data buffer of the block
+ */
 unsigned char * tape_block::get_raw_data(){
   return raw_data;
 }
 
+/*!
+ *\brief Determine the block's type
+ *
+ * This routine returns the block's type.\\
+ *\retval TBT_DATA if it's a data block
+ *\retval TBT_EOT if it's an end of tape mark
+ *\retval TBR_DISCARD if the block is unusable
+ *
+ *\note This function is virtual. That means that if the tape block in
+ * question is of the subtype data_block which overwrites this method,
+ * it will return the block type contained in the upper 4 bits of the
+ * first data word! Try to avoid testing for TBT_DATA. \n
+ * Blocks generated by gen_from_fd should never have TBT_DATA.
+ */
 int tape_block::get_type(){
-  int result=block_type;
-  if (block_type==TBT_DATA){
-    data_block * mb=new data_block(*this);
-    result=mb->get_type();
-  }
-  return result;
+  return block_type;
 }
 
+/*!
+ *\brief Determine the block's subtype
+ *
+ * Some data block types have a subtype.
+ *\note This is to be used in derived classes.
+ *\return Always 0
+ */
 int tape_block::get_subtype(){
-  int result=0;
-  if (block_type==TBT_DATA){
-    data_block * mb=new data_block(*this);
-    result=mb->get_subtype();
-  }
-  return result;
+  return 0;
 }
 
+
+/*!
+ *\brief Initialize word representation of data
+ *\retval 0 on success
+ *\retval 1 on error
+ */
 int tape_block::init_words(){
   word_size=raw_size/3;
   if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
@@ -194,13 +318,15 @@ int tape_block::init_words(){
   return 0;
 }
 
+/*!
+ *\brief Calculate and check checksum
+ *\retval 0 on success
+ *\retval 1 on error
+ */
 int tape_block::test_checksum(){
   unsigned short sum=0;
   for (int pos=0; pos<word_size-1;pos++)
-    //    sum^=word_data[pos];
-    sum+=word_data[pos];
-    
-  //  printf("cs:%04x\n",word_data[word_size-1]);
+    sum^=word_data[pos];
   if (sum==word_data[word_size-1]) return 0;
   else return 1;
 }