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

whenBuilder document #257

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions source/SpinalHDL/Semantic/when_switch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,61 @@
// Execute when (not cond1) and (not cond2)
}


WhenBuilder
------

Check warning on line 44 in source/SpinalHDL/Semantic/when_switch.rst

View workflow job for this annotation

GitHub Actions / test

Title underline too short.

Check warning on line 44 in source/SpinalHDL/Semantic/when_switch.rst

View workflow job for this annotation

GitHub Actions / test

Title underline too short.

Check warning on line 44 in source/SpinalHDL/Semantic/when_switch.rst

View workflow job for this annotation

GitHub Actions / build

Title underline too short.

Check warning on line 44 in source/SpinalHDL/Semantic/when_switch.rst

View workflow job for this annotation

GitHub Actions / build

Title underline too short.

Sometimes we need to generate some parameters for the when condition,
and the original structure of when else is not very suitable.
Therefore, we provide a 'whenBuilder' method to achieve this goal

.. code-block:: scala

import spinal.lib._

val conds = Bits(8 bits)
val result = UInt(8 bits)

val ctx = WhenBuilder()
ctx.when(conds(0)) {
result := 0
}
ctx.when(conds(1)) {
result := 1
}
if(true){
ctx.when(conds(2)) {
result := 2
}
}
ctx.when(conds(3)) {
result := 3
}

Compared to the when/elsewhen/otherwise approach, it might be more convenient for parameterization.
we can also use like this

.. code-block:: scala

for(i <- 5 to 7) ctx.when(conds(i)) {
result := i
}

ctx.otherwise{
result := 255
}

switch(addr) {
for (i <- addressElements ) {
is(i) {
rdata := buffer(i)
}
}
}

This way, we can parameterize priority circuits similar to how we use 'foreach' inside 'switch()', and generate code in a more intuitive if-else format.


Switch
------

Expand Down
Loading