From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Is there a better (Ada) way?
Date: Wed, 22 Oct 2003 20:50:41 GMT
Date: 2003-10-22T20:50:41+00:00 [thread overview]
Message-ID: <3F96ED50.2040009@comcast.net> (raw)
In-Reply-To: 20619edc.0310221056.4c92d10c@posting.google.com
Mike Silva wrote:
> I've started writing a little CPU simulator, just for fun and to be
> doing *something* in Ada, and I want to make sure I'm not writing "C
> in Ada" when it comes to all the bit, bitfield and register
> manipulations.
>
> Below is a typical question. I need to add a 5-bit signed offset to a
> 16-bit register. The way I came up with is shown on the last line of
> the example code, but I want to find out if there's a more Ada-ish way
> to do what I'm doing. Any comments about any part of the example code
> are welcome.
> R16 : Reg16_t;
> PB : Postbyte_t;
> begin
> ....
> R16 := R16 + Reg16_t( Integer( PB.offset5 ) mod Reg16_t'Modulus );
> -- the above line works, but is it good Ada, or C-in-Ada?
Probably not.
R16 := R16 + Reg16_t(PB.offset5);
Should do exactly the same thing--but it doesn't. I would probably write:
R16 := Reg16_t(Integer(R16) + Integer(PB.offset5));
However, you need to think about what you want to happen when you add a
positive offset to R16'Last, or a negative offset to Reg16'First. If
you do want wraparound semantics, you can probably suppress
Constraint_Error and use the first line I supplied.
And incidently, it is pretty common in Ada when doing things like this
that you have to think about which exceptional cases you don't want
detected, instead of which special cases you do want to handle.
--
Robert I. Eachus
"Quality is the Buddha. Quality is scientific reality. Quality is the
goal of Art. It remains to work these concepts into a practical,
down-to-earth context, and for this there is nothing more practical or
down-to-earth than what I have been talking about all along...the repair
of an old motorcycle." -- from Zen and the Art of Motorcycle
Maintenance by Robert Pirsig
next prev parent reply other threads:[~2003-10-22 20:50 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-10-22 18:56 Is there a better (Ada) way? Mike Silva
2003-10-22 19:33 ` sk
2003-10-23 18:33 ` Mike Silva
2003-10-22 20:50 ` Robert I. Eachus [this message]
2003-10-23 16:57 ` Mike Silva
2003-10-23 21:42 ` Robert I. Eachus
2003-10-24 1:42 ` Mike Silva
2003-10-23 3:12 ` Steve
2003-10-23 18:30 ` Mike Silva
2003-10-24 22:20 ` Nick Roberts
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox