Clocked Counter with Reset and Enable
Problem statement
Model an unsigned width-bit up counter, starting at initial. Each row of cycles is [reset, enable] and describes one rising clock edge.
- If reset is 1, set the count to 0, even when enable is also 1.
- Otherwise, if enable is 1, add 1 and wrap from
2^width - 1to 0. - Otherwise, hold the current count.
Return the count after every edge. This exercise grades the counter state transitions; it does not compile RTL or check circuit timing.
Function
counterValues(width: int, initial: int, cycles: int[][]) → int[]Examples
Example 1
width = 2initial = 2cycles = [[0,1],[0,1],[0,1]]return = [3,0,1]A 2-bit count ranges from 0 to 3. The three enabled edges produce 3, wrap to 0, then produce 1.
Example 2
width = 3initial = 7cycles = [[1,1],[0,0],[0,1]]return = [0,0,1]Reset wins over enable, the next edge holds 0, and the last edge increments to 1.
Example 3
width = 1initial = 1cycles = []return = []No clock edges means no output counts.
Constraints
1 <= width <= 30.0 <= initial < 2^width.0 <= cycles.length <= 10000.- Each cycle has exactly two integers, both 0 or 1.