Lab 3: Music Player
Part C: Music Player
Lab 3 will give you experience designing, implementing, testing, and prototyping combinational and sequential logic using the Verilog hardware description language. The lab will continue to leverage concepts from Topic 2: Combinational Logic, Topic 3: Boolean Algebra, and Topic 4: Combinational Building Blocks but will also leverage concepts from Topic 6: Sequential Logic, Topic 7: Finite-State Machines, and Topic 8: Sequential Building Blocks. More specifically, the lab will give students experience with: latches, flip-flops, and registers; Moore and Mealy FSMs; and counters. The lab will continue to reinforce three key abstraction principles: modularity, hierarchy, and regularity.
You will be implementing a music player that takes as input a song selection (via the switches) and a start song signal (via a push button). The music player will then play the chosen song by generating a square wave at appropriate note frequencies suitable for use with a piezoelectric buzzer. An idle signal is displayed using an LED so that the user knows when the player is ready to play a new song. The music player will make use of the adders and muxes from Lab 2. The song selection and the current note are both displayed using seven-segment displays from Lab 1. This lab also serves as a transition from lower-level gate-level (GL) modeling to higher-level register-transfer-level (RTL) modeling. Some of parts of your design will use explicit GL modeling, while other parts of your design will use RTL modeling. Students will have a chance to appreciate how RTL modeling can improve productivity but with less control over the final hardware implementation. The lab includes six parts:
-
Part A: Counters
- Due 10/15 @ 11:59pm via GitHub
- Students should work on Part A before, during, and after your assigned lab section during the week of 10/5
- Pre-lab survey on Canvas is (roughly) due by end of lab section during the week of 10/5
-
Part B: Breadboard, FPGA, and Simulation
- Due week of 10/5 during assigned lab section
- Even though completed with a partner, every student must turn in their own paper check-off sheet in their lab section!
-
Part C: Music Player
- Due 10/22 @ 11:59pm via GitHub
- It is ok if students need a little more time to complete the music player and clock divider as long as they are pushed to GitHub by 10/25 @ 11:59pm
- Plan to work on Part C after fall break and during the week of 10/19
-
Part D: FPGA Prototype v1
- Due week of 10/19 during assigned lab section
- This part will focus on prototyping the code developed in Part A
- Even though completed with a partner, every student must turn in their own paper check-off sheet in their lab section!
-
Part E: FPGA Prototype v2
- Due week of 10/26 during assigned lab section
- This part will focus on prototyping the code developed in Part C
- Even though completed with a partner, every student must turn in their own paper check-off sheet in their lab section!
-
Part F: Datasheets and Report
- Due week of 10/26, three days after lab section @ 11:59pm via Canvas
- Post-lab survey on Canvas is due at the same time as the report
All parts of Lab 3 must be done with a partner. You can confirm your partner on Canvas (Click on People, then Groups, then search for your name to find your lab group).
Both students must contribute to all parts!
It is not acceptable for one student to exclusively work on the code while the other student exclusively works on the report. It is not acceptable for one student to exclusively work on hardware design while the other student exclusively works on testing. Both students must contribute to all parts. Student understanding of Verilog design and testing will be assessed on the prelim exams, final exam, and Verilog coding exam. The instructors will also survey the Git commit log on GitHub to confirm that both students are contributing equally. If you are using pair programming, then both students must take turns using their own account so both students have representative Git commits. Students should create commits after finishing each step of the lab, so their contribution is clear in the Git commit log. A student's whose contribution is limited as represented by the Git commit log will receive a significant deduction to their lab score.
This handout assumes that you have read and understand the course
tutorials and that you have attended the discussion sections. This
handout assumed you have successfully completed Part A. To get started,
use VS Code to log into a specific ecelinux server, source the setup
script, and use git pull to ensure you have any recent updates before
working on your lab assignment.
where XX should be replaced with your group number. The following table
shows all of the hardware modules you will be developing in Lab 3.

1. Music Background
Our multi-note and music player FPGA prototypes will use a piezoelectric ("piezo") buzzer. A piezo buzzer is a two-terminal device where one terminal is connected to ground and the other side is connected to a digital signal.

Driving the buzzer with a square wave produces a tone whose pitch is determined by the square-wave frequency. Essentially the piezo buzzer is a tiny speaker that can play notes. Higher frequencies produce higher pitches. Frequency, measured in hertz (Hz), is the number of complete waveform repetitions per second. The period is the time for one complete repetition, so period = 1/frequency. For example, a 200 Hz square wave has a period of 1/200 seconds, or 5 ms.
Here is a table showing all of the notes and their corresponding frequencies and periods on a 25-key keyboard:

The seven notes we will use in our music player are highlighted in red on the keyboard and shown below for reference.
freq period
note (Hz) (ms)
G3 196.00 5.102
A3 220.00 4.545
B3 246.94 4.050
C4 261.63 3.822
D4 293.67 3.405
E4 329.63 3.034
F4 349.23 2.863
We will use the counter developed in Part A to create notes (i.e., square waves) with the appropriate period. We will be using a 50MHz clock on the FPGA prototype. A 50MHz clock has a 20ns period, but we want to create square waves with periods on the order of milliseconds. So we will use a clock divider to slow down the 50MHz clock by a factor of 1024 resulting in an effective clock frequency of 48.828 kHz and an effective period of 20,480ns (i.e., 0.02048ms).
If we want a counter that is operating at a 0.02048ms clock period to wait for roughly 1ms it would need to count (1/0.02048) = 48.828 clock cycles. So if we want a counter to wait for roughly 1 second it would need to count 1000*48.828 = 48,828 clock cycles. We can now calculate how many the square wave period in cycles assuming a 50MHz clock divided by 1024. This spreadsheet shows all possible periods and identifies which periods are the closest to the target periods shown above.
Here are the final periods in cycles we will be using for each note in this lab. These result in an error of less than <0.35%.
freq period period
note (Hz) (ms) (cycs)
G3 195.31 5.12 250
A3 219.95 4.55 222
B3 246.61 4.06 198
C4 262.52 3.81 186
D4 294.15 3.40 166
E4 329.92 3.03 148
F4 348.77 2.87 140
2. Interface and Implementation Specification
You will use the modules you implemented in Part A to build a note player that continuously generates a square wave at a specified frequency. You will then build a multi-note player that includes seven note players each configured to play a different note (G3, A3, B3, C4, D4, E4, F4) for a specified duration. Finally, you will build a music player that reads a sequence of notes from memory and plays them using the multi-note player. You will also implement a clock divider to slow down the clock.
For each module, this section describes the required interface (its ports and functional behavior), followed by the required implementation (the hardware used inside the module).
2.1. Note Player
The note player generates a square wave at a given frequency forever. It includes a control unit and a counter.

The clock and reset signals are not shown in the above block diagram. The period is provided as an input port, and the period must be an even value from 0 through 254. The control unit should be implemented using the following four-state Moore FSM.

The FSM must use the following state encoding:
The outputs are specified in each state: cl stands for count_load and n
stands for note. In the LOAD_HIGH state, the FSM should load the
counter to count from zero through period/2, set the note output to be
high, and move into WAIT_HIGH. In the WAIT_HIGH state, the FSM should
continue to set the note output to be high. Once the counter is done, the
FSM should move into LOAD_LOW. In the LOAD_LOW state, the FSM should load
the counter to count from zero through period/2, set the note output to
be low, and move into WAIT_LOW. In the WAIT_LOW state, the FSM should
continue to set the note output to be low. Once the counter is again
done, the FSM should move back to LOAD_HIGH to repeat the sequence.
LOAD_HIGH is the reset state.
The period input may be set during reset. After reset, it must remain
constant except while state is LOAD_HIGH. It may change in LOAD_HIGH
and must then remain constant until the next LOAD_HIGH. Changing
period in any other state is undefined.
Here is a trace of the expected output when the period is two.
cyc rst period state note
------------------------------------------
0: 0 00000010 > 00 (LOAD_HIGH) 1
1: 0 00000010 > 01 (WAIT_HIGH) 1
2: 0 00000010 > 01 (WAIT_HIGH) 1
3: 0 00000010 > 10 (LOAD_LOW ) 0
4: 0 00000010 > 11 (WAIT_LOW ) 0
5: 0 00000010 > 11 (WAIT_LOW ) 0
6: 0 00000010 > 00 (LOAD_HIGH) 1 ---
7: 0 00000010 > 01 (WAIT_HIGH) 1 ^
8: 0 00000010 > 01 (WAIT_HIGH) 1 | 6 cycles
9: 0 00000010 > 10 (LOAD_LOW ) 0 |
10: 0 00000010 > 11 (WAIT_LOW ) 0 V
11: 0 00000010 > 11 (WAIT_LOW ) 0 ---
12: 0 00000010 > 00 (LOAD_HIGH) 1
13: 0 00000010 > 01 (WAIT_HIGH) 1
14: 0 00000010 > 01 (WAIT_HIGH) 1
15: 0 00000010 > 10 (LOAD_LOW ) 0
16: 0 00000010 > 11 (WAIT_LOW ) 0
17: 0 00000010 > 11 (WAIT_LOW ) 0
Pay careful attention to the exact cycle of each state transition and the
exact cycles the note is high and low. Each half of the period includes
one LOAD cycle and period/2 + 1 WAIT cycles, so each half period lasts
period/2 + 2 clock cycles. The actual period therefore lasts period +
4 clock cycles. For example, when the period input is two, the actual
waveform period is six clock cycles, as shown in the trace. This is the
expected behavior. Your note player must produce the exact same
cycle-level behavior.
A period input of zero produces a waveform that is high for two cycles and low for two cycles.
To design the FSM, you should follow the same process described in lecture:
- Create a FSM next state and output table
- Determine FSM next state and output equations (use Kmaps!)
- Implement these equations using gate-level modeling
Fill in the corresponding sections of the following worksheet to work through the process:
Save a copy of this worksheet, since you will need to include it with your lab report.
The GL version of the note player control unit should include three
sections: sequential logic, next-state combinational logic, and output
combinational logic. For the sequential logic, instantiate two DFFR_GL
modules to store the two-bit FSM state. The next state combinational
logic and output combinational logic should be implemented using explicit
gate-level modeling.
The GL version of the note player just composes the GL note player
control unit with the GL counter. Keep in mind the counter is meant to
count half the number of cycles in the period. You can divide the period
in two by simply ignoring the least significant bit (i.e., period[7:1]
is the period divided by two). You will need to think carefully about how
to set all of the input ports of the counter to ensure it counts
correctly.
The RTL version of the note player control unit should include three
sections: sequential logic, next-state combinational logic, and output
combinational logic. For the sequential logic, instantiate two DFFR_RTL
modules to store the two-bit FSM state. The next state combinational
logic should be implemented by mapping your FSM next state table into a
single always_comb block with a single casez or case statement. The
output combinational logic should be implemented by mapping your FSM
output table into a separate always_comb block with a single casez or
case statement. The control unit is an example of a flat RTL module,
since it does not instantiate any hardware modules other than the
flip-flops.
The RTL version of the note player just composes the RTL note player
control unit with the RTL counter. Keep in mind the counter is meant to
count half the number of cycles in the period. You can divide the period
in two by simply ignoring the least significant bit (i.e., period[7:1]
is the period divided by two). You will need to think carefully about how
to set all of the input ports of the counter to ensure it counts
correctly.
2.2. Multi-Note Player
The multi-note player can produce one of seven notes or a rest for a specified duration by composing a counter, mux, register, and seven note players.

We will only be implementing an RTL version of the multi-note player, and
so we will use the RTL version of each of these hardware modules. This is
an example of structural RTL, since it only instantiates other RTL
modules (and maybe a few explicit gates). It does not directly include
any logic implemented using always_comb blocks or assign statements.
The clock and reset signals are not shown in the above block diagram.
Note periods must be even and between 0 and 254. Note periods must be set
during reset and held constant when not in reset. The note duration must
be between 1 and 65,535 clock cycles (i.e., a duration of zero is
undefined). You will not be using the state output of the note players
so you can mark those as unused. Think carefully about how you should set
the inputs to the counter not shown above to appropriate constant values.
Instead of implementing a Register_3b_RTL just use your
Register_16b_RTL to store the note number. You should zero-extend the
input from 3-bits to 16-bits and then slice off the bottom three bits of
the output. You need to mark the top 13-bits of the output as unused.
The multi-note player uses a latency insensitive valid/ready interface. A val/rdy interface uses the following micro-protocol:
Valid/Ready Micro-Protocol
Assume we have a producer that wishes to send a message to a consumer using the valid/ready micro-protocol. At the beginning of the cycle, the producer determines if it has a new message to send to the consumer. If so, it sets the message bits appropriately and then sets the valid signal high. Also at the beginning of the cycle, the consumer determines if it is able to accept a new message from the producer. If so, it sets the ready signal high. At the end of the cycle, the producer and consumer can independently AND the valid and ready signals together; if both signals are true then the message is considered to have been sent from the producer to the consumer and both sides can update their internal state appropriately. Otherwise, we will try again on the next cycle. To avoid long combinational paths and/or combinational loops, we should avoid making the valid signal depend on the ready signal or the ready signal depend on the valid signal. If you absolutely must, you can make the ready signal depend on the valid signal but it is considered very bad practice to make the valid signal depend on the ready signal. As long as you adhere to this valid/ready micro-protocol, composing modules via the stream interfaces should not cause significant timing issues.
So to play a note using the multi-note player, set play_note_num to the
number of the note you want to play (i.e., which of the seven note period
inputs to use), set play_note_dur to the desired duration of the note,
and set play_note_val to one. If on that same cycle play_note_rdy is
one, then the multi-note player accepts the request at the rising clock
edge, capturing play_note_num and play_note_dur together. It starts
playing the corresponding note on the next cycle. Later changes to the
message inputs must not affect the accepted note or its duration. If on
that same cycle play_note_rdy is zero, then the multi-note player is
busy and so we will need to try again on the next cycle. The desired note
is produced on the note output and when the duration is done the
play_note_rdy output is set high. The note_sel output is provided for
debugging. When the note number is zero, then the multi-note player
should produce a rest (i.e., the note should stay low).
Note how we need to use an AND gate to ensure we only load a new value
into the counter when there is a new note to be played (i.e.,
play_note_val is high) and the counter is done. Without the AND gate,
it might be possible for a new note to start playing before the old note
is finished playing.
2.3. Music Player
The music player plays a song stored in memory. We will compose the music player with a memory as shown below.

The note duration and periods are provided as input ports and are assumed
to never change after reset. The music player uses a latency insensitive
valid/ready interface. To play a song using the music player, set
play_song_num to the number of the song you want to play and set
play_song_val to one. If on that same cycle play_song_rdy is one,
then the music player will start playing the corresponding song on the
next cycle. If on that same cycle play_song_rdy is zero, then the music
player is busy and so we will need to try again on the next cycle. The
song is produced as a series of notes on the note output and when the
song is done the play_song_rdy output is set high. The note_sel
output is provided for debugging.
Songs are stored in memory as a sequence of notes. The memory interface
has a memory request valid port (mem_val) and a memory address port
(mem_addr). The memory address is a byte address. To read a value
from memory, we need to set the memory address to the desired byte
address and set the memory valid signal high. The data will be returned
combinationally (i.e., on that same cycle) on the mem_rdata port.
Our music player can play songs using seven distinct notes and a rest. Each note is stored in one 4-byte word using the following encoding:
NOTE Encoding
------------------------
REST 32'h00000000
NOTE_G 32'h00000001
NOTE_A 32'h00000002
NOTE_B 32'h00000003
NOTE_C 32'h00000004
NOTE_D 32'h00000005
NOTE_E 32'h00000006
NOTE_F 32'h00000007
SONG_END 32'h0000ffff
Each song can have a maximum of 127 notes. The value 32'h0000_FFFF is
used to indicate the end of a song. The music player can play up to four
songs; so the music player will use the following amount of memory:
The song memory map (i.e., how the songs are arranged in memory) looks like this:
note
address song index comment
-----------------------------------------------------
0x0000_0000 0 0 start of song 0
0x0000_0004 0 1
0x0000_0008 0 2
...
0x0000_01fc 0 127 last possible word of song 0
0x0000_0200 1 0 start of song 1
0x0000_0204 1 1
0x0000_0208 1 2
...
0x0000_03fc 1 127 last possible word of song 1
0x0000_0400 2 0 start of song 2
...
Note that even though the memory's total capacity is 2KB we still use a 32-bit address. Accessing any address above 0x7ff is undefined.
We will divide the implementation of the music player into two parts: the control unit will include a finite-state machine implemented in RTL and the datapath will compose the RTL versions of a counter, equality comparator, register, and multi-note player as shown below.

The clock and reset signals are not shown in the above block diagram. The music player datapath should be implemented using structural RTL (i.e., instantiating an RTL counter, equality comparator, register, and multi-note player).
-
Address Counter: The address counter is responsible for generating memory addresses starting from the first note of the song. Remember each note is stored in one 4-byte word and all addresses are byte addresses, so the address counter will need to increment the address by four when enabled. The control unit is in charge of starting and enabling the counter. Think carefully about how you should set the inputs to the counter not shown above to appropriate constant values.
-
Equality Comparator: The equality comparator is responsible for detecting the end of the song. It compares the bottom 16 bits of the data returned from memory with the special end song value (i.e.,
16'hFFFF) -
Note Number Register: A 16-bit register to hold the bottom 16 bits of the data returned from memory to be used as the note for the multi-note player.
-
Multi-Note Player: The multi-note player is exactly what we developed earlier in the lab. The control unit is in charge of when to play a note through the
play_note_valandplay_note_rdysignals while what note to play is determined by the value returned from memory.
The datapath will also need to turn the song number into a start value for the counter. From the above memory map, we can see that the start value for a song is just:
The counter’s 16-bit output is zero extended to make a 32-bit memory address.
The music player control unit should be implemented using the following four-state Moore FSM.

The FSM must use the following state encoding:
Only the next state logic is shown in the FSM diagram. You will need to determine the appropriate output logic from the following specification.
-
IDLE: The FSM should make sure
play_song_rdyis one to indicate the music player is ready to play a new song. The FSM should initialize the memory address counter to the desired song usingaddr_counter_load. -
LOAD_NOTE: The FSM should read the next note from memory by setting
mem_valhigh. Since the memory is combinational, the LOAD_NOTE state should also handle the memory response by enabling the note number register. The FSM should also enable the counter to increment the address to use for loading the next note. -
PLAY_NOTE: The FSM should use the multi-note player to play the note by setting
play_note_valto one. -
WAIT_NOTE: The FSM should not do anything since it is waiting for the multi-note player to be ready to play another note.
To design the FSM, you should follow the same process described in lecture. You just need to create a FSM next state and output table. Since we are only using an RTL implementation there is no need to determine the FSM next state and output equations nor implement these equations using gate-level modeling. Fill in the corresponding sections of the following worksheet to work through the process:
Save a copy of this worksheet, since you will need to include it with your lab report.
The RTL version of the music player control unit should include three
sections: sequential logic, next-state combinational logic, and output
combinational logic. For the sequential logic, instantiate two DFFR_RTL
modules or a Register_16b_RTL and only use the bottom two bits. The
next state combinational logic should be implemented by mapping your FSM
next state table into a single always_comb block with a single casez
or case statement. The output combinational logic should be implemented
by mapping your FSM output table into a separate always_comb block with
a single casez or case statement. The control unit is an example of a
flat RTL module, since it does not instantiate any hardware modules
other than the flip-flops.
2.4. Clock Divider
A clock divider takes a higher-frequency clock on clk_in and produces a
lower-frequency clock on clk_out. The interface for the clock divider
we will be using is shown below.

The four-bit divide_sel input selects the division factor:
For example, setting divide_sel to zero divides the input frequency by
two, while setting it to nine divides the input frequency by 1024. All
selector values from zero through fifteen are supported, corresponding
to division factors from two through 65536.
divide_sel |
Clock Division | Division Factor |
|---|---|---|
| 0 | \(2^{0+1}\) | 2 |
| 1 | \(2^{1+1}\) | 4 |
| 2 | \(2^{2+1}\) | 8 |
| 3 | \(2^{3+1}\) | 16 |
| 4 | \(2^{4+1}\) | 32 |
| 5 | \(2^{5+1}\) | 64 |
| 6 | \(2^{6+1}\) | 128 |
| 7 | \(2^{7+1}\) | 256 |
| 8 | \(2^{8+1}\) | 512 |
| 9 | \(2^{9+1}\) | 1,024 |
| 10 | \(2^{10+1}\) | 2,048 |
| 11 | \(2^{11+1}\) | 4,096 |
| 12 | \(2^{12+1}\) | 8,192 |
| 13 | \(2^{13+1}\) | 16,384 |
| 14 | \(2^{14+1}\) | 32,768 |
| 15 | \(2^{15+1}\) | 65,536 |
The output clock spends equal amounts of time high and low. Set
divide_sel before initialization and keep it constant during operation.
Unlike the other modules, the clock divider does not have a system reset input. Its output supplies the clock to modules that use a synchronous reset. These modules need rising clock edges to reset, so the clock divider must continue running while reset is one.
In the real hardware, the clock divider initially has a binary value in
hardware, but is X in simulation. The init input is provided to
initialize the clock divider during simulation. When init is one at a
rising edge of clk_in, the divider clears its internal counter to zero.
After initialization, set init to zero so the counter can run. The
output before initialization is undefined. In the the real hardware we
will set init to zero.
Implement the clock divider using a free-running 16-bit counter. The
counter increments by one on every rising edge of clk_in and wraps from
65535 to zero. Each successive counter bit toggles at half the frequency
of the preceding bit, so we can simply index into the counter output
using divide_sel to choose which bit produces the desired output clock.
Divide the implementation into two sections:
-
Sequential Logic: Instantiate a
Register_16b_RTLmodule to hold the counter value. Connect its clock toclk_in, its reset toinit, and its enable to one. -
Combinational Logic: Use one
assignstatement to compute the next counter value. Use a secondassignstatement to select the counter bit forclk_out. Do not usealways_combblocks or instantiate additional RTL modules for this logic.
3. Testing Strategy
You will need to write your own tests for the note player control unit and the note player. We provide you tests for the multi-note player, music player, and clock divider.
3.1. Interactive Simulators
We have provided you two new interactive simulators which will emulate the FPGA prototype you will be demoing in the lab. After finishing implementing and thoroughly testing your multi-note player, you can build and run the simulator like this:
% cd ${HOME}/ece2300/groupXX/build
% make multi-note-player-sim
% ./multi-note-player-sim +switches=000
The switches are connected to the note number. Here is a table showing the mapping from note number to actual notes.
note period freq
num (ms) (Hz) note
1 5.12 195.31 G3
2 4.55 219.95 A3
3 4.06 246.61 B3
4 3.81 262.52 C4
5 3.40 294.15 D4
6 3.03 329.92 E4
7 2.87 348.77 F4
The simulator will generate a VCD file that you can then open using Surfer to look at the note output waveform. You can measure the period between rising edges of the note output to verify that the note player is indeed generating the note at the proper frequency. For example, this is what the output looks like when setting the switches to 001. The period is close to 5.12ms which is 195Hz or the note G3. Try other notes!

After finishing implementing and thoroughly testing your music player, you can build and run the simulator like this:
The switches are connected to the song number. We currently have two songs you can play using your music player. Here is what the output looks like for the first few notes of song 0.

You can see that the song starts with the notes 3, 2, 1, 2, 3, 3, 3, rest. The simulator uses a very short note duration. We will use a much longer and more realistic note duration in the actual FPGA prototype. You will figure out which song this is in Part E of the lab.
4. Lab Code Submission
To submit your code you simply push your code to GitHub. You can push
your code as many times as you like before the deadline. Students are
responsible for going to the GitHub website for their repository, browsing
the source code, and confirming the code on GitHub is the code they want
to submit. Be sure to verify your code is passing your tests
both on ecelinux and on GitHub Actions. Your design code will be
assessed both in terms of code quality, verification quality, and
functionality.
4.1. Part A Revisions
If your design is failing some tests from Part A, then you should fix whatever is wrong. If your design is passing your tests but failing our tests you should browse the staff tests to see how to improve your own testing. Fixing whatever is wrong now can improve your code functionality score and also ensures that you can use your work from this lab in future labs.
To revise your submission, simply push any updates to your tests and/or your hardware designs to GitHub just like normal. You do not need to switch branches; just push your changes to the main branch like normal. Make sure your updates are passing all of your (potentially updated) tests on the main branch on GitHub Actions.
All Part A revisions need to be finalized by the due date for Part C. No revisions are allowed for Part C code.
4.2. Code and Verification Quality
Code and verification quality will be assessed in the same way as for Part A.
4.3. Functionality
Your functionality score will be determined by running your code against a series of tests developed by the instructors to test its correctness. Note that we will be using the automated build system to test your final code submission as shown below.