Jump to content
pouyashiri

unhandled exception for all codes

Recommended Posts

Hello everyone

I'm new to systemc and VS. I've made some codes, but when I debug the testbench of this code, I start getting "unhandled exception at memory location xxx" for ALL codes I used to run. How can I fix this? What causes this?

 

SC_MODULE(my_memory)
{
sc_in <bool> clk;
sc_in <sc_bv<12> > address;
sc_inout <sc_bv<16> > content;
sc_in <bool> write;
sc_signal <sc_bv<16> > mem[4096];
void  my_memory::read () {content.write(mem[address.read().to_uint()]);}
void my_memory::writ()
{
if((write == (bool) '1') &&  clk.event() && (clk.read() == '1') )
mem[address.read().to_uint()]=content.read();
}
 
SC_CTOR(my_memory)
{
for(int i = 0; i <= 4095;i++)
mem.write(0);
SC_THREAD(writ);
sensitive<<clk<<write;
SC_THREAD(read);
sensitive<<address;
}
};
 
 
 
Regards

 

Share this post


Link to post
Share on other sites

Assuming that you mean "Visual Studio" by mentioning "VS", please see http://forums.accellera.org/topic/71-/ for a solution.

BTW, this is documented in the INSTALL instructions of SystemC.

 

/Philipp

Yes I meant Visual Studio. As I said, I have run some codes before, so I'm familiar with project settings. The problem still persists.

I can just guess it's for assigning too much memory to memory signal (sc_signal <sc_bv<16> > mem[4096]).

Share this post


Link to post
Share on other sites

As I checked the reason of crashing is this :

complete beinding failed: "port not bound: port 'CUT.port_5' (sc_out)"
 
I have to mention again that this code is running on other pcs.
I get this error for all systemC codes!

Share this post


Link to post
Share on other sites

Can you make a very small test case and see if that crashes? In your post title you say "unhandled exception for all codes", so does

 

#include "systemc.h"

int sc_main(int argv, char ** argv) {

 

   cout << "hallo world" << endl;

 

  return 0;

 

}

 

Does that crash? If so what exactly is the error?

 

regards

Alan

Share this post


Link to post
Share on other sites

Does that crash? If so what exactly is the error?

 

no it didn't crash. Just found that the code crashes on the last port of all codes and the error is  " port not bound: CUT.port_x' (sc_out or sc_in) "

where 'x' is the number of last out or in port, and CUT is the name of my constructor, like this : my_register top("CUT");

Share this post


Link to post
Share on other sites

this is the code that makes all later codes crash!

//////////////////////////////////////START OF CODE

////FILE ONE: MEMORY MODULE, HEADER FILE

#ifndef Memory_H
#define Memory_H

#include "systemc.h"

SC_MODULE(my_memory)
{
    sc_in <bool> clk;
    sc_in <sc_bv<12> > address;
    sc_inout <sc_bv<16> > content;
    sc_in <bool> write;
    sc_signal <sc_bv<16> > mem[4096];
    void  my_memory::read () {content.write(mem[address.read().to_uint()]);}
    void my_memory::writ()
    {
        if((write == (bool) '1') &&  clk.event() && (clk.read() == '1') )
            mem[address.read().to_uint()]=content.read();
    }

    SC_CTOR(my_memory)
    {
        for(int i = 0; i <= 4095;i++)
            mem.write(0);
        SC_THREAD(writ);
        sensitive<<clk<<write;
        SC_THREAD(read);
        sensitive<<address;
    }
};
#endif
 

/////PART TWO: MEMORY TESTBENCH, HEADER FILE

#ifndef memory_tb_H
#define memory_tb_H

#include "memory.h"
#include "systemc.h"
SC_MODULE(memory_tb)
{
    sc_signal <bool> clk_t;
    sc_signal <sc_bv<12> > address_t;
    sc_signal <sc_bv<16> > content_t;
    sc_signal <bool> write_t;
    

    void memory_tb::apply_clk()
    {
        for(int i = 0;i < 1000;i+=5)
        {
            clk_t = (bool)'0';
            wait(5,SC_NS);
            clk_t = (bool)'1';
            wait(5,SC_NS);
        }
        wait();
    }

    void memory_tb::apply_write()
    {
        wait(9,SC_NS);
        write_t = 1;
        content_t.write(5);
        
        wait(2,SC_NS);
        write_t = 0;
        
        wait(2,SC_NS);
        content_t.write(7);

        wait(20,SC_NS);
        write_t = 0;

        printf("read at 32ns : %d\n",content_t.read().to_uint());

        wait(10,SC_NS);
        printf("read at 42ns : %d\n",content_t.read().to_uint());
        
        wait();


    }

    void memory_tb::apply_address()
    {
        address_t.write(0);
        wait(10,SC_NS);

        address_t.write(1);
        wait(10,SC_NS);
        
        address_t.write(2);
        wait(10,SC_NS);

        address_t.write(1);
        wait(10,SC_NS);

        address_t.write(2);
        wait(10,SC_NS);

        wait();
    }
    

    my_memory *mem;
    SC_CTOR(memory_tb)
    {
        SC_THREAD(apply_clk);
        SC_THREAD(apply_address);
        SC_THREAD(apply_write);
        
        mem = new my_memory("pouya");
        (*mem)(clk_t,address_t,content_t,write_t);
    }
};

        
#endif

 

/////PART THREE THE MAIN .CPP FILE

#include "MEMORY_tb.h"

int main()
{
    
    printf("Starting the simulation ...\n");
    
    my_memory top("CUT");

    sc_start();

    return 0;

}

/////////////////////////////////////////END OF CODE
 

Share this post


Link to post
Share on other sites

this is the code that makes all later codes crash!

//////////////////////////////////////START OF CODE

////FILE ONE: MEMORY MODULE, HEADER FILE

#ifndef Memory_H

#define Memory_H

#include "systemc.h"

SC_MODULE(my_memory)

{

    sc_in <bool> clk;

    sc_in <sc_bv<12> > address;

    sc_inout <sc_bv<16> > content;

    sc_in <bool> write;

    sc_signal <sc_bv<16> > mem[4096];

    void  my_memory::read () {content.write(mem[address.read().to_uint()]);}

    void my_memory::writ()

    {

        if((write == (bool) '1') &&  clk.event() && (clk.read() == '1') )

            mem[address.read().to_uint()]=content.read();

    }

    SC_CTOR(my_memory)

    {

        for(int i = 0; i <= 4095;i++)

            mem.write(0);

        SC_THREAD(writ);

        sensitive<<clk<<write;

        SC_THREAD(read);

        sensitive<<address;

    }

};

#endif

 

/////PART TWO: MEMORY TESTBENCH, HEADER FILE

#ifndef memory_tb_H

#define memory_tb_H

#include "memory.h"

#include "systemc.h"

SC_MODULE(memory_tb)

{

    sc_signal <bool> clk_t;

    sc_signal <sc_bv<12> > address_t;

    sc_signal <sc_bv<16> > content_t;

    sc_signal <bool> write_t;

    

    void memory_tb::apply_clk()

    {

        for(int i = 0;i < 1000;i+=5)

        {

            clk_t = (bool)'0';

            wait(5,SC_NS);

            clk_t = (bool)'1';

            wait(5,SC_NS);

        }

        wait();

    }

    void memory_tb::apply_write()

    {

        wait(9,SC_NS);

        write_t = 1;

        content_t.write(5);

        

        wait(2,SC_NS);

        write_t = 0;

        

        wait(2,SC_NS);

        content_t.write(7);

        wait(20,SC_NS);

        write_t = 0;

        printf("read at 32ns : %d\n",content_t.read().to_uint());

        wait(10,SC_NS);

        printf("read at 42ns : %d\n",content_t.read().to_uint());

        

        wait();

    }

    void memory_tb::apply_address()

    {

        address_t.write(0);

        wait(10,SC_NS);

        address_t.write(1);

        wait(10,SC_NS);

        

        address_t.write(2);

        wait(10,SC_NS);

        address_t.write(1);

        wait(10,SC_NS);

        address_t.write(2);

        wait(10,SC_NS);

        wait();

    }

    

    my_memory *mem;

    SC_CTOR(memory_tb)

    {

        SC_THREAD(apply_clk);

        SC_THREAD(apply_address);

        SC_THREAD(apply_write);

        

        mem = new my_memory("pouya");

        (*mem)(clk_t,address_t,content_t,write_t);

    }

};

        

#endif

 

/////PART THREE THE MAIN .CPP FILE

#include "MEMORY_tb.h"

int main()

{

    

    printf("Starting the simulation ...\n");

    

    my_memory top("CUT");

    sc_start();

    return 0;

}

/////////////////////////////////////////END OF CODE

 

Hello Sir,

Maybe this is not related to your crashing issue, but certain sections of your code

appear troublesome. In the memory model, both read and write operations are

declared/defined as threads:

 

  SC_CTOR(my_memory)

  {

        for(int i = 0; i <= 4095;i++)

            mem.write(0);

        SC_THREAD(writ);

        sensitive<<clk<<write;

        SC_THREAD(read);

        sensitive<<address;

  }

 

  But how are the read/write operations going to 

  run, over and over ?

 

    void  my_memory::read () {content.write(mem[address.read().to_uint()]);}

    void my_memory::writ()

    {

        if((write == (bool) '1') &&  clk.event() && (clk.read() == '1') )

            mem[address.read().to_uint()]=content.read();

    }

 

All SystemC threads execute just ONCE and must be made to respond to

the triggerring events. These two methods would not do that. Hope that

helps.

Share this post


Link to post
Share on other sites

There's a number of things that I notice. As Dakupoto says, you need infinite loops in your SC_THREADs of your memory model, otherwise they will only run once at time zero.

 

You should use sc_main() in your top level (not main()).

 

All SystemC processes run at time zero independently of their sensitivity (unless you use dont_initialize()).

 

That means that void my_memory::writ() will run at time zero with an undefined address value. I suggest checking the address value for out-of-range values before using it.

 

Declaring a large sc_signal array  sc_signal<sc_bv<16> mem [4096] may cause stack problems - I don't think that needs to be an sc_signal, so why not use std::vector instead (which uses dynamically allocated/ heap memory and so can't cause stack overflow).

 

I always use named port binding as positional port binding is error-prone, so personally I wouldn't write

  (*mem)(clk_t,address_t,content_t,write_t);

 

though of course it is legal.

 

regards

Alan

Share this post


Link to post
Share on other sites

There's a number of things that I notice. As Dakupoto says, you need infinite loops in your SC_THREADs of your memory model, otherwise they will only run once at time zero.

 

You should use sc_main() in your top level (not main()).

 

All SystemC processes run at time zero independently of their sensitivity (unless you use dont_initialize()).

 

That means that void my_memory::writ() will run at time zero with an undefined address value. I suggest checking the address value for out-of-range values before using it.

 

Declaring a large sc_signal array  sc_signal<sc_bv<16> mem [4096] may cause stack problems - I don't think that needs to be an sc_signal, so why not use std::vector instead (which uses dynamically allocated/ heap memory and so can't cause stack overflow).

 

I always use named port binding as positional port binding is error-prone, so personally I wouldn't write

  (*mem)(clk_t,address_t,content_t,write_t);

 

though of course it is legal.

 

regards

Alan

 

Thanks for the points you mentioned. Now I know what problem this code has caused, after running this code, the memory that visual assigns to stack is overrun hence destroyed. As a result when I add a sc_in port or sc_out port in my codes it cannot bind them and I always get unhandled exceptions at memory locations.

 

Any tips for resetting this memory dedicated to stack?

 

Regards

Share this post


Link to post
Share on other sites

The bad solution is to increase the stack size. From memory, there's an option in Visual C++ like /Zm which affects stack size.

 

The good solution is to avoid allocating large amounts of memory on the heap, in other words use dynamic memory for your memory array. You can do this by making the array dynamically allocated memory, or as I said above, just use std::vector to hold the data,

 

regards

Alan

Share this post


Link to post
Share on other sites

The bad solution is to increase the stack size. From memory, there's an option in Visual C++ like /Zm which affects stack size.

 

The good solution is to avoid allocating large amounts of memory on the heap, in other words use dynamic memory for your memory array. You can do this by making the array dynamically allocated memory, or as I said above, just use std::vector to hold the data,

 

regards

Alan

 

Mr Fitch, thanks so much for the help. I have almost done everything you said but I still get "port not bound, complete binding failed".

Spent more than 24hours on this issue now.

Share this post


Link to post
Share on other sites

Hi,

  I compiled your code, and eventually spotted that you'd instanced the memory in sc_main, not the test bench. If you instance the testbench, and fix a few other things, it works.

#ifndef Memory_H
#define Memory_H

#include "systemc.h"

SC_MODULE(my_memory)
{
   sc_in <bool> clk;
   sc_in <sc_bv<12> > address;
   sc_in <sc_bv<16> > datain;
   sc_out <sc_bv<16> > dataout;
   sc_in <bool> write_en;
   
   sc_signal <sc_bv<16> > mem[4096];
   
   void  read () {
      while (true) {
         wait();
         dataout.write(mem[address.read().to_uint()]);
      }
   }
   
   void writ() {
      while (true) {
         wait();
         if( write_en.read() == true  ) {
            mem[address.read().to_uint()]=datain.read();
            cout << "Writing to address " << address << " data " << datain << endl; 
         }
      }
   }

   SC_CTOR(my_memory) : clk("clk"), address("address"), datain("datain"),
                       dataout("dataout"), write_en("write_en") {
      for(int i = 0; i <= 4095;i++)
         mem[i].write(0);
      SC_THREAD(writ);
      sensitive<<clk.pos();
      SC_THREAD(read);
      sensitive<<address;
   }
};
#endif

#ifndef memory_tb_H
#define memory_tb_H

//#include "memory.h"
#include "systemc.h"
SC_MODULE(memory_tb)
{
   sc_signal <bool> clk_t;
   sc_signal <sc_bv<12> > address_t;
   sc_signal <sc_bv<16> > datain_t, dataout_t;
   sc_signal <bool> write_t;

   void apply_clk() {
      for(int i = 0;i < 1000;i+=5) {
         clk_t = false;
         wait(5,SC_NS);
         clk_t = true;
         wait(5,SC_NS);
      }
      wait();
   }

   void apply_write() {
      write_t = true;
      datain_t.write(5);
      wait(9,SC_NS);

      wait(2,SC_NS);
      write_t = false;

      wait(2,SC_NS);
      datain_t.write(7);

      wait(20,SC_NS);
      write_t = false;

      cout << "read at " << sc_time_stamp() << " " <<  dataout_t << endl;
 //     printf("read at 32ns : %d\n",dataout_t.read().to_uint());

      wait(10,SC_NS);
      cout << "read at " << sc_time_stamp() << " " << dataout_t << endl;
//      printf("read at 42ns : %d\n",dataout_t.read().to_uint());

      wait(10,SC_NS);
      cout << "read at " << sc_time_stamp() << " " << dataout_t << endl;

      wait();

   }

   void apply_address() {
      address_t.write(0);
      wait(10,SC_NS);

      address_t.write(1);
      wait(10,SC_NS);

      address_t.write(2);
      wait(10,SC_NS);

      address_t.write(1);
      wait(10,SC_NS);

      address_t.write(2);
      wait(10,SC_NS);
      
      address_t.write(0);
      wait(10,SC_NS);

      wait();
   }

   my_memory *mem;
   SC_CTOR(memory_tb) : clk_t("clk_t"), address_t("address_t"), 
                        datain_t("datain_t"), dataout_t("dataout_t"), write_t("write_t")  {
      SC_THREAD(apply_clk);
      SC_THREAD(apply_address);
      SC_THREAD(apply_write);

      mem = new my_memory("pouya");
      (*mem).clk(clk_t);
      (*mem).address(address_t);
      (*mem).datain(datain_t);
      (*mem).dataout(dataout_t);
      (*mem).write_en(write_t);
   }
};
#endif



int sc_main(int argc, char ** argv)
{

   printf("Starting the simulation ...\n");

   memory_tb top("CUT");

   sc_start(1000, SC_NS);

   return 0;

}

regards

Alan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×