Strange combinational circuit

86 views
Skip to first unread message

Martin Schoeberl

unread,
Feb 15, 2013, 8:56:54 PM2/15/13
to chisel...@googlegroups.com
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.

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 ;-)

Anyway, if one would like to look into this, here is a small example to reproduce the issue
and look it up with gtkwave:

git clone git://github.com/schoeberl/comphdl.git
cd comphdl/chisel
make test MAIN=error.CombErrMain
gtkwave ErrorContainer.vcd

Cheers,
Martin

Jonathan Bachrach

unread,
Feb 16, 2013, 8:08:03 AM2/16/13
to chisel...@googlegroups.com
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.

> git clone git://github.com/schoeberl/comphdl.git
> cd comphdl/chisel
> make test MAIN=error.CombErrMain
> gtkwave ErrorContainer.vcd
>
> Cheers,
> Martin
>
> --
> 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.
>
>

Martin Schoeberl

unread,
Feb 16, 2013, 1:24:48 PM2/16/13
to chisel...@googlegroups.com

On 16 Feb 2013, at 05:08, Jonathan Bachrach <jackba...@gmail.com> wrote:

> 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.

I understand. I was just playing around to get this ? : expression.

So is there a chance to actually make

val x = when ...

a compile error instead of 'interesting' behavior?

> 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.

Ok, then it is (3) and I did something completely odd ;-)

I think it came from the Scala way that you have just an expression for
a return value from a function and you can write:
if xxx then y else z

>> 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.

I just wanted to avoid the Mux as it is not so clear which
value is returned when. First when the expression is true, or
when it is 0.

Martin
Reply all
Reply to author
Forward
0 new messages