summaryrefslogtreecommitdiff
path: root/17.md
blob: 33196ede8f526ccaedf6ed3c60c526e64b6c3130 (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
145
146
147
148
149
150
151
152
[\<- Procedural Verilog and specifying sequential circuits in Verilog](16.md)

---

# Counters

## Counter concepts

### What is a counter?

- Multi-bit state to represent a count value
- Every cycle the count changes (possibly)
	- "Normal" counting adds 1 each cycle
	- Count value "wraps" around to 0 after max \#
		- In the below example, the max \# is 7, as it goes back to zero when it goes from 7 to 8

![diagram](17.1.png)

- The above diagram is **not** a truth table

### Designing a counter

- Same sequential design concepts
	- Define logic to generate inputs to flops
	- Current count value is the input to the logic
	- Next count value is the output => flop inputs

![diagram](17.2.png)

### Designing the logic

- Truth table for "next count" logic
	- N-bit counter has 2^N rows
	- Input is Q, output is D
	- For each row, output is the next value in the sequence

|Q2Q1Q0|D2D1D0|
|------|------|
|000   |001   |
|001   |010   |
|010   |011   |
|011   |100   |
|100   |101   |
|101   |110   |
|110   |111   |
|111   |000   |

---

## "Abnormal" counting

- Works for any type of counting
	- Count by 1, 2, up, down, etc.
	- Different ranges (start and end points)
		- Use don't cares for values not in range
- Counting from 0 to 5 means going back to 0 once 5 is reached
	- 6 and 7 are don't cares

|Q2Q1Q0|D2D1D0|
|------|------|
|000   |001   |
|001   |010   |
|010   |011   |
|011   |100   |
|100   |101   |
|101   |000   |
|110   |ddd   |
|111   |ddd   |

### Another example: count by 3

- Sequence is 000, 011, 001, 100, 111, 010, 101, 000, etc.
- Note the left hand side of the truth table is \*exactly\* like any other 3-input table

|Q2Q1Q0|D2D1D0|
|------|------|
|000   |011   |
|001   |100   |
|010   |101   |
|011   |110   |
|100   |111   |
|101   |000   |
|110   |001   |
|111   |010   |

---

## Counter controls: count enable and parallel load

### Counter controls

- Count enables are common
	- Stop counting but hold the current value
	- Can be factored into input logic or tied to "embedded" load enable
- Parallel load allows counter to start at a certain value
	- Needs a mux to select between next count value and new value to load
- Combination of the two (count enable and parallel load) can be done in different ways

### Count Enable & Parallel Load

![diagram](17.3.png)

---

## The abstraction of a counter

- Because counters are common, they are often represented in larger circuits as an abstraction
	- Typically count by 1, full range, and wrap
	- As with other abstractions, once we've learned how they work on the inside, it's useful to be able to step away from the details

![diagram](17.4.png)

### A loadable counter

- An intermediate-level abstraction to help think about how a loadable counter works

![diagram](17.5.png)

---

## Using/re-purposing a counter

### Repurposing a counter

- Using a 3-bit counter abstraction
	- Normally counts from 0 to 7 (modulo 8)
- Modify it to count from 0 to 5 (modulo 6)
	- Parallel load 0 when the count reaches 5
- In this example, count is always enabled

![diagram](17.6.png)

### A BCD Counter

- Binary-coded-decimal counts from 0 to 9
- Second digit counts when first reaches 9

![diagram](17.7.png)

---

## Summary: designing from scratch vs re-purposing

### From scratch vs repurposing

- From scratch means creating the truth table and working out the logic yourself
- Re-purposing means using a counter that has already been designed, and taking advantage of the provided control signals, like parallel load and count enable
	- Control signals (inputs) \*must\* be set to something, cannot be left hanging

---

[State machine concepts ->](18.md)