{"id":13914,"date":"2019-09-12T22:40:27","date_gmt":"2019-09-13T05:40:27","guid":{"rendered":"https:\/\/blogs.mentor.com\/verificationhorizons\/?p=13914"},"modified":"2026-03-27T08:39:50","modified_gmt":"2026-03-27T12:39:50","slug":"verilog-in-constraints","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/2019\/09\/12\/verilog-in-constraints\/","title":{"rendered":"A Little Verilog Knowledge Goes A Long Way in Understanding How SystemVerilog Constraints Work"},"content":{"rendered":"<p>In its simplest form, a constraint is nothing more than a Boolean expression with random variables where the solver is asked to find values that make the expression true. One way of thinking about how a constraint solver works is that it repeatedly tries different values until the values make the expression true, then it writes those values into the set of random variables. It\u2019s actually much more complicated than that because we want the solver to quickly converge on a solution or tell us no solution is possible without wasting a lot of time. But let\u2019s just focus on the expression evaluation.<\/p>\n<p>A fundamental principle that drove SystemVerilog\u2019s development was unifying semantics so that expression evaluation was identical across all facets of the language. This meant all the idiosyncrasies from Verilog\u2019s weak type and expression evaluation systems got absorbed into constraint expressions. One of the unique things that distinguishes Verilog from most other programming languages is its ability to declare variables of different bit-widths and have expressions of mixed bit-widths. The weak type system means it silently truncates and pads different bit widths without any notice of overflow or underflow. This system also has to deal with signed and unsigned arithmetic, and real to integer truncation.<\/p>\n<p>Let\u2019s look at the following two seemingly equivalent expressions:<\/p>\n<p>Expression A: <code>(X * 4) == Y<br \/>\n<\/code><\/p>\n<p>Expression B: \u00a0<code>X == (Y \/4)<\/code><\/p>\n<p>If we remember our elementary algebra, the division property of equality says we should be able to divide both sides of the equality in Expression A by 4 and wind up with Expression B. You would think using either expression in a constraint gives the exact same results. <em>NOT TRUE<\/em>. Each expression has a complex series of steps for determining the bit width of an expression result and all the intermediate operations. We need to be aware of this even when we think everything has the same bit-widths.<\/p>\n<p>For Expression A, suppose we declared X and Y as 32-bit unsigned variables (the numeric literal 4 is also considered 32-bits. Verilog has a table for every operator that specifies the resulting width its result. For all arithmetic operators the result width is the size of its largest operand, not the width of the largest possible result which would be 34 bits in this case. This means the result of <code>32\u2019h40000001*32\u2019d4<\/code> gets truncated to <code>32\u2019h00000004<\/code>. So, the solver could choose either <code>32\u2019h40000001<\/code> or <code>32\u2019h00000001<\/code> for X when choosing the value <code>32'h00000004<\/code> for Y.<\/p>\n<p>The solution space for\u00a0<code>(X * 32'd4) == Y<\/code>is shown in the following table.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>X<\/th>\n<th>Y<\/th>\n<\/tr>\n<tr>\n<td><code>32'h00000000<\/code><\/td>\n<td><code>32'h00000000<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h00000001<\/code><\/td>\n<td><code>32'h00000004<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h00000002<\/code><\/td>\n<td><code>32'h00000008<\/code><\/td>\n<\/tr>\n<tr align=\"center\">\n<td colspan=\"2\">\u2026<\/td>\n<\/tr>\n<tr>\n<td><code>32\u2019h3FFFFFFF<\/code><\/td>\n<td><code>32'hFFFFFFFC<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h40000000<\/code><\/td>\n<td><code>32'h00000000<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h40000001<\/code><\/td>\n<td><code>32'h00000004<\/code><\/td>\n<\/tr>\n<tr align=\"center\">\n<td colspan=\"2\">\u2026<\/td>\n<\/tr>\n<tr>\n<td><code>32\u2019hFFFFFFFF<\/code><\/td>\n<td><code>32'hFFFFFFFC<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>The fix for this is making sure the width of the multiplication result is large enough not to truncate using :\u00a0<code>(X * 34\u2019d4) == Y<\/code>. That eliminates the overflow truncation and the second set of unexpected solutions.<\/p>\n<p>For expression B, the bit-widths are not as precarious. \u00a0But we do need to understand Verilog integer division truncates any fractional part towards 0, it does not round. That means there will be for consecutive values for Y associated with one value of X.\u00a0The solution space for\u00a0<code>X == (Y \/ 4)<\/code> is shown in the following table.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>X<\/th>\n<th>Y<\/th>\n<\/tr>\n<tr>\n<td><code>32'h00000000<\/code><\/td>\n<td><code>32'h00000000<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h00000000<\/code><\/td>\n<td><code>32'h00000001<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h00000000<\/code><\/td>\n<td><code>32'h00000002<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32\u2019h00000000<\/code><\/td>\n<td><code>32'h00000003<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h40000001<\/code><\/td>\n<td><code>32'h00000004<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>32'h40000001<\/code><\/td>\n<td><code>32'h00000005<\/code><\/td>\n<\/tr>\n<tr align=\"center\">\n<td colspan=\"2\">\u2026<\/td>\n<\/tr>\n<tr>\n<td><code>32\u2019h3FFFFFFF<\/code><\/td>\n<td><code>32'hFFFFFFFF<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>To summarize, I&#8217;ve show a simple example of how Verilog expression evaluation rules affect the solution space of your SystemVerilog constraints. So anytime you get unexpected values from the constraint solver, go back to your textbooks on Verilog or even Algebra to remember how expressions get evaluated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In its simplest form, a constraint is nothing more than a Boolean expression with random variables where the solver is&#8230;<\/p>\n","protected":false},"author":71589,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[395,533,751,822,831],"industry":[],"product":[],"coauthors":[],"class_list":["post-13914","post","type-post","status-publish","format-standard","hentry","category-news","tag-constrained-random-simulation","tag-ieee-1800","tag-systemverilog","tag-verification-engineer","tag-verilog"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/13914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/users\/71589"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/comments?post=13914"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/13914\/revisions"}],"predecessor-version":[{"id":19886,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/13914\/revisions\/19886"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media?parent=13914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/categories?post=13914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/tags?post=13914"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/industry?post=13914"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/product?post=13914"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/coauthors?post=13914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}