Bowers
Bowers

Reputation: 846

Project with multiple SystemC Simulations leads to an exception

In my project there are several functions which perform SystemC simulations (each has its own declaration prelude and sc_start()).

So they are constructed as follows:

// first Simulation:
sc_signal<double> s1_sim1;
..
ControlFoo<double> *cf = new ControlFoo<double>();
cf->Foo_port(s1_sim1);
..
sc_start(); // works fine
delete(cf);
..
// second Simulation:
sc_signal<double> s1_sim2; // this leads to an exception

The first simulation runs as desired until the sc_stop(). But when I try to declare new sc_signal after the first simulation is completed, it throws an exception.

How do I avoid this?

Upvotes: 1

Views: 446

Answers (1)

Jellybaby
Jellybaby

Reputation: 986

User Guide: "You can only start a simulation after you instantiate and properly connect all modules and signals."

How would one dynamically create hardware?

The options are to split the models into separate projects or to declare all the models in one project and have a method of switching them on and off, such as an 'enable' signal

In answer to your follow up question you could try something like this. We create a controller module that has "enable" outputs and we use them to control 3 controlled modules which we are using here to represent some models or sub-systems we wish to switch on and off.

#include <systemc.h>
#include <iostream>

using namespace sc_core;
using namespace std;

/*
 Controller module with output enable signals to enable and 
 disable other modules representing the models we wish to switch on/off
*/
SC_MODULE(Controller){

    SC_CTOR(Controller) 
        : clk_i("clk_i"), 
        en_a("en_a"), 
        en_b("en_b"),
        en_c("en_c"),
        counter(0)
    {
        SC_METHOD(proc);
        sensitive << clk_i.pos(); //run process on positive clock edge
    }

    void proc(){
        if(counter < 10){ //enable model A and disable others
            en_a.write(true);
            en_b.write(false);
            en_c.write(false);
        }
        else if(counter < 20){ //enable B and disable others
            en_a.write(false);
            en_b.write(true);
            en_c.write(false);
        }
        else{ //enable C and disable others
            en_a.write(false);
            en_b.write(false);
            en_c.write(true);
        }
        counter = (counter + 1) % 30; 
    }

    sc_in<bool> clk_i; // clock input
    sc_out<bool> en_a; // enable model A when high
    sc_out<bool> en_b; // enable model B when high
    sc_out<bool> en_c; // enable model C when high
    int counter; //simple counter to simulate some condition
};

/*
 Module with an enable signal to represent the sub-systems we 
 wish to switch on and off
*/
SC_MODULE(Controlled){
    SC_CTOR(Controlled) : en_i("en_i"), clk_i("clk_i"){
        SC_METHOD(proc);
        sensitive << clk_i.pos(); //run process on positive clock edge
    }

    void proc(){
        //if we are enabled then run "real process" otherwise do nothing
        if(en_i.read() == true) enabledProc();

    }

    // the "real process" that we wish to switch on and off
    void enabledProc(){
        cout << "model " << name() << " is enabled\n";
    }

    sc_in<bool> en_i;
    sc_in<bool> clk_i;
};


int sc_main(int, char**){

    // created controller and 3 controlled modules
    Controller controller("controller");
    Controlled modelA("A"), modelB("B"), modelC("C");

    // create a clock and connect it to all 4 modules
    sc_clock clk("clk", 1.0, SC_SEC);
    controller.clk_i(clk);
    modelA.clk_i(clk);
    modelB.clk_i(clk);
    modelC.clk_i(clk);

    // create an enable signal for each module and connect to controller
    sc_signal<bool> en_a("en_a");
    sc_signal<bool> en_b("en_b");
    sc_signal<bool> en_c("en_c");
    controller.en_a(en_a);
    controller.en_b(en_b);
    controller.en_c(en_c);
    // connect enable lines to controlled modules
    modelA.en_i(en_a);
    modelB.en_i(en_b);
    modelC.en_i(en_c);

    sc_start(30, SC_SEC); // run for 30 seconds

    return 0;
}

And you should get the output

model A is enabled
model A is enabled
model A is enabled
model A is enabled
model A is enabled
model A is enabled
model A is enabled
model A is enabled
model A is enabled
model A is enabled
model B is enabled
model B is enabled
model B is enabled
model B is enabled
model B is enabled
model B is enabled
model B is enabled
model B is enabled
model B is enabled
model B is enabled
model C is enabled
model C is enabled
model C is enabled
model C is enabled
model C is enabled
model C is enabled
model C is enabled
model C is enabled
model C is enabled
model C is enabled

As the controller enables and disables each modules in turn. This simple example should get you started until you can finally substitute your real systems for my simple printing ones

Upvotes: 3

Related Questions