You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,8 @@ The goal that BitField strives to accomplish is to allow for the creation of min
23
23
require "bitfield"
24
24
```
25
25
26
+
### Standard Definition
27
+
26
28
You can define both numeric and boolean fields. When defining a numeric field, its type is that of the class' generic type. Additionally, the entire value of the bitfield is accessible though the #value method. An example bitfield might look something like this
27
29
28
30
```crystal
@@ -44,6 +46,36 @@ bf.bool = false
44
46
bf.value # => 0x94
45
47
```
46
48
49
+
### Locking Values
50
+
51
+
Since I've primarily developed this shard for use in my emulator projects where bitfields are typically used to define IO registers, it's typical to have a register where fields may need to be locked in place in a bitfield. That is achievable by simply providing a `lock: true` argument with the field you want to lock.
52
+
53
+
```crystal
54
+
class TestLock < BitField(UInt8)
55
+
num top, 3
56
+
num mid, 2, lock: true
57
+
num bot, 3
58
+
end
59
+
```
60
+
61
+
The effect of locking a field is that it will not change when writing to bitfield's #value method. If it needs to be mutated after initialization, it will need to be through the field's specific setter method.
62
+
63
+
```crystal
64
+
bf = TestLock.new 0x00
65
+
bf.top # => 0x0
66
+
bf.mid # => 0x0
67
+
bf.bot # => 0x0
68
+
bf.value = 0xFF
69
+
bf.top # => 0x7
70
+
bf.mid # => 0x0
71
+
bf.bot # => 0x7
72
+
bf.value # => 0xE7
73
+
bf.mid = 0x3
74
+
bf.value # => 0xFF
75
+
```
76
+
77
+
### Errors
78
+
47
79
The full number of bits must be specified. For example, if you define your bitfield over a UInt8, you must specify exactly 8 bits in the field. If you fail to do so, you will get a message like this at runtime.
0 commit comments