summaryrefslogtreecommitdiff
path: root/22.md
blob: 0412fa66ad2aaf25d4cee1770562189a34fc1904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
[\<- Multiple control outputs](21.md)

---

# Mealy machines

## Mealy state machine concept

### Mealy State Machine

- Often it is useful to have output to be a function of both current state \*and\* the current set of inputs
	- Called a Mealy-style state machine
	- Previous examples were Moore-style state machines; outputs are a function of only the current state

![diagram](22.1.png)

### Sequence detector, revisited

- Revisiting example 1, what if we wanted output to assert as soon as we see the second cycle of input?
	- Need output to "respond" in the same cycle as the input, not wait a cycle as we saw before

![diagram](22.2.png)

---

## Mealy state diagrams and tables

![diagram](22.3.png)

### Circuit and Timing Diagram

![diagram](22.4.png)

### State Diagram for Example 2

![diagram](22.5.png)

- Notice that the Mealy machine takes fewer states

### Mealy vs Moore

- It depends on whether we want the output to change as soon as the input changes
	- Think about a truth table for the outputs as a function of the inputs
		- Must also include some state, else not a state machine
- Consider a counter, with a count enable, as a state machine
	- When the count enable changes, do we want the count value to change

---

## Serial adder block diagram

- We learned how to do addition earlier in the quarter, and we saw how to implement and chain together full adders
- A different approach would be to do one bit-position at a time using shift registers: two operand registers and a result register
- Every cycle, shift out the lowest bits of the operands, compute the sum and shift into the result register
- Shift registers need to be loaded and shifted, but let's just focus on the addition piece

![diagram](22.6.png)

---

## Serial adder state diagram and table

### Think about the output

- If we were to try to create a truth table for 's' as a function of 'a' and 'b', could we do it?
	- Maybe for first bit, but after that it depends on whether there was a carry
	- Since carry is from the "past", it is a type of state

|ab|s|
|--|-|
|00|?|
|01|?|
|10|?|
|11|?|

### State diagram

- Note that the output is specified on the transition arcs, not in the state bubbles

![diagram](22.7.png)

### State table

- The fact that the ouputs are on the arcs means the output section needs the same structure as the next state section
	- A column for each input combination

![diagram](22.8.png)

---

## Verilog for Mealy state machines

- Generally, follow the same approach:
	- Interface: inputs a, b, clock; output s
	- State variable to differentiate the two states, defined as reg
	- Next state variable, also defined as reg
	- Separate always block for state update
		- `always @(posedge clock)`
- But, combine next state and output logic
	- Case structure is the same
	- Output must be of type reg (since now in always block)

### Example always block

```
always @(*)
begin
	nState = cState; //most scenarios stay in same state
	case(cState)
		G: begin //needed because there are 2 statements here
			if({a, b} == 2'b11) nState = H; //one exception
			s = a^b; //easier to just write equation
		end
		H: begin //same
			if(A{a, b} == 2'b00) nState=G; //another exception
			s = !(a ^ b); //equation is different than state G
		end
	endcase
end
```

### Another approach

- Have `{a, b}` be the object of the case statement

```
always @(*)
	case({a, b})
		2'b00: begin
			nState = G; s = (cState == H);
		end
		2'b01:
		2'b10: begin
			nState = cState; s = (cState == G);
		end
		2'b11: begin
			nState = H; s = (cState == H);
		end
	endcase
```

---

[Decoders and register files ->](23.md)