A register

A D flip-flop can store one bit. We can imagine a register as a row of D flip-flops, each storing one bit, with the possibility to load new values into all D flip-flops simultaneously.

A register implementation in SystemC is shown in Figure 6.

#include "n_bit_register.h"

SC_HAS_PROCESS(n_bit_register);

n_bit_register::n_bit_register(sc_core::sc_module_name name, int N):
    sc_module(name)
{
    reg_value_max = (1 << N) - 1;
    cout << "reg max is " << reg_value_max << "\n"; 
    SC_METHOD(update);
    sensitive << clk.pos(); 
}

void n_bit_register::update()
{
    reg_value = data_in.read();
    data_out.write(reg_value);
    // std::cout << "data_in " << data_in.read() << "\n"; 
    // std::cout << "reg_value " << reg_value << "\n"; 
    // std::cout << data_out << "\n"; 
}

Figure 6. A register in SystemC.

This the SystemC-TLM view - other views are VHDL - Verilog

The code in Figure 6 starts with an include directive. The include directive refers to header file defines a class called n_bit_register, as

class n_bit_register : sc_core::sc_module
{
  public: 
    sc_in<bool> clk;
    sc_in<int> data_in;
    sc_out<int> data_out;
    n_bit_register(sc_core::sc_module_name name, int N); 
  private:
    void update();
    int reg_value;
    int reg_value_max;
}; 

The class defines two inputs, called clk and data_in, and one output, called data_out.

The class also defines a function called update, and a variable called reg_value.

The variable reg_value will contain the actual value stored in the register.

The code in Figure 6 defines the function update to be a SystemC process. In addition, it defines the module n_bit_register to be sensitive to rising edges of the clock signal. The result of the sensitivity definition is that the function update will be called at every positive edge of the clock signal.

We see, from the contents of the function update, that it ensures that the state variable reg_value is updated at every rising edge of the clock.

An assignment of the variable data_out also done, inside the function update. This assignment ensures that the output data_out has the same value as the current value of the state variable reg_value.