How do I costraint state transistion?

class foo;

enum { read, write } states;
constraint c1 {
{ read, write => read};
{ read => read => read => read => write};

}

endclass : foo

I would like to constraint state read and write as shown in this example. { read, write => read} and { read => read => read => read => write}; I get a compile error on EDA Playground.

How do I write this state transition constraint?

In reply to superUVM:
Can you explain in words without trying to use syntax.

In reply to dave_59:

I want to generate read and write transactions in sequence with following conditions,

  1. read can be followed by another read on next cycle
  2. Write can be followed by another read but not write on next cycle. (No back to back write permitted)
  3. More than 4 Reads can not be back to back.

How can I use SV state transitions with constraint to implement these?

In reply to superUVM:

As Dave pointed out, the initial code you posted is incomplete and insufficient.

I would approach this problem as below :

  • Start with the enum as a typedef.
  • Use this typedef-ed enum to define an array/queue of states.
  • Use a constraint on this array/queue of states.

// Code your testbench here
// or browse Examples
class foo;

  typedef enum { read, write } states;
  
  rand states states_da[];
  
  function new(int number_of_states);
    states_da = new[number_of_states];
  endfunction

constraint c1 {
  foreach(states_da[i]) {
    if(i >= 2) {
      (states_da[i-2]==read && states_da[i-1]==write) -> (states_da[i] == read);
    }
  }
  foreach(states_da[i]) {
    if(i >= 4) {
      (states_da[i-4]==read && states_da[i-3]==read && states_da[i-2]==read && states_da[i-1]==read) -> (states_da[i] == write);
    }
  }
}
  
endclass


module top;
  foo i_foo;
  
  initial begin
    i_foo =new(20);
      i_foo.randomize();
      $display("%p",i_foo.states_da);
  end
  
  
endmodule

You get the idea. You can add more constraints as needed. You can also move the size of array inside the class, and randomize that as well, to suit your needs.

In reply to KillSteal:

Awesome, Thanks!