Split and casting address into different integers in Ada

To interface with a certain piece of hardware (in this case a TSS entry of an x86 GDT), it is required to use the following structure in memory:

type UInt32 is mod 2 ** 32;
type UInt16 is mod 2 ** 16;
type UInt8  is mod 2 ** 8;

type TSSEntry is record
   Limit       : UInt16;
   BaseLow16   : UInt16;
   BaseMid8    : UInt8;
   Flags1      : UInt8;
   Flags2      : UInt8;
   BaseHigh8   : UInt8;
   BaseUpper32 : UInt32;
   Reserved    : UInt32;
end record;
for TSSEntry use record
   Limit       at 0 range 0  .. 15;
   BaseLow16   at 0 range 16 .. 31;
   BaseMid8    at 0 range 32 .. 39;
   Flags1      at 0 range 40 .. 47;
   Flags2      at 0 range 48 .. 55;
   BaseHigh8   at 0 range 56 .. 63;
   BaseUpper32 at 0 range 64 .. 95;
   Reserved    at 0 range 96 .. 127;
end record;
for TSSEntry'Size use 128;

When translating some C code into Ada, I ran into several issues, and I could not find many resources online. the C snippet is:

TSSEntry tss;

void loadTSS(size_t address) {
    tss.baseLow16   = (uint16_t)address;
    tss.baseMid8    = (uint8_t)(address >> 16);
    tss.flags1      = 0b10001001;
    tss.flags2      = 0;
    tss.baseHigh8   = (uint8_t)(address >> 24);
    tss.baseUpper32 = (uint32_t)(address >> 32);
    tss.reserved    = 0;
}

This is the Ada code I tried to translate it to:

TSS : TSSEntry;

procedure loadTSS (Address : System.Address) is
begin
   TSS.BaseLow16   := Address; --  How would I downcast this to fit in the 16 lower bits?
   TSS.BaseMid8    := Shift_Right(Address, 16); -- Bitwise ops dont take System.Address + downcast
   TSS.Flags1      := 2#10001001#;
   TSS.Flags2      := 0;
   TSS.BaseHigh8   := Shift_Right(Address, 24); -- Same as above
   TSS.BaseUpper32 := Shift_Right(Address, 32); -- Same as above
   TSS.Reserved    := 0;
end loadTSS;

How would I be able to show the issues I highlighted in the code? Are there any resources a beginner can use for help in cases likes this? Thanks in advance!



Read more here: https://stackoverflow.com/questions/65714365/split-and-casting-address-into-different-integers-in-ada

Content Attribution

This content was originally published by Rottenheimer2 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: