MATLAB: Does the Legacy Code Tool not generate code that supports Sample Time Offset of zero in Simulink 6.5 (R2006b)

simulink

When I execute the following:
def = legacy_code('initialize');
def.SourceFiles = {'doubleIt.c'};
def.HeaderFiles = {'doubleIt.h'};
def.SFunctionName = 'ex_sfun_doubleit';
def.OutputFcnSpec = 'double y1 = doubleIt(double u1)';
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def);
Then simulate the attached model lct1.mdl, there is a difference in output of the S-function generated (ex_sfun_doubleit.c) and the gain block.
ex_sfun_doubleit.c (the generated S-function) has the following lines of code:
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
}

Best Answer

The generated S-function has a sample time offset which is "FIXED_IN_MINOR_STEP_OFFSET", however, the Gain block does not have sample time offset. This is the reason for difference in output.
The ability to generate an S-function with sample time offset as 0 is not available in Legacy Code Tool in Simulink 6.5 (R2006b).
To work around the issue change the following:
ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET)
To:
ssSetOffsetTime(S, 0, 0)
in the generated S-function. Save the file and then execute the following:
legacy_code('compile', def);