tag:blogger.com,1999:blog-20509621764043057052024-03-18T15:18:19.598+05:30VHDL coding tips and tricksFirst tip is "Think Hardware, not software!"vipinhttp://www.blogger.com/profile/17675762038225600067noreply@blogger.comBlogger127110tag:blogger.com,1999:blog-2050962176404305705.post-30861191398288815992022-04-05T13:22:00.003+05:302022-04-05T13:22:57.470+05:30Extension of Digital Clock Project with 7 segment Decoder<p> In the last post I shared a <a href="https://vhdlguru.blogspot.com/2022/04/digital-clock-with-ability-to-set-time.html" target="_blank">Digital Clock VHDL code</a> with you along with its testbench. Even though it was synthesizable, it would have been a bit cumbersome to test it on a FPGA board as it was. Because the timer outputs were of unsigned type, you would need to use LED's or something like that to see the time. But who would want a LED digital clock!?</p><p><span><span> Many FPGA boards have 7 segment displays in them. And this post is to take advantage of such boards. We will take the initial steps to display the time on 7 segment displays. Of course the codes here would need some additional modifications, based on the type of 7 segment displays available on the board and if they share a common bus etc.. But as I mentioned, this is just an initial step.</span><br /></span></p><p><span><span><span> Without further ado, let me share the codes with you. I have commented the codes plus have uploaded an YouTube video explaining the code. If its useful to you in some way please like and comment on the video. As this helps me with the future direction this blog/YouTube channel will take.</span><br /></span></span></p><p><span><span><span><br /></span></span></span></p><p><span><span></span></span></p><div class="separator" style="clear: both; text-align: center;"><span><span><iframe allowfullscreen="" class="BLOG_video_class" height="381" src="https://www.youtube.com/embed/y26idaFJVS8" width="458" youtube-src-id="y26idaFJVS8"></iframe></span></span></div><span><span><br /><span><br /></span></span></span><p></p><p><b><span style="color: red; font-size: medium;">Digital Clock: <i>digital_clock.vhd</i></span></b></p><p><span><span><span> You can get it from my last post: </span></span></span><a href="https://vhdlguru.blogspot.com/2022/04/digital-clock-with-ability-to-set-time.html" target="_blank">Digital Clock VHDL code</a></p><p><span style="color: red; font-size: medium;"><b><span><span><span>Binary to BCD Converter: </span></span></span><i>bin2bcd.vhd</i></b></span></p><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #888888;">--Library declaration</span>
<span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">IEEE</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.STD_LOGIC_1164.ALL</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.NUMERIC_STD.ALL</span>;
<span style="color: #888888;">--Binary to BCD Converter</span>
<span style="color: #008800; font-weight: bold;">entity</span> <span style="color: #bb0066; font-weight: bold;">bin2bcd</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">port</span>( binary_in : <span style="color: #008800; font-weight: bold;">in</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
bcd_out : <span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>) <span style="color: #888888;">--8 bits(4 bits each for 2 digits)</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">bin2bcd</span>;
<span style="color: #008800; font-weight: bold;">architecture</span> <span style="color: #bb0066; font-weight: bold;">Behavioral</span> <span style="color: #008800; font-weight: bold;">of</span> <span style="color: #bb0066; font-weight: bold;">bin2bcd</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #888888;">--actual value=0.0001100110011001101, so divide by 2^19.</span>
<span style="color: #008800; font-weight: bold;">constant</span> one_by_ten : unsigned(<span style="color: #0000dd; font-weight: bold;">15</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>) := <span style="background-color: #fff0f0;">"1100110011001101"</span>;
<span style="color: #008800; font-weight: bold;">signal</span> result_div_by_ten : unsigned(<span style="color: #0000dd; font-weight: bold;">21</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> msb_digit : unsigned(<span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #888888;">--divide the input by 10. In VHDL, division by a constant is </span>
<span style="color: #888888;">--easily done by multiplication by its inverse.</span>
result_div_by_ten <= binary_in*one_by_ten;
<span style="color: #888888;">--get the decimal part of the result. Bits 18:0 are fractional part</span>
msb_digit <= <span style="color: #0044dd;">'0'</span> & result_div_by_ten(<span style="color: #0000dd; font-weight: bold;">21</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">19</span>);
bcd_out(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">4</span>) <= msb_digit; <span style="color: #888888;">--assign it to MSB part of output </span>
<span style="color: #888888;">--subtract the product, 10*msb_digit, from the input binary number to get LSB digit.</span>
bcd_out(<span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>) <= to_unsigned(to_integer(binary_in) - to_integer(msb_digit)*<span style="color: #0000dd; font-weight: bold;">10</span>, <span style="color: #0000dd; font-weight: bold;">4</span>);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">Behavioral</span>;</pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><p><span style="color: red; font-size: medium;"><b><span><span><span>Top Module: </span></span></span><i>digital_clock_topmodule.vhd</i></b></span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); overflow: auto; tab-size: 2; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #888888;">--Libraries</span>
<span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">IEEE</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.STD_LOGIC_1164.ALL</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.NUMERIC_STD.ALL</span>;
<span style="color: #888888;">--Top module which instantiates and connect together the 3 components:</span>
<span style="color: #888888;">--Digital clock, Binary to BCD converter, BCD to 7 segment code converter</span>
<span style="color: #008800; font-weight: bold;">entity</span> <span style="color: #bb0066; font-weight: bold;">digital_clock_topmodule</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #888888;">--frequency of the clock passed as a generic parameter.</span>
<span style="color: #008800; font-weight: bold;">generic</span>(
CLOCK_FREQ <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">50000000</span>
);
<span style="color: #008800; font-weight: bold;">port</span>(
Clock <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--system clock</span>
reset <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--resets the time</span>
inc_secs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the seconds by 1.</span>
inc_mins <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the minutes by 1.</span>
inc_hrs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the hours by 1.</span>
secs_7seg1 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--seconds LSB digit</span>
secs_7seg10 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--seconds MSB digit</span>
mins_7seg1 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--minutes LSB digit</span>
mins_7seg10 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--minutes MSB digit</span>
hrs_7seg1 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--hours LSB digit</span>
hrs_7seg10 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>) <span style="color: #888888;">--hours MSB digit</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">digital_clock_topmodule</span>;
<span style="color: #008800; font-weight: bold;">architecture</span> <span style="color: #bb0066; font-weight: bold;">Behavioral</span> <span style="color: #008800; font-weight: bold;">of</span> <span style="color: #bb0066; font-weight: bold;">digital_clock_topmodule</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #888888;">--Digital clock component</span>
<span style="color: #008800; font-weight: bold;">COMPONENT</span> <span style="color: #bb0066; font-weight: bold;">digital_clock</span>
<span style="color: #008800; font-weight: bold;">GENERIC</span>( CLOCK_FREQ <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">50000000</span>);
<span style="color: #008800; font-weight: bold;">PORT</span>(
Clock <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
reset <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
inc_secs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
inc_mins <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
inc_hrs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
seconds <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">OUT</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
minutes <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">OUT</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
hours <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">OUT</span> unsigned(<span style="color: #0000dd; font-weight: bold;">4</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>)
);
<span style="color: #008800; font-weight: bold;">END</span> <span style="color: #008800; font-weight: bold;">COMPONENT</span>;
<span style="color: #888888;">--Binary to BCD converter component</span>
<span style="color: #008800; font-weight: bold;">COMPONENT</span> <span style="color: #bb0066; font-weight: bold;">bin2bcd</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">PORT</span>(
binary_in <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
bcd_out <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>)
);
<span style="color: #008800; font-weight: bold;">END</span> <span style="color: #008800; font-weight: bold;">COMPONENT</span>;
<span style="color: #888888;">--Function to convert a BCD digit into a 7 segment code</span>
<span style="color: #888888;">--Source: https://vhdlguru.blogspot.com/2010/03/vhdl-code-for-bcd-to-7-segment-display.html</span>
<span style="color: #888888;">--The function is created by converting the code from the above link.</span>
<span style="color: #008800; font-weight: bold;">function</span> bcd2seg7(bcd_in <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>)) <span style="color: #008800; font-weight: bold;">return</span> unsigned <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">variable</span> segment7 <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #008800; font-weight: bold;">case</span> bcd_in <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0000"=> segment7 :="0000001"</span>; <span style="color: #888888;">-- '0'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0001"=> segment7 :="1001111"</span>; <span style="color: #888888;">-- '1'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0010"=> segment7 :="0010010"</span>; <span style="color: #888888;">-- '2'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0011"=> segment7 :="0000110"</span>; <span style="color: #888888;">-- '3'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0100"=> segment7 :="1001100"</span>; <span style="color: #888888;">-- '4'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0101"=> segment7 :="0100100"</span>; <span style="color: #888888;">-- '5'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0110"=> segment7 :="0100000"</span>; <span style="color: #888888;">-- '6'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"0111"=> segment7 :="0001111"</span>; <span style="color: #888888;">-- '7'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"1000"=> segment7 :="0000000"</span>; <span style="color: #888888;">-- '8'</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="background-color: #fff0f0;">"1001"=> segment7 :="0000100"</span>; <span style="color: #888888;">-- '9'</span>
<span style="color: #888888;">--nothing is displayed when a number more than 9 is given as input.</span>
<span style="color: #008800; font-weight: bold;">when</span> <span style="color: #008800; font-weight: bold;">others</span><span style="color: #333333;">=></span> segment7 <span style="color: #333333;">:=</span><span style="background-color: #fff0f0;">"1111111"</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">case</span>;
<span style="color: #008800; font-weight: bold;">return</span> segment7;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">bcd2seg7</span>;
<span style="color: #888888;">--Declare internal signals </span>
<span style="color: #008800; font-weight: bold;">signal</span> seconds <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> minutes <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> hours <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">4</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> bcd_secs,bcd_mins,bcd_hrs <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> hours_extended <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #888888;">-- Instantiate the Digital Clock component</span>
uut<span style="color: #333333;">:</span> digital_clock
<span style="color: #008800; font-weight: bold;">GENERIC</span> <span style="color: #008800; font-weight: bold;">MAP</span>(CLOCK_FREQ <span style="color: #333333;">=></span> CLOCK_FREQ)
<span style="color: #008800; font-weight: bold;">PORT</span> <span style="color: #008800; font-weight: bold;">MAP</span> (
Clock <span style="color: #333333;">=></span> Clock,
reset <span style="color: #333333;">=></span> reset,
inc_secs <span style="color: #333333;">=></span> inc_secs,
inc_mins <span style="color: #333333;">=></span> inc_mins,
inc_hrs <span style="color: #333333;">=></span> inc_hrs,
seconds <span style="color: #333333;">=></span> seconds,
minutes <span style="color: #333333;">=></span> minutes,
hours <span style="color: #333333;">=></span> hours
);
<span style="color: #888888;">--convert binary to BCD for seconds</span>
bin2bcd_secs <span style="color: #333333;">:</span> bin2bcd
<span style="color: #008800; font-weight: bold;">port</span> <span style="color: #008800; font-weight: bold;">map</span>(
binary_in <span style="color: #333333;">=></span> seconds,
bcd_out <span style="color: #333333;">=></span> bcd_secs
);
<span style="color: #888888;">--convert binary to BCD for minutes</span>
bin2bcd_mins <span style="color: #333333;">:</span> bin2bcd
<span style="color: #008800; font-weight: bold;">port</span> <span style="color: #008800; font-weight: bold;">map</span>(
binary_in <span style="color: #333333;">=></span> minutes,
bcd_out <span style="color: #333333;">=></span> bcd_mins
);
hours_extended <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span> <span style="color: #333333;">&</span> hours; <span style="color: #888888;">--just make it the same size as seconds and minutes.</span>
<span style="color: #888888;">--convert binary to BCD for hours</span>
bin2bcd_hrs <span style="color: #333333;">:</span> bin2bcd
<span style="color: #008800; font-weight: bold;">port</span> <span style="color: #008800; font-weight: bold;">map</span>(
binary_in <span style="color: #333333;">=></span> hours_extended,
bcd_out <span style="color: #333333;">=></span> bcd_hrs
);
<span style="color: #888888;">--Call the bcd2seg7 function to convert each BCD digit into a </span></pre><pre style="line-height: 125%; margin: 0px;"><span style="color: #888888;"><span> --</span>format which can be used on the 7 segment display</span>
secs_7seg1 <span style="color: #333333;"><=</span> bcd2seg7(bcd_secs(<span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>));
secs_7seg10 <span style="color: #333333;"><=</span> bcd2seg7(bcd_secs(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">4</span>));
mins_7seg1 <span style="color: #333333;"><=</span> bcd2seg7(bcd_mins(<span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>));
mins_7seg10 <span style="color: #333333;"><=</span> bcd2seg7(bcd_mins(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">4</span>));
hrs_7seg1 <span style="color: #333333;"><=</span> bcd2seg7(bcd_hrs(<span style="color: #0000dd; font-weight: bold;">3</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>));
hrs_7seg10 <span style="color: #333333;"><=</span> bcd2seg7(bcd_hrs(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">4</span>));
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">Behavioral</span>;</pre></div><p><span style="color: red; font-size: medium;"><b><span><span>Testbench for the top module:<i> </i></span></span><i>tb_digitalClock_topModule.vhd</i></b></span></p>
<!--HTML generated using hilite.me--><div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; overflow: auto; tab-size: 2; width: auto;"><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">LIBRARY</span> <span style="color: #0e84b5; font-weight: bold;">ieee</span>;
<span style="color: #008800; font-weight: bold;">USE</span> <span style="color: #0e84b5; font-weight: bold;">ieee.std_logic_1164.ALL</span>;
<span style="color: #008800; font-weight: bold;">USE</span> <span style="color: #0e84b5; font-weight: bold;">ieee.numeric_std.ALL</span>;
<span style="color: #888888;">--the below library is used for finishing the simulation after we are done. </span></pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #888888;">--Otherwise it will run continously.</span>
<span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">std</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">std.env.finish</span>;
<span style="color: #008800; font-weight: bold;">ENTITY</span> <span style="color: #bb0066; font-weight: bold;">tb_digitalClock_topModule</span> <span style="color: #008800; font-weight: bold;">IS</span>
<span style="color: #008800; font-weight: bold;">END</span> <span style="color: #bb0066; font-weight: bold;">tb_digitalClock_topModule</span>;
<span style="color: #008800; font-weight: bold;">ARCHITECTURE</span> <span style="color: #bb0066; font-weight: bold;">behavior</span> <span style="color: #008800; font-weight: bold;">OF</span> <span style="color: #bb0066; font-weight: bold;">tb_digitalClock_topModule</span> <span style="color: #008800; font-weight: bold;">IS</span>
<span style="color: #888888;">-- Component Declaration for the Unit Under Test (UUT)</span>
<span style="color: #008800; font-weight: bold;">COMPONENT</span> <span style="color: #bb0066; font-weight: bold;">digital_clock_topmodule</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #888888;">--frequency of the clock passed as a generic parameter.</span>
<span style="color: #008800; font-weight: bold;">generic</span>(
CLOCK_FREQ <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">50000000</span>
);
<span style="color: #008800; font-weight: bold;">port</span>(
Clock <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--system clock</span>
reset <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--resets the time</span>
inc_secs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the seconds by 1.</span>
inc_mins <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the minutes by 1.</span>
inc_hrs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the hours by 1.</span>
secs_7seg1 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--seconds LSB digit</span>
secs_7seg10 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--seconds MSB digit</span>
mins_7seg1 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--minutes LSB digit</span>
mins_7seg10 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--minutes MSB digit</span>
hrs_7seg1 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--hours LSB digit</span>
hrs_7seg10 <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>) <span style="color: #888888;">--hours MSB digit</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">COMPONENT</span>;
<span style="color: #888888;">--Inputs</span>
<span style="color: #008800; font-weight: bold;">signal</span> Clock <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> reset <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> inc_secs <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> inc_mins <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> inc_hrs <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #888888;">--Outputs</span>
<span style="color: #008800; font-weight: bold;">signal</span> secs_7seg1,secs_7seg10 : unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);</pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span> </span><span style="color: #008800; font-weight: bold;">signal</span> mins_7seg1,mins_7seg10 <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);</pre><pre style="background-color: white; line-height: 125%; margin: 0px;"> <span style="color: #008800; font-weight: bold;">signal</span> hrs_7seg1,hrs_7seg10 <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">6</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);</pre><pre style="background-color: white; line-height: 125%; margin: 0px;"> <span style="color: #888888;">-- Clock period definitions</span>
<span style="color: #008800; font-weight: bold;">constant</span> Clock_period <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">time</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">10</span> ns;
<span style="color: #888888;">--Clock frequency in Hz. Use a smaller value for testbench. </span></pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #888888;"><span> --</span>When testing on board it need to be set as 50 million, 100 million etc.</span>
<span style="color: #008800; font-weight: bold;">constant</span> CLOCK_FREQ <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">10</span>;
<span style="color: #008800; font-weight: bold;">BEGIN</span>
<span style="color: #888888;">-- Instantiate the Unit Under Test (UUT)</span>
uut<span style="color: #333333;">:</span> digital_clock_topmodule
<span style="color: #008800; font-weight: bold;">GENERIC</span> <span style="color: #008800; font-weight: bold;">MAP</span>(CLOCK_FREQ <span style="color: #333333;">=></span> CLOCK_FREQ)
<span style="color: #008800; font-weight: bold;">PORT</span> <span style="color: #008800; font-weight: bold;">MAP</span> (
Clock <span style="color: #333333;">=></span> Clock,
reset <span style="color: #333333;">=></span> reset,
inc_secs <span style="color: #333333;">=></span> inc_secs,
inc_mins <span style="color: #333333;">=></span> inc_mins,
inc_hrs <span style="color: #333333;">=></span> inc_hrs,
secs_7seg1 <span style="color: #333333;">=></span> secs_7seg1,
secs_7seg10 <span style="color: #333333;">=></span> secs_7seg10,
mins_7seg1 <span style="color: #333333;">=></span> mins_7seg1,
mins_7seg10 <span style="color: #333333;">=></span> mins_7seg10,
hrs_7seg1 <span style="color: #333333;">=></span> hrs_7seg1,
hrs_7seg10 <span style="color: #333333;">=></span> hrs_7seg10
);
<span style="color: #888888;">-- Clock process definitions</span>
Clock_process <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">process</span>
<span style="color: #008800; font-weight: bold;">begin</span>
Clock <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">/</span><span style="color: #0000dd; font-weight: bold;">2</span>;
Clock <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">/</span><span style="color: #0000dd; font-weight: bold;">2</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
<span style="color: #888888;">-- Stimulus process</span>
stim_proc<span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">process</span>
<span style="color: #008800; font-weight: bold;">begin</span>
reset <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>;
<span style="color: #888888;">-- hold reset state for 100 ns.</span>
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">100</span> ns;
reset <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span><span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span><span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">25</span>; <span style="color: #888888;">--run the clock for 25 hours</span>
<span style="color: #888888;">--increment seconds</span>
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period;
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 secs after incrementing seconds once.</span>
<span style="color: #888888;">--increment seconds 60 times</span>
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span>;
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 secs after incrementing seconds.</span>
<span style="color: #888888;">--increment minutes</span>
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period;
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 mins after incrementing minutes once.</span>
<span style="color: #888888;">--increment minutes 60 times</span>
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span>;
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 mins after incrementing minutes.</span>
<span style="color: #888888;">--increment hours</span>
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period;
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 hours after incrementing hours once.</span>
<span style="color: #888888;">--increment hours 25 times</span>
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">25</span>;
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 hours after incrementing hours.</span>
<span style="color: #888888;">--apply reset</span>
reset <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>;
<span style="color: #888888;">--wait for 100 Clock cycles and then finish the simulation.</span>
<span style="color: #888888;">--with the current settings it will run around 9 ms of simualtion time. </span>
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">100</span>;
finish;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
<span style="color: #008800; font-weight: bold;">END</span>;
</pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><br /></pre><b><span style="color: red; font-size: medium;">Top Level Block Diagrams:</span></b><pre style="background-color: white; line-height: 125%; margin: 0px;"><br /></pre><pre style="background-color: white; line-height: 125%; margin: 0px;">The code was synthesized in Xilinx ISE 14.7. And the top level block diagram is shown below:</pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><br /></pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><br /></pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEg7gNz5IJ-lFN7xG2NzaXbLhySTty48QY2gMOyTeeaqO2AnXBclcbpKbsceIZoNvMj2-FmPyQsx44TgZ7tEEEdMwmEwh3i-ZqE7HjqFMU_pAQ5xIkcmMpIC3Jz7gYa7ltTBOa_0EjTFipQ1K0zX0AuVQ1GEJWfGgQgbPP-AW1UbIKV-ePk-qGVpDALfWg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="751" data-original-width="744" height="314" src="https://blogger.googleusercontent.com/img/a/AVvXsEg7gNz5IJ-lFN7xG2NzaXbLhySTty48QY2gMOyTeeaqO2AnXBclcbpKbsceIZoNvMj2-FmPyQsx44TgZ7tEEEdMwmEwh3i-ZqE7HjqFMU_pAQ5xIkcmMpIC3Jz7gYa7ltTBOa_0EjTFipQ1K0zX0AuVQ1GEJWfGgQgbPP-AW1UbIKV-ePk-qGVpDALfWg=w311-h314" width="311" /></a></div><pre style="line-height: 125%; margin: 0px;"><br /></pre><pre style="line-height: 125%; margin: 0px;"><br /></pre><pre style="line-height: 125%; margin: 0px;">The inside view of the top module looks like this:</pre><pre style="line-height: 125%; margin: 0px;"><br /></pre><pre style="line-height: 125%; margin: 0px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/a/AVvXsEg3GEafGNL9QwqdYUbjvgTp-Pn_w6MAvVk5U7Yc7FiQptjQYSu5nOv_J96KHDnHqPIh6Dw_DF4ONDnLoq2VtRe-HeE4jHiLCNnynyl9m4AY7TmqsPvJv23gxVWGoEsRdleOzTBfWUiBZ_SS-r0JnJrKM6NwiE_mdc3ZkXYjE1o5Spnp0NW_w1Qm-v_czA" style="margin-left: 1em; margin-right: 1em;"><img data-original-height="1419" data-original-width="1219" height="400" src="https://blogger.googleusercontent.com/img/a/AVvXsEg3GEafGNL9QwqdYUbjvgTp-Pn_w6MAvVk5U7Yc7FiQptjQYSu5nOv_J96KHDnHqPIh6Dw_DF4ONDnLoq2VtRe-HeE4jHiLCNnynyl9m4AY7TmqsPvJv23gxVWGoEsRdleOzTBfWUiBZ_SS-r0JnJrKM6NwiE_mdc3ZkXYjE1o5Spnp0NW_w1Qm-v_czA=w343-h400" width="343" /></a></div><br /><br /></pre><pre style="line-height: 125%; margin: 0px;"><br /></pre><br /><br /></pre></div>
vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0tag:blogger.com,1999:blog-2050962176404305705.post-86977007109785110632022-04-03T11:58:00.000+05:302022-04-03T11:58:29.824+05:30Digital Clock (With ability to Set time) And Testbench in VHDL<span> More than a decade back I had written a Digital Clock module in this blog, which was when I just started learning VHDL. Obviously it had its own shortcomings and through this post, I wanted to rectify these shortcomings. Plus add the ability to set time.</span><div><span><br /></span></div><div>The codes are shared below. They are commented to help you understand the logic. For a detailed understanding watch the video. Also please make sure to like the video if it was helpful and subscribe to my YouTube channel for more such videos in the future. </div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="368" src="https://www.youtube.com/embed/1PiqMRXQCAw" width="443" youtube-src-id="1PiqMRXQCAw"></iframe></div><br /><div><br /></div><div><br /></div><div><b><u><span style="color: red; font-size: medium;">Digital Clock:</span></u></b></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); overflow: auto; tab-size: 2; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #888888;">--Declare the libraries</span>
<span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">IEEE</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.STD_LOGIC_1164.ALL</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.NUMERIC_STD.ALL</span>; <span style="color: #888888;">--we need this one because we are using "unsigned" data type.</span>
<span style="color: #008800; font-weight: bold;">entity</span> <span style="color: #bb0066; font-weight: bold;">digital_clock</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #888888;">--frequency of the clock passed as a generic parameter.</span>
<span style="color: #008800; font-weight: bold;">generic</span>( CLOCK_FREQ <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">50000000</span> );
<span style="color: #008800; font-weight: bold;">port</span>(
Clock <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--system clock</span>
reset <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--resets the time</span>
inc_secs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the seconds by 1.</span>
inc_mins <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the minutes by 1.</span>
inc_hrs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--set a pulse here to increment the hours by 1.</span>
seconds <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--seconds output</span>
minutes <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>); <span style="color: #888888;">--minutes output</span>
hours <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">4</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>) <span style="color: #888888;">--hours output</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">digital_clock</span>;
<span style="color: #008800; font-weight: bold;">architecture</span> <span style="color: #bb0066; font-weight: bold;">Behavioral</span> <span style="color: #008800; font-weight: bold;">of</span> <span style="color: #bb0066; font-weight: bold;">digital_clock</span> <span style="color: #008800; font-weight: bold;">is</span>
<br /></pre><pre style="line-height: 125%; margin: 0px;"><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #888888;">--temperory signals as we cant directly perform arithmetic operations on outputs</span></pre><span style="color: #008800; font-weight: bold;">signal</span> secs, mins, hrs <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">0</span>; </pre><pre style="line-height: 125%; margin: 0px;"><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #888888;">--counter used for getting the 1 sec duration from the system Clock.</span></pre><span style="color: #008800; font-weight: bold;">signal</span> counter <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #008800; font-weight: bold;">process</span>(Clock, reset)
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #008800; font-weight: bold;">if</span>(reset <span style="color: #333333;">=</span> <span style="color: #0044dd;">'1'</span>) <span style="color: #008800; font-weight: bold;">then</span> <span style="color: #888888;">--reset the time.</span>
secs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
mins <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
hrs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
counter <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">elsif</span>(rising_edge(Clock)) <span style="color: #008800; font-weight: bold;">then</span>
<span style="color: #888888;">--increment the seconds. also increment mins and hours if needed.</span>
<span style="color: #008800; font-weight: bold;">if</span>(inc_secs <span style="color: #333333;">=</span> <span style="color: #0044dd;">'1'</span>) <span style="color: #008800; font-weight: bold;">then</span>
<span style="color: #008800; font-weight: bold;">if</span>(secs <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">59</span>) <span style="color: #008800; font-weight: bold;">then</span>
secs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">if</span>(mins <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">59</span>) <span style="color: #008800; font-weight: bold;">then</span>
mins <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">if</span>(hrs <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">23</span>) <span style="color: #008800; font-weight: bold;">then</span>
hrs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">else</span>
hrs <span style="color: #333333;"><=</span> hrs<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
mins <span style="color: #333333;"><=</span> mins<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
secs <span style="color: #333333;"><=</span> secs <span style="color: #333333;">+</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #888888;">--increment the minutes. also increment hours if needed.</span>
<span style="color: #008800; font-weight: bold;">elsif</span>(inc_mins <span style="color: #333333;">=</span> <span style="color: #0044dd;">'1'</span>) <span style="color: #008800; font-weight: bold;">then</span>
<span style="color: #008800; font-weight: bold;">if</span>(mins <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">59</span>) <span style="color: #008800; font-weight: bold;">then</span>
mins <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">if</span>(hrs <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">23</span>) <span style="color: #008800; font-weight: bold;">then</span>
hrs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">else</span>
hrs <span style="color: #333333;"><=</span> hrs<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
mins <span style="color: #333333;"><=</span> mins<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #888888;">--increment the hours. </span>
<span style="color: #008800; font-weight: bold;">elsif</span>(inc_hrs <span style="color: #333333;">=</span> <span style="color: #0044dd;">'1'</span>) <span style="color: #008800; font-weight: bold;">then</span>
<span style="color: #008800; font-weight: bold;">if</span>(hrs <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">23</span>) <span style="color: #008800; font-weight: bold;">then</span>
hrs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">else</span>
hrs <span style="color: #333333;"><=</span> hrs<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #888888;">--regular operation of the clock</span>
<span style="color: #008800; font-weight: bold;">if</span>(counter <span style="color: #333333;">=</span> CLOCK_FREQ<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span>) <span style="color: #008800; font-weight: bold;">then</span> <span style="color: #888888;">--counting CLOCK_FREQ times takes 1 second. </span>
counter <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #888888;">--check and change values of secs, mins and hours</span>
<span style="color: #008800; font-weight: bold;">if</span>(secs <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">59</span>) <span style="color: #008800; font-weight: bold;">then</span>
secs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">if</span>(mins <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">59</span>) <span style="color: #008800; font-weight: bold;">then</span>
mins <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">if</span>(hrs <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">23</span>) <span style="color: #008800; font-weight: bold;">then</span>
hrs <span style="color: #333333;"><=</span> <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">else</span>
hrs <span style="color: #333333;"><=</span> hrs<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
mins <span style="color: #333333;"><=</span> mins<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
secs <span style="color: #333333;"><=</span> secs <span style="color: #333333;">+</span> <span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
counter <span style="color: #333333;"><=</span> counter<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
<span style="color: #888888;">--The internal integer signals are converted into unsigned format. </span>
<span style="color: #888888;">--The size of the output unsigned signal is assigned via the 2nd parameter(5 or 6 bits)</span></pre><pre style="line-height: 125%; margin: 0px;">seconds <span style="color: #333333;"><=</span> to_unsigned(secs, <span style="color: #0000dd; font-weight: bold;">6</span>);
minutes <span style="color: #333333;"><=</span> to_unsigned(mins, <span style="color: #0000dd; font-weight: bold;">6</span>);
hours <span style="color: #333333;"><=</span> to_unsigned(hrs, <span style="color: #0000dd; font-weight: bold;">5</span>);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">Behavioral</span>;</pre></div>
<div><b><u><span style="color: red; font-size: medium;"><br /></span></u></b></div><div><b><u><span style="color: red; font-size: medium;">Testbench:</span></u></b></div><div><br /></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; overflow: auto; tab-size: 2; width: auto;"><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">LIBRARY</span> <span style="color: #0e84b5; font-weight: bold;">ieee</span>;
<span style="color: #008800; font-weight: bold;">USE</span> <span style="color: #0e84b5; font-weight: bold;">ieee.std_logic_1164.ALL</span>;
<span style="color: #008800; font-weight: bold;">USE</span> <span style="color: #0e84b5; font-weight: bold;">ieee.numeric_std.ALL</span>;
<span style="color: #888888;">--the below library is used for finishing the simulation after we are done. </span></pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #888888;">--Otherwise it will run continuously.</span>
<span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">std</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">std.env.finish</span>;
<span style="color: #008800; font-weight: bold;">ENTITY</span> <span style="color: #bb0066; font-weight: bold;">tb_digitalClock</span> <span style="color: #008800; font-weight: bold;">IS</span>
<span style="color: #008800; font-weight: bold;">END</span> <span style="color: #bb0066; font-weight: bold;">tb_digitalClock</span>;
<span style="color: #008800; font-weight: bold;">ARCHITECTURE</span> <span style="color: #bb0066; font-weight: bold;">behavior</span> <span style="color: #008800; font-weight: bold;">OF</span> <span style="color: #bb0066; font-weight: bold;">tb_digitalClock</span> <span style="color: #008800; font-weight: bold;">IS</span>
<span style="color: #888888;">-- Component Declaration for the Unit Under Test (UUT)</span>
<span style="color: #008800; font-weight: bold;">COMPONENT</span> <span style="color: #bb0066; font-weight: bold;">digital_clock</span>
<span style="color: #008800; font-weight: bold;">generic</span>( CLOCK_FREQ <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">50000000</span> );
<span style="color: #008800; font-weight: bold;">PORT</span>(
Clock <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
reset <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
inc_secs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
inc_mins <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
inc_hrs <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">IN</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
seconds <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">OUT</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
minutes <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">OUT</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
hours <span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">OUT</span> unsigned(<span style="color: #0000dd; font-weight: bold;">4</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>)
);
<span style="color: #008800; font-weight: bold;">END</span> <span style="color: #008800; font-weight: bold;">COMPONENT</span>;
<span style="color: #888888;">--Inputs</span>
<span style="color: #008800; font-weight: bold;">signal</span> Clock <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> reset <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> inc_secs <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> inc_mins <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">signal</span> inc_hrs <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #333333;">:=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #888888;">--Outputs</span>
<span style="color: #008800; font-weight: bold;">signal</span> seconds <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> minutes <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">5</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> hours <span style="color: #333333;">:</span> unsigned(<span style="color: #0000dd; font-weight: bold;">4</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #888888;">-- Clock period definitions</span>
<span style="color: #008800; font-weight: bold;">constant</span> Clock_period <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">time</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">10</span> ns;
<span style="color: #888888;">--Clock frequency in Hz. Use a smaller value for testbench. </span></pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #888888;"><span> --</span>When testing on board it need to be set as 50 million, 100 million etc.</span>
<span style="color: #008800; font-weight: bold;">constant</span> CLOCK_FREQ <span style="color: #333333;">:</span> <span style="color: #333399; font-weight: bold;">integer</span> <span style="color: #333333;">:=</span> <span style="color: #0000dd; font-weight: bold;">10</span>;
<span style="color: #008800; font-weight: bold;">BEGIN</span>
<span style="color: #888888;">-- Instantiate the Unit Under Test (UUT)</span>
uut<span style="color: #333333;">:</span> digital_clock
<span style="color: #008800; font-weight: bold;">GENERIC</span> <span style="color: #008800; font-weight: bold;">MAP</span>(CLOCK_FREQ <span style="color: #333333;">=></span> CLOCK_FREQ)
<span style="color: #008800; font-weight: bold;">PORT</span> <span style="color: #008800; font-weight: bold;">MAP</span> (
Clock <span style="color: #333333;">=></span> Clock,
reset <span style="color: #333333;">=></span> reset,
inc_secs <span style="color: #333333;">=></span> inc_secs,
inc_mins <span style="color: #333333;">=></span> inc_mins,
inc_hrs <span style="color: #333333;">=></span> inc_hrs,
seconds <span style="color: #333333;">=></span> seconds,
minutes <span style="color: #333333;">=></span> minutes,
hours <span style="color: #333333;">=></span> hours
);
<span style="color: #888888;">-- Clock process definitions</span>
Clock_process <span style="color: #333333;">:</span><span style="color: #008800; font-weight: bold;">process</span>
<span style="color: #008800; font-weight: bold;">begin</span>
Clock <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">/</span><span style="color: #0000dd; font-weight: bold;">2</span>;
Clock <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">/</span><span style="color: #0000dd; font-weight: bold;">2</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
<span style="color: #888888;">-- Stimulus process</span>
stim_proc<span style="color: #333333;">:</span> <span style="color: #008800; font-weight: bold;">process</span>
<span style="color: #008800; font-weight: bold;">begin</span>
reset <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>;
<span style="color: #888888;">-- hold reset state for 100 ns.</span>
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">100</span> ns;
reset <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span><span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span><span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">25</span>; <span style="color: #888888;">--run the clock for 25 hours</span>
<span style="color: #888888;">--increment seconds</span>
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period;
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 secs after incrementing seconds once.</span>
<span style="color: #888888;">--increment seconds 60 times</span>
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span>;
inc_secs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 secs after incrementing seconds.</span>
<span style="color: #888888;">--increment minutes</span>
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period;
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 secs after incrementing minutes once.<br /></span>
<span style="color: #888888;">--increment minutes 60 times</span>
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">60</span>;
inc_mins <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 secs after incrementing minutes.<br /></span>
<span style="color: #888888;">--increment hours</span>
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period;
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="background-color: white; line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;"><span> </span>wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span>CLOCK_FREQ<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">5</span>; <span style="color: #888888;">--wait for 5 secs after incrementing hours once.<br /></span>
<span style="color: #888888;">--increment hours 25 times</span>
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'1'</span>; <span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> Clock_period<span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">25</span>;
inc_hrs <span style="color: #333333;"><=</span> <span style="color: #0044dd;">'0'</span>; </pre><pre style="line-height: 125%; margin: 0px;"><span style="background-color: white; color: #008800; font-weight: bold;"><span> </span>wait</span><span style="background-color: white;"> </span><span style="background-color: white; color: #008800; font-weight: bold;">for</span><span style="background-color: white;"> Clock_period</span><span style="background-color: white; color: #333333;">*</span><span style="background-color: white;">CLOCK_FREQ</span><span style="background-color: white; color: #333333;">*</span><span style="background-color: white; color: #0000dd; font-weight: bold;">5</span><span style="background-color: white;">; </span><span style="background-color: white; color: #888888;">--wait for 5 secs after incrementing hours.<br /></span><span style="background-color: white;">
</span><span style="background-color: white; color: #888888;">--apply reset</span><span style="background-color: white;">
reset </span><span style="background-color: white; color: #333333;"><=</span><span style="background-color: white;"> </span><span style="background-color: white; color: #0044dd;">'1'</span><span style="background-color: white;">;
</span><span style="background-color: white; color: #888888;">--wait for 100 Clock cycles and then finish the simulation.</span><span style="background-color: white;">
</span><span style="background-color: white; color: #888888;">--with the current settings it will run around 9 ms of simualtion time. </span><span style="background-color: white;">
</span><span style="background-color: white; color: #008800; font-weight: bold;">wait</span><span style="background-color: white;"> </span><span style="background-color: white; color: #008800; font-weight: bold;">for</span><span style="background-color: white;"> Clock_period</span><span style="background-color: white; color: #333333;">*</span><span style="background-color: white; color: #0000dd; font-weight: bold;">100</span><span style="background-color: white;">;
finish;
</span><span style="background-color: white; color: #008800; font-weight: bold;">end</span><span style="background-color: white;"> </span><span style="background-color: white; color: #008800; font-weight: bold;">process</span><span style="background-color: white;">;
</span><span style="background-color: white; color: #008800; font-weight: bold;">END</span><span style="background-color: white;">;</span>
</pre><br /><br /><b><u><span style="color: red; font-size: medium;">So what next?</span></u></b><br /><br /><span> </span>As an extension of this project I want to show you how you can convert the time into BCD format and then connect a BCD to 7 segment converter to it. This will be useful for those who want to try this clock on a real FPGA board. Look forward to it in the next post.<br /></div><div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; overflow: auto; tab-size: 2; width: auto;"><br /></div><div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; overflow: auto; tab-size: 2; width: auto;">So until next time...</div><div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; overflow: auto; tab-size: 2; width: auto;"><br /></div>
vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0tag:blogger.com,1999:blog-2050962176404305705.post-31720800074198325022020-12-20T17:57:00.003+05:302020-12-20T18:28:33.666+05:30Image Processsing: RGB to Gray scale Converter in VHDL<p><span> Implementing image processing algorithms in VHDL is a scary thing for many. Though I agree that its much more difficult to do it in VHDL than in a high level programming language like C, Matlab etc, it needn't be that scary.</span><br /></p><p><span> </span>In this post I am going to share the code for a simple image processing algorithm - A RGB to Gray scale image converter. </p><p><span> </span>There are many ways, from simple to complex, in which you can do this. I have done it in a way, which makes sense to me. Touching upon few topics related to this subject. For example reading the image data from a text file, storing it in RAM and accessing the data within the code and then manipulating them etc...</p><p><span> I have used the standard Matlab image Lenna.bmp for this. The original image was 512*512*3 pixels in size. This takes a long time to load and run in Modelsim. So I first reduced its size to 1/8th of its original size, making it a 64*64*3 pixel image. Each pixel ranges from 0 to 255 and the dimension "3" indicates the presence of Red, Green and Blue components.</span><br /></p><p><span> VHDL text file operations aren't ideal for reading multiple pixels from the same row. So I converted the 3 Dimensional image data into a 1 Dimensional matrix. And then used <i>dlmwrite</i> Matlab command to write it to a text file named rgb.txt. This will be our input image.<br /></span></p><p><span><span><span> In Matlab, I manually converted the above RGB image into a gray scale image using the formula:</span></span></span></p><p><span style="background-color: white; font-family: Arial, Verdana, Tahoma; font-size: 16px; text-align: justify;"> Grayscale Image = ( (0.3 * R) + (0.59 * G) + (0.11 * B) ).</span></p><p><span> </span>The above grayscale pixels were converted to a 1-D matrix and written to a text file called gray.txt. This text file would be read by our VHDL testbench to verify that the results from our VHDL design is the same as the ideal result obtained from Matlab.</p><p>The <span style="font-size: large;"><b>Matlab program</b></span> which I used for achieving all this is shared below:</p><p><br /></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;">I=imread(</span><span style="color: #aa04f9; font-family: "Courier New"; font-size: 10pt;">'Lenna.bmp'</span><span style="font-family: "Courier New"; font-size: 10pt;">); </span><span style="color: #028009; font-family: "Courier New"; font-size: 10pt;">%read the
image into memory</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;">I=imresize(I,1/8); </span><span style="color: #028009; font-family: "Courier New"; font-size: 10pt;">%reduce the
size by 8 times.</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #028009; font-family: "Courier New"; font-size: 10pt;">%convert image to 1-D array and write it to a text file.</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;">dlmwrite(</span><span style="color: #aa04f9; font-family: "Courier New"; font-size: 10pt;">'rgb.txt'</span><span style="font-family: "Courier New"; font-size: 10pt;">,reshape(I,64*64*3,1,1)); </span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;">I4=double(I); </span><span style="color: #028009; font-family: "Courier New"; font-size: 10pt;">%convert it to
double format</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #028009; font-family: "Courier New"; font-size: 10pt;">%convert rgb pixels to gray manually as per formula.</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #0e00ff; font-family: "Courier New"; font-size: 10pt;">for</span><span style="font-family: "Courier New"; font-size: 10pt;"> i=1:64</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;"> </span><span style="color: #0e00ff; font-family: "Courier New"; font-size: 10pt;">for</span><span style="font-family: "Courier New"; font-size: 10pt;"> j=1:64</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;"> I2(i,j) =
I4(i,j,1)*0.3 + I4(i,j,2)*0.59 + I4(i,j,3)*0.11;</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;"> </span><span style="color: #0e00ff; font-family: "Courier New"; font-size: 10pt;">end</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #0e00ff; font-family: "Courier New"; font-size: 10pt;">end</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: #028009; font-family: "Courier New"; font-size: 10pt;">%the converted image is changed to 1-D and then written to a
text file.</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;">I3=reshape(I2,64*64,1);</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p>
</p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Courier New"; font-size: 10pt;">dlmwrite(</span><span style="color: #aa04f9; font-family: "Courier New"; font-size: 10pt;">'gray.txt'</span><span style="font-family: "Courier New"; font-size: 10pt;">,I3);</span><span style="font-family: "Courier New"; font-size: 10pt; mso-bidi-font-family: Mangal;"><o:p></o:p></span></p><p><br /></p><p><span> The standard RGB format Lenna image, the resized version of it and the converted Grayscale image are shared below:</span></p><p><span><br /></span></p><p><span></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMaIvGqat5OEnCLeXSjzDAR_h-dD0Yz5yRqg-jM72kAiwmffwuK6NgbvDnXl4RQ1p0JynlxzH2Dd0sr_Oh9iLvyvDH-HGqmXU5MCPz1iYKh_IL9TE9grvmW2Bs5PYJI7NKHvarQKLH9sfo/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="588" data-original-width="1142" height="330" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMaIvGqat5OEnCLeXSjzDAR_h-dD0Yz5yRqg-jM72kAiwmffwuK6NgbvDnXl4RQ1p0JynlxzH2Dd0sr_Oh9iLvyvDH-HGqmXU5MCPz1iYKh_IL9TE9grvmW2Bs5PYJI7NKHvarQKLH9sfo/" width="640" /></a></div><br /><br /><p></p><p><span><span> </span>Now that we have prepared the above basic data to work with, we can go and explore the VHDL designs. There are three VHDL files in this project:</span></p><h3 style="text-align: left;">1) im_ram.vhd:</h3><p> The input RGB image is internally stored in a RAM. The RAM is declared and initialized with the pixel values from the text file <i>rgb.txt</i>. The <i>std.texti</i>o library is used, to do the file reading operations.<br /></p><p><span><br /></span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--RAM entity for storing the image.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #6a9955;">--the below libraries are needed for reading text file.</span></div><div><span style="color: #569cd6;">library</span> std;</div><div><span style="color: #569cd6;">use</span> std.textio.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">im_ram</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(</div><div> ADDR_WIDTH : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">16</span>; <span style="color: #6a9955;">--Address bus size of the Image Ram.</span></div><div> IM_SIZE_D1 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span>; <span style="color: #6a9955;">--Size along Dimension 1</span></div><div> IM_SIZE_D2 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span> <span style="color: #6a9955;">--Size along Dimension 2</span></div><div> );</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clk : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> addr_in : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">unsigned</span>(ADDR_WIDTH-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--Address bus to the Image Ram.</span></div><div> rgb_out : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic_vector</span>(<span style="color: #b5cea8;">23</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--24 bit RGB pixel output</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">im_ram</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">Behav</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">im_ram</span> <span style="color: #569cd6;">is</span></div><br /><div><span style="color: #6a9955;">--custom array declaration.</span></div><div><span style="color: #569cd6;">type</span> <span style="color: #4ec9b0;">im_ram_type</span> <span style="color: #569cd6;">is</span> <span style="color: #569cd6;">array</span> (<span style="color: #b5cea8;">0</span> <span style="color: #569cd6;">to</span> IM_SIZE_D1*IM_SIZE_D2-<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">std_logic_vector</span>(<span style="color: #b5cea8;">23</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><br /><div><span style="color: #6a9955;">--function for reading the image pixels from text file and use</span></div><div><span style="color: #6a9955;">--it to initialize the RAM.</span></div><div><span style="color: #569cd6;">impure function</span> <span style="color: #dcdcaa;">im_ram_initialize</span> <span style="color: #569cd6;">return</span> im_ram_type <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">variable</span> line_var : <span style="color: #4ec9b0;">line</span>;</div><div> <span style="color: #569cd6;">file</span> text_var : <span style="color: #4ec9b0;">text</span>;</div><div> <span style="color: #569cd6;">variable</span> pixel : <span style="color: #4ec9b0;">integer</span>;</div><div> <span style="color: #569cd6;">variable</span> image_pixels : im_ram_type;</div><div><span style="color: #569cd6;">begin</span> </div><div> <span style="color: #6a9955;">--Open the file in read mode.</span></div><div> file_open(text_var,<span style="color: #ce9178;">"rgb.txt"</span>,read_mode); </div><div> <span style="color: #569cd6;">while</span>(<span style="color: #569cd6;">NOT</span> <span style="color: #dcdcaa;">ENDFILE</span>(text_var)) <span style="color: #569cd6;">loop</span> <span style="color: #6a9955;">--until end of file is reached</span></div><div> <span style="color: #569cd6;">for</span> k <span style="color: #569cd6;">in</span> <span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">to</span> <span style="color: #b5cea8;">3</span> <span style="color: #569cd6;">loop</span> <span style="color: #6a9955;">--through R, G and B.</span></div><div> <span style="color: #569cd6;">for</span> j <span style="color: #569cd6;">in</span> <span style="color: #b5cea8;">0</span> <span style="color: #569cd6;">to</span> IM_SIZE_D2-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">loop</span></div><div> <span style="color: #569cd6;">for</span> i <span style="color: #569cd6;">in</span> <span style="color: #b5cea8;">0</span> <span style="color: #569cd6;">to</span> IM_SIZE_D1-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">loop</span></div><div> <span style="color: #dcdcaa;">readline</span>(text_var,line_var); <span style="color: #6a9955;">--read one row. Each row contains one pixel.</span></div><div> <span style="color: #dcdcaa;">read</span>(line_var,pixel); <span style="color: #6a9955;">--From the read line, read the integer value.</span></div><div> <span style="color: #6a9955;">--save the pixel in the RAM.</span></div><div> image_pixels(i*IM_SIZE_D2+j)(k*<span style="color: #b5cea8;">8</span>-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> k*<span style="color: #b5cea8;">8</span>-<span style="color: #b5cea8;">8</span>) := <span style="color: #4ec9b0;">std_logic_vector</span>(<span style="color: #dcdcaa;">to_unsigned</span>(pixel,<span style="color: #b5cea8;">8</span>));</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> file_close(text_var); <span style="color: #6a9955;">--close the file after reading.</span></div><div> <span style="color: #569cd6;">return</span> image_pixels; </div><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">function</span>;</div><br /><div><span style="color: #6a9955;">--declare and initialize the image ram.</span></div><div><span style="color: #569cd6;">signal</span> ram : im_ram_type := im_ram_initialize;</div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div><span style="color: #6a9955;">--read the R,G and B pixels from RAM with the addr_in input.</span></div><div>rgb_out <= ram(<span style="color: #dcdcaa;">to_integer</span>(addr_in));</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><h3 style="text-align: left;"><br /></h3><h3 style="text-align: left;">2) rgb2gray.vhd:</h3><p><span> This is the top level entity which converts the image from RGB format to Grayscale. The RAM block is instantiated as a component inside this entity. </span><br /></p><p><span><br /></span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--Convert a internally stored RGB image into gray image.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">rgb2gray</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(</div><div> ADDR_WIDTH : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">16</span>; <span style="color: #6a9955;">--Address bus size of the Image Ram.</span></div><div> IM_SIZE_D1 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span>; <span style="color: #6a9955;">--Size along Dimension 1</span></div><div> IM_SIZE_D2 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span> <span style="color: #6a9955;">--Size along Dimension 2</span></div><div> );</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clk : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> reset : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>; <span style="color: #6a9955;">--active high asynchronous reset</span></div><div> data_valid : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic</span>; <span style="color: #6a9955;">--High when gray_out has valid output.</span></div><div> gray_out : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">unsigned</span>(<span style="color: #b5cea8;">7</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--8 bit gray pixel output</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">rgb2gray</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">Behav</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">rgb2gray</span> <span style="color: #569cd6;">is</span></div><br /><div> <span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">im_ram</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(</div><div> ADDR_WIDTH : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">16</span>; <span style="color: #6a9955;">--Address bus size of the Image Ram.</span></div><div> IM_SIZE_D1 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span>; <span style="color: #6a9955;">--Size along Dimension 1</span></div><div> IM_SIZE_D2 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span> <span style="color: #6a9955;">--Size along Dimension 2</span></div><div> );</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clk : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> addr_in : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">unsigned</span>(ADDR_WIDTH-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--Address bus to the Image Ram.</span></div><div> rgb_out : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic_vector</span>(<span style="color: #b5cea8;">23</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--24 bit RGB pixel output</span></div><div> );</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div> <span style="color: #569cd6;">signal</span> rgb_out : <span style="color: #4ec9b0;">std_logic_vector</span>(<span style="color: #b5cea8;">23</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> <span style="color: #569cd6;">signal</span> addr_in : <span style="color: #4ec9b0;">unsigned</span>(ADDR_WIDTH-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> <span style="color: #6a9955;">--Instantiation of Image RAM. Internally stored image.</span></div><div> image_ram : <span style="color: #569cd6;">im_ram</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span>(ADDR_WIDTH, IM_SIZE_D1 ,IM_SIZE_D2)</div><div> <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span>(Clk, addr_in, rgb_out);</div><br /><div> <span style="color: #6a9955;">--Process to convert RGB to Gray image.</span></div><div> CONVERTER_PROC : <span style="color: #569cd6;">process</span>(Clk,reset)</div><div> <span style="color: #6a9955;">--temperary variables</span></div><div> <span style="color: #569cd6;">variable</span> temp1,temp2,temp3,temp4 : <span style="color: #4ec9b0;">unsigned</span>(<span style="color: #b5cea8;">15</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #569cd6;">if</span>(reset = <span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">then</span> <span style="color: #6a9955;">--active high asynchronous reset</span></div><div> addr_in <= (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> data_valid <= <span style="color: #b5cea8;">'0'</span>;</div><div> <span style="color: #569cd6;">elsif</span> <span style="color: #dcdcaa;">rising_edge</span>(Clk) <span style="color: #569cd6;">then</span></div><div> <span style="color: #6a9955;">--output is ready when the last address in the ram has reached.</span></div><div> <span style="color: #569cd6;">if</span>(<span style="color: #dcdcaa;">to_integer</span>(addr_in) = IM_SIZE_D1*IM_SIZE_D2-<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">then</span> </div><div> addr_in <= (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> data_valid <= <span style="color: #b5cea8;">'0'</span>;</div><div> <span style="color: #569cd6;">else</span> <span style="color: #6a9955;">--otherwise keep incrementing the address value.</span></div><div> addr_in <= addr_in + <span style="color: #b5cea8;">1</span>;</div><div> data_valid <= <span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--indicates output is ready</span></div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #6a9955;">--Gray pixel = 0.3*Red pixel + 0.59*Green pixel + 0.11*Blue pixel</span></div><div> <span style="color: #6a9955;">--the 24 bit value is split into R,G and B components and multiplied</span></div><div> <span style="color: #6a9955;">--with their respective weights and then added together.</span></div><div> temp1 := <span style="color: #b5cea8;">"01001100"</span> * <span style="color: #4ec9b0;">unsigned</span>(rgb_out(<span style="color: #b5cea8;">7</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)); <span style="color: #6a9955;">--(0.3 * R) </span></div><div> temp2 := <span style="color: #b5cea8;">"10010111"</span> * <span style="color: #4ec9b0;">unsigned</span>(rgb_out(<span style="color: #b5cea8;">15</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">8</span>)); <span style="color: #6a9955;">--(0.59 * G) </span></div><div> temp3 := <span style="color: #b5cea8;">"00011100"</span> * <span style="color: #4ec9b0;">unsigned</span>(rgb_out(<span style="color: #b5cea8;">23</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">16</span>)); <span style="color: #6a9955;">--(0.11 * B)</span></div><div> temp4 := temp1 + temp2 + temp3;</div><div> <span style="color: #6a9955;">--Most significant bit of the LSB portion is added to the MSB portion. </span></div><div> <span style="color: #6a9955;">--To round off the result.</span></div><div> gray_out <= temp4(<span style="color: #b5cea8;">15</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">8</span>) + (<span style="color: #b5cea8;">"0000000"</span> & temp4(<span style="color: #b5cea8;">7</span>));</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><h3 style="text-align: left;"><br /></h3><h3 style="text-align: left;">3) tb.vhd:</h3><p> This is the testbench for testing our main designs. The output image is received from the main design and compared with the actual result. Previously using Matlab, we have saved the actual result in a file called <i>gray.txt</i> .</p><p><span><br /></span></p><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--Testbench</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #6a9955;">--the below libraries are needed for reading text file.</span></div><div><span style="color: #569cd6;">library</span> std;</div><div><span style="color: #569cd6;">use</span> std.textio.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #6a9955;">--Testbench entity is always empty.</span></div><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">tb</span> <span style="color: #569cd6;">is</span></div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">tb</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">sim</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">tb</span> <span style="color: #569cd6;">is</span></div><br /><div> <span style="color: #6a9955;">--the generic parameters are declared and initialized as constants here.</span></div><div> <span style="color: #569cd6;">constant</span> IM_SIZE_D1: <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span>;</div><div> <span style="color: #569cd6;">constant</span> IM_SIZE_D2: <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span>; <span style="color: #6a9955;">--we have a 64*64 image.</span></div><div> <span style="color: #569cd6;">constant</span> ADDR_WIDTH: <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">12</span>; <span style="color: #6a9955;">--64*64=4096 which needs 12 bits.</span></div><div> </div><div> <span style="color: #6a9955;">--this array is used to store the output gray pixels.</span></div><div> <span style="color: #569cd6;">type</span> <span style="color: #4ec9b0;">image_type</span> <span style="color: #569cd6;">is</span> <span style="color: #569cd6;">array</span> (<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">to</span> IM_SIZE_D1, <span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">to</span> IM_SIZE_D2) <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">integer</span>;</div><div> <span style="color: #569cd6;">signal</span> image_pixels : image_type := (<span style="color: #569cd6;">others</span> => (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">0</span>));</div><br /><div> <span style="color: #6a9955;">--component declaration.</span></div><div> <span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">rgb2gray</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(</div><div> ADDR_WIDTH : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">16</span>;</div><div> IM_SIZE_D1 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span>;</div><div> IM_SIZE_D2 : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">64</span></div><div> );</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clk : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> reset : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> data_valid : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> gray_out : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">unsigned</span>(<span style="color: #b5cea8;">7</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div> <span style="color: #6a9955;">--temperory signal declarations.</span></div><div> <span style="color: #569cd6;">signal</span> Clk,reset,data_valid : <span style="color: #4ec9b0;">std_logic</span> := <span style="color: #b5cea8;">'0'</span>;</div><div> <span style="color: #569cd6;">signal</span> gray_out : <span style="color: #4ec9b0;">unsigned</span>(<span style="color: #b5cea8;">7</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> <span style="color: #569cd6;">constant</span> Clk_period : <span style="color: #4ec9b0;">time</span> := <span style="color: #b5cea8;">10</span> <span style="color: #4ec9b0;">ns</span>; <span style="color: #6a9955;">--clock period.</span></div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> <span style="color: #6a9955;">--generate the clock signal.</span></div><div> Clk <= <span style="color: #569cd6;">not</span> Clk <span style="color: #569cd6;">after</span> Clk_period/<span style="color: #b5cea8;">2</span>;</div><br /><div> <span style="color: #6a9955;">--Instantiate the Unit under test.</span></div><div> UUT : <span style="color: #569cd6;">rgb2gray</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span>(ADDR_WIDTH, IM_SIZE_D1, IM_SIZE_D2)</div><div> <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span>(Clk, reset, data_valid, gray_out);</div><br /><br /><div> <span style="color: #6a9955;">--Process where we apply inputs, read outputs and verify the result. </span></div><div> STIMULUS_PROC : <span style="color: #569cd6;">process</span></div><div> <span style="color: #569cd6;">variable</span> i,j : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">1</span>; <span style="color: #6a9955;">--loop indices.</span></div><div> <span style="color: #569cd6;">variable</span> line_var : <span style="color: #4ec9b0;">line</span>;</div><div> <span style="color: #569cd6;">file</span> text_var : <span style="color: #4ec9b0;">text</span>;</div><div> <span style="color: #569cd6;">variable</span> pixel : <span style="color: #4ec9b0;">integer</span>;</div><div> <span style="color: #569cd6;">variable</span> <span style="color: #4ec9b0;">error</span> : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>; <span style="color: #6a9955;">--this value should be zero at the end of simulation.</span></div><div> <span style="color: #569cd6;">variable</span> diff : image_type := (<span style="color: #569cd6;">others</span> => (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">0</span>)); </div><div> <span style="color: #569cd6;">begin</span></div><div> reset <= <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> Clk_period;</div><div> reset <= <span style="color: #b5cea8;">'0'</span>; <span style="color: #6a9955;">--reset is applied for one clock cycle.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> data_valid = <span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--wait for valid data at the output port.</span></div><div> <span style="color: #569cd6;">while</span>(data_valid = <span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">loop</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> (<span style="color: #dcdcaa;">falling_edge</span>(Clk)); <span style="color: #6a9955;">--sample outputs at the falling edge of clock</span></div><div> image_pixels(i,j) <= <span style="color: #dcdcaa;">to_integer</span>(gray_out); <span style="color: #6a9955;">--save pixel as integer.</span></div><div> <span style="color: #6a9955;">--generate indices to save the pixels in the correct place.</span></div><div> <span style="color: #569cd6;">if</span>(j = IM_SIZE_D2) <span style="color: #569cd6;">then</span></div><div> j := <span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">if</span>(i = IM_SIZE_D1) <span style="color: #569cd6;">then</span></div><div> i := <span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">else</span></div><div> i := i+<span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">else</span></div><div> j := j+<span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>; </div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> (<span style="color: #dcdcaa;">rising_edge</span>(Clk)); <span style="color: #6a9955;">--pause until rising edge of the clock </span></div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> <span style="color: #6a9955;">--all output gray pixels are read. Activate reset again.</span></div><div> reset <= <span style="color: #b5cea8;">'1'</span>;</div><br /><div> <span style="color: #6a9955;">--Now check if the results are the same as in Matlab</span></div><div> <span style="color: #6a9955;">--Open the file in read mode. gray.txt contains pixels calculated using Matlab.</span></div><div> file_open(text_var,<span style="color: #ce9178;">"gray.txt"</span>,read_mode); </div><div> <span style="color: #569cd6;">while</span>(<span style="color: #569cd6;">NOT</span> <span style="color: #dcdcaa;">ENDFILE</span>(text_var)) <span style="color: #569cd6;">loop</span> <span style="color: #6a9955;">--until end of file is reached</span></div><div> <span style="color: #569cd6;">for</span> j <span style="color: #569cd6;">in</span> <span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">to</span> IM_SIZE_D2 <span style="color: #569cd6;">loop</span></div><div> <span style="color: #569cd6;">for</span> i <span style="color: #569cd6;">in</span> <span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">to</span> IM_SIZE_D1 <span style="color: #569cd6;">loop</span></div><div> <span style="color: #dcdcaa;">readline</span>(text_var,line_var); <span style="color: #6a9955;">--read one row. Each row contains one pixel.</span></div><div> <span style="color: #dcdcaa;">read</span>(line_var,pixel);</div><div> <span style="color: #6a9955;">--calculate the difference between actual gray pixels from Matlab</span></div><div> <span style="color: #6a9955;">--and pixel values from our rgb2gray VHDL design.</span></div><div> diff(i,j) := <span style="color: #569cd6;">abs</span>(image_pixels(i,j) - pixel);</div><div> <span style="color: #6a9955;">--If the difference is 2 or more then, we take it as an error</span></div><div> <span style="color: #6a9955;">--and increment the variable 'error' by 1.</span></div><div> <span style="color: #569cd6;">if</span>(diff(i,j) > <span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">then</span></div><div> <span style="color: #4ec9b0;">error</span> := <span style="color: #4ec9b0;">error</span>+<span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> <span style="color: #b5cea8;">1</span> <span style="color: #4ec9b0;">ns</span>; <span style="color: #6a9955;">--pause for 1 ns. </span></div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> file_close(text_var); <span style="color: #6a9955;">--close the file after reading.</span></div><br /><div> <span style="color: #569cd6;">wait</span>; <span style="color: #6a9955;">--wait eternally after finishing simulation.</span></div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>; <span style="color: #6a9955;">--End of Testbench</span></div></div><p><br /></p><p><span> Note that </span>IM_SIZE_D1 and IM_SIZE_D2 are the number of rows and columns respectively of the image matrix. You can read it as a short form for "Image size along dimension 1 (or) 2".</p><p><span> <span> In the testbench we have a variable matrix called "diff". This matrix contains the absolute value of difference between gray image pixels, from Matlab and VHDL. A difference of "1" is considered okay, as binary multiplications can cause some loss of least significant bits. But more than a difference of "1" is considered as an error. We can see that at the end of simulation, we didn't get any "error". </span></span></p><p><span><span><span> There is no address output from the top level entity rgb2gray. In our design, I thought it was unnecessary, because the pixel outputs were coming out in a sequential manner. So the testbench "knew" where to place each output pixel.</span></span></span></p><p><span><span><span><span> I hope this post was helpful for you and gave you some ideas on how to do image processing using VHDL.</span></span></span></span></p><p><span><span><span><span> You can Download the VHDL codes, images etc from <a href="https://drive.google.com/file/d/1bfN6kvQ3PahtgOKhv7-zcy20rz7GxJ28/view?usp=sharing" target="_blank">Here</a>.</span><br /></span></span></span></p><p><span><span><span><span><br /></span></span></span></span></p>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com4tag:blogger.com,1999:blog-2050962176404305705.post-38778733134471916592020-12-08T22:57:00.003+05:302020-12-08T22:57:35.453+05:30Synthesizable Clocked Square Root Calculator In VHDL<p><span> </span>Long back I had shared <a href="https://vhdlguru.blogspot.com/2010/03/vhdl-function-for-finding-square-root.html" target="_blank">VHDL function for finding the square root of a number</a>. This function too was synthesisable, but as it was a function, it was purely combinatorial. If you want to find the square root of a relatively larger number, then the resource usage was very high.</p><p><span> In such cases, it makes sense to use a clocked design. Such a clocked design enables us to reuse one set of resources over and over. The advantage of such a design is that it uses far less resources while the disadvantage being the low speed. </span><br /></p><p><span><span> For example, in the design I have shared in this post, to find the square root of a N-bit number, you need to wait N/2 clock cycles. </span><br /></span></p><p><span> The code is written based on Figure (8) from this paper: </span><a href="https://drive.google.com/file/d/1bhOr-KdjjQjOQ13TKr4b6WiEq42trPQj/view" target="_blank">A New Non-Restoring Square Root Algorithm and Its VLSI Implementations</a>.</p><p><span> </span>The codes are well commented, so I wont write much about how it works here. Please refer to the block diagram from the paper in case you have some doubts.</p><p>Let me share the codes now:</p><h3 style="text-align: left;">square_root.vhd:</h3><div><br /></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--Synthesisable Design for Finding Square root of a number.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">square_root</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clk : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>; <span style="color: #6a9955;">--Clock</span></div><div> rst : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>; <span style="color: #6a9955;">--Asynchronous active high reset.</span></div><div> <span style="color: #4ec9b0;">input</span> : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">unsigned</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--this is the number for which we want to find square root.</span></div><div> done : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic</span>; <span style="color: #6a9955;">--This signal goes high when output is ready</span></div><div> sq_root : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">unsigned</span>(N/<span style="color: #b5cea8;">2</span>-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--square root of 'input'</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">square_root</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">Behav</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">square_root</span> <span style="color: #569cd6;">is</span></div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> SQROOT_PROC : <span style="color: #569cd6;">process</span>(Clk,rst)</div><div> <span style="color: #569cd6;">variable</span> a : <span style="color: #4ec9b0;">unsigned</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--original input.</span></div><div> <span style="color: #569cd6;">variable</span> left,right,r : <span style="color: #4ec9b0;">unsigned</span>(N/<span style="color: #b5cea8;">2</span>+<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>):=(<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>); <span style="color: #6a9955;">--input to adder/sub.r-remainder.</span></div><div> <span style="color: #569cd6;">variable</span> q : <span style="color: #4ec9b0;">unsigned</span>(N/<span style="color: #b5cea8;">2</span>-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>); <span style="color: #6a9955;">--result.</span></div><div> <span style="color: #569cd6;">variable</span> i : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>; <span style="color: #6a9955;">--index of the loop. </span></div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #569cd6;">if</span>(rst = <span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">then</span> <span style="color: #6a9955;">--reset the variables.</span></div><div> done <= <span style="color: #b5cea8;">'0'</span>;</div><div> sq_root <= (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> i := <span style="color: #b5cea8;">0</span>;</div><div> a := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> left := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> right := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> r := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> q := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">elsif</span>(<span style="color: #dcdcaa;">rising_edge</span>(Clk)) <span style="color: #569cd6;">then</span></div><div> <span style="color: #6a9955;">--Before we start the first clock cycle get the 'input' to the variable 'a'.</span></div><div> <span style="color: #569cd6;">if</span>(i = <span style="color: #b5cea8;">0</span>) <span style="color: #569cd6;">then</span> </div><div> a := <span style="color: #4ec9b0;">input</span>;</div><div> done <= <span style="color: #b5cea8;">'0'</span>; <span style="color: #6a9955;">--reset 'done' signal.</span></div><div> i := i+<span style="color: #b5cea8;">1</span>; <span style="color: #6a9955;">--increment the loop index.</span></div><div> <span style="color: #569cd6;">elsif</span>(i < N/<span style="color: #b5cea8;">2</span>) <span style="color: #569cd6;">then</span> <span style="color: #6a9955;">--keep incrementing the loop index.</span></div><div> i := i+<span style="color: #b5cea8;">1</span>; </div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #6a9955;">--These statements below are derived from the block diagram.</span></div><div> right := q & r(N/<span style="color: #b5cea8;">2</span>+<span style="color: #b5cea8;">1</span>) & <span style="color: #b5cea8;">'1'</span>;</div><div> left := r(N/<span style="color: #b5cea8;">2</span>-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) & a(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> N-<span style="color: #b5cea8;">2</span>);</div><div> a := a(N-<span style="color: #b5cea8;">3</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) & <span style="color: #b5cea8;">"00"</span>; <span style="color: #6a9955;">--shifting left by 2 bit.</span></div><div> <span style="color: #569cd6;">if</span> ( r(N/<span style="color: #b5cea8;">2</span>+<span style="color: #b5cea8;">1</span>) = <span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">then</span> <span style="color: #6a9955;">--add or subtract as per this bit.</span></div><div> r := left + right;</div><div> <span style="color: #569cd6;">else</span></div><div> r := left - right;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> q := q(N/<span style="color: #b5cea8;">2</span>-<span style="color: #b5cea8;">2</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) & (<span style="color: #569cd6;">not</span> r(N/<span style="color: #b5cea8;">2</span>+<span style="color: #b5cea8;">1</span>));</div><div> <span style="color: #569cd6;">if</span>(i = N/<span style="color: #b5cea8;">2</span>) <span style="color: #569cd6;">then</span> <span style="color: #6a9955;">--This means the max value of loop index has reached. </span></div><div> done <= <span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--make 'done' high because output is ready.</span></div><div> i := <span style="color: #b5cea8;">0</span>; <span style="color: #6a9955;">--reset loop index for beginning the next cycle.</span></div><div> sq_root <= q; <span style="color: #6a9955;">--assign 'q' to the output port.</span></div><div> <span style="color: #6a9955;">--reset other signals for using in the next cycle.</span></div><div> left := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> right := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> r := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> q := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>; </div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><h3 style="text-align: left;"><br /></h3><h3 style="text-align: left;">Testbench: tb.vhd</h3><div><br /></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--Testbench for out square root calculator design.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.math_real.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #6a9955;">--empty entity as its a testbench</span></div><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">tb</span> <span style="color: #569cd6;">is</span></div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">tb</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">sim</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">tb</span> <span style="color: #569cd6;">is</span></div><br /><div> <span style="color: #6a9955;">--Declare the component which we want to test.</span></div><div> <span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">square_root</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clk : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> rst : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> <span style="color: #4ec9b0;">input</span> : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">unsigned</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> done : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> sq_root : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">unsigned</span>(N/<span style="color: #b5cea8;">2</span>-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div> <span style="color: #569cd6;">constant</span> clk_period : <span style="color: #4ec9b0;">time</span> := <span style="color: #b5cea8;">10</span> <span style="color: #4ec9b0;">ns</span>; <span style="color: #6a9955;">--set the clock period for simulation.</span></div><div> <span style="color: #569cd6;">constant</span> N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">16</span>; <span style="color: #6a9955;">--width of the input.</span></div><div> <span style="color: #569cd6;">signal</span> Clk,rst,done : <span style="color: #4ec9b0;">std_logic</span> := <span style="color: #b5cea8;">'0'</span>;</div><div> <span style="color: #569cd6;">signal</span> <span style="color: #4ec9b0;">input</span> : <span style="color: #4ec9b0;">unsigned</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">signal</span> sq_root : <span style="color: #4ec9b0;">unsigned</span>(N/<span style="color: #b5cea8;">2</span>-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">signal</span> <span style="color: #4ec9b0;">error</span> : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>; <span style="color: #6a9955;">--this indicates the number of errors encountered during simulation.</span></div><div> </div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> Clk <= <span style="color: #569cd6;">not</span> Clk <span style="color: #569cd6;">after</span> clk_period / <span style="color: #b5cea8;">2</span>; <span style="color: #6a9955;">--generate clock by toggling 'Clk'.</span></div><br /><div> <span style="color: #6a9955;">--entity instantiation.</span></div><div> DUT : <span style="color: #569cd6;">entity</span> <span style="color: #569cd6;">work</span>.<span style="color: #569cd6;">square_root</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span>(N => N)</div><div> <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span>(Clk,rst,<span style="color: #4ec9b0;">input</span>,done,sq_root);</div><br /><div> <span style="color: #6a9955;">--Apply the inputs to the design and check if the results are correct. </span></div><div> <span style="color: #6a9955;">--The number of inputs for which the results were wrongly calculated are counted by 'error'. </span></div><div> SEQUENCER_PROC : <span style="color: #569cd6;">process</span></div><div> <span style="color: #569cd6;">variable</span> actual_result,i : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>;</div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #6a9955;">--First we apply reset input for one clock period.</span></div><div> rst <= <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> rst <= <span style="color: #b5cea8;">'0'</span>;</div><div> <span style="color: #6a9955;">--Test the design for all the combination of inputs.</span></div><div> <span style="color: #6a9955;">--Since we have (2^16)-1 inputs, we test all of them one by one. </span></div><div> <span style="color: #569cd6;">while</span>(i <= <span style="color: #b5cea8;">2</span>**N-<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">loop</span></div><div> <span style="color: #4ec9b0;">input</span> <= <span style="color: #dcdcaa;">to_unsigned</span>(i,N); <span style="color: #6a9955;">--convert 'i' from integer to unsigned format.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> done=<span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--wait until the 'done' output signal goes high.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> <span style="color: #dcdcaa;">falling_edge</span>(Clk); <span style="color: #6a9955;">--we sample the output at the falling edge of the clock.</span></div><div> actual_result := <span style="color: #4ec9b0;">integer</span>(<span style="color: #dcdcaa;">floor</span>(<span style="color: #dcdcaa;">sqrt</span>(<span style="color: #4ec9b0;">real</span>(i)))); <span style="color: #6a9955;">--Calculate the actual result.</span></div><div> <span style="color: #6a9955;">--if actual result and calculated result are different increment 'error' by 1.</span></div><div> <span style="color: #569cd6;">if</span> (actual_result /= <span style="color: #dcdcaa;">to_integer</span>(sq_root)) <span style="color: #569cd6;">then</span> </div><div> <span style="color: #4ec9b0;">error</span> <= <span style="color: #4ec9b0;">error</span> + <span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>; </div><div> i := i+<span style="color: #b5cea8;">1</span>; <span style="color: #6a9955;">--increment the loop index.</span></div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>;</div><div> reset <= <span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--all inputs are tested. Apply reset</span></div><div> <span style="color: #4ec9b0;">input</span> <= (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>); <span style="color: #6a9955;">--reset the 'input'</span></div><div> <span style="color: #569cd6;">wait</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><p><br /></p><h3 style="text-align: left;">Simulation Waveform from ModelSim:</h3><p><span></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB3Py48ZaqM9zUARyoPXn4dH0Kgm0cWAZhRfiP_9EceD04dHTm6KIGFXoFwucHmVP6mm1Gwu31N9AhjaSFIEmg_TDQT-ZJKG24ufxD4X918bA0OstX_ZFUxudIH4TexjD8Suu1mipfXebx/s1475/i1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="579" data-original-width="1475" height="252" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB3Py48ZaqM9zUARyoPXn4dH0Kgm0cWAZhRfiP_9EceD04dHTm6KIGFXoFwucHmVP6mm1Gwu31N9AhjaSFIEmg_TDQT-ZJKG24ufxD4X918bA0OstX_ZFUxudIH4TexjD8Suu1mipfXebx/w640-h252/i1.PNG" width="640" /></a></div><br /><br /><p></p><p><span> <span> T</span></span>o reach the end of the testbench, you need to simulate only for 5.5 msec of simulation time.</p><p><br /></p>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0tag:blogger.com,1999:blog-2050962176404305705.post-27070595734166962262020-12-07T19:22:00.000+05:302020-12-07T19:22:44.961+05:30Synthesizable Polynomial Equation Calculator in VHDL<p> <span> A polynomial equation is an equation which is formed with variables, coefficients and exponents. They can be written in the following format:</span></p><p><em style="background-color: white; color: #172b4d; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; text-align: center;"><span style="color: black;"><span style="font-size: 14px;"> </span><span style="font-size: 14px;"> </span><span style="font-size: 14px;"> </span><span style="font-size: 14px;"> </span><span style="font-size: 14px;"> </span> y =</span><span style="color: black;">a</span><sub><span style="color: black;">n</span></sub><span style="color: black;">x</span><sup><span style="color: black;">n</span></sup><span style="color: black;"> + </span><span style="color: black;">a</span><sub><span style="color: black;">n-1</span></sub><span style="color: black;">x</span><sup><span style="color: black;">n-1</span></sup><span style="color: black;"> + ... +</span><span style="color: black;">a</span><sub><span style="color: black;">1</span></sub><span style="color: black;">x + </span><span style="color: black;">a</span><sub><span style="color: black;">0</span></sub><span style="color: black;"> .</span></em></p><p><span>Here </span><em style="background-color: white; color: #172b4d; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; text-align: center;"><span style="color: black;">a</span><sub><span style="color: black;">n, </span></sub><span style="color: black;">a</span><sub><span style="color: black;">n-1 </span></sub><span style="color: black;">... , </span><span style="color: black;">a</span><sub><span style="color: black;">1 ,</span></sub><span style="color: black;">a</span><sub><span style="color: black;">0 </span></sub></em>are coefficients,</p><p>n, n-1, ... etc are exponents and x is the variable. </p><p>In this post, I want to share, VHDL code for computing the value of 'y' if you know the value of 'x' and coefficients forming the polynomial. </p><p>Directly calculating the polynomial with the above equation is very inefficient. So, first we reformat the equation as per Horner's rule:</p><p> <em style="background-color: white; color: #172b4d; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; text-align: center;"><span style="color: black;">y =(((</span><span style="color: black;">a</span><sub><span style="color: black;">n</span></sub><span style="color: black;">x + </span><span style="color: black;">a</span><sub><span style="color: black;">n-1</span></sub><span style="color: black;">)</span><span style="color: black;">x</span><span style="color: black;"> + </span><span style="color: black;">a</span><sub><span style="color: black;">n-2</span></sub><span style="color: black;">)</span><span style="color: black;">x</span><span style="color: black;"> + </span><span style="color: black;">a</span><sub><span style="color: black;">n-3</span></sub><span style="color: black;">)</span><span style="color: black;">x</span><span style="color: black;"> + ... )</span><span style="color: black;">x</span><span style="color: black;"> +</span><span style="color: black;">a</span><sub><span style="color: black;">0</span></sub><span style="color: black;"> </span><span style="color: black;">.</span></em></p><p>This modified equation can be represented by the following block diagram:</p><p><br /></p><p></p><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV4avnwLeChibuDsICBE8DDWDT5OYo-zHC9O-2tS6ewv3cVJRcODUmYA2_zbjN9H67xbBrZepo76dK8w_H6D8cConmq4mQTwP2ssXdSbqyaM2MCHaSxwZEN9cTqrZk0nn24VAG4zBEHtYJ/" style="margin-left: 1em; margin-right: 1em;"><img data-original-height="584" data-original-width="1123" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV4avnwLeChibuDsICBE8DDWDT5OYo-zHC9O-2tS6ewv3cVJRcODUmYA2_zbjN9H67xbBrZepo76dK8w_H6D8cConmq4mQTwP2ssXdSbqyaM2MCHaSxwZEN9cTqrZk0nn24VAG4zBEHtYJ/w400-h208/image.png" width="400" /></a></div><br /><br /></div><br /></div><p></p><p>There are 3 sub-entities in the above block diagram.</p><p><b><u>1) Block number 1:</u></b></p><p><span> </span>This block keep generating successive powers of input x. </p><p>In the first clock cycle it generates x^0 = 1.</p><p>In the 2ndclock cycle it generates x^1.</p><p>In the 3rd clock cycle it generates x^2.</p><p>In the nth clock cycle it generates x^(n-1).</p><p><span> Every clock cycle, the previous outputs are multiplied with 'x' to get the next power of x.</span><br /></p><p><span><b><u>2)Block number 2:</u></b></span></p><p><span><span> </span>The powers of x generated in the first block are multiplied with the corresponding coefficients of the polynomial in Block numbered 2. The </span>coefficients are declared and initialized in the top level entity and can be changed easily.</p><p><b><u>3)Block number 4:</u></b></p><p><span><span> This block is basically an accumulative adder which accumulates all the products generated by the multiplier. Once it does 'n' additions, we have the final result available in output 'y'. An output 'done' is set as high to indicate that the output is ready.</span><br /></span></p><p><span><span><br /></span></span></p><p><span><span>Let me share the VHDL codes for these entities along with the testbench.</span></span></p><h3 style="text-align: left;"><span><span><b>1) power_module.vhd</b></span></span></h3><div><span><span><b><br /></b></span></span></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--This module generates successive powers of input x in each clock cycle.</span></div><div><span style="color: #6a9955;">--For example 1,x,x^2,x^3,x^4 etc. </span></div><div><span style="color: #6a9955;">--The output from the previous cycle is multiplied again by x to get the next power.</span></div><div><span style="color: #6a9955;">--The output from this entity is passed to the multiplier module, where it gets</span></div><div><span style="color: #6a9955;">--multiplied by the corresponding coefficient.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">power_module</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clock : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> reset : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> x : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> x_powered : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">power_module</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">Behav</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">power_module</span> <span style="color: #569cd6;">is</span></div><br /><div> <span style="color: #569cd6;">signal</span> x_pow : <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := <span style="color: #dcdcaa;">to_signed</span>(<span style="color: #b5cea8;">1</span>,N);</div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> POWER_PROC : <span style="color: #569cd6;">process</span>(Clock,reset)</div><div> <span style="color: #569cd6;">variable</span> temp_prod : <span style="color: #4ec9b0;">signed</span>(<span style="color: #b5cea8;">2</span>*N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #569cd6;">if</span> (reset=<span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">then</span></div><div> x_pow <= <span style="color: #dcdcaa;">to_signed</span>(<span style="color: #b5cea8;">1</span>,N);</div><div> <span style="color: #569cd6;">elsif</span>(<span style="color: #dcdcaa;">rising_edge</span>(Clock)) <span style="color: #569cd6;">then</span></div><div> temp_prod := x*x_pow;</div><div> <span style="color: #6a9955;">--The MSB half of the result is ignored. We assume that all intermediate powers of x</span></div><div> <span style="color: #6a9955;">--can be represented by a max of N bits.</span></div><div> x_pow <= temp_prod(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div> x_powered <= x_pow;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><h3 style="text-align: left;"><span><span><b><br /></b></span></span></h3><h3 style="text-align: left;"><span><span><b>2) multiplier.vhd</b></span></span></h3><div><span><span><b><br /></b></span></span></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--The successive powers of x are multiplied by the corresponding coeffcients here. </span></div><div><span style="color: #6a9955;">--The results are sent to the adder module which acts as an "accumulator".</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">multiplier</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clock : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> x,y : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> prod : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">multiplier</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">Behav</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">multiplier</span> <span style="color: #569cd6;">is</span></div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> MULT_PROC : <span style="color: #569cd6;">process</span>(Clock)</div><div> <span style="color: #569cd6;">variable</span> temp_prod : <span style="color: #4ec9b0;">signed</span>(<span style="color: #b5cea8;">2</span>*N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #569cd6;">if</span>(<span style="color: #dcdcaa;">rising_edge</span>(Clock)) <span style="color: #569cd6;">then</span></div><div> temp_prod := x*y;</div><div> <span style="color: #6a9955;">--The MSB half of the result is ignored. We assume that all intermediate numbers</span></div><div> <span style="color: #6a9955;">--be represented by a max of N bits.</span></div><div> prod <= temp_prod(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>; </div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><div> </div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><h3 style="text-align: left;"><span><span><b><br /></b></span></span></h3><h3 style="text-align: left;"><span><span><b>3) adder.vhd</b></span></span></h3><div><span><span><b><br /></b></span></span></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--The output from multiplier module is received by this module and accumulated to</span></div><div><span style="color: #6a9955;">--form the output. If the polynomial equation has a degree of 'n' then 'n' additions has to take place</span></div><div><span style="color: #6a9955;">--to get the final result.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">adder</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clock : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> reset : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> x : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> sum : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">adder</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">Behav</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">adder</span> <span style="color: #569cd6;">is</span></div><br /><div> <span style="color: #569cd6;">signal</span> temp_sum : <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> ADDER_PROC : <span style="color: #569cd6;">process</span>(Clock,reset)</div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #569cd6;">if</span> (reset = <span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">then</span></div><div> temp_sum <= (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">elsif</span>(<span style="color: #dcdcaa;">rising_edge</span>(Clock)) <span style="color: #569cd6;">then</span></div><div> temp_sum <= temp_sum + x;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div> sum <= temp_sum;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><h3 style="text-align: left;"><span><span><b><br /></b></span></span></h3><h3 style="text-align: left;"><span><span><b>4) Top Level Entity : poly_eq_calc.vhd</b></span></span></h3><div><span><span><b><br /></b></span></span></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--This is the top level entity for the polynomial equation calculator.</span></div><div><span style="color: #6a9955;">--The power_module, multiplier and adders entities are instantiated inside this top level block.</span></div><div><span style="color: #6a9955;">--When the output signal done is High, the output available at the 'y' output is the result we want.</span></div><div><span style="color: #6a9955;">--Ignore all other values of 'y' when done is Low.</span></div><div><span style="color: #6a9955;">--We have assumed that all the intermediate numbers calculated to reach the final result, can be represented</span></div><div><span style="color: #6a9955;">--by a maximum of N bits. This simplifies the design very much.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">poly_eq_calc</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clock : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> reset : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> x : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> y : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> done : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">poly_eq_calc</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">Behav</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">poly_eq_calc</span> <span style="color: #569cd6;">is</span></div><br /><div> <span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">power_module</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clock : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> reset : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> x : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> x_powered : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div> <span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">multiplier</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clock : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> x,y : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> prod : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div> <span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">adder</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>);</div><div> <span style="color: #569cd6;">port</span> (</div><div> Clock : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> reset : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic</span>;</div><div> x : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><div> sum : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>)</div><div> );</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div> <span style="color: #569cd6;">signal</span> x_powered,coeff,product_from_mult : <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">constant</span> NUM_COEFFS : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">4</span>; <span style="color: #6a9955;">--Change here to change the degree of the polynomial. </span></div><div> <span style="color: #569cd6;">type</span> <span style="color: #4ec9b0;">arr_type</span> <span style="color: #569cd6;">is</span> <span style="color: #569cd6;">array</span> (<span style="color: #b5cea8;">0</span> <span style="color: #569cd6;">to</span> NUM_COEFFS-<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); </div><div> <span style="color: #6a9955;">--Eq : 3*x^3 - 2*x^2 - 4*x + 5;</span></div><div> <span style="color: #6a9955;">--The coefficients belonging to higher powers of x are stored in higher addresses in Coeffs array.</span></div><div> <span style="color: #569cd6;">signal</span> Coeffs : arr_type := (<span style="color: #dcdcaa;">to_signed</span>(<span style="color: #b5cea8;">5</span>,N), <span style="color: #6a9955;">--change coefficients here. </span></div><div> <span style="color: #dcdcaa;">to_signed</span>(-<span style="color: #b5cea8;">4</span>,N),</div><div> <span style="color: #dcdcaa;">to_signed</span>(-<span style="color: #b5cea8;">2</span>,N),</div><div> <span style="color: #dcdcaa;">to_signed</span>(<span style="color: #b5cea8;">3</span>,N));</div><div> </div><div> <span style="color: #569cd6;">signal</span> reset_adder : <span style="color: #4ec9b0;">std_logic</span> := <span style="color: #b5cea8;">'0'</span>;</div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> <span style="color: #6a9955;">--Instantiate the 3 sub-entities. </span></div><div> calc_power_of_x : <span style="color: #569cd6;">power_module</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span>(N => N)</div><div> <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span>(Clock,reset,x,x_powered);</div><br /><div> multiply : <span style="color: #569cd6;">multiplier</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span>(N => N)</div><div> <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span>(Clock,x_powered,coeff,product_from_mult);</div><br /><div> add : <span style="color: #569cd6;">adder</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span>(N => N)</div><div> <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span>(Clock,reset_adder,product_from_mult,y);</div><br /><div> <span style="color: #6a9955;">--The process controlling the 3 sub-entities and also supplying them with coefficients. </span></div><div> MAIN_PROC : <span style="color: #569cd6;">process</span>(Clock,reset)</div><div> <span style="color: #569cd6;">variable</span> coeff_index : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>; </div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #569cd6;">if</span>(reset = <span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">then</span></div><div> done <= <span style="color: #b5cea8;">'0'</span>;</div><div> coeff_index := <span style="color: #b5cea8;">0</span>;</div><div> coeff <= Coeffs(<span style="color: #b5cea8;">0</span>);</div><div> reset_adder <= <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">elsif</span>(<span style="color: #dcdcaa;">rising_edge</span>(Clock)) <span style="color: #569cd6;">then</span></div><div> reset_adder <= <span style="color: #b5cea8;">'0'</span>; <span style="color: #6a9955;">--the disabling of 'reset of adder' gets noticed by adder entity in the next clock cycle.</span></div><div> <span style="color: #569cd6;">if</span>(coeff_index < NUM_COEFFS-<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">then</span></div><div> coeff_index := coeff_index+<span style="color: #b5cea8;">1</span>;</div><div> coeff <= Coeffs(coeff_index); <span style="color: #6a9955;">--send the coefficients one by one to the multiplier entity. </span></div><div> <span style="color: #569cd6;">elsif</span>(coeff_index = NUM_COEFFS-<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">then</span></div><div> coeff_index := coeff_index+<span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">else</span></div><div> done <= <span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--The final result is available in 'y' now.</span></div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>; </div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><h3 style="text-align: left;"><span><span><b><br /></b></span></span></h3><h3 style="text-align: left;"><span><span><b>5) Testbench : tb_poly_eq_calc.vhd</b></span></span></h3><div><span><span><b><br /></b></span></span></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #6a9955;">--Testbench code.</span></div><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><br /><div><span style="color: #6a9955;">--The testbench entity is always empty.</span></div><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">tb_poly_eq_calc</span> <span style="color: #569cd6;">is</span></div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">tb_poly_eq_calc</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">sim</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">tb_poly_eq_calc</span> <span style="color: #569cd6;">is</span></div><br /><div> <span style="color: #569cd6;">constant</span> clk_period : <span style="color: #4ec9b0;">time</span> := <span style="color: #b5cea8;">10</span> <span style="color: #4ec9b0;">ns</span>; <span style="color: #6a9955;">--set the clock period here.</span></div><br /><div> <span style="color: #569cd6;">signal</span> Clk : <span style="color: #4ec9b0;">std_logic</span> := <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">signal</span> reset : <span style="color: #4ec9b0;">std_logic</span> := <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">constant</span> N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">32</span>; <span style="color: #6a9955;">--change the width of input and output here.</span></div><div> <span style="color: #569cd6;">signal</span> x,y : <span style="color: #4ec9b0;">signed</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div> <span style="color: #569cd6;">signal</span> done : <span style="color: #4ec9b0;">std_logic</span> := <span style="color: #b5cea8;">'0'</span>;</div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div> Clk <= <span style="color: #569cd6;">not</span> Clk <span style="color: #569cd6;">after</span> clk_period / <span style="color: #b5cea8;">2</span>; <span style="color: #6a9955;">--generate clock</span></div><br /><div> <span style="color: #6a9955;">--Instantiating Design Under Test.</span></div><div> DUT : <span style="color: #569cd6;">entity</span> <span style="color: #569cd6;">work</span>.<span style="color: #569cd6;">poly_eq_calc</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span>(N => N)</div><div> <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span> (Clk, reset, x, y, done);</div><br /><div> <span style="color: #6a9955;">--Apply inputs here. Only 'x' can be changed here. </span></div><div> <span style="color: #6a9955;">--To change coefficients and degree of polynomial edit the top level entity directly.</span></div><div> SEQUENCER_PROC : <span style="color: #569cd6;">process</span></div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #6a9955;">--first input.</span></div><div> reset <= <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period * <span style="color: #b5cea8;">2.5</span>;</div><div> reset <= <span style="color: #b5cea8;">'0'</span>;</div><div> x <= <span style="color: #dcdcaa;">to_signed</span>(<span style="color: #b5cea8;">4</span>, N);</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> done=<span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--wait for result to be out.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><br /><div> <span style="color: #6a9955;">--second input.</span></div><div> reset <= <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> reset <= <span style="color: #b5cea8;">'0'</span>;</div><div> x <= <span style="color: #dcdcaa;">to_signed</span>(<span style="color: #b5cea8;">7</span>, N);</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> done=<span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--wait for result to be out.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><br /><div> <span style="color: #6a9955;">--third input.</span></div><div> reset <= <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> reset <= <span style="color: #b5cea8;">'0'</span>;</div><div> x <= <span style="color: #dcdcaa;">to_signed</span>(<span style="color: #b5cea8;">15</span>, N);</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> done=<span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--wait for result to be out.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><br /><div> <span style="color: #6a9955;">--fourth input.</span></div><div> reset <= <span style="color: #b5cea8;">'1'</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> reset <= <span style="color: #b5cea8;">'0'</span>;</div><div> x <= <span style="color: #dcdcaa;">to_signed</span>(-<span style="color: #b5cea8;">9</span>, N);</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">until</span> done=<span style="color: #b5cea8;">'1'</span>; <span style="color: #6a9955;">--wait for result to be out.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> clk_period;</div><div> reset <= <span style="color: #b5cea8;">'1'</span>;</div><br /><div> <span style="color: #569cd6;">wait</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div> <span style="color: #6a9955;">--Check if the results are correct and report the result.</span></div><div> CHECK_RESULTS_PROC : <span style="color: #569cd6;">process</span>(Clk)</div><div> <span style="color: #569cd6;">variable</span> actual_res : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>;</div><div> <span style="color: #569cd6;">variable</span> input_num,x_int : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>;</div><div> <span style="color: #569cd6;">begin</span></div><div> <span style="color: #569cd6;">if</span>(<span style="color: #dcdcaa;">rising_edge</span>(Clk)) <span style="color: #569cd6;">then</span></div><div> <span style="color: #569cd6;">if</span>(done = <span style="color: #b5cea8;">'1'</span>) <span style="color: #569cd6;">then</span></div><div> x_int := <span style="color: #dcdcaa;">to_integer</span>(x);</div><div> <span style="color: #6a9955;">--Change this equation if you change the polynomial eq inside the top level entity.</span></div><div> actual_res := <span style="color: #b5cea8;">3</span>*x_int*x_int*x_int - <span style="color: #b5cea8;">2</span>*x_int*x_int - <span style="color: #b5cea8;">4</span>*x_int + <span style="color: #b5cea8;">5</span>; </div><div> input_num := input_num+<span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">if</span>(actual_res = <span style="color: #dcdcaa;">to_integer</span>(y)) <span style="color: #569cd6;">then</span></div><div> <span style="color: #569cd6;">report</span> <span style="color: #ce9178;">"Input number "</span> & <span style="color: #4ec9b0;">integer</span><span style="color: #569cd6;">'image</span>(input_num) & <span style="color: #ce9178;">" Worked Well"</span>;</div><div> <span style="color: #569cd6;">else</span></div><div> <span style="color: #569cd6;">report</span> <span style="color: #ce9178;">"Input number "</span> & <span style="color: #4ec9b0;">integer</span><span style="color: #569cd6;">'image</span>(input_num) & <span style="color: #ce9178;">" Has Error"</span>; </div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>; </div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>; </div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">architecture</span>;</div></div><p><br /></p><p>The code was simulated successfully using <b>Modelsim 10.4a</b> version. The simulation waveform below shows the signals for the first two set of inputs:</p><p><br /></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMvXgqCZ8FzR9G-OC4mS4OJW4_-okryWOy2bP34M2dE8M-RDgR3MXFrbniRwsN0XDS1RnOJRmlp4XvC_-nHPQfhqb669Lv7ewqKBqnDqQGjEAauZG02DPKcEVbW7gTt-KJ7cXDF2jv6LrL/s1913/image1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="831" data-original-width="1913" height="278" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMvXgqCZ8FzR9G-OC4mS4OJW4_-okryWOy2bP34M2dE8M-RDgR3MXFrbniRwsN0XDS1RnOJRmlp4XvC_-nHPQfhqb669Lv7ewqKBqnDqQGjEAauZG02DPKcEVbW7gTt-KJ7cXDF2jv6LrL/w640-h278/image1.png" width="640" /></a></div><br /><p>The design was synthesized successfully using Vivado 2020.2 version. </p><p><br /></p>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0tag:blogger.com,1999:blog-2050962176404305705.post-6488638644140364222020-12-05T13:08:00.002+05:302020-12-05T13:08:51.232+05:30Generic VHDL Code for Binary to Gray and Gray to Binary converter<p><span> </span>Few years back I had written a <a href="https://vhdlguru.blogspot.com/2017/10/4-bit-binary-to-gray-code-and-gray-code.html" target="_blank">4 bit converter for conversion between Gray and Binary codes</a>. After receiving much positive response I decided to write a generic version of the same.</p><p>Let me share the codes...</p><h3 style="text-align: left;"><b>Binary to Gray Code Converter:</b></h3><div><b><br /></b></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">LIBRARY</span> ieee;</div><div><span style="color: #569cd6;">USE</span> ieee.std_logic_1164.<span style="color: #569cd6;">ALL</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">bin2gray</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">4</span>);</div><div><span style="color: #569cd6;">port</span>( bin : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--binary input</span></div><div> G : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--gray code output</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">bin2gray</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">gate_level</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">bin2gray</span> <span style="color: #569cd6;">is</span> </div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div>G(N-<span style="color: #b5cea8;">1</span>) <= bin(N-<span style="color: #b5cea8;">1</span>);</div><div><span style="color: #6a9955;">--generate xor gates.</span></div><div><span style="color: #569cd6;">xor_gates</span> : <span style="color: #569cd6;">for</span> i <span style="color: #569cd6;">in</span> N-<span style="color: #b5cea8;">2</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span> <span style="color: #569cd6;">generate</span></div><div> G(i) <= bin(i+<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">xor</span> bin(i);</div><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">generate</span>; </div><br /><div><span style="color: #569cd6;">end</span>;</div></div><h3><b><br /></b></h3><h3><b>Gray Code to Binary Converter:</b></h3><div><b><br /></b></div><div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">LIBRARY</span> ieee;</div><div><span style="color: #569cd6;">USE</span> ieee.std_logic_1164.<span style="color: #569cd6;">ALL</span>;</div><br /><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">gray2bin</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">4</span>);</div><div><span style="color: #569cd6;">port</span>( G : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--gray code input</span></div><div> bin : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--binary output</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">gray2bin</span>;</div><br /><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">gate_level</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">gray2bin</span> <span style="color: #569cd6;">is</span> </div><br /><div><span style="color: #569cd6;">signal</span> temp : <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>);</div><br /><div><span style="color: #569cd6;">begin</span></div><br /><div>temp(N-<span style="color: #b5cea8;">1</span>) <= G(N-<span style="color: #b5cea8;">1</span>);</div><div><span style="color: #6a9955;">--generate xor gates.</span></div><div><span style="color: #569cd6;">xor_gates</span> : <span style="color: #569cd6;">for</span> i <span style="color: #569cd6;">in</span> N-<span style="color: #b5cea8;">2</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span> <span style="color: #569cd6;">generate</span></div><div> temp(i) <= temp(i+<span style="color: #b5cea8;">1</span>) <span style="color: #569cd6;">xor</span> G(i);</div><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">generate</span>; </div><br /><div>bin <= temp;</div><br /><div><span style="color: #569cd6;">end</span>;</div></div></div><div><b><br /></b></div><div><h3><b>Testbench:</b></h3></div><div><b><br /></b></div><div style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, "Courier New", monospace; font-size: 14px; line-height: 19px; white-space: pre;"><div><span style="color: #569cd6;">library</span> ieee;</div><div><span style="color: #569cd6;">use</span> ieee.std_logic_1164.<span style="color: #569cd6;">all</span>;</div><div><span style="color: #569cd6;">use</span> ieee.numeric_std.<span style="color: #569cd6;">all</span>;</div><div> </div><div><span style="color: #569cd6;">entity</span> <span style="color: #4ec9b0;">tb</span> <span style="color: #569cd6;">is</span></div><div><span style="color: #569cd6;">end</span> <span style="color: #4ec9b0;">tb</span>;</div><div> </div><div><span style="color: #569cd6;">architecture</span> <span style="color: #4ec9b0;">behavior</span> <span style="color: #569cd6;">of</span> <span style="color: #4ec9b0;">tb</span> <span style="color: #569cd6;">is</span> </div><div> </div><div> <span style="color: #6a9955;">-- component declaration for the unit under test's (uut) </span></div><div><span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">bin2gray</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">4</span>);</div><div><span style="color: #569cd6;">port</span>( bin : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--binary input</span></div><div> G : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--gray code output</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div><span style="color: #569cd6;">component</span> <span style="color: #4ec9b0;">gray2bin</span> <span style="color: #569cd6;">is</span></div><div> <span style="color: #569cd6;">generic</span>(N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">4</span>);</div><div><span style="color: #569cd6;">port</span>( G : <span style="color: #569cd6;">in</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>); <span style="color: #6a9955;">--gray code input</span></div><div> bin : <span style="color: #569cd6;">out</span> <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) <span style="color: #6a9955;">--binary output</span></div><div> );</div><div><span style="color: #569cd6;">end</span> <span style="color: #569cd6;">component</span>;</div><br /><div><span style="color: #569cd6;">constant</span> N : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">16</span>; <span style="color: #6a9955;">--Change this to control the number of bits in the input/output.</span></div><div><span style="color: #569cd6;">signal</span> bin,g,bin_out : <span style="color: #4ec9b0;">std_logic_vector</span>(N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">downto</span> <span style="color: #b5cea8;">0</span>) := (<span style="color: #569cd6;">others</span> => <span style="color: #b5cea8;">'0'</span>);</div><div><span style="color: #569cd6;">signal</span> <span style="color: #4ec9b0;">error</span> : <span style="color: #4ec9b0;">integer</span> := <span style="color: #b5cea8;">0</span>; </div><div><span style="color: #569cd6;">begin</span></div><div> <span style="color: #6a9955;">-- Both the converters are connected back to back to see the binary input going to the</span></div><div> <span style="color: #6a9955;">--first entity is the same as the output coming out of the second entity.</span></div><div> uut1: <span style="color: #569cd6;">bin2gray</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span> (N => N) <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span> (</div><div> bin => bin,</div><div> g => g</div><div> );</div><div> </div><div> uut2: <span style="color: #569cd6;">gray2bin</span> <span style="color: #569cd6;">generic</span> <span style="color: #569cd6;">map</span> (N => N) <span style="color: #569cd6;">port</span> <span style="color: #569cd6;">map</span> (</div><div> g => g,</div><div> bin => bin_out</div><div> );</div><div> </div><div> <span style="color: #6a9955;">-- stimulus process</span></div><div> <span style="color: #6a9955;">--this tests for all the input combinations.</span></div><div> stim_proc: <span style="color: #569cd6;">process</span></div><div> <span style="color: #569cd6;">begin</span> </div><div> <span style="color: #569cd6;">for</span> i <span style="color: #569cd6;">in</span> <span style="color: #b5cea8;">0</span> <span style="color: #569cd6;">to</span> <span style="color: #b5cea8;">2</span>**N-<span style="color: #b5cea8;">1</span> <span style="color: #569cd6;">loop</span> <span style="color: #6a9955;">--loop through all the available inputs </span></div><div> bin <= <span style="color: #4ec9b0;">std_logic_vector</span>(<span style="color: #dcdcaa;">to_unsigned</span>(i,N)); <span style="color: #6a9955;">--convert integer to std_logic_vector.</span></div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> <span style="color: #b5cea8;">5</span> <span style="color: #4ec9b0;">ns</span>;</div><div> <span style="color: #6a9955;">--Count the number of errors. Should be zero at the end of simulation.</span></div><div> <span style="color: #569cd6;">if</span>(bin /= bin_out) <span style="color: #569cd6;">then</span> </div><div> <span style="color: #4ec9b0;">error</span> <= <span style="color: #4ec9b0;">error</span> + <span style="color: #b5cea8;">1</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">if</span>;</div><div> <span style="color: #569cd6;">wait</span> <span style="color: #569cd6;">for</span> <span style="color: #b5cea8;">5</span> <span style="color: #4ec9b0;">ns</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">loop</span>; </div><div> <span style="color: #569cd6;">wait</span>;</div><div> <span style="color: #569cd6;">end</span> <span style="color: #569cd6;">process</span>;</div><br /><div><span style="color: #569cd6;">end</span>;</div></div><p><br /></p><p>The codes were tested using Modelsim 10.4a version. Simply change the value of the constant 'N' in the testbench to test for different sized converters.</p><p><br /></p>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0tag:blogger.com,1999:blog-2050962176404305705.post-80140564940726296242020-11-29T11:02:00.000+05:302020-11-29T11:02:11.654+05:30Writing a Gate Level VHDL design (and Testbench) from Scratch<p><span> </span>In this video I want to show you how you can take a logic circuit diagram and write the corresponding VHDL code along with its testbench. </p><p><br /></p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="463" src="https://www.youtube.com/embed/k4wku4yJ2JU" width="557" youtube-src-id="k4wku4yJ2JU"></iframe></div><br /><div class="separator" style="clear: both; text-align: center;"><br /></div><p>The VHDL codes presented in the video are given below:</p><h3 style="text-align: left;">xor_gate.vhd:</h3><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #008800; font-weight: bold;"><br /></span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">ieee</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">ieee.std_logic_1164.all</span>;
<span style="color: #008800; font-weight: bold;">entity</span> <span style="color: #bb0066; font-weight: bold;">xor_gate</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">port</span> (
A,B : <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
C : <span style="color: #008800; font-weight: bold;">out</span> <span style="color: #333399; font-weight: bold;">std_logic</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">entity</span>;
<span style="color: #008800; font-weight: bold;">architecture</span> <span style="color: #bb0066; font-weight: bold;">gate_level</span> <span style="color: #008800; font-weight: bold;">of</span> <span style="color: #bb0066; font-weight: bold;">xor_gate</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">signal</span> An,Bn,t1,t2 : <span style="color: #333399; font-weight: bold;">std_logic</span> := <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">begin</span>
An <= <span style="color: #008800; font-weight: bold;">not</span> A;
Bn <= <span style="color: #008800; font-weight: bold;">not</span> B;
t1 <= An <span style="color: #008800; font-weight: bold;">and</span> B;
t2 <= Bn <span style="color: #008800; font-weight: bold;">and</span> A;
C <= t1 <span style="color: #008800; font-weight: bold;">or</span> t2;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">architecture</span>;</pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><h3 style="text-align: left;">tb_xor.vhd:</h3><div><br /></div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">ieee</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">ieee.std_logic_1164.all</span>;
<span style="color: #008800; font-weight: bold;">entity</span> <span style="color: #bb0066; font-weight: bold;">tb_xor</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">entity</span>;
<span style="color: #008800; font-weight: bold;">architecture</span> <span style="color: #bb0066; font-weight: bold;">behav</span> <span style="color: #008800; font-weight: bold;">of</span> <span style="color: #bb0066; font-weight: bold;">tb_xor</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">component</span> <span style="color: #bb0066; font-weight: bold;">xor_gate</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">port</span> (
A,B : <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
C : <span style="color: #008800; font-weight: bold;">out</span> <span style="color: #333399; font-weight: bold;">std_logic</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">component</span>;
<span style="color: #008800; font-weight: bold;">signal</span> A,B,C : <span style="color: #333399; font-weight: bold;">std_logic</span> := <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">begin</span>
UUT : xor_gate <span style="color: #008800; font-weight: bold;">port</span> <span style="color: #008800; font-weight: bold;">map</span> (A,B,C);
stimulus : <span style="color: #008800; font-weight: bold;">process</span>
<span style="color: #008800; font-weight: bold;">begin</span>
A <= <span style="color: #0044dd;">'0'</span>;
B <= <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">100</span> ns;
A <= <span style="color: #0044dd;">'0'</span>;
B <= <span style="color: #0044dd;">'1'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">100</span> ns;
A <= <span style="color: #0044dd;">'1'</span>;
B <= <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">100</span> ns;
A <= <span style="color: #0044dd;">'1'</span>;
B <= <span style="color: #0044dd;">'1'</span>;
<span style="color: #008800; font-weight: bold;">wait</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">architecture</span>;</pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><h3 style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px; text-align: left;"><br />The Logic circuit diagram is given below:</h3><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Zq0oHLJDZbfJv59vpzeDA57D1CXKD9TaF5dxR8bvJfyWmTanL_Jf5WYOEdCKq_yrQsIYpbbVYF43VfkRX25pTh1A_j7tyRsGAoXz9VJrsmYoH78WgRXdDpuk6L3h7bvaMkAtaMaZ9rj3/s1450/image1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="818" data-original-width="1450" height="362" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Zq0oHLJDZbfJv59vpzeDA57D1CXKD9TaF5dxR8bvJfyWmTanL_Jf5WYOEdCKq_yrQsIYpbbVYF43VfkRX25pTh1A_j7tyRsGAoXz9VJrsmYoH78WgRXdDpuk6L3h7bvaMkAtaMaZ9rj3/w640-h362/image1.png" width="640" /></a></div><br /><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><h3 style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px; text-align: left;"><b>Simulation Waveform from Modelsim:</b></h3><div><b><br /></b></div><div><b><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqWfsumESIZrNM108pwLsNeq8nG2fOFmBhuOHpwPrLGVe9MA-FSHf5kUEeVnOsvHO6ONVvN7UT0zqas9Ly_PNrCnYNNL4UM4n6HnOZnMJMQz5pmgcWWKUH3CFjYoJ8tIC0IBBeqZa-euFq/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="182" data-original-width="883" height="66" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqWfsumESIZrNM108pwLsNeq8nG2fOFmBhuOHpwPrLGVe9MA-FSHf5kUEeVnOsvHO6ONVvN7UT0zqas9Ly_PNrCnYNNL4UM4n6HnOZnMJMQz5pmgcWWKUH3CFjYoJ8tIC0IBBeqZa-euFq/" width="320" /></a></div><br /><br /><br /></b></div>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0tag:blogger.com,1999:blog-2050962176404305705.post-61598215431317787482020-11-28T10:55:00.001+05:302020-11-28T10:55:44.091+05:30Synthesizable Matrix Multiplier in VHDL<p><span> </span>Long back I had posted a <a href="https://vhdlguru.blogspot.com/2010/03/matrix-multiplication-in-vhdl.html" target="_blank">simple matrix multiplier</a> which works well in simulation but couldn't be synthesized. But many people had requested for synthesizable version of this code. So here we go.</p><p><span> </span>The design takes two matrices of 3 by 3 and outputs a matrix of 3 by 3. Each element is stored as unsigned 8 bits. This is not a generic multiplier, but if you watch the video explaining the code, you might be able to extend it to a different sized multiplier. </p><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="440" src="https://www.youtube.com/embed/8bqYa9NcD2I" width="530" youtube-src-id="8bqYa9NcD2I"></iframe></div><br /><p><br /></p><p><span> Each matrix has 9 elements, each of which is 8 bits in size. So I am passing the matrix as a 72 bit 1-Dimensional array in the design. The following table shows how the 2-D elements are mapped into the 1-D array.</span><br /></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">Row<o:p></o:p></p>
</td>
<td style="border-left: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">Column<o:p></o:p></p>
</td>
<td style="border-left: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">Bit’s Position
in 1-D array<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">0<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">0<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">7:0<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">0<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">1<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">15:8<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">0<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">2<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">23:16<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">1<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">0<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">31:24<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">1<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">1<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">39:32<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">1<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">2<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">47:40<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">2<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">0<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">55:48<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">2<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">1<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">63:56<o:p></o:p></p>
</td>
</tr>
<tr>
<td style="border-top: none; border: 1pt solid windowtext; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.4pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">2<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 49.6pt;" valign="top" width="66">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">2<o:p></o:p></p>
</td>
<td style="border-bottom: 1pt solid windowtext; border-left: none; border-right: 1pt solid windowtext; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt; width: 5cm;" valign="top" width="189">
<p align="center" class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; text-align: center;">71:64<o:p></o:p></p>
</td>
</tr>
</tbody></table><br /><p>Let me share the codes now...</p><h3 style="text-align: left;"><u>matrix_mult.vhd:</u></h3><div><u><br /></u></div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #888888;">--3 by 3 matrix multiplier. Each element of the matrix is 8 bit wide. </span>
<span style="color: #888888;">--Inputs are called A and B and output is called C. </span>
<span style="color: #888888;">--Each matrix has 9 elements each of which is 8 bit wide. So the inputs is 9*8=72 bit long.</span>
<span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">IEEE</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.std_logic_1164.all</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.numeric_std.all</span>;
<span style="color: #008800; font-weight: bold;">entity</span> <span style="color: #bb0066; font-weight: bold;">matrix_mult</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">port</span> ( Clock: <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
reset : <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--active high reset</span>
start : <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--A '1' starts the matrix multiplication process.</span>
A,B : <span style="color: #008800; font-weight: bold;">in</span> unsigned(<span style="color: #0000dd; font-weight: bold;">71</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
C : <span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">71</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
done : <span style="color: #008800; font-weight: bold;">out</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #888888;">--a '1' indicates that multiplication is done and result is availble at C.</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">entity</span>;
<span style="color: #008800; font-weight: bold;">architecture</span> <span style="color: #bb0066; font-weight: bold;">Behav</span> <span style="color: #008800; font-weight: bold;">of</span> <span style="color: #bb0066; font-weight: bold;">matrix_mult</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">type</span> matType <span style="color: #008800; font-weight: bold;">is</span> <span style="color: #008800; font-weight: bold;">array</span>(<span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span>,<span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span>) <span style="color: #008800; font-weight: bold;">of</span> unsigned(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> matA, matB, matC : matType := (<span style="color: #008800; font-weight: bold;">others</span> => (<span style="color: #008800; font-weight: bold;">others</span> => X<span style="background-color: #fff0f0;">"00"</span>));
<span style="color: #008800; font-weight: bold;">type</span> state_type <span style="color: #008800; font-weight: bold;">is</span> (init,do_mult,apply_outputs);
<span style="color: #008800; font-weight: bold;">signal</span> state : state_type := init;
<span style="color: #008800; font-weight: bold;">signal</span> i,j,k : <span style="color: #333399; font-weight: bold;">integer</span> := <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">begin</span>
sm : <span style="color: #008800; font-weight: bold;">process</span> (Clock,reset) <span style="color: #888888;">--process implementing the state machine for multiplying the matrices.</span>
<span style="color: #008800; font-weight: bold;">variable</span> temp : unsigned(<span style="color: #0000dd; font-weight: bold;">15</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>) := (<span style="color: #008800; font-weight: bold;">others</span> => <span style="color: #0044dd;">'0'</span>);
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #008800; font-weight: bold;">if</span>(reset = <span style="color: #0044dd;">'1'</span>) <span style="color: #008800; font-weight: bold;">then</span>
state <= init;
i <= <span style="color: #0000dd; font-weight: bold;">0</span>;
j <= <span style="color: #0000dd; font-weight: bold;">0</span>;
k <= <span style="color: #0000dd; font-weight: bold;">0</span>;
done <= <span style="color: #0044dd;">'0'</span>;
matA <= (<span style="color: #008800; font-weight: bold;">others</span> => (<span style="color: #008800; font-weight: bold;">others</span> => X<span style="background-color: #fff0f0;">"00"</span>));
matB <= (<span style="color: #008800; font-weight: bold;">others</span> => (<span style="color: #008800; font-weight: bold;">others</span> => X<span style="background-color: #fff0f0;">"00"</span>));
matC <= (<span style="color: #008800; font-weight: bold;">others</span> => (<span style="color: #008800; font-weight: bold;">others</span> => X<span style="background-color: #fff0f0;">"00"</span>));
<span style="color: #008800; font-weight: bold;">elsif</span> rising_edge(Clock) <span style="color: #008800; font-weight: bold;">then</span>
<span style="color: #008800; font-weight: bold;">case</span> state <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">when</span> init => <span style="color: #888888;">--the matrices which are in a 1-D array are converted to 2-D matrices first.</span>
<span style="color: #008800; font-weight: bold;">if</span>(start = <span style="color: #0044dd;">'1'</span>) <span style="color: #008800; font-weight: bold;">then</span>
<span style="color: #008800; font-weight: bold;">for</span> i <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #008800; font-weight: bold;">loop</span> <span style="color: #888888;">--run through the rows</span>
<span style="color: #008800; font-weight: bold;">for</span> j <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #008800; font-weight: bold;">loop</span> <span style="color: #888888;">--run through the columns</span>
matA(i,j) <= A((i*<span style="color: #0000dd; font-weight: bold;">3</span>+j+<span style="color: #0000dd; font-weight: bold;">1</span>)*<span style="color: #0000dd; font-weight: bold;">8</span>-<span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #008800; font-weight: bold;">downto</span> (i*<span style="color: #0000dd; font-weight: bold;">3</span>+j)*<span style="color: #0000dd; font-weight: bold;">8</span>);
matB(i,j) <= B((i*<span style="color: #0000dd; font-weight: bold;">3</span>+j+<span style="color: #0000dd; font-weight: bold;">1</span>)*<span style="color: #0000dd; font-weight: bold;">8</span>-<span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #008800; font-weight: bold;">downto</span> (i*<span style="color: #0000dd; font-weight: bold;">3</span>+j)*<span style="color: #0000dd; font-weight: bold;">8</span>);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">loop</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">loop</span>;
state <= do_mult;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">when</span> do_mult =>
temp := matA(i,k)*matB(k,j);
matC(i,j) <= matC(i,j) + temp(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">if</span>(k = <span style="color: #0000dd; font-weight: bold;">2</span>) <span style="color: #008800; font-weight: bold;">then</span>
k <= <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">if</span>(j = <span style="color: #0000dd; font-weight: bold;">2</span>) <span style="color: #008800; font-weight: bold;">then</span>
j <= <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">if</span> (i= <span style="color: #0000dd; font-weight: bold;">2</span>) <span style="color: #008800; font-weight: bold;">then</span>
i <= <span style="color: #0000dd; font-weight: bold;">0</span>;
state <= apply_outputs;
<span style="color: #008800; font-weight: bold;">else</span>
i <= i + <span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
j <= j+<span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">else</span>
k <= k+<span style="color: #0000dd; font-weight: bold;">1</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">when</span> apply_outputs => <span style="color: #888888;">--convert 3 by 3 matrix into a 1-D matrix.</span>
<span style="color: #008800; font-weight: bold;">for</span> i <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #008800; font-weight: bold;">loop</span> <span style="color: #888888;">--run through the rows</span>
<span style="color: #008800; font-weight: bold;">for</span> j <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #008800; font-weight: bold;">loop</span> <span style="color: #888888;">--run through the columnss</span>
C((i*<span style="color: #0000dd; font-weight: bold;">3</span>+j+<span style="color: #0000dd; font-weight: bold;">1</span>)*<span style="color: #0000dd; font-weight: bold;">8</span>-<span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #008800; font-weight: bold;">downto</span> (i*<span style="color: #0000dd; font-weight: bold;">3</span>+j)*<span style="color: #0000dd; font-weight: bold;">8</span>) <= matC(i,j);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">loop</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">loop</span>;
done <= <span style="color: #0044dd;">'1'</span>;
state <= init;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">case</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">architecture</span>;</pre><h3><u><br /></u></h3><h3><u>tb_matrix_mult.vhd:</u></h3><div><u><br /></u></div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #888888;">--Testbench for testing the 3 by 3 matrix multiplier.</span>
<span style="color: #008800; font-weight: bold;">library</span> <span style="color: #0e84b5; font-weight: bold;">IEEE</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.std_logic_1164.all</span>;
<span style="color: #008800; font-weight: bold;">use</span> <span style="color: #0e84b5; font-weight: bold;">IEEE.numeric_std.all</span>;
<span style="color: #008800; font-weight: bold;">entity</span> <span style="color: #bb0066; font-weight: bold;">tb_matrix_mult</span> <span style="color: #008800; font-weight: bold;">is</span> <span style="color: #888888;">--testbench entity is always empty. No input or output ports.</span>
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">entity</span>;
<span style="color: #008800; font-weight: bold;">architecture</span> <span style="color: #bb0066; font-weight: bold;">behav</span> <span style="color: #008800; font-weight: bold;">of</span> <span style="color: #bb0066; font-weight: bold;">tb_matrix_mult</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">component</span> <span style="color: #bb0066; font-weight: bold;">matrix_mult</span> <span style="color: #008800; font-weight: bold;">is</span>
<span style="color: #008800; font-weight: bold;">port</span> ( Clock: <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>;
reset : <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--active high reset</span>
start : <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #333399; font-weight: bold;">std_logic</span>; <span style="color: #888888;">--A '1' starts the matrix multiplication process.</span>
A,B : <span style="color: #008800; font-weight: bold;">in</span> unsigned(<span style="color: #0000dd; font-weight: bold;">71</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
C : <span style="color: #008800; font-weight: bold;">out</span> unsigned(<span style="color: #0000dd; font-weight: bold;">71</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
done : <span style="color: #008800; font-weight: bold;">out</span> <span style="color: #333399; font-weight: bold;">std_logic</span> <span style="color: #888888;">--a '1' indicates that multiplication is done and result is availble at C.</span>
);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">component</span>;
<span style="color: #008800; font-weight: bold;">signal</span> A,B,C : unsigned(<span style="color: #0000dd; font-weight: bold;">71</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> Clock,reset, start, done : <span style="color: #333399; font-weight: bold;">std_logic</span> := <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">type</span> matType <span style="color: #008800; font-weight: bold;">is</span> <span style="color: #008800; font-weight: bold;">array</span>(<span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span>,<span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span>) <span style="color: #008800; font-weight: bold;">of</span> unsigned(<span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #008800; font-weight: bold;">downto</span> <span style="color: #0000dd; font-weight: bold;">0</span>);
<span style="color: #008800; font-weight: bold;">signal</span> matC : matType := (<span style="color: #008800; font-weight: bold;">others</span> => (<span style="color: #008800; font-weight: bold;">others</span> => X<span style="background-color: #fff0f0;">"00"</span>));
<span style="color: #008800; font-weight: bold;">begin</span>
matrix_multiplier : matrix_mult <span style="color: #008800; font-weight: bold;">port</span> <span style="color: #008800; font-weight: bold;">map</span> (Clock, reset, start, A,B, C,done);
<span style="color: #888888;">--generate a 50Mhz clock for testing the design.</span>
Clk_generator : <span style="color: #008800; font-weight: bold;">process</span>
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">10</span> ns;
Clock <= <span style="color: #008800; font-weight: bold;">not</span> Clock;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
apply_inputs : <span style="color: #008800; font-weight: bold;">process</span>
<span style="color: #008800; font-weight: bold;">begin</span>
reset <= <span style="color: #0044dd;">'1'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">100</span> ns;
reset <= <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">20</span> ns;
A <= X<span style="background-color: #fff0f0;">"09" & X"08" & X"07" & X"06" & X"05" & X"04" & X"03" & X"02" & X"01"</span>;
B <= X<span style="background-color: #fff0f0;">"01" & X"09" & X"08" & X"07" & X"06" & X"05" & X"04" & X"03" & X"02"</span>;
start <= <span style="color: #0044dd;">'1'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">20</span> ns;
start <= <span style="color: #0044dd;">'0'</span>;
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">until</span> done = <span style="color: #0044dd;">'1'</span>;
<span style="color: #888888;">--The result C should be (93,150,126,57,96,81,21,42,36)</span>
<span style="color: #008800; font-weight: bold;">wait</span> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #0000dd; font-weight: bold;">5</span> ns;
<span style="color: #008800; font-weight: bold;">for</span> i <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #008800; font-weight: bold;">loop</span> <span style="color: #888888;">--run through the rows</span>
<span style="color: #008800; font-weight: bold;">for</span> j <span style="color: #008800; font-weight: bold;">in</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #008800; font-weight: bold;">to</span> <span style="color: #0000dd; font-weight: bold;">2</span> <span style="color: #008800; font-weight: bold;">loop</span> <span style="color: #888888;">--run through the columnss</span>
matC(i, j) <= C((i * <span style="color: #0000dd; font-weight: bold;">3</span> + j + <span style="color: #0000dd; font-weight: bold;">1</span>) * <span style="color: #0000dd; font-weight: bold;">8</span> - <span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #008800; font-weight: bold;">downto</span> (i * <span style="color: #0000dd; font-weight: bold;">3</span> + j) * <span style="color: #0000dd; font-weight: bold;">8</span>);
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">loop</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">loop</span>;
<span style="color: #008800; font-weight: bold;">wait</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #bb0066; font-weight: bold;">behav</span>;</pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><h3 style="color: black; font-family: "Times New Roman"; white-space: normal;"><u>Simulation Results:</u></h3></pre><p> The design was simulated successfully using Modelsim SE 10.4a version. Screenshots of the simulation waveform is shown below:</p><p></p><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNo47nhS0GbKigL-WYXhVUgjGDb5OuWdiRhfn3s8dut-mYnHLeBUF4D863-dKmuUn7mMVQGQbOmx0pu5DsycekeAOF7655dN0z1zb76__12mb3QyPac88lK-64f6t7eNWqNAf0TZDbyw9K/" style="margin-left: 1em; margin-right: 1em;"><img data-original-height="690" data-original-width="1505" height="294" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNo47nhS0GbKigL-WYXhVUgjGDb5OuWdiRhfn3s8dut-mYnHLeBUF4D863-dKmuUn7mMVQGQbOmx0pu5DsycekeAOF7655dN0z1zb76__12mb3QyPac88lK-64f6t7eNWqNAf0TZDbyw9K/w640-h294/image.png" width="640" /></a></div></div><br /><p></p><p style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWEZio8_8YPGPoX8IIgQrVt9R_PpHM8l2H0-WlEyoCMwRi1OoAFYiIghFznVop-2xKC05Ln9-6z2TXlrZFLHDeG60DRs0KmTtu_gYIFOsGF3pe3Odp8FmtVA71jZqpc8rj8Y5LE7A4O0wx/" style="margin-left: 1em; margin-right: 1em;"><img data-original-height="679" data-original-width="1509" height="288" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWEZio8_8YPGPoX8IIgQrVt9R_PpHM8l2H0-WlEyoCMwRi1OoAFYiIghFznVop-2xKC05Ln9-6z2TXlrZFLHDeG60DRs0KmTtu_gYIFOsGF3pe3Odp8FmtVA71jZqpc8rj8Y5LE7A4O0wx/w640-h288/image.png" width="640" /></a></p><p><br /></p><p><span> </span>Please let me know if you are unable to get the code to work or if its not synthesisable. Good luck with your projects. </p><p> </p>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com1tag:blogger.com,1999:blog-2050962176404305705.post-40184805477993742032020-11-26T09:43:00.005+05:302020-12-04T22:13:48.747+05:30Signals and Variables in VHDL<p><span> Every programming language has objects for storing values. VHDL too have them. Two of these object types are called </span>Signals and Variables. They might look very similar for a beginner, but there are few fundamental differences between them. </p><p></p><ul style="text-align: left;"><li>Variables are assigned using the := operator. And signals are assigned with the <= operator.</li><li>Variables can be declared and used only within a process/function/procedure but Signals can be declared and used anywhere.</li></ul><h4 style="text-align: left;"><u><br /></u></h4><h4 style="text-align: left;"><u>A very fundamental difference:</u></h4><p></p><p><span> </span>In a block of statements, the statements with variables immediately take their values. Very similar to how it works in programming languages like C. But in a group of statements with Signals on the left hand side, the signals does not take it's new value until the process has suspended (either hit the bottom or hit a wait statement).</p><p>This can be further explained with the following example scenario. </p><p>Suppose I want to implement a swapping function in VHDL using Signals. </p><p>I can simply write, </p><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #008800; font-weight: bold;">signal</span> x,y : <span style="color: #333399; font-weight: bold;">std_logic</span> := <span style="color: #0000dd; font-weight: bold;">0</span>;</pre><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #008800; font-weight: bold;">process</span>(Clk)
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #008800; font-weight: bold;">if</span>(rising_edge(Clk)) <span style="color: #008800; font-weight: bold;">then</span>
x <= y;
y <= x;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;</pre></pre><p><span> What happens above is that, though the 'x' is assigned the value of 'y' sequentially first, the new value isn't updated to 'x' until we "exit" the process. So from a practical point of looking at it, it looks like they happen in parallel. </span></p><p><span> </span>Now if I have to use variables for implementing a swapping function, I need three statements. Like below:</p><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #008800; font-weight: bold;">process</span>(Clk)
<span style="color: #008800; font-weight: bold;">variable</span> x,y,temp : <span style="color: #333399; font-weight: bold;">std_logic</span> := <span style="color: #0000dd; font-weight: bold;">0</span>;
<span style="color: #008800; font-weight: bold;">begin</span>
<span style="color: #008800; font-weight: bold;">if</span>(rising_edge(Clk)) <span style="color: #008800; font-weight: bold;">then</span>
temp := x;
x := y;
y := x;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">if</span>;
<span style="color: #008800; font-weight: bold;">end</span> <span style="color: #008800; font-weight: bold;">process</span>;</pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><br /></pre><p style="text-align: left;"> <span style="font-family: Times New Roman;"><span style="white-space: normal;">Since variables take the values assigned to them right away, we need a temporary variable to hold the value of 'x' before assigning 'y' to it.</span></span></p></pre><ul style="text-align: left;"><li>Variables declared in different processes cannot communicate with each other. They are local to the process. On the other hand signals declared in a VHDL entity can be used anywhere in the entity.</li><li>You cannot declare or use a Signal inside a VHDL Function. Functions are purely combinatorial in VHDL and thus you have to have use variables. </li></ul><p></p><p><br /></p><p><span> </span>If you want the code to be synthesised, then beware of the consequences of using a variable. Variables often create latches when implemented on a FPGA and synthesis tools often pass a warning to notify. If not needed its good to avoid latches in your design. </p><p><span> </span>Though using variables might seem make the work easier, it might not pass the synthesis stage. For many, who come to VHDL from a C background, using variables is very tempting.</p><p><br /></p><h4 style="text-align: left;"><span><u>Be easy with the use of Variables:</u></span></h4><p><span> </span>Check this <a href="https://vhdlguru.blogspot.com/2010/03/matrix-multiplication-in-vhdl.html" target="_blank">Matrix Multiplication code using Variables</a> to see some of the dangers involved with them. Multiplication of two matrices requires a large number of multipliers and adders. In C, you would use some nested "for" loops to achieve this. And with the use of variables you can do the same thing in VHDL too like you can see from the link. </p><p><span> </span>But using this same piece of code in a real FPGA is impossible to achieve. Either the design wont pass the synthesis stage or it will take days to get it done. </p><p><span> </span>All those individual additions and multiplications gets done in "one" clock cycle. None of the adders and multipliers get reused and the loops get unfolded into a concatenated series of resources. </p><p><span><span> In such a case its necessary to use signals and spilt the whole operation over many clock cycles. This reduces the resource usage and more importantly you have a chance to get your design synthesised. </span><br /></span></p><p><span><span><br /></span></span></p>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0tag:blogger.com,1999:blog-2050962176404305705.post-35878513527492257182020-11-22T21:23:00.002+05:302020-11-22T21:23:30.146+05:30Simulating a VHDL/Verilog code using Modelsim SE<p><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><span> </span>This is a simple How-To video for ModelSim SE 10.4a version. If you are already familiar with this software tool then you may not need to watch this video.</span></p><br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;" /><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;">In this video, I am trying to show you:</span><br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;" /><ol style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"><li style="margin: 0px 0px 0.25em; padding: 0px;">How to create a new project in ModelSim SE.</li><li style="margin: 0px 0px 0.25em; padding: 0px;">Add VHDL codes to this project.</li><li style="margin: 0px 0px 0.25em; padding: 0px;">Compile and simulate the codes.</li><li style="margin: 0px 0px 0.25em; padding: 0px;">Few tips on the simulation part of the tool.<br /><br /></li></ol><div><span style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13.2px;"><br /></span></span></div><div><span style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="392" src="https://www.youtube.com/embed/9uSWnVwk_vg" width="472" youtube-src-id="9uSWnVwk_vg"></iframe></div><br /><span style="font-size: 13.2px;"><br /></span></span></div><div><span style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 13.2px;"><br /></span></span></div>vipinhttp://www.blogger.com/profile/02146017720228354842noreply@blogger.com0