Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access memory addresses directly in Ada?

So I'm new to Ada, and I'm attempting to write a kernel in it, but I cannot seem to find any good information on how to do this properly. In C, I would write:

unsigned char* videoram = (char*) 0xB8000;
videoram[0] = 65;

to access the video ram directly and write 'a' to it. I've heard I need to use an Ada array and other pragma's to do this in a typesafe manner in Ada. Is there any good resources on this kind of Ada programming?

like image 716
MarkHammons Avatar asked May 30 '12 06:05

MarkHammons


2 Answers

You can use the 'Address attribute:

Videoram : String (1 .. Videoram_Size);
for Videoram'Address use 16#B8000#;
-- ...
Videoram (1) := 'a';

If you don't want to use String and Characters, you can define your own data types.. like:

type Byte is mod 2**8; -- unsigned char
type Byte_Array is array (Natural range <>) of Byte;
Videoram : Byte_Array (0 .. Videoram_Size - 1);
for Videoram'Address use 16#B8000#;
-- ...
Videoram (0) := 65;

Btw, you even get range checking for the index, so you can't write outside of the Videoram range.

like image 156
Rommudoh Avatar answered Sep 28 '22 08:09

Rommudoh


If you use an address attribute (i.e. for Object'Address use ... ), you should use the To_Address() function found in System.Storage_Elements because the Address type doesn't have to be an integer. The Ada Reference Manual only states:

"Address is a definite, nonlimited type with preelaborable initialization"

Whereas for the Integer_Address type in System.Storage_Elements it states:

"Integer_Address is a (signed or modular) integer subtype. To_Address and To_Integer convert back and forth between this type and Address."

So, you should really use:

for Object'Address use To_Address( 16#B8000# );

One last thing to point out from T.E.D's answer is that if you are concerned about object initialization using this method, simply add a pragma Import( Ada, your_object ) after the declaration so that default initialization is suppressed.

like image 32
Micronian Coder Avatar answered Sep 28 '22 08:09

Micronian Coder