Casting a Variable to the same type as another variable in Ada
I have a codebase which is being built for several different targets, each with its own set of register definitions. For a given target, there is a register definition file which will define register contents like this:
type Register1 is record
field1 : U7;
field2 : U8;
end record;
The types U7
and U8
are just Unsigned types defined like this:
type U7 is mod 2 ** 7 with Size => 7;
type U8 is mod 2 ** 8 with Size => 8;
...
In my driver, I have a Register_Write function which takes a Register1 as an argument. I've tried calling it like this:
field1_val: U32:= 1;
field2_val: U32:= 2;
...
Register_Write( Register1'(field1 => field1_val, field2 => field2_val ));
But this does not work and I get the error expected type "U7", found type "U32"
. The issue is that I cannot just make field1_val a U7, because the type of this field is different for different targets. For example, on some targets it may be U7, on some it may be U6, etc.
Is there a way of saying "Cast field1_val to the type of field1" when I create a Register1 record?