← All problems

Music Box

Medium · Sequential · SystemVerilog · VHDL

Imagine a music box game for two players: player 1 teaches the box a tune, say C, D, E, C. The box plays the tune, and then player 2 attempts to play it back. If they get it wrong, the box plays the tune again until they get it right. Once player 2 gets it right, the box becomes ready to learn a new tune.

Design the controller for this music box: it learns a tune from player 1, plays it back, and checks player 2's attempt to repeat it. For this exercise, a tune is N notes, and a note is an 8-bit code; two notes match only when their codes are equal.

Learn

The box learns a tune through a valid/ready handshake with player 1's controller, which provides one note at a time on p1_data.

Play

In the cycle after the box receives the N-th note, it starts playing the tune, one note per cycle on play_data, asserting play_valid as it streams notes. It must not stall its playback.

Listen

In the cycle after the last note is played, the box starts listening for player 2's attempt using a valid/ready handshake with player 2's controller. It takes the player's next note in each cycle in which the player offers one.

If one of player 2's notes does not match the next note of the tune, or player 2 goes TIMEOUT cycles in a row without providing a note, the box stops listening and asserts p2_wrong in the following cycle to signal the failure. In the cycle after that, it plays the tune again, then begins listening for a new attempt, starting from the first note.

When the player's N-th note matches, the box stops listening and asserts p2_correct in the following cycle to signal the success. In the cycle after that, it tells player 1 that it is ready to learn a new tune, and the process repeats.

Reset

resetn is a synchronous, active-low reset that abandons whatever the box is doing, its tune included, and starts it learning a new one.

Open this challenge in the editor →