Use Simulink Templates for HDL Code Generation - MATLAB & Simulink (original) (raw)

HDL Coder™ model templates in Simulink® provide you with design patterns and best practices for models intended for HDL code generation. Models you create from one of the HDL Coder model templates have their configuration parameters and solver settings set up for HDL code generation. To configure an existing model for HDL code generation, use hdlsetup.

Create Model Using HDL Coder Model Template

To model hardware for efficient HDL code generation, create a model using an HDL Coder model template.

  1. Open the Simulink Start Page. In the MATLAB® Home tab, select the Simulink button. Alternatively, at the command line, enter:
  2. In the HDL Coder section, you see templates that are preconfigured for HDL code generation. Selecting the template opens a blank model in the Simulink Editor. To save the model, select > .
  3. To open the Simulink Library Browser and then open the HDL Coder Block Library, select the Library Browser button in the Simulink Editor. Alternatively, at the command line, enter
    To filter the Simulink Library Browser to show the block libraries that support HDL code generation, use the hdllib function:

HDL Coder Model Templates

Complex Multiplier

The Complex Multiplier template shows how to model a complex multiplier-accumulator and manually pipeline the intermediate stages. The hardware implementation of complex multiplication uses four multipliers and two adders.

The template applies the following best practices:

Simulink Template of Complex Multiplier.

Complex multiplier model. The model contains four multiplier blocks, top two multiplier blocks computes multiplication of real part and bottom two multiplier computes multiplication of imaginary part.

MATLAB Arithmetic

The MATLAB Arithmetic template contains MATLAB arithmetic operations that infer DSP48s in hardware.

Simulink Template of MATLAB Arithmetic System

MATLAB Arithmetic model that has five MATLAB Function blocks.

For example, the ml_mul_acc MATLAB Function block shows how to write a multiply-accumulate operation in MATLAB. hdlfimath applies fixed-point math settings for HDL code generation.

function y = fcn(u1, u2)

% design of a 6x6 multipler % same reset on inputs and outputs % followed by an adder

nt = numerictype(0,6,0); nt2 = numerictype(0,12,0); fm = hdlfimath;

persistent u1_reg u2_reg mul_reg add_reg; if isempty(u1_reg) u1_reg = fi(0, nt, fm); u2_reg = fi(0, nt, fm); mul_reg = fi(0, nt2, fm); add_reg = fi(0, nt2, fm); end

mul = mul_reg; mul_reg = u1_reg * u2_reg; add = add_reg; add_reg(:) = mul+add; u1_reg = u1; u2_reg = u2;

y = add;

ROM

The ROM template is a design pattern that maps to a ROM in hardware.

The template applies the following best practices:

Simulink template of Read-Only Memory System

LUT Based Read only memory system, which has a Direct Lookup Table block followed by a Delay block.

Register

The Register template shows how to model hardware registers:

Simulink Template of Register

Simulink model that has MATLAB Function block and Unit Delay block.

The MATLAB code in the MATLAB Function block uses a persistent variable to model the register.

function y = fcn(u) % Unit delay implementation that maps to a register in hardware

persistent u_d; if isempty(u_d) % defines initial value driven by unit delay at time step 0 u_d = cast(0, 'like', u); end

% return delayed input from last sample time hit y = u_d;

% store the current input u_d = u;

SRL

The SRL template shows how to implement a shift register that maps to an SRL16 in hardware. You can use a similar pattern to map to an SRL32.

Simulink template for SRL system

SRL Subsystem that has MATLAB function block named select_tap.

In the shift register subsystem, the Tapped Delay implements the shift operation, and the MATLAB Function, select_tap, implements the output mux.

In select_tap, the zero-based address, addr increments by 1 because MATLAB indices are one-based.

function dout = fcn(addr, tdelay) %#codegen

addr1 = fi(addr+1,0,5,0); dout = tdelay(addr1);

In the generated code, HDL Coder automatically omits the increment because Verilog®, SystemVerilog and VHDL® are zero-based.

The template also applies the following best practices for mapping to an SRL16 in hardware:

The Simulink Hardware Patterns template contains design patterns for common hardware operations:

Simulink Hardware Pattern Template

Simulink Hardware Pattern Subsystem. A subsystem contains five subsystems named, shift_reg, detect_rising_edge, detect_falling_edge, SR_latch, and RS_latch.

For example, the design patterns for rising edge detection and falling edge detection:

detect_rising_edge subsystem

detect_falling_edge subsystem

State Machine in MATLAB

The State Machine in MATLAB template shows how to implement Mealy and Moore state machines using theMATLAB Function block.

Simulink Template for State Machine

To learn more about best practices for modeling state machines, see Model a State Machine for HDL and High-Level Synthesis Code Generation.

See Also

hdlsetup | makehdl

Topics