---
title: Problem set 10
heading: "Problem set 10: Verilog, design and logic in code"
eyebrow: C7 · Problem set 10
type: problems
---

::: hook
These problems use lessons 48 to 51: reading Verilog, writing Verilog, the adder and vending-machine sandbox, and the logic lab.
:::

::: pool
pick: 12

::: check
kind: parts
given: m = 7 | 15 | 31; k = 0 | 4

## A module declares `wire [{m}:0] bus;`.

::: part
answer: m + 1
tolerance: 0

### How many wires is that?

::: misses
- [any] Inclusive at both ends.
:::
:::

::: part
answer: m - k + 1
tolerance: 0

### How many bits does `bus[{m}:{k}]` select?

::: misses
- [any] From {m} down to {k}, inclusive.
:::
:::
:::

::: check
kind: parts
given: a = 3 | 7 | 12; b = 5 | 9

## Registers a = {a} and b = {b} run one clock of `a <= b; b <= a + b;` (nonblocking).

::: part
answer: b
tolerance: 0

### What is a afterward?

::: misses
- [any] It takes b's old value.
:::
:::

::: part
answer: a + b
tolerance: 0

### What is b afterward?

::: misses
- [any] Nonblocking: b uses a's old value.
:::
:::
:::

::: check
kind: parts
given: a = 3 | 7 | 12; b = 5 | 9

## The same registers (a = {a}, b = {b}) instead run `a = b; b = a + b;` (blocking) once.

::: part
answer: b
tolerance: 0

### What is a afterward?

::: misses
- [any] It takes b's value.
:::
:::

::: part
answer: 2*b
tolerance: 0

### What is b afterward?

::: misses
- [any] Blocking: b reads the new a.
:::
:::
:::

::: check
kind: parts
given: w1 = 4 | 8; w2 = 1 | 16; w3 = 3 | 32

## A clocked block assigns `reg [{w1}-1:0] x`, `reg [{w2}-1:0] y` and `reg [{w3}-1:0] z`, all with `<=`.

::: part
answer: w1 + w2 + w3
tolerance: 0

### How many flip-flops does it create?

::: misses
- [any] One per bit.
:::
:::

::: part
answer: 2^w1 - 1
tolerance: 0

### What is the largest value x can hold?

::: misses
- [any] $2^{w} - 1$.
:::
:::
:::

::: check
kind: parts
given: w = 4 | 8; d = 1 | 3; n = 6 | 50; let s = 2^w - d

## A `reg [{w}-1:0] c` starts at {s} and runs `c <= c + 1;` each clock.

::: part
answer: s + n - 2^w*floor((s + n)/2^w)
tolerance: 0

### What does it hold after {n} clocks?

::: misses
- [any] Wrap modulo $2^w$.
:::
:::

::: part
answer: floor((s + n)/2^w)
tolerance: 0

### How many times has it wrapped through 0?

::: misses
- [any] How many multiples of $2^w$ it passed.
:::
:::
:::

::: check
kind: boolean
vars: A, B, C
answer: A & B | ~A & C
ops: and, or, not

## What does `assign y = a ? b : c;` compute? Write it with AND, OR and NOT (inputs A, B, C).

::: misses
- [any] b when a is 1, c otherwise.
:::
:::

::: check
kind: parts
given: A = 3 | 9 | 14; B = 5 | 7 | 12

## A 4-bit ripple-carry adder adds A = {A} and B = {B}.

::: part
answer: A + B - 16*floor((A + B)/16)
tolerance: 0

### What is its 4-bit sum?

::: misses
- [any] The sum modulo 16.
:::
:::

::: part
answer: floor((A + B)/16)
tolerance: 0

### What is its carry out?

::: misses
- [any] 1 when the sum is 16 or more.
:::
:::
:::

::: check
kind: parts
given: n = 4 | 7 | 11

## The sandbox's vending machine sells a 15¢ item for nickels, one coin per clock, and vends on the third.

::: part
answer: floor(n/3)
tolerance: 0

### How many items have vended after {n} nickels?

::: misses
- [any] One per three nickels.
:::
:::

::: part
answer: 5*(n - 3*floor(n/3))
unit: ¢
tolerance: 0

### How many cents is it holding toward the next?

::: misses
- [any] The leftover nickels.
:::
:::
:::

::: check
kind: parts
given: n = 4 | 5 | 6; k = 1 | 2 | 3

## In Quine–McCluskey for {n} variables, a pattern has {k} dashes.

::: part
answer: n - k
tolerance: 0

### How many literals does its term have?

::: misses
- [any] One per 0 or 1.
:::
:::

::: part
answer: 2^k
tolerance: 0

### How many minterms does it cover?

::: misses
- [any] $2^k$.
:::
:::
:::

::: check
kind: parts
given: r = 5 | 11 | 13; i = 0 | 2

## A 4-input truth table numbers its rows with the first input as the top bit.

::: part
answer: floor(r/2^(3 - i)) - 2*floor(r/2^(4 - i))
tolerance: 0

### What is input {i} (counting from 0 at the first input) in row {r}?

::: misses
- [any] Bit 3 − i of {r}.
:::
:::

::: part
answer: 15 - r
tolerance: 0

### Which row has every input inverted?

::: misses
- [any] Flip all four bits.
:::
:::
:::

::: check
kind: parts
given: k = 3 | 6 | 9; L = 10 | 20

## A running-parity Mealy machine outputs the parity of all the 1s seen so far. A stream of {L} bits contains {k} ones.

::: part
answer: k - 2*floor(k/2)
tolerance: 0

### What does it output after the last bit?

::: misses
- [any] The parity of {k}.
:::
:::

::: part
answer: 2
tolerance: 0

### How many states does it need?

::: misses
- [any] Even or odd so far.
:::
:::
:::

::: check
kind: boolean
vars: A, B, C, D
answer: ~B & ~C | C & ~D | ~A & B & D
ops: and, or, not

## Find a minimal cover of $\sum m(0, 1, 2, 5, 6, 7, 8, 9, 10, 14)$ using three of its prime implicants.

::: misses
- [any] Start from the implicants that cover 0, 1, 8 and 9.
:::
:::

::: check
kind: parts
given: N = 6 | 12 | 20; f = 10 | 50

## `always @(posedge clk) c <= c + 1;` with an N = {N}-bit c runs at {f} MHz.

::: part
answer: f*1e6/2^N
unit: Hz
tolerance: 0.5%

### What frequency comes out of its top bit?

::: misses
- [any] $\frac{f}{2^N}$.
:::
:::

::: part
answer: 2^N - 1
tolerance: 0

### What's its largest value?

::: misses
- [any] $2^N - 1$.
:::
:::
:::

::: check
kind: boolean
vars: S1, S0, a, b, c, d
answer: ~S1 & ~S0 & a | ~S1 & S0 & b | S1 & ~S0 & c | S1 & S0 & d
ops: and, or, not

## Write the logic of a 4:1 multiplexer: `case ({S1, S0})` selecting a, b, c, d for 00, 01, 10, 11.

::: right
Right: one AND term per select value, ORed together.
:::

::: misses
- [any] Not yet. Each input passes only when the select bits equal its code.
:::
:::

::: check
kind: parts
given: N = 4 | 8 | 16; k = 1 | 2

## `always @(posedge clk) r <= {r[N-2:0], d};` with N = {N}, followed by {k} more registers in a chain.

::: part
answer: N + k
tolerance: 0

### How many flip-flops in all?

::: misses
- [any] N plus k.
:::
:::

::: part
answer: N + k
tolerance: 0

### After how many clocks does a bit of d reach the last register?

::: misses
- [any] One stage per clock.
:::
:::
:::
:::
