comp.lang.ada
 help / color / mirror / Atom feed
* Array slicing question
@ 2014-07-07  7:31 Mike Silva
  2014-07-07  8:04 ` Simon Wright
  2014-07-07 13:34 ` G.B.
  0 siblings, 2 replies; 3+ messages in thread
From: Mike Silva @ 2014-07-07  7:31 UTC (permalink / raw)


I (I being an Ada novice) am still fiddling around with writing 4 bits of LCD display data into a 32-bit GPIO register.  I have this:

   type Bits_1  is mod 2**1 with Size => 1;
   type Bits_32x1 is array (0 .. 31) of Bits_1 with Pack, Size => 32;
   type Bits_8x1 is array (0 .. 7) of Bits_1 with Pack, Size => 8;

and I want to do things like this:

   a32 : Bits_32x1;
   a8  : Bits_8x1;
...
   a32(4..7) := a8(4..7);

This is not allowed, apparently, because a32 and a8 are different types (even though the elements of the arrays are the same type, so I'm a little confused, but OK).

So can I do this?  I'm guessing if Bits_32x1 and Bits_8x1 were both subtypes of the same type, I could do it - is this correct?  But I don't know how to declare such a type, especially given the Pack and Size attributes.

I note that (ref confusion above) I CAN do this:

      for i in 4..7 loop
         gpio32(i) := b8(i);



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

* Re: Array slicing question
  2014-07-07  7:31 Array slicing question Mike Silva
@ 2014-07-07  8:04 ` Simon Wright
  2014-07-07 13:34 ` G.B.
  1 sibling, 0 replies; 3+ messages in thread
From: Simon Wright @ 2014-07-07  8:04 UTC (permalink / raw)


Mike Silva <mjsilva697@gmail.com> writes:

> I (I being an Ada novice) am still fiddling around with writing 4 bits
> of LCD display data into a 32-bit GPIO register.  I have this:
>
>    type Bits_1  is mod 2**1 with Size => 1;
>    type Bits_32x1 is array (0 .. 31) of Bits_1 with Pack, Size => 32;
>    type Bits_8x1 is array (0 .. 7) of Bits_1 with Pack, Size => 8;
>
> and I want to do things like this:
>
>    a32 : Bits_32x1;
>    a8  : Bits_8x1;
> ...
>    a32(4..7) := a8(4..7);
>
> This is not allowed, apparently, because a32 and a8 are different
> types (even though the elements of the arrays are the same type, so
> I'm a little confused, but OK).
>
> So can I do this?  I'm guessing if Bits_32x1 and Bits_8x1 were both
> subtypes of the same type, I could do it - is this correct?  But I
> don't know how to declare such a type, especially given the Pack and
> Size attributes.
>
> I note that (ref confusion above) I CAN do this:
>
>       for i in 4..7 loop
>          gpio32(i) := b8(i);

because the components _are_ the same type.

Would this help?

   type Bits is array (Natural range <>) of Boolean with Pack;

   subtype Bits_32 is Bits (0 .. 31);
   subtype Bits_8 is Bits (0 .. 7);

   A32 : Bits_32 with Size => 32;
   A8 : Bits_8 with Size => 8;

   ...

   A32 (4 .. 7) := A8 (4 .. 7);

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

* Re: Array slicing question
  2014-07-07  7:31 Array slicing question Mike Silva
  2014-07-07  8:04 ` Simon Wright
@ 2014-07-07 13:34 ` G.B.
  1 sibling, 0 replies; 3+ messages in thread
From: G.B. @ 2014-07-07 13:34 UTC (permalink / raw)


On 07.07.14 09:31, Mike Silva wrote:
> I (I being an Ada novice) am still fiddling around with writing 4 bits of LCD display data into a 32-bit GPIO register.  I have this:
>
>     type Bits_1  is mod 2**1 with Size => 1;
>     type Bits_32x1 is array (0 .. 31) of Bits_1 with Pack, Size => 32;
>     type Bits_8x1 is array (0 .. 7) of Bits_1 with Pack, Size => 8;
>
> and I want to do things like this:
>
>     a32 : Bits_32x1;
>     a8  : Bits_8x1;
> ...
>     a32(4..7) := a8(4..7);

Since this expression stays brief only as long as the slices
have no holes in them – and if atomicity of the update is
required, not touching the other bits in a32 – creating
a temporary doesn't seem so bad, perhaps hidden behind
a subprogram serving as abstraction.
   The assignment could not use a simple loop then, either,
so a bit-wise assignment from the components of a8 to components
of a32 seems still more direct than shifts and masks.

My 2¢ (for what might be too general a scheme)

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

end of thread, other threads:[~2014-07-07 13:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-07  7:31 Array slicing question Mike Silva
2014-07-07  8:04 ` Simon Wright
2014-07-07 13:34 ` G.B.

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