MATLAB: How to make a test bench file for a matlab m file?

HDL Coder

I have a matlab file. and now i want to convert it into a vhdl code. for i need to have a test bench. Please help me because i don't know how to code a test bench??

Best Answer

A testbench is just MATLAB code that provides input stimulus to your design, and also reads and verifies the outputs. A good testbench should exercise your design with enough input data so that you are confident that your implementation is correct.
For example, assume your design is a new way to add 2 8-bit numbers. Your inputs A and B are 8-bit values, and you have a single 9-bit output. A poor testbench would make a single call to your function:
result = myAdd(int8(2), int8(3));
if result ~= 5
<report error>
end
A better testbench for this case, since the inputs are small, could run loops across both inputs and test each possible pair of inputs.
for aa = 0:255
for bb = 0:255
result = myAdd(int8(aa), int8(bb));
if result ~= aa + bb
<report error>
end
end
end
With a more complicated design you cannot do exhaustive testing like this in a reasonable time. You can design your testbench to test expected problem areas, extreme values, corner cases, use random data, etc. It's up to you on what to do.
The idea is to run your code in MATLAB and make sure that it performs as you expect it to. HDL Coder can use your testbench and verify that the HDL you generate behaves identically to the original MATLAB code in a 3rd party HDL simulator or even when implemented on a target FPGA board.