MATLAB: How to influence order of automatic generated C-Code from Simulink Model

order in c-codes-functionssimulink coder

Hallo,
I have a simulink-model containing normal math operations and s-functions as input and outputs to hardware.
Lets say two s-funtions give in1 and in2, a multiplication calculates mult1 = in1 * in2, a s-function writes the value of mult1.
I would expect the following c-code from my model:
/* S-Function (nios_mm_read) Block: <Root>/in1 */
MEM_FAST_READ(simulink_model.in1, short, IN1_BASE);
/* S-Function (nios_mm_read) Block: <Root>/in2 */
MEM_FAST_READ(simulink_model.in2, short, IN2_BASE);
/* product */
simulink_model.Product = simulink_model.in1 *
simulink_model.in2 ;
/* S-Function (nios_mm_write) Block: <Root>/Product */
MEM_FAST_WRITE(simulink_model.Product, short, OUT_BASE);
Unfortunately the order functions is mixed up after generating the c-code from the model, so that, in my minimal example, the multiplication is caclulated, before the actual values are read via the s-functions.
Is there any possibility to influence the order appearance of the blocks in the generated code?
Thanks and regards

Best Answer

Yes, there is a block sorted order in all blocks in Simulink. This documentation link talks about block sorted order at great length.
If Simulink "doesn't care" which blocks runs first, i.e., if there are no real dependencies and the order of blocks is arbitrary, you can modify the priority of each block which MAY affect its sorted order. The "Priority" section in this link discusses this.
When you generate code from a model, the lines of code should directly match the block sorted order. So, you can enable the sorted order display in your model to verify how your code will be arranged.
- Sebastian