SystemVerilog - Constraint display issue

Hi all,

just a quick question regarding my current issue.

The randomization works, but I do not get how I should print out how many people got grades of each range I have set up using constraints.

This is my code for now

module constraint_distrib (

);

initial
begin
  $fsdbDumpfile("dump.fsdb");
  $fsdbDumpvars(0, constraint_distrib);

end

/*-----CLASSES-----*/
class the_tests;
  rand bit [7:0] the_grade; 

  constraint limit_score {the_grade <= 100;}   

endclass :  the_tests
  
typedef enum {F, D, C, B, A} grade; 

class myGrade extends the_tests; 
  rand grade grade_type; 
  int average; 


  constraint grade_range 
  {
    (grade_type == F) <-> the_grade inside {[0:59]};        
    (grade_type == D) <-> the_grade inside {[60:69]};      
    (grade_type == C) <-> the_grade inside {[70:79]};      
    (grade_type == B) <-> the_grade inside {[80:89]};        
    (grade_type == A) <-> the_grade inside {[90:100]};      

  }

endclass  : myGrade

// beginning stimulus
int Accepted, Below_A, Come_on, Down_bad, Failed;

initial begin
  myGrade students;
  students = new; 

  // among 100 students, show grade distribution among these guys
  for (int i = 0; i < 100; i++)
  begin
    if (students.randomize() == 1) 
      begin
        // $display("Grade: %s", students.grade_type);
        $display("Grade num: %0d", students.the_grade);

        case (students.the_grade)
          F:  Failed++;
          D:  Down_bad++;
          C:  Come_on++;
          B:  Below_A++;
          A:  Accepted++;
        endcase
      end
    

    else
      $display("RANDOMIZATION FAILED");
  end

  $display("A: %0d   B: %0d   C: %0d   D: %0d   F: %0d  ", Accepted, Below_A, Come_on, Degenerate, Failed);
  
  $finish; 
end

endmodule

Unfortunately, when I tried printing out the grade distribution, all I get is the following:

A: 0   B: 2   C: 2   D: 2   F: 1  

Is there something that I did not consider or something that I am not understanding well?

Thank you

Sangwoo

You are classifying based on ‘the_grade’, when you want to use ‘grade_type’.

// Enum
typedef enum {F, D, C, B, A} grade; 

// Classes
class the_tests;
  rand bit [7:0] the_grade; 

  constraint limit_score {the_grade <= 100;}   

endclass :  the_tests
  

class myGrade extends the_tests; 
  rand grade grade_type; 
  int average; 

  constraint grade_range {
    (grade_type == F) <-> the_grade inside {[0:59]};        
    (grade_type == D) <-> the_grade inside {[60:69]};      
    (grade_type == C) <-> the_grade inside {[70:79]};      
    (grade_type == B) <-> the_grade inside {[80:89]};        
    (grade_type == A) <-> the_grade inside {[90:100]};
  }

endclass  : myGrade

module constraint_distrib ();

  // beginning stimulus
  int Accepted, Below_A, Come_on, Down_bad, Failed;

  initial begin
    myGrade students;
    students = new; 

    // among 100 students, show grade distribution among these guys
    for (int i = 0; i < 100; i++) begin
      if (!students.randomize()) begin
        $display("RANDOMIZATION FAILED");
      end
      else begin
        // $display("Grade: %s", students.grade_type);
        $display("Grade num: %0d Grade type: %s", students.the_grade, students.grade_type.name());

        case (students.grade_type)
          F:  Failed++;
          D:  Down_bad++;
          C:  Come_on++;
          B:  Below_A++;
          A:  Accepted++;
        endcase
      end
    end

    $display("A: %0d   B: %0d   C: %0d   D: %0d   F: %0d  ", Accepted, Below_A, Come_on, Down_bad, Failed);
  end

endmodule