Chisel CRV is a project that aims to mimic the functionality of SystemVerilog constraint programming and integrates them into chisel-tester2. Chisel CRV combines a Constraint Satisfactory Problem Solver, with some helper classes to create and use random objects inside your tests.
class frame_t;
rand pkt_type ptype;
rand integer len;
randc bit [1:0] no_repeat;
rand bit [7:0] payload [];
// Constraint the members
constraint legal {
len >= 2;
len <= 5;
payload.size() == len;
}
class Frame extends sv.Random(33) {
var pkType: RandInt = rand(pkType, pktType.domainValues())
var len: RandInt = rand(len, 0 to 10 toList)
var noRepeat: RandCInt = randc(noRepeat, 0 to 1 toList)
var payload: RandInt = rand(payload, 0 to 7 toList)
val myBlock: ConstraintBlock = constraintBlock (
unary ( len => len >= 2),
unary (len => len <= 5),
binary ((len, payload) => len == payload)
)
}
Based on the ideas of the book Artificial Intelligence A Modern Approach,
Is a combination of BacktrackSearching and Constraint Propagation.
The pseudocode for the algorithm used can be found here.
The CSP solver and the relative components are stored in the csp
package.
The random class is the base class that each Random object should extend in order to implement constrained programming.
Create a class that inherits from the base class sv.Random
class Frame extends sv.Random
The class can also be seeded like
class Frame extends sv.Random(33)
You can add random field by declaring a var
field as type RandInt
and assigin it a domain
with the rand
macro. A domain is a list of values. For now only integer values are supported.
var len: RandInt = rand(len, 0 to 10 toList)
You can also add a continuous Random Integer by adding a var
field to your class and declaring it
RandIntC
var noRepeat: RandCInt = randc(noRepeat, 0 to 1 toList)
Finally you can constraint the newly added fields by creating a constraint block
val myBlock: ConstraintBlock = constraintBlock (
unary ( len => len >= 2),
unary (len => len <= 5),
)
To randomize the newly create random object, just call the randomize
method
val frame = new Frame
while (frame.randomize) {
println(frame.debug())
assert(frame.len >= 2)
assert(frame.len <= 5)
}
output
pkType = 1; len = 5; noRepeat = 0; payload = 5;
pkType = 0; len = 5; noRepeat = 1; payload = 5;
pkType = 11; len = 5; noRepeat = 0; payload = 5;
pkType = 1; len = 3; noRepeat = 1; payload = 3;
[...]
- Constraint Solvingwithout Surprises in Object-ConstraintProgramming Languages
- Exact Cover Series of 3 articles about constraint programming
- CLP(FD) Prolog library about constraints
- Refactoring the csp package
- Add random arrays
- Add soft constraint