Salta al contenuto
Note per Studenti Digital Systems - Exam of 21 June 2022 Report

Digital Systems - Exam of 21 June 2022

Questa pagina non ha ancora la versione ripasso: qui sotto c'è il testo completo.

In questa pagina 24

Exam of the equivalent Italian-language course, Circuiti Digitali (Engineering, University of Padova), first sitting, 21 June 2022, translated into English. The paper has two parts: 20 multiple-choice questions (three answers each, one correct; the sheet says that wrong answers have a negative score and unanswered ones do not, without giving the amounts) and 3 exercises written on the sheet. The sheet gives no points for the exercises and no duration. Books, notes, calculators and colleagues are not allowed.

The official solution is not public. Every answer below was derived from the course notes and checked: the truth tables were enumerated by a script, the minimisation was compared with a symbolic algebra system, and both state machines were simulated on input sequences. Where a question is ambiguous this is said explicitly. The syllabus of this course covers every question of the paper.

Quiz 1

The precision of a digital system can be improved:

  • a. by increasing the number of bits used to represent the processed information
  • b. by widening the range of the physical quantity (e.g. voltage) used to represent one bit of information
  • c. none of the above
Show the result

a.

Show the full solution

Quiz 2

In a top-down approach:

  • a. one starts from the high-level specifications and then splits the system into smaller and smaller blocks
  • b. one starts from the elementary blocks, which are progressively assembled to obtain the desired function
  • c. none of the above
Show the result

a.

Show the full solution

Quiz 3

In the hexadecimal system:

  • a. each hexadecimal digit corresponds to two binary digits
  • b. each hexadecimal digit corresponds to three binary digits
  • c. each hexadecimal digit corresponds to four binary digits
Show the result

c.

Show the full solution

Quiz 4

In a Gray code:

  • a. exactly one bit changes when passing from one code word to the next
  • b. at most two bits change when passing from one code word to the next
  • c. none of the above
Show the result

a.

Show the full solution

Quiz 5

The Hamming distance between the representations of two numbers is:

  • a. the number of bits that differ between the two representations
  • b. the algebraic difference between the two representations
  • c. none of the above
Show the result

a.

Show the full solution

Quiz 6

Which of the following statements is correct?

  • a. 1 xor 0 xor 1=11\ \mathrm{xor}\ 0\ \mathrm{xor}\ 1 = 1
  • b. 1 xor 0 xor 1=01\ \mathrm{xor}\ 0\ \mathrm{xor}\ 1 = 0
  • c. none of the above
Show the result

b.

Show the full solution

Quiz 7

The absorption theorem states that:

  • a. x+xy=xx+xy=x
  • b. x+xy‾=x\overline{x+xy}=x
  • c. x+xy=x‾x+xy=\overline{x}
Show the result

a.

Show the full solution

Quiz 8

The consensus theorem states that:

  • a. xy+x‾z+yz=xy+x‾zxy+\overline{x}z+yz=xy+\overline{x}z
  • b. xy+x‾z+yz=xyzxy+\overline{x}z+yz=xyz
  • c. xy+x‾z+yz=xzxy+\overline{x}z+yz=xz
Show the result

a.

Show the full solution

The term yzyz is the consensus of xyxy and x‾z\overline xz and is redundant: yz=yz(x+x‾)=xyz+x‾yzyz=yz(x+\overline x)=xyz+\overline x yz, and xyzxyz is absorbed by xyxy, x‾yz\overline xyz by x‾z\overline xz. Counterexamples to the others: for x=0,y=0,z=1x=0,y=0,z=1 the left side is 11 and xyz=0xyz=0 (b. false); for x=1,y=1,z=0x=1,y=1,z=0 the left side is 11 and xz=0xz=0 (c. false). Theory: Boolean Algebra - Axioms, Theorems and Function ComplementBoolean algebra works on two-valued variables with AND, OR, NOT. Fundamental identities (identity element, idempotence, complement, commutative, associative, both distributive laws) come in dual pairs; De Morgan, absorption, adjacency and consensus are the main theorems, proved by perfect induction or algebraically. The complement of a function is obtained with De Morgan or with the dual plus literal negation. Cost is measured in literals or gate inputs.Boolean Algebra - Axioms, Theorems and Function Complement →.

Quiz 9

A minterm is:

  • a. a sum of products in which all variables appear, direct or negated
  • b. a product in which all variables appear, direct or negated
  • c. a sum in which all variables appear, direct or negated
Show the result

b.

Show the full solution

Quiz 10

A logic expression in SOP form:

  • a. can be implemented with a circuit with two levels of logic gates
  • b. can be implemented with a single level of logic gates
  • c. none of the above
Show the result

a.

Show the full solution

Quiz 11

The following VHDL fragment, where a is a std_logic signal declared in the entity as out:

vhdl
a <= '1';
a <= '0';
  • a. does not generate errors if inserted alone, without other elements, in the body of an architecture
  • b. can be used only in a test bench
  • c. none of the above
Show the result

c.

Show the full solution

Statements written directly in the body of an architecture are concurrent: each assignment is a separate driver of a. Two drivers with different values ('1' and '0') make the signal 'X' in simulation (conflict) and cannot be synthesised: it is a design error, so a. is false. Answer b. is false too: the same two lines are legal inside a process, where they are sequential and the last assignment wins (a ends at '0'), and this is used in processes of any design, not only in test benches. Hence c.

Remark on the text: a VHDL compiler does accept the two lines (std_logic is a resolved type), so "no errors" is true only at compilation; the error appears at simulation (XX) and at synthesis, which is why a. is not the intended answer. Theory: VHDL - Concurrent Statements, Processes and TestbenchesConcurrent VHDL statements are the plain signal assignment, when-else (priority logic, conditions evaluated in order) and with-select (parallel logic, every case covered once, others required). A process is a concurrent statement with a sequential body (if, case, loop) that wakes up when a signal in its sensitivity list changes; for combinational logic the list holds all inputs; signals are updated when the process suspends and the last assignment wins. An if without else or an uncovered case creates unwanted memory. A testbench has an empty entity, the instantiated DUT and a stimulus process with wait statements.VHDL - Concurrent Statements, Processes and Testbenches →, VHDL - Entity, Architecture, Types and Description LevelsVHDL is a hardware description language, not a programming language: its statements are concurrent and describe blocks of circuit, used to document, simulate and synthesise. A block has an entity (name and in/out ports) and at least one architecture (what is inside) written in structural style with components and port map, dataflow style with Boolean equations, or behavioral style. Types include std_logic with values 0, 1, X, Z, U and -, std_logic_vector, bit, boolean and integer; signal is used for internal connections and constant for fixed values.VHDL - Entity, Architecture, Types and Description Levels →.

Quiz 12

A VHDL description of dataflow type:

  • a. always leads to a more optimised synthesis than a behavioral description
  • b. makes exclusive use of direct instantiations
  • c. none of the above
Show the result

c.

Show the full solution

Quiz 13

The two's complement representation of the number −3-3 on 8 bits is:

  • a. 11111101
  • b. 11111100
  • c. 10000011
Show the result

a. (1111110111111101)

Show the full solution

Quiz 14

In a Mealy finite state machine:

  • a. the state and the outputs always change at the same moment
  • b. the state is a function of the output
  • c. none of the above
Show the result

c.

Show the full solution

Quiz 15

The setup time is:

  • a. the time for which the input signal of a flip-flop must remain stable before the arrival of the clock edge, to guarantee that the data is captured
  • b. the time for which the input signal of a flip-flop must remain stable after the arrival of the clock edge, to guarantee that the data is captured
  • c. the propagation time of a flip-flop
Show the result

a.

Show the full solution

Quiz 16

A sequential access memory:

  • a. allows access to memory locations at consecutive addresses faster than at arbitrary addresses
  • b. allows access to all memory locations at the same speed, regardless of the order of the accesses
  • c. none of the above
Show the result

a.

Show the full solution

In a sequential (serial) access memory, such as a magnetic tape or a CD, the access time depends on the position of the data, so consecutive locations are faster than arbitrary ones. Answer b. describes a random access memory (RAM). Theory: Main Memory - RAM, ROM and Chip OrganisationA memory is an array of cells (words of several bits) with control circuitry; memories are classified as read-only or read/write, random, serial or hybrid access, volatile or non-volatile. A RAM of 2^k x n bits has k address bits (independent of n), n data bits, read/write and chip select. SRAM uses a bistable cell (latch), is fast and needs no refresh; DRAM stores charge in a capacitor, is denser and needs refresh. Chips use row and column decoders (coincident selection), tri-state outputs, and arrays of chips for more words or more bits per word.Main Memory - RAM, ROM and Chip Organisation →, Memories - ROM, RAM, SRAM and DRAMA memory is a set of cells organised in words, with control circuitry. It is classified as read-only (ROM) or read/write, random access (RAM), serial (SAM) or hybrid (Flash), volatile (SRAM, DRAM) or non-volatile. A RAM 2^k x n has k address bits (independent of n), n data bits, read/write and chip select. SRAM uses a bistable latch cell, is fast and needs no refresh; DRAM stores charge on a capacitor, is denser and needs refresh. Chips are organised as a row/column matrix with coincident selection and tri-state outputs, and combined in arrays for more words or more bits per word.Memories - ROM, RAM, SRAM and DRAM →.

Quiz 17

The instruction register contains:

  • a. the address of the next instruction to execute
  • b. the number of instructions executed
  • c. the instruction currently being executed
Show the result

c.

Show the full solution

Quiz 18

A microprocessor with a microprogrammed decoding unit:

  • a. allows operations of any complexity to be performed in a single clock cycle
  • b. allows instructions that require more clock cycles to be implemented
  • c. none of the above
Show the result

b.

Show the full solution

A microprogrammed control unit is a state machine that executes an instruction as a sequence of steps, one per clock cycle: complex instructions simply take more cycles. No control unit can do an arbitrarily complex operation in one cycle (a. false). Theory: Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle ControlThe 16-bit instruction word has an opcode of 7 bits plus register fields (DR, SA, SB or an immediate OP) or a branch format with a 6-bit PC-relative signed offset; the instruction set is load/store with ADD, SUB, logic, shifts, LDI, ADI, LD, ST, BRZ, BRN and JMP. In the single-cycle computer (separate instruction and data memories) a combinational instruction decoder derives MB, MD, RW, MW, PL, JB, BC from bits 15, 14, 13 and 9 and FS from bits 12 to 9, and the clock period is the sum of the delays on the critical path (9.8 ns in the example). In the multi-cycle computer (one memory) an instruction register, internal registers R8 to R15 and a sequential decoder are needed: fetch (INF) plus one or more execution states.Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle Control →, Machine Instructions, Addressing Modes, RISC and CISCA machine instruction is a fixed-width word split into an opcode and operand fields; in a load/store architecture only load and store reach memory, and operands are given by a few addressing modes (register, register indirect, immediate, PC-relative). RISC machines use simple fixed-length instructions that fit one pass through the pipeline, 32 registers with R0 equal to 0 and four addressing modes; CISC machines have complex variable-length instructions, many addressing modes and a microprogrammed control (a ROM-based state machine) taking several cycles.Machine Instructions, Addressing Modes, RISC and CISC →.

Quiz 19

What is the correct sequence of the functional units in a 4-stage microprocessor pipeline?

  • a. IF, DOF, EX, WB
  • b. DOF, IF, WB, EX
  • c. IF, DOF, WB, EX
Show the result

a.

Show the full solution

Quiz 20

An interrupt:

  • a. is an output signal of the CPU and an input signal of the peripheral
  • b. is an input signal of the CPU and an output signal of the peripheral
  • c. is a bidirectional signal between the CPU and the peripheral
Show the result

b.

Show the full solution

Exercise 1

Minimise the logic equation controlling segment 2 of the following seven-segment display using Karnaugh maps. The circuit has as inputs the variables a b c da\,b\,c\,d, which represent the bits of the number to display, from 0 to 9, from the least significant (aa) to the most significant (dd), and as output yy, which must be 11 when the segment has to be lit and 00 otherwise.

The segments are numbered like this (the figure of the sheet):

  0 0 0
5       1
5       1
5       1
  6 6 6
4       2
4       2
4       2
  3 3 3

Report the Karnaugh map with the groupings used to simplify the function and the final equation obtained, exploiting the don't care conditions in a suitable way.

Show the result

Segment 2 is the lower right one: it is lit for all digits except 22. Inputs d c b ad\,c\,b\,a (dd most significant); y=∑m(0,1,3,4,5,6,7,8,9)y=\sum m(0,1,3,4,5,6,7,8,9) with don't cares 10,…,1510,\dots,15. Three groups of 8 cells: y=a+b‾+cy=a+\overline{b}+c

Show the full solution

Step 1: which digits light segment 2. In the figure segment 2 is the lower right vertical bar. In the usual shapes of the digits (Decoders, Encoders and MultiplexersAn n-to-m decoder converts a binary input into a one-hot output whose lines are the minterms of the inputs (m AND gates; hierarchical construction lowers the input cost; enable). Any function equals a decoder plus an OR of its minterms. The encoder does the inverse but fails with several or no active inputs; the priority encoder fixes it with a priority and a valid output V. A 2^n-to-1 multiplexer computes Y = sum of m_i(S) I_i and implements any n-variable function; a smaller MUX uses the last variable (0, 1, X, not X) as data.Decoders, Encoders and Multiplexers →, BCD to seven segments) it is lit for 0,1,3,4,5,6,7,8,90,1,3,4,5,6,7,8,9 and off only for 22 (the digit 22 uses top, upper right, middle, lower left and bottom). The doubtful shapes (66 with or without the top bar, 77 with or without the upper left bar, 99 with or without the bottom bar) do not change segment 2.

Step 2: truth table. Four bits are needed for 00 to 99; the six combinations 1010,…,11111010,\dots,1111 never occur and are don't care (Karnaugh Maps and Two-Level MinimizationA Karnaugh map is the truth table arranged in a grid with rows and columns in Gray code, so that adjacent cells (also across opposite edges) differ in one variable. Groups of 2^k ones eliminate k variables and give a product. A prime implicant is a maximal group, an essential one is the only cover of some minterm; the minimum cover has all essentials plus few other primes. For POS, group the zeros. Don't cares may be used as 1 or 0. XOR functions have a checkerboard map and do not simplify.Karnaugh Maps and Two-Level Minimization →).

number d c b ad\,c\,b\,a yy
0 0000 1
1 0001 1
2 0010 0
3 0011 1
4 0100 1
5 0101 1
6 0110 1
7 0111 1
8 1000 1
9 1001 1
10-15 1010-1111 X

Step 3: Karnaugh map. Rows d cd\,c, columns b ab\,a, both in Gray order 00,01,11,1000,01,11,10 so that adjacent cells differ by one variable:

dc\badc \backslash ba 00 01 11 10
00 1 1 1 0
01 1 1 1 1
11 X X X X
10 1 1 X X

(the only 00 is the cell 00100010, number 22; the cells of the row dc=10dc=10 are 8,9,11,108,9,11,10).

Step 4: groupings. With the don't cares used as 11 where useful, three groups of 8 cells, the largest possible (Karnaugh Maps and Two-Level MinimizationA Karnaugh map is the truth table arranged in a grid with rows and columns in Gray code, so that adjacent cells (also across opposite edges) differ in one variable. Groups of 2^k ones eliminate k variables and give a product. A prime implicant is a maximal group, an essential one is the only cover of some minterm; the minimum cover has all essentials plus few other primes. For POS, group the zeros. Don't cares may be used as 1 or 0. XOR functions have a checkerboard map and do not simplify.Karnaugh Maps and Two-Level Minimization →):

  • columns ba=01ba=01 and 1111 (all four rows): a=1a=1 constant, giving the term aa (cells 1,3,5,7,9,11,13,151,3,5,7,9,11,13,15);
  • rows dc=01dc=01 and 1111 (all four columns): c=1c=1 constant, giving the term cc (cells 4,5,6,7,12,13,14,154,5,6,7,12,13,14,15);
  • columns ba=00ba=00 and 0101 (adjacent, all four rows): b=0b=0 constant, giving the term b‾\overline b (cells 0,1,4,5,8,9,12,130,1,4,5,8,9,12,13).

All three are essential: cell 33 (00110011) is covered only by the first group, cell 66 (01100110) only by the second, cell 00 (00000000) only by the third. The don't care 10101010 is left uncovered (treated as 00): it is not needed, since the three groups already cover all the 11s.

Step 5: equation. y=a+b‾+cy=a+\overline{b}+c Meaning: segment 2 is off only when a=0a=0, b=1b=1, c=0c=0 (that is, the number is 22 or the don't care 10101010), whatever dd is: y=a‾ b c‾‾y=\overline{\overline a\,b\,\overline c} by De Morgan (Boolean Algebra - Axioms, Theorems and Function ComplementBoolean algebra works on two-valued variables with AND, OR, NOT. Fundamental identities (identity element, idempotence, complement, commutative, associative, both distributive laws) come in dual pairs; De Morgan, absorption, adjacency and consensus are the main theorems, proved by perfect induction or algebraically. The complement of a function is obtained with De Morgan or with the dual plus literal negation. Cost is measured in literals or gate inputs.Boolean Algebra - Axioms, Theorems and Function Complement →). The circuit is one inverter and one 3-input OR. The same function can be written as a single product of sums, y=(a+b‾+c)y=(a+\overline b+c), so the POS and SOP minimisations coincide.

Check. The equation gives 11 for all nine digits other than 22 and 00 for 22. A script also compared it with the minimisation of sympy (SOP and POS, with the don't cares): same result.

Without don't cares the minimal form would be y=d‾ a+d‾ c+b‾ c‾y=\overline d\,a+\overline d\,c+\overline b\,\overline c, with 6 literals instead of 3.

Typical mistakes: reading aa as the most significant bit (the sheet says aa is the least significant and dd the most significant); mixing up segment 2 with segment 1 (which is also lit for 22); not using the don't cares; ordering the rows or columns of the map in binary instead of Gray order; leaving y=a‾ b c‾‾y=\overline{\overline a\,b\,\overline c} unsimplified.

Exercise 2

Draw 1) the symbol with inputs and outputs and 2) the state diagram, without equivalent states, complete with all transitions and all signals, of a coffee machine with these characteristics:

  • there are two buttons, coffee and cappuccino, that generate a pulse of one clock cycle when pressed on the lines caffè_btn and capp_btn respectively;
  • the machine has a coffee dispenser and a milk dispenser, activated by a pulse of one clock cycle on the lines caffè_er and latte_er;
  • when the coffee button is pressed, the machine must activate the coffee dispenser for one whole clock cycle;
  • when the cappuccino button is pressed, the machine must activate the milk dispenser for two whole clock cycles and then the coffee dispenser for one whole clock cycle;
  • while milk or coffee is being dispensed, any button presses are ignored.
Show the result

Moore machine with 4 states, all pairwise non-equivalent (outputs caffè_er, latte_er):

  • IDLE (0,00,0): caffè_btn=1 goes to CAFFE; otherwise capp_btn=1 goes to LATTE1; otherwise stays in IDLE;
  • LATTE1 (0,10,1) goes to LATTE2 whatever the inputs;
  • LATTE2 (0,10,1) goes to CAFFE whatever the inputs;
  • CAFFE (1,01,0) goes to IDLE whatever the inputs.

Cappuccino pressed in cycle tt: latte_er=1 in cycles t+1t+1 and t+2t+2, caffè_er=1 in cycle t+3t+3. Symbol: inputs caffè_btn, capp_btn, clock, reset; outputs caffè_er, latte_er.

Show the full solution

Step 1: symbol. A rectangle with four inputs on the left, caffè_btn, capp_btn, clock, reset, and two outputs on the right, caffè_er and latte_er (all of one bit). The text does not mention a reset, but a state machine needs one to start from a known state (Finite State Machines - SynthesisSynthesis of a synchronous sequential circuit follows specifications, state diagram, state table (merging equivalent states), binary state encoding, choice of D flip-flop, flip-flop input and output equations, minimisation, circuit and verification. A state summarises the useful part of history; a sequence recogniser has one state per recognised prefix, reusing states for overlaps. A reset gives a known initial state. Gray encoding (minimum flip-flops) versus one-hot (one flip-flop per state, simpler logic): for the 1101 recogniser Gray costs about half.Finite State Machines - Synthesis →).

Step 2: type of machine and states. The outputs are fixed pulses of a whole clock cycle, decided by what the machine is doing and not by the instantaneous value of the buttons: a Moore machine, with the outputs written inside the states (Finite State Machines - AnalysisAnalysing a synchronous sequential circuit means deriving from the circuit the flip-flop input equations (next state) and output equations, the state table (2^(m+n) rows for m flip-flops and n inputs), the state diagram (circles are states, arrows carry input/output) and a timing simulation. Mealy: output depends on state and input (written on the arrows, may change between clock edges); Moore: output depends only on the state (written in the circle, changes only at the clock edge). Two states are equivalent if they give the same outputs and lead to equivalent states, and can be merged to save flip-flops.Finite State Machines - Analysis →). The states are the situations to be remembered:

  • IDLE: waiting for a button, both outputs 00;
  • LATTE1 and LATTE2: the first and the second cycle of milk, latte_er=1; two states are needed because the machine must count two cycles and a state cannot count by itself;
  • CAFFE: the cycle with the coffee dispenser, caffè_er=1.

Step 3: transitions (X means "either value"):

state outputs caffè_er, latte_er caffè_btn, capp_btn next state
IDLE 0, 0 1X CAFFE
IDLE 0, 0 01 LATTE1
IDLE 0, 0 00 IDLE
LATTE1 0, 1 XX LATTE2
LATTE2 0, 1 XX CAFFE
CAFFE 1, 0 XX IDLE

Description to redraw the diagram: four circles; entry arrow (reset) on IDLE; IDLE with a loop labelled caffè_btn=0, capp_btn=0; IDLE →\to CAFFE labelled caffè_btn=1; IDLE →\to LATTE1 labelled caffè_btn=0, capp_btn=1; LATTE1 →\to LATTE2, LATTE2 →\to CAFFE and CAFFE →\to IDLE with no condition (or X): in these states the buttons are ignored, as required. Inside each circle the name of the state and the two outputs, for example LATTE1 / caffè_er=0, latte_er=1.

The text does not say what happens if both buttons are pressed in the same cycle: here the coffee wins (any choice is accepted if it is stated, for example ignoring both).

Step 4: no equivalent states. Two states are equivalent only if they give the same outputs and lead to equivalent states (Finite State Machines - AnalysisAnalysing a synchronous sequential circuit means deriving from the circuit the flip-flop input equations (next state) and output equations, the state table (2^(m+n) rows for m flip-flops and n inputs), the state diagram (circles are states, arrows carry input/output) and a timing simulation. Mealy: output depends on state and input (written on the arrows, may change between clock edges); Moore: output depends only on the state (written in the circle, changes only at the clock edge). Two states are equivalent if they give the same outputs and lead to equivalent states, and can be merged to save flip-flops.Finite State Machines - Analysis →). IDLE and CAFFE have different outputs from LATTE1 and LATTE2 and from each other, so the only pair to examine is LATTE1 and LATTE2: they have the same output (0,1)(0,1), but the next states are LATTE2 and CAFFE, which differ in the output (0,10,1 against 1,01,0). So LATTE1 and LATTE2 are not equivalent: after LATTE1 the coffee comes after two cycles, after LATTE2 after one. The machine has the minimum number of states for a Moore machine.

Step 5: simulation. Cappuccino pressed in cycle 1, then, while the machine is busy, the buttons are pressed again in cycles 2 and 3 (ignored); coffee pressed in cycle 7:

cycle 0 1 2 3 4 5 6 7 8 9 10
state IDLE IDLE LATTE1 LATTE2 CAFFE IDLE IDLE IDLE CAFFE IDLE IDLE
caffè_btn 0 0 1 0 0 0 0 1 0 0 0
capp_btn 0 1 0 1 0 0 0 0 0 0 0
caffè_er 0 0 0 0 1 0 0 0 1 0 0
latte_er 0 0 1 1 0 0 0 0 0 0 0

The milk is on for two whole cycles (2 and 3), then the coffee for one cycle (4), and the presses in cycles 2 and 3 have no effect. A coffee pressed in cycle 7 gives the coffee dispenser in cycle 8, one cycle later, because in a Moore machine the output follows the state.

Variant (Mealy). With the outputs on the arrows the same behaviour needs only 3 states: IDLE with caffè_btn=1 / caffè_er=1 as a loop, IDLE →\to L2 with capp_btn=1 / latte_er=1, L2 →\to C with latte_er=1, C →\to IDLE with caffè_er=1. The outputs appear one cycle earlier than in the Moore machine, in the same cycle as the button pulse; the simulation confirmed that it is the Moore output advanced by one cycle. The sheet asks for a diagram without equivalent states, which both solutions satisfy.

Typical mistakes: only one milk state (the milk would last one cycle); the coffee dispenser forgotten after the milk; transitions from the dispensing states that look at the buttons (they must be ignored); outputs on the arrows in a Moore diagram; no entry arrow or no reset; merging LATTE1 and LATTE2 in the state reduction (the outputs are equal, but the next states are not equivalent).

Exercise 3

  1. Describe in VHDL the finite state machine with inputs xx and yy and output zz, having the following state diagram. In all cases not indicated, the system does not change state and the output is 00. Separate in the code the memory elements from the purely combinational parts.
  2. Create a VHDL test bench that makes the machine go through all the states.

Diagram (described in words): four states S0S0, S1S1, S2S2, S3S3; the output is written on the arrows, under the condition, in the form condition over zz. An entry arrow with the label rst=1 / z=0 points to S0S0 (reset state). Arrows:

  • S0→S1S0\to S1 with x=1 and y=0 / z=1;
  • S1→S2S1\to S2 with x=1 / z=0;
  • S2→S0S2\to S0 without label;
  • S0→S3S0\to S3 with x=0 and y=1 / z=1;
  • S3→S2S3\to S2 with x=0 or y=1 / z=0.
Show the result

Mealy machine (the output is on the arrows). Next state / output zz, for x y=00,01,10,11x\,y=00,01,10,11:

state 00 01 10 11
S0S0 S0/0S0/0 S3/1S3/1 S1/1S1/1 S0/0S0/0
S1S1 S1/0S1/0 S1/0S1/0 S2/0S2/0 S2/0S2/0
S2S2 S0/0S0/0 S0/0S0/0 S0/0S0/0 S0/0S0/0
S3S3 S2/0S2/0 S2/0S2/0 S3/0S3/0 S2/0S2/0

VHDL: entity with clk, rst, x, y in and z out, and three processes (state register with reset, next state, output). Test bench sequence x=0110100101010x=0110100101010, y=0101101001110y=0101101001110 (one pair per cycle after the reset), with z=1z=1 in cycles 22, 66, 1010 only; it visits all four states and all the arrows.

Show the full solution

Step 1: analysis of the diagram. The output is written on the arrows: a Mealy machine, zz depends on the state and on x,yx,y (Finite State Machines - AnalysisAnalysing a synchronous sequential circuit means deriving from the circuit the flip-flop input equations (next state) and output equations, the state table (2^(m+n) rows for m flip-flops and n inputs), the state diagram (circles are states, arrows carry input/output) and a timing simulation. Mealy: output depends on state and input (written on the arrows, may change between clock edges); Moore: output depends only on the state (written in the circle, changes only at the clock edge). Two states are equivalent if they give the same outputs and lead to equivalent states, and can be merged to save flip-flops.Finite State Machines - Analysis →). The inputs xx and yy have one bit each. Reading the text "in all cases not indicated, the system does not change state and the output is 00":

  • stay arrows (not drawn): S0S0 with x y=00x\,y=00 and 1111 (neither condition holds); S1S1 with x=0x=0; S3S3 with x=1x=1 and y=0y=0 (the only case where x=0 or y=1 is false);
  • the arrow S2→S0S2\to S0 has no condition: it is taken always, whatever xx and yy are; its output is not indicated, hence 00;
  • z=1z=1 only on the two arrows leaving S0S0; every other arrow, loop included, has z=0z=0;
  • rst=1 / z=0: with the reset active the machine is in S0S0 and the output is 00.

Step 2: structure. The text asks to separate the memory from the combinational parts: three processes, as in the course (VHDL - Finite State Machines and Sequential TestbenchesA finite state machine is described in behavioral VHDL with an enumerated type for the states and three processes: the state register (reset and state <= next_state on the clock edge), the next state as a combinational function of state and input (case state, with default next_state <= state), and the output as a combinational function of the state alone (Moore) or of state and input (Mealy). The testbench has a clock process and an input process (initial reset, then a test sequence that visits all states).VHDL - Finite State Machines and Sequential Testbenches →):

  1. state register: sensitive to clk and rst, asynchronous reset to S0S0, otherwise state <= next_state on the rising edge (VHDL - Latches, Flip-Flops and ResetsIn VHDL a memory element is a process in which some branch does not assign the signal: the D latch is process(D,C) with if C='1' then Q<=D, the positive-edge-triggered flip-flop is process(CLK) with if CLK'event and CLK='1'. Asynchronous reset: RST is in the sensitivity list and tested first (if RST='1' ... elsif clock edge); synchronous reset: only CLK in the list and RST tested inside the clock-edge branch, so a clock edge is needed for it to act. The testbench of a synchronous circuit has a process generating the clock and one applying inputs away from the active edge.VHDL - Latches, Flip-Flops and Resets →);
  2. next state: purely combinational, sensitive to state, x and y, with the default next_state <= state for "does not change state";
  3. output: purely combinational and, being a Mealy machine, sensitive to state, x, y (and rst, which forces z=0z=0 as in the label rst=1 / z=0).

Step 3: code of the machine.

vhdl
library IEEE;
use IEEE.std_logic_1164.all;

entity fsm_xy is
  port ( clk, rst : in  std_logic;
         x, y     : in  std_logic;
         z        : out std_logic );
end fsm_xy;

architecture beh of fsm_xy is
  type state_type is (S0, S1, S2, S3);
  signal state, next_state : state_type;
begin
  -- 1) memory: state register with asynchronous reset
  state_register : process (clk, rst)
  begin
    if rst = '1' then
      state <= S0;
    elsif clk'event and clk = '1' then
      state <= next_state;
    end if;
  end process;

  -- 2) next state: combinational
  next_state_comb : process (state, x, y)
  begin
    next_state <= state;                       -- default: the state does not change
    case state is
      when S0 =>
        if x = '1' and y = '0' then
          next_state <= S1;
        elsif x = '0' and y = '1' then
          next_state <= S3;
        end if;
      when S1 =>
        if x = '1' then
          next_state <= S2;
        end if;
      when S2 =>
        next_state <= S0;                      -- arrow without condition
      when S3 =>
        if x = '0' or y = '1' then
          next_state <= S2;
        end if;
    end case;
  end process;

  -- 3) output (Mealy): function of state and inputs
  output_comb : process (state, x, y, rst)
  begin
    z <= '0';                                  -- default: output 0
    if rst = '0' and state = S0 then
      if (x = '1' and y = '0') or (x = '0' and y = '1') then
        z <= '1';
      end if;
    end if;
  end process;
end beh;

Notes on the code:

  • the default next_state <= state; and the default z <= '0'; express "in all cases not indicated the state does not change and the output is 00", and prevent unwanted latches because every signal has a value in every branch;
  • the condition of S3S3 can also be written if not (x = '1' and y = '0') then; the machine leaves S3S3 unless x=1x=1 and y=0y=0;
  • the arrow S2→S0S2\to S0 is an assignment without if; the case covers the four values of the enumerated type, so when others is not needed;
  • the output process needs x and y in the sensitivity list: without them zz would not react to a change of the inputs between two clock edges (a typical error that turns a Mealy machine into a Moore one);
  • the test rst = '0' in the output process realises the label rst=1 / z=0; without it the output could be 11 during the reset if x≠yx\ne y, although the state is already S0S0.

Step 4: test bench. The test bench has no ports, instantiates the machine, generates the clock and applies the inputs: two processes in parallel (VHDL - Concurrent Statements, Processes and TestbenchesConcurrent VHDL statements are the plain signal assignment, when-else (priority logic, conditions evaluated in order) and with-select (parallel logic, every case covered once, others required). A process is a concurrent statement with a sequential body (if, case, loop) that wakes up when a signal in its sensitivity list changes; for combinational logic the list holds all inputs; signals are updated when the process suspends and the last assignment wins. An if without else or an uncovered case creates unwanted memory. A testbench has an empty entity, the instantiated DUT and a stimulus process with wait statements.VHDL - Concurrent Statements, Processes and Testbenches →). The sequence must pass through all the states, and it is better if it also uses the stay arrows and both conditions of S3→S2S3\to S2.

vhdl
library IEEE;
use IEEE.std_logic_1164.all;

entity test_fsm_xy is
end test_fsm_xy;

architecture test of test_fsm_xy is
  signal clock, reset, x, y, z : std_logic;
  signal test_x : std_logic_vector(0 to 12) := "0110100101010";
  signal test_y : std_logic_vector(0 to 12) := "0101101001110";
  constant PERIOD : time := 100 ns;
begin
  DUT : entity work.fsm_xy(beh)
    port map (clk => clock, rst => reset, x => x, y => y, z => z);

  generate_clock : process
  begin
    clock <= '1'; wait for PERIOD/2;
    clock <= '0'; wait for PERIOD/2;
  end process;

  apply_inputs : process
  begin
    reset <= '1'; x <= '0'; y <= '0';
    wait for 3*PERIOD/2;                       -- initial reset: known starting state
    reset <= '0';
    for i in 0 to 12 loop
      x <= test_x(i);                          -- one pair (x, y) per cycle,
      y <= test_y(i);                          -- applied on the falling edge
      wait for PERIOD;
    end loop;
    std.env.stop;
  end process;
end test;

The clock rises at 0,100,200,…0,100,200,\dots ns; the reset is active from 00 to 150150 ns; the pair of cycle ii is applied at 150+100i150+100i ns, on the falling edge and far from the rising edge that samples it at 200+100i200+100i ns.

Step 5: expected behaviour (state present in the cycle, zz during the cycle, state after the next rising edge):

cycle 0 1 2 3 4 5 6 7 8 9 10 11 12
xx 0 1 1 0 1 0 0 1 0 1 0 1 0
yy 0 1 0 1 1 0 1 0 0 1 1 1 0
present state S0S0 S0S0 S0S0 S1S1 S1S1 S2S2 S0S0 S3S3 S3S3 S2S2 S0S0 S3S3 S2S2
zz 0 0 1 0 0 0 1 0 0 0 1 0 0
next state S0S0 S0S0 S1S1 S1S1 S2S2 S0S0 S3S3 S3S3 S2S2 S0S0 S3S3 S2S2 S0S0

What each part of the sequence tests: cycles 0,10,1 the stay arrows of S0S0; cycle 22 the arrow S0→S1S0\to S1 with z=1z=1; cycle 33 the stay arrow of S1S1 (x=0x=0); cycle 44 S1→S2S1\to S2; cycles 5,9,125,9,12 the unconditional arrow S2→S0S2\to S0; cycles 6,106,10 the arrow S0→S3S0\to S3 with z=1z=1; cycle 77 the stay arrow of S3S3 (x=1,y=0x=1,y=0); cycle 88 S3→S2S3\to S2 through x=0; cycle 1111 S3→S2S3\to S2 through y=1 (with x=1x=1). Being a Mealy output, zz rises as soon as the inputs of cycles 22, 66, 1010 are applied (falling edge) and falls when the next ones arrive, not at a clock edge.

The behaviour of the next-state and output processes was checked against the transition table on all 16 combinations of state and inputs with a Python model, and the sequence above on that model; the VHDL was checked by hand against the table, but it was not run in a VHDL simulator.

Typical mistakes:

  • writing the output as a function of the state only (that would be a Moore machine and would give wrong values for zz);
  • forgetting state in the sensitivity list of the next-state process, or x, y in that of the output process;
  • putting a condition on the arrow S2→S0S2\to S0, or reading x=0 or y=1 as x=0 and y=1 (an S3S3 with x=0,y=0x=0,y=0 must leave);
  • writing memory and logic in the same process, against the request of the text;
  • a test bench that never reaches S3S3 (it needs x=0x=0, y=1y=1 from S0S0) or that changes the inputs on the rising edge;
  • forgetting the initial reset in the test bench: the state would be undefined at the start.
Self-assessment grid
exercise points what must be there for full points typical mistakes
Quiz 1-20 not stated on the sheet (negative score for a wrong answer) the correct answer for each of the 20 questions; for the ambiguous ones (4, 11) the reasoning confusing setup and hold (15), top-down and bottom-up (2), IR and PC (17), interrupt direction (20); applying a rule to the wrong form (7, 8)
Exercise 1 not stated truth table with the don't cares for 1010-1515, map with the three groups of 8 cells, y=a+b‾+cy=a+\overline b+c bit order (aa is the least significant), segment 2 mistaken for another, don't cares not used, map not in Gray order
Exercise 2 not stated symbol with the four inputs and two outputs, diagram with 4 states and outputs inside the states, transitions of IDLE with the buttons, buttons ignored in the other states, no equivalent states one milk state only, coffee missing after the milk, buttons read while dispensing, outputs on the arrows of a Moore diagram
Exercise 3 not stated three processes (memory with reset, next state, output) with default values, correct conditions of S0S0 and S3S3, unconditional S2→S0S2\to S0, a test bench with clock, reset and input sequence through all states Moore output instead of Mealy, missing sensitivity signals, and and or swapped on S3S3, no reset in the test bench