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

Fix defaultNextState diagram generation in FSM #454

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions lib/src/finite_state_machine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,19 @@ class FiniteStateMachine<StateIdentifier> {

for (final state in _states) {
for (final entry in state.events.entries) {
figure.addTransitions(state.identifier.toString(),
entry.value.toString(), entry.key.name);
figure.addTransitions(
state.identifier.toString(),
entry.value.toString(),
entry.key.name,
);
}

if (state.defaultNextState != state.identifier) {
figure.addTransitions(
state.identifier.toString(),
state.defaultNextState.toString(),
'(default)',
);
}
}
figure.writeToFile();
Expand Down
51 changes: 51 additions & 0 deletions test/fsm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ class TestModule extends Module {
}
}

class DefaultStateFsmMod extends Module {
late final FiniteStateMachine<MyStates> _fsm;
DefaultStateFsmMod(Logic reset) {
reset = addInput('reset', reset);
final clk = SimpleClockGenerator(10).clk;
final b = addOutput('b', width: 8);

_fsm = FiniteStateMachine<MyStates>(clk, reset, MyStates.state1, [
State(
MyStates.state1,
events: {Const(0): MyStates.state2},
actions: [b < 1],
defaultNextState: MyStates.state3,
),
State(MyStates.state2, events: {}, actions: [b < 2]),
State(MyStates.state3,
events: {}, actions: [b < 3], defaultNextState: MyStates.state4),
State(MyStates.state4, events: {}, actions: [b < 4]),
]);
}
}

enum LightStates { northFlowing, northSlowing, eastFlowing, eastSlowing }

enum TrafficPresence {
Expand Down Expand Up @@ -226,6 +248,35 @@ void main() {
verifyMermaidStateDiagram(_simpleFSMPath);
});

test('default next state fsm', () async {
final pipem = DefaultStateFsmMod(Logic());

await pipem.build();

final vectors = [
Vector({'reset': 1}, {}),
Vector({'reset': 0}, {'b': 1}),
Vector({'reset': 0}, {'b': 3}),
Vector({'reset': 0}, {'b': 4}),
Vector({'reset': 0}, {'b': 4}),
];
await SimCompare.checkFunctionalVector(pipem, vectors);
SimCompare.checkIverilogVector(pipem, vectors);

if (!kIsWeb) {
const fsmPath = '$_tmpDir/default_next_state_fsm.md';
pipem._fsm.generateDiagram(outputPath: fsmPath);

final mermaid = File(fsmPath).readAsStringSync();
expect(mermaid, contains('state2'));
expect(mermaid, contains('state3'));
expect(mermaid, contains('state4'));
expect(mermaid, contains('(default)'));

verifyMermaidStateDiagram(fsmPath);
}
});

test('traffic light fsm', () async {
final pipem = TrafficTestModule(Logic(width: 2), Logic());
await pipem.build();
Expand Down