Skip to content

Commit 16c23f8

Browse files
author
Oron Port
committed
more documentation updates
1 parent 6d674d2 commit 16c23f8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/user-guide/conditionals/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,40 @@ bits match
9999
case _ => y := x
100100
```
101101

102+
6. **Bit Extractor Patterns**:
103+
```scala
104+
// Match and extract a 32-bit section between DEAD and BEEF
105+
y match
106+
case h"DEAD${secret: B[32]}BEEF" =>
107+
// Use extracted secret bits
108+
109+
// Extract multiple sections
110+
y match
111+
case h"DE${part1: B[16]}AD${part2: B[16]}BEEF" =>
112+
// Use part1 and part2 bits
113+
114+
// Store extracted bits in variables
115+
val h"DEAD${extracted: B[32]}BEEF" = input: @unchecked
116+
117+
// Extract multiple sections into variables
118+
val h"DE${first: B[16]}ADBE${second: B[16]}EF" = input: @unchecked
119+
120+
// Using guards with extracted bit fields
121+
y match
122+
case h"DEAD${secret: B[32]}BEEF" if secret > h"20000000" =>
123+
// Match when the secret section is greater than 0x20000000
124+
case h"DE${part1: B[16]}AD${part2: B[16]}BEEF" if part1 == h"FFFF" =>
125+
// Match when part1 is all ones
126+
```
127+
128+
Bit extractor patterns allow you to:
129+
- Match specific bit patterns while extracting variable sections
130+
- Use hex or binary notation for the fixed parts
131+
- Specify the width of extracted sections using `B[width]` syntax
132+
- Store extracted bits in variables for later use
133+
- Extract multiple sections in a single pattern match
134+
- Use guards to add conditions on extracted bit fields
135+
102136
### Match to If Conversion
103137

104138
During compilation, match expressions are typically converted to if-else chains for hardware implementation. For example:

0 commit comments

Comments
 (0)