AHDL in C

You can build your own blocks in C using Microsoft Visual C++ version 4.2 or higher.

A template is provided which models a first order differential equation, like described in the manual. The example standardml.csi, which is provided for this template calls the modeling language file standardml.dll three times and the results are compared with the results from an INF block.

You can run this example from the debugger and also put breakpoints in the source code, to debug your modeling language.

Click to open full screen

In the above figure a breakpoint is inserted when t exceeds 5seconds

The code for the above example is in the template standardml.c. Open this template by opening the standardML.mdp project workspace file. If you want to use the debugger, place you simulation file in the same directory as standardML.dll in debug mode is created. This is mostly in the debug directory, although you can change this in the options for the compiler.

If you run the example from another directory where another standardML.dll is already present, the debugger will not work correctly. Remove the old standardML.dll file, before compiling a new version and debugging it in the compiler.

You can use this temple (standardML.c and standardML.def) for defining your own C-code dlls. The template contains the correct function calls and prototypes.

If you want to create a new C-code dll, start Visual C++ and select File/New

Click to open full screen

Select ‘Project Workspace’ and select [OK]. This will invoke a new project. Select [Dynamic Link Library]

Click to open full screen

Type the directory where you want to have the files installed at: "Location:", for example, "d:/caspoc2001/MyC"

Type the name of the project file at "Name:", for example, "MyC" and click [Create].

Open the explorer and copy the templates standardML.c and standardML.def to your newly created directory. Rename them to the same name as the project file for convenience, for example, "MyC.c" and "MyC.def"

Include these two files in your project by selecting Insert/Files into Project. Select All Files (*.*) at [Files of type]. Select the *.c and the *.def files, for example, MyC.c and MyC.def

Click to open full screen

Select [Add] to include the files in your project workspace. By double clicking the files folder in the left window, you can select the *.c or the *.def files for editing. The exported function should be specified in the *.def file.

Click to open full screen

The dll, which you use during debugging, should not be used as final release. Therefore the ‘Release’ option can be set. This Release version, which runs without debugger is much faster. Set the [Release] compile option for creating your final dll in the toolbar ‘Project’.

Click to open full screen

Select Win32 Release

Click to open full screen

In the project settings [Build/Settings], (ALT-F7), you can specify where the *.dll is created.

Click to open full screen

The function:

int ML_C(void){return 1;}

has to be defined to let Caspoc know that this is a C file and not a Delphi file.

Click to open full screen

#include <windows.h>

int ML_C(void){return 1;}

double _stdcall STANDARDML(
        double input[],
        double bus[],
        double x0[],
        double x[],
        double dxdt[],
        double p[]
        )


{
double k,tau,step;
int ProcedureNumber;
double t,dt;


ProcedureNumber = (int)p[1];
t = p[2];
dt = p[3];

switch(ProcedureNumber)
  {
  case 0:
    {
    x0[1] = 0.2;
    bus[4] = 0.2;
    break;
    }
  case 1:
   {
   k = input[1];
   tau = input[2];
   step = input[3];

   if(t>5)
     k=1;

   dxdt[1]= (1/tau) * (k*step-x[1]);

   bus[4] = x[1];
   break;
   }
  }
return(bus[4]);
}

LIBRARY StandardML

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE

EXPORTS
   STANDARDML
   ML_C

© Simulation Research, www.caspoc.com