modules and processes 2pages 2012_l3

Upload: gagan-chopra

Post on 14-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    1/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 1

    Dr. Zhonghai Lu

    School for ICT

    KTH The Royal Institute of Technology

    KTH IL2452 System Design Languages

    C++ Basics SystemC

    Introductionand Get

    Started

    Concurrency

    and Time

    SystemC

    Data Types

    and

    Debugging

    Modules

    and

    Processes

    Channels

    and

    Interfaces

    Transaction

    Level

    ModelingSystemC

    Summary

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    2/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 2

    Aim Understand the building blocks of SystemC and their

    natures

    Master how to design with modules and processes

    Content Modules

    Module syntax

    Module instantiation

    Module constructor alternatives

    Ports and signals, built-in primitive channels

    Processes

    Method vs. Thread

    Static sensitivity vs. dynamic sensitivity

    Static process vs. dynamic process

    KTH IL2452 System Design Languages

    Modules are classes in C++, and similar to

    entity in VHDL.

    Modules are the basic building blocks,

    allowing to partition complex systems into

    smaller components.

    Modules are typically hierarchical.

    KTH IL2452 System Design Languages

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    3/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 3

    Modules may

    contain

    Ports

    Signals, channels

    Processes

    Constructors

    Local data

    Other modules

    KTH IL2452 System Design Languages

    // Header fileSC_MODULE(module_name) {

    Port declarations

    Local channel declarations

    Data member declarations

    Process declarationsOther method declarations

    Module instantiations

    SC_CTOR(module_name){Process registration

    Static sensitivity list

    Module initializationModule instance / channel binding

    }} ;

    Declare using the SC_MODULE macro

    Using class declaration, deriving from sc_module

    Using struct declaration

    Members of a struct are public unless otherwise

    specified.

    SC_MODULE(Name) {.

    }

    class Name : public sc_module {

    public:

    }

    struct Name : sc_module {

    }

    KTH IL2452 System Design Languages

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    4/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 4

    Ports

    Processes: registered memberfunctions( methods)

    Constructor Process registration

    Sensitivity list

    SC_MODULE (myDesign) {

    //ports, methods, internal data, etc.

    SC_CTOR (myDesign) {

    //body of constructor;

    //process declaration, sensitivities, etc.

    }};

    ++a

    b

    f

    SC_MODULE (Adder) {

    sc_in a;sc_in b;

    sc_out f;

    void compute(){f = a + b;

    }

    SC_CTOR (Adder) {

    SC_METHOD(compute);

    sensitive

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    5/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 5

    How to instantiate? Instantiation alternatives:

    Static way

    Dynamic via pointers

    Where to instantiate?

    Within modules

    In sc_main()

    KTH IL2452 System Design Languages

    Instantiation within

    modules

    A module instance is

    a data member

    When instantiating amodule, a string

    name must be passed

    as an argument to the

    constructor.

    When instantiating

    signals, the string

    name is optional.

    +ab + fi

    SC_MODULE (Adder3In) {sc_in a;

    sc_in b;

    sc_in c;sc_out f;

    Adder adder1, adder2;sc_signal i;

    SC_CTOR (Adder3In): adder1(adder1), adder2(adder2), i(internal) {

    adder1.a(a);

    adder1.b(b);adder1.f(i);

    adder2.a(i);

    adder2.b(c);

    adder2.f(f);}};

    c

    adder1 adder2

    Initializer list

    Module instance is

    a data member

    KTH IL2452 System Design Languages

    fab

    fab

    Adder3In

    Port-to-channel binding

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    6/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 6

    //main.cpp

    #include systemc.h

    #include adder3in.hint sc_main(int argc, char* argv[]){

    sc_signal ta(ta), tb(tb), tc(tc), tf(tf);

    Adder3In myAdder(myAdder);

    myAdder.a(ta);myAdder.b(tb);

    myAdder.c(tc);myAdder.f(tf);

    sc_start();return 0;

    }

    Instantiation within

    sc_main()

    A module instance is

    a variable

    Review: Named

    binding of ports to

    signals, which are

    primitive channels. Positional binding is

    discouraged.

    +ab + fi

    SC_MODULE (Adder3In) {sc_in a;

    sc_in b;

    sc_in c;sc_out f;

    };

    c

    adder1 adder2

    Named binding

    of ports to

    signal channels

    Module instance is

    a variable

    KTH IL2452 System Design Languages

    Adder3In

    Can be used for

    instantiation both

    within modules and

    within sc_main()

    Dynamic memory

    allocation

    Requires Destructor

    to release memory

    after completion

    SC_MODULE (Adder3In) {

    sc_in a;

    sc_in b;sc_in c;

    sc_out f;

    Adder *adder1, *adder2;

    sc_signal i;

    SC_CTOR (Adder3In)

    : adder1(adder1), adder2(adder2), i(internal) {adder1 = new Adder(adder1);

    adder2 = new Adder(adder2);

    adder1->a(a);

    adder1->b(b);

    adder1->f(i);

    adder2->a(i);

    adder2->b(c);adder2->f(f);}

    ~Adder3In() { delete adder1; delete adder2;}};

    Pointer to sc_module

    Allocate memory space Give a string name

    Call using -> instead of.

    KTH IL2452 System Design Languages

    Memory de-allocation

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    7/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 7

    LIMITATION OF SC_CTOR Delcares a constructor for class

    sc_module with one (and onlyone) argument: the modulename.

    Explicit constructor like C++ Support any number of

    arguments

    Of any constructor for classsc_module, it must have anargument of typesc_module_name.

    This name is passed to the base

    class constructor in theinitializer list of the constructor.

    Strictly, this can be skipped sincethe SystemC library takes care ofthis passage.

    However, without SC_CTOR,using SC_METHOD or SC_THREADwill cause compile-time errors.

    SC_CTOR(ModuleName): initializer list { }

    Eg.SC_CTOR (Adder3In)

    : adder1(adder1), adder2(adder2), i(internal) { }

    ClassName( sc_module_name_n, otherarguments): initializer list { }

    Eg.Adder3In(sc_module_name_n, const string s =

    internal): sc_module(_n), i(s) { }

    Adder3In(sc_module_name _n, const string s =internal)

    : sc_module(_n), i(s) { }

    SC_HAS_PROCESS solves the problem!

    KTH IL2452 System Design Languages

    It is used only necessarywithin a module ifSC_METHOD or SC_THREADis used but withoutSC_CTOR.

    It allows an arbitrary

    number of parameters tocreateparameterisedmodules.

    Moreover, it allows toseparate Constructordefinition from declaration.

    //counter.h

    #include systemc.h

    Class Counter: public sc_module {public:

    sc_in clock, reset;

    sc_out q;

    Counter(sc_module_name _n, int _mod): sc_module(_n), cnt(0), moduluo(_mod) {

    SC_METHOD(count);

    sensitive

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    8/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 8

    Separated constructor declaration and definition.

    The SC_CTOR macro can be used within the class definition butNot when defining the constructor body in the .cpp file.

    //counter.h

    #include systemc.h

    Class Counter: public sc_module {public:

    sc_in clock, reset;

    sc_out q;

    Counter(sc_module_name _n, int _mod);

    SC_HAS_PROCESS(Counter);

    private:

    void count();int cnt;

    const int modulo;

    };

    //counter.cpp

    #include counter.h

    Counter::Counter(sc_module_name _n, int_mod) : sc_module(_n), cnt(0), moduluo(_mod) {

    SC_METHOD(count);sensitive

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    9/15

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    10/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 10

    Current SystemC has 5 built-in primitive channels.

    sc_buffer is derived from sc_signal

    sc_fifo is a first-in-first-out (FIFO) buffer used in

    untimed functional modeling.

    sc_mutex and sc_semaphore are used to synchronize

    processes when accessing shared resources.

    KTH IL2452 System Design Languages

    Primitive channel Events Ports

    sc_signal

    sc_buffer

    Change in value

    Write()

    sc_in, sc_out, sc_inout

    sc_in, sc_out, sc_inout

    sc_fifo Write() or read() sc_fifo_in, sc_fifo_out

    sc_mutex

    sc_semaphore

    n/a

    n/a

    n/a

    n/a

    Highlights Each process is a single sequential thread of execution

    used to model functionality or computation.

    Processes execute concurrently.

    Must be within in a module (not in a function orsc_main()).

    A module may contain any number of processes of anykind.

    Three different kinds of processes SC_METHOD methods

    SC_THREAD threads

    SC_CTHREAD clocked processes for synthesis

    Process creation Static creation before simulation starts

    Dynamic creation during simulation from SystemC 2.1

    KTH IL2452 System Design Languages

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    11/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 11

    Methods Repeated conditional execution

    with three stages Activation (triggered by events) ->

    Execution -> Return control to thekernel

    Cannot be suspended (cannot callwait())

    No infinite loop

    SC_METHOD(process_name)

    Threads Execute once, but can use an

    infinite loop to always keep it alive

    Can be suspended

    SC_THREAD(process_name)

    Cthreads Execute like methods, but only

    triggered by clock pulses

    SC_CTHREAD(process_name,clock value);

    SC_MODULE(Register){

    sc_in d;sc_out q;

    SC_CTOR(Register){SC_METHOD(process);

    sensitive

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    12/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 12

    SC_THREAD is more general and powerful thanSC_METHOD.

    With SC_THREAD, simulation is typically slowerthan SC_METHOD.

    SC_THREAD and SC_METHOD may be chosen toimplement the same functionality. But due totheir difference, the treatment will likely bedifferent. For example,

    Local variables can be used to store the state ofthread processes.

    In method processes, local variables cannot be usedfor this purpose. Instead, data members have to beused.

    KTH IL2452 System Design Languages

    Local variables can be used to store the state in an infinitely loopedthread since they never terminate.

    The same code will not work if simply changing SC_THREAD toSC_METHOD.

    //counter.h

    #include systemc.h

    class Counter: public sc_module {

    public:sc_in clock, reset;

    sc_out q;Counter(sc_module_name _n, int _mod)

    : sc_module(_n), modulo(_mod) {SC_THREAD(count);

    sensitive

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    13/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 13

    Local variables cannot be used to store the statein method processes. However, data memberscan be used.

    //counter.h

    #include systemc.h

    class Counter: public sc_module {public:

    sc_in clock, reset;

    sc_out q;Counter(sc_module_name _n, int _mod)

    : sc_module(_n), cnt(0), modulo(_mod) {

    SC_METHOD(count);

    sensitive

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    14/15

    9/6/2012

    KTHIL2452SystemDesignLanguagesLecture3 14

    SystemC 2.1 added dynamic processes, which

    are processes created during simulation.

    Using the global function sc_spawn().

    Spawned processes can have all the same

    properties as static processes.

    Both method and thread processes can be

    spawned and can have static sensitivity list.

    To use dynamic processes, the OSCI

    reference simulator requires the macro

    SC_INCLUDE_DYNAMIC_PROCESSES, though it

    is not strictly part of IEEE standard 1666.

    KTH IL2452 System Design Languages

    Spawn a thread process during simulation

    time

    KTH IL2452 System Design Languages

    #define SC_INCLUDE_DYNAMIC_PROCESSES

    SC_MODULE(SpawnModule){

    void thread_1(){

    for(;;) {wait(100, SC_NS);. }

    }

    void thread_2(){

    wait(200, SC_NS);

    sc_spawn(sc_bind(&thread_1));

    }

    SC_CTOR(SpawnModule) {SC_THREAD(thread_2);}

    };

  • 7/30/2019 Modules and Processes 2pages 2012_L3

    15/15

    9/6/2012

    KTHIL2452SystemDesignLanguages

    Modules Module construction via inheriting sc_module:

    SC_MODULE(Module_name), struct, class

    Module instantiation Objects, pointers

    Inside a module or sc_main()

    Alternative constructors SC_CTOR() in module definition with one and only one argument.

    SC_HAS_PROCESS enables multiple arguments, and move the moduleconstructor to .cpp.

    Ports and signals, 5 built-in primitive channels

    Processes Basic unit of execution Registered class functions with the kernel

    Threads vs. methods

    Static sensitivity vs. dynamic sensitivity

    Static vs. dynamic processes

    KTH IL2452 System Design Languages

    www.systemc.org

    www.doulos.com

    SystemC: From the Ground Up by D. C. Black,J. Donovan, B. Bunton, and A. Keist.

    System Design with SystemC by T. Grtker, S.Liao, G. Martin, and S. Swan

    Others

    Homework:

    Try the example in this lecture, which illustratesthe differences between method and thread.

    KTH IL2452 System Design Languages