comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
To: "comp.lang.ada mail to news gateway" <comp.lang.ada@ada.eu.org>
Subject: Re: Type-safe low-level programming?
Date: Mon, 17 Feb 2003 20:04:39 -0600
Date: 2003-02-17T20:04:39-06:00	[thread overview]
Message-ID: <mailman.12.1045533776.9948.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: cbdd91ae.0302171606.57d2ba77@posting.google.com

You could do something like this:

pragma No_Run_Time;
subtype Bit_Number is Natural range 0 .. 7;
type Bit is mod 2;
subtype Address_Type is Natural range 0 .. 16#Ff#;
subtype Bit_Index is Natural range 0 .. (Bit_Number'Last + 1) *
    (Address_Type'Last  + 1) - 1;
type Registers is array (Bit_Index) of Bit;
pragma Pack (Registers);
Storage_Unit : constant Natural := Bit_Number'Last + 1;

Register_A : constant Address_Type := 0;
Bit_A1 : constant Bit_Index := 4 + Register_A * Storage_Unit;
Bit_A2 : constant Bit_Index := 1 + Register_A * Storage_Unit;
Bit_A3 : constant Bit_Index := 7 + Register_A * Storage_Unit;

Register_B : constant Address_Type := 1;
Bit_B1 : constant Bit_Index := 4 + Register_B * Storage_Unit;
Bit_B2 : constant Bit_Index := 2 + Register_B * Storage_Unit;
Bit_B3 : constant Bit_Index := 5 + Register_B * Storage_Unit;

Register_Z : constant Address_Type := 25;
Bit_Z1 : constant Bit_Index := 6 + Register_Z * Storage_Unit;
Bit_Z2 : constant Bit_Index := 0 + Register_Z * Storage_Unit;
Bit_Z3 : constant Bit_Index := 5 + Register_Z * Storage_Unit;

procedure Set_Bit (Regs : in Registers; Idx : in Bit_Index);
procedure Clear_Bit (Regs : in Registers; Idx : in Bit_Index);

Once all of the Bit_Index constants are correctly declared, they
contain within their values the register number as well.

This also simplifies the Set and Clear procedures, as well,
because the specific bit is accessed simply as Regs (Idx)

 
----- Original Message ----- 
From: "Bernd Trog" <berndtrog@yahoo.com>
Newsgroups: comp.lang.ada,comp.arch.embedded
To: <comp.lang.ada@ada.eu.org>
Sent: February 17, 2003 6:06 PM
Subject: Type-safe low-level programming?


> Hi,
> lets say I've something like this:
> 
> pragma No_Run_Time;
> 
> type Bit_Number is range 0 .. 7;
> type Address_Type is range 0 .. 16#ff#;
> 
> Register_A : constant Address_Type := 1;
> Bit_A1 : constant Bit_Number := 4;  --Register A, Bit 4
> Bit_A2 : constant Bit_Number := 1;
> Bit_A3 : constant Bit_Number := 7;
> 
> Register_B : constant Address_Type = 2;
> Bit_B1 : constant Bit_Number :=4;
> Bit_B2 : constant Bit_Number :=2;
> Bit_B3 : constant Bit_Number :=5;
> [...]
> Register_Z : constant Address_Type = 2;
> Bit_Z1 : constant Bit_Number :=6;
> Bit_Z2 : constant Bit_Number :=0;
> Bit_Z3 : constant Bit_Number :=5;
> 
> procedure Set_Bit( Adr : in Address_Type; Nr : in Bit_Number );
> procedure Clear_Bit( Adr : in Address_Type; Nr : in Bit_Number );
> 
> 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
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 
> 




  reply	other threads:[~2003-02-18  2:04 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. [this message]
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
replies disabled

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