-
Notifications
You must be signed in to change notification settings - Fork 4
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
Comments
Flags are bits in a cell in Forth. A Nybble is 4 bits. Depends on where On Tue, Dec 29, 2015 at 10:31 PM, Rick Carlino notifications@github.com
|
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. |
My ARM Forth includes: SETBITS [SETBITS](addr val --) On Tue, Dec 29, 2015 at 11:23 PM, Dennis Ruffer notifications@github.com
|
Also: [CLRBITS](addr value) On Tue, Dec 29, 2015 at 11:23 PM, Dennis Ruffer notifications@github.com
|
Forth83 has |
I suppose one could come up with a design to support something like this:
|
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). |
I like superbits. I feel they should have the superpower to solve any programming problem! |
Am Mittwoch, 30. Dezember 2015, 00:06:17 schrieb Lars Brinkhoff:
Since we have no nested structs, a bitfield should just be an entity of its Union is also just some individual structures, and the size of the union is Bernd Paysan |
EDIT: Fixed to include implementations of actual |
I know this is an old post but I will add something in case it is of use to someone. 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 |
The obvious omission to BOOLEAN.FTH is: : BTOGGLE ( bit# addr[] -- ) The file has been updated on GITHub |
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.
( When I want to use a particular bit number, as is the case with your
I also have
So I can write |
Nice and a different way to factor it with the BIT word. 2** is 2 to the exponent ** ? |
Thanks.
That's right, it raises 2 to a power.
It's a primitive on my Forth, but in high-level Forth it would be equivalent to: |
Lacking better names and not finding
any names or conventions others might have used, many years ago I
defined primitives C_OR_BITS and C_ANDBITS,
both with stack effect ( addr mask -- ), for setting or
clearing bits in a byte at an address. The mask for ANDing was a
normal AND mask, so for example if you wanted to clear only one
bit, there would be seven '1' bits and one '0' bit. I put the
extra _ in C_OR_BITS name for better vertical alignment
and visual factoring in situations like the following (which are
from an actual hardware setup word:
VIA2ACR 11000011 C_ANDBITS \ Reset shift register.
VIA2ACR 00001100 C_OR_BITS \ Set T2 & SR modes.
VIA2PCR 00001110 C_OR_BITS \ Set CA2 as high
output for handshake.
(obviously with BASE in binary at this point).
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
On 08/25/2018 11:07 AM, Mark W. Humphries wrote:
: BTOGGLE ( bit# addr[] -- ) ...
Below is is the version of bit toggling I use, 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 rather than a mask
as is the case with your btoggle above, I write <bit#>
bit mask <addr> toggle, where bit
and mask are defined as follows:
: bit ( # -- bit ) 2** ;inline
: mask ( bit -- mask ) 1- ;inline
|
I have SETBITS, ANDBITS and CLRBITS that take ones in the 32 but word stack
as the operative for the words.
On Thu, Aug 30, 2018 at 1:42 PM Garth Wilson <notifications@github.com>
wrote:
…
Lacking better names and not finding
any names or conventions others might have used, many years ago I
defined primitives C_OR_BITS and C_ANDBITS,
both with stack effect ( addr mask -- ), for setting or
clearing bits in a byte at an address. The mask for ANDing was a
normal AND mask, so for example if you wanted to clear only one
bit, there would be seven '1' bits and one '0' bit. I put the
extra _ in C_OR_BITS name for better vertical alignment
and visual factoring in situations like the following (which are
from an actual hardware setup word:
VIA2ACR 11000011 C_ANDBITS \ Reset shift register.
VIA2ACR 00001100 C_OR_BITS \ Set T2 & SR modes.
VIA2PCR 00001110 C_OR_BITS \ Set CA2 as high
output for handshake.
(obviously with BASE in binary at this point).
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
On 08/25/2018 11:07 AM, Mark W. Humphries wrote:
: BTOGGLE ( bit# addr[] -- ) ...
Below is is the version of bit toggling I use, 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 rather than a mask
as is the case with your btoggle above, I write <bit#>
bit mask <addr> toggle, where bit
and mask are defined as follows:
: bit ( # -- bit ) 2** ;inline
: mask ( bit -- mask ) 1- ;inline
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#20 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFC6xaE-iZU9e5umbRC7VBNeyg_l690Fks5uWDIpgaJpZM4G8pFB>
.
|
yep
…On Mon, Aug 27, 2018 at 9:39 AM theBF ***@***.***> wrote:
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?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#20 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFC6xfQDbpPFTgAhaWQH8Y2jlb4PELOCks5uVAS3gaJpZM4G8pFB>
.
|
I generally use Some other examples of this convention would be reverse subtraction |
Darn. The formatting is sure getting messed up in the replies, and even
in my own post! To see if it was just in the emails, I checked also on
the github page, and that was messed up too.
|
@rdrop-exit, I like that |
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.
The text was updated successfully, but these errors were encountered: