comp.lang.ada
 help / color / mirror / Atom feed
From: Thad Smith <thadsmith@acm.org>
Subject: Re: Type-safe low-level programming?
Date: Tue, 18 Feb 2003 09:21:18 -0700
Date: 2003-02-18T09:21:18-07:00	[thread overview]
Message-ID: <3E525D7D.BA64EE12@acm.org> (raw)
In-Reply-To: b2t2gq$650$1@nets3.rz.RWTH-Aachen.DE

Hans-Bernhard Broeker wrote:
> 
> In comp.arch.embedded Bernd Trog <berndtrog@yahoo.com> wrote:
> 
> > Now I wonder, if its possible to make 'Set_Bit' type-safe *without*
> > increasing the program memory size for every new register?
> 
> The only way I see is to drop the idea of passing the register and the
> bit number inside that register as independent parameters --- they
> aren't independent, and pretending they are is what causes your grief.
> The need to introduce a comment to explain what Bit_A1 is supposed to
> mean should have given you the hint.
> 
> What you need would be more like
> 
>   type Bit_Number is range 0 .. 8*16#ff#);
> 
>   Register_A : constant Address_Type := 1;
> 
>   Bit_A1 : constant Bit_Number := Register_A*8 + 4;
>   Bit_A2 : constant Bit_Number := Register_A*8 + 1;

Good point.  A note to fellow comp.arch.embedded readers: this also
works as well in other languages.  I have used a similar scheme in C and
assembly, bundling the port address and bit number and number of bits
and bit polarity into a single macro definition.  A single macro
invocation, such as 

	SetOutput (RF_ATTENUATOR, 1);

can be used to set the correct bit in the correct port.  An I/O
definition header file defines all I/O signals in terms of the
application, not port/bit names.  Changing the port assignment or
inverting the sense of an I/O signal is done in the definition file.

The code generated is close to optimal, since the compiler is working on
constant definitions, which it can optimize well and no function calls
are required.

The biggest downside is that the macro definitions (in C) look like a
bowl of Chinese Alphabet soup to handle all the details.

Thad



  reply	other threads:[~2003-02-18 16:21 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 [this message]
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