[comp.lang.pascal] Need BCD routines for TP4.0/TP5.0/TP5.5

schrader@loligo (Dave Schrader) (10/02/89)

This is my first post to the *net*. I have read, with interest, but had no
need to post until now. Please forgive the length of this posting.
  
A project (personal) I am working on (which grew out of an earlier posting)
requires that I read/write the system real clock. I have written all of the
appropriate routines (see below for basic structure), but the problem is on
a READ it returns the values in BCD and on a WRITE is requires that they be
passed BCD values.
  
I have looked, in vain, for that information in the TurboPascal 4.0, 5.0,
and 5.5 manuals and examples. They indicate that BCD support was phased out
after TP 3.0. The documentation and the examples are some what confusing and
seem, to me, contradictory. Does anyone have the source to some routines that
will take a number (0..23,0..59,1..100) and convert it to BCD and one that
will take the BCD representation of a number (same ranges) and convert it to
a number (type integer in both cases...). I DO need source code.
  
Any help will be appreciated.
  
   PROGRAM xyz ;
   CONST   Read_Real_Time_Clock  = hex_value;   {memory fails, notes at home}
           Rite_Real_Time_Clock  = hex_value;   {memory fails, notes at home}
           Timer_Interupt_Value  = hex_value;   {memory fails, notes at home}
   USES    dos,crt;
   VAR     dos_regs : Registers;
           HH       : INTEGER; {In the range of 0..23}
           MM       : INTEGER; {In the range of 0..59}
           SS       : INTEGER; {In the range of 0..59}

   { FUNCTION BCD_to_Int and FUNCTION Int_to_BCD} will go here eventually }

   PROCEDURE Read_RT_Clock(VAR HH , MM , SS : INTEGER) ;
   BEGIN
      dos_regs.AH := Read_Real_Time_Clock;
      INTR(dos_regs,Timer_Interupt_Value);
      HH := BCD_to_Int(dos_regs.CH)      ; {This is what I need }
      : 
   END;
   PROCEDURE Rite_RT_Clock(    HH , MM , SS : INTEGER) ;
   BEGIN
      dos_regs.CH := Int_to_BCD(HH)      ; {This is what I need }
      : 
      dos_regs.AH := Rite_Real_Time_Clock;
      INTR(dos_regs,Timer_Interupt_Value);
   END;
  
Thanks (again),
  
David Schrader

d88-eli@nada.kth.se (Erik Liljencrantz) (10/03/89)

Converting to and from BCD is fairly easy (and this hasn't got anything to do
with the BCD support previously found in Turbo 3.0):

  FUNCTION BCD_to_Int(BCD:BYTE):BYTE;
  { Convert two digit BCD to 0..99 integer }
  BEGIN
    BCD_to_Int:=(BCD AND 15)+(BCD SHR 4)*10;
  END;

  FUNCTION Int_to_BCD(I:BYTE):BYTE;
  { Convert integer (0..99) into BCD }
  BEGIN
    Int_to_BCD:=(I MOD 10)+(I DIV 10)*16;
  END;

Hope this helps...
-- 
Erik Liljencrantz     | "No silly quotes!!"
d88-eli@nada.kth.se   |  Embraquel D. Tuta

ts@chyde.uwasa.fi (Timo Salmi LASK) (10/03/89)

In article <234@vsserv.scri.fsu.edu> schrader@loligo.UUCP (David Schrader) writes:
>a READ it returns the values in BCD and on a WRITE is requires that they be
>passed BCD values.

>I have looked, in vain, for that information in the TurboPascal 4.0, 5.0,
>and 5.5 manuals and examples. They indicate that BCD support was phased out
>after TP 3.0. The documentation and the examples are some what confusing and

Haven't tried them, but Turbo Professional 5.0 seems to have BCD
arithmetics for Turbo Pascal 4.0 and 5.0. 

...................................................................
Prof. Timo Salmi                                (Site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: vakk::salmi Bitnet: salmi@finfun