On Feb 15, 2013, at 5:56 PM, Martin Schoeberl <
mar...@jopdesign.com> wrote:
> Hi all,
>
> I have following Chisel code:
>
> val limit = r1 === CNT_MAX
> val tickb = when(limit) { Bits(1) } .otherwise { Bits(0) }
>
> The intention is that this resembles the C/Java expression: val = cond ? trueVal : falseVal.
> I know that one can use a Mux or in this case directly tick = limit. However, this code
> compiles and it does something.
"when" is not an expression but a statement and is not supposed to be used to return a value.
instead it is meant for doing conditional updates (e.g., a := b).
you need to use Mux for expressions.
you can use when to do a counter by doing:
val limit = r1 === CNT_MAX
when (limit) { c := c + Bits(1) }
or
val limit = r1 === CNT_MAX
val tickb = Mux(limit, Bits(1), Bit(0))
c := c + tickb
> More interesting is looking at two versions:
>
> val limit = r1 === CNT_MAX
>
> val ticka = when(limit) { Bits(0) } .otherwise { Bits(1) }
> val tickb = when(limit) { Bits(1) } .otherwise { Bits(0) }
>
> Both variables (ticka and tickb) get the same value: 0 when limit is true, 1 when false.
>
> So either:
> (1) this is code that should not be legal Chisel code, then it should not be accepted by the compiler; or
> (2) this is an error in Chisel; or
> (3) I'm completely off and don't know what I do and don't understand all about Chisel/Scala ;-)
i've never seen this error before because once you know that you can't use "when" for an expression then you just don't do this.
> Anyway, if one would like to look into this, here is a small example to reproduce the issue
> and look it up with gtkwave:
where did you get the idea that when returns a value?
let me know and i'll correct the documentation.
> --
> You received this message because you are subscribed to the Google Groups "chisel-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
chisel-users...@googlegroups.com.
> To post to this group, send email to
chisel...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>