Re: Unions - overlaying bits & logic


Subject: Re: Unions - overlaying bits & logic
From: Peter Flake (flake@co-design.com)
Date: Wed Apr 10 2002 - 03:35:21 PDT


Kevin,

Normal unions do not define the data storage, so writing one member and reading another is not recommended because it is implementation dependent.  However we all know that this kind of "dirty" operation is sometimes useful, and the packed union defines a limited version which has a standard behavior.

I sent the following on 17 March.  It uses the word "masked" to mean 4 state.

A packed union contains members that are packed structures or arrays of the same size. This ensures that you can read back a union member that was written as another member. If any member is masked, the whole union is masked. A packed union can also be used as a whole with arithmetic and logical operators, and its behavior is determined by the signed or unsigned keyword, the latter being the default.

For example, a union can be accessible with different access widths:
typedef union packed { // default unsigned
s_atmcell acell;
bit [423:0] bit_slice;
bit [52:0][7:0] byte_slice;
} u_atmcell;

u_atmcell u1;
byte b; bit [3:0] nib;
b = u1.bit_slice[415:408]; // same as b = u1.byte_slice[51];
nib = u1.bit_slice [423:420]; // same as nib = u1.acell.GFC;

Note that writing one member and reading another is independent of the byte ordering of the machine, unlike a normal union of normal structures, which are C-compatible and have members in ascending address order.

Peter.

At 11:21 AM 4/9/02 -0700, Kevin Cameron x3251 wrote:

Wouldn't mind a response from Co-Design on this before the next meeting.

Ta,
Kev.

> Apart from endianess, there is an issue with size of data in unions. The types bit,char,integer etc. are
> as in C, but types like "logic" do not have a defined bit representation - it could be:
>
>  A: an enum (e.g. L0=0,L1=1,LX=2,LZ=3) - requires 2 bits (per node)
>  B: value & strength  (0,1,X) * (1,Z) - requires 3 bits
>  C: value, strength and certainty (0,1) * (1,Z) * (1,X) - also 3 bits, but different coding
>
> I think it is therfore impossible to sensibly overlay logic and bit types, unions should have
> either all logic or all bit as the base type of all elements. The alternative is to store the
> X & Z orthogonally so that only the bit value is overlayed (using representation C),
> which has performance issues.
>
> If a union of "structs" is declared, the the fields being overlayed should match in their
> base type (rather than the whole struct).
>
> [Note: 'logic' and 'reg' should probably be something other than 'integer_type' in the BNF]
>
> Kev.



This archive was generated by hypermail 2b28 : Wed Apr 10 2002 - 03:36:18 PDT