<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ro">
	<id>http://wiki.dcae.pub.ro/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Zhascsi</id>
	<title>WikiLabs - Contribuții utilizator [ro]</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.dcae.pub.ro/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Zhascsi"/>
	<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php/Special:Contribu%C8%9Bii/Zhascsi"/>
	<updated>2026-04-17T09:31:11Z</updated>
	<subtitle>Contribuții utilizator</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Advanced_Digital_Systems&amp;diff=8113</id>
		<title>Advanced Digital Systems</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Advanced_Digital_Systems&amp;diff=8113"/>
		<updated>2025-05-28T15:13:57Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* [https://users.dcae.pub.ro/~zhascsi/courses/add/add2.pdf Structured design with verilog]&lt;br /&gt;
* [https://users.dcae.pub.ro/~zhascsi/courses/add/add3.pdf Carry Lookahead Adder - iterative design]&lt;br /&gt;
* [https://users.dcae.pub.ro/~zhascsi/courses/add/add4.pdf Carry Lookahead Adder - recursive design]&lt;br /&gt;
&lt;br /&gt;
== Test code for adder ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
module test;&lt;br /&gt;
&lt;br /&gt;
logic [31:0] a;&lt;br /&gt;
logic [31:0] b;&lt;br /&gt;
logic [32:0] s;&lt;br /&gt;
&lt;br /&gt;
cla   #(32) dut (.a(a), .b(b), .s(s));&lt;br /&gt;
&lt;br /&gt;
initial begin&lt;br /&gt;
    repeat(100) begin&lt;br /&gt;
        #1&lt;br /&gt;
        a  = $random;&lt;br /&gt;
        b  = $random;&lt;br /&gt;
        #1&lt;br /&gt;
        if(s !== a + b)&lt;br /&gt;
            $display(&amp;quot;ERROR\n&amp;quot;);&lt;br /&gt;
        else&lt;br /&gt;
            $display(&amp;quot;OK\n&amp;quot;);&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
endmodule&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Projects ==&lt;br /&gt;
&lt;br /&gt;
# [[MD5 Hash]]&lt;br /&gt;
# [[SHA-1 Hash]]&lt;br /&gt;
# [[SHA-256 Hash]]&lt;br /&gt;
# [[SHA-512 Hash]]&lt;br /&gt;
# [[AES Encryption]]&lt;br /&gt;
# [[DES Encryption]]&lt;br /&gt;
# [[RSA Encryption]]&lt;br /&gt;
# [[RC4 Stream Cypher]]&lt;br /&gt;
# [[Sorting Network]]&lt;br /&gt;
# [[Sum of Absolute Differences]]&lt;br /&gt;
# [[Sum of Squared Differences]]&lt;br /&gt;
# [[Artificial Neural Network]]&lt;br /&gt;
# [[Gaussian 2D Filter]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Advanced_Digital_Systems&amp;diff=8112</id>
		<title>Advanced Digital Systems</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Advanced_Digital_Systems&amp;diff=8112"/>
		<updated>2025-05-28T14:59:48Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* [https://users.dcae.pub.ro/~zhascsi/courses/add/add2.pdf Structured design with verilog]&lt;br /&gt;
* [https://users.dcae.pub.ro/~zhascsi/courses/add/add3.pdf Carry Lookahead Adder - iterative design]&lt;br /&gt;
* [https://users.dcae.pub.ro/~zhascsi/courses/add/add4.pdf Carry Lookahead Adder - recursive design]&lt;br /&gt;
&lt;br /&gt;
== Projects ==&lt;br /&gt;
&lt;br /&gt;
# [[MD5 Hash]]&lt;br /&gt;
# [[SHA-1 Hash]]&lt;br /&gt;
# [[SHA-256 Hash]]&lt;br /&gt;
# [[SHA-512 Hash]]&lt;br /&gt;
# [[AES Encryption]]&lt;br /&gt;
# [[DES Encryption]]&lt;br /&gt;
# [[RSA Encryption]]&lt;br /&gt;
# [[RC4 Stream Cypher]]&lt;br /&gt;
# [[Sorting Network]]&lt;br /&gt;
# [[Sum of Absolute Differences]]&lt;br /&gt;
# [[Sum of Squared Differences]]&lt;br /&gt;
# [[Artificial Neural Network]]&lt;br /&gt;
# [[Gaussian 2D Filter]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8088</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8088"/>
		<updated>2025-05-04T21:37:00Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Verification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Lab10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 100 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 10 clock cycles (instead of one at each 100,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
* Use push buttons for &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; inputs, and switches for the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; input value.&lt;br /&gt;
* Use one digit from the left display to show the current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value in decimal.&lt;br /&gt;
* Use one digit from the right display to show the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value in decimal&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd)&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8087</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8087"/>
		<updated>2025-05-04T21:36:18Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* pulsegen */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Lab10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 100 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
* Use push buttons for &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; inputs, and switches for the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; input value.&lt;br /&gt;
* Use one digit from the left display to show the current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value in decimal.&lt;br /&gt;
* Use one digit from the right display to show the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value in decimal&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd)&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8086</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8086"/>
		<updated>2025-05-04T21:34:22Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Lab10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
* Use push buttons for &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; inputs, and switches for the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; input value.&lt;br /&gt;
* Use one digit from the left display to show the current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value in decimal.&lt;br /&gt;
* Use one digit from the right display to show the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value in decimal&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd)&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8085</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8085"/>
		<updated>2025-05-04T21:32:08Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Lab10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
* Use push buttons for &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; inputs, and switches for the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; input value.&lt;br /&gt;
* Use one digit from the left display to show the current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value in decimal.&lt;br /&gt;
* Use one digit from the right display to show the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value in decimal&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8084</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8084"/>
		<updated>2025-05-04T21:31:49Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Lab10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Use push buttons for &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; inputs, and switches for the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; input value.&lt;br /&gt;
Use one digit from the left display to show the current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value in decimal.&lt;br /&gt;
Use one digit from the right display to show the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value in decimal&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Digital_Integrated_Circuits_(lab)&amp;diff=8083</id>
		<title>Digital Integrated Circuits (lab)</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Digital_Integrated_Circuits_(lab)&amp;diff=8083"/>
		<updated>2025-05-04T21:29:33Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;# [[Applications 1 | Laboratory 1]]&lt;br /&gt;
# [[Applications 2 | Laboratory 2]]&lt;br /&gt;
# [[Applications 3 | Laboratory 3]]&lt;br /&gt;
# [[Applications 4 | Laboratory 4]]&lt;br /&gt;
# [[Applications 5 | Laboratory 5]]&lt;br /&gt;
# Test 1&lt;br /&gt;
# [[Applications 6 | Laboratory 6]]&lt;br /&gt;
# [[Applications 7 | Laboratory 7]]&lt;br /&gt;
# [[Applications 8 | Laboratory 8]]&lt;br /&gt;
# [[Applications 9 | Laboratory 9]]&lt;br /&gt;
# [[Applications 10 | Laboratory 10]]&lt;br /&gt;
&lt;br /&gt;
== Verilog Tutorials ==&lt;br /&gt;
* [http://www.asic-world.com/verilog/veritut.html Verilog Tutorial] from ASIC World&lt;br /&gt;
* [http://chipverify.com/verilog/verilog-tutorial Verilog Tutorial] from Chip Verify&lt;br /&gt;
* [http://www.emmelmann.org/Library/Tutorials/docs/verilog_ref_guide/vlog_ref_top.html Verilog reference guide]&lt;br /&gt;
&lt;br /&gt;
== Software ==&lt;br /&gt;
* [https://www.xilinx.com/products/design-tools/vivado/vivado-ml.html Vivado™ ML Standard Edition free]&lt;br /&gt;
* [https://wiki.dcae.pub.ro/images/2/24/VivadoNewProjectTutorial.pdf Vivado New Project Tutorial]&lt;br /&gt;
&lt;br /&gt;
== Hardware ==&lt;br /&gt;
[https://www.realdigital.org/hardware/boolean Boolean Board] from RealDigital&lt;br /&gt;
* [https://www.realdigital.org/doc/02013cd17602c8af749f00561f88ae21 Boolean Board - user manual]&lt;br /&gt;
* [[Boolean Board - Pinout]]&lt;br /&gt;
To implement a circuit on Boolean Board you should select the following target device in Project Settings: &amp;#039;&amp;#039;&amp;#039;xc7s50csga324-1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
* [https://dpoauwgwqsy2x.cloudfront.net/Download/pynqz2_user_manual_v1_0.pdf Pynq-Z2 - user manual]&lt;br /&gt;
* [[Pynq-Z2 - Pinout]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8082</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8082"/>
		<updated>2025-05-04T21:26:55Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* triangle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Lab10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Using the recommended board display digits, push buttons, switches and clock sources (see Figure 1), implement the &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039; top-level design module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Lab10_triangle.png&amp;diff=8081</id>
		<title>Fișier:Lab10 triangle.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Lab10_triangle.png&amp;diff=8081"/>
		<updated>2025-05-04T21:26:14Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8080</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8080"/>
		<updated>2025-05-04T21:19:49Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Verification */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 10;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Using the recommended board display digits, push buttons, switches and clock sources (see Figure 1), implement the &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039; top-level design module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8079</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8079"/>
		<updated>2025-05-04T21:19:26Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* rom16x8 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 5;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Using the recommended board display digits, push buttons, switches and clock sources (see Figure 1), implement the &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039; top-level design module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8078</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8078"/>
		<updated>2025-05-04T21:18:48Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* fsm_ctrl */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. A push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;init&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 5;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Using the recommended board display digits, push buttons, switches and clock sources (see Figure 1), implement the &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039; top-level design module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8077</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8077"/>
		<updated>2025-05-04T21:17:55Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* pulsegen */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 10 million clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 100_000_000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. Keep in mind that the push button is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, and that a push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;init&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 5;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Using the recommended board display digits, push buttons, switches and clock sources (see Figure 1), implement the &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039; top-level design module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8076</id>
		<title>Applications 10</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_10&amp;diff=8076"/>
		<updated>2025-05-04T21:17:02Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* triangle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Requirement ==&lt;br /&gt;
&amp;#039;&amp;#039;Design, verify and implement a triangular sequence generator. It generates a sequence of numbers that periodically increase and decrease between two configurable limits, limith and limitl. The sequence rate is one number per second. The binary sequence of numbers, &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;, is outputted for display as &amp;#039;&amp;#039;&amp;#039;seg_bcd&amp;#039;&amp;#039;&amp;#039;. Figure 1 shows an example of triangular sequence that varies between 6 and 9. The bottom waveform is the analog signal that would be generated by a DAC whose input is the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle_wave.png|400px]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;The default limit values are 0 for &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and 9 for &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;, and they are set each time the reset is applied. To change the limits use the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button in this order. Set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a desired value for the low limit and push the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button to set the &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; value to &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. Then change the &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value to the desired high limit and with a second push set the &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; value this new &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value. The current &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; value is displayed through &amp;#039;&amp;#039;&amp;#039;seg_din&amp;#039;&amp;#039;&amp;#039; output. After configuration is done, the &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; button is ignored.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== triangle ===&lt;br /&gt;
&lt;br /&gt;
The top level design, named &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039;, has 4 sequential blocks and one dual port ROM memory (Figure 2).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Figure 2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl10_triangle.png]]&lt;br /&gt;
&lt;br /&gt;
All sequential blocks are clocked by the same 100 MHz input clock and share the reset sinal, &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, active &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pulsegen ===&lt;br /&gt;
&lt;br /&gt;
To easy the design of the circuit, the pulse generator, &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039;, generates a periodic sequence of pulses, at a rate of one pulse per second, each pulse lasting only one clock cycle. The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; is a counter that generates one pulse at each 50,000,000 clock cycles. However, to make possible the verification of the circuit, this number is not given as an explicit literal in HDL description but as a parameter declared inside the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
parameter CYCLE = 50000000;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The parameter value may be changed from the testbench to much lower values, suitable for verification (otherwise the simulation will run for hours to count 50 million pulses!).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; output, &amp;#039;&amp;#039;&amp;#039;pulse&amp;#039;&amp;#039;&amp;#039;, is used to enable the counting of the &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039; counter.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== cnt_bcd ===&lt;br /&gt;
&lt;br /&gt;
Whenever its &amp;#039;&amp;#039;&amp;#039;cen&amp;#039;&amp;#039;&amp;#039; input is active, it increments or decrements, according to &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; input. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; it increments. If &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039; is &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; it decrements.&lt;br /&gt;
&lt;br /&gt;
=== fsm_cnt ===&lt;br /&gt;
&lt;br /&gt;
This automaton controls the sequence generated by &amp;#039;&amp;#039;&amp;#039;cnt_bcd&amp;#039;&amp;#039;&amp;#039;, changing its direction of counting such that to keep the &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; sequence between the limits, &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039;. Whenever &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039; value reaches one of the limits, the counting direction, &amp;#039;&amp;#039;&amp;#039;dir&amp;#039;&amp;#039;&amp;#039;, is reversed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== fsm_ctrl ===&lt;br /&gt;
&lt;br /&gt;
Another automaton controls the configuration process. It keeps two registers, &amp;#039;&amp;#039;&amp;#039;limith&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;limitl&amp;#039;&amp;#039;&amp;#039;, whose values are changed according to the configuration protocol described above. At the reset, their values are set to the default ones, &amp;#039;&amp;#039;&amp;#039;9&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. Two successive push update the values of the limits, after which the new limit values stay stable until reset. Keep in mind that the push button is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, and that a push is completed after the button is released.&lt;br /&gt;
&lt;br /&gt;
=== rom16x8 ===&lt;br /&gt;
&lt;br /&gt;
The ROM memory is a dual port read only memory. It may be initialized through the &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; tasks with proper values for digit display. Alternatively, the initialization of the memory content may be done with an &amp;#039;&amp;#039;&amp;#039;init&amp;#039;&amp;#039;&amp;#039; process:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial begin // the last 7 bits are the digit segment values&lt;br /&gt;
    mem[0] = 8&amp;#039;b01000000;&lt;br /&gt;
    mem[1] = 8&amp;#039;b01111001;&lt;br /&gt;
    .... // initialization for the other memory locations&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Verification ==&lt;br /&gt;
&lt;br /&gt;
Write a testbench with the following test scenario:&lt;br /&gt;
* apply reset&lt;br /&gt;
* wait for 100 clock cycles&lt;br /&gt;
* set &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;din&amp;#039;&amp;#039;&amp;#039; to a high limit and apply &amp;#039;&amp;#039;&amp;#039;push&amp;#039;&amp;#039;&amp;#039; for a couple of cycles&lt;br /&gt;
* let the simulation run for some other 100 clock cycles&lt;br /&gt;
&lt;br /&gt;
In the testbench module redefine the &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; parameter, such that is generates a pulse at each 5 clock cycles (instead of one at each 50,000,000 cycles):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
defparam dut.pulsegen.CYCLE = 5;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
Using the recommended board display digits, push buttons, switches and clock sources (see Figure 1), implement the &amp;#039;&amp;#039;&amp;#039;triangle&amp;#039;&amp;#039;&amp;#039; top-level design module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Interfaces ==&lt;br /&gt;
&lt;br /&gt;
pulsegen(rst, clk, pulse) with an internal parameter named &amp;#039;&amp;#039;&amp;#039;CYCLE&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
cnt_bcd(rst, clk, cen, dir, value)&lt;br /&gt;
&lt;br /&gt;
fsm_cnt(rst, clk, limith, limitl, value, dir)&lt;br /&gt;
&lt;br /&gt;
fsm_ctrl(rst, clk, push, din, limith, limitl)&lt;br /&gt;
&lt;br /&gt;
rom16x8(addra, douta, addrb, doutb)&lt;br /&gt;
&lt;br /&gt;
triangle(rst, clk, push, din, seg_din, seg_bcd) with the pulsegen instance name &amp;#039;&amp;#039;&amp;#039;pulsegen&amp;#039;&amp;#039;&amp;#039; and a 4 bit connection named &amp;#039;&amp;#039;&amp;#039;bcd&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
triangle_tb with the triangle instance name &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8075</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8075"/>
		<updated>2025-04-29T23:35:42Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* State transitions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal signal declarations&lt;br /&gt;
# description (structural with instantiations, or behavioral with always processes and continuous assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process is sensitive only to the active clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The state variable must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8074</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8074"/>
		<updated>2025-04-29T23:32:32Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Outputs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal signal declarations&lt;br /&gt;
# description (structural with instantiations, or behavioral with always processes and continuous assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process is sensitive only to the active clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8073</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8073"/>
		<updated>2025-04-29T23:31:50Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Outputs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal signal declarations&lt;br /&gt;
# description (structural with instantiations, or behavioral with always processes and continuous assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process is sensitive only to the active clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8072</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8072"/>
		<updated>2025-04-29T23:30:58Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* State coding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal signal declarations&lt;br /&gt;
# description (structural with instantiations, or behavioral with always processes and continuous assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process is sensitive only to the active clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl9_moore_wave.png&amp;diff=8071</id>
		<title>Fișier:Appl9 moore wave.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl9_moore_wave.png&amp;diff=8071"/>
		<updated>2025-04-29T23:11:18Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a încărcat o versiune nouă pentru Fișier:Appl9 moore wave.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8070</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8070"/>
		<updated>2025-04-29T23:08:43Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Outputs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process is sensitive only to the active clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always_comb&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8069</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8069"/>
		<updated>2025-04-29T23:08:08Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* State transitions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process is sensitive only to the active clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8068</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8068"/>
		<updated>2025-04-29T23:07:09Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Exercise 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is sensitive only to clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always_ff&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8067</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8067"/>
		<updated>2025-04-29T23:06:23Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Exercise 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is sensitive only to clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl9_ex1.png&amp;diff=8066</id>
		<title>Fișier:Appl9 ex1.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl9_ex1.png&amp;diff=8066"/>
		<updated>2025-04-29T23:03:40Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a încărcat o versiune nouă pentru Fișier:Appl9 ex1.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8065</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8065"/>
		<updated>2025-04-29T23:00:14Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* State transitions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is sensitive only to clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
&amp;lt;!--* To implement the design assign KEY0, SW1 and SW2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LEDR1 and LEDR2 to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.--&amp;gt;&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8064</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8064"/>
		<updated>2025-04-29T22:59:04Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Outputs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is sensitive only to clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_comb begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
&amp;lt;!--* To implement the design assign KEY0, SW1 and SW2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LEDR1 and LEDR2 to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.--&amp;gt;&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8063</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8063"/>
		<updated>2025-04-29T22:58:17Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Outputs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is sensitive only to clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(*) begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
&amp;lt;!--* To implement the design assign KEY0, SW1 and SW2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LEDR1 and LEDR2 to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.--&amp;gt;&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8062</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8062"/>
		<updated>2025-04-29T22:57:49Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Exercise 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is sensitive only to clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(*) begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
&amp;lt;!--* To implement the design assign KEY0, SW1 and SW2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LEDR1 and LEDR2 to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.--&amp;gt;&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always_ff @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8061</id>
		<title>Applications 9</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_9&amp;diff=8061"/>
		<updated>2025-04-29T22:57:29Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Exercise 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Almost always the FSM is described behaviorally, leaving the CLC gate level generation and optimization to compiler/synthesizer. However, the usual behavioral description closely follows the block structure of the FSM, with one block that computes the transition function and another block that computes the output. The FSMs are of two categories:&lt;br /&gt;
* Moore FSM, whose outputs depend only on the current state&lt;br /&gt;
* Mealy FSM, whose outputs depend also on inputs&lt;br /&gt;
&lt;br /&gt;
Most often the Mealy FSM is a delayed Mealy FSM. Its output depends on the previous state and on the input values at transition time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs remain asserted until reset. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
=== Moore FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a Moore FSM, with two separate states for those two particular output configurations. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final states, &amp;#039;&amp;#039;&amp;#039;R&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;U&amp;#039;&amp;#039;&amp;#039;, the automaton stays until reset. From any state the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== State coding ===&lt;br /&gt;
&lt;br /&gt;
It is not recommended to use explicit values whenever the state variable is used. To make code much more readable and easier to design and debug, the state values are parameters. Another advantage of using parameters is that the state explicit values are declared in a single place, at parameter declaration, therefore if their values need to be changed, only the parameter declaration is affected.&lt;br /&gt;
&lt;br /&gt;
The state values are internal design options, therefore the parameters used for states are local ones. They are declared as local parameters, using the template &amp;#039;&amp;#039;&amp;#039;localparam&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;name&amp;#039;&amp;#039;=&amp;#039;&amp;#039;value&amp;#039;&amp;#039;; The names of parameters are upper case letters, and may include digits and underline.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
localparam STATE_S = 3&amp;#039;d0;&lt;br /&gt;
localparam STATE_P = 3&amp;#039;d1;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* The state values must be distinct! Otherwise you may end with a state having multiple names.&lt;br /&gt;
* Local parameter declarations are the first statements after the module interface. The order of statements for a module should be the following:&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;module&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# interface declaration (the list of inputs and outputs)&lt;br /&gt;
# localparam declarations&lt;br /&gt;
# internal wire and reg declarations&lt;br /&gt;
# description (structural with instantiations or behavioral with always processes and continuos assignments)&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;endmodule&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
All transitions are grouped in a single sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process, with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that groups transitions by current state.&lt;br /&gt;
The combinational function that computes the next state (the transition function) is implicitly described by the case statement and the statements for each case.&lt;br /&gt;
The state is updated sequentially because the &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is sensitive only to clock edges.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~rst)&lt;br /&gt;
        state &amp;lt;= STATE_S;&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
            if(a)&lt;br /&gt;
                state &amp;lt;= STATE_P;&lt;br /&gt;
            else if(b)&lt;br /&gt;
                state &amp;lt;= STATE_T;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state; // stays in the same state&lt;br /&gt;
        end&lt;br /&gt;
        STATE_P: begin&lt;br /&gt;
            if(b)&lt;br /&gt;
                state &amp;lt;= STATE_R;&lt;br /&gt;
            else&lt;br /&gt;
                state &amp;lt;= state;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their transitions&lt;br /&gt;
        default:&lt;br /&gt;
                state &amp;lt;= STATE_S; // from undefined states jump to the initial state&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variable for the state must be declared with the required number of bits, which is the same as the number of bits declared for each value of the state value parameters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since Moore outputs depend only on state they are described either through continuous assignments, or inside a combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
&lt;br /&gt;
If continuous assignments are used, the outputs are assigned logic expressions based on state:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
assign y1 = (state == R);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The combinational &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process is preferred when state values are easier to group by states:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(*) begin&lt;br /&gt;
    case(state)&lt;br /&gt;
    STATE_S: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_P: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    STATE_R: {y1, y2} = 2&amp;#039;b10;&lt;br /&gt;
    // other states with their outputs&lt;br /&gt;
    default: {y1, y2} = 2&amp;#039;b00;&lt;br /&gt;
    endcase&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the combinational process are done with the blocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Create a new project in a new folder&lt;br /&gt;
* Design the automaton as the top level design entity&lt;br /&gt;
* Design a testbench with the input signals generated as in the figure below.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure.&lt;br /&gt;
&amp;lt;!--* To implement the design assign KEY0, SW1 and SW2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LEDR1 and LEDR2 to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.--&amp;gt;&lt;br /&gt;
* To implement the design assign BTN0, BTN1 and BTN2 to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; inputs. Assign LED1 and LED2  to &amp;#039;&amp;#039;&amp;#039;y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;y2&amp;#039;&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_moore_wave.png]]&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an automaton that determines the order in which two buttons are pressed. The buttons generate logic signals, &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;, that are &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; if the corresponding button is pressed. The automaton has two logic outputs, &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y1&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;Y2&amp;#039;&amp;#039;&amp;#039; is asserted if &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; was pressed first, followed by &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039;. The outputs are asserted for ONLY ONE CLOCK CYCLE. The reset is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex1.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mealy FSM ===&lt;br /&gt;
&lt;br /&gt;
The circuit may be designed as a delayed Mealy FSM, with fewer states than if designed as a Moore FSM. In the graph below only the transitions to different states are shown. If their conditions are not met, the FSM stays in the current state, whatever it is. In the final state, &amp;#039;&amp;#039;&amp;#039;V&amp;#039;&amp;#039;&amp;#039;, the outputs are active for one clock cycle, depending on the input values that determined the transition to it. All other transitions are with outputs &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;. From any state, the reset forces the FSM to its initial state, &amp;#039;&amp;#039;&amp;#039;S&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy.png]]&lt;br /&gt;
&lt;br /&gt;
=== State transitions ===&lt;br /&gt;
&lt;br /&gt;
The transition function is described in the same way as for the Moore FSM. However, there are only 4 states, therefore the &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039; variable and the state value parameters have only 2 bits. The &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; inside the sequential process for transitions has also fewer cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Outputs ===&lt;br /&gt;
&lt;br /&gt;
Since the delayed Mealy outputs depend both on the previous state AND the previous input values (at the last transition) they are described inside a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process.&lt;br /&gt;
The outputs must be declared of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~rst) begin&lt;br /&gt;
        y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        case(state)&lt;br /&gt;
        STATE_S: begin&lt;br /&gt;
             y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
             y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        // other states with their outputs&lt;br /&gt;
        default: begin&lt;br /&gt;
            y1 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
            y2 &amp;lt;= 1&amp;#039;b0;&lt;br /&gt;
        end&lt;br /&gt;
        endcase&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* All assignments inside the sequential process are done with the nonblocking assignment operator, &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;lt;=&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* Don&amp;#039;t forget the &amp;#039;&amp;#039;&amp;#039;default&amp;#039;&amp;#039;&amp;#039; case!&lt;br /&gt;
&lt;br /&gt;
=== Requirements ===&lt;br /&gt;
* Use the same project as before&lt;br /&gt;
* Redesign the automaton as a Mealy FSM&lt;br /&gt;
* The testbench should not be modified.&lt;br /&gt;
* Run simulation. If the design is correct the outputs should appear as in the figure below. The differences from the previous waveform are in the state values and output pulses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_mealy_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Design an FSM for an automatic chocolate selling machine, which delivers chocolates at 2.5&amp;amp;#8364;. The machine accepts only coins of 50 cents and 1 euro, and does not return any change. When the amount of 2.5&amp;amp;#8364; or more is reached, the machine delivers a chocolate and will resume the process, subtracting 2.5&amp;amp;#8364; from the current amount. The process resumes with whatever amount remains after delivering, which may be 0 or 0.5&amp;amp;#8364;. &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The inputs are the digital signals &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When the user inserts a valid coin, the corresponding input, &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039;, is asserted and stays &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; for some clock cycles. The FSM considers the coin validated only after the corresponding input goes back to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The output is a digital signal, &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039;, that is asserted when the amount reaches or surpasses 2.5&amp;amp;#8364; and stays asserted until the input &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is asserted. The FSM resumes the process after &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; is deasserted.&lt;br /&gt;
&lt;br /&gt;
The internal states of the FSM may be equivalent to the current amount, 0, 0.5, 1, 1.5, 2, 2.5 and 3 euros.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Implement the FSM for the automatic chocolate selling machine using three push buttons for &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039; inputs &amp;lt;!--(the pushbutton generates &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; when pressed)--&amp;gt;, an LED to indicate the &amp;#039;&amp;#039;&amp;#039;deliver&amp;#039;&amp;#039;&amp;#039; output, and another pushbutton for the reset (that resets the amount to 0).&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Hint&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To easy the implementation and keep the FSM as simple as possible, the inputs &amp;#039;&amp;#039;&amp;#039;cents&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ack&amp;#039;&amp;#039;&amp;#039;, which are pulses of arbitrary length (that may last hundreds of thousands of clock cycles!), may be transformed to one cycle pulses, asserted during the clock cycle that follows the original pulse. The FSM uses these one clock cycle pulses to compute the transitions.&lt;br /&gt;
&lt;br /&gt;
As an example, the &amp;#039;&amp;#039;&amp;#039;euro&amp;#039;&amp;#039;&amp;#039; input is transformed to &amp;#039;&amp;#039;&amp;#039;euro_p&amp;#039;&amp;#039;&amp;#039; internal signal,  whose pulses last one clock cycle. These internal signal is further used by the FSM.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
logic euro_d; // delayed version of the euro input&lt;br /&gt;
logic euro_p; // one clock cycle pulse to be used by the FSM&lt;br /&gt;
...&lt;br /&gt;
always @(posedge clk) euro_d &amp;lt;= euro; // euro_d is the copy of euro input, shifted one clock cycle&lt;br /&gt;
assign euro_p = euro_d &amp;amp; ~euro; // euro_p is asserted one clock cycle after euro pulse has ended&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl9_ex3wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl9_mealy_wave.png&amp;diff=8060</id>
		<title>Fișier:Appl9 mealy wave.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl9_mealy_wave.png&amp;diff=8060"/>
		<updated>2025-04-29T22:54:13Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a încărcat o versiune nouă pentru Fișier:Appl9 mealy wave.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Digital_Integrated_Circuits_(lab)&amp;diff=8059</id>
		<title>Digital Integrated Circuits (lab)</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Digital_Integrated_Circuits_(lab)&amp;diff=8059"/>
		<updated>2025-04-29T22:46:10Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;# [[Applications 1 | Laboratory 1]]&lt;br /&gt;
# [[Applications 2 | Laboratory 2]]&lt;br /&gt;
# [[Applications 3 | Laboratory 3]]&lt;br /&gt;
# [[Applications 4 | Laboratory 4]]&lt;br /&gt;
# [[Applications 5 | Laboratory 5]]&lt;br /&gt;
# Test 1&lt;br /&gt;
# [[Applications 6 | Laboratory 6]]&lt;br /&gt;
# [[Applications 7 | Laboratory 7]]&lt;br /&gt;
# [[Applications 8 | Laboratory 8]]&lt;br /&gt;
# [[Applications 9 | Laboratory 9]]&lt;br /&gt;
&lt;br /&gt;
== Verilog Tutorials ==&lt;br /&gt;
* [http://www.asic-world.com/verilog/veritut.html Verilog Tutorial] from ASIC World&lt;br /&gt;
* [http://chipverify.com/verilog/verilog-tutorial Verilog Tutorial] from Chip Verify&lt;br /&gt;
* [http://www.emmelmann.org/Library/Tutorials/docs/verilog_ref_guide/vlog_ref_top.html Verilog reference guide]&lt;br /&gt;
&lt;br /&gt;
== Software ==&lt;br /&gt;
* [https://www.xilinx.com/products/design-tools/vivado/vivado-ml.html Vivado™ ML Standard Edition free]&lt;br /&gt;
* [https://wiki.dcae.pub.ro/images/2/24/VivadoNewProjectTutorial.pdf Vivado New Project Tutorial]&lt;br /&gt;
&lt;br /&gt;
== Hardware ==&lt;br /&gt;
[https://www.realdigital.org/hardware/boolean Boolean Board] from RealDigital&lt;br /&gt;
* [https://www.realdigital.org/doc/02013cd17602c8af749f00561f88ae21 Boolean Board - user manual]&lt;br /&gt;
* [[Boolean Board - Pinout]]&lt;br /&gt;
To implement a circuit on Boolean Board you should select the following target device in Project Settings: &amp;#039;&amp;#039;&amp;#039;xc7s50csga324-1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
* [https://dpoauwgwqsy2x.cloudfront.net/Download/pynqz2_user_manual_v1_0.pdf Pynq-Z2 - user manual]&lt;br /&gt;
* [[Pynq-Z2 - Pinout]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8058</id>
		<title>Fișier:Zlab8 ram.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8058"/>
		<updated>2025-04-21T22:02:23Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a readus Fișier:Zlab8 ram.png la o versiune mai veche&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8057</id>
		<title>Fișier:Zlab8 ram.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8057"/>
		<updated>2025-04-21T22:00:50Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a readus Fișier:Zlab8 ram.png la o versiune mai veche&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8056</id>
		<title>Fișier:Zlab8 ram.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8056"/>
		<updated>2025-04-21T22:00:13Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a încărcat o versiune nouă pentru Fișier:Zlab8 ram.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl8_ram.png&amp;diff=8055</id>
		<title>Fișier:Appl8 ram.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl8_ram.png&amp;diff=8055"/>
		<updated>2025-04-21T21:59:17Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a readus Fișier:Appl8 ram.png la o versiune mai veche&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl8_ram.png&amp;diff=8054</id>
		<title>Fișier:Appl8 ram.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Appl8_ram.png&amp;diff=8054"/>
		<updated>2025-04-21T21:59:08Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: Zhascsi a încărcat o versiune nouă pentru Fișier:Appl8 ram.png&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8053</id>
		<title>Applications 8</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8053"/>
		<updated>2025-04-21T21:56:52Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* RAM - Read-Write Memory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A random access memory is an address-based data storage block, with a highly regular structure, which may be logically defined either as a bi-dimensional array of bits, or a uni-dimensional list of data words. Each row of the array, or each item of the list has a unique address. The addresses form a contiguous set of integers, starting from zero and running up to the highest address which defines the size of the address space, usually a power of 2. The memory capacity is therefore the number of address locations, N, times the width of the memory location (or of the memory word), W. These two numbers are the main parameters of any memory:&lt;br /&gt;
* N - the number of address locations, also the number of words;&lt;br /&gt;
* W - the number of bits for each location, or the data word width;&lt;br /&gt;
The storage capacity is N x W bits, but the memory capacity may also be given in bytes.&lt;br /&gt;
&lt;br /&gt;
Being a vector of words or a bidimensional array of bits, the memory could be described using a single bidimensional variable, usually declared as a vector of multibit &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; items. A memory array of N locations, with W bits per location is declared as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
logic [W-1:0] memory [0:N-1];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* the width of the word is declared as usual, with msb as the left index, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[msb:0]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* the length of the memory is declared after the variable name, and starts with 0, the right index being the highest index of the vector, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[0:endindex]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two kinds of memories:&lt;br /&gt;
* read-only memories&lt;br /&gt;
* writable memories&lt;br /&gt;
&lt;br /&gt;
Memories can also be classified by their synchronous or asynchronous character:&lt;br /&gt;
* asynchronous - read data is immediately available at the output, write is performed as into a latch&lt;br /&gt;
* synchronous  - all operations are synchronized by one of the clock edges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
=== ROM - Read-Only Memory ===&lt;br /&gt;
&lt;br /&gt;
It may be described as a purely combinational logic circuit, whose output (data) is a logic function of its input (address). The transcoder from [[Applications 4#transcoder|Applications 4]] is nothing else than a ROM, with &amp;#039;&amp;#039;&amp;#039;value&amp;#039;&amp;#039;&amp;#039; input acting as an address that selects a particular location from where data is read and sent out as &amp;#039;&amp;#039;&amp;#039;seg&amp;#039;&amp;#039;&amp;#039;. There it was described behaviorally with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that had a branch for each combination of input value bits.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_rom.png]]&lt;br /&gt;
&lt;br /&gt;
A more flexible description employs a single memory array, declared as a vector of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variables, and a statement that assigns the selected (addressed) element of the memory vector to the memory output.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign dout = memory[addr];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization task ===&lt;br /&gt;
&lt;br /&gt;
Memory arrays may be initialized with predefined values from a text file using verilog system tasks &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; reads predefined values given in binary, while &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; is used if values are given in hexadecimal. These verilog system tasks have two mandatory arguments and two optional arguments, in the following order:&lt;br /&gt;
# initialization filename, given as a string (between double quotes)&lt;br /&gt;
# the memory array variable name&lt;br /&gt;
# the start address (optional)&lt;br /&gt;
# the end address  (optional)&lt;br /&gt;
If the start and end addresses are not given, the memory array is initialized from the first address (address zero) until the last address or until the end of initialization file is reached.&lt;br /&gt;
&lt;br /&gt;
If the memory initialization file, &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;, resides in the project&amp;#039;s folder, and the memory array to be initialized is &amp;#039;&amp;#039;&amp;#039;mem&amp;#039;&amp;#039;&amp;#039; inside the memory instance &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the testbench may initialize the memory right at the beginning of the simulation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, dut.mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the initialization is intended to be done for the implemented memory, the initialization process must be placed inside the memory module, in which case the memory array name is just the variable array to be initialized:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The filename, the first argument of &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; tasks, may be given either relative to the simulation folder (as in the previous two examples), or as the complete absolute filename, with the pathname starting from the linux root, &amp;#039;&amp;#039;&amp;#039;/&amp;#039;&amp;#039;&amp;#039;, for example &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;quot;/home/student/rom/meminit.txt&amp;quot;&amp;lt;/syntaxhighlight&amp;gt; if &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039; file resides in the project&amp;#039;s folder &amp;#039;&amp;#039;rom&amp;#039;&amp;#039;, created in the student&amp;#039;s home directory &amp;#039;&amp;#039;/home/student&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization file ===&lt;br /&gt;
&lt;br /&gt;
The initialization file is a text file with values written in binary (using &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; symbols) or in hexadecimal (using digits and letters &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;#039;f&amp;#039;&amp;#039;&amp;#039;, either uppercase of lowercase). Values are separated by white spaces, tabs or newlines. Comments (started with &amp;#039;&amp;#039;&amp;#039;//&amp;#039;&amp;#039;&amp;#039; as in verilog) are ignored. An example of a binary initialization file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in binary text format&lt;br /&gt;
00000011&lt;br /&gt;
00001111&lt;br /&gt;
00111111&lt;br /&gt;
11111111&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same initialization sequence, but given as a hexadecimal text file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in hexadecimal text format&lt;br /&gt;
03&lt;br /&gt;
0f&lt;br /&gt;
3f&lt;br /&gt;
ff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;rom.sv&amp;#039;&amp;#039; that describes a 16x8 ROM using a vector variable and a continuous assignment.&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;rom_tb&amp;#039;&amp;#039;&amp;#039;, that drives the input of &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; with a sequence of addresses from 0 to highest one. Run simulation.&lt;br /&gt;
* Open a text editor, write some values in binary format on successive lines, and save the file into the &amp;#039;&amp;#039;rom&amp;#039;&amp;#039; folder with the name &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;&lt;br /&gt;
* In the testbench module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Run simulation.&lt;br /&gt;
* Assign the segments of a display to &amp;#039;&amp;#039;&amp;#039;dout&amp;#039;&amp;#039;&amp;#039;, and 4 switches, from SW3 to SW0, to &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* In the &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Pay atention how its second argument is given - the array variable is now directly accesible. Compile the project and program the FPGA.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
=== RAM - Read-Write Memory ===&lt;br /&gt;
&lt;br /&gt;
A synchronous memory reacts only on clock edges. The addresses are sampled at clock edges, the output changes also on clock edges, and the write is performed on clock edges too.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Zlab8_ram.png]]&lt;br /&gt;
&lt;br /&gt;
The FPGA has dedicated blocks for memory implementation, the so called &amp;#039;&amp;#039;ram blocks&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;ramblocks&amp;#039;&amp;#039;. In order for the synthesis to map a memory description to a &amp;#039;&amp;#039;ramblock&amp;#039;&amp;#039;, the sequential description must conform to a template:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~we)                   // if write is enabled,&lt;br /&gt;
        mem[addr] &amp;lt;= wdata;   //     update the addressed location to wdata value&lt;br /&gt;
    rdata &amp;lt;= mem[addr];       // data read from the addressed location is transferred to the output&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;, in a new folder &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;ram.sv&amp;#039;&amp;#039; that describes a 16x4 RAM using a vector variable and a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process that assigns to &amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039; output the value of the memory location selected by &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. In the same process update the same location with &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; value if &amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039; input is active (active at &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;ram_tb&amp;#039;&amp;#039;&amp;#039;, which writes some data to four addresses, and then reads the same addresses in the same order, as indicated in the waveforms below. Don&amp;#039;t forget the clock! Run simulation and check that the written data are correctly read afterwards.&lt;br /&gt;
* Assign LEDs to the output, switches to the &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; inputs, a push button to the write enable input, and the board&amp;#039;s source clock to &amp;#039;&amp;#039;&amp;#039;clk&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* Compile, program the FPGA and test the RAM. Set switches to an address and a data value, then briefly push the write enable button. The LEDs will change accordingly. Change the address and data and push again the write enable button. Change back the address switches and see if the previously written configuration reappears on LED.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Zlab8_ram_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
=== Programmable display ===&lt;br /&gt;
&lt;br /&gt;
These application employs a dual port synchronous RAM. One port, the &amp;#039;&amp;#039;read port&amp;#039;&amp;#039;, is used to read data (&amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039;) from the memory, using the read address &amp;#039;&amp;#039;&amp;#039;raddr&amp;#039;&amp;#039;&amp;#039;. The other, the &amp;#039;&amp;#039;write port&amp;#039;&amp;#039;, accesses the same memory array, but using another address, &amp;#039;&amp;#039;&amp;#039;waddr&amp;#039;&amp;#039;&amp;#039;, to write &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; into. The write operation is performed only if the write enable pin (&amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039;) is active (here it is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
The data read from RAM is translated by the ROM into a 7 bit combination for the digit display. The RAM memory is read in sequence, address by address, changing the address at around each second. The sequence of 4 bit addresses are generated by a counter from which four suitable bits are chosen such that the rate of change is around 1 Hz.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ramrom.png]]&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; file from Exercise 1 into this folder.&lt;br /&gt;
* Add an initialization file to this folder, and ensures that &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; initialization tasks reads it from the correct location.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;ram.v&amp;#039;&amp;#039; from from Exercise 2 into this folder. Change &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039; description to a dual RAM.&lt;br /&gt;
* Add &amp;#039;&amp;#039;&amp;#039;cnt32.v&amp;#039;&amp;#039;&amp;#039; to this project (you may reuse the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module or file from the previous Application).&lt;br /&gt;
* Create the top-level design file, &amp;#039;&amp;#039;ramrom.v&amp;#039;&amp;#039;&amp;#039;, and instantiate the modules and connect them as indicated.&lt;br /&gt;
* Assign swithces, push button, clock source and digit display as shown.&lt;br /&gt;
* Compile, program the FPGA, and play with the board switches and buttons.&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram_wave.png&amp;diff=8052</id>
		<title>Fișier:Zlab8 ram wave.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram_wave.png&amp;diff=8052"/>
		<updated>2025-04-21T21:56:30Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8051</id>
		<title>Applications 8</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8051"/>
		<updated>2025-04-21T21:42:09Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* RAM - Read-Write Memory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A random access memory is an address-based data storage block, with a highly regular structure, which may be logically defined either as a bi-dimensional array of bits, or a uni-dimensional list of data words. Each row of the array, or each item of the list has a unique address. The addresses form a contiguous set of integers, starting from zero and running up to the highest address which defines the size of the address space, usually a power of 2. The memory capacity is therefore the number of address locations, N, times the width of the memory location (or of the memory word), W. These two numbers are the main parameters of any memory:&lt;br /&gt;
* N - the number of address locations, also the number of words;&lt;br /&gt;
* W - the number of bits for each location, or the data word width;&lt;br /&gt;
The storage capacity is N x W bits, but the memory capacity may also be given in bytes.&lt;br /&gt;
&lt;br /&gt;
Being a vector of words or a bidimensional array of bits, the memory could be described using a single bidimensional variable, usually declared as a vector of multibit &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; items. A memory array of N locations, with W bits per location is declared as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
logic [W-1:0] memory [0:N-1];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* the width of the word is declared as usual, with msb as the left index, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[msb:0]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* the length of the memory is declared after the variable name, and starts with 0, the right index being the highest index of the vector, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[0:endindex]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two kinds of memories:&lt;br /&gt;
* read-only memories&lt;br /&gt;
* writable memories&lt;br /&gt;
&lt;br /&gt;
Memories can also be classified by their synchronous or asynchronous character:&lt;br /&gt;
* asynchronous - read data is immediately available at the output, write is performed as into a latch&lt;br /&gt;
* synchronous  - all operations are synchronized by one of the clock edges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
=== ROM - Read-Only Memory ===&lt;br /&gt;
&lt;br /&gt;
It may be described as a purely combinational logic circuit, whose output (data) is a logic function of its input (address). The transcoder from [[Applications 4#transcoder|Applications 4]] is nothing else than a ROM, with &amp;#039;&amp;#039;&amp;#039;value&amp;#039;&amp;#039;&amp;#039; input acting as an address that selects a particular location from where data is read and sent out as &amp;#039;&amp;#039;&amp;#039;seg&amp;#039;&amp;#039;&amp;#039;. There it was described behaviorally with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that had a branch for each combination of input value bits.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_rom.png]]&lt;br /&gt;
&lt;br /&gt;
A more flexible description employs a single memory array, declared as a vector of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variables, and a statement that assigns the selected (addressed) element of the memory vector to the memory output.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign dout = memory[addr];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization task ===&lt;br /&gt;
&lt;br /&gt;
Memory arrays may be initialized with predefined values from a text file using verilog system tasks &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; reads predefined values given in binary, while &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; is used if values are given in hexadecimal. These verilog system tasks have two mandatory arguments and two optional arguments, in the following order:&lt;br /&gt;
# initialization filename, given as a string (between double quotes)&lt;br /&gt;
# the memory array variable name&lt;br /&gt;
# the start address (optional)&lt;br /&gt;
# the end address  (optional)&lt;br /&gt;
If the start and end addresses are not given, the memory array is initialized from the first address (address zero) until the last address or until the end of initialization file is reached.&lt;br /&gt;
&lt;br /&gt;
If the memory initialization file, &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;, resides in the project&amp;#039;s folder, and the memory array to be initialized is &amp;#039;&amp;#039;&amp;#039;mem&amp;#039;&amp;#039;&amp;#039; inside the memory instance &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the testbench may initialize the memory right at the beginning of the simulation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, dut.mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the initialization is intended to be done for the implemented memory, the initialization process must be placed inside the memory module, in which case the memory array name is just the variable array to be initialized:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The filename, the first argument of &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; tasks, may be given either relative to the simulation folder (as in the previous two examples), or as the complete absolute filename, with the pathname starting from the linux root, &amp;#039;&amp;#039;&amp;#039;/&amp;#039;&amp;#039;&amp;#039;, for example &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;quot;/home/student/rom/meminit.txt&amp;quot;&amp;lt;/syntaxhighlight&amp;gt; if &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039; file resides in the project&amp;#039;s folder &amp;#039;&amp;#039;rom&amp;#039;&amp;#039;, created in the student&amp;#039;s home directory &amp;#039;&amp;#039;/home/student&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization file ===&lt;br /&gt;
&lt;br /&gt;
The initialization file is a text file with values written in binary (using &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; symbols) or in hexadecimal (using digits and letters &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;#039;f&amp;#039;&amp;#039;&amp;#039;, either uppercase of lowercase). Values are separated by white spaces, tabs or newlines. Comments (started with &amp;#039;&amp;#039;&amp;#039;//&amp;#039;&amp;#039;&amp;#039; as in verilog) are ignored. An example of a binary initialization file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in binary text format&lt;br /&gt;
00000011&lt;br /&gt;
00001111&lt;br /&gt;
00111111&lt;br /&gt;
11111111&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same initialization sequence, but given as a hexadecimal text file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in hexadecimal text format&lt;br /&gt;
03&lt;br /&gt;
0f&lt;br /&gt;
3f&lt;br /&gt;
ff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;rom.sv&amp;#039;&amp;#039; that describes a 16x8 ROM using a vector variable and a continuous assignment.&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;rom_tb&amp;#039;&amp;#039;&amp;#039;, that drives the input of &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; with a sequence of addresses from 0 to highest one. Run simulation.&lt;br /&gt;
* Open a text editor, write some values in binary format on successive lines, and save the file into the &amp;#039;&amp;#039;rom&amp;#039;&amp;#039; folder with the name &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;&lt;br /&gt;
* In the testbench module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Run simulation.&lt;br /&gt;
* Assign the segments of a display to &amp;#039;&amp;#039;&amp;#039;dout&amp;#039;&amp;#039;&amp;#039;, and 4 switches, from SW3 to SW0, to &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* In the &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Pay atention how its second argument is given - the array variable is now directly accesible. Compile the project and program the FPGA.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
=== RAM - Read-Write Memory ===&lt;br /&gt;
&lt;br /&gt;
A synchronous memory reacts only on clock edges. The addresses are sampled at clock edges, the output changes also on clock edges, and the write is performed on clock edges too.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: Zlab8_ram.png]]&lt;br /&gt;
&lt;br /&gt;
The FPGA has dedicated blocks for memory implementation, the so called &amp;#039;&amp;#039;ram blocks&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;ramblocks&amp;#039;&amp;#039;. In order for the synthesis to map a memory description to a &amp;#039;&amp;#039;ramblock&amp;#039;&amp;#039;, the sequential description must conform to a template:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~we)                   // if write is enabled,&lt;br /&gt;
        mem[addr] &amp;lt;= wdata;   //     update the addressed location to wdata value&lt;br /&gt;
    rdata &amp;lt;= mem[addr];       // data read from the addressed location is transferred to the output&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;, in a new folder &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;ram.sv&amp;#039;&amp;#039; that describes a 16x4 RAM using a vector variable and a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process that assigns to &amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039; output the value of the memory location selected by &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. In the same process update the same location with &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; value if &amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039; input is active (active at &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;ram_tb&amp;#039;&amp;#039;&amp;#039;, which writes some data to four addresses, and then reads the same addresses in the same order, as indicated in the waveforms below. Don&amp;#039;t forget the clock! Run simulation and check that the written data are correctly read afterwards.&lt;br /&gt;
* Assign LEDs to the output, switches to the &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; inputs, a push button to the write enable input, and the board&amp;#039;s source clock to &amp;#039;&amp;#039;&amp;#039;clk&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* Compile, program the FPGA and test the RAM. Set switches to an address and a data value, then briefly push the write enable button. The LEDs will change accordingly. Change the address and data and push again the write enable button. Change back the address switches and see if the previously written configuration reappears on LED.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ram_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
=== Programmable display ===&lt;br /&gt;
&lt;br /&gt;
These application employs a dual port synchronous RAM. One port, the &amp;#039;&amp;#039;read port&amp;#039;&amp;#039;, is used to read data (&amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039;) from the memory, using the read address &amp;#039;&amp;#039;&amp;#039;raddr&amp;#039;&amp;#039;&amp;#039;. The other, the &amp;#039;&amp;#039;write port&amp;#039;&amp;#039;, accesses the same memory array, but using another address, &amp;#039;&amp;#039;&amp;#039;waddr&amp;#039;&amp;#039;&amp;#039;, to write &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; into. The write operation is performed only if the write enable pin (&amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039;) is active (here it is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
The data read from RAM is translated by the ROM into a 7 bit combination for the digit display. The RAM memory is read in sequence, address by address, changing the address at around each second. The sequence of 4 bit addresses are generated by a counter from which four suitable bits are chosen such that the rate of change is around 1 Hz.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ramrom.png]]&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; file from Exercise 1 into this folder.&lt;br /&gt;
* Add an initialization file to this folder, and ensures that &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; initialization tasks reads it from the correct location.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;ram.v&amp;#039;&amp;#039; from from Exercise 2 into this folder. Change &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039; description to a dual RAM.&lt;br /&gt;
* Add &amp;#039;&amp;#039;&amp;#039;cnt32.v&amp;#039;&amp;#039;&amp;#039; to this project (you may reuse the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module or file from the previous Application).&lt;br /&gt;
* Create the top-level design file, &amp;#039;&amp;#039;ramrom.v&amp;#039;&amp;#039;&amp;#039;, and instantiate the modules and connect them as indicated.&lt;br /&gt;
* Assign swithces, push button, clock source and digit display as shown.&lt;br /&gt;
* Compile, program the FPGA, and play with the board switches and buttons.&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8050</id>
		<title>Fișier:Zlab8 ram.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab8_ram.png&amp;diff=8050"/>
		<updated>2025-04-21T21:41:40Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8049</id>
		<title>Applications 8</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8049"/>
		<updated>2025-04-21T21:37:22Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* Memory initialization task */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A random access memory is an address-based data storage block, with a highly regular structure, which may be logically defined either as a bi-dimensional array of bits, or a uni-dimensional list of data words. Each row of the array, or each item of the list has a unique address. The addresses form a contiguous set of integers, starting from zero and running up to the highest address which defines the size of the address space, usually a power of 2. The memory capacity is therefore the number of address locations, N, times the width of the memory location (or of the memory word), W. These two numbers are the main parameters of any memory:&lt;br /&gt;
* N - the number of address locations, also the number of words;&lt;br /&gt;
* W - the number of bits for each location, or the data word width;&lt;br /&gt;
The storage capacity is N x W bits, but the memory capacity may also be given in bytes.&lt;br /&gt;
&lt;br /&gt;
Being a vector of words or a bidimensional array of bits, the memory could be described using a single bidimensional variable, usually declared as a vector of multibit &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; items. A memory array of N locations, with W bits per location is declared as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
logic [W-1:0] memory [0:N-1];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* the width of the word is declared as usual, with msb as the left index, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[msb:0]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* the length of the memory is declared after the variable name, and starts with 0, the right index being the highest index of the vector, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[0:endindex]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two kinds of memories:&lt;br /&gt;
* read-only memories&lt;br /&gt;
* writable memories&lt;br /&gt;
&lt;br /&gt;
Memories can also be classified by their synchronous or asynchronous character:&lt;br /&gt;
* asynchronous - read data is immediately available at the output, write is performed as into a latch&lt;br /&gt;
* synchronous  - all operations are synchronized by one of the clock edges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
=== ROM - Read-Only Memory ===&lt;br /&gt;
&lt;br /&gt;
It may be described as a purely combinational logic circuit, whose output (data) is a logic function of its input (address). The transcoder from [[Applications 4#transcoder|Applications 4]] is nothing else than a ROM, with &amp;#039;&amp;#039;&amp;#039;value&amp;#039;&amp;#039;&amp;#039; input acting as an address that selects a particular location from where data is read and sent out as &amp;#039;&amp;#039;&amp;#039;seg&amp;#039;&amp;#039;&amp;#039;. There it was described behaviorally with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that had a branch for each combination of input value bits.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_rom.png]]&lt;br /&gt;
&lt;br /&gt;
A more flexible description employs a single memory array, declared as a vector of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variables, and a statement that assigns the selected (addressed) element of the memory vector to the memory output.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign dout = memory[addr];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization task ===&lt;br /&gt;
&lt;br /&gt;
Memory arrays may be initialized with predefined values from a text file using verilog system tasks &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; reads predefined values given in binary, while &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; is used if values are given in hexadecimal. These verilog system tasks have two mandatory arguments and two optional arguments, in the following order:&lt;br /&gt;
# initialization filename, given as a string (between double quotes)&lt;br /&gt;
# the memory array variable name&lt;br /&gt;
# the start address (optional)&lt;br /&gt;
# the end address  (optional)&lt;br /&gt;
If the start and end addresses are not given, the memory array is initialized from the first address (address zero) until the last address or until the end of initialization file is reached.&lt;br /&gt;
&lt;br /&gt;
If the memory initialization file, &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;, resides in the project&amp;#039;s folder, and the memory array to be initialized is &amp;#039;&amp;#039;&amp;#039;mem&amp;#039;&amp;#039;&amp;#039; inside the memory instance &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the testbench may initialize the memory right at the beginning of the simulation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, dut.mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the initialization is intended to be done for the implemented memory, the initialization process must be placed inside the memory module, in which case the memory array name is just the variable array to be initialized:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The filename, the first argument of &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; tasks, may be given either relative to the simulation folder (as in the previous two examples), or as the complete absolute filename, with the pathname starting from the linux root, &amp;#039;&amp;#039;&amp;#039;/&amp;#039;&amp;#039;&amp;#039;, for example &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;quot;/home/student/rom/meminit.txt&amp;quot;&amp;lt;/syntaxhighlight&amp;gt; if &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039; file resides in the project&amp;#039;s folder &amp;#039;&amp;#039;rom&amp;#039;&amp;#039;, created in the student&amp;#039;s home directory &amp;#039;&amp;#039;/home/student&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization file ===&lt;br /&gt;
&lt;br /&gt;
The initialization file is a text file with values written in binary (using &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; symbols) or in hexadecimal (using digits and letters &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;#039;f&amp;#039;&amp;#039;&amp;#039;, either uppercase of lowercase). Values are separated by white spaces, tabs or newlines. Comments (started with &amp;#039;&amp;#039;&amp;#039;//&amp;#039;&amp;#039;&amp;#039; as in verilog) are ignored. An example of a binary initialization file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in binary text format&lt;br /&gt;
00000011&lt;br /&gt;
00001111&lt;br /&gt;
00111111&lt;br /&gt;
11111111&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same initialization sequence, but given as a hexadecimal text file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in hexadecimal text format&lt;br /&gt;
03&lt;br /&gt;
0f&lt;br /&gt;
3f&lt;br /&gt;
ff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;rom.sv&amp;#039;&amp;#039; that describes a 16x8 ROM using a vector variable and a continuous assignment.&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;rom_tb&amp;#039;&amp;#039;&amp;#039;, that drives the input of &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; with a sequence of addresses from 0 to highest one. Run simulation.&lt;br /&gt;
* Open a text editor, write some values in binary format on successive lines, and save the file into the &amp;#039;&amp;#039;rom&amp;#039;&amp;#039; folder with the name &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;&lt;br /&gt;
* In the testbench module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Run simulation.&lt;br /&gt;
* Assign the segments of a display to &amp;#039;&amp;#039;&amp;#039;dout&amp;#039;&amp;#039;&amp;#039;, and 4 switches, from SW3 to SW0, to &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* In the &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Pay atention how its second argument is given - the array variable is now directly accesible. Compile the project and program the FPGA.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
=== RAM - Read-Write Memory ===&lt;br /&gt;
&lt;br /&gt;
A synchronous memory reacts only on clock edges. The addresses are sampled at clock edges, the output changes also on clock edges, and the write is performed on clock edges too.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ram.png]]&lt;br /&gt;
&lt;br /&gt;
The FPGA has dedicated blocks for memory implementation, the so called &amp;#039;&amp;#039;ram blocks&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;ramblocks&amp;#039;&amp;#039;. In order for the synthesis to map a memory description to a &amp;#039;&amp;#039;ramblock&amp;#039;&amp;#039;, the sequential description must conform to a template:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~we)                   // if write is enabled,&lt;br /&gt;
        mem[addr] &amp;lt;= wdata;   //     update the addressed location to wdata value&lt;br /&gt;
    rdata &amp;lt;= mem[addr];       // data read from the addressed location is transferred to the output&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;, in a new folder &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;ram.sv&amp;#039;&amp;#039; that describes a 16x4 RAM using a vector variable and a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process that assigns to &amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039; output the value of the memory location selected by &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. In the same process update the same location with &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; value if &amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039; input is active (active at &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;ram_tb&amp;#039;&amp;#039;&amp;#039;, which writes some data to four addresses, and then reads the same addresses in the same order, as indicated in the waveforms below. Don&amp;#039;t forget the clock! Run simulation and check that the written data are correctly read afterwards.&lt;br /&gt;
* Assign LEDs to the output, switches to the &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; inputs, a push button to the write enable input, and the board&amp;#039;s source clock to &amp;#039;&amp;#039;&amp;#039;clk&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* Compile, program the FPGA and test the RAM. Set switches to an address and a data value, then briefly push the write enable button. The LEDs will change accordingly. Change the address and data and push again the write enable button. Change back the address switches and see if the previously written configuration reappears on LED.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ram_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
=== Programmable display ===&lt;br /&gt;
&lt;br /&gt;
These application employs a dual port synchronous RAM. One port, the &amp;#039;&amp;#039;read port&amp;#039;&amp;#039;, is used to read data (&amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039;) from the memory, using the read address &amp;#039;&amp;#039;&amp;#039;raddr&amp;#039;&amp;#039;&amp;#039;. The other, the &amp;#039;&amp;#039;write port&amp;#039;&amp;#039;, accesses the same memory array, but using another address, &amp;#039;&amp;#039;&amp;#039;waddr&amp;#039;&amp;#039;&amp;#039;, to write &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; into. The write operation is performed only if the write enable pin (&amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039;) is active (here it is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
The data read from RAM is translated by the ROM into a 7 bit combination for the digit display. The RAM memory is read in sequence, address by address, changing the address at around each second. The sequence of 4 bit addresses are generated by a counter from which four suitable bits are chosen such that the rate of change is around 1 Hz.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ramrom.png]]&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; file from Exercise 1 into this folder.&lt;br /&gt;
* Add an initialization file to this folder, and ensures that &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; initialization tasks reads it from the correct location.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;ram.v&amp;#039;&amp;#039; from from Exercise 2 into this folder. Change &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039; description to a dual RAM.&lt;br /&gt;
* Add &amp;#039;&amp;#039;&amp;#039;cnt32.v&amp;#039;&amp;#039;&amp;#039; to this project (you may reuse the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module or file from the previous Application).&lt;br /&gt;
* Create the top-level design file, &amp;#039;&amp;#039;ramrom.v&amp;#039;&amp;#039;&amp;#039;, and instantiate the modules and connect them as indicated.&lt;br /&gt;
* Assign swithces, push button, clock source and digit display as shown.&lt;br /&gt;
* Compile, program the FPGA, and play with the board switches and buttons.&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8048</id>
		<title>Applications 8</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8048"/>
		<updated>2025-04-21T21:36:48Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A random access memory is an address-based data storage block, with a highly regular structure, which may be logically defined either as a bi-dimensional array of bits, or a uni-dimensional list of data words. Each row of the array, or each item of the list has a unique address. The addresses form a contiguous set of integers, starting from zero and running up to the highest address which defines the size of the address space, usually a power of 2. The memory capacity is therefore the number of address locations, N, times the width of the memory location (or of the memory word), W. These two numbers are the main parameters of any memory:&lt;br /&gt;
* N - the number of address locations, also the number of words;&lt;br /&gt;
* W - the number of bits for each location, or the data word width;&lt;br /&gt;
The storage capacity is N x W bits, but the memory capacity may also be given in bytes.&lt;br /&gt;
&lt;br /&gt;
Being a vector of words or a bidimensional array of bits, the memory could be described using a single bidimensional variable, usually declared as a vector of multibit &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; items. A memory array of N locations, with W bits per location is declared as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
logic [W-1:0] memory [0:N-1];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* the width of the word is declared as usual, with msb as the left index, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[msb:0]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* the length of the memory is declared after the variable name, and starts with 0, the right index being the highest index of the vector, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[0:endindex]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two kinds of memories:&lt;br /&gt;
* read-only memories&lt;br /&gt;
* writable memories&lt;br /&gt;
&lt;br /&gt;
Memories can also be classified by their synchronous or asynchronous character:&lt;br /&gt;
* asynchronous - read data is immediately available at the output, write is performed as into a latch&lt;br /&gt;
* synchronous  - all operations are synchronized by one of the clock edges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
=== ROM - Read-Only Memory ===&lt;br /&gt;
&lt;br /&gt;
It may be described as a purely combinational logic circuit, whose output (data) is a logic function of its input (address). The transcoder from [[Applications 4#transcoder|Applications 4]] is nothing else than a ROM, with &amp;#039;&amp;#039;&amp;#039;value&amp;#039;&amp;#039;&amp;#039; input acting as an address that selects a particular location from where data is read and sent out as &amp;#039;&amp;#039;&amp;#039;seg&amp;#039;&amp;#039;&amp;#039;. There it was described behaviorally with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that had a branch for each combination of input value bits.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_rom.png]]&lt;br /&gt;
&lt;br /&gt;
A more flexible description employs a single memory array, declared as a vector of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variables, and a statement that assigns the selected (addressed) element of the memory vector to the memory output.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign dout = memory[addr];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization task ===&lt;br /&gt;
&lt;br /&gt;
Memory arrays may be initialized with predefined values from a text file using verilog system tasks &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; reads predefined values given in binary, while &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; is used if values are given in hexadecimal. These verilog system tasks have two mandatory arguments and two optional arguments, in the following order:&lt;br /&gt;
# initialization filename, given as a string (between double quotes)&lt;br /&gt;
# the memory array variable name&lt;br /&gt;
# the start address (optional)&lt;br /&gt;
# the end address  (optional)&lt;br /&gt;
If the start and end addresses are not given, the memory array is initialized from the first address (address zero) until the last address or until the end of initialization file is reached.&lt;br /&gt;
&lt;br /&gt;
If the memory initialization file, &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;, resides in the project&amp;#039;s folder, and the memory array to be initialized is &amp;#039;&amp;#039;&amp;#039;mem&amp;#039;&amp;#039;&amp;#039; inside the memory instance &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the testbench may initialize the memory right at the beginning of the simulation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, dut.mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the initialization is intended to be done for the implemented memory, the initialization process must be placed inside the memory module, in which case the memory array name is just the variable array to be initialized:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The filename, the first argument of &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; tasks, may be given either relative to the simulation folder (as in the previous two examples), or as the complete absolute filename, with the pathname starting from the linux root, &amp;#039;&amp;#039;&amp;#039;/&amp;#039;&amp;#039;&amp;#039;, for example &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;quot;/home/student/rom/meminit.txt&amp;quot;&amp;lt;/syntaxhighlight&amp;gt; if &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039; file resides in the project&amp;#039;s folder &amp;#039;&amp;#039;rom&amp;#039;&amp;#039;, created in the student&amp;#039;s home directory &amp;#039;&amp;#039;/home/student&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization file ===&lt;br /&gt;
&lt;br /&gt;
The initialization file is a text file with values written in binary (using &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; symbols) or in hexadecimal (using digits and letters &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;#039;f&amp;#039;&amp;#039;&amp;#039;, either uppercase of lowercase). Values are separated by white spaces, tabs or newlines. Comments (started with &amp;#039;&amp;#039;&amp;#039;//&amp;#039;&amp;#039;&amp;#039; as in verilog) are ignored. An example of a binary initialization file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in binary text format&lt;br /&gt;
00000011&lt;br /&gt;
00001111&lt;br /&gt;
00111111&lt;br /&gt;
11111111&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same initialization sequence, but given as a hexadecimal text file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in hexadecimal text format&lt;br /&gt;
03&lt;br /&gt;
0f&lt;br /&gt;
3f&lt;br /&gt;
ff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;rom.sv&amp;#039;&amp;#039; that describes a 16x8 ROM using a vector variable and a continuous assignment.&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;rom_tb&amp;#039;&amp;#039;&amp;#039;, that drives the input of &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; with a sequence of addresses from 0 to highest one. Run simulation.&lt;br /&gt;
* Open a text editor, write some values in binary format on successive lines, and save the file into the &amp;#039;&amp;#039;rom&amp;#039;&amp;#039; folder with the name &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;&lt;br /&gt;
* In the testbench module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Run simulation.&lt;br /&gt;
* Assign the segments of a display to &amp;#039;&amp;#039;&amp;#039;dout&amp;#039;&amp;#039;&amp;#039;, and 4 switches, from SW3 to SW0, to &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* In the &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Pay atention how its second argument is given - the array variable is now directly accesible. Compile the project and program the FPGA.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
=== RAM - Read-Write Memory ===&lt;br /&gt;
&lt;br /&gt;
A synchronous memory reacts only on clock edges. The addresses are sampled at clock edges, the output changes also on clock edges, and the write is performed on clock edges too.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ram.png]]&lt;br /&gt;
&lt;br /&gt;
The FPGA has dedicated blocks for memory implementation, the so called &amp;#039;&amp;#039;ram blocks&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;ramblocks&amp;#039;&amp;#039;. In order for the synthesis to map a memory description to a &amp;#039;&amp;#039;ramblock&amp;#039;&amp;#039;, the sequential description must conform to a template:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~we)                   // if write is enabled,&lt;br /&gt;
        mem[addr] &amp;lt;= wdata;   //     update the addressed location to wdata value&lt;br /&gt;
    rdata &amp;lt;= mem[addr];       // data read from the addressed location is transferred to the output&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;, in a new folder &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;ram.sv&amp;#039;&amp;#039; that describes a 16x4 RAM using a vector variable and a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process that assigns to &amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039; output the value of the memory location selected by &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. In the same process update the same location with &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; value if &amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039; input is active (active at &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;ram_tb&amp;#039;&amp;#039;&amp;#039;, which writes some data to four addresses, and then reads the same addresses in the same order, as indicated in the waveforms below. Don&amp;#039;t forget the clock! Run simulation and check that the written data are correctly read afterwards.&lt;br /&gt;
* Assign LEDs to the output, switches to the &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; inputs, a push button to the write enable input, and the board&amp;#039;s source clock to &amp;#039;&amp;#039;&amp;#039;clk&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* Compile, program the FPGA and test the RAM. Set switches to an address and a data value, then briefly push the write enable button. The LEDs will change accordingly. Change the address and data and push again the write enable button. Change back the address switches and see if the previously written configuration reappears on LED.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ram_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
=== Programmable display ===&lt;br /&gt;
&lt;br /&gt;
These application employs a dual port synchronous RAM. One port, the &amp;#039;&amp;#039;read port&amp;#039;&amp;#039;, is used to read data (&amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039;) from the memory, using the read address &amp;#039;&amp;#039;&amp;#039;raddr&amp;#039;&amp;#039;&amp;#039;. The other, the &amp;#039;&amp;#039;write port&amp;#039;&amp;#039;, accesses the same memory array, but using another address, &amp;#039;&amp;#039;&amp;#039;waddr&amp;#039;&amp;#039;&amp;#039;, to write &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; into. The write operation is performed only if the write enable pin (&amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039;) is active (here it is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
The data read from RAM is translated by the ROM into a 7 bit combination for the digit display. The RAM memory is read in sequence, address by address, changing the address at around each second. The sequence of 4 bit addresses are generated by a counter from which four suitable bits are chosen such that the rate of change is around 1 Hz.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ramrom.png]]&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; file from Exercise 1 into this folder.&lt;br /&gt;
* Add an initialization file to this folder, and ensures that &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; initialization tasks reads it from the correct location.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;ram.v&amp;#039;&amp;#039; from from Exercise 2 into this folder. Change &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039; description to a dual RAM.&lt;br /&gt;
* Add &amp;#039;&amp;#039;&amp;#039;cnt32.v&amp;#039;&amp;#039;&amp;#039; to this project (you may reuse the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module or file from the previous Application).&lt;br /&gt;
* Create the top-level design file, &amp;#039;&amp;#039;ramrom.v&amp;#039;&amp;#039;&amp;#039;, and instantiate the modules and connect them as indicated.&lt;br /&gt;
* Assign swithces, push button, clock source and digit display as shown.&lt;br /&gt;
* Compile, program the FPGA, and play with the board switches and buttons.&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8047</id>
		<title>Applications 8</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_8&amp;diff=8047"/>
		<updated>2025-04-21T21:36:22Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A random access memory is an address-based data storage block, with a highly regular structure, which may be logically defined either as a bi-dimensional array of bits, or a uni-dimensional list of data words. Each row of the array, or each item of the list has a unique address. The addresses form a contiguous set of integers, starting from zero and running up to the highest address which defines the size of the address space, usually a power of 2. The memory capacity is therefore the number of address locations, N, times the width of the memory location (or of the memory word), W. These two numbers are the main parameters of any memory:&lt;br /&gt;
* N - the number of address locations, also the number of words;&lt;br /&gt;
* W - the number of bits for each location, or the data word width;&lt;br /&gt;
The storage capacity is N x W bits, but the memory capacity may also be given in bytes.&lt;br /&gt;
&lt;br /&gt;
Being a vector of words or a bidimensional array of bits, the memory could be described using a single bidimensional variable, usually declared as a vector of multibit &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; items. A memory array of N locations, with W bits per location is declared as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
logic [W-1: 0] memory [0: N-1];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* the width of the word is declared as usual, with msb as the left index, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[msb:0]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* the length of the memory is declared after the variable name, and starts with 0, the right index being the highest index of the vector, &amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot; inline&amp;gt;[0:endindex]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are two kinds of memories:&lt;br /&gt;
* read-only memories&lt;br /&gt;
* writable memories&lt;br /&gt;
&lt;br /&gt;
Memories can also be classified by their synchronous or asynchronous character:&lt;br /&gt;
* asynchronous - read data is immediately available at the output, write is performed as into a latch&lt;br /&gt;
* synchronous  - all operations are synchronized by one of the clock edges.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
=== ROM - Read-Only Memory ===&lt;br /&gt;
&lt;br /&gt;
It may be described as a purely combinational logic circuit, whose output (data) is a logic function of its input (address). The transcoder from [[Applications 4#transcoder|Applications 4]] is nothing else than a ROM, with &amp;#039;&amp;#039;&amp;#039;value&amp;#039;&amp;#039;&amp;#039; input acting as an address that selects a particular location from where data is read and sent out as &amp;#039;&amp;#039;&amp;#039;seg&amp;#039;&amp;#039;&amp;#039;. There it was described behaviorally with a &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement that had a branch for each combination of input value bits.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_rom.png]]&lt;br /&gt;
&lt;br /&gt;
A more flexible description employs a single memory array, declared as a vector of &amp;#039;&amp;#039;&amp;#039;reg&amp;#039;&amp;#039;&amp;#039; variables, and a statement that assigns the selected (addressed) element of the memory vector to the memory output.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
assign dout = memory[addr];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization task ===&lt;br /&gt;
&lt;br /&gt;
Memory arrays may be initialized with predefined values from a text file using verilog system tasks &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039;. &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; reads predefined values given in binary, while &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; is used if values are given in hexadecimal. These verilog system tasks have two mandatory arguments and two optional arguments, in the following order:&lt;br /&gt;
# initialization filename, given as a string (between double quotes)&lt;br /&gt;
# the memory array variable name&lt;br /&gt;
# the start address (optional)&lt;br /&gt;
# the end address  (optional)&lt;br /&gt;
If the start and end addresses are not given, the memory array is initialized from the first address (address zero) until the last address or until the end of initialization file is reached.&lt;br /&gt;
&lt;br /&gt;
If the memory initialization file, &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;, resides in the project&amp;#039;s folder, and the memory array to be initialized is &amp;#039;&amp;#039;&amp;#039;mem&amp;#039;&amp;#039;&amp;#039; inside the memory instance &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the testbench may initialize the memory right at the beginning of the simulation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, dut.mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the initialization is intended to be done for the implemented memory, the initialization process must be placed inside the memory module, in which case the memory array name is just the variable array to be initialized:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
initial $readmemb(&amp;quot;../../meminit.txt&amp;quot;, mem);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The filename, the first argument of &amp;#039;&amp;#039;&amp;#039;$readmemb&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;$readmemh&amp;#039;&amp;#039;&amp;#039; tasks, may be given either relative to the simulation folder (as in the previous two examples), or as the complete absolute filename, with the pathname starting from the linux root, &amp;#039;&amp;#039;&amp;#039;/&amp;#039;&amp;#039;&amp;#039;, for example &amp;lt;syntaxhighlight lang=&amp;quot;Verilog&amp;quot; inline&amp;gt;&amp;quot;/home/student/rom/meminit.txt&amp;quot;&amp;lt;/syntaxhighlight&amp;gt; if &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039; file resides in the project&amp;#039;s folder &amp;#039;&amp;#039;rom&amp;#039;&amp;#039;, created in the student&amp;#039;s home directory &amp;#039;&amp;#039;/home/student&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Memory initialization file ===&lt;br /&gt;
&lt;br /&gt;
The initialization file is a text file with values written in binary (using &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039; symbols) or in hexadecimal (using digits and letters &amp;#039;&amp;#039;&amp;#039;a&amp;#039;&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;#039;f&amp;#039;&amp;#039;&amp;#039;, either uppercase of lowercase). Values are separated by white spaces, tabs or newlines. Comments (started with &amp;#039;&amp;#039;&amp;#039;//&amp;#039;&amp;#039;&amp;#039; as in verilog) are ignored. An example of a binary initialization file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in binary text format&lt;br /&gt;
00000011&lt;br /&gt;
00001111&lt;br /&gt;
00111111&lt;br /&gt;
11111111&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same initialization sequence, but given as a hexadecimal text file:&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
// four values in hexadecimal text format&lt;br /&gt;
03&lt;br /&gt;
0f&lt;br /&gt;
3f&lt;br /&gt;
ff&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;rom.sv&amp;#039;&amp;#039; that describes a 16x8 ROM using a vector variable and a continuous assignment.&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;rom_tb&amp;#039;&amp;#039;&amp;#039;, that drives the input of &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; with a sequence of addresses from 0 to highest one. Run simulation.&lt;br /&gt;
* Open a text editor, write some values in binary format on successive lines, and save the file into the &amp;#039;&amp;#039;rom&amp;#039;&amp;#039; folder with the name &amp;#039;&amp;#039;meminit.txt&amp;#039;&amp;#039;&lt;br /&gt;
* In the testbench module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Run simulation.&lt;br /&gt;
* Assign the segments of a display to &amp;#039;&amp;#039;&amp;#039;dout&amp;#039;&amp;#039;&amp;#039;, and 4 switches, from SW3 to SW0, to &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* In the &amp;#039;&amp;#039;&amp;#039;rom&amp;#039;&amp;#039;&amp;#039; module add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process with a single statement that calls the system task for memory initialization. Pay atention how its second argument is given - the array variable is now directly accesible. Compile the project and program the FPGA.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
=== RAM - Read-Write Memory ===&lt;br /&gt;
&lt;br /&gt;
A synchronous memory reacts only on clock edges. The addresses are sampled at clock edges, the output changes also on clock edges, and the write is performed on clock edges too.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ram.png]]&lt;br /&gt;
&lt;br /&gt;
The FPGA has dedicated blocks for memory implementation, the so called &amp;#039;&amp;#039;ram blocks&amp;#039;&amp;#039;, or &amp;#039;&amp;#039;ramblocks&amp;#039;&amp;#039;. In order for the synthesis to map a memory description to a &amp;#039;&amp;#039;ramblock&amp;#039;&amp;#039;, the sequential description must conform to a template:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always @(posedge clk) begin&lt;br /&gt;
    if(~we)                   // if write is enabled,&lt;br /&gt;
        mem[addr] &amp;lt;= wdata;   //     update the addressed location to wdata value&lt;br /&gt;
    rdata &amp;lt;= mem[addr];       // data read from the addressed location is transferred to the output&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;, in a new folder &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* Create a new verilog file &amp;#039;&amp;#039;ram.sv&amp;#039;&amp;#039; that describes a 16x4 RAM using a vector variable and a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process that assigns to &amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039; output the value of the memory location selected by &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039;. In the same process update the same location with &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; value if &amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039; input is active (active at &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
* Create a testbench, &amp;#039;&amp;#039;&amp;#039;ram_tb&amp;#039;&amp;#039;&amp;#039;, which writes some data to four addresses, and then reads the same addresses in the same order, as indicated in the waveforms below. Don&amp;#039;t forget the clock! Run simulation and check that the written data are correctly read afterwards.&lt;br /&gt;
* Assign LEDs to the output, switches to the &amp;#039;&amp;#039;&amp;#039;addr&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; inputs, a push button to the write enable input, and the board&amp;#039;s source clock to &amp;#039;&amp;#039;&amp;#039;clk&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* Compile, program the FPGA and test the RAM. Set switches to an address and a data value, then briefly push the write enable button. The LEDs will change accordingly. Change the address and data and push again the write enable button. Change back the address switches and see if the previously written configuration reappears on LED.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ram_wave.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
=== Programmable display ===&lt;br /&gt;
&lt;br /&gt;
These application employs a dual port synchronous RAM. One port, the &amp;#039;&amp;#039;read port&amp;#039;&amp;#039;, is used to read data (&amp;#039;&amp;#039;&amp;#039;rdata&amp;#039;&amp;#039;&amp;#039;) from the memory, using the read address &amp;#039;&amp;#039;&amp;#039;raddr&amp;#039;&amp;#039;&amp;#039;. The other, the &amp;#039;&amp;#039;write port&amp;#039;&amp;#039;, accesses the same memory array, but using another address, &amp;#039;&amp;#039;&amp;#039;waddr&amp;#039;&amp;#039;&amp;#039;, to write &amp;#039;&amp;#039;&amp;#039;wdata&amp;#039;&amp;#039;&amp;#039; into. The write operation is performed only if the write enable pin (&amp;#039;&amp;#039;&amp;#039;we&amp;#039;&amp;#039;&amp;#039;) is active (here it is active &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
The data read from RAM is translated by the ROM into a 7 bit combination for the digit display. The RAM memory is read in sequence, address by address, changing the address at around each second. The sequence of 4 bit addresses are generated by a counter from which four suitable bits are chosen such that the rate of change is around 1 Hz.&lt;br /&gt;
&lt;br /&gt;
[[Fișier: appl8_ramrom.png]]&lt;br /&gt;
&lt;br /&gt;
* Start a new project, &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;, in a newly created folder &amp;#039;&amp;#039;&amp;#039;ramrom&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; file from Exercise 1 into this folder.&lt;br /&gt;
* Add an initialization file to this folder, and ensures that &amp;#039;&amp;#039;rom.v&amp;#039;&amp;#039; initialization tasks reads it from the correct location.&lt;br /&gt;
* Copy the &amp;#039;&amp;#039;ram.v&amp;#039;&amp;#039; from from Exercise 2 into this folder. Change &amp;#039;&amp;#039;&amp;#039;ram&amp;#039;&amp;#039;&amp;#039; description to a dual RAM.&lt;br /&gt;
* Add &amp;#039;&amp;#039;&amp;#039;cnt32.v&amp;#039;&amp;#039;&amp;#039; to this project (you may reuse the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module or file from the previous Application).&lt;br /&gt;
* Create the top-level design file, &amp;#039;&amp;#039;ramrom.v&amp;#039;&amp;#039;&amp;#039;, and instantiate the modules and connect them as indicated.&lt;br /&gt;
* Assign swithces, push button, clock source and digit display as shown.&lt;br /&gt;
* Compile, program the FPGA, and play with the board switches and buttons.&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Digital_Integrated_Circuits_(lab)&amp;diff=8046</id>
		<title>Digital Integrated Circuits (lab)</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Digital_Integrated_Circuits_(lab)&amp;diff=8046"/>
		<updated>2025-04-21T21:28:15Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;# [[Applications 1 | Laboratory 1]]&lt;br /&gt;
# [[Applications 2 | Laboratory 2]]&lt;br /&gt;
# [[Applications 3 | Laboratory 3]]&lt;br /&gt;
# [[Applications 4 | Laboratory 4]]&lt;br /&gt;
# [[Applications 5 | Laboratory 5]]&lt;br /&gt;
# Test 1&lt;br /&gt;
# [[Applications 6 | Laboratory 6]]&lt;br /&gt;
# [[Applications 7 | Laboratory 7]]&lt;br /&gt;
# [[Applications 8 | Laboratory 8]]&lt;br /&gt;
&lt;br /&gt;
== Verilog Tutorials ==&lt;br /&gt;
* [http://www.asic-world.com/verilog/veritut.html Verilog Tutorial] from ASIC World&lt;br /&gt;
* [http://chipverify.com/verilog/verilog-tutorial Verilog Tutorial] from Chip Verify&lt;br /&gt;
* [http://www.emmelmann.org/Library/Tutorials/docs/verilog_ref_guide/vlog_ref_top.html Verilog reference guide]&lt;br /&gt;
&lt;br /&gt;
== Software ==&lt;br /&gt;
* [https://www.xilinx.com/products/design-tools/vivado/vivado-ml.html Vivado™ ML Standard Edition free]&lt;br /&gt;
* [https://wiki.dcae.pub.ro/images/2/24/VivadoNewProjectTutorial.pdf Vivado New Project Tutorial]&lt;br /&gt;
&lt;br /&gt;
== Hardware ==&lt;br /&gt;
[https://www.realdigital.org/hardware/boolean Boolean Board] from RealDigital&lt;br /&gt;
* [https://www.realdigital.org/doc/02013cd17602c8af749f00561f88ae21 Boolean Board - user manual]&lt;br /&gt;
* [[Boolean Board - Pinout]]&lt;br /&gt;
To implement a circuit on Boolean Board you should select the following target device in Project Settings: &amp;#039;&amp;#039;&amp;#039;xc7s50csga324-1&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
* [https://dpoauwgwqsy2x.cloudfront.net/Download/pynqz2_user_manual_v1_0.pdf Pynq-Z2 - user manual]&lt;br /&gt;
* [[Pynq-Z2 - Pinout]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_7&amp;diff=8045</id>
		<title>Applications 7</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_7&amp;diff=8045"/>
		<updated>2025-04-13T15:15:05Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* 32 bit Counter : Synchronous load */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The counter is one of the most used digital blocks and surely one of the best known outside the world of electronics. Any microprocessor has a program counter, without which it can not run programs on the computer. The program counter generates a sequence of memory addresses in ascending order, one in each clock period, and occasionally jumps in the program to an address other than the next one.&lt;br /&gt;
&lt;br /&gt;
== Example 1 ==&lt;br /&gt;
=== 32 bit Counter : Behavioral description ===&lt;br /&gt;
&lt;br /&gt;
[[File: appl7_cnt_32.png]]&lt;br /&gt;
&lt;br /&gt;
The simplest counter of all, in terms of functionality, uninterruptedly generates an ordered sequence of numbers, cycle by cycle. An upward counter generates the sequence in increasing order: 0, 1, 2, 3, 4 a.s.o.,  After it reaches the highest number, with all bits 1, the next cycle it turns around to all bits 0.&lt;br /&gt;
&lt;br /&gt;
As an automaton, the counter may be regarded as a register with an incrementer closing the outer loop. The behavioral description needs only a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process to update the counter value, and an assignment for the next value of the counter.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    cnt &amp;lt;= cnt + 1;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* create a new project&lt;br /&gt;
* create a new source file for &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039;, and describe it as shown above&lt;br /&gt;
* assign fpga pins to connect an external clock and LEDs to four of the output bits, as indicated below&lt;br /&gt;
* compile and program the FPGA. The red LEDs should be blinking, LED0 about once a second, LED1 twice slower, LED2 twice slower than LED1 a.s.o.&lt;br /&gt;
 &lt;br /&gt;
[[File: Zlab7_cnt_32_led.png]]&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Initial state ===&lt;br /&gt;
&lt;br /&gt;
* in the same project add a verilog file for &amp;#039;&amp;#039;&amp;#039;cnt32_tb&amp;#039;&amp;#039;&amp;#039;. In the testbench you need only to generate the clock. Simulate and observe the counter output waveform.&lt;br /&gt;
&lt;br /&gt;
Why the output of &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; is completely unknown in simulation, whereas its output on FPGA works as expected?&lt;br /&gt;
&lt;br /&gt;
However, if you looked carefully how the LEDs started blinking immediately after the FPGA had been programmed, you noticed that they started from a random configuration (some LEDs light on, the others off). The initial configuration of counter&amp;#039;s internal flip-flops is unknown at power-up. They clearly settle after power-up at some logic values, but those values cannot be predicted. The simulator cannot guess them, so it starts with an unknown value, and incrementing an unknown value you get also an unknown value.&lt;br /&gt;
&lt;br /&gt;
A workaround to initialize the &amp;#039;&amp;#039;&amp;#039;cnt&amp;#039;&amp;#039;&amp;#039; variable in simulation is to force its value from outside the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, directly from the testbench module.&lt;br /&gt;
&lt;br /&gt;
During simulation we can have direct access to any (variable) signal inside the tested module if we use its full name, an unique name within the block hierarchy of the dut instance. The full name of a signal is written in the same way the full name of a variable in an object-oriented program is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
topInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
topInstanceName.blockInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
topInstanceName.blockInstanceName.subblockInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the case of our counter, if in the test module its instance is named &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the &amp;#039;&amp;#039;&amp;#039;cnt&amp;#039;&amp;#039;&amp;#039; variable can be initialized in step 0 of the simulation directly from the testbench module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial dut.cnt=0;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mode of access should be used with parsimony and only in test modules. It is not a synthesized statement and cannot be used in design modules! It is recommended to use direct access in test modules only for the purpose of reading (monitoring) variables that are not accessible trough the top-level design interface.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* It is forbidden in a design module to use direct access to an internal signal of another module.&lt;br /&gt;
&lt;br /&gt;
Much more elegant is to provide the counter with a reset or clear input, that is used to initialize the counter at the reset or to bring the counter to the initial state whenever needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Reset ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* add the &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; pin to &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, and change the sequential process such that the counter is set to 0 if reset is applied. &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; is synchronous and active 1.&lt;br /&gt;
* add an initial process to the testbench to apply the reset pulse for at least two clock cycles. Rerun the simulation and see the effect of the reset. &lt;br /&gt;
* assign a pushbutton (BTN) to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* compile and program the FPGA&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7 cnt 32 rst.png]]&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Count enable ===&lt;br /&gt;
&lt;br /&gt;
It is useful for many counter applications to be able to sometime freeze the counting. An additional control signal, counter enable (&amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;), enables the counting only when it&amp;#039;s active.&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7_cnt_32_en.png]]&lt;br /&gt;
&lt;br /&gt;
* add the &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; pin to &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, and change the sequential process such that the counting is enabled only if &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; is 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(rst) begin&lt;br /&gt;
        // initialization at reset&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        // everything else, including count enable control&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* add another initial process to the testbench to change from time to time the &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; value. &lt;br /&gt;
* assign a switch (SW0) to &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; input&lt;br /&gt;
* compile and program the FPGA&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Synchronous load ===&lt;br /&gt;
&lt;br /&gt;
Another very useful feature is to bring the counter state to whatever desired value (&amp;#039;&amp;#039;&amp;#039;target&amp;#039;&amp;#039;&amp;#039; value). This feature makes easy the design of frequency dividers and lots of counter based automata.&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7_cnt_32_load.png]]&lt;br /&gt;
&lt;br /&gt;
When load input (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;) is active, the active clock edge causes the counter to load the value of the &amp;#039;&amp;#039;&amp;#039;target&amp;#039;&amp;#039;&amp;#039; input. If both control inputs (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;) are active, the load command has priority. All functions of this counter can be arranged in a table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- bgcolor=&amp;quot;#ddeeff&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
| ld || ce ||next cnt value|| function&lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|0 ||0 ||cnt ||freeze &lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|0 ||1 ||cnt+1 ||count&lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|1 ||X ||target ||load&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The X in the first row means that the value of &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; input doesn&amp;#039;t matter (it may be either &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;) because &amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; has priority over &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;. The truth table can thus be substantially compressed. Instead of two separate rows for the input combinations (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;) and (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;), for which the function is the same (load), a single combination is used (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;X&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
This compaction can also be used in Verilog table constructions, such as the &amp;#039;case&amp;#039; statement. In order to use the X value in specifying the cases of the instruction, the alternative &amp;#039;&amp;#039;&amp;#039;casex&amp;#039;&amp;#039;&amp;#039; statement, which has the same format as the &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement, must be used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
casex (expression_case)&lt;br /&gt;
    value1: instruction1&lt;br /&gt;
    ...&lt;br /&gt;
    default: instruction&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike the &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; block, which admits only precise numerical values ​​for each branch, the &amp;#039;&amp;#039;&amp;#039;casex&amp;#039;&amp;#039;&amp;#039; block allows some bits to be undefined for some branches. For example, the combinations for active &amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; can be combined into a single branch:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
casex({ld,ce})&lt;br /&gt;
    2&amp;#039;b1X: out &amp;lt;= target;&lt;br /&gt;
    // other cases&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
case({ld,ce})&lt;br /&gt;
    2&amp;#039;b10,&lt;br /&gt;
    2&amp;#039;b11: out &amp;lt;= target;&lt;br /&gt;
    // other cases&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; behavioral description to conform to a synchronous counter with a synchronous load.&lt;br /&gt;
* add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process in the testbench to load a particular target value after some time. Try to reproduce the stimuli scenario from the figure below and see if you get the same output as in the figure:&lt;br /&gt;
&lt;br /&gt;
[[File: Lab7_cnt_32_load_wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Lab7_cnt_32_load_wave.png&amp;diff=8044</id>
		<title>Fișier:Lab7 cnt 32 load wave.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Lab7_cnt_32_load_wave.png&amp;diff=8044"/>
		<updated>2025-04-13T15:12:09Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_7&amp;diff=8043</id>
		<title>Applications 7</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_7&amp;diff=8043"/>
		<updated>2025-04-13T12:18:35Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* 32 bit Counter : Synchronous load */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The counter is one of the most used digital blocks and surely one of the best known outside the world of electronics. Any microprocessor has a program counter, without which it can not run programs on the computer. The program counter generates a sequence of memory addresses in ascending order, one in each clock period, and occasionally jumps in the program to an address other than the next one.&lt;br /&gt;
&lt;br /&gt;
== Example 1 ==&lt;br /&gt;
=== 32 bit Counter : Behavioral description ===&lt;br /&gt;
&lt;br /&gt;
[[File: appl7_cnt_32.png]]&lt;br /&gt;
&lt;br /&gt;
The simplest counter of all, in terms of functionality, uninterruptedly generates an ordered sequence of numbers, cycle by cycle. An upward counter generates the sequence in increasing order: 0, 1, 2, 3, 4 a.s.o.,  After it reaches the highest number, with all bits 1, the next cycle it turns around to all bits 0.&lt;br /&gt;
&lt;br /&gt;
As an automaton, the counter may be regarded as a register with an incrementer closing the outer loop. The behavioral description needs only a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process to update the counter value, and an assignment for the next value of the counter.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    cnt &amp;lt;= cnt + 1;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* create a new project&lt;br /&gt;
* create a new source file for &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039;, and describe it as shown above&lt;br /&gt;
* assign fpga pins to connect an external clock and LEDs to four of the output bits, as indicated below&lt;br /&gt;
* compile and program the FPGA. The red LEDs should be blinking, LED0 about once a second, LED1 twice slower, LED2 twice slower than LED1 a.s.o.&lt;br /&gt;
 &lt;br /&gt;
[[File: Zlab7_cnt_32_led.png]]&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Initial state ===&lt;br /&gt;
&lt;br /&gt;
* in the same project add a verilog file for &amp;#039;&amp;#039;&amp;#039;cnt32_tb&amp;#039;&amp;#039;&amp;#039;. In the testbench you need only to generate the clock. Simulate and observe the counter output waveform.&lt;br /&gt;
&lt;br /&gt;
Why the output of &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; is completely unknown in simulation, whereas its output on FPGA works as expected?&lt;br /&gt;
&lt;br /&gt;
However, if you looked carefully how the LEDs started blinking immediately after the FPGA had been programmed, you noticed that they started from a random configuration (some LEDs light on, the others off). The initial configuration of counter&amp;#039;s internal flip-flops is unknown at power-up. They clearly settle after power-up at some logic values, but those values cannot be predicted. The simulator cannot guess them, so it starts with an unknown value, and incrementing an unknown value you get also an unknown value.&lt;br /&gt;
&lt;br /&gt;
A workaround to initialize the &amp;#039;&amp;#039;&amp;#039;cnt&amp;#039;&amp;#039;&amp;#039; variable in simulation is to force its value from outside the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, directly from the testbench module.&lt;br /&gt;
&lt;br /&gt;
During simulation we can have direct access to any (variable) signal inside the tested module if we use its full name, an unique name within the block hierarchy of the dut instance. The full name of a signal is written in the same way the full name of a variable in an object-oriented program is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
topInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
topInstanceName.blockInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
topInstanceName.blockInstanceName.subblockInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the case of our counter, if in the test module its instance is named &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the &amp;#039;&amp;#039;&amp;#039;cnt&amp;#039;&amp;#039;&amp;#039; variable can be initialized in step 0 of the simulation directly from the testbench module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial dut.cnt=0;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mode of access should be used with parsimony and only in test modules. It is not a synthesized statement and cannot be used in design modules! It is recommended to use direct access in test modules only for the purpose of reading (monitoring) variables that are not accessible trough the top-level design interface.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* It is forbidden in a design module to use direct access to an internal signal of another module.&lt;br /&gt;
&lt;br /&gt;
Much more elegant is to provide the counter with a reset or clear input, that is used to initialize the counter at the reset or to bring the counter to the initial state whenever needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Reset ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* add the &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; pin to &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, and change the sequential process such that the counter is set to 0 if reset is applied. &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; is synchronous and active 1.&lt;br /&gt;
* add an initial process to the testbench to apply the reset pulse for at least two clock cycles. Rerun the simulation and see the effect of the reset. &lt;br /&gt;
* assign a pushbutton (BTN) to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* compile and program the FPGA&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7 cnt 32 rst.png]]&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Count enable ===&lt;br /&gt;
&lt;br /&gt;
It is useful for many counter applications to be able to sometime freeze the counting. An additional control signal, counter enable (&amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;), enables the counting only when it&amp;#039;s active.&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7_cnt_32_en.png]]&lt;br /&gt;
&lt;br /&gt;
* add the &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; pin to &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, and change the sequential process such that the counting is enabled only if &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; is 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(rst) begin&lt;br /&gt;
        // initialization at reset&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        // everything else, including count enable control&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* add another initial process to the testbench to change from time to time the &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; value. &lt;br /&gt;
* assign a switch (SW0) to &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; input&lt;br /&gt;
* compile and program the FPGA&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Synchronous load ===&lt;br /&gt;
&lt;br /&gt;
Another very useful feature is to bring the counter state to whatever desired value (&amp;#039;&amp;#039;&amp;#039;target&amp;#039;&amp;#039;&amp;#039; value). This feature makes easy the design of frequency dividers and lots of counter based automata.&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7_cnt_32_load.png]]&lt;br /&gt;
&lt;br /&gt;
When load input (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;) is active, the active clock edge causes the counter to load the value of the &amp;#039;&amp;#039;&amp;#039;target&amp;#039;&amp;#039;&amp;#039; input. If both control inputs (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;) are active, the load command has priority. All functions of this counter can be arranged in a table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- bgcolor=&amp;quot;#ddeeff&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
| ld || ce ||next cnt value|| function&lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|0 ||0 ||cnt ||freeze &lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|0 ||1 ||cnt+1 ||count&lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|1 ||X ||target ||load&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The X in the first row means that the value of &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; input doesn&amp;#039;t matter (it may be either &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;) because &amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; has priority over &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;. The truth table can thus be substantially compressed. Instead of two separate rows for the input combinations (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;) and (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;), for which the function is the same (load), a single combintion is used (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;X&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
This compaction can also be used in Verilog table constructions, such as the &amp;#039;case&amp;#039; statement. In order to use the X value in specifying the cases of the instruction, the alternative &amp;#039;&amp;#039;&amp;#039;casex&amp;#039;&amp;#039;&amp;#039; statement, which has the same format as the &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement, must be used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
casex (expression_case)&lt;br /&gt;
    value1: instruction1&lt;br /&gt;
    ...&lt;br /&gt;
    default: instruction&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike the &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; block, which admits only precise numerical values ​​for each branch, the &amp;#039;&amp;#039;&amp;#039;casex&amp;#039;&amp;#039;&amp;#039; block allows some bits to be undefined for some branches. For example, the combinations for active &amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; can be combined into a single branch:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
casex({ld,ce})&lt;br /&gt;
    2&amp;#039;b1X: out &amp;lt;= target;&lt;br /&gt;
    // other cases&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
case({ld,ce})&lt;br /&gt;
    2&amp;#039;b10,&lt;br /&gt;
    2&amp;#039;b11: out &amp;lt;= target;&lt;br /&gt;
    // other cases&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; behavioral description to conform to a synchronous counter with a synchronous load.&lt;br /&gt;
* add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process in the testbench to load a particular target value after some time. Try to reproduce the stimuli scenario from the figure below and see if you get the same output as in the figure:&lt;br /&gt;
&lt;br /&gt;
[[File: appl7_cnt_32_load_wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab7_cnt_32_load.png&amp;diff=8042</id>
		<title>Fișier:Zlab7 cnt 32 load.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Zlab7_cnt_32_load.png&amp;diff=8042"/>
		<updated>2025-04-13T12:16:09Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
	<entry>
		<id>http://wiki.dcae.pub.ro/index.php?title=Applications_7&amp;diff=8041</id>
		<title>Applications 7</title>
		<link rel="alternate" type="text/html" href="http://wiki.dcae.pub.ro/index.php?title=Applications_7&amp;diff=8041"/>
		<updated>2025-04-13T12:15:11Z</updated>

		<summary type="html">&lt;p&gt;Zhascsi: /* 32 bit Counter : Count enable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The counter is one of the most used digital blocks and surely one of the best known outside the world of electronics. Any microprocessor has a program counter, without which it can not run programs on the computer. The program counter generates a sequence of memory addresses in ascending order, one in each clock period, and occasionally jumps in the program to an address other than the next one.&lt;br /&gt;
&lt;br /&gt;
== Example 1 ==&lt;br /&gt;
=== 32 bit Counter : Behavioral description ===&lt;br /&gt;
&lt;br /&gt;
[[File: appl7_cnt_32.png]]&lt;br /&gt;
&lt;br /&gt;
The simplest counter of all, in terms of functionality, uninterruptedly generates an ordered sequence of numbers, cycle by cycle. An upward counter generates the sequence in increasing order: 0, 1, 2, 3, 4 a.s.o.,  After it reaches the highest number, with all bits 1, the next cycle it turns around to all bits 0.&lt;br /&gt;
&lt;br /&gt;
As an automaton, the counter may be regarded as a register with an incrementer closing the outer loop. The behavioral description needs only a sequential &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; process to update the counter value, and an assignment for the next value of the counter.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    cnt &amp;lt;= cnt + 1;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* create a new project&lt;br /&gt;
* create a new source file for &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039;, and describe it as shown above&lt;br /&gt;
* assign fpga pins to connect an external clock and LEDs to four of the output bits, as indicated below&lt;br /&gt;
* compile and program the FPGA. The red LEDs should be blinking, LED0 about once a second, LED1 twice slower, LED2 twice slower than LED1 a.s.o.&lt;br /&gt;
 &lt;br /&gt;
[[File: Zlab7_cnt_32_led.png]]&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Initial state ===&lt;br /&gt;
&lt;br /&gt;
* in the same project add a verilog file for &amp;#039;&amp;#039;&amp;#039;cnt32_tb&amp;#039;&amp;#039;&amp;#039;. In the testbench you need only to generate the clock. Simulate and observe the counter output waveform.&lt;br /&gt;
&lt;br /&gt;
Why the output of &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; is completely unknown in simulation, whereas its output on FPGA works as expected?&lt;br /&gt;
&lt;br /&gt;
However, if you looked carefully how the LEDs started blinking immediately after the FPGA had been programmed, you noticed that they started from a random configuration (some LEDs light on, the others off). The initial configuration of counter&amp;#039;s internal flip-flops is unknown at power-up. They clearly settle after power-up at some logic values, but those values cannot be predicted. The simulator cannot guess them, so it starts with an unknown value, and incrementing an unknown value you get also an unknown value.&lt;br /&gt;
&lt;br /&gt;
A workaround to initialize the &amp;#039;&amp;#039;&amp;#039;cnt&amp;#039;&amp;#039;&amp;#039; variable in simulation is to force its value from outside the &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, directly from the testbench module.&lt;br /&gt;
&lt;br /&gt;
During simulation we can have direct access to any (variable) signal inside the tested module if we use its full name, an unique name within the block hierarchy of the dut instance. The full name of a signal is written in the same way the full name of a variable in an object-oriented program is written:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
topInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
topInstanceName.blockInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
topInstanceName.blockInstanceName.subblockInstanceName.signal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the case of our counter, if in the test module its instance is named &amp;#039;&amp;#039;&amp;#039;dut&amp;#039;&amp;#039;&amp;#039;, the &amp;#039;&amp;#039;&amp;#039;cnt&amp;#039;&amp;#039;&amp;#039; variable can be initialized in step 0 of the simulation directly from the testbench module:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
initial dut.cnt=0;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This mode of access should be used with parsimony and only in test modules. It is not a synthesized statement and cannot be used in design modules! It is recommended to use direct access in test modules only for the purpose of reading (monitoring) variables that are not accessible trough the top-level design interface.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention!&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* It is forbidden in a design module to use direct access to an internal signal of another module.&lt;br /&gt;
&lt;br /&gt;
Much more elegant is to provide the counter with a reset or clear input, that is used to initialize the counter at the reset or to bring the counter to the initial state whenever needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Reset ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* add the &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; pin to &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, and change the sequential process such that the counter is set to 0 if reset is applied. &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; is synchronous and active 1.&lt;br /&gt;
* add an initial process to the testbench to apply the reset pulse for at least two clock cycles. Rerun the simulation and see the effect of the reset. &lt;br /&gt;
* assign a pushbutton (BTN) to &amp;#039;&amp;#039;&amp;#039;rst&amp;#039;&amp;#039;&amp;#039; input.&lt;br /&gt;
* compile and program the FPGA&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7 cnt 32 rst.png]]&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Count enable ===&lt;br /&gt;
&lt;br /&gt;
It is useful for many counter applications to be able to sometime freeze the counting. An additional control signal, counter enable (&amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;), enables the counting only when it&amp;#039;s active.&lt;br /&gt;
&lt;br /&gt;
[[File: Zlab7_cnt_32_en.png]]&lt;br /&gt;
&lt;br /&gt;
* add the &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; pin to &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; module, and change the sequential process such that the counting is enabled only if &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; is 1.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SystemVerilog&amp;quot;&amp;gt;&lt;br /&gt;
always_ff @(posedge clk) begin&lt;br /&gt;
    if(rst) begin&lt;br /&gt;
        // initialization at reset&lt;br /&gt;
    end&lt;br /&gt;
    else begin&lt;br /&gt;
        // everything else, including count enable control&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* add another initial process to the testbench to change from time to time the &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; value. &lt;br /&gt;
* assign a switch (SW0) to &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; input&lt;br /&gt;
* compile and program the FPGA&lt;br /&gt;
&lt;br /&gt;
=== 32 bit Counter : Synchronous load ===&lt;br /&gt;
&lt;br /&gt;
Another very useful feature is to bring the counter state to whatever desired value (&amp;#039;&amp;#039;&amp;#039;target&amp;#039;&amp;#039;&amp;#039; value). This feature makes easy the design of frequency dividers and lots of counter based automata.&lt;br /&gt;
&lt;br /&gt;
[[File: appl7_cnt_32_load.png]]&lt;br /&gt;
&lt;br /&gt;
When load input (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;) is active, the active clock edge causes the counter to load the value of the &amp;#039;&amp;#039;&amp;#039;target&amp;#039;&amp;#039;&amp;#039; input. If both control inputs (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;) are active, the load command has priority. All functions of this counter can be arranged in a table:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|- bgcolor=&amp;quot;#ddeeff&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
| ld || ce ||next cnt value|| function&lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|0 ||X ||target ||load&lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|1 ||0 ||cnt ||freeze &lt;br /&gt;
|- bgcolor=&amp;quot;#ddffdd&amp;quot; align=&amp;quot;left&amp;quot;&lt;br /&gt;
|1 ||1 ||cnt+1 ||count&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The X in the first row means that the value of &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039; input doesn&amp;#039;t matter (it may be either &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;) because &amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; has priority over &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;. The truth table can thus be substantially compressed. Instead of two separate rows for the input combinations (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;) and (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;1&amp;#039;&amp;#039;&amp;#039;), for which the function is the same (load), a single combintion is used (&amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ce&amp;#039;&amp;#039;&amp;#039;=&amp;#039;&amp;#039;&amp;#039;X&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
This compaction can also be used in Verilog table constructions, such as the &amp;#039;case&amp;#039; statement. In order to use the X value in specifying the cases of the instruction, the alternative &amp;#039;&amp;#039;&amp;#039;casex&amp;#039;&amp;#039;&amp;#039; statement, which has the same format as the &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; statement, must be used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
casex (expression_case)&lt;br /&gt;
    value1: instruction1&lt;br /&gt;
    ...&lt;br /&gt;
    default: instruction&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Unlike the &amp;#039;&amp;#039;&amp;#039;case&amp;#039;&amp;#039;&amp;#039; block, which admits only precise numerical values ​​for each branch, the &amp;#039;&amp;#039;&amp;#039;casex&amp;#039;&amp;#039;&amp;#039; block allows some bits to be undefined for some branches. For example, the combinations for active &amp;#039;&amp;#039;&amp;#039;ld&amp;#039;&amp;#039;&amp;#039; can be combined into a single branch:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
casex({ld,ce})&lt;br /&gt;
    2&amp;#039;b0X: out &amp;lt;= target;&lt;br /&gt;
    // other cases&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is equivalent to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;verilog&amp;quot;&amp;gt;&lt;br /&gt;
case({ld,ce})&lt;br /&gt;
    2&amp;#039;b00,&lt;br /&gt;
    2&amp;#039;b01: out &amp;lt;= target;&lt;br /&gt;
    // other cases&lt;br /&gt;
endcase&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* change &amp;#039;&amp;#039;&amp;#039;cnt32&amp;#039;&amp;#039;&amp;#039; behavioral description to conform to a synchronous counter with a synchronous load.&lt;br /&gt;
* add an &amp;#039;&amp;#039;&amp;#039;initial&amp;#039;&amp;#039;&amp;#039; process in the testbench to load a particular target value after some time. Try to reproduce the stimuli scenario from the figure below and see if you get the same output as in the figure:&lt;br /&gt;
&lt;br /&gt;
[[File: appl7_cnt_32_load_wave.png]]&lt;/div&gt;</summary>
		<author><name>Zhascsi</name></author>
	</entry>
</feed>