⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sf_tx.c

📁 CSMA / CD simulation code, Gigabit Ethernet previous versions used to compete for a unified approach
💻 C
字号:
/* * * Filename:    dspSFunction.c * * Module: * * Description: Discrete-time S-function template.  This is a simple *              S-function using typical options for a DSP (discrete-time) *              algorithm implementation.  All it does is to multiply *              each element of the input vector by 2. * *              Most of the housekeeping is done using standard macros *              defined in the Mathworks header files.  These act on the *              Block's SimStruct data structure S, which holds the block's *              parameters and other option settings. * *              The standard format for macros is along the lines of - * *                 ssSetInputPortWidth(S,0,1); * *              - which sets input port 0 to a width of 1.  Input and *              output ports are always referenced using 0-based indexes. * * Compilation * notes:       Compile using the command: mex <filename>  at the MATLAB *              command line.  The mex utility will produce an executable *              .dll file (for PC platforms), which must be located in a *              directory on the MATLAB path at simulation runtime.  Note *              that mex will place the .dll into the current MATLAB working *              directory at compile time, which may not be the directory *              containing the original source file!  You may first need to *              configure the mex utility using mex -setup at the MATLAB *              command line. * * Design ref: * * Author:      Chris Thorpe, Cambridge Control Ltd * * Version:     1.1 * * Date: * * *//* The name of the S-function must be #defined the same as the filename */#define S_FUNCTION_NAME sf_tx/* This sets the format of the S-function: always use S_FUNCTION_LEVEL_2 */#define S_FUNCTION_LEVEL 2/* The simstruc.h header file is always required */#include "simstruc.h"/* -----DIRECTIVES----------------------------------------------- *//* -----OPTIONAL FUNCTIONS--------------------------------------- */#define MDL_START#if defined MDL_STARTstatic void mdlStart(SimStruct *S) {    /* Initialise array index for reading off outputs*/    ssGetIWork(S)[0] = 0;}#endif /* MDL_START *//* -----MANDATORY FUNCTIONS-------------------------------------- *//* mdlInitializeSizes() is called at simulation startup and whenever the   block diagram is updated.  It contains general initialisation settings. */static void mdlInitializeSizes(SimStruct *S){   /* Set number of input ports (one) */   ssSetNumInputPorts               (S,2);   /* Set input port width (DYNAMICALLY_SIZED adjusts it to match input signal) */   ssSetInputPortWidth              (S,0,DYNAMICALLY_SIZED);   ssSetInputPortWidth              (S,1,1);   /* Set Direct Feedthrough option (used if outputs are computed directly from inputs      - as opposed to being computed via a state-space algorithm.) */   ssSetInputPortDirectFeedThrough  (S,0,1);   ssSetInputPortDirectFeedThrough  (S,1,1);   /* Set number of output ports (one) */   ssSetNumOutputPorts              (S,1);   /* Set output port width (DYNAMICALLY_SIZED adjusts it to match input width) */   ssSetOutputPortWidth             (S,0,1);   ssSetNumIWork(S,1);   /* Set number of sample rates the block operates at (single-rate) */   ssSetNumSampleTimes              (S,1);   /* Set 'exception-free code' option - most efficient. Do not set if the      s-function calls any potentially exception-throwing mx functions. */   ssSetOptions                     (S,SS_OPTION_EXCEPTION_FREE_CODE);}/* mdlInitializeSampleTimes() sets block and port sample times at initialisation. */static void mdlInitializeSampleTimes(SimStruct *S){   /* Set block sample rate (INHERITED_SAMPLE_TIME inherits from driving block) */   ssSetSampleTime                  (S,0,INHERITED_SAMPLE_TIME);   /* Set offset for sample time (usually 0.0) */   ssSetOffsetTime                  (S,0,0.0);}/* mdlOutputs() is called at each simulation step to do the actual work. */static void mdlOutputs(SimStruct *S, int_T tid){   /* The input signals are not guaranteed to be contiguous arrays -      their elements are accessed using arrays of pointers */   InputRealPtrsType    uPtrs = ssGetInputPortSignalPtrs(S,0);   InputRealPtrsType    ctrl  = ssGetInputPortSignalPtrs(S,1);   /* The output signals are contiguous arrays */   real_T              *y     = ssGetOutputPortRealSignal(S,0);   int_T                width = ssGetInputPortWidth(S,0);   int_T               *idx   = ssGetIWork(S);   if (**ctrl == 0.0) {      y[0]    = 0.0;      *idx    = 0;   } else {      y[0] = (*uPtrs[ (*idx) ]);      (*idx)++;      if (*idx >= width) {         *idx = 0;      }   }}/* mdlTerminate() is called at the end of the simulation run to clean up.   A stub function must be provided even if it is not actually used.  */static void mdlTerminate(SimStruct *S){}/*----FINAL DIRECTIVES------------------------------------------ *//* These preprocessor directives are always required at the end of the file. */#ifdef MATLAB_MEX_FILE#include "simulink.c"#else#include "cg_sfun.h"#endif/* [EOF] */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -