MATLAB / Simulink HDL Quick Start

MATLAB / Simulink HDL Quick Start

We will use examples to explain getting started with MATLAB/Simulink HDL.

fda95b1e7a06ae175a5ff19ea8ef6bf8.jpeg

To start this project, you first need to create a new Simulink containing Stateflow. Just click anywhere in the canvas and start typing into Stateflow.

728e81d8643c361c2ec9c65e0e4ed80b.png

You should now see the Stateflow icon on the canvas. Double-click the icon to edit.

979892bd3419e293259701c6173b8f2f.png

Once in the chart editor, you can add states and transitions between states. To start, we create two states and name them "idle" and "LED". There are no conditions for conversion between them.

4851dd2314d9d3cd6ee93cf46e94f6eb.png

To add state transition conditions, double-click the transition and enter the required conditions.

765254984319cbdc1593095de1407ed9.png

Of course, a state machine can have Mealey and Moore outputs that occur as a function of the current state (Moore) or the current state and inputs (Mealey). In this example, declare the LED output as a Moore output and declare it in each state.

edeb1ee07cbb64fe0016a85eae44a982.png

The final result is shown below.

4a44b8545cf91c9b3ba8e1d812506927.png

Using the Model Browser we can define the inputs and outputs of the state machine. For the inputs, we keep them as the same types as Simulink, but need to define the outputs. Since LED requires three bits, we use type fixdt(0,3,0). This means that the vector is three bits wide, unsigned, and has no numeric fractional elements.

5910ce1cc2ebe07ee3f53e16983faa9d.png

Also, check the "Execute (input) chart on initialization" option in the Model Explorer.

9977cebafda8432abbb5d97d4299b62b.png

Navigate to the canvas above the chart. Here we need to add block IO and we will also add latency. In the canvas, start typing input or output to get the desired port.

aff463ac4b6b7a33d2bf79e711787200.png

You can also name the ports and set them to the correct type by double-clicking on the inputs and outputs.

62457b46a6d478c67a17fdec84242f54.png

Set sw_in to the same fixdt(0,3,0) as the previously declared output type. We use delays to add registers. To add additional delays, just type in the canvas.

251f1dc949726debf87d7feede34a345.png

To change the length of the delay, double-click the delay and change it to Delay 1.

e14ae30bbc374cff8ec64ccd4ba3a5eb.png

The image below shows the complete chart.

6f09b0fe1712ebde863c478ca093457e.png

Now we can generate RTL from it, but first we will create a testbench for it. Select all elements on the canvas, right-click it and choose Create Subsystem from Selection.

91c9a238dae0317104b51a5c2711541d.png

Add the step function and constants, set the block type used in the subsystem block, and make sure the sample time for discrete sampling is set to -1.

e109d5bc552fa0a108b9688dffa38899.png

Right-click on the signal of interest and select Start recording the selected signal.

74a348d1118801d05257526e2699d6fd.png

Open Model Explorer and set the model to discrete time with a fixed step timer.

3f706577a27eb5dd8cf7c9258307ef7f.png

Run the simulation and open the Data Inspector. You should see SW_ENB being set and the LED output going high after the next clock.

52f27345dfa8ee4df95230fac6600749.png

Now we can create the HDL and export it for use in Vivado. We can do this by right-clicking on the subsystem and selecting Generate HDL for Subsystem.

0daefc6ef7a06ee9dfaa6ab5f4362690.png

If you want to change the format of any generated HDL code (i.e. remove clock enablement), you need to select the Global Settings option from the HDL Code Generation tab.

28d7178552bce7192ff2d4b3deb2ac45.png

After you generate the code, you will see a message in the MATLAB window where the code was generated.

80a3339bbed4f6d6970bb7125a09daae.png

This HDL can then be imported into a Vivado project. The generated code itself is actually readable and depends on how well we annotate the Simulink diagram. For example, I can name the state diagram and this will be reflected in the case statement name.

Three VHDL files are generated: the package containing the declarations, the actual source code implementing the state machine, and the top-level file.

Synthesized in Vivado, the final design required three flip-flops and two LUTs.

14ae1f1203bd30a420ce55a12e25d334.png

Of course, this is a simple example, but be able to learn the process so that you can use it for more complex applications.

Guess you like

Origin blog.csdn.net/Pieces_thinking/article/details/134258192