# Implement Divide Block with Control Signals

This example shows how to implement the control-signal based Divide block and use it to generate HDL code.

### Open and Run Simulink Model

Specify the input data as a linear sweep. You can change these values according to your requirements.

divisor_input = fi(1/2^17:1/2^17:1,0,18,17)';
dividend_input = fi((1/2^17 + 1/2^16):1/2^17:1,0,18,17)';
divisor_input_size = size(divisor_input);
dividend_input_size = size(dividend_input);
if(divisor_input_size(1) > dividend_input_size(1))
data_size = dividend_input_size(1);
else
data_size = divisor_input_size(1);
end
dividend_input = dividend_input(1:data_size,1);
divisor_input =  divisor_input(1:data_size,1);

Specify the word length for fixed-point datatypes and the latency for the model. For more information on the latency calculation, see Divide.

WL_divide = 18;  divide_latency = 39;

Open the hdlcoder_divide_shiftadd_control model and specify a stop time sufficient to process all the input combinations.

stoptime_divide = length(dividend_input)-1+divide_latency;

The figure shows waveform when you simulate the above model. The dataOut output is valid when validOut is 1.

### Validate Simulink Output By Using Reference Output

To validate the output of the Simulink model, compare this output with a reference value. Compute the reference output by using the divide function.

ref_divide = double(dividend_input)./double(divisor_input);

Use logical indexing to extract valid output.

Plot the comparison results by using the comparison_plot_divide function. The maximum error value is significantly smaller than the output of the model.

comparison_plot_divide(ref_divide,implementation_divide,11,'divide linear input');
Maximum Error divide linear input 1.525844e-05
Maximum PctError divide linear input 1.525786e-03

### Generate HDL Code for Divide Implementation

Check HDL settings of the model by using the hdlsaveparams function.

%% Set Model 'hdlcoder_divide_shiftadd_control' HDL parameters

% Set SubSystem HDL parameters

% Set SubSystem HDL parameters

% Set SubSystem HDL parameters

To generate HDL code for the Divide block in the model, use makehdl function.

close all;
### Using the config set for model <a href="matlab:configset.showParameterGroup('hdlcoder_divide_shiftadd_control', { 'HDL Code Generation' } )">hdlcoder_divide_shiftadd_control</a> for HDL code generation parameters.
### Running HDL checks on the model 'hdlcoder_divide_shiftadd_control'.
### Begin compilation of the model 'hdlcoder_divide_shiftadd_control'...
### Begin compilation of the model 'hdlcoder_divide_shiftadd_control'...
### Working on the model 'hdlcoder_divide_shiftadd_control'...
### <a href="matlab:configset.internal.open('hdlcoder_divide_shiftadd_control','AdaptivePipelining')">'AdaptivePipelining'</a> is set to 'Off' for the model. 'AdaptivePipelining' can improve the achievable clock frequency and reduce the area usage on FPGA boards. To enable adaptive pipelining, please set the option to 'On'. When adaptive pipelining is enabled, it inserts pipeline registers to create patterns that efficiently map blocks to DSP units on the target FPGA device.
### <a href="matlab:configset.internal.open('hdlcoder_divide_shiftadd_control','LUTMapToRAM')">'LUTMapToRAM'</a> is set to 'On' for the model. This option is used to map lookup tables to a block RAM in hardware.  To disable pipeline insertion for mapping lookup tables to RAM, please set the option to 'Off'.
### Working on... <a href="matlab:configset.internal.open('hdlcoder_divide_shiftadd_control', 'GenerateModel')">GenerateModel</a>
### Begin model generation 'gm_hdlcoder_divide_shiftadd_control' ....
### Rendering DUT with optimization related changes (IO, Area, Pipelining)...
### Model generation complete.
### Begin VHDL Code Generation for 'hdlcoder_divide_shiftadd_control'.
### Working on... <a href="matlab:configset.internal.open('hdlcoder_divide_shiftadd_control', 'Traceability')">Traceability</a>