From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4ac8a7e7d3e637dc X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-22 13:50:41 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!wn14feed!wn13feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!attbi_s51.POSTED!not-for-mail Message-ID: <3F96ED50.2040009@comcast.net> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Is there a better (Ada) way? References: <20619edc.0310221056.4c92d10c@posting.google.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.34.139.183 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s51 1066855841 24.34.139.183 (Wed, 22 Oct 2003 20:50:41 GMT) NNTP-Posting-Date: Wed, 22 Oct 2003 20:50:41 GMT Organization: Comcast Online Date: Wed, 22 Oct 2003 20:50:41 GMT Xref: archiver1.google.com comp.lang.ada:1470 Date: 2003-10-22T20:50:41+00:00 List-Id: 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