Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you do struct packing in FORTH? #20

Open
RickCarlino opened this issue Dec 30, 2015 · 21 comments
Open

How do you do struct packing in FORTH? #20

RickCarlino opened this issue Dec 30, 2015 · 21 comments

Comments

@RickCarlino
Copy link
Member

FORTH 200X introduced structs. I can't find any documentation on online or in documentation for the best way to perform struct packing, though.

Is it possible in FORTH (GForth, specifically)? I'm trying build an MQTT message packet which requires setting flags that are smaller than one byte.

@cwpjr
Copy link
Member

cwpjr commented Dec 30, 2015

Flags are bits in a cell in Forth. A Nybble is 4 bits. Depends on where
your flags reside in the quibble!

On Tue, Dec 29, 2015 at 10:31 PM, Rick Carlino notifications@github.com
wrote:

FORTH 200X introduced structs
https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Forth200x-Structures.html.
I can't find any documentation on online or in documentation for the best
way to perform struct packing, though.

Is it possible in FORTH (GForth, specifically)? I'm trying build an MQTT
message packet which requires setting flags that are smaller than one byte.


Reply to this email directly or view it on GitHub
#20.

@DRuffer
Copy link
Member

DRuffer commented Dec 30, 2015

I remember doing bit fields in the polyForth DataBase Support System (see https://dl.dropboxusercontent.com/u/49100658/pFDatabase-5.zip), but it's been a while, and I'm not sure what happened to that support. I'll keep looking.

@cwpjr
Copy link
Member

cwpjr commented Dec 30, 2015

My ARM Forth includes:

SETBITS [SETBITS](addr val --)
Logical OR the value-bits with the contents of addr using read, modify,
write
method.
( Create and initialize variable TEST_SETBITS )
VARIABLE TEST_SETBITS 110b TEST_SETBITS !
( Use SETBITS to modify TEST_SETBITS )
TEST_SETBITS 10001b SETBITS
( View results )
TEST_SETBITS @ .B ( 10111 )
See CLRBITS and ANDBITS

On Tue, Dec 29, 2015 at 11:23 PM, Dennis Ruffer notifications@github.com
wrote:

I remember doing bit fields in the polyForth DataBase Support System (see
https://dl.dropboxusercontent.com/u/49100658/pFDatabase-5.zip), but it's
been a while, and I'm not sure what happened to that support. I'll keep
looking.


Reply to this email directly or view it on GitHub
#20 (comment).

@cwpjr
Copy link
Member

cwpjr commented Dec 30, 2015

Also:

[CLRBITS](addr value)
Logical AND then NOT the value-bits with the contents of addr using read,
modify, write method.
( Create and initialize variable TEST_CLRBITS )
VARIABLE TEST_CLRBITS 110b TEST_CLRBITS !
( Use ANDBITS to modify TEST_CLRBITS )
TEST_CLRBITS 10b CLRBITS
( View results )
TEST_CLRBITS @ .B ( 100 )
See ANDBITS and SETBITS
CMOVE

On Tue, Dec 29, 2015 at 11:23 PM, Dennis Ruffer notifications@github.com
wrote:

I remember doing bit fields in the polyForth DataBase Support System (see
https://dl.dropboxusercontent.com/u/49100658/pFDatabase-5.zip), but it's
been a while, and I'm not sure what happened to that support. I'll keep
looking.


Reply to this email directly or view it on GitHub
#20 (comment).

@larsbrinkhoff
Copy link
Member

Forth83 has !BITS and @BITS for storing and fetching bit fields.

@larsbrinkhoff
Copy link
Member

I suppose one could come up with a design to support something like this:

begin-structure foo
  begin-bitfield
    1 +bits a
    1 +bits b
    2 +bits c
    4 +bits d
  end-bitfield
end-structure

@lowfatcomputing
Copy link
Member

I have added something like this to mecrisp-stellaris:

http://hub.darcs.net/pointfree/mecrisp-stellaris/browse/mk20dx256/bitstruct-example.txt

http://hub.darcs.net/pointfree/mecrisp-stellaris/browse/mk20dx256/bitstruct.txt

It was based on this Mitch Bradley's, Structured Data with Bit Fields but extended to support fields that contain subfields (I called them superfields).

@larsbrinkhoff
Copy link
Member

I like superbits. I feel they should have the superpower to solve any programming problem!

@forthy42
Copy link
Member

Am Mittwoch, 30. Dezember 2015, 00:06:17 schrieb Lars Brinkhoff:

I suppose one could come up with a design to support something like this:

begin-structure foo
  begin-bitfield
    1 +bits a
    1 +bits b
    2 +bits c
    4 +bits d
  end-bitfield
end-structure

Since we have no nested structs, a bitfield should just be an entity of its
own. But having such bit fields is indeed a good idea.

Union is also just some individual structures, and the size of the union is
simply the maximum of those sizes (no syntactical support).

Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o ID: kQusJzA;7_?t=uy@X}1GWr!+0qqp_Cn176t4(dQ_
http://bernd-paysan.de/

@lowfatcomputing
Copy link
Member

: struct 0 ;
: end-struct constant ;
: field create over , + does> @ + ;
: superfield create , does> @ + ;
: union 0 ;
: end-union dup constant + ;
: unionfield create max does> @ + ;

( sample usage )
struct
 4 field part1
 9 superfield part2_3_4
  4 field part2
  4 field part3
  1 field part4
 4 superfield part5_6
  2 field part5
  2 field part6
 union
   4 unionfield part7
   1 unionfield part8 
 end-union myunion-size
 union
   2 unionfield part9
 end-union myunion2-size
end-struct mystruct-size

 : buffer: create allot ;
 mystruct-size buffer: mybuf

 $AAAAAAAA mybuf part1 !
 $BBBBBBBB mybuf part2 !
 $CCCCCCCC mybuf part3 !
 $DD       mybuf part4 !
 $EEEE     mybuf part5 !
 $FFFF     mybuf part6 !

           mybuf part5 @ hex. cr  ( prints EEEE )
           mybuf part6 @ hex. cr  ( prints FFFF )

 $DEADBEEF mybuf part5_6 !
           mybuf part5_6 @ hex. cr ( prints DEADBEEF )

EDIT: Fixed to include implementations of actual unions supporing differently sized members as @forthy42 described them.

@bfox9900
Copy link
Member

bfox9900 commented Aug 1, 2018

I know this is an old post but I will add something in case it is of use to someone.
Lowfatcomputing's struct/union example is so cool. Thanks

This is not bit structures but arrays of bits, but it might provide some ideas for managing bit fields. It seems to work on 16,32 and 64 bit platforms. It is factored for understanding. It's not real fast from using /MOD at runtime, but it's not too bad with native code compilers.

https://github.com/bfox9900/CAMEL99-V2/blob/master/LIB.ITC/BOOLEAN.FTH
Edit: Fixed this link

@bfox9900
Copy link
Member

bfox9900 commented Aug 16, 2018

The obvious omission to BOOLEAN.FTH is:

: BTOGGLE ( bit# addr[] -- )
BITFLD \ -- bit# addr
SWAP BITMASK >R \ save mask
DUP @ \ -- addr bits
R> XOR SWAP ! ; \ toggle bit, store back in addr

The file has been updated on GITHub

@rdrop-exit
Copy link
Member

rdrop-exit commented Aug 25, 2018

: BTOGGLE ( bit# addr[] -- ) ...

Below is is the factoring that I use for bit toggling , it takes a mask as an argument rather than a particular bit number, so that if needed I can toggle multiple bits simultaneously in a cell.

0 shadow  Bitwise Miscellanea  4|4
1
2 set    Set <mask> bits in cell at <a>.
3
4 reset  Reset <mask> bits in cell at <a>.
5
6 toggle Xor <mask> bits in cell at <a>.

0 source  Bitwise Miscellanea  4|4
1
2 : set    ( mask a -- ) tuck @ or   \! ;inline
3 : reset  ( mask a -- ) tuck @ cand \! ;inline
4 : toggle ( mask a -- ) tuck @ xor  \! ;inline

(\! is a primitive instruction equivalent to swap !)

When I want to use a particular bit number, as is the case with your btoggle above, rather than a mask, I write <bit#> bit <addr> toggle, where bit is defined as follows:

: bit ( # -- bit ) 2** ;inline

I also have mask defined as:

 : mask ( bit -- mask ) 1- ;inline

So I can write <#bits> bit mask <addr> toggle to toggle the low <#bits>.

@bfox9900
Copy link
Member

Nice and a different way to factor it with the BIT word.
Looks very efficient.

2** is 2 to the exponent ** ?
How is that implemented?
It would seem to be similar to RSHIFT, no?

@rdrop-exit
Copy link
Member

rdrop-exit commented Aug 28, 2018

Thanks.

2** is 2 to the exponent ** ?

That's right, it raises 2 to a power.

How is that implemented?
It would seem to be similar to RSHIFT, no?

It's a primitive on my Forth, but in high-level Forth it would be equivalent to:
: 2** ( u -- 1<<u ) 1 swap lshift ;

@GarthWilson
Copy link

GarthWilson commented Aug 30, 2018 via email

@cwpjr
Copy link
Member

cwpjr commented Aug 31, 2018 via email

@cwpjr
Copy link
Member

cwpjr commented Aug 31, 2018 via email

@rdrop-exit
Copy link
Member

O.T.: Mark, is your ! word (in your post, quoted
below) in common usage (even if it's not adopted into any
standard), with that name? I ask because I use the same primitive
in my 65816 kernel, but called SWAP! . It's headerless
and is not in any applications so far, so I could easily change
the name to whatever is in common usage.
Garth

I generally use \<name> to indicate an alternative 'reversed' version of the primitive <name> where the normal argument order is either reversed or permuted in some way. It's not common usage as far as I know. It could be that I copied it from some other Forth long ago, but I can't recall.

Some other examples of this convention would be reverse subtraction \- ( x1 x2 -- x2-x1 ), and two different 2-to-1 mux primitives mux ( x1 x2 mask -- x ) and \mux ( mask x1 x2 -- x ).

@GarthWilson
Copy link

GarthWilson commented Aug 31, 2018 via email

@larsbrinkhoff
Copy link
Member

@rdrop-exit, I like that \<name> convention. I use the -<name> convention for negating or removing a "name", but \ works in another dimension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants