{"id":9778,"date":"2015-08-31T09:15:55","date_gmt":"2015-08-31T16:15:55","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=9778"},"modified":"2024-12-28T16:41:40","modified_gmt":"2024-12-29T00:41:40","slug":"the-constraints-file-also-known-as-magical-moving-stairs","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/the-constraints-file-also-known-as-magical-moving-stairs\/","title":{"rendered":"The Constraints File, Also Known as Magical Moving Stairs"},"content":{"rendered":"<p>A point of confusion for a lot of people new to FPGA design is the constraints file. People are used to just writing code and having it work. However, in FPGA design we have to specify what hardware is being used.<\/p>\n<p>The part that is easiest to grasp for most users is the .v or .vhd file, depending on if you are writing in Verilog or VHDL. This is where you write what you want to do. So, say you want to implement a simple AND\u00a0gate.\u00a0You want the inputs to be switches, and the output to be an LED. In Verilog you would write:<\/p>\n<p><span style=\"color: #008000;\">Assign LED\u00a0= SW0\u00a0&amp; SW1;<\/span><\/p>\n<p>This would AND\u00a0together SW0\u00a0and SW1, and assign LED\u00a0that value. In computer science, at this point you would be done. Your variable LED has its value.<\/p>\n<p>However, in hardware language, you need to connect that &#8220;Net&#8221; to a physical component, or its pin location. The same needs to be done for the switches. This is done in the constraints file.<\/p>\n<p>The way I like to think of a constraints file is like magical stairs from Harry Potter:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"http:\/\/img1.wikia.nocookie.net\/__cb20090622140129\/harrypotter\/images\/3\/32\/Moving_stairs.gif\" alt=\"\" width=\"500\" height=\"212\" \/><\/p>\n<p>The base of the staircase is like the net. You know where that leads to, but the stairs have the ability to go anywhere (any physical pin location), and until they are connected they don&#8217;t lead anywhere. Well, the constraints file is like the stairs connecting. Once the constraints file is written, the pin location\u00a0is set, you know the bottom of the stairs is connected to LED and you know the top is connected to say H17. However, just like the stairs you can easily change the final destination, by changing the pin location.<\/p>\n<p>When using Xilinx tools, which is what is used with Digilent boards, you have two types of constraints files, <span style=\"color: #0000ff;\">UCF (user constraints files)<\/span>, and <span style=\"color: #ff0000;\">XDC files (xilinx constraints files)<\/span>, to be used with <span style=\"color: #0000ff;\">ISE<\/span> and <span style=\"color: #ff0000;\">Vivado<\/span>, respectively.<\/p>\n<p>Each of these types of constraints files contains the same information. For all the pins it contains a net name, a pin location, and the IO signaling standards.\u00a0The net name, is like a variable name. It&#8217;s what you want that pin to connect to in your design. The pin location is the physical pin that you want to send the signal to. The IOStandard is like a communication protocol.<\/p>\n<p>This is the basic information a constraints file requires, but it can also contain more information.<\/p>\n<p>So say I want to connect <span style=\"text-decoration: underline;\">pin H17<\/span>, which is led 0 on the Nexys 4, to my output <span style=\"text-decoration: underline;\">LED<\/span>, with iostandard <span style=\"text-decoration: underline;\">LVCMOS33<\/span>.<\/p>\n<p>For a <span style=\"color: #0000ff;\">UCF<\/span> file I would need to write:<\/p>\n<p><span style=\"color: #0000ff;\">NET &#8220;<span style=\"text-decoration: underline;\">LED<\/span>&#8221; LOC=<span style=\"text-decoration: underline;\">H17<\/span> | IOSTANDARD=<span style=\"text-decoration: underline;\"><span style=\"color: #ff0000; text-decoration: underline;\">LVCMOS33<\/span><\/span>;<\/span><\/p>\n<p>For an\u00a0<span style=\"color: #ff0000;\">XDC<\/span> file I would need to write:<\/p>\n<p><span style=\"color: #ff0000;\">set_property -dict { PACKAGE_PIN <span style=\"text-decoration: underline;\">H17<\/span> IOSTANDARD <span style=\"text-decoration: underline;\">LVCMOS33<\/span> } [get_ports { <span style=\"text-decoration: underline;\">LED<\/span> }];<\/span><\/p>\n<p>Luckily, you don&#8217;t have to type this whole thing out from scratch for everything you want to use on the FPGA. You can download the master <span style=\"color: #0000ff;\">UCF<\/span> or <span style=\"color: #ff0000;\">XDC<\/span> file from the product page of the FPGA you are using, uncomment the lines you need, and change the net name to match your design.<\/p>\n<p>So, to finish my example, in the design I would need:<\/p>\n<p><span style=\"color: #008000;\">#.v file<\/span><\/p>\n<p><span style=\"color: #008000;\">module AND (<\/span><br \/>\n<span style=\"color: #008000;\"> input SW0,<\/span><br \/>\n<span style=\"color: #008000;\"> input SW1,<\/span><br \/>\n<span style=\"color: #008000;\"> output LED<\/span><br \/>\n<span style=\"color: #008000;\"> );<\/span><\/p>\n<p><span style=\"color: #008000;\">assign LED = SW0 &amp; SW1;<\/span><\/p>\n<p><span style=\"color: #008000;\">endmodule<\/span><\/p>\n<p>And for the constraints file either:<\/p>\n<p><span style=\"color: #0000ff;\"># UCF<\/span><\/p>\n<p><span style=\"color: #0000ff;\">NET &#8220;SW0&#8221; LOC=J15 | IOSTANDARD=LVCMOS33;<\/span><br \/>\n<span style=\"color: #0000ff;\"> NET &#8220;SW1&#8221; LOC=L16 | IOSTANDARD=LVCMOS33;<\/span><br \/>\n<span style=\"color: #0000ff;\"> NET &#8220;LED&#8221; LOC=H17 | IOSTANDARD=LVCMOS33;<\/span><\/p>\n<p>Or:<\/p>\n<p><span style=\"color: #ff0000;\">#XDC<\/span><\/p>\n<p><span style=\"color: #ff0000;\">set_property -dict { PACKAGE_PIN J15 IOSTANDARD LVCMOS33 } [get_ports { SW0 }];<\/span><br \/>\n<span style=\"color: #ff0000;\"> set_property -dict { PACKAGE_PIN L16 IOSTANDARD LVCMOS33 } [get_ports { SW1 }];<\/span><br \/>\n<span style=\"color: #ff0000;\"> set_property -dict { PACKAGE_PIN H17 IOSTANDARD LVCMOS33 } [get_ports { LED }];<\/span><\/p>\n<p>The <span style=\"color: #ff0000;\">XDC<\/span> and <span style=\"color: #0000ff;\">UCF<\/span> file I used in this example is on the Nexys 4 DDR product page. To view a <span style=\"color: #ff0000;\">XDC<\/span> or <span style=\"color: #0000ff;\">UCF<\/span> file in action see any of our many FPGA examples on the <a href=\"http:\/\/reference.blog.digilentinc.com\">Wiki<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-9778 jlk' data-task='like' data-post_id='9778' data-nonce='ac068a413b' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-9778 lc'>+2<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-9778 jlk' data-task='unlike' data-post_id='9778' data-nonce='ac068a413b' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-9778 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-9778 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>A point of confusion for a lot of people new to FPGA design is the constraints file. People are used to just writing code and having it work. However, in FPGA design we have to specify what hardware is being used.<\/p>\n","protected":false},"author":18,"featured_media":9784,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4327,35],"tags":[1662,1732,453,36],"ppma_author":[4466],"class_list":["post-9778","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","category-fpga","tag-fpga","tag-fpga-design","tag-vivado","tag-xilinx"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/08\/Moving_stairs.gif","jetpack_sharing_enabled":true,"authors":[{"term_id":4466,"user_id":18,"is_guest":0,"slug":"kaitlyn","display_name":"Kaitlyn Franz","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/9276021eaa44781ce19f364cfca46ada25e1500769cf4803a095a5bae83c912a?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/9778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=9778"}],"version-history":[{"count":1,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/9778\/revisions"}],"predecessor-version":[{"id":31235,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/9778\/revisions\/31235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/9784"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=9778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=9778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=9778"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=9778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}