{"id":7388,"date":"2015-06-01T20:00:15","date_gmt":"2015-06-02T03:00:15","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=7388"},"modified":"2025-01-30T15:19:39","modified_gmt":"2025-01-30T23:19:39","slug":"coding-logic-gates","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/coding-logic-gates\/","title":{"rendered":"Coding Logic Gates"},"content":{"rendered":"<p>Earlier today, Josh\u00a0(a\u00a0fellow intern and blog contributor) wrote a <a href=\"https:\/\/digilent.com\/blog\/index.php\/logic-gates\/\">blog post about logic gates<\/a>. After reading through Josh&#8217;s post and gaining an understanding of the concepts and basic functions of those gates, I figured now would be the perfect time to learn some code. I am going to go over each logic gate and it&#8217;s code in <span style=\"color: #008000;\">Verilog<\/span> (a hardware language), <span style=\"color: #ff0000;\">VHDL<\/span> (another hardware language) and <span style=\"color: #800080;\">C<\/span> (software language). If you need a refresher on what a hardware language is, you can read about <a href=\"https:\/\/digilent.com\/blog\/verilog-vs-vhdl\/\" target=\"_blank\" rel=\"noopener\">Verilog vs. VHDL<\/a>. Each gate is shown below with the code for each language in their respective colors.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><strong>NOT:<\/strong><\/p>\n<figure id=\"attachment_7396\" aria-describedby=\"caption-attachment-7396\" style=\"width: 348px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/notdiff.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7396\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/notdiff.png\" alt=\"The not gate.\" width=\"348\" height=\"115\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/notdiff.png 348w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/notdiff-225x74.png 225w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/a><figcaption id=\"caption-attachment-7396\" class=\"wp-caption-text\">The NOT\u00a0gate.<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><span style=\"color: #008000;\">\u00a0 \u00a0 \u00a0assign Y = ~A;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\">\u00a0 \u00a0 \u00a0Y &lt;= NOT A;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0Y = !A;<\/span><\/p>\n<p style=\"text-align: center;\"><strong>AND:<\/strong><\/p>\n<figure id=\"attachment_7171\" aria-describedby=\"caption-attachment-7171\" style=\"width: 406px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/and2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7171\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/and2.png\" alt=\"The and gate.\" width=\"406\" height=\"103\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/and2.png 406w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/and2-225x57.png 225w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-7171\" class=\"wp-caption-text\">The AND\u00a0gate.<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><span style=\"color: #ff99cc;\"><span style=\"color: #008000;\">\u00a0 \u00a0 assign Y = A &amp; B<\/span>;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\">\u00a0 \u00a0 Y &lt;= A\u00a0AND\u00a0B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #800080;\">\u00a0 \u00a0 Y\u00a0= A &amp;&amp; B;<\/span><\/p>\n<p style=\"text-align: center;\"><strong>NAND<\/strong>:<\/p>\n<figure id=\"attachment_7391\" aria-describedby=\"caption-attachment-7391\" style=\"width: 406px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/nandfix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7391\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/nandfix.png\" alt=\"The nand gate.\" width=\"406\" height=\"107\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/nandfix.png 406w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/nandfix-225x59.png 225w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-7391\" class=\"wp-caption-text\">The NAND\u00a0gate.<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><span style=\"color: #008000;\">\u00a0 \u00a0assign Y = ~(A &amp; B);<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\">\u00a0 \u00a0Y &lt;= A NAND\u00a0B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #800080;\">\u00a0 \u00a0Y = !(A &amp;&amp; B);<\/span><\/p>\n<p style=\"text-align: center;\"><strong>OR:<\/strong><\/p>\n<figure id=\"attachment_7197\" aria-describedby=\"caption-attachment-7197\" style=\"width: 406px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/or.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7197\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/or.png\" alt=\"The or gate.\" width=\"406\" height=\"110\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/or.png 406w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/or-225x61.png 225w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-7197\" class=\"wp-caption-text\">The OR\u00a0gate.<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><span style=\"color: #008000;\">\u00a0 \u00a0assign Y = A|B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\">\u00a0 \u00a0Y &lt;= A OR\u00a0B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #800080;\">\u00a0 \u00a0Y = A || B;<\/span><\/p>\n<p style=\"text-align: center;\"><strong>NOR:<\/strong><\/p>\n<figure id=\"attachment_7392\" aria-describedby=\"caption-attachment-7392\" style=\"width: 406px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/norfix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7392\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/norfix.png\" alt=\"The nor gate.\" width=\"406\" height=\"115\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/norfix.png 406w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/norfix-225x64.png 225w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-7392\" class=\"wp-caption-text\">The NOR\u00a0gate.<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><span style=\"color: #008000;\">\u00a0 \u00a0 assign Y = ~(A|B);<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\">\u00a0 \u00a0 Y &lt;= A NOR\u00a0B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #800080;\">\u00a0 \u00a0 Y = ! (A || B);<\/span><\/p>\n<p style=\"text-align: center;\"><strong>XOR:<\/strong><\/p>\n<figure id=\"attachment_7201\" aria-describedby=\"caption-attachment-7201\" style=\"width: 406px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/xor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7201\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/xor.png\" alt=\"The xor gate.\" width=\"406\" height=\"115\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/xor.png 406w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/05\/xor-225x64.png 225w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-7201\" class=\"wp-caption-text\">The XOR\u00a0gate.<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><span style=\"color: #008000;\">\u00a0 \u00a0assign Y = A^B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\">\u00a0 \u00a0Y &lt;= A\u00a0XOR\u00a0B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #800080;\">\u00a0 \u00a0Y = ((!A) &amp;&amp; B) || (A &amp;&amp; (!B));<\/span><\/p>\n<p style=\"text-align: center;\"><strong>XNOR:<\/strong><\/p>\n<figure id=\"attachment_7393\" aria-describedby=\"caption-attachment-7393\" style=\"width: 406px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/xnorfix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7393\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/xnorfix.png\" alt=\"The xnor gate.\" width=\"406\" height=\"115\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/xnorfix.png 406w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/xnorfix-225x64.png 225w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-7393\" class=\"wp-caption-text\">The XNOR\u00a0gate.<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><span style=\"color: #008000;\">\u00a0 \u00a0\u00a0assign Y = A^B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\">\u00a0 \u00a0Y &lt;= A XNOR\u00a0B;<\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #800080;\">\u00a0 \u00a0Y = !((!A) &amp;&amp; B) || (A &amp;&amp; (!B));<\/span><\/p>\n<p style=\"text-align: left;\">As you can see from the examples of all of these gates, Verilog and VHDL aren&#8217;t all that different from C in terms of syntax. In fact, C has bitwise logic operators that are the same as those of the hardware languages. However, bitwise operations aren&#8217;t very common in C. Unfortunately, C doesn&#8217;t have a symbol for XOR and XNOR, so those had to be made out of AND, OR, and NOT\u00a0gates.<\/p>\n<p style=\"text-align: left;\">This content is relevant to the <a href=\"https:\/\/digilent.com\/shop\/basys-3-artix-7-fpga-trainer-board-recommended-for-introductory-users\/\">Basys 3<\/a>, \u00a0<a href=\"https:\/\/digilent.com\/shop\/nexys-a7-fpga-trainer-board-recommended-for-ece-curriculum\/\">Nexys A7<\/a>, <a href=\"https:\/\/digilent.com\/shop\/cmod-a7-35t-breadboardable-artix-7-fpga-module\/\">Cmod A7<\/a>,\u00a0and <a href=\"https:\/\/digilent.com\/shop\/cmod-s7-breadboardable-spartan-7-fpga-module\/\">Cmod S7.<\/a> All can be found on our <a href=\"https:\/\/digilent.com\/shop\/about-system-boards-and-components\/#System\">FPGA product page<\/a>.<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-7388 jlk' data-task='like' data-post_id='7388' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-7388 lc'>+2<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-7388 jlk' data-task='unlike' data-post_id='7388' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-7388 unlc'>-2<\/span><\/a><\/div><\/div> <div class='status-7388 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Earlier today, Josh (a fellow intern and blog contributor) wrote a blog post about logic gates. After reading through Josh&#8217;s post and gaining an understanding of the concepts and basic functions of those gates, I figured now would be the perfect time to learn some code. I am going to go over each logic gate and it&#8217;s code in Verilog (a hardware language), VHDL (another hardware language) and C (software language). <\/p>\n","protected":false},"author":18,"featured_media":7398,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4323,1563],"tags":[],"ppma_author":[4466],"class_list":["post-7388","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/06\/feature.png","authors":[{"term_id":4466,"user_id":18,"is_guest":0,"slug":"kaitlyn","display_name":"Kaitlyn Franz","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/3f16f6159d3666c6fd05a4b73a18c286?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Franz","last_name_2":"","first_name":"Kaitlyn","first_name_2":"","job_title":"","description":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/7388","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=7388"}],"version-history":[{"count":3,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/7388\/revisions"}],"predecessor-version":[{"id":31326,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/7388\/revisions\/31326"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/7398"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=7388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=7388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=7388"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=7388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}