73
SKKU 휴휴휴휴휴 © 휴휴휴 2008 1 조 조 조 2008.1 SoC 휴휴휴휴휴 - 휴휴

SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

Embed Size (px)

Citation preview

Page 1: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 1

조 준 동

2008.1

SoC 설계방법론 - 실습

Page 2: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 2

1. SystemC 설치

1. SystemC 가져오기

1) 홈페이지 http://www.systemc.org 를 방문한다 .

2) ‘Create New Account’ 를 클릭하여 사용자 등록을 하고 , 로그인을 한다 .

3) 오른쪽에 ‘ Quick Links’ 에 ‘ SystemC.2.1.v1’ 을 클릭한다 .

4) ‘systemc-2.0.1.exe’ 를 다운받는다 .

2. 설치하기

1) 압축을 풀어 ‘ C:\SystemC’ 를 설치하고 C:\SystemC\systemc-2.0.1\msvc60\systemc’ 디렉터리로 이동한다 .

2) 해당 디렉터리에 있는 ‘ systemc.dsw’ 가 Visual C++ 를 위한 파일이다 . 이를 두 번 클릭하면 Visual C++ 창이 나타난다 .

3) ‘Build’→‘Set Active Configuration…’ 을 선택하고 나오는 창에 ‘ Release’ 또는 ‘ Debug’를 선택한다 .

4) ‘Build’→‘Build systemc.lib’ 를 선택하면 SystemC 패키지가 컴파일 된다 .

5) Visual C++ 을 종료한다 .

6) ‘…\msvc60\systemc\Release’ 또는 ‘… \msvc60\systemc\Debug’ 디렉터리에 SystemC 라이브러리파일 (systemc.lib) 이 있는지 확인한다 .

Page 3: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 3

2. Adder

• Simple adder program– Adder_method : SC_METHOD– Adder_thread : SC_THREAD, sensitivity – clk– Adder_thread2 : SC_THREAD, sensitivity – a, b– Adder_thread3 : SC_THREAD, sensitivity – a, b, clk– Adder_cthread : SC_THREAD

• Compare SC_METHOD, SC_THREAD, SC_CTHREAD

Page 4: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 4

2.1 Adder Main Source

// main.cpp#include "adder_method.h"#include "adder_thread1.h"#include "adder_thread2.h"#include "adder_thread3.h"#include "adder_cthread.h"#include "stimulus.h"int sc_main(int argc, char *argv[]) { sc_signal<int> A, B, R_M, R_T1, R_T2, R_T3, R_C; sc_set_time_resolution(1, SC_NS); // V2.0 sc_set_default_time_unit(1, SC_NS); sc_clock clock("clock", 20, SC_NS, 0.5, 15, SC_NS); adder_method AD_M("adder_method"); // instantiation AD_M(A, B, R_M); // port connection adder_thread AD_T1("adder_thread"); AD_T1(A, B, R_T1, clock); adder_thread2 AD_T2("adder_thread2"); AD_T2(A, B, R_T2); adder_thread3 AD_T3("adder_thread3"); AD_T3(A, B, R_T3, clock.signal()); adder_cthread AD_C("adder_cthread"); AD_C(A, B, R_C, clock.signal());

stimulus STIM("stimulus"); // instantiation STIM(A, B, clock.signal()); // port connection

// trace file creation sc_trace_file *tf = sc_create_vcd_trace_file("wave"); sc_trace(tf, clock, "clock"); sc_trace(tf, A, "A"); sc_trace(tf, B, "B"); sc_trace(tf, R_M, "R_M"); sc_trace(tf, R_T1, "R_T1"); sc_trace(tf, R_T2, "R_T2"); sc_trace(tf, R_T3, "R_T3"); sc_trace(tf, R_C, "R_C"); sc_start(80, SC_NS); return(0);}

Page 5: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 5

2.2 Adder Stimulus Source

SC_CTOR(stimulus) { SC_THREAD(do_stim); sensitive << clk; }};

// stimulus.h#include "systemc.h"

SC_MODULE(stimulus) { sc_out<int> A, B; sc_in<bool> clk;

void do_stim() { while (1) { A = 10; B = 20; wait(); A = 13; B = 11; wait(); wait(); A = 1; B = 3; wait(); A = 11; wait(); A = 100; B = 1; wait(); wait(); wait(); } }

Page 6: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 6

2.3 Adder Method Source

// adder_method.h#include "systemc.h"

SC_MODULE(adder_method) { sc_in<int> A, B; sc_out<int> C;

void do_adder() { C = A + B; }

SC_CTOR(adder_method) { SC_METHOD(do_adder); sensitive << A << B; }};

// adder_thread.h#include "systemc.h"

SC_MODULE(adder_thread) { sc_in<int> A, B; sc_out<int> C; sc_in<bool> clk;

void do_adder() { while (1) { C = A + B; wait(); } }

SC_CTOR(adder_thread) { SC_THREAD(do_adder); sensitive_pos(clk); // sensitive_pos << clk; }};

Page 7: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 7

2.4 Adder Thread Source

// adder_thread.h#include "systemc.h"

SC_MODULE(adder_thread2) { sc_in<int> A, B; sc_out<int> C;

void do_adder() { while (1) { C = A + B; wait(); } }

SC_CTOR(adder_thread2) { SC_THREAD(do_adder); sensitive(A); sensitive(B); }};

// adder_thread.h#include "systemc.h"

SC_MODULE(adder_thread3) { sc_in<int> A, B; sc_out<int> C; sc_in<bool> clk;

void do_adder() { while (1) { C = A + B; wait(); } }

SC_CTOR(adder_thread3) { SC_THREAD(do_adder); sensitive << A << B << clk.pos(); }};

Page 8: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 8

2.5 Adder Cthread Source

// adder_cthread.h#include "systemc.h"

SC_MODULE(adder_cthread) { sc_in<int> A, B; sc_out<int> C; sc_in<bool> clk;

void do_adder() { while (1) { C = A + B; wait(); } }

SC_CTOR(adder_cthread) { SC_CTHREAD(do_adder, clk.pos()); }};

Page 9: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 9

2.6 Adder Result

Page 10: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 10

3. Signal generator and receiver

• Simple signal generator and receiver

• Compare sc_signal, sc_buffer

Page 11: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 11

3.1 Signal Main Source

// main.cpp#include "systemc.h" SC_MODULE (generator) { sc_out<short> sig; sc_out<short> buf; void do_it(void) { wait(5, SC_NS); sig.write(5); buf.write(5); wait(10, SC_NS); sig.write(6); buf.write(6); wait(10, SC_NS); sig.write(6); buf.write(6); wait(10, SC_NS); sig.write(7); buf.write(7); wait(10, SC_NS); sig.write(7); buf.write(7); wait(10, SC_NS); } SC_CTOR(generator) { SC_THREAD(do_it); sig.initialize(0); buf.initialize(0); }};

SC_MODULE (receiver) { sc_in<short> iport; void do_it(void) { cout << sc_time_stamp() << ":" << name() << " got " << iport.read() << endl; } SC_CTOR(receiver) { SC_METHOD(do_it); sensitive << iport; dont_initialize(); }};

Page 12: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 12

3.2 Signal SC_Main Source

int sc_main(int argc, char *argv[]) { sc_signal<short> sig; sc_buffer<short> buf; generator GEN("GEN"); GEN.sig(sig); GEN.buf(buf); receiver REV_SIG("REV_SIG"); REV_SIG.iport(sig); receiver REV_BUF_A("REV_BUF"); REV_BUF.iport(buf); // trace file creation sc_trace_file *tf = sc_create_vcd_trace_file("wave"); sc_trace(tf, sig, "sig"); sc_trace(tf, buf, "buf"); sc_start(); sc_close_vcd_trace_file(tf); return(0);}

Page 13: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 13

3.3 Signal Result

• Result

0 s:REV_BUF got 0WARNING: Default time step is used for VCD tracing.5 ns:REV_SIG got 55 ns:REV_BUF got 515 ns:REV_SIG got 615 ns:REV_BUF got 625 ns:REV_BUF got 635 ns:REV_SIG got 735 ns:REV_BUF got 745 ns:REV_BUF got 7

Page 14: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 14

4. counter

1. 예제 설계그림과 같은 간단한 예제설계를 통해 SystemC 로 모델링하는 방법과 파일 방법 그리고 디버깅 방법에 대한 것을 살펴본다 .

Counter

(counter)

Counter

(counter)

value

Testbench

(test)

Testbench

(test)

Clock(clk)Clock(clk)

reset

clk

go

Page 15: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 15

4. counter

2. SystemC 모델

systemc.h

Makefile.defs

counter.h test.h test.cpp

Makefile

main.cpp

counter.cpp

make

run.x

Counter project directory

Page 16: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 16

4. counter

2.1 counter 모듈 counter.h : 선언 file, 모듈의 입출력과 내부구성을 표현 counter.cpp: 구현 file, 모듈의 기능을 모델링

//counter.h#include "systemc.h" // 헤더파일

//counter 모듈SC_MODULE(counter) { // 모듈 선언을 시작 , 괄호 안은 모듈 명

sc_in<bool> clk, reset, go; // 입력 포트sc_out<char> value; // 출력 포트

void do_count(); //counter 모듈의 기능을 모델링한 ‘ do_count()’ 함수를 프로토타입

unsigned char local_value; // 내부적으로 사용할 변수 선언

//counter 모듈을 어떻게 생성할 것인가를 표현한 constructorSC_CTOR(counter) { //constructor 의 시작 , 괄호 안은 모듈명과 동일 해야 함

SC_METHOD(do_count); //‘do_counter()’ 함수를 ‘ SC_METHOD’ 프로세서로 등록 sensitive << reset << go <<clk.pos(); // 등록한 프로세서가 언제 기동되는지

결정//reset, go 의 값이 변할 때와 clk 이 rising edge 일 때

local_value =0; // 내부 변수인 ‘ local_value’ 를 초기화value.initialize(0); // 시뮬레이션이 사직되기 전에 해당 출력 포트 값을 초기화

}};

Page 17: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 17

4. counter

//counter.cpp#include "counter.h" // 헤더 파일

//do_count 라는 SystemC 프로세스를 구성//C++ 에서 클래서 맴버함수와 동일한 형식을 갖지만 반드시 void, 인자들도 없어야 한다 .void counter::do_count() { //'counter' 라는 모듈에 프로세서로 선언된 'do_count'

if(reset) local_value=0; //reset=1, 초기화else if (go){ //go=1, 카운터를 동작

if (clk.posedge()) { //clk 의 rising edge 에서local_value++; //local_value 값을 1 증가 cout << "[" << sc_simulation_time() << "]"// 현재의 시뮬레이션 시간을 출력

<< (unsigned short)local_value <<endl;//'local_valu' 값 출력

}}value.write(local_value); //'local_value' 값을 출력 포트 'value' 에 인가하여 외부로

출력}

Page 18: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 18

4. counter 2.2 test 모듈 test.h : 선언 file, 모듈의 입출력과 내부구성을 표현 test.cpp: 구현 file, 모듈의 기능을 모델링

//test.h#include "systemc.h"// 헤더 파일

//test 모듈SC_MODULE(test) { // 모듈 선언을 시작 , 괄호 안은 모듈명

sc_in<bool> clock;sc_out<bool> reset, go;sc_in<char> value; // 모듈의 입출력 포트들

void do_test(); //test 모듈의 기능을 모델링한 'do_test()' 함수를 프로토타입

//constructorSC_CTOR(test) { //'test' 모듈의 constructor 시작

SC_CTHREAD(do_test, clock.neg());//'do_test()' 함수를 'SC_THREAD' 프로세서로 등록// 이때 'do_test()' 함수는 'clock' 신호의 falling edge 에서 기동reset.initialize(1);go. initialize(0); // 출력포트의 초기값 설정

}};

Page 19: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 19

4. counter

//test.cpp#include "test.h" // 헤더 파일

//'do_test()' 함수를 구현void test::do_test() { //do_test 는 복귀값 , 인자 없음 , test 모듈에 속해있음을 명시

wait(); //'wait()' 는 해당라인에서 프로세서가 정지하였다가 // 센스티비티로 등록된 사건 (falling edge) 이 발생할때까지 기다린다 .

reset.write(0); //'reset' 값을 0 으로 바꾼다 .wait();while (true) { //'SC_CTHREAD' 사용시에는 무한루프를 반드시 만들어 주어야 하고

//무한루프 안에는 'wait()' 가 한개 이상 있어야 한다 .go.write(1); //'go' 값을 1 로 바꾼다 .wait(5); //5 사이클 기다린다 .go.write(0); //'go' 값을 0 으로 바꾼다 .wait(2); //2 사이클 기다린다 .

}}

Page 20: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 20

4. counter

2.3 Top-level main.cpp : ‘counter’ 모듈과 ‘ test’ 모듈을 연결

//main.cpp#include "counter.h"#include "test.h" // 사용할 모듈을 선언

//최상위 레벨 SystemC 설계를 구성int sc_main(int argc, char* argv[]) { // 일반적인 C/C++ 의 'main()' 과 동일한 형식

// 반드시 'sc_main(int argc, char** argv)', 또는 'sc_main(int argc, char* argv[])' 이여야 한다 .//'counter' 와 'test' 모듈을 연결하는 데 사용될 신호들을 'sc_signal' 로 선언sc_signal<char> value;sc_signal<bool> reset, go;sc_clock CLK("clock", 10, SC_NS); //'sc_clock' 로 'CLK' 라는 클럭 만들기 , 주기는 10ns

//make counter module with named connection counter CNT("CNT");CNT.clk(CLK); CNT.reset(reset); CNT.go(go); CNT.value(value); //'named mapping' 방법

//make TESTBENCH module with positional connectiontest TST("TST");TST(CLK, reset, go, value); //'function-style positional mappingg' 방법 , 포트들을 어떤 순서로 선언하였는지가 매우 중요

Page 21: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 21

4. counter

//trace file creation// 신호 파형을 wave.vcd 라는 파일에 저장sc_trace_file *tf = sc_create_vcd_trace_file("wave");// 어떤 신호를 저장할 것인가를 정의 , 하위모듈의 신호 또는 멤버변수도 지정 가능sc_trace(tf, CLK, "clock"); sc_trace(tf, reset, "reset");sc_trace(tf, go, "go"); sc_trace(tf, value, "value");sc_trace(tf, CNT.local_value, "CNT.local_value");sc_start(200, SC_NS); //200ns 동안 시뮬레이션

//'sc_start(-1)' 이거나 'sc_start()' 면 'sc_stop()' 이 수행될 때까지 수행sc_close_vcd_trace_file(tf); // 시뮬레이션이 끝난 뒤 신호파형을 저장한 파일을 닫는다 . return(0);

}

Page 22: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 22

4. counter

3. 컴파일하기

Visual Studio 를 수행시키고 FileNew 메뉴를 선택하여 새로운 프로젝트를 시작한다 .

Win32 Console Application 을 선택하고 , Project name 에는 적당한 프로젝트 이름을 입력하고 , location 에는 카운터 예제를 만들 디렉토리를 선택하여 지정하고 OK를 선택한다 .

이어서 나타나는 윈도우에서 An empty project 를 선택한 후 , Finish 를 선택한다 .

그 다음 윈도우에서 정보를 확인하고 OK를 선택한다 .

Page 23: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 23

4. counter

Projectsettings 메뉴를 선택한다 .

Page 24: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 24

4. counter

C/C++ 탭에서 Category 에는 C++ Language 를 선택하고

Enable Run-Time Type Information [RTTI] 를 선택한다 .

Page 25: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 25

4. counter

같은 윈도우에서 Category 를 Preprocessor 로 선택하고 additional include directories 에 SystemC 헤더파일이 있는 디렉토리를 지정한다 .( 여기서는 C:\SystemC\systemc-2.0.1\src )

Page 26: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 26

4. counter

같은 윈도우에서 Link 탭을 선택하고 Category 에 General 를 선택한 후 , Object/library modules 에 SystemC 라이브러리인 systemc.lib 을 추가하고 OK를 선택한다 .

Page 27: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 27

4. counter

Tools Option 메뉴를 선택한다 .

Page 28: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 28

4. counter

Directories 에 Show directories for 에 Include files 를 선택 , SystemC 헤더파일의 위치를 넣어준다 .( 여기서는 C:\SystemC\systemc-2.0.1\src)

Page 29: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 29

4. counter

같은 윈도우에서 Show directories for 에 Library files 를 선택한 후 ,Directories 에 SystemC 라이브러리화일의 위치를 지정한다 .( 여기서는 C:\SystemC\systemc-2.0.1\msvc60\systemc\Release)

Page 30: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 30

4. counter

카운터 예제의 source code 중 구현파일 (main.cpp, counter.cpp, test.cpp) 과 헤더파일 (counter.h, test.h) 를 추가한다 .

Build Build counter.exe 메뉴를 선택하여 컴파일 한다 .

Build Execute counter.exe 메뉴를 선택하여 실행한다 .

실행이 잘 되었다면 카운터 예제가 있는 디렉토리 밑에 ‘ counter’ 라는 프로젝트 디렉토리가 생길 것이고 , 이곳에 수행결과인 ‘ wave.vcd’ 가 생성될 것이다 .

Page 31: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 31

4. counter

Page 32: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 32

4. counter

시뮬레이션 결과로 만들어진 ‘ wave.vcd’ 를 신호파형출력기로 보려면 ‘ winwave’ 또는 ‘ gtkwave’ 를 수행시킴으로써 가능하다 .

Page 33: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 33

4. counter

Search Signal Search Tree 를 선택한다 .

Page 34: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 34

4. counter

Page 35: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 35

4. counter

신호파형 결과는 다음과 같다 .

Page 36: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 36

5. memory

1. 예제 설계 • 메모리 모듈 ‘ memory’ 는 내부에 ‘ decoder’ 모듈과 ‘ ram’ 모듈을 갖고 , ‘testbench’모듈은 시험을 위해 준비한 것이다 .

• 여기서 ‘ decoder’ 모듈은 ‘ address’ 와 ‘ enable’ 포트를 통해 입력된 신호를 통해 요청되는 메모리 참조가 할당된 주소공간에 해당하는지 확인한다 .

• 만약 할당된 주소 공간에 해당하면 ‘ selected’ 포트를 통해 신호를 구동한다 .

• ‘ram’ 모듈은 ‘ data’ 포트를 통해 전달되는 데이터를 저장하는 기능을 한다 .

testbenchtestbench

decoderdecoder

clock (clk)clock (clk)

ramram

memory

en

rw

add

we

data

selected

enable

rdwr

address

we

data

clk

Page 37: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 37

5. memory

2. SystemC 모델

main.cppmain.cppMakefileMakefile

memory.hmemory.h

testbench.htestbench.h

testbench.cpptestbench.cpp

systemc.hsystemc.h

ram.hram.h

ram.cppram.cppdecoder.hdecoder.h

decoder.cppdecoder.cpp

Page 38: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 38

5. memory

2.1 memory 모듈

//memory.h

#include "systemc.h"

#include "ram.h"

#include "decoder.h"

SC_MODULE(memory) {

sc_in<int> address;

sc_in<bool> enable, rdwr, we;

sc_inout_rv<16> data;

ram* RAM;

decoder DECODER;

sc_signal<bool> selected;

SC_CTOR(memory) : DECODER("D") {

RAM = new ram("R");

RAM->address(address);

RAM->enable(selected);

RAM->rdwr(rdwr);

RAM->we(we);

RAM->data(data);

DECODER << address << enable << selected;

}

};

Page 39: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 39

5. memory

2.2 ram 모듈//ram.h

#include "systemc.h"

SC_MODULE(ram) {

sc_in<int> address;

sc_in<bool> enable;

sc_in<bool> rdwr; // active-high read, active-low write

sc_in<bool> we; // data written at rising edge

sc_inout_rv<16> data;

void do_ram();

SC_CTOR(ram) {

SC_METHOD(do_ram);

sensitive << address << enable << rdwr << we << data;

for (int i=0; i<256; i++) ram_data[i] = "XXXXXXXXXXXXXXXX";

}

private:

sc_lv<16> ram_data[256];

};

Page 40: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 40

5. memory

//ram.cpp

#include "ram.h"

void ram::do_ram() {

/*insert your code*/

}

Page 41: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 41

5. memory

2.3 decoder 모듈//decoder.h

#include "systemc.h"

SC_MODULE(decoder) {

sc_in<int> address;

sc_in<bool> enable;

sc_out<bool> selected;

void do_decoder();

SC_CTOR(decoder) {

SC_METHOD(do_decoder);

sensitive << address << enable;

}

};

Page 42: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 42

5. memory

//decoder.cpp

#include "decoder.h"

void decoder::do_decoder() {

/*insert your code*/

}

Page 43: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 43

5. memory

2.4 testbench 모듈//testbench.h

#include "systemc.h"

SC_MODULE(testbench) {

sc_out<int> add;

sc_out<bool> en;

sc_out<bool> rw;

sc_out<bool> we;

sc_inout_rv<16> data;

sc_in<bool> clock;

void do_test();

SC_CTOR(testbench) {

SC_CTHREAD(do_test, clock.pos());

}

private:

int read_cycle(int);

void write_cycle(int, int);

};

Page 44: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 44

5. memory

//testbench.cpp

#include "testbench.h"

void testbench::do_test() {

int i, j, flag, tmpA, tmpDW, tmpDR;

for(j=0; 1; j++) {

flag = 0;

wait(2);

tmpA = 1;

tmpDW = 123;

for (i=0; i<3; i++) {

read_cycle(tmpA+i+j);

}

for (i=0; i<3; i++) {

write_cycle(tmpA+i+j, tmpDW+i+j);

}

for (i=0; i<3; i++) {

tmpDR = read_cycle(tmpA+i+j);

if (tmpDR != (tmpDW+i+j)) {

fprintf(stderr, "Error: expeced 0x%x, but 0x%x\n", tmpDW+i+j, tmpDR);

flag=1;

Page 45: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 45

5. memory

}

}

if (!flag) fprintf(stderr, "Test passed ...\n");

}

}

int testbench::read_cycle(int iadd) {

int tmp;

add.write(iadd);

en.write(1);

rw.write(1);

we.write(0);

wait(2);

tmp = data.read().to_int();

wait(1);

add.write(0);

en.write(0);

wait(1);

return tmp;

}

void testbench::write_cycle(int iadd, int idata) {

add.write(iadd);

en.write(1);

rw.write(0);

we.write(0);

data.write(idata);

wait(1);

we.write(1);

wait(1);

add.write(0);

en.write(0);

rw.write(1);

data.write("ZZZZZZZZZZZZZZZZ");

wait(1);

}

Page 46: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 46

5. memory

2.5 main 함수//main.cpp

#include "memory.h"

#include "testbench.h"

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

sc_signal<int> add;

sc_signal<bool> en;

sc_signal<bool> rw;

sc_signal<bool> we;

sc_signal_rv<16> data;

sc_set_time_resolution(1, SC_NS);

sc_set_default_time_unit(1, SC_NS);

sc_clock CLOCK("clock", 10, 0.5, 5, true);

//make MEMORY module

/*insert your code*/

//make TESTBENCH module

/*insert your code*/

//trace file creation

sc_trace_file *tf = sc_create_vcd_trace_file("wave");

sc_trace(tf, CLOCK, "clock");

sc_trace(tf, add, "AD");

sc_trace(tf, en, "EN");

sc_trace(tf, rw, "RW");

sc_trace(tf, we, "WE");

sc_trace(tf, data, "DA");

sc_trace(tf, MEMORY.selected, "SEL");

sc_start(600, SC_NS);

sc_close_vcd_trace_file(tf);

return (0);

}

Page 47: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 47

5. memory

Page 48: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 48

6. parity generator and checker

1. 예제 설계

• ‘parity’ 모듈은 패리티 생성과 점검을 동시에 하는 것이다 .• ‘mode’ 포트의 입력이 ‘ 0’ 이면 even parity, ‘1’ 이면 odd parity 이다 .• ‘data’ 포트를 통해 8 비트를 입력 받아 패리티 비트를 생성하여 ‘ pout’

포트로 출력한다 . • 동시에 ‘ data’ 포트의 8 비트 입력과 ‘ pin’ 포트의 패리티 비트를

이용하여 패리티 비트를 점검을 하고 , 패리티 오류가 있으면 ‘ error’ 에 ‘ 1’ 을 출력하고 그렇지 않으면 ‘ 0’ 을 출력한다 .

paritygenerator/checker

paritygenerator/checker

1-bit parity_in

8-bit data

1-bit even_odd1-bit parity_error1-bit parity_out

mode

data

pinpouterror

Page 49: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 49

6. parity generator and checker

2.1 parity 모듈//parity.h

#ifndef _PARITY_H_

# define _PARITY_H_

#include "systemc.h"

SC_MODULE(parity) {

sc_in<unsigned char> data;

sc_in<bool> mode;

sc_in<bool> pin;

sc_out<bool> pout;

sc_out<bool> error;

//process and function

void do_parity_gen();

void do_parity_check();

SC_CTOR(parity) {

/*insert your code*/

}

};

#endif

Page 50: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 50

6. parity generator and checker

//parity.cpp

#include "parity.h"

/*insert your code*/

Page 51: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 51

6. parity generator and checker

paritygenerator

paritygenerator

mode

data

pin pouterror

paritychecker

paritychecker

mode

data

pinpouterror

paritychecker

paritychecker

error

data

clk

mode

parity

clockclock

top

Page 52: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 52

6. parity generator and checker

2.2 main 함수//main.cpp

//main routine for parity generator and checker

#include "top.h"

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

parity_top top("top");

sc_start(-1);

return(0);

}

Page 53: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 53

6. parity generator and checker

2.3 top 모듈 //top.h

#ifndef _TOP_H_

# define _TOP_H_

#include "systemc.h"

#include "parity.h"

#include "tester.h"

SC_MODULE(parity_top) {

/*insert your code*/

};

#endif

Page 54: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 54

6. parity generator and checker

//top.cpp

//Top-level for parity generator and checker

#include "top.h"

/*insert your code*/

Page 55: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 55

6. parity generator and checker

2.4 tester 모듈 //tester.h

#ifndef _TESTER_H_

# define _TESTER_H_

#include <stdlib.h>

#include "systemc.h"

SC_MODULE(tester) {

sc_out<unsigned char> data;

sc_in<bool> parity;

sc_out<bool> mode;

sc_in<bool> error;

sc_in<bool> clk;

//process and function

void do_gen();

void do_che();

SC_CTOR(tester) {

/*insert your code*/

srand(0); //initialize random generator

}

};

#endif

Page 56: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 56

6. parity generator and checker

//tester.cpp

#include "tester.h"

void tester::do_gen() {

unsigned char tmp;

while (true) {

mode.write(0); //even parity

for (int i=0; i<10; i++) {

tmp = rand() & 0xFF;

data.write(tmp);

wait();

}

mode.write(1); //odd parity

for (int i=0; i<10; i++) {

tmp = rand() & 0xFF;

data.write(tmp);

wait();

}

}

}

Page 57: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 57

6. parity generator and checker

void tester::do_che() {

unsigned dd;

int none;

int odd_par, even_par;

int par, err;

while (true) {

wait();

dd = data.read();

none = 0;

//count the number of 1s

for (int i=0; i<8; i++) {

if (dd&0x1) none++;

dd >>= 1;

}

odd_par = (none%2)?0:1;

even_par = (none%2)?1:0;

par = parity.read()?1:0;

err = error.read()?1:0;

Page 58: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 58

6. parity generator and checker

if(mode) { //odd parity

if (odd_par!=par) {

printf("[%f] Odd parity generation error: data[0x%x] parity[%01x]\n", sc_simulation_time(), data.read(), par);

}

if (((odd_par==par)&&err)||((odd_par!=par)&&!err)) {

printf("[%f] Odd parity check error: data[0x%x] parity[%01x]\n error[%01x]\n", sc_simulation_time(), data.read(), par, err);

} else { //een parity

printf("[%f] Even parity generation error: data[0x%x] parity[%01x]\n", sc_simulation_time(), data.read(), par);

}

if (((odd_par==par)&&err)||((odd_par!=par)&&!err)) {

printf("[%f] Odd parity check error: data[0x%x] parity[%01x]\n error[%01x]\n", sc_simulation_time(), data.read(), par, err);

}

}

}

}

Page 59: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 59

6. parity generator and checker

Page 60: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 60

7. Time

• Time a type: – internally represented by an unsigned integer of at least 64-bits, – starts at 0,– moves forward only.

• Three important time type in systemC:

1) sc_time 2)Time Resolution 3) Default Time Unit

Page 61: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 61

7.1 sc_time

• Represent time or time intervals in SystemC, • Constructed from a numeric value (type double) and a time unit (type

sc_time_unit):

enum sc_time_unit {

SC_FS = 0, // femtosecond SC_PS, // picosecond SC_NS, // nanosecond SC_US, // microsecond SC_MS, // millisecond SC_SEC // second

};

Page 62: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 62

7.2 Time Resolution

• The smallest amount of time, represented by all sc_time objects in a SystemC simulation.

• Default value : 1 picosecond

• Setter: sc_set_time_resolution( double val, sc_time_unit tu );

– Val: • must be positive and a power of ten,

• must be greater than or equal to 1 femtosecond.

• Getter: sc_time sc_get_time_resolution();

Page 63: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 63

7.3 Default Time

• Used to specify the unit of time for the values without time unit,

• Default value: 1 nanosecond,

• Setter: sc_set_default_time_unit( double val, sc_time_unit tu );

– val must be: • positive and a power of ten,

• greater than or equal to the current time resolution.

• Getter: sc_time sc_get_time_resolution();

Page 64: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 64

7.4 Time Related Functions

• sc_time sc_get_default_time_unit();• sc_time sc_get_time_resolution();• double sc_simulation_time(); • void sc_set_default_time_unit( double val, sc_time_unit tu );• void sc_set_time_resolution( double val, sc_time_unit tu );• void sc_start( const sc_time& duration )• void sc_start( double d_val, sc_time_unit d_tu );• void sc_start( double d_val = -1 );• void sc_stop();• const sc_time& sc_time_stamp();

Page 65: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 65

7.5 Example

//Given

sc_time r_time( 1000, SC_NS);

// Then

sc_start(r_time); // run 1000 nSec

sc_start(1000, SC_NS); // run 1000 nSec

sc_start( 1000 ); // run 1000 default time units

sc_start(); // run forever

sc_start(-1); // run forever

Page 66: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 66

7.6 Structure of a SC program

Module

ports processes channelsinoutinout

methods

threads

constructor

events

Sensitivity list

Page 67: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 67

8. Events

• Event an object:– Represented by class sc_event

– Determinnig whether and when a process’ execution should be triggered or resumed

– provides basic synchronization for processes.

Page 68: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 68

• Processes can be made “sensitive” to events,• Processes are activated automatically when events occur

that the process are sensitive to,• Events are implemented in channels and bounded to

processes through ports,

• An event executes on time by notify()

8.1 sc_event type

Page 69: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 69

8.2 Events’ Notification

• Immidiate– event is triggered in the current evaluation phase of the

current delta-cycle.

• Delta-cycle delayed– the event will be triggered during the evaluate phase of

the next delta-cycle.

• Timed– event will be triggered at the specified time in the futur

e

Page 70: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 70

8.3 Examples

sc_event a, b, c ; // event declaration sc_time t (10, SC_NS) // declaration of a 10ns time interval

a.notify(); // immediate notification, current delta-cycle notify(SC_ZERO_TIME, b); // delta-delay notification, next delta-cycle notify(t, c); // 10 ns delay

//Cancel an event notification a.cancel(); // Error! Can't cancel immediate notification b.cancel(); // cancel notification on event b c.cancel(); // cancel notification on event c

Page 71: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 71

9. Sesitivity

• A member variable of sc_module,• Operator “sensitive << port;”• Each process in a module has its own sensitivity d

eclaration• Two kind:

– Static

– Dynamic

Page 72: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 72

9.1 Example of Static Sensitivity

SC_MODULE(Adder){sc_int<int> a;sc_int<int> b;sc_out<int> c;void compute{

c = a + b;}SC_CTOR(Adder){

SC_METHOD(compute);sensitive << a << b;

}}

Page 73: SKKU 휴대폰학과 © 조준동 2008 1 조 준 동 2008.1 SoC 설계방법론 - 실습

SKKU 휴대폰학과 © 조준동 2008 73

9.2 Example of Dynamic Sensitivity

• Suspends and resumes processes– wait(); // wait for the next event been notified

– wait( e1 ); // wait for “e1” been notified

– wait( e1 & e2 ) //wait for both e1 and e2

– wait( e1 | e2 ) //wait for either e1 or e2

– wait( 200, SC_NS ) // trigger 200 ns later– wait( 200, SC_NS, e1 ); // e1, timeout 200 ns