comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@attbi.com>
Subject: Re: Type-safe low-level programming?
Date: Tue, 18 Feb 2003 14:53:56 GMT
Date: 2003-02-18T14:53:56+00:00	[thread overview]
Message-ID: <8Or4a.165295$vm2.120896@rwcrnsc54> (raw)
In-Reply-To: cbdd91ae.0302171606.57d2ba77@posting.google.com

Here is one way to get there:

  TYPE Bit_Number IS RANGE 0 .. 7;
  TYPE Address_Type IS RANGE 0 .. 16#ff#;

  TYPE Reg_A_Bit_Number IS NEW Bit_Number;
  TYPE Reg_B_Bit_Number IS NEW Bit_Number;
  TYPE Reg_Z_Bit_Number IS NEW Bit_Number;

  TYPE Reg_A_Address_Type IS NEW Address_Type;
  TYPE Reg_B_Address_Type IS NEW Address_Type;
  TYPE Reg_Z_Address_Type IS NEW Address_Type;

  Register_A : CONSTANT Reg_A_Address_Type := 1;
  Bit_A1 : CONSTANT Reg_A_Bit_Number := 4; --Register A, Bit 4
  Bit_A2 : CONSTANT Reg_A_Bit_Number := 1;
  Bit_A3 : CONSTANT Reg_A_Bit_Number := 7;

  Register_B : CONSTANT Reg_B_Address_Type := 2;
  Bit_B1 : CONSTANT Reg_B_Bit_Number :=4;
  Bit_B2 : CONSTANT Reg_B_Bit_Number :=2;
  Bit_B3 : CONSTANT Reg_B_Bit_Number :=5;

  Register_Z : CONSTANT Reg_Z_Address_Type := 2;
  Bit_Z1 : CONSTANT Reg_Z_Bit_Number :=6;
  Bit_Z2 : CONSTANT Reg_Z_Bit_Number :=0;
  Bit_Z3 : CONSTANT Reg_Z_Bit_Number :=5;

  PROCEDURE Set_Bit_Exp( Adr : IN Address_Type; Nr : IN Bit_Number );
  PRAGMA Export( Ada, Set_Bit_Exp, "Set_Bit_Exp" );

  PROCEDURE Clear_Bit_Exp( Adr : IN Address_Type; Nr : IN Bit_Number );
  PRAGMA Export( Ada, Clear_Bit_Exp, "Clear_Bit_Exp" );

  PROCEDURE Set_Bit( Adr : IN Reg_A_Address_Type; Nr : IN
Reg_A_Bit_Number );
  PROCEDURE Set_Bit( Adr : IN Reg_B_Address_Type; Nr : IN
Reg_B_Bit_Number );
  PROCEDURE Set_Bit( Adr : IN Reg_Z_Address_Type; Nr : IN
Reg_Z_Bit_Number );
  PRAGMA Import( Ada, Set_Bit, "Set_Bit_Exp" );

  PROCEDURE Clear_Bit( Adr : IN Reg_A_Address_Type; Nr : IN
Reg_A_Bit_Number );
  PROCEDURE Clear_Bit( Adr : IN Reg_B_Address_Type; Nr : IN
Reg_B_Bit_Number );
  PROCEDURE Clear_Bit( Adr : IN Reg_Z_Address_Type; Nr : IN
Reg_Z_Bit_Number );
  PRAGMA Import( Ada, Clear_Bit, "Clear_Bit_Exp" );

Using the export and import clauses only one instance of Set_Bit_Exp and
Clear_Bit_Exp are created.  This is a bit of a "trick" to have type checking
while not having type checking (if you know what I mean).

You could also try defining the different types as shown as well as separate
instances of the routines, only define them all as in-line.  I suspect the
complexity of your Set_Bit and Clear_Bit functions would be so simple that
in-line code would not increase the size.

You could also try creating the routines as generic, with a different
instance for each type.  Some compilers would share code.

Steve
(The Duck)

"Bernd Trog" <berndtrog@yahoo.com> wrote in message
news:cbdd91ae.0302171606.57d2ba77@posting.google.com...
> Hi,
> lets say I've something like this:
>
> pragma No_Run_Time;
[snip]

> On a not-so-good day I wrote:
> Set_Bit( Register_A, Bit_B3 );
> and found my error days later :-(
>
> Now I wonder, if its possible to make 'Set_Bit' type-safe *without*
> increasing the program memory size for every new register?
>
>
> Bernd





      parent reply	other threads:[~2003-02-18 14:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-18  0:06 Type-safe low-level programming? Bernd Trog
2003-02-18  2:04 ` David C. Hoos, Sr.
2003-02-18 10:39 ` Hans-Bernhard Broeker
2003-02-18 16:21   ` Thad Smith
2003-02-18 16:52   ` Hans-Bernhard Broeker
2003-02-18 14:53 ` Steve [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox