   Convert an HP disc image between SIMH and HPDrive formats.

   Copyright (c) 2012-2013, J. David Bryan


   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
   AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

   Except as contained in this notice, the name of the author shall not be used
   in advertising or otherwise to promote the sale, use or other dealings in
   this Software without prior written authorization from the author.


   HPDrive is a free program written by Ansgar Kueckes that uses a PC and a GPIB
   card to emulate a variety of vintage Hewlett-Packard disc and tape drives
   that interface to computers via the HP-IB.  It is available from:

     http://www.hp9845.net/9845/projects/hpdrive/

   This program converts an HP disc image from SIMH to HPDrive format or
   vice-versa.  This permits interchanging images between the two programs.

     Usage: hpconvert <disc-image-file>

   SIMH writes and reads disc images in little-endian format, regardless of the
   byte order of the host.  In addition, SIMH accesses images for the the 7905
   and 7906(H) drives in platter order (i.e., all tracks on heads 0 and 1,
   followed by all tracks on heads 2 and 3) to improve locality of access.  It
   accesses images for the 7920(H), 7925(H), and all CS/80 drives in cylinder
   order.

   HPDrive writes and reads images in big-endian format, regardless of the byte
   order of the host, and accesses all images in cylinder order.

   This program swaps each pair of bytes in the disc image.  In addition, if the
   image is precisely the size of a 7905 or 7906(H) drive (15,151,104 or
   20,201,472 bytes, respectively), the access order is restructured from
   platter to cylinder, or vice-versa.

   Note that SIMH does not currently create full-size disc images unless the
   last sector on the drive is written.  Therefore, it is recommended that new
   images be initialized using the RTE FORMT or SWTCH programs to ensure that
   the image files are of the correct size for this program and HPDrive.

   This program creates a scratch file to store the converted image.  Only when
   conversion is complete is the original file deleted and the scratch file
   renamed to the original file's name.  Running the program twice on the same
   file will return the file to its original configuration.

   To determine the format of the image, the program examines the OS signature
   at the start of the file and compares it against a list of known operating
   systems.  If the OS signature is not recognized, the current format is
   unknown.  The format must be known in order to restructure 7905 or 7906(H)
   images; if it is not, the program terminates without altering the file.
   Other images are byte-swapped unconditionally, and the format is only used to
   report the image condition.

   Signatures for these operating systems and compatible 7905/06(H) drives are
   recognized:

     - HP 1000 RTE-IVB:  7906H ICD
     - HP 1000 RTE-6/VM: 7906H ICD
     - HP 3000 MPE:      7905A/7906A MAC

   The signatures are contained in the first four words at the start of each
   disc image.  In hex representation, they are:

     - RTE-IVB  ICD: 676D 06C0 776B 0B40
     - RTE-6/VM ICD: 676D 06C0 776B 0B40
     - MPE MAC:      5359 5354 454D 2044

   These represent the start of the boot extension for RTE and the start of the
   system disc label for MPE.  The boot extension machine instructions are:

     RTE-IVB ICD
     -----------

       62000         LDA   EQU 062000B
       72000         STA   EQU 072000B
       01200         O0    EQU HSTRT-1400B

       02600 063555  HSTRT ABS LDA+HHIGH-O0   (word 1)
       02601 003300            CMA,CCE        (word 2)
       02602 073553        ABS STA+HRCNT-O0   (word 3)
       02603 005500            ERB            (word 4)

       02753 000000  HRCNT NOP

       02755 077377  NW#DS OCT 77377
       02755         HHIGH EQU NW#DS

     RTE-6/VM ICD
     ------------

             062000  LDA   EQU 062000B
             072000  STA   EQU 072000B
             000000R O0    EQU HSTRT-1400B

       01400 063555  HSTRT ABS LDA+HHIGH-O0   (word 1)
       01401 003300            CMA,CCE        (word 2)
       01402 073553        ABS STA+HRCNT-O0   (word 3)
       01403 005500            ERB            (word 4)

       01553 000000  HRCNT NOP

       01555 077377  NW#DS OCT 77377
             001555R HHIGH EQU NW#DS

   And the disc label is:

     MPE MAC and CS/80
     -----------------

       00000 051531  LABEL ASC 6,SYSTEM DISC  (word 1)
       00001 051524                           (word 2)
       00002 042515                           (word 3)
       00003 020104                           (word 4)
       00004 044523
       00005 041440

   If the signature appears in the order given, then the image is in big-endian
   (HPDrive) format.  If the signature appears in byte-reversed order, then the
   image is in little-endian (SIMH) format.
