Jump to content

Design oscillator with enable


Alaba

Recommended Posts

Hello 

I was trying to design an oscillator without using the sc_clock 

 


#include "systemc.h"

SC_MODULE(osc)
{

  sc_in<bool> enable;  
  sc_out<sc_bit> out;
  sc_bit bval;

  SC_HAS_PROCESS(osc);
  osc(sc_module_name name, uint _delay = 0)
      : sc_module(name), delay(_delay)

  {
     //SC_THREAD(run_clk);
     SC_METHOD(run_clk);
     sensitive << enable.pos();
  }  

  void run_clk()
  {
   
    while (enable.read() ==true)
    {
      bval = '0';
      out.write(bval);
      // next_trigger() ;
      next_trigger(20, SC_NS);
      // wait(1, SC_NS);
      bval = '1';
      out.write(bval);
      next_trigger(20, SC_NS);
      // wait(1, SC_NS);
      if (false)  break;
    }
  }

private:
  uint delay;

}; // End of Module counter

And the Test bench

#include "systemc.h"
#include "osc.hpp"
#include <systemc-ams>
int sc_main(int argc, char *argv[])
{
  sc_signal<sc_bit> clks;
  sc_signal<bool> enable;

  osc osc0("osc", 1);
  osc0.out(clks);
  osc0.enable(enable);

  sca_util::sca_trace_file *wf = sca_util::sca_create_tabular_trace_file("clks.dat");
  sca_trace(wf, clks, "clks");
  sca_trace(wf, enable, "enable");

  enable = false;
  sc_start(100, SC_NS);

  enable = true;
  sc_start(100, SC_NS);

  enable = false;
  sc_start(100, SC_NS);

  sca_close_tabular_trace_file(wf);
  return 0;
}

 

I need the clock (oscillator) to start at enable=true and to  stop when enable is false.

I start with enable = false

when enable is changed to true  the sensitivity catches it  

But if it is in enabled  state and  I change input to false  sensitivity wont catch it and gets stuck in an infinite loop. I guess I cant change state once in the while in the loop    

Any solution will be appreciated 🙂 

 

Link to comment
Share on other sites

Declaring run_clk as SC_METHOD and and using a while loop will effectively stop your simulation as time will not progress. next_trigger() will not wait rather trigger the method after the given time.

You should define it as:

#include "systemc.h"

SC_MODULE(osc)
{

  sc_in<bool> enable;  
  sc_out<bool> out;

  SC_HAS_PROCESS(osc);
  osc(sc_module_name name, uint _delay = 0)
      : sc_module(name), delay(_delay)
  {
     //SC_THREAD(run_clk);
     SC_METHOD(run_clk);
     sensitive << enable;
  }  

  void run_clk() {
    if (enable.read() || out.read()) { 
      // executes while enable is active or the out is high. If enable is false it will end with out==false
      out.write(!out.read());
      next_trigger(20, SC_NS);
    }
  }

private:
  uint delay;

}; // End of Module counter

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...