comp.lang.ada
 help / color / mirror / Atom feed
* Re: Direct memory access
  1999-03-25  0:00 Direct memory access Gordy
  1999-03-25  0:00 ` Joachim Schroeer
@ 1999-03-25  0:00 ` Jerry van Dijk
  1999-03-25  0:00   ` Tom Moran
  1 sibling, 1 reply; 10+ messages in thread
From: Jerry van Dijk @ 1999-03-25  0:00 UTC (permalink / raw)


Gordy (gbissell@coventry.ac.uk) wrote:

: I'm trying to write some code to allow direct access to memory (for 
: testing memory on an embedded system). I simply want something like 'C' 
: pointers, so I can write/read values to a given memory address.

: I've had a look at the Machine_Code package (to write my own functions) 
: but there must be a better way (i.e is there a function in the System 
: package which will allow writes to memory given a value and a 
: System.Address)?

Yes, although you must have permission from the OS to access the memory.

Simple example:

-----------------------------------------------------------------------
with Ada.Text_IO;
with System.Address_To_Access_Conversions;

procedure Example is

   --------------------------
   --  Create some memory  --
   --------------------------
   
   type Byte is mod 2 ** 8;
   for Byte'Size use 8;

   Memory : array (0 .. 2048) of Byte := (others => 0);

   ---------------------------------------
   --  Create package to access memory  --
   ---------------------------------------

   package Byte_Conversions is new
     System.Address_To_Access_Conversions (Byte);

   -----------------
   --  Variables  --
   -----------------
     
   Byte_Ptr : Byte_Conversions.Object_Pointer;
   --  can hold an access value to a byte
   
begin

   --  Write 10 values to memory
   
   for I in 512 .. 521 loop

      Byte_Ptr     := Byte_Conversions.To_Pointer (Memory(I)'Address);
      Byte_Ptr.all := Byte (I - 512);

   end loop;
   
   --  Display 10 bytes from memory

   for I in 512 .. 521 loop

      Byte_Ptr := Byte_Conversions.To_Pointer (Memory(I)'Address);
      Ada.Text_IO.Put_Line (Byte'Image (Byte_Ptr.all));
      
   end loop;
   
end Example;
-----------------------------------------------------------------------

--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada       | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-25  0:00 ` Joachim Schroeer
@ 1999-03-25  0:00   ` dennison
  1999-03-25  0:00     ` Markus Kuhn
  1999-03-25  0:00     ` Jerry van Dijk
  1999-03-26  0:00   ` robert_dewar
  1 sibling, 2 replies; 10+ messages in thread
From: dennison @ 1999-03-25  0:00 UTC (permalink / raw)


In article <7dda9v$bf3$1@fleetstreet.Austria.EU.net>,
  "Joachim Schroeer" <schroeer@amst.co.at> wrote:
>
> Gordy schrieb in Nachricht ...
> >I'm using GNAT v3.11 for IBM PC platforms.
> >
> >I'm trying to write some code to allow direct access to memory (for
> >testing memory on an embedded system). I simply want something like 'C'
> >pointers, so I can write/read values to a given memory address.

> Use the address attribute, LRM 13.3 Representation Attributes, e.g.:
>
> a : record_type;
>
> for a'address use 16#0000ffff#;

That's clearly not what was asked for. The poster asked for something like C
pointers, and you gave a solution that's easier, more understandable, and more
reliable. Shame on you.

:-)

T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-25  0:00 ` Jerry van Dijk
@ 1999-03-25  0:00   ` Tom Moran
  0 siblings, 0 replies; 10+ messages in thread
From: Tom Moran @ 1999-03-25  0:00 UTC (permalink / raw)


> package Byte_Conversions is new
>     System.Address_To_Access_Conversions (Byte);
Why not use System.Storage_Elements?  If he wants to write a RAM test
program, presumably he wants to access all memory and know the
addresses, which would appear to be what System.Storage_Elements is
about, no?
> you must have permission from the OS to access the memory
and be able to see physical, not virtual, addresses.




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-25  0:00   ` dennison
@ 1999-03-25  0:00     ` Markus Kuhn
  1999-03-25  0:00     ` Jerry van Dijk
  1 sibling, 0 replies; 10+ messages in thread
From: Markus Kuhn @ 1999-03-25  0:00 UTC (permalink / raw)


"Joachim Schroeer" <schroeer@amst.co.at> wrote:
>I'm using GNAT v3.11 for IBM PC platforms.
>
>I'm trying to write some code to allow direct access to memory (for
>testing memory on an embedded system). I simply want something like 'C'
>pointers, so I can write/read values to a given memory address.

Pointer arithmetic in Ada95 is defined in the package System.Storage_Elements.
You can transform an integer into an address using

  function To_Address(Value : Integer_Address) return Address;

You can do pointer arithmetic on type Address. You can convert
an address into a pointer for some object type using the generic
package System.Address_To_Access_Conversions which contains the function

  function To_Pointer(Value : Address) return Object_Pointer;

This is all you need to do all the messy thing that you can do with
C pointer arithmetic. It is just quite a bit more bureaucratic in Ada,
because you have to add one more line than in the corresponding C code
to instantiate Address->pointer conversion function, and because Ada
(unlike C, BCPL, and Assembler) does distinguish between integers and
addresses and therefore requires an additional type conversion.

Note that all these conversion functions are intrinsic functions,
therefore the compiler should not generate a single instruction for them
and the resulting code should be identical to what you get in C with
just *(sometype *)x = y; .

Ada clearly works hard to discourage direct memory access and
pointer arithmetic, therefore developpers are more likely to
encapsulate these clumsy accesses in separate packages, and
as a result the dangerous unchecked code is concentrated in one
place and not spread throught the software.

http://www.adahome.com/rm95/rm9x-13-07.html

Markus

-- 
Markus G. Kuhn, Computer Laboratory, University of Cambridge, UK
Email: mkuhn at acm.org,  WWW: <http://www.cl.cam.ac.uk/~mgk25/>




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-25  0:00   ` dennison
  1999-03-25  0:00     ` Markus Kuhn
@ 1999-03-25  0:00     ` Jerry van Dijk
  1999-03-26  0:00       ` dennison
  1 sibling, 1 reply; 10+ messages in thread
From: Jerry van Dijk @ 1999-03-25  0:00 UTC (permalink / raw)


dennison@telepath.com wrote:

: > a : record_type;
: >
: > for a'address use 16#0000ffff#;

Did you try this ? :-)

--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada       | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Direct memory access
@ 1999-03-25  0:00 Gordy
  1999-03-25  0:00 ` Joachim Schroeer
  1999-03-25  0:00 ` Jerry van Dijk
  0 siblings, 2 replies; 10+ messages in thread
From: Gordy @ 1999-03-25  0:00 UTC (permalink / raw)


I'm using GNAT v3.11 for IBM PC platforms. 

I'm trying to write some code to allow direct access to memory (for 
testing memory on an embedded system). I simply want something like 'C' 
pointers, so I can write/read values to a given memory address.

I've had a look at the Machine_Code package (to write my own functions) 
but there must be a better way (i.e is there a function in the System 
package which will allow writes to memory given a value and a 
System.Address)?

Any help greatly appreciated.

Gordon.





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-25  0:00 Direct memory access Gordy
@ 1999-03-25  0:00 ` Joachim Schroeer
  1999-03-25  0:00   ` dennison
  1999-03-26  0:00   ` robert_dewar
  1999-03-25  0:00 ` Jerry van Dijk
  1 sibling, 2 replies; 10+ messages in thread
From: Joachim Schroeer @ 1999-03-25  0:00 UTC (permalink / raw)


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


Gordy schrieb in Nachricht ...
>I'm using GNAT v3.11 for IBM PC platforms.
>
>I'm trying to write some code to allow direct access to memory (for
>testing memory on an embedded system). I simply want something like 'C'
>pointers, so I can write/read values to a given memory address.
>
>I've had a look at the Machine_Code package (to write my own functions)
>but there must be a better way (i.e is there a function in the System
>package which will allow writes to memory given a value and a
>System.Address)?
>
>Any help greatly appreciated.
>
>Gordon.
>

Use the address attribute, LRM 13.3 Representation Attributes, e.g.:

a : record_type;

for a'address use 16#0000ffff#;

    J. Schr�er








^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-25  0:00 ` Joachim Schroeer
  1999-03-25  0:00   ` dennison
@ 1999-03-26  0:00   ` robert_dewar
  1 sibling, 0 replies; 10+ messages in thread
From: robert_dewar @ 1999-03-26  0:00 UTC (permalink / raw)


In article <7dda9v$bf3$1@fleetstreet.Austria.EU.net>,
  "Joachim Schroeer" <schroeer@amst.co.at> wrote:

> Use the address attribute, LRM 13.3 Representation
> Attributes, e.g.:
>
> a : record_type;
>
> for a'address use 16#0000ffff#;


No Ada 95 compiler should accept the above statements,
given the implementation advice in the RM:

    37   Address should be of a private type.

Indeed if any Ada 95 compiler *does* ignore this very
sensible implementation advice, it would be interesting to
look in annex M of their documentation and see what the
justification was (possibly Ada 83 compatibility :-)

Anyway, to even attempt writing this is incorrect, and it
will not work on many systems. The proper thing is to use
the To_Address function in System.Storage_Elements.


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-25  0:00     ` Jerry van Dijk
@ 1999-03-26  0:00       ` dennison
  1999-03-27  0:00         ` Jerry van Dijk
  0 siblings, 1 reply; 10+ messages in thread
From: dennison @ 1999-03-26  0:00 UTC (permalink / raw)


In article <F96B2y.71@jvdsys.stuyts.nl>,
  jerry@jvdsys.stuyts.nl (Jerry van Dijk) wrote:
> dennison@telepath.com wrote:
>
> : > a : record_type;
> : >
> : > for a'address use 16#0000ffff#;
>
> Did you try this ? :-)

No. But then again, I didn't post it either. Check your quoting.

T.E.D.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Direct memory access
  1999-03-26  0:00       ` dennison
@ 1999-03-27  0:00         ` Jerry van Dijk
  0 siblings, 0 replies; 10+ messages in thread
From: Jerry van Dijk @ 1999-03-27  0:00 UTC (permalink / raw)


dennison@telepath.com wrote:
: In article <F96B2y.71@jvdsys.stuyts.nl>,
:   jerry@jvdsys.stuyts.nl (Jerry van Dijk) wrote:
: > dennison@telepath.com wrote:
: >
: > : > a : record_type;
: > : >
: > : > for a'address use 16#0000ffff#;
: >
: > Did you try this ? :-)

: No. But then again, I didn't post it either. Check your quoting.

Not an easy feat to accomplish using tin. But I will try again:

>> a : record_type;
>>
>> for a'address use 16#0000ffff#;
>
> That's clearly not what was asked for. The poster asked for something like C
> pointers, and you gave a solution that's easier, more understandable, and more
> reliable. Shame on you.
>
> :-)
>
> T.E.D.

--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada       | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~1999-03-27  0:00 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-25  0:00 Direct memory access Gordy
1999-03-25  0:00 ` Joachim Schroeer
1999-03-25  0:00   ` dennison
1999-03-25  0:00     ` Markus Kuhn
1999-03-25  0:00     ` Jerry van Dijk
1999-03-26  0:00       ` dennison
1999-03-27  0:00         ` Jerry van Dijk
1999-03-26  0:00   ` robert_dewar
1999-03-25  0:00 ` Jerry van Dijk
1999-03-25  0:00   ` Tom Moran

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