Lab 1: FSMs
- Due 4 Apr 2023 by 17:00
- Points 1
Estimated required time, including (fast) reading about FSM modeling: 4h (part 1) + 1h (part 2)
This assignment consists of two parts. Part 1 requires you to design and simulate an FSM model using PtolemyII's Virgil. For a detailed description of how FSM modeling works in PtolemyII please consult the Using PtolemyII book, Chapter 6. Part 2 requires you to implement the FSM above into a running code in a language of your choice - we recommend you use C, since this is standard for most embedded platforms. Details follow.
Part 1
Design and simulate an FSM able to parse the network packages formed according to the description below. The packages arrive as sequences of values to a unique input and your FSM has to identify the contents and output other values as requested by the packages. The FSM should also keep track of a "routing table" storing information received via packages as described below. You should handle the following sequences:
- 0, D - is a request for the routing table contents at index D
- 1, 0, D - update the routing table at index 0 with data D
- 1, 1, 0, D - update the routing table at index 1 with data D
- 1, 1, 1, D - update the routing table at index 2 with data D
The routing table should be initialized with -1 on all positions. Note that D may be any integer in most cases.
As an example, your FSM should be able to process the following sequence:
1, 1, 0, 5, 1, 0, 3, 0, 1, 1, 0, 4, 0, 2, 0, 0
by identifying cases III, II, I, II, I, I and producing outputs 5, -1, 4
You may use, for instance, a Sequence actor (Actor/Sources/SequenceSources) to generate inputs and a Display (Actor/Sinks/GenericSinks) to show the outputs.
Extend the sequence to cover the last case, and other combinations capturing all the transitions in your FSM.
Hints:
- to check for the presence of an input S in your guards, use S_isPresent, see page 196.
- if you are using the SDF Director to run your FSM as a ModalModel, make sure you give it enough iterations in the parameters to produce all the numbers in your sequence.
- have a look at arrays in PtolemyII book, section 13.3.1, especially update function on page 465. You can use these internally to store your routing table.
Part 2
Start with the model above and write the C (alternatively Python, Rust, Julia, or VHDL - your TA might not be able to help you with all of these however) code that reads the input provided as a sequence of comma separated values (as above) and produces the outputs above and the states it goes through. You will be provided with more sequences than the one above during the lab or made available on this page later soon.
Hints:
- in C you may use switch-case statements while in Python you may use dictionaries
- make it easy to input data via a file
IMPORTANT: write your code without using an AI assistant at first. Once you have something running you may use something like ChatGPT or Copilot and check their code against yours - it is highly likely they will produce incorrect code. Try to optimize the code (reduce the number of states) using an AI assistant - do you get correct results? Is the source code still an implementation of your FSM? Make any corrections need to get a proper implementation.