Im making a xor gate in SystemC, from the binding of four NAND gates. I want the module to receive a vector of N bits, where N is passed as parameter. I should be able to perform & and not bitwise operations (for the NAND gate).

The best solution may be using sc_bv_base type, but I don't know how to initialize it in the constructor.

How can I create a bit vector using a custom length?

有帮助吗?

解决方案

A way to parameterise the module is to create a new C++ template for the module.

In this example, the width of the input vector can be set at the level of the instantiation of this module

#ifndef MY_XOR_H_
#define MY_XOR_H_

#include <systemc.h>       

template<int depth>
struct my_xor: sc_module {
    sc_in<bool > clk;
    sc_in<sc_uint<depth> > din;
    sc_out<bool > dout;

    void p1() {                                                           
        dout.write(xor_reduce(din.read()));
    }

    SC_CTOR(my_xor) {
        SC_METHOD(p1);
        sensitive << clk.pos();
    }

};
#endif /* MY_XOR_H_ */

Note that the struct my_xor: sc_module is used i.s.o. the SC_MODULE macro. (See page 40 , 5.2.5 SC_MODULE of the IEEE Std 1666-2011).

You can test this with the following testbench:

//------------------------------------------------------------------
// Simple Testbench for xor file
//------------------------------------------------------------------

#include <systemc.h>
#include "my_xor.h"

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

    const int WIDTH = 8;

    sc_signal<sc_uint<WIDTH> > din;
    sc_signal<bool> dout;

    sc_clock clk("clk", 10, SC_NS, 0.5);   // Create a clock signal

    my_xor<WIDTH> DUT("my_xor");           // Instantiate Device Under Test

    DUT.din(din);                          // Connect ports
    DUT.dout(dout);
    DUT.clk(clk);

    sc_trace_file *fp;                  // Create VCD file
    fp = sc_create_vcd_trace_file("wave");     // open(fp), create wave.vcd file
    fp->set_time_unit(100, SC_PS);      // set tracing resolution to ns
    sc_trace(fp, clk, "clk");             // Add signals to trace file
    sc_trace(fp, din, "din");
    sc_trace(fp, dout, "dout");

    sc_start(31, SC_NS);                // Run simulation
    din = 0x00;
    sc_start(31, SC_NS);                // Run simulation
    din = 0x01;
    sc_start(31, SC_NS);                // Run simulation
    din = 0xFF;
    sc_start(31, SC_NS);                // Run simulation

    sc_close_vcd_trace_file(fp);        // close(fp)

    return 0;
}

Note that I'm using a struct and not a class. A class is also possible.

class my_xor: public sc_module{
public:

The XOR in this code is just the xor_reduce. You can find more about in the IEEE Std 1666-2011 at page 197 (7.2.8 Reduction operators). But I assume this is not the solution you wanted to have.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top