home *** CD-ROM | disk | FTP | other *** search
- --------------------------------------------------------------------------
- F i l e I n f o r m a t i o n
-
- * DESCRIPTION
- Used in Turbo programs which read files created by BASIC programs, and
- rewrites them in Turbo Pascal format.
- Requires: Turbo Pascal 2.0, 3.0. Author: Rick Solomon. Version T1.0
-
- * ASSOCIATED FILES
- DECODE.PAS
- DECODE88.PAS
- DECODE.TXT
-
- ==========================================================================
- }
- {.PL65}
- {The following function (DECODE) converts REAL numbers stored
- by a BASIC program in a random access file (i.e. in ASCII
- format) and read by a Pascal program into a number which
- can be used by Pascal. BASIC stores a REAL (single precision)
- number as a four byte vector starting with the least
- significant byte in the first position to the most significant
- byte in third position with the exponent in fourth. TURBO
- Pascal uses a six byte format starting with the exponent in
- position 0 then least to most significant bytes in positions
- one through five respectively.
-
- Since BASIC and TURBO use the same encoding formula, an
- alternative method would be to place the bytes read from
- the BASIC file directly into the appropriate locations
- in a REAL variable using absolute addressing. This option
- is not available to TURBO-87/TURBOBCD users, however, since
- TURBO-87/TURBOBCD use an eight/ten byte REALs with entirely
- different encoding.
-
- DECODE uses the following formula:
-
- ((MSB*2^16 + MID*2^8 + LSB)/2^24) * 2^(EXP-128)
-
- MSB : Most significant byte;
- LSB : Least significant byte;
- MID : Middle byte;
- EXP : Exponent.
-
- A global Type Short_Vector = Array[1..4] of Byte must be
- declared in the calling routine and used To pass the values
- to the Function.}
-
- Function Decode(Basic_Real:Short_VecTor):Real;
- Var
- Zero : Boolean;
-
- Function Work_It_Out(Basic_No:Short_VecTor):Real;
- {Computes all non Zero Reals}
- Const
- Two8 = 256.0; {2^8}
- Two16 = 65536.0; {2^16}
- Two24 = 16777216.0; {2^24}
- Type
- Sign_Type = (Positive,Negative);
- Var
- Temp : Real;
- Sign : Sign_Type;
- I : Integer;
- Msb, Lsb, Mid, Exp : Byte;
- Begin
- Sign:= Positive;
- Exp:= Basic_No[4];
- Msb:= Basic_No[3];
- Mid:= Basic_No[2];
- Lsb:= Basic_No[1];
- { The Sign of the number is sTored in bit 7 of Msb. 0 is
- Positive, 1 is Negative. This bit must be checked prior
- To calculation, and switched To 1 if it is Zero. }
- IF Msb < 128 {i.e. if bit 7 = 0}
- Then Msb := Msb + 128 {put a 1 in bit 7}
- Else Sign := Negative;
- Temp := ((Msb*Two16) + (Mid*Two8) + Lsb)/(Two24);
- IF Exp > 128 Then For I:= 129 To Exp do Temp:= Temp * 2;
- IF Exp < 128 Then For I:= Exp To 127 do Temp:= Temp/2;
- Case Sign Of
- Positive : Work_It_Out := Temp;
- Negative : Work_It_Out := Temp * -1;
- End; {Case}
- End; {Function Work_It_Out}
-
- Begin {Function Decode}
- {Check For Zero Exponent. If the Exponent is 0 the num is 0 by definition}
- Zero := (Basic_Real[4] = 0);
- Case Zero Of
- True : Decode := 0;
- False : Decode := Work_It_Out(Basic_Real);
- End; {Case}
- End; {Function Decode}
-