kevin_i_orourke has asked for the wisdom of the Perl Monks concerning the following question:

The project I'm currently working on involves DSP programming, which makes debugging and testing tricky (it usually involves an oscilloscope!).

To get round this one of the more senior guys developed a 'virtual' version of the system using a few bits of extra code to compile it under Windows. To control the simulation he made up his own (very simple) scripting language. The scripts are read in by the simulator program at start up, then executed.

As part of my continuing efforts to spread the word about Perl I suggested that inventing your own scripting languages was a bad idea. I've now been asked to demonstrate how we could do the same thing using an existing language.

The basic idea is that we set up a load of system settings and then specify events to occur at a given time. The main simulation code runs, triggers the events and logs what has happened. To me this looks a lot like the way Perl-Tk works.

Has anyone done something like this before? My initial idea is to use Inline::C to provide an interface to the C DSP code and a Perl module with functions that allow easy setting up of events and control of the simulated hardware.

Of course, there's no time allocated for this on the project, I suspect I'll be developing this in my own time.

--

Kevin O'Rourke

Replies are listed 'Best First'.
Re: Software simulation using Perl
by tadman (Prior) on Jul 03, 2001 at 16:27 UTC
    Inline::C is the stuff that dreams are made of. I'm serious.

    Here's how you use Inline::C:

    1. Find some C code that you want to use, preferably in ANSI C format, which is pretty standard stuff.
    2. Plonk your C code into Perl using a here document parameter to use Inline::C
    3. Use the functions directly from Perl as if they were there all along.
    I know, it sounds too good to be true, but it is. No XS is required, which is good because most of that stuff never made much sense to the average Perl hacker.

    If you have just a library file, like a shared library, or a statically linked one, it's not that hard to plow that into Inline::C using the header file. It's just a few more options.

    If you're feeling particularly adventurous, you would try and convert any library output from nasty C structures into Perl ones using the low-level conversion routines that are part of XS. For instance, instead of getting back a linked list in C, you could convert to an AV*, or Array Value, so that it is just a regular Perl array, and it just requires making a quick "wrapper" function. Since you're working one-on-one with the Perl interpreter, right inside it, actually, you can make hashes, classes, the works.

    As a bonus, when you're merging your C code with Perl, you can choose when Perl is better and when C is. You can work back and forth within the same program as required. So this way you can have the output of the C function go through an intermediate Perl function, sorting or substituting perhaps, and then back into C for some final work before being used in the main Perl program. It's really incredible what you can do.

    I know, I'm gushing, but it is really very cool stuff.