Mcode使用非常重要。当我们编写好 function 时候,保存相应的.M 文件,然后我们可以新建 simulink ,然后在加入 Mcode 模块,然后进行相应的函数设置,将函数的输入端口连接 in1,
在函数的输出端口加out1,然后加入 system generator token , run ,然后就可以在 命令行调用该函数了。
%%%%%%%%%%%%%%%%%% example function body %%%%%%%%%%%%%%%
function [z1, z2, z3, z4] = xlSimpleArith(a, b)
% xlSimpleArith demonstrates some of the arithmetic operations
% supported by the Xilinx MCode block. The function uses xfix()
% to create Xilinx fixed-point numbers with appropriate
% container types.%
% You must use a xfix() to specify type, number of bits, and
% binary point position to convert floating point values to
% Xilinx fixed-point constants or variables.
% By default, the xfix call uses xlTruncate
% and xlWrap for quantization and overflow modes.
% const1 is Ufix_8_3
const1 = xfix({xlUnsigned, 8, 3}, 1.53);
% const2 is Fix_10_4
const2 = xfix({xlSigned, 10, 4, xlRound, xlWrap}, 5.687);
z1 = a + const1;
z2 = -b - const2;
z3 = z1 - z2;
% convert z3 to Fix_12_8 with saturation for overflow
z3 = xfix({xlSigned, 12, 8, xlTruncate, xlSaturate}, z3);
% z4 is true if both inputs are positive
z4 = a>const1 & b>-1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% xfix() function %%%%%%%%%%%%%%%%%%%%%%%
One thing worth discussing is the xfix function call. The function requires two arguments:
the first for fixed-point data type precision and the second indicating the value. The
precision is specified in a cell array. The first element of the precision cell array is the type
value. It can be one of three different types: xlUnsigned, xlSigned, or xlBoolean. The
second element is the number of bits of the fixed-point number. The third is the binary
point position. If the element is xlBoolean, there is no need to specify the number of bits
and binary point position. The number of bits and binary point position must be specified
in pair. The fourth element is the quantization mode and the fifth element is the overflow
mode. The quantization mode can be one of xlTruncate, xlRound, or xlRoundBanker.
The overflow mode can be one of xlWrap, xlSaturate, or xlThrowOverflow.
Quanitization mode and overflow mode must be specified as a pair. If the
quantization-overflow mode pair is not specified, the xfix function uses xlTruncate and
xlWrap for signed and unsigned numbers. The second argument of the xfix function can
be either a double or a Xilinx fixed-point number. If a constant is an integer number, there
is no need to use the xfix function. The Mcode block converts it to the appropriate
fixed-point number automatically.
在函数的输出端口加out1,然后加入 system generator token , run ,然后就可以在 命令行调用该函数了。
%%%%%%%%%%%%%%%%%% example function body %%%%%%%%%%%%%%%
function [z1, z2, z3, z4] = xlSimpleArith(a, b)
% xlSimpleArith demonstrates some of the arithmetic operations
% supported by the Xilinx MCode block. The function uses xfix()
% to create Xilinx fixed-point numbers with appropriate
% container types.%
% You must use a xfix() to specify type, number of bits, and
% binary point position to convert floating point values to
% Xilinx fixed-point constants or variables.
% By default, the xfix call uses xlTruncate
% and xlWrap for quantization and overflow modes.
% const1 is Ufix_8_3
const1 = xfix({xlUnsigned, 8, 3}, 1.53);
% const2 is Fix_10_4
const2 = xfix({xlSigned, 10, 4, xlRound, xlWrap}, 5.687);
z1 = a + const1;
z2 = -b - const2;
z3 = z1 - z2;
% convert z3 to Fix_12_8 with saturation for overflow
z3 = xfix({xlSigned, 12, 8, xlTruncate, xlSaturate}, z3);
% z4 is true if both inputs are positive
z4 = a>const1 & b>-1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% xfix() function %%%%%%%%%%%%%%%%%%%%%%%
One thing worth discussing is the xfix function call. The function requires two arguments:
the first for fixed-point data type precision and the second indicating the value. The
precision is specified in a cell array. The first element of the precision cell array is the type
value. It can be one of three different types: xlUnsigned, xlSigned, or xlBoolean. The
second element is the number of bits of the fixed-point number. The third is the binary
point position. If the element is xlBoolean, there is no need to specify the number of bits
and binary point position. The number of bits and binary point position must be specified
in pair. The fourth element is the quantization mode and the fifth element is the overflow
mode. The quantization mode can be one of xlTruncate, xlRound, or xlRoundBanker.
The overflow mode can be one of xlWrap, xlSaturate, or xlThrowOverflow.
Quanitization mode and overflow mode must be specified as a pair. If the
quantization-overflow mode pair is not specified, the xfix function uses xlTruncate and
xlWrap for signed and unsigned numbers. The second argument of the xfix function can
be either a double or a Xilinx fixed-point number. If a constant is an integer number, there
is no need to use the xfix function. The Mcode block converts it to the appropriate
fixed-point number automatically.