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

4 views (last 30 days)
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??

Accepted Answer

Tim McBrayer
Tim McBrayer on 17 Dec 2013
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.
  2 Comments
Sriparna Boote
Sriparna Boote on 18 Dec 2013
Thank you sir! One more thing to know, is it possible to genearte a test bench code for the comand randint (1,1,2)?
Tim McBrayer
Tim McBrayer on 18 Dec 2013
You can test anything; the issue is that you need to either know the expected results in advance, or be able to calculate them on the fly via a different method.
Using your example of randint: this function is not supported by HDL Coder for code generation. I don't know the details of randint, but I will assume it's a deterministic pseudorandom generator. In this case your testbench could run randint and store the results before invoking your design for hardware. Then, it would execute your design and compare the generated results to the MATLAB implementation of randint.
If randint is nondeterministic then your problem is more difficult. You cannot then compare the individual values. You could, perhaps, gather statistical data to confirm that your nondeterministic algorithm met the expected nature of your design (min, max, median, distribution, etc.)

Sign in to comment.

More Answers (2)

Sriparna Boote
Sriparna Boote on 17 Dec 2013
Thank you sir!!.. One more thing, is it possible to generate a test bench code for random integer (0 ,1) ??

prathap soma
prathap soma on 16 Jun 2017
using HDL coder can I convert my matlab code which is on image processing side. if yes, then how would i give testbench file to coder. can any one help because am new to HDL code generator.

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!