Skip to content

Commit

Permalink
Move all logic to rising clock edge of 'clk'
Browse files Browse the repository at this point in the history
  • Loading branch information
meriac committed Oct 29, 2023
1 parent 6be66ce commit 55f60a5
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 121 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ over a Piezo Speaker connected across io_out[1:0]. The speaker is driven
in differential PWM mode to increase its output power.

### Listen To The Generated Sound ###
Listen to the [Verilog Simulation PWM output for 62505Hz clock frequency](https://github.com/meriac/tt05-play-tune/releases/download/v0.1/tune-modulation-verilog-simulated.wav) here.
Listen to the [Verilog Simulation PWM output for 100kHz clock frequency](https://github.com/meriac/tt05-play-tune/releases/download/v0.1/tune-modulation-verilog-simulated.wav) here.

### Chip Design of the Super Mario Tune player ###
![Chip Design of the Super Mario Tune player](img/chip-design.png)
![Chip Design of the Super Mario Tune player](img/chip-design.jpg)
(see also the [interactive version of this design](https://meriac.github.io/tt05-play-tune/))

## Verilog Design Files ##
Expand All @@ -29,7 +29,7 @@ Using [GTKWave](https://gtkwave.sourceforge.net/) for visualization of Simulatio
![Zooming into the Waveform](img/waveform-detail.png)

## How To Test ##
Provide 6250Hz clock on io_in[0], briefly hit reset io_in[1] (Low, High, Low)
Provide 100kHz clock on io_in[0], briefly hit reset io_in[1] (Low, High, Low)
and io_out[1:0] will play a differential sound wave over piezo speaker
(Super Mario Tune).

Expand Down
4 changes: 2 additions & 2 deletions generator/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
import numpy as np
import scipy.io.wavfile as wav

transpose = -4
transpose = 0
timer_resolution_ms = 50
sampling_rate=6250
sampling_rate=100_000
rttl='Super Mario:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6,4p'

data=np.zeros(0)
Expand Down
Binary file added img/chip-design.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed img/chip-design.png
Binary file not shown.
Binary file modified img/waveform-detail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/waveform-overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ documentation:
# Instructions on how someone could test your project, include things like what buttons do what and how to set the clock if needed
how_to_test: |
Provide 6.25kHz clock on io_in[0], briefly hit reset io_in[1] (L->H->L) and io_out[1:0] will play a differential sound wave over piezo speaker (Super Mario)
Provide 100kHz clock on io_in[0], briefly hit reset io_in[1] (L->H->L) and io_out[1:0] will play a differential sound wave over piezo speaker (Super Mario)
# A description of what the inputs do (e.g. red button, SPI CLK, SPI MOSI, etc).
inputs:
Expand Down Expand Up @@ -68,5 +68,5 @@ documentation:
external_hw: "Piezo speaker connected across io_out[1:0]" # Describe any external hardware needed
discord: "foolsdelight" # Your discord handle, used for communication and automatically assigning tapeout role after a submission
doc_link: "https://github.com/meriac/tt05-play-tune#readme" # URL to longer form documentation, eg the README.md in your repository
clock_hz: 6250 # Clock frequency in Hz (if required)
picture: "img/chip-design.png" # relative path to a picture in your repository (must be 512kb or less)
clock_hz: 100000 # Clock frequency in Hz (if required)
picture: "img/chip-design.jpg" # relative path to a picture in your repository (must be 512kb or less)
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*.wav
/*.tim
/results.xml
/sim_build
Expand Down
49 changes: 21 additions & 28 deletions src/player.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module tt_um_meriac_play_tune #( parameter MAX_COUNT = 100 ) (
wire [11:0] db_entry;

reg [6:0] note_address;
reg [11:0] ticks;
reg [15:0] ticks;
reg [7:0] freq, counter;
reg speaker;

Expand All @@ -64,49 +64,42 @@ module tt_um_meriac_play_tune #( parameter MAX_COUNT = 100 ) (

always @(posedge clk) begin

// if reset, set note_address to 0
if (!rst_n) begin
note_address <= 0;

ticks <= 0;
freq <= 0;
counter <= 0;
speaker <= 0;
note_address <= 0;

end else begin

if (!ticks) begin
if (note_address<MAX_COUNT) begin
note_address <= note_address + 1'b1;
if (ticks>0) begin
ticks <= ticks - 1'b1;

// tone frequency divider
if (counter>0) begin
counter <= counter - 1'b1;
speaker <= counter >= (freq/2);
end else begin
note_address <= 0;
counter <= freq;
speaker <= 1'b0;
end
end

// tone frequency divider
if (counter>0) begin
counter <= counter - 1'b1;
speaker <= counter >= (freq/2);
end else begin
counter <= freq;
speaker <= 1'b0;
end

end
end

always @(negedge clk) begin

if (rst_n) begin

if (ticks>0) begin
ticks <= ticks - 1'b1;
end else begin
// update per-note delay
ticks[11:8] <= db_entry[3:0];
ticks[7:0] <= 0;
ticks[15:12] <= db_entry[3:0];
ticks[11:0] <= 0;

// reset tone generator
counter <= db_entry[11:4];
freq <= db_entry[11:4];

if (note_address<MAX_COUNT) begin
note_address <= note_address + 1'b1;
end else begin
note_address <= 0;
end
end

end
Expand Down
21 changes: 13 additions & 8 deletions src/tb.gtkw
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
[*]
[*] GTKWave Analyzer v3.3.115 (w)1999-2023 BSI
[*] Mon Oct 23 02:01:40 2023
[*] Sun Oct 29 08:31:35 2023
[*]
[dumpfile] "tb.vcd"
[dumpfile_mtime] "Mon Oct 23 01:57:18 2023"
[dumpfile_size] 9065301
[dumpfile_mtime] "Sun Oct 29 07:52:20 2023"
[dumpfile_size] 154838784
[savefile] "tb.gtkw"
[timestart] 0
[size] 3840 2113
[pos] 3865 0
*-37.899998 64900000000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[size] 2268 384
[pos] 827 793
*-41.500000 14107500000000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[treeopen] tb.
[treeopen] tb.tt_um_meriac_play_tune.
[sst_width] 343
[signals_width] 264
[sst_expanded] 1
[sst_vpaned_height] 654
[sst_vpaned_height] 79
@28
tb.clk
tb.tt_um_meriac_play_tune.rst_n
@8024
tb.piezo_speaker[1:0]
@29
@28
tb.tt_um_meriac_play_tune.speaker
@8024
tb.tt_um_meriac_play_tune.note_address[6:0]
tb.tt_um_meriac_play_tune.counter[7:0]
@8023
tb.tt_um_meriac_play_tune.freq[7:0]
[pattern_trace] 1
[pattern_trace] 0
4 changes: 2 additions & 2 deletions src/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
@cocotb.test()
async def test_tune(dut):
dut._log.info("start")
clock = Clock(dut.clk, 160, units="us")
clock = Clock(dut.clk, 10, units="us")
cocotb.fork(clock.start())

dut._log.info("reset")
dut.rst_n.value = 0
await ClockCycles(dut.clk, 10)
dut.rst_n.value = 1

await ClockCycles(dut.clk, 156250)
await ClockCycles(dut.clk, 2500000)
Loading

0 comments on commit 55f60a5

Please sign in to comment.