channels and interfaces in systemc

Upload: rajendra-nayak

Post on 06-Jul-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/18/2019 Channels and Interfaces in SystemC

    1/16

    Dr. Zhonghai LuKTH Royal Institute of Technology

    2

    C++ Basics SystemCIntroductionand GetStarted

    Concurrencyand Time

    SystemCData TypesandDebugging

    ModulesandProcesses

    ChannelsandInterfaces

    TransactionLevelModeling

    SystemCSummary

    KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    2/16

    Aim

    Understand communication concepts in SystemCand know how to use it for design

    ContentPrimitive channels and interfacesHow to use primitive channels?How to design my own channels?Ports (used by modules to access channels)Hierarchical channelsHow to design and use my own hierarchicalchannels?

    3KTH IL2452 System Design Languages

    Concept for communicationChannel categories

    Primitive channelsHierarchical channels

    4KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    3/16

    In SystemC, channels model communication enablingthe ESL design concepts.

    Separate communication from computation whileoffering interface.Refine communication while keeping the interfacesuntouched.Fast simulation using function calls for communication.

    An example

    CPU ASIC

    MEM I/O

    ArbiterAbstract Bus

    5KTH IL2452 System Design Languages

    An interface is declared as a set of methods ( pure virtualfunctions ), like APIs (Application Programming Interfaces) forhardware and software.An interface is an abstract base class of the channel.

    A channel implements an interface.A module calls interface methods via a port. Note that, within ina module, accessing a channel can be done without using ports.The port definition specifies the type of interface required by themodule in order to access the channel.

    Interface

    6KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    4/16

    Primitive channels

    Implement one or more interfacesDerived from sc_prim_channelHave access to the update phase of the scheduler

    Examplessc_signal, sc_signal_resolvedsc_buffersc_fifo

    sc_mutex, sc_semaphoreTo understand the built-in primitivechannels, we need to know their associatedevents and ports altogether.

    7KTH IL2452 System Design Languages

    sc_signal vs. sc_buffersc_buffer is derived from sc_signal, and thus shares all thesame methods and specialized port types.They differ in what constitutes an event . For sc_signal , anevent is a change in the value of the signal ; With sc_buffer,every call to the write() method causes an event , whether ornot the value of the sc_buffer changes.sc_signal for modeling hardware wire or bus(sc_signal_resolved), sc_buffer for modeling data flow whereeach new input data will be sampled irrespective of if it is thesame or different from the previous sample.

    Primitive channel Event Port

    sc_signalsc_buffer

    change in valuewrite()

    sc_in, sc_out, sc_inout

    sc_fifo write() or read() sc_fifo_in, sc_fifo_out

    sc_mutexsc_semaphore

    --

    --

    8KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    5/16

    sc_fifo is a first-in-first-out buffer, widelyused in untimed functional modeling.sc_mutex vs. sc_semaphore

    Used to synchronzize accesses to shared

    resources.Sc_mutex is for a single shared resource whilesc_semaphore is more general, used for multipleshared resources.Modeling operating systems and software.

    Primitive channel Event Port

    sc_signal

    sc_buffer

    change in value

    write()

    sc_in, sc_out, sc_inout

    sc_fifo write() or read() sc_fifo_in, sc_fifo_out

    sc_mutexsc_semaphore

    --

    --

    9KTH IL2452 System Design Languages

    Template classBounded FIFO with adefault depth 16,

    minimum depth 1Infinite FIFO defined intlm::tlm_fifo

    Interface methodsNonblocking read andwriteBlocking read andwrite

    Specialized ports for

    accessing SC_FIFO,without using “->”operator

    sc_fifo fifo(“fifo”, 5)

    void fifo.read(T &); //wait if emptyvoid fifo.write(const T &); //wait if full

    bool fifo.nb_read(T &); //false if emptybool fifo.nb_write(const T &); //false if full

    sc_fifo_out fifo_out; //for writesc_fifo_in fifo_in; //for read

    10KTH IL2452 System Design Languages

    sc_fifo_in is equivalent to sc_port >

  • 8/18/2019 Channels and Interfaces in SystemC

    6/16

    SC_MODULE(Producer) {sc_fifo_out fifo_out;

    ….fifo_out . write(data);….

    };

    SC_MODULE(Consumer) {sc_fifo_in fifo_in;

    ….fifo_in . read(data);….

    };

    SC_MODULE(Top) {Producer p;Consumer c;sc_fifo fifo;

    SC_CTOR(Top): p(“p”), c(“c”), fifo(“fifo”,5){p.fifo_out(fifo);c.fifo_in(fifo);….

    };

    Producer Consumer

    Top sc_fifo

    11KTH IL2452 System Design Languages

    Specialized port

    “.” operator

    Step1: Declare an abstract interface classderived from sc_interface

    Virtual inheritance to allow multiple inheritancePure virtual functions because it is mandatory forusers to supply the implementation

    Example: Queue

    class queue_write_if : virtual public sc_interface {public: virtual void write (int c) = 0;};

    class queue_read_if : virtual public sc_interface {public: virtual int read () = 0;};

    class queue_if : public queue_write_if, public queue_read_if { };

    12KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    7/16

    Declare the queue class asour own channel

    The interface class isdeclared but does nothing.It is the channel’sresponsibility to overridethe pure virtual methodsand describe functionality.Derived from sc_object .

    Since the queue channel isneither a hierarchical nor aprimitive channel, it isrecommended that suchchannels are at least derived

    from sc_object so that theyown attributes such as ahierarchical name and aposition in the modulehierarchy.sc_object is the base class forall objects in the modulehierarchy.

    #include “queue_if.h”

    class Queue : public queue_if , public sc_object {public:

    Queue(char *_nm, int _size): sc_object(_nm),size(_size) {

    data = new int[size];w=r=n=0;}

    void write(int c);int read();

    private:int* data;int size, w, r, n;

    };

    13KTH IL2452 System Design Languages

    w: write pointerr: read pointern: number of data

    in the queue

    Class sc_object isthe base class forall objects in the

    module hierarchy.

    sc_object

    +name+kindget_child_objectsget_parent_objects

    sc_module

    +kindget_child_objects

    sc_prim_channel

    +kind

    sc_port_base

    +kind

    sc_process*

    +kind+get_child_objects

    14KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    8/16

    Define interface methods

    With the same interfacedeclaration, definitionsmay be different.

    #include “queue_if.h”

    void Queue :: write(int c) {if (n < size) {

    n++;data[w++] = c;if (w == size) w = 0;

    }};

    int Queue::read(){int c = 0;if (n > 0) {

    n--;c = data[r++];if (r == size) r = 0;

    }return c;

    }

    15KTH IL2452 System Design Languages

    We write a module totest the functionality

    of the channel.use port-less channelaccess, i.e. access thechannel withoutdeclaring ports .

    class Test : public sc_module {

    public:Queue queue;

    SC_CTOR(Test): queue(“queue”, 10){SC_THREAD(testing);

    }

    void testing(){for(int i=0; i

  • 8/18/2019 Channels and Interfaces in SystemC

    9/16

    Class sc_port , a class template with three parameters, isthe base class for all ports in SystemC.

    Interface typeThe max. number of channels to which the port may be bound(0 means no limit, default is 1).Port binding policy (optional) with respect to unbounded ports.

    sc_port if1, if2, …;

    template class sc_in : public sc_port { … };

    templateclass sc_fifo_in : public sc_port { …};

    Port definitions can use the class sc_port directly or use aport specialization, which is a class dervied from sc_port,for example, sc_in, sc_fifo_in.

    17KTH IL2452 System Design Languages

    Channel: interface + implementationModule uses ports to access the channel interface

    channel

    Module

    Process

    Required interface: sif

    Offered interface: sif

    sc_port< sif > port;

    port->method(); struct sif : virtual sc_interface {virtual void method() = 0;};

    struct channel: sif , sc_object {void method() { …}

    };

    Inherit sc_module ifhierarchical channel

    The port is declared with interface sif , so a process may call the method method()through the port.Note that the port is typed using the name of the interface, not the channel. Thus, anychannel that implements the interface sif may be bound to the port.

    18KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    10/16

    Define interface-typed portsDefine a process to call an interface method

    Through the typed port out , call the write() method offered bya queue_write_if . Which channel (queue) to use is decided bythe Port-to-Channel binding.

    Producer Consumer

    Top

    queue

    queue_write_if queue_read_if

    SC_MODULE(Producer){

    public:sc_port out;void write_chan();

    SC_CTOR(Producer){SC_THREAD(write_chan); }

    };

    void Producer :: write_chan(){

    for(int i=0; i write(i);

    }

    Overloaded operator ->Interface method call (IMC)

    19KTH IL2452 System Design Languages

    The syntax for binding sc_port instances to channels is thesame as we would use for one of the standard signal ports,like binding sc_in , sc_out ports to sc_signals .

    Producer Consumer

    Top

    queue

    queue_write_if queue_read_if

    SC_MODULE(Top){Queue queue;Producer producer;Consumer consumer;

    SC_CTROR(Top):queue(“queue”, 5){producer.out(queue);consumer.in(queue);

    }};

    Channel instance

    Port to channel binding

    Module instance

    20KTH IL2452 System Design Languages

    out in

  • 8/18/2019 Channels and Interfaces in SystemC

    11/16

    Besides functional methods, one can add

    debug facilities to an interface.For example, add a dump() method that writesout the content of the queue at a time instance.

    class queue_debug_if : virtual public sc_interface {public:

    virtual void dump const() = 0;};

    class Queue : public queue_if, public queue_debug_if , public sc_object {

    public:void write(int c);int read();

    void dump() const;};

    21KTH IL2452 System Design Languages

    Different from primitive channels,hierarchical channels are modules which may

    have ports and sub-modules .Used to model complex communication, suchas network on chip.The distinction between module and channelbecomes fuzzy because of hierarchialchannels, but any channel must have welldefined interfaces .

    22KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    12/16

    1. Define the channel interface

    2. Implement the channel3. Port creation: Create ports ( interface-typed ) at

    the user moduleeg. sc_port out;The user module uses the interface method calleg. out->write(i);

    4. Port-to-Channel binding: Bind ports of the usermodule to the channel instance

    Example: A stack channelDefine a write and a read interface, usingnonblocking method.

    23KTH IL2452 System Design Languages

    Define a write and a read interface, usingnonblocking method.

    #ifndef STACK_IF_H#define STACK_IF_H#include

    class stack_write_if : virtual public sc_interface {public:

    virtual bool nb_write(char) = 0; //write a charactervirtual void reset()=0; //empty the stack

    };

    class stack_read_if : virtual public sc_interface {

    public:virtual bool nb_read(char &) = 0; //read a character};

    #endif 24KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    13/16

    The stack channel implements the methods.#ifndef STACK.H#define STACK.H#include #include “stack.h”class stack : pubic sc_module , public stack_read_if, stack_write_if {

    private:char data[100];int top; //top of the stack

    public:stack(sc_module_nm nm) : sc_module(nm), top(0){ }

    bool nb_write (char c) {if (top 0) { c = data[--top]; return true;}return false;}

    };#endif

    25KTH IL2452 System Design Languages

    To use the stack, it must be instantiated. We useagain a producer and a consumer.

    #ifndef PRODUCER.H#define PRODUCER.H#include #include “stack_if.h”class producer : pubic sc_module {

    public:sc_port out; //declare a port that interfaces to the stacksc_in clock;

    void write_stack (){int i=0; char* TestString=“Hello world”;while true {

    wait(); //wait for clock edgeif ( out->nb_write(TestString[i] ) //write to the stack via the port calling nb_write() method

    cout

  • 8/18/2019 Channels and Interfaces in SystemC

    14/16

    #include ”systemc.h”#include ”producer.h”

    #include “consumer.h”#include “stack.h”

    int sc_main(int argc, char* argv[]){sc_clock ClkFast(“ClkFast”, 100, SC_NS);sc_clock ClkSlow(“ClkSlow”, 50, SC_NS);

    stack Stack1(“Stack 1”);producer P1(“Producer 1”);P1.out(Stack1);P1.clock(ClkFast);

    consumer C1(“Consumer 1”);C1.in(Stack1);C1.clock(ClkSlow);

    sc_start(5000, SC_NS);return 0;

    } 27KTH IL2452 System Design Languages

    Processes communicate with channelsChannels inside the module: directly calling the

    channel methods (port-less access)Channels outside the module: indirectly callingvia port(s) of the module

    Create Interface-typed portUse interface method call

    28KTH IL2452 System Design Languages

  • 8/18/2019 Channels and Interfaces in SystemC

    15/16

    SystemC concepts in C++Interfaces

    Abstract classChannels

    Class implementation of interfacesPorts

    Template class sc_portSpecialized ports: sc_in, sc_out, sc_fifo_in, sc_fifo_out

    ChannelsPrimitive channels: sc_signal, sc_fifo, sc_buffer.Hierarchical channels are essentially modules but with interfaces

    We can follow well-defined steps to build own channels withappropriate interfaces.

    Homework :Try the example of creating own channel Queue.

    KTH IL2452 System Design Languages 29

    TLM whitepaper “Transaction Level Modelingin SystemC by Rose, Swan et al.

    Download TLM-1.0 package for TLMexamples.

    KTH IL2452 System Design Languages 30

  • 8/18/2019 Channels and Interfaces in SystemC

    16/16

    www.systemc.org, www.accelera.org

    www.doulos.comSystemC: From the Ground Up by D. C. Black,J. Donovan, B. Bunton, and A. Keist.System Design with SystemC by T. Grötker, S.Liao, G. Martin, and S. SwanOthers

    31KTH IL2452 System Design Languages