----boundary-LibPST-iamunique-2017952193_-_-
Content-type: text/plain
Dear Robert:
I have advanced a little bit concerning bit manipulation and
unsigned assignments. As a practical example, today I accomplished
a mundane task of packing two bytes from a byte buffer into
a 32-bit integer. Something, which in C is perhaps as simple as
intVar = bhi<<8 + blo;
The following code fragment is a bit more complex because the sign
bits get in the way and have to be cut. I wonder if such manipulations
are the most efficient way of packing/unpacking data. In any case,
I did not have to use SYSTEM to accomplish this.
VAR setHi,setLo: SET; (*bit field variables (hi,lo byte)*)
FOR i := 0 TO len-1 DO
(*Pack two consequtive bytes into an integer. MSB is first.*)
(*Cutting with bit masks avoids sign extension*)
setHi := BITS(ASH(buffer[2*i],8)) * {8..15};
setLo := BITS( buffer[2*i+1]) * {0..7};
histogram.bin [i] := ORD(setHi + setLo);
END;
In any case, I am now enjoying a graphical display of my first data set
received from the FPGA over the USB link directly into the BlackBox
environment, where I am using my Gr package to display the histogram.
This involves going through layers of VHDL, C, and finally into the
wonderful realm of Component Pascal, where all men are strong,
all women are pretty, and all children are above average.
Kind regards,
Wojtek Skulski skulski{([at]})nowhere.xy
On Tue, 18 Jun 2002 robert.d.campbell{([at]})nowhere.xy
> Wojtek Skulski wrote
>
> >"My more immediate concern is how to make assignments of bit patterns
> >such as 0ABH to a variable of a given bit width. These variables are
> >directly mapped onto C-style DLL variables like UCHAR or USHORT.
> >I have spent considerable time reading C-headers and trying to match
> >these to variable types CP is giving me. The interesting part is
> >C structures and pointers to C structures.
>
> The BB documentation addresses variable layout in memory and on disk
> in the language reference and Stores Docus. The mapping of memory bit
> numbers to (eg) INTEGERs is not documented as it might be platform
> specific.
>
> However bit 5 in a set is bit 5 whatever platform is used. It is an
> open secret that on PC's bit 0 is the LSB. What I gues you want to do
> is to set (and process) your bits using the SET abstraction, then map them
> to BYTEs using SYSTEM.VAL in a potentially platform specific way. (Maybe
> some SHORTs are also needed.)
>
> The example below uses this kind of approach to compute a file checksum
> using a 32 bit irreducible polynominal over Galois Field 2 - sounds fancy
> but is simple bit bashing. Setting the bit patterns using the SET
> abstraction is easy; as is converting in and out of INTEGER when required.
>
>
> PROCEDURE CheckFile (loc : Files.Locator; fInfo : Files.FileInfo) : INTEGER;
> CONST
> fDatSiz = 102400 * 2;
> mask = {0, 1, 27, 28};
> lowByte = {0 .. 7};
> VAR
> i : INTEGER;
> fIn : Files.File;
> rd : Files.Reader;
> fData : ARRAY fDatSiz OF BYTE;
> cSum : SET;
> BEGIN
> IF ~zpr.cSum OR (fInfo.length > fDatSiz) THEN RETURN 0 END;
> fIn := dir.Old (loc, fInfo.name, TRUE);
> IF fIn = NIL THEN RETURN 0 END;
>
> ASSERT (fIn.Length () = fInfo.length, 65);
> rd := fIn.NewReader (rd);
> rd.ReadBytes (fData, 0, fInfo.length); INC (xCnt, fInfo.length);
>
> cSum := {0};
> FOR i := 0 TO fInfo.length - 1 DO
> cSum := cSum / (SYSTEM.VAL (SET, fData [i]) * lowByte);
> IF 31 IN cSum THEN
> cSum := SYSTEM.LSH (cSum, 1);
> cSum := cSum / mask
> ELSE
> cSum := SYSTEM.LSH (cSum, 1)
> END
> END;
> fIn.Close ();
> RETURN SYSTEM.VAL (INTEGER, cSum)
> END CheckFile;
>
>
> Regards
>
>
> Robert Campbell
--------------------------------------------
To unsubscribe from this mailing list, send a message containing the word "unsubscribe" to:
blackbox-request{([at]})nowhere.xy
To get a list of valid e-mail commands and instructions on their usage, send a message containing the word "help" to the above address.
Send any problem reports or questions related to this email list to the list owner at
owner-blackbox{([at]})nowhere.xy
Current posting policy:
a) To post you should use the same address by which you are subscribed to the mailing list. That way, the list server will recognize you as subscriber and forward your posting immediately, without creating any overhead.
b) If, for some reason, you cannot post from the address, by which you are subscribed, your message will be moderated to avoid spam. Please understand that moderation will often cause some delay, in particular over weekends or holydays.
----boundary-LibPST-iamunique-2017952193_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"
e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRle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