Previous Section Next Section

6.6 Exercises

  1. Define a struct test1 with two fields, a1: 8 bits and a2: 8 bits. Define a method m() that returns an 8-bit uint that is the sum of a1 and a2. Instantiate the struct test1 in the sys struct. Invoke the method m() in the post_generate() method of the test1 struct and assign the result to variable i using the out() method. (Hint: Use the implicit result variable.)

  2. Define a struct test2 with two fields, b1: 8 bits and b2: 8 bits. Define a method m() as follows (Hint: Use the case action):

    1. Argument type can have values of 0, 1, 2 or 3.

    2. Return value is an 8-bit uint.

    3. If type == 0, then return value = b1 + b2.

    4. If type == 1, then return value = b1 ^ b2.

    5. If type == 2, then return value = b1 & b2.

    6. If type == 3, then return value = sys.test1.a1 + sys.test1.a2.

    7. Print the value of the type and result using the outf() method.

    8. Invoke the method m() with type == 3 in the post_generate() method of the test2 struct and assign the result to a variable i.

    9. Instantiate the struct test2 in the sys struct.

  3. Rewrite Exercise 2 using the if-then-else action instead of the case action. Name the struct test3 instead of test2.

  4. Define a struct named "instruction" that contains the fields: opcode: 8 bits, op1: 8 bits and op2: 8 bits. Define l_instructions as a list of the struct instruction in the sys struct. In the sys struct, define a method loop() as follows:

    1. Declare a local variable count of the type uint.

    2. Declare a local variable list_size of the type uint.

    3. Set list_size equal to the size of the l_instructions list.

    4. Write an e style for loop using the variable count that loops from 0 to list_size-1 in list l_instructions and prints out the value of the fields of instruction using the out() method.

    5. Write a C style for loop using the variable count that loops from 0 to list_size-1 in list l_instructions and prints out the value of the fields of instruction using the outf() method.

    6. Write a for each loop that loops for each element in list l_instructions and prints out the value of the struct instruction using the print action.

    7. Invoke the method loop() in the post_generate() method of the sys struct.

  5. Extend the sys struct. Extend the method loop() using the is also keywords. Write a while loop that counts up each element in list l_instructions from 0 to list_size-1 and prints the struct instruction using the print action. (Hint: You may need to redeclare the variable count.)

  6. Load all code from exercises 1 to 5 into Specman Elite and verify that it loads without error.

Previous Section Next Section