Variables Inside Methods
To declare a local variable inside a method is same as declaring in a struct, only difference is the variable declaration should start with reserve word var as shown in example below.
Variables declared inside a method can not be accessed by other methods or outside the method which declares. Thus we call them local variables.
1 <'
2 struct scoreboard {
3 // This is global variable
4 ! mem_list : list of int;
5 // Just another method
6 add_item (addr : int) is {
7 mem_list.push(addr);
8 // This is local variable
9 var i : int = 0; // We can assign a default value
10 };
11 };
12 '>
Invoking Methods
Depending on if the method is time consuming method or regular method or we want to make the time consuming method to run as thread we can invoke method as one the way listed below.
- tcm()
- start tcm()
- method()
- compute method()
tcm()
A TCM can be called from another TCM. A called TCM begins execution either when its sampling event occurs or immediately, if the sampling event has already occurred for the current Specman tick.
The calling TCM waits until the called TCM returns before continuing execution. For this reason, a called TCM is considered a subthread of the calling TCM and shares the same thread handle (thread ID) with the calling TCM.
1 <'
2 struct memory_driver {
3 // Define a event for posedge of clock
4 event pos_clk is rise('top.clk')@sim;
5 // Define a method to drive memory write
6 mem_write( data: byte, addr : byte ) @pos_clk is {
7 // Wait for one cycle and drive command
8 wait [1]*cycle;
9 'top.mem_en' = 1;
10 'top.mem_addr' = addr;
11 'top.mem_data' = data;
12 'top.mem_wr' = 1;
13 outf("Writing to Address %x with Data %x\n",addr,data);
14 // Wait for one cycle and deassert the command
15 wait [1]*cycle;
16 'top.mem_en' = 0;
17 'top.mem_addr' = 0;
18 'top.mem_data' = 0;
19 'top.mem_wr' = 0;
20 };
21 // We use this TCM to call mem_write TCM
22 mem_tst()@pos_clk is {
23 mem_write(0x10, 0x5);
24 mem_write(0x15, 0x6);
25 mem_write(0xAA, 0x7);
26 out("Done with Simulation");
27 };
28 };
29 '>
start tcm()
You can use a start action within another method, either a TCM or a regular method. A started TCM begins execution either when its sampling event occurs or immediately, if the sampling event has already occurred for the current Specman tick.
A started TCM runs in parallel with the TCM that started it on a separate thread. A started TCM has a unique thread handle (thread ID) that is assigned to it automatically by the scheduler. You can retrieve this handle using one of the predefined methods of the scheduler
The recommended way to start an initial TCM, which can then invoke other TCMs, is to extend the related struct's predefined run() method. |
- A TCM that has a return value cannot be started with a start action.
- You cannot start a TCM before the run phase begins or after the check phase begins
1 <'
2 // Note : We need a verilog code and Verilog simulator
3 // to simulate below example
4 struct memory_driver {
5 // Define a event for posedge of clock
6 event pos_clk is rise('top.clk')@sim;
7 // Define a method to drive memory write
8 mem_write( data: byte, addr : byte ) @pos_clk is {
9 // Wait for one cycle and drive command
10 wait [1]*cycle;
11 'top.mem_en' = 1;
12 'top.mem_addr' = addr;
13 'top.mem_data' = data;
14 'top.mem_wr' = 1;
15 outf("Writing to Address %x with Data %x\n",addr,data);
16 // Wait for one cycle and deassert the command
17 wait [1]*cycle;
18 'top.mem_en' = 0;
19 'top.mem_addr' = 0;
20 'top.mem_data' = 0;
21 'top.mem_wr' = 0;
22 };
23 // We use this TCM to call mem_write TCM
24 mem_tst()@pos_clk is {
25 mem_write(0x10, 0x5);
26 mem_write(0x15, 0x6);
27 mem_write(0xAA, 0x7);
28 out("Done with Simulation");
29 };
30 };
31
32 extend sys {
33 memory_driver : memory_driver;
34 run() is also {
35 // since the method is tcm use start
36 // Using start causes TCM run as parallel thread
37 start memory_driver.mem_tst();
38 };
39 };
40 '>
Bạn Có Đam Mê Với Vi Mạch hay Nhúng - Bạn Muốn Trau Dồi Thêm Kĩ Năng