Jump to content
Sign in to follow this  
darrenxu

how to link bool port with sc_int

Recommended Posts

I am a beginner of systemC

recently I did a practice

it require using two full-adders to make a 2-bit-adder

the code of full-adder is below 
//-------------------------------
#include "systemc.h"

SC_MODULE (full_adder){
  sc_in<bool> a,b,carry_in;
  sc_out<bool> sum,carry;
  void prc_full_adder()
  {  sum= a^b^carry;
    carry= ((a & b )|(a & carry_in)|(b & carry_in));
  }
  SC_CTOR(full_adder)
    { 
      SC_METHOD (prc_full_adder);
      sensitive<<a<<b<<carry_in;
    }
};


//-------------------------------------------
the code for two_bit_adder is written down below
//------------------------------------------
#include "systemc.h"
#include "full_adder.cpp"
SC_MODULE(two_bit_adder){
    sc_in < bool>   carry_in0;
    sc_in < sc_uint<2> > num1 , num2;
    sc_out < bool > carry_in2;
    sc_out < sc_uint<2> > sum2;
    full_adder *f1, *f2;

    void prc_2_bit_adder()
    {    
    }

    SC_CTOR(two_bit_adder)
      {  
         SC_METHOD(prc_2_bit_adder);
         sensitive<<carry_in0<<num1<<num2;
         f1= new full_adder ("f1");
         f2= new full_adder ("f2");
         

        f1->a(  );
          f1->b(  );
          f1->carry_in (  );
          f1->sum ( );
          f1->carry ( );

        f2->a( );
          f2->b( );
          f2->carry_in( );
          f2->sum( );
          f2->carry ( );

      }

    ~ two_bit_adder()
      { delete f1;
        delete f2;
      }
};

//-----------------------

the problem is the red part, how to link them? because the instance only can be linked by sc_signals, all the io ports of the full-adder are single bit, however the num1,num2,sum2 are two bits width, as io ports and sc_signals in systemC don't support bit select, I have to use sc_uint to convert them, where I should write code for converting? in SC_CTOR or in the function prc_2_bit_adder? I tried both, none of them give me the correct answers or correct compiler results

please help me

thanks!

Share this post


Link to post
Share on other sites

I am a beginner of systemC

recently I did a practice

it require using two full-adders to make a 2-bit-adder

the code of full-adder is below 

//-------------------------------

#include "systemc.h"

SC_MODULE (full_adder){

  sc_in<bool> a,b,carry_in;

  sc_out<bool> sum,carry;

  void prc_full_adder()

  {  sum= a^b^carry;

    carry= ((a & b )|(a & carry_in)|(b & carry_in));

  }

  SC_CTOR(full_adder)

    { 

      SC_METHOD (prc_full_adder);

      sensitive<<a<<b<<carry_in;

    }

};

//-------------------------------------------

the code for two_bit_adder is written down below

//------------------------------------------

#include "systemc.h"

#include "full_adder.cpp"

SC_MODULE(two_bit_adder){

    sc_in < bool>   carry_in0;

    sc_in < sc_uint<2> > num1 , num2;

    sc_out < bool > carry_in2;

    sc_out < sc_uint<2> > sum2;

    full_adder *f1, *f2;

    void prc_2_bit_adder()

    {    

    }

    SC_CTOR(two_bit_adder)

      {  

         SC_METHOD(prc_2_bit_adder);

         sensitive<<carry_in0<<num1<<num2;

         f1= new full_adder ("f1");

         f2= new full_adder ("f2");

         

        f1->a(  );

          f1->b(  );

          f1->carry_in (  );

          f1->sum ( );

          f1->carry ( );

        f2->a( );

          f2->b( );

          f2->carry_in( );

          f2->sum( );

          f2->carry ( );

      }

    ~ two_bit_adder()

      { delete f1;

        delete f2;

      }

};

//-----------------------

the problem is the red part, how to link them? because the instance only can be linked by sc_signals, all the io ports of the full-adder are single bit, however the num1,num2,sum2 are two bits width, as io ports and sc_signals in systemC don't support bit select, I have to use sc_uint to convert them, where I should write code for converting? in SC_CTOR or in the function prc_2_bit_adder? I tried both, none of them give me the correct answers or correct compiler results

please help me

thanks!

 

Hello Sir,

SystemC allows embedding modules inside other modules

(just as its parent language C++).  So, it you are trying to

create a 2 bit adder with two 1 bit adders, then in the

2 bit adder modules, and then link the two 1 bit adder

modules with signal channels, for example  :

(sc_core::sc_signal<bool> sig 1;

sc_core::sc_signal<bool> sig2; and so on)

So, in this case, the input ports of the first 1 bit adder will

be connected to the input ports of the container 2 bit adder

module. BUT, the output ports of the first 1 bit adder module

must be connected to the signal channels ( see above).

The input ports of the second 1 bit adder module must be

connected to the signal channels carrying the output of the

first 1 bit adder, but the output ports of the second 1 bit adder

module must be connected to the output ports of the container

2 bit adder. This will work just fine. Also, avoid instantiating

objects with pointers whenever possible.

Hope that helps.

 

 

 

1. Create/instantiate two 1 bit adder modules. Here, your

"full_adder" module should work fine.

 

2.

Share this post


Link to post
Share on other sites

i know how to link two full-adder as a two-bit-adder, but i have no ideal to write the code to link them

 

for example i write the code like that 

 

sc_uint<2> num1_t, num2_t, sum2_t;

sc_signal <bool> num1_s1, num1_s2, num2_s1, num2_s2, carry_in, sum_s1, sum_s2;

       num1_t=num1;

       num1_s1=num1_t[0];

       num1_s2=num1_t[1];

       num2_s1=num2_t[0];

       num2_s2=num2_t[1];

       sum2_t[0]=sum_s1;      

       sum2_t[1]=sum_s2;

       sum2=sum2_t;

       f1->a( num1_s1 );
          f1->b( num2_s1 );
          f1->carry_in ( carry_in );
          f1->sum ( sum_s1);
          f1->carry ( carry_in1);

        f2->a( num1_s2 );

          f2->b( num2_s2 );
          f2->carry_in( carry_in );
          f2->sum( sum_s2);
          f2->carry ( carry_in2);

 

if i put the green part in the SC_CTOR, the compiler will give me the errors, or i try to put that in prc_2_bit_adder, it also give me the error or incorrect results! i have used one week to modify my code , none of them are work.

Share this post


Link to post
Share on other sites

i know how to link two full-adder as a two-bit-adder, but i have no ideal to write the code to link them

 

for example i write the code like that 

 

sc_uint<2> num1_t, num2_t, sum2_t;

sc_signal <bool> num1_s1, num1_s2, num2_s1, num2_s2, carry_in, sum_s1, sum_s2;

       num1_t=num1;

       num1_s1=num1_t[0];

       num1_s2=num1_t[1];

       num2_s1=num2_t[0];

       num2_s2=num2_t[1];

       sum2_t[0]=sum_s1;      

       sum2_t[1]=sum_s2;

       sum2=sum2_t;

       f1->a( num1_s1 );

          f1->b( num2_s1 );

          f1->carry_in ( carry_in );

          f1->sum ( sum_s1);

          f1->carry ( carry_in1);

        f2->a( num1_s2 );

          f2->b( num2_s2 );

          f2->carry_in( carry_in );

          f2->sum( sum_s2);

          f2->carry ( carry_in2);

 

if i put the green part in the SC_CTOR, the compiler will give me the errors, or i try to put that in prc_2_bit_adder, it also give me the error or incorrect results! i have used one week to modify my code , none of them are work.

Hello Sir,

May we know the exact goal of this exercise ?  Is it direct conversion

from a bool to a sc_int or vice versa ? The "type" of a bool variable is

completely different from that of a "sc_int" -- C++ does not provide any

means for direct conversion. The bool data type can have two values

only(false, true) but the sc_int a huge range of values  The best one

could do is use the ternary comparison operator as:

Suppose there is a sc_int variable s_i, and a boolean variable b_b;

Then one could have(for example) :

s_i = b_b == false ? 1 : 0;

Similarly, one could have (for example):

b_b = s_i > 10  ? true : false;

Or one could use the C/C++ 'switch - case - break" construct.

Once the conversion is complete, the variable may be written to

the appropriate port. One wonders however for the necessity of

conversion from/to sc_int in a purely binary circuit.

Share this post


Link to post
Share on other sites

Hi Darrenxu,

   the code you've written in green won't work because SystemC has no shorthand for a concurrent signal assignment (VHDL) or assign (SystemVerilog).

 

One solution is to write a helper process, e.g.

void assemble_proc() {
     num1_t=num1;
       num1_s1=num1_t[0];
       num1_s2=num1_t[1];
       num2_s1=num2_t[0];
       num2_s2=num2_t[1];
       sum2_t[0]=sum_s1;      
       sum2_t[1]=sum_s2;
       sum2=sum2_t;
}

// in the constructor
SC_METHOD(assemble_proc) ;
sensitive << sum2_2 << sum_st << sum_s1 << num2_t << num1_t;
 

Another solution would be to use sc_vector (available in 1666-2011, Proof-of-Concept simulator 2.3.0). Instead of using sc_uint, you could then create a vector of sc_in<bool> and sc_signal<bool> for instance, and assemble and dis-assemble the vector.

 

kind regards

Alan

 

P.S. I am also tempted to say "do it in VHDL or SystemVerilog instead" but that's not very helpful if you're trying to learn SystemC :-)

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
Sign in to follow this  

×