comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: 64bit access to an array of 8bit values
Date: Sun, 3 Mar 2002 16:24:24 -0600
Date: 2002-03-03T16:24:24-06:00	[thread overview]
Message-ID: <mailman.1015194302.22754.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: 3C823A1A.6030006@users.sf.net

How about something like this?

with Ada.Text_IO;
with Interfaces;
procedure Test is

   type Ram_Range is range 0 .. 2 ** 12 -1;

   Ram : array (Ram_Range) of Interfaces.Unsigned_8;

   function Value (Index : Ram_Range) return Interfaces.Unsigned_8
   is
      Result : Interfaces.Unsigned_8;
      for Result'Address use Ram (Index)'Address;
   begin
      return Result;
   end Value;

   function Value (Index : Ram_Range) return Interfaces.Unsigned_64
   is
      Result : Interfaces.Unsigned_64;
      for Result'Address use Ram (Index)'Address;
   begin
      return Result;
   end Value;

   procedure Set (Value : Interfaces.Unsigned_8; Index : Ram_Range)
   is
      Target : Interfaces.Unsigned_8;
      for Target'Address use Ram (Index)'Address;
   begin
      Target := Value;
   end Set;

   procedure Set (Value : Interfaces.Unsigned_64; Index : Ram_Range)
   is
      Target : Interfaces.Unsigned_64;
      for Target'Address use Ram (Index)'Address;
   begin
      Target := Value;
   end Set;

   Value_64 : Interfaces.Unsigned_64;

begin
   Set (Interfaces.Unsigned_64'(12345678901234567890), 1234);
   Value_64 := Value (1234);
   Ada.Text_IO.Put_Line
     ("Interfaces.Unsigned_64 value:" &
      Interfaces.Unsigned_64'Image (Value_64));
   for B in Ram_Range'(1234) .. 1241 loop
      Ada.Text_IO.Put_Line
        ("Interfaces.Unsigned_8 value at index" & Ram_Range'Image (B) & ":" &
         Interfaces.Unsigned_8'Image (Value (B)));
   end loop;
end Test;

----- Original Message ----- 
From: "Dave Poirier" <instinc@users.sourceforge.net>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: March 03, 2002 8:58 AM
Subject: 64bit access to an array of 8bit values


> 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
> 
> ------
> procedure test is
>    type Bit8 is mod 2**8;
>    type Bit64 is mod 2**64;
>    type Bit8Acc is access Bit8;
>    type Bit64Acc is access Bit64;
> 
>    type RamRange is range 0 .. 4000;
> 
>    Ram : array (RamRange) of aliased Bit8;
>    Value : Bit64Acc;
> 
> begin
>    Value := Ram(0)'Access;
> end test;
> ------
> 
> test.adb:14:18: expected type "Bit64Acc" defined at line 6
> test.adb:14:18: found type access to "bit8" defined at line 14
> 
> and also..
> ------
> procedure test is
>    type Bit8 is mod 2**8;
>    type Bit64 is mod 2**64;
>    type Bit8Acc is access all Bit8;
>    type Bit64Acc is access all Bit64;
> 
>    type RamRange is range 0 .. 4000;
> 
>    Ram : array (RamRange) of aliased Bit8;
>    Value : Bit64Acc;
> 
> begin
>    Value := Bit64Acc(Ram(0)'Access);
> end test;
> ------
> 
> test.adb:14:12: argument of conversion cannot be access
> test.adb:14:12: use qualified expression instead
> 
> I'm just a smooth skinned Ada95 newbie, so don't get too big weapons to 
> send replies ;)
> 
> Thanks for helping me learn :)
> EKS - Dave Poirier
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 





  parent reply	other threads:[~2002-03-03 22:24 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. [this message]
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
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