comp.lang.ada
 help / color / mirror / Atom feed
From: "Pat Rogers" <progers@classwide.com>
Subject: Re: 64bit access to an array of 8bit values
Date: Mon, 04 Mar 2002 03:15:25 GMT
Date: 2002-03-04T03:15:25+00:00	[thread overview]
Message-ID: <hFBg8.62687$J%7.1246781616@newssvr30.news.prodigy.com> (raw)
In-Reply-To: 3C823A1A.6030006@users.sf.net

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3028 bytes --]

"Dave Poirier" <instinc@users.sf.net> wrote in message
news:3C823A1A.6030006@users.sf.net...
> I'm trying to modelize a small virtual machine, and I'm stuck on the
> memory model.  I'm defining the memory as an array of 8bit values, so
> that I am able to access every byte individually.  The problem comes
> when I try to access them with other data size, like 16/32/64bit.  GNAT
> simply refuses to do the pointer conversion (yeah, probably a bad habit).
>
> So, what would be a "clean" way to do it?  here's what I tried
<snip>

The predefined way to manipulate values of arbitrary types at arbitrary
locations is to use the generic package
System.Address_to_Access_Conversions:

generic
    type Object (<>) is limited private;
package System.Address_To_Access_Conversions is
    type Object_Pointer is access all Object;
    function To_Pointer( Value : Address ) return Object_Pointer;
    function To_Address( Value : Object_Pointer ) return Address;
    pragma Convention( Intrinsic, To_Pointer );
    pragma Convention( Intrinsic, To_Address );
end System.Address_To_Access_Conversions;


For example, if you wanted to swap the two bytes at an arbitrary address
(assuming that was
the reasonable thing to do), you could do it like this:


with Interfaces;
with System.Address_To_Access_Conversions;
package body Byte_Swapping is

    type Word is new Interfaces.Unsigned_16;

    package Word_Ops is new System.Address_To_Access_Conversions( Word );
    use Word_Ops;

    procedure Swap2( Location : in System.Address ) is
        X : Word renames To_Pointer(Location).all;
    begin
        X := ( Shift_Left(X,8) and 16#FF00# ) or
               ( Shift_Right(X,8) and 16#00FF# );
    end Swap2;

    �

end Byte_Swapping;

There are other ways, of course, but this illustrates the idea.

Note that there is no guarantee that the address you provide to To_Pointer is
actually a meaningful address for a value of a given type.  You have to be
concerned with issues such as padding and alignment, for example.

Having described the above, let me also say that you can do unchecked
conversions from addresses into access values if you are knowledgeable about how
access values are implemented for your given machine/vendor.  For example, there
is no reason to think that an access value is simply a single address -- it
might be an address pair, and it depends upon the type designated.

By the way, depending on exactly what you are doing, if you want to model
contiguous memory you should consider using the type
System.Storage_Elements.Storage_Array -- it is guaranteed contiguous, unlike
other array types.  That may not be an issue for your model, but if you are
doing address calculations it is worth thinking about.

Hope that helps,

---
Patrick Rogers                       Consulting and Training in:
http://www.classwide.com          Real-Time/OO Languages
progers@classwide.com               Hard Deadline Schedulability Analysis
(281)648-3165                                 Software Fault Tolerance





  parent reply	other threads:[~2002-03-04  3:15 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-03 14:58 64bit access to an array of 8bit values Dave Poirier
2002-03-03 15:38 ` Jim Rogers
2002-03-03 18:02 ` John R. Strohm
2002-03-03 16:39   ` Dave Poirier
2002-03-03 17:27   ` Jeffrey Creem
2002-03-05 20:49     ` John R. Strohm
2002-03-05 23:52       ` Jeffrey Creem
2002-03-06  7:30         ` John R. Strohm
2002-03-06 11:50           ` Jeffrey Creem
2002-03-07 20:03             ` John R. Strohm
2002-03-04 10:29   ` Robert Dewar
2002-03-04 13:02     ` Larry Kilgallen
2002-03-04 13:41       ` Dave Poirier
2002-03-03 22:24 ` David C. Hoos, Sr.
2002-03-03 22:51   ` Dave Poirier
2002-03-04  2:40     ` David C. Hoos, Sr.
2002-03-04  4:08     ` David Starner
2002-03-04 10:31   ` Robert Dewar
2002-03-04 18:00   ` Warren W. Gay VE3WWG
2002-03-04  3:15 ` Pat Rogers [this message]
2002-03-04  7:23 ` Jeffrey Carter
replies disabled

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