Methods Part-V

Print

Passing Parameters

As in any programming language, we can pass parameters to function. In this case methods. Passing of parameters can be donw with one of the following ways

Scalar Parameter Passing

Scalar parameters include numeric, Boolean, and enumerated types. When you pass a scalar parameter to a method, by default the value of the parameter is passed, this is same as in other programming language.

  1 <'

  2 struct scoreboard {

  3      ! mem_list : list of int;

  4     // This method uses one input parameter

  5     add_item (addr : intis {

  6       mem_list.push(addr);

  7     };

  8     // This method does not have any input

  9     // parameters

 10     print_sb () is {

 11      print mem_list;

 12     };

 13 };

 14 // Extend sys to run the simulate above methods

 15 extend sys {

 16   U_sb : scoreboard;

 17   run() is also {

 18     U_sb.add_item(10);

 19     U_sb.add_item(22);

 20     U_sb.print_sb();

 21   };

 22 };

 23 '>

  mem_list =

0.             10

1.           22

Compound Parameter Passing

Sometimes we may want to pass lot of parameters to a methods. Instead of typing each and every parameter, we can create a struct and pass this struct as parameter. Once again this is same as in C or C++. We can also pass a list as parameter to a method.

  1 <'

  2 struct mem_base_object {

  3   addr : int;

  4   data : int;

  5   cmd  : bool;

  6 };

  7

  8 struct mem_txgen {

  9    ! base_object : mem_base_object;

 10   num_cmds    : int;

 11   // This methods the commands

 12   gen_cmds () is {

 13    var i : int = 0;

 14    for i from 1 to  num_cmds do {

 15      // gen commands

 16      gen base_object;

 17      // Print the commands

 18      print_cmds(base_object);

 19    };

 20   };

 21   // This method prints the command details

 22   print_cmds (cmd_struct : mem_base_object) is {

 23     out ("Following command is generated");

 24     outf("      Address  : %x\n",cmd_struct.addr);

 25     outf("      Date     : %x\n",cmd_struct.data);

 26     if (cmd_struct.cmd) {

 27       out("      CMD      : Read");

 28     } else {

 29       out("      CMD      : Write");

 30     };

 31   };

 32 };

 33 // Extend sys to run the simulate above methods

 34 extend sys {

 35   txgen : mem_txgen;

 36   keep txgen.num_cmds == 2;

 37   run() is also {

 38     txgen.gen_cmds();

 39   };

 40 };

Following command is generated

      Address  : 87abfa57

      Date     : af6d7f9

      CMD      : Read

Following command is generated

      Address  : f49771af

      Date     : 827afe11

      CMD      : Read

Passing By Reference

As in the C and C++, we can pass a parameter as reference, in this case what happens is if the variable is modified inside the method, the value gets reflected in global variable also. This is applicable to both Compound parameter passing and also to scalar parameter passing.

  1 <'

  2 struct counter_struct {

  3      ! count_var : int;

  4     // Counter

  5     counter () is {

  6       var i : int = 0;

  7       for i from 0 to 10 do {

  8         increment(count_var); // There is no * here

  9         outf("Current value of count : %x\n",count_var);

 10       };

 11     };

 12     // This method increments the passed parameter

 13     // * is for the data type and not to variable

 14     increment (count_passed : *intis {

 15       count_passed = count_passed + 1;

 16     };

 17 };

 18 // Extend sys to run the simulate above methods

 19 extend sys {

 20   U_c : counter_struct;

 21   run() is also {

 22      U_c.counter();

 23   };

 24 };

 25 '>

Current value of count : 1

Current value of count : 2

Current value of count : 3

Current value of count : 4

Current value of count : 5

Current value of count : 6

Current value of count : 7

Current value of count : 8

Current value of count : 9

Current value of count : a

 

Current value of count : b

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

Mong Muốn Có Thêm Cơ Hội Trong Công Việc

Và Trở Thành Một Người Có Giá Trị Hơn

Bạn Chưa Biết Phương Thức Nào Nhanh Chóng Để Đạt Được Chúng

Hãy Để Chúng Tôi Hỗ Trợ Cho Bạn. SEMICON  

Last Updated ( Wednesday, 01 June 2022 18:12 )