I'm trying to split a state machine into several traits, so one trait per state. Each state has a substate machine, so the division would make the code more readable for me.
Actually I'm failing at the first when statement.
My actual Test Code looks like this:
class A extends Module with B{
val io = IO(
new Bundle{
val state = Output(UInt(2.W))
val substate = Output(UInt(2.W))
}
)
lazy val state = RegInit(0.U(2.W))
lazy val substate = RegInit(0.U(2.W))
io.state := state
io.substate := substate
}
trait B{
def state : UInt
def substate : UInt
when(state === 0.U){
state := 1.U
substate := ~substate
}
}
When generating the verilog code it ignores the when for the toggling of the substate. So the verilog "always" with the assignments looks like this:
assign _T_7 = state == 2'h0; // @[traittest.scala 25:16:@9.4]
assign _T_11 = ~ substate; // @[traittest.scala 27:21:@13.6]
always @(posedge clock) begin
if (reset) begin
state <= 2'h0;
end else begin
if (_T_7) begin
state <= 2'h1;
end
end
if (reset) begin
substate <= 2'h0;
end else begin
substate <= _T_11;
end
end
I tried it with the override and protected modifiers for the state and substate val's but it doesn't make a difference and I have no other idea how to get it working. Perhaps traits aren't the right constructs for that?