use simulink templates for hdl code generation -凯发k8网页登录

main content

use simulink templates for hdl code generation

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 .

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:

    simulink
  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 file > save as.

  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

    sllibrarybrowser

    to filter the simulink library browser to show the block libraries that support hdl code generation, use the hdllib function:

    hdllib

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:

  • in the configuration parameters dialog box, in hdl code generation > global settings, reset type is set to synchronous.

  • to improve speed, delay blocks, which map to registers in hardware, are at the inputs and outputs of the multipliers and adders.

  • to support the output data of a full-precision complex multiplier, the output data word length is manually specified to be (operand_word_length * 2) 1.

    for example, in the template, the operand word length is 18, and the output word length is 37.

matlab arithmetic

the matlab arithmetic template contains matlab arithmetic operations that infer dsp48s in hardware.

for example, the ml_mul_acc matlab function block shows how to write a multiply-accumulate operation in matlab. 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:

  • at the output of the lookup table, there is a delay block with resettype = none.

  • the lookup table is structured such that the spacing between breakpoints is a power of two.

    using table dimensions that are a power of two enables hdl coder to generate shift operations instead of division operations. if necessary, pad the table with zeros.

  • the number of lookup table entries is a power of two. for some synthesis tools, a lookup table that has a power-of-two number of entries maps better to rom. if necessary, pad the table with zeros.

register

the register template shows how to model hardware registers:

  • in simulink, using the delay block.

  • in matlab, using persistent variables.

    this design pattern also shows how to use cast to propagate data types automatically.

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.

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® and vhdl® are zero-based.

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

  • for the tapped delay block:

    • in the block parameters dialog box, include current input in output vector is not enabled.

    • in the hdl block properties dialog box, resettype is set to none.

  • for the subsystem block, in the hdl block properties dialog box, flattenhierarchy is set to on.

simulink hardware patterns

the simulink hardware patterns template contains design patterns for common hardware operations:

  • serial-to-parallel shift register

  • detect rising edge

  • detect falling edge

  • sr latch

  • rs latch

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

state machine in matlab

the state machine in matlab template shows how to implement mealy and moore state machines using the matlab function block.

to learn more about best practices for modeling state machines, see .

see also

|

related topics

    网站地图