This example shows how to use the MCode block to build an accumulator
using persistent state variables and parameters to provide implementation
flexibility. The following M-code, which defines function xl_accum
is contained in file xl_accum.m:
function q = xl_accum(b, rst, load, en, nbits, ov, op, feed_back_down_scale)
% q = xl_accum(b, rst, nbits, ov, op, feed_back_down_scale) is
% equivalent to our Accumulator block.
binpt = xl_binpt(b);
init = 0;
precision = {xlSigned, nbits, binpt, xlTruncate, ov};
persistent s, s = xl_state(init, precision);
q = s;
if rst
if load
% reset from the input port
s = b;
else
% reset from zero
s = init;
end
else
if ~en
else
% if enabled, update the state
if op==0
s = s/feed_back_down_scale + b;
else
s = s/feed_back_down_scale - b;
end
end
end
The following diagram shows a Subsystem containing the accumulator
MCode block using M-function xl_accum
. The MCode
block is labeled MCode Accumulator. The Subsystem also contains the Xilinx Accumulator block, labeled Accumulator, for
comparison purposes. The MCode block provides the same functionality as the Xilinx Accumulator block; however, its mask interface
differs in that parameters of the MCode block are specified with a cell array in the
Function Parameter Bindings parameter.
Optional inputs rst
and load
of block Accum_MCode1
are disabled in the cell array of the Function Parameter
Bindings parameter. The block mask for block MCode Accumulator is shown below:
The example contains two additional accumulator Subsystems with MCode blocks using the same M-function, but different parameter settings to accomplish different accumulator implementations.