Constraint Problem

I am trying to write a constraint such that dst_addr > src_addr+length or dst_addr+length < src_addr, but the below constraint is not working. That means dst_addr should never be in the range of src_addr+length and there should be enough space between dst_addr and src_addr that all the data can transferred from src to dst. dst_addr can be smaller than or greater than src_addr but the absolute difference between two should be greater than or equal to the length.

class trans;
  rand bit [7:0] src_addr;
  rand bit [7:0] dst_addr;
  rand bit [7:0] length;
  
  constraint c1 {! ( dst_addr inside {[src_addr:src_addr+length]}) && ! (src_addr inside {[dst_addr:dst_addr+length]} );}

endclass

module tb;
  trans t;
  initial begin
    t = new;
    repeat (30)
      begin
    t.randomize;
        $display(t.src_addr);
        $display(t.dst_addr);
        $display(t.length);
        $display("END");
      end
  end
endmodule

In reply to Neal:

Please use code tags making your code easier to read. I have added them for you.

It also helps to explain what “not working” means in terms of what you are actually seeing versus what you are expecting.

If you plugged in the values you are actually seeing into the constraint equation, you would know that you are missing some overflow considerations:

  constraint c2 { 9'(src_addr+ length) < 256; 9'(dst_addr+ length) < 256; 

In reply to dave_59:

Thanks Dave I think it is working perfectly fine with added constraint.

values just with c1;

class trans;
  rand bit [7:0] src_addr;
  rand bit [7:0] dst_addr;
  rand bit [7:0] length;
 
  constraint c1 {! ( dst_addr inside {[src_addr:src_addr+length]}) && ! (src_addr inside {[dst_addr:dst_addr+length]} );}
 
endclass
 
module tb;
  trans t;
  initial begin
    t = new;
    repeat (30)
      begin
    t.randomize;
        $display(t.src_addr);
        $display(t.dst_addr);
        $display(t.length);
        $display("END");
      end
  end
endmodule

149

236

14

END

230

5

162

END

160

202

31

END

202

102

96

END

38

224

178

END

215

44

41

END

103//src_addr

93//dst_addr

189//length

END //In this particular case the difference between the |dst_addr - src_addr| >= length but the difference is 10

101

237

67

END

209

119

17

END

168 //src_addr

160 //dst_addr

115 //length

END //In this particular case the difference between the |dst_addr - src_addr| >= length but the difference is 8

79 //src_addr

130 // dst_addr

221 //length

END //In this particular case the difference between the |dst_addr - src_addr| >= length but the difference is 51

65

244

153

END

96 //src_addr

96 //dst_addr

180 //length

END // This is also not working as per expected

98

135

200

END

145

123

181

END

223

167

39

END

222

38

2

END

225

136

58

END

33

199

153

END

5

248

128

END

56

253

94

END

75

196

48

END

255

43

196

END

109

129

5

END

208

225

131

END

234

33

33

END

67

198

70

END

127

57

241

END

242

214

109

END

6

255

240

END

With the fix constraint c1 and c2: It works perfectly fine, with the above constraint numbers it is less intuitive that I might be missing the overflow condition.
Thanks now it is working.

class trans;
  rand bit [7:0] src_addr;
  rand bit [7:0] dst_addr;
  rand bit [7:0] length;
  
  constraint c1 {! ( dst_addr inside {[src_addr:src_addr+length]}) && ! (src_addr inside {[dst_addr:dst_addr+length]} );}
  constraint c2 { 9'(src_addr+ length) < 256; 9'(dst_addr+ length) < 256;} 

endclass

module tb;
  trans t;
  initial begin
    t = new;
    repeat (30)
      begin
    t.randomize;
        $display(t.src_addr);
        $display(t.dst_addr);
        $display(t.length);
        $display("END");

      end
  end
endmodule

6

149

79

END

76

144

51

END

182

219

31

END

6

115

96

END

13

128

110

END

44

144

97

END

50

103

10

END

2

101

88

END

119

188

28

END

160

1

51

END

210

119

42

END

244

54

5

END

138

96

9

END

135

28

14

END

3

132

118

END

167

51

27

END

38

29

2

END

136

70

10

END

202

33

11

END

177

5

53

END

107

5

94

END

196

203

6

END

43

111

58

END

129

23

2

END

225

239

4

END

33

151

6

END

198

219

7

END

233

127

2

END

191

242

7

END

171

6

24

END