Home / SystemC / SystemC . sc_find_object()

SystemC . sc_find_object()

¤ Overview

sc_object* sc_find_object( const char* );

  • sc_find_object() function cho phép tìm một object dựa trên hierarchical name.
  • sc_find_object() function sẽ trả về một pointer với loại là sc_object khi mà hierarchical name của object đó khớp với chuỗi kí tự truyền vào.
  • Ngược lại sẽ trả về một null pointer.

¤ Example

  • Tìm object của một model ( C++ class kế thừa sc_module )
// create object 
obj_peripheral = new Peripheral("peripheral");

// create API
void peripheral_write(std::string token, std::string name, unsigned long long value) {
    Peripheral *obj;
    if (!(obj = dynamic_cast<Peripheral *>(sc_find_object(token.c_str())))) {

    }
    else {
        obj->set_output_port_api(name, value);
    }
}

//call API
peripheral_write("peripheral", "clock", 1000000);
  • Tìm object của một systemc port
template<typename porttype, typename datatype>
void Peripheral::writePort(sc_object *obj, porttype port, datatype value) {
    if (!(port = dynamic_cast<porttype> (obj))) {
        /*do nothing*/
    }
    else {
        port->write(value);
    }
}
void Peripheral::get_type(sc_object *obj, std::string &type_port, std::string &type_data) {
    type_port = obj->kind();

    std::string temp_1 = typeid(*obj).name();
    size_t first = temp_1.find('<');
    size_t second = temp_1.find('>');
    const char * temp_2 = temp_1.c_str();
    for (unsigned int i = first + 1; i < second; i++)type_data += temp_2[i];
}

void Peripheral::set_output_port_api(std::string name, unsigned long long value)
{
    std::string port_name = this->basename() + std::string(".") + name;
    sc_object *object = sc_find_object(port_name.c_str());

    if (!object) {
        printf("[%10s]Error (%s) port %s is not found.\n", sc_time_stamp().to_string().c_str(), this->name(),  name.c_str());
        exit(1);
    }
    else {
        std::string type_port("");
        std::string type_data("");
        get_type(object, type_port, type_data);
        if (type_port == "sc_out") {
            if (type_data == "bool") {
                bool temp_value = (bool)value;
                sc_out<bool> *port = 0;
                writePort(object, port, temp_value);
            }
            else if (type_data == "unsigned char") {
                unsigned char temp_value = (unsigned char)value;
                sc_out<unsigned char> *port = 0;
                writePort(object, port, temp_value);
            }
            else if (type_data == "unsigned int") {
                unsigned int temp_value = (unsigned int)value;
                sc_out<unsigned int> *port = 0;
                writePort(object, port, temp_value);
            }
            else if (type_data == "unsigned __int64") {
                unsigned __int64 temp_value = (unsigned __int64)value;
                sc_out<sc_dt::uint64> *port = 0;
                writePort(object, port, temp_value);
            }

        }
        else {
            printf("[%10s]Error (%s) It(%s) is not output port type .\n", sc_time_stamp().to_string().c_str(), this->name(),  name.c_str());
            exit(1);
        }
    }
}

Download :     Github

__FreeSourceC.com__

About admin

Check Also

[ SC Tutorial ] 6. Module : The Basic Unit of Execution

1.Simulation process Simulation process là đơn vị thực thi cơ bản trong SystemC. Chúng có …

Leave a Reply

Your email address will not be published. Required fields are marked *