HDL coder generates VHDL or Verilog from Simulink or MATLAB that you can use to target FPGA or ASIC Hardware
So we get asked a lot how to target a MATLAB algorithm directly to an FPGA
The short answer is that you will most likely need to adapt your algorithm to work on a stream of bits and manage that stream
Because in Hardware you have this fixed set of resources and data gets clocked in one bit at a time once per clock cycle
Whereas in your MATLAB algorithm you can easily perform operations on large sets of data and hardware you need to figure out how to perform the same calculations on this stream of bits while coordinating the timing of it all and still get the right answer
You will be able to parallelize calculations but you also need to be aware that hardware is a fixed set of resources
So you will need to trade off parallel throughput versus resource usage
And managing your data word lengths is key on hardware because as they get larger you consume more resources
In the case of an FPGA which is a prefixed set of resources that you map to, multiplications are mapped to DSP slices which typically take inputs of 18 by 18 or 25 by 18
So if your word sizes grow beyond that, a multiplication operation on them would be mapped to multiple DSP slices and you can use them up pretty quickly
Storing data is similar, you need to pay attention to your ward length and how many words you store because Ram sizes will need to be pre allocated for the max needed by your algorithm
So while MATLAB is the environment to manipulate large sets of data explore mathematics and visualize data
Simulink is a great environment for adding the implementation detail that you need to describe how your algorithm will work in streaming hardware
You can still include MATLAB code within Simulink, but Simulink lets you express and visualize parallel architecture,
it has a built in sense of timing and can show you how data types and sizes propagating through calculations, which are all key aspects of creating a good hardware architecture for your algorithm
So our recommended workflow based on our experience across thousands of customer projects over the past ten plus years is to use your MATLAB algorithm as a golden reference
This is what we'll use to verify that your hardware implementation gets the right answers
Add some implementation detail to your MATLAB algorithm
The step is optional but it helps in a lot of cases you will see in the example design we use
Then move to Simulink to build a hardware architecture for your algorithm
Convert that to fixed point in Simulink
Converting the fixed point can be done in MATLAB before you move to Simulink, but we'll show some advantages of doing this in Simulink
Use Simulink visualization to optimize your hardware architecture before you generate HDL code and then of course to verify that everything works correctly according to your golden reference
To illustrate, let's look at an algorithm that detects a pulse in a wireless signal
In our MATLAB code, we start by generating the pulse we want to detect, which we will insert into our transmitted signal and add some random noise to that that we might see when we receive it
Now to perform the operation in MATLAB, we set up the coefficients for our match filter and apply the filter on our receive signal and detect the peak value in its location
This operates on the entire signal at once which is a strength of MATLAB, but it's not our hardware works
So we'll have to create an algorithm it operates on the signal as it streams in,
and for that we will slide a window to detect the peak value of the last 11 samples
Now for a lot of designs we can go straight to Simulink and start designing the hardware architecture
but in this case since we are actually changing the algorithm to one that finds a global peak by looking at a bunch of local Peaks
We actually develop and test that in MATLAB to make sure it gets us the answer we're looking for before we move to Simulink
So this algorithm will store the last 11 samples and look at the middle sample and compare to the other 10 to see if it's the largest and if so whether it's larger than a threshold that we specify
In getting that threshold right is key for identifying that global peak versus all the local ones will get as we slide the window
You might be wondering if we've created this hardware friendly algorithm in MATLAB why can't we just generate HDL from it
Well let's go to simulate totake a look at some of the hardware details that Simulink helps us with
The first thing is the data coming in, in MATLAB we took the whole signal and ran a for loop on it to look at each 11 sample window
Hardware operating in the real world is just reacting to a stream of bits coming in at each clock cycle
Simulink has a built-in notion of timing so here we will pull the value from the MATLAB workspace variable, and we'll specify to stream it in a sample at a time
And then we convert it to be a fixed point data type, signed with a total word length of 18 bits with 16 of those being precision bits
But it's a complex number so there are real and imaginary components
And one of the other things you need to worry about is whether the signal is even valid data so we use a valid signal here and for this test we just tie it to a constant one to specify that all the data we're giving it is valid
For our matched filter, we can just use a standard digital filter block
Simulink has over 250 blocks that support HDL code generation and many like this filter have multiple implementation options
You can also embed MATLAB functions for control logic such as state machines
And here we use the coefficient variable that we set in our MATLAB algorithm
So you can use the two environments together pretty seamlessly
Now the word size of the filters output is the result of each multiplication in the filter preserving all the bits which Simulink does by default
But for our design, we really only need 18 bits so we specify that before if the data goes into the subsystem where we do multiplication
So now we can fit each multiply operation on one DSP slice
in adding those delays which map to registers and hardware not only helps us to synchronize signals to make sure that they arrive at operations at the same time, but also help us model something that maps to a DSP slice which has registers at its input and output
Here we also parallel lies the squaring of the real and imaginary values so we can perform them both in a single clock cycle
Now in the peak detector subsystem, we have this tapped delay line here with 11 delays
This is how we're going to store the 11 sample window and be able to process them in parallel
Here's where we pull out the middle sample and subtract it from each of the other samples in the window to see if it's the largest and in parallel we also check that it's also larger than our threshold
You'll also notice that we add the same amount of delays to the parallel valid signal
and the square sinks you see scattered throughout our passing values to work space variables so we can access those with our MATLAB code
Back to the MATLAB, after we calculate the golden reference results, we just call the Simulink model which again pulls data from the MATLAB our X variable and writes the results back into MATLAB variables via those workspace blocks
Now we can compare the results, looking at the amount of error between our fixed point Hardware implementation and the double precision MATLAB algorithm
So if we run this MATLAB script, it performs all these calculations and visualizes the results right in MATLAB
So this workflow leverages the strength of each environment
MATLAB's ability to easily manipulate large data sets explore mathematics and visualize data that's great for algorithm development and testing
and Simulink ability to describe and visualize parallel architectures, events that happen over time and the propagation of data types and sizes through the architecture
and the two environments work together to allow you to take advantage of the strengths of each and verify against each other
So if you're looking to deploy your MATLAB algorithm to an FPGA or ASIC, the best path is to refine and deploy it through Simulink connected back to your MATLAB golden reference