system generator Mcode 使用心得

本文分享了使用 MATLAB System Generator 中的 Mcode 功能进行系统设计和仿真的经验,探讨了如何通过 Mcode 将算法转换为高效 C 代码,并在 Simulink 中进行快速原型验证。通过实例展示了 Mcode 在提升代码生成效率和优化硬件实施方面的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值