SystemC - How can I get the module name to which sc_signal connected? - systemc

I'd like to print out the name of sc_module to which sc_signal connected.
How can I get the module name, "module_a" in the following code, from "sig_out"?
#include "systemc.h"
class sig_if : virtual public sc_interface
{
};
class my_sig : public sc_module, public sig_if
{
public:
my_sig(sc_module_name nm) : sc_module(nm)
{
}
};
SC_MODULE(test_module)
{
sc_port<sig_if> out;
SC_CTOR(test_module)
{
}
};
int sc_main(int argc, char* argv[]) {
test_module module_a("module_a");
my_sig sig_out("sig_out");
module_a.out(sig_out);
// std::cout << sig_out.get_parent() << std::endl;
sc_start();
return 0;
}

You could override sc_interface::register_port() in the my_sig class to save a reference to the bound port. The parent of the bound port is the module that contains the port.
#include <iostream>
#include <cassert>
#include "systemc.h"
class sig_if : virtual public sc_interface
{
};
class my_sig : public sc_module, public sig_if
{
public:
my_sig(sc_module_name nm) : sc_module(nm), bound_port(NULL)
{
}
void register_port(sc_port_base& port, const char*) {
bound_port = &port;
}
sc_object* get_bound_module() const {
assert(bound_port);
return bound_port->get_parent_object();
};
sc_port_base* bound_port;
};
SC_MODULE(test_module)
{
sc_port<sig_if> out;
SC_CTOR(test_module)
{
}
};
int sc_main(int argc, char* argv[]) {
test_module module_a("module_a");
my_sig sig_out("sig_out");
module_a.out(sig_out);
sc_start(0, SC_NS);
std::cout << sig_out.get_bound_module()->name() << std::endl;
return 0;
}
The above code prints the name of module_a. Note that sig::get_bound_module() can only be called after elaboration, that is, after sc_start() has been called, because sc_interface::register_port() is only called during elaboration.

Related

Member of b object can't write in SystemC

I got this error on SystemC, and I don't understand why. The error is:
'write': is not a member of
'sc_core::sc_in' ConsoleApplication1
'write': is not a member of 'sc_core::sc_in'
class "sc_core::sc_in" has no member "write"
class "sc_core::sc_in" has no member "write"
Here I put together the code.
#include<systemc.h>
SC_MODULE(prin) {
sc_in<bool> a;
void print() {
cout << "Hello World\n";
}
SC_CTOR(prin) {
SC_METHOD(print);
sensitive << a;
}
};
SC_MODULE(input) {
prin b;
void in() {
b.a.write(false);
wait();
b.a.write(true);
wait();
}
SC_CTOR(input) : b("sds"){
SC_THREAD(in);
}
};
int sc_main(int argc, char* argv[]) {
input prin1("pint");
sc_start();
return 0;
}
If the error seems confusing, here I put together the picture of my error:
The port "a" is an input port so cannot be written to. If you make it an output port then you can write to it. Also, the port is not bound so you will also get an error for that so I have bound a signal to it just so it compiles.
#include <systemc.h>
SC_MODULE(prin) {
sc_out<bool> a; //output port
sc_signal<bool> sig; //something to bind port a to
void print() {
cout << "Hello World\n";
}
SC_CTOR(prin) {
SC_METHOD(print);
sensitive << a;
a(sig); //bind port a to s signal
}
};
SC_MODULE(input) {
prin b;
void in() {
b.a.write(false);
wait();
b.a.write(true);
wait();
}
SC_CTOR(input) : b("sds"){
SC_THREAD(in);
}
};
int sc_main(int argc, char* argv[]) {
input prin1("pint");
sc_start();
return 0;
}
Then
g++ -file.cpp -lsystemc
./a.out
Gives me the output
SystemC 2.3.2-Accellera --- Apr 16 2018 00:15:03
Copyright (c) 1996-2017 by all Contributors,
ALL RIGHTS RESERVED
Hello World

Port not bound SystemC (E112)

I am trying to implement a producer (master) speaking to a memory element (slave) through the memory controller (which implements the interface simple_mem_interface).
Note: Some functions details and include statements are not fully mentioned in the code attached.
Searching for bugs in the code.
Adding debugging tools to find the fault in Write Enable Port.
binding.cpp
int sc_main(int argc, char* argv[])
{
sc_signal<unsigned int> d_out,d_in,address_d;
sc_signal<bool> wen, ren, ack;
sc_clock ClkFast("ClkFast", 100, SC_NS);
sc_clock ClkSlow("ClkSlow", 50, SC_NS);
Memory_Controller Controller1 ("Controller");
d_out = Controller1.data_mem_read;
ren.write(Controller1.REN);
ack.write(Controller1.ack);
d_in.write(Controller1.data_write);
address_d.write(Controller1.address);
wen.write(Controller1.WEN);
producer P1("Producer");
P1.out(Controller1);
P1.Clk(ClkFast);
Memory_module MEM("Memory");
MEM.Wen(wen);
MEM.Ren(ren);
MEM.ack(ack);
MEM.Clock(ClkSlow);
MEM.data_in(d_in);
MEM.data_out(d_out);
MEM.address(address_d);
sc_start(5000, SC_NS);
return 0;
Memory_controller.h
#define MEM_SIZE 100
#include <interface_func.h>
class Memory_Controller : public sc_module, public simple_mem_if
{
public:
// Ports
sc_in <unsigned int> data_mem_read{ "Data_Read_from_Memory" };
sc_out<bool> REN { "Read_Enable" };
sc_out<bool> WEN { "Write_Enable" };
sc_out <bool> ack{ "ACK_Bool" };
sc_out<unsigned int> address{ "Memory_Address" }, data_write{
"Data_Written_to_Memory" };
// constructor
Memory_Controller(sc_module_name nm) : sc_module(nm)
{ // Creating a 2 dimentional array holding adresses and data
WEN.write(false);
REN.write(false);
ack.write(false);
}
~Memory_Controller() //destructor
{
}
bool Write(unsigned int address_i, unsigned int datum) // blocking write
{
WEN.write(true);
REN.write(false);
data_write.write(datum);
address.write(address_i);
if (ack == true)
return true;
else
return false;
}
bool Read(unsigned int address_i, unsigned int& datum_i) // blocking read
{
WEN.write(false);
REN.write(true);
datum_i=data_mem_read;
address.write(address_i);
if (ack == true)
return true;
else
return false;
}
void register_port(sc_port_base& port, const char* if_typename)
{
cout << "binding " << port.name() << " to "
<< "interface: " << if_typename << endl;
}
};
Memory.h
#define MEM_SIZE 100
#include "interface_func.h"
class Memory_module : public sc_module
{
public:
sc_in<bool> Wen,Ren;
sc_in <unsigned int> address, data_in ;
sc_in<bool> Clock;
sc_out <unsigned int> data_out;
sc_out <bool> ack;
bool fileinput = false;
ifstream myfile;
unsigned int item [MEM_SIZE];
Memory_module()
{
}
void Write() // blocking write
{
while (true)
{
wait();
if (Wen==true)
{
if (address >= MEM_SIZE || address < 0)
{
ack=false;
}
else
{
item[address]=data_in;
ack=true;
}
}
}
}
void Read() // blocking read
{
while (true)
{
wait();
if (Ren)
{
if (address >= MEM_SIZE || address < 0)
ack=false;
else
{
data_out.write(item[address]);
ack=true;
}
}
}
}
SC_CTOR(Memory_module)
{
SC_THREAD(Read);
sensitive << Clock.pos();
SC_THREAD(Write);
sensitive << Clock.pos();
}
};
interface_func.h
class simple_mem_if : virtual public sc_interface
{
public:
virtual bool Write(unsigned int addr, unsigned int data) = 0;
virtual bool Read(unsigned int addr, unsigned int& data) = 0;
};
After debugging the SystemC binder.cpp code, the following error arises:
(E112) get interface failed: port is not bound : port 'Controller.Write_Enable' (sc_out)
You cannot drive your unconnected ports in the Memory_Controller constructor. If you want to explicitly drive these ports during startup, move these calls to a start_of_simulation callback:
Memory_Controller(sc_module_name nm) : sc_module(nm)
{}
void start_of_simulation()
{
WEN.write(false);
REN.write(false);
ack.write(false);
}

How do I setup Using Systems::IO::Ports in DLL so that it can be called from a loadlibrary function

Using vc2012 express c++
I am a little confused on how a runtime library works, but I had needed to create one for a driver from some hardware I have so that it can be used in a SDK.
My source code is as follows
#include "PhantomAdapter.h"
#include <stdexcept>
int ready()
{
//return Comms::SerialPort::check();
return 1;
}
int open()
{
int flag=0;
//flag=Comms::SerialPort::openPort();
return flag;
}
int close()
{
Comms::SerialPort::closePort();
return 1;
}
int angle(double& angle)
{
angle = Comms::SerialPort::read();
return 0;
}
int torque(double torque)
{
Comms::SerialPort::send((Byte)torque);
return 1;
}
namespace Comms
{
//static p1 = gcnew System::IO::Ports::SerialPort();
int SerialPort::openPort()
{
bool check=0;
p1 = gcnew System::IO::Ports::SerialPort();
p1->BaudRate = 57600;
p1->PortName = "COM3";
if(p1->IsOpen)
return 0;
else {
p1->Open();
return 1;
}
}
int SerialPort::check()
{
array<String^>^ serialPorts = nullptr;
bool flag = true;
serialPorts = p1->GetPortNames();
for each(String^ port in serialPorts)
{
if(port=="COM3")
flag= true;
}
return flag;
}
void SerialPort::closePort()
{
p1->Close();
}
void SerialPort::send(Byte data)
{
array<unsigned char>^ buffer = gcnew array<Byte>(1);
buffer[0] = (char)data;
p1->Write(buffer,0,1);
}
double SerialPort::read()
{
double data;
data = p1->ReadByte();
return data;
}
}
header
#define PHANTOMADAPTER_API __declspec(dllexport)
#else
#define PHANTOMADAPTER_API __declspec(dllexport)
#endif
#using <mscorlib.dll>
#using <system.dll>
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
extern "C" {
PHANTOMADAPTER_API int ready();
PHANTOMADAPTER_API int open();
PHANTOMADAPTER_API int close();
PHANTOMADAPTER_API int angle(double& angle);
PHANTOMADAPTER_API int torque(double torque);
}
namespace Comms
{
public ref class SerialPort
{
private:
static System::IO::Ports::SerialPort^ p1;
public:
static int openPort();
static void closePort();
static double read();
static void send(Byte data);
static int check();
};
}
I am getting the following error when I call the angle DLL function or any function that requires the Comms namespace.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.IO.Ports.SerialPort.get_IsOpen()
at System.IO.Ports.SerialPort.ReadByte()
at angle(Double* angle)
can someone please point me in the right direction, I feel as if the serialPort class can't be open from runtime library unless I import it somehow

override signal handler in gtkmm

I struggled with the following code. My signal handler on_button_press_event() is never called but I have no idea why. Could someone have a look on it? Maybe someone is able to run through the gtkmm lib with debug infos. I only have the pre-installed gtkmm packages which could not be used for debugging into the library itself.
#include <iostream>
using namespace std;
#include <gtkmm.h>
#include <goocanvasmm.h>
bool MyExternalHandler( const Glib::RefPtr<Goocanvas::Item>& item, GdkEventButton* ev )
{
cout << "External Handler" << endl;
return false;
}
class MyRect : public Goocanvas::Rect
{
public:
MyRect( double x, double y, double w, double h)
//: Goocanvas::Rect( x,y,w,h)
{
property_x()=x;
property_y()=y;
property_width()=w;
property_height()=h;
}
public:
virtual void nonsens() {}
bool on_button_press_event(const Glib::RefPtr<Item>& target, GdkEventButton* event) override
{
cout << "override handler" << endl;
return false;
}
bool Handler( const Glib::RefPtr<Goocanvas::Item>& item, GdkEventButton* ev )
{
cout << "via mem_fun" << endl;
return false;
}
bool on_enter_notify_event(const Glib::RefPtr<Item>& target, GdkEventCrossing* event) override
{
cout << "override enter notify" << endl;
return false;
}
};
int main(int argc, char* argv[])
{
Gtk::Main app(&argc, &argv);
Goocanvas::init("example", "0.1", argc, argv);
Gtk::Window win;
Goocanvas::Canvas m_canvas;
m_canvas.set_size_request(640, 480);
m_canvas.set_bounds(0, 0, 1000, 1000);
MyRect* ptr;
Glib::RefPtr<MyRect> m_rect_own(ptr=new MyRect(225, 225, 150, 150));
m_rect_own->property_line_width() = 1.0;
m_rect_own->property_stroke_color() = "black";
m_rect_own->property_fill_color_rgba() = 0x555555ff;
Glib::RefPtr<Goocanvas::Item> root = m_canvas.get_root_item();
root->add_child( m_rect_own);
((Glib::RefPtr<Goocanvas::Item>&)m_rect_own)->signal_button_press_event().connect(sigc::ptr_fun(&MyExternalHandler));
((Glib::RefPtr<Goocanvas::Item>&)m_rect_own)->signal_button_press_event().connect(sigc::mem_fun(*ptr, &MyRect::Handler));
win.add(m_canvas);
win.show_all_children();
Gtk::Main::run(win);
return 0;
}
Your on_button_press_event() method is not an override, because it has the wrong parameters:
https://developer.gnome.org/gtkmm/unstable/classGtk_1_1Widget.html#aba72b7f8655d1a0eb1273a26894584e3

PCL create a pcd cloud

This is what I have so far and I want to save pcd file from it
I know I have to do something like this but not exactly sure
pcl::PointCloud::PointPointXYZRGBA> cloud;
pcl::io:;savePCDFileASCII("test.pcd",cloud);
what do i have to add in my current code that i will have test.pcd
Thanks
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/openni_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/common/time.h>
class SimpleOpenNIProcessor
{
public:
SimpleOpenNIProcessor () : viewer ("PCL OpenNI Viewer") {}
void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr &cloud)
{
static unsigned count = 0;
static double last = pcl::getTime ();
if (++count == 30)
{
double now = pcl::getTime ();
std::cout << "distance of center pixel :" << cloud->points [(cloud->width >> 1) * (cloud->height + 1)].z << " mm. Average framerate: " << double(count)/double(now - last) << " Hz" << std::endl;
count = 0;
last = now;
}
if (!viewer.wasStopped())
viewer.showCloud (cloud);
}
void run ()
{
// create a new grabber for OpenNI devices
pcl::Grabber* interface = new pcl::OpenNIGrabber();
// make callback function from member function
boost::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f =
boost::bind (&SimpleOpenNIProcessor::cloud_cb_, this, _1);
// connect callback function for desired signal. In this case its a point cloud with color values
boost::signals2::connection c = interface->registerCallback (f);
// start receiving point clouds
interface->start ();
// wait until user quits program with Ctrl-C, but no busy-waiting -> sleep (1);
while (true)
boost::this_thread::sleep (boost::posix_time::seconds (1));
// stop the grabber
interface->stop ();
}
pcl::visualization::CloudViewer viewer;
};
int main ()
{
SimpleOpenNIProcessor v;
v.run ();
return (0);
}
#include <iostream>
#include <string>
#include <sstream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/io/openni_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
using namespace std;
const string OUT_DIR = "D:\\frame_saver_output\\";
class SimpleOpenNIViewer
{
public:
SimpleOpenNIViewer () : viewer ("PCL Viewer")
{
frames_saved = 0;
save_one = false;
}
void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr &cloud)
{
if (!viewer.wasStopped()) {
viewer.showCloud (cloud);
if( save_one ) {
save_one = false;
std::stringstream out;
out << frames_saved;
std::string name = OUT_DIR + "cloud" + out.str() + ".pcd";
pcl::io::savePCDFileASCII( name, *cloud );
}
}
}
void run ()
{
pcl::Grabber* interface = new pcl::OpenNIGrabber();
boost::function<void (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr&)> f =
boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
interface->registerCallback (f);
interface->start ();
char c;
while (!viewer.wasStopped())
{
//sleep (1);
c = getchar();
if( c == 's' ) {
cout << "Saving frame " << frames_saved << ".\n";
frames_saved++;
save_one = true;
}
}
interface->stop ();
}
pcl::visualization::CloudViewer viewer;
private:
int frames_saved;
bool save_one;
};
int main ()
{
SimpleOpenNIViewer v;
v.run ();
return 0;
}
Here you go.