diff --git a/.gitignore b/.gitignore index e9cc4a9..5728930 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ games/raid/out/ games/warden/out/ games/swarm/out/ games/cavern/out/ +sim/work-sound/ diff --git a/README.md b/README.md index 47d3df9..174bb7c 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,13 @@ so scrolling adds no arithmetic to the pixel pipeline. held-down bits, and 89h any key by scan code (`rtl/ps2_keyboard.v`, `ps2_keystate`), so games don't depend on typematic repeat. +**Sound:** a Digilent PmodAMP2 on the top row of Pmod JA plays a small +sound chip in the FPGA (`rtl/sound.v`): three square-wave voices and a noise +source, a mixer, 16 log-scale volumes and an attack/decay envelope per voice, +on ports 90h-9Dh, mixed into one bit by a sigma-delta modulator for the +PmodAMP2's filter and amplifier. `CLS` silences it. `scripts/sim-sound.sh` +tests it; `basic/examples/sound.bas` plays it from BASIC. + **The Iron Keep** (`games/keep/`) is a top-down escape game in C for the Z-80 using all of this: twelve rooms, guards that patrol, see along rows and columns and shoot, keys and locked doors, gold, ammunition and a crown to @@ -596,6 +603,7 @@ which Vivado needs on newer distros. | `rtl/text_display.v` | VGA timing, colour palette, character RAM, font lookup | | `rtl/ps2_keyboard.v` | PS/2 receiver and scan code set 2 to ASCII / VT220 keys | | `rtl/uart.v` | UART receiver and transmitter | +| `rtl/sound.v` | Sound chip: three voices, noise, envelopes, sigma-delta output for a PmodAMP2 | | `rtl/fifo.v`, `rtl/sevenseg.v` | Helpers | | `font/font.mem` | 256x16 font ROM, made by `tools/psf2mem.py` from the Linux console fonts | | `terminfo/basys3.ti` | terminfo entries: `basys3` (VT220, 16 colours, 80x32) and `basys3-w` (132x64) | diff --git a/basic/examples/sound.bas b/basic/examples/sound.bas new file mode 100644 index 0000000..f0ee41b --- /dev/null +++ b/basic/examples/sound.bas @@ -0,0 +1,26 @@ +10 REM SOUND DEMO - NEEDS A PMODAMP2 ON THE TOP ROW OF PMOD JA +20 REM PITCH = 125000 / FREQUENCY. PORTS: 144-149 PITCHES, 150 NOISE, +30 REM 151 MIXER, 152-154 VOLUMES, 155-157 ENVELOPES (ATTACK*16+DECAY) +40 PRINT "A SCALE" +50 OUT 151, 1: OUT 155, 3 +60 FOR I = 1 TO 8 +70 READ P: GOSUB 500 +80 OUT 152, 15 +90 IF INP(152) > 0 THEN 90 +100 NEXT I +110 PRINT "A LASER" +120 OUT 151, 4: OUT 149, 0: OUT 157, 0: OUT 154, 13 +130 FOR P = 30 TO 250 STEP 6: OUT 148, P: NEXT P +140 OUT 154, 0 +150 PRINT "AN EXPLOSION" +160 OUT 151, 16: OUT 150, 24: OUT 156, 8: OUT 153, 15 +170 IF INP(153) > 0 THEN 170 +180 PRINT "A CHORD" +190 OUT 151, 7 +200 OUT 144, 221: OUT 145, 1: OUT 146, 123: OUT 147, 1: OUT 148, 63: OUT 149, 1 +210 OUT 155, 69: OUT 156, 69: OUT 157, 69 +220 OUT 152, 12: OUT 153, 12: OUT 154, 12 +230 IF INP(152) > 0 THEN 230 +240 END +500 OUT 144, P - INT(P / 256) * 256: OUT 145, INT(P / 256): RETURN +900 DATA 477, 425, 379, 358, 319, 284, 253, 239 diff --git a/constraints/basys3.xdc b/constraints/basys3.xdc index 05a649d..a17dd63 100644 --- a/constraints/basys3.xdc +++ b/constraints/basys3.xdc @@ -102,6 +102,13 @@ set_clock_groups -asynchronous -group [get_clocks sys_clk_pin] \ -group [get_clocks -of_objects [get_pins pix_mmcm/CLKOUT0]] set_false_path -to [get_ports {led[*] seg[*] dp an[*] RsTx}] +## Pmod JA top row: a PmodAMP2 (AIN, GAIN, NC, SHUTDOWN) +set_property -dict { PACKAGE_PIN J1 IOSTANDARD LVCMOS33 } [get_ports {JA[0]}] +set_property -dict { PACKAGE_PIN L2 IOSTANDARD LVCMOS33 } [get_ports {JA[1]}] +set_property -dict { PACKAGE_PIN J2 IOSTANDARD LVCMOS33 } [get_ports {JA[2]}] +set_property -dict { PACKAGE_PIN G2 IOSTANDARD LVCMOS33 } [get_ports {JA[3]}] +set_false_path -to [get_ports {JA[*]}] + ## Configuration set_property CONFIG_VOLTAGE 3.3 [current_design] set_property CFGBVS VCCO [current_design] diff --git a/docs/basic-manual.md b/docs/basic-manual.md index 59bd594..5370507 100644 --- a/docs/basic-manual.md +++ b/docs/basic-manual.md @@ -629,6 +629,7 @@ kept free for it. | 112–120, 128–135 | sprites (section 7) | sprites (section 7) | | 136 | keys held down, for games (below) | — | | 137 | 1 if the key picked with `OUT 137` is held down, else 0 | pick a key by its scan code | +| 144–157 | voice volumes now (152–154) | sound (below) | ``` 10 FOR I = 0 TO 255 @@ -677,6 +678,45 @@ game should throw those away now and then with `IF INP(0) AND 1 THEN K = INP(1)` in a loop. Held keys only work with the board's own keyboard, not with characters sent from the PC. +### Sound + +The board has no speaker of its own. Plug a Digilent **PmodAMP2** into the +top row of Pmod header **JA** (pin 1 at the end marked 1) and headphones or a +powered speaker into its jack. The FPGA has a little sound chip in the style +of the 8-bit computers: three voices that play square-wave tones, a noise +source they can use instead or as well, sixteen volumes and an envelope for +each voice, so a note or a bang fades away by itself. + +| Port | `OUT` | +|---|---| +| 144, 145 | voice 0 pitch: low byte, then high byte (0–15) | +| 146, 147 | voice 1 pitch | +| 148, 149 | voice 2 pitch | +| 150 | noise pitch, 0–31 (higher is lower and rougher) | +| 151 | mixer: add 1, 2, 4 for the tone of voice 0, 1, 2 and 8, 16, 32 for noise on voice 0, 1, 2 | +| 152, 153, 154 | voice 0, 1, 2 volume, 0 (off) to 15, which starts its envelope; `INP` gives the volume it has got to | +| 155, 156, 157 | voice 0, 1, 2 envelope: attack × 16 + decay, each 0–15 | + +- **Pitch** = 125000 ÷ frequency: 284 is the A above middle C (440 Hz), + 477 middle C. Each voice goes from about 30 Hz up beyond hearing. +- **Envelope:** with an attack, the volume climbs from 0 to the volume set, + one step every attack × 4 ms; then, with a decay, it falls back to 0 one step + every decay × 4 ms. 0 means no climb, or hold the volume until it is + changed. An envelope of 3 plays a short note; 8 with noise is an explosion. +- **Waiting for a sound to end:** `IF INP(152) > 0 THEN …` loops until voice + 0 has faded out. +- A voice that has neither its tone nor noise on in the mixer gives a steady + level, so changing its volume clicks; that is how the old chips played + samples. +- `CLS` silences everything (as does restarting the Z-80). + +``` +10 OUT 151, 1: OUT 155, 3 : REM TONE ON VOICE 0, A SHORT NOTE +20 OUT 144, 28: OUT 145, 1: OUT 152, 15 : REM PITCH 284 = 440 HZ +``` + +`basic/examples/sound.bas` plays a scale, a laser, an explosion and a chord. + ### Speed The Z-80 runs at 90 MHz and needs about 2.7 clocks per instruction, roughly 50 diff --git a/docs/basic-manual.pdf b/docs/basic-manual.pdf index f9a30c6..74e3ca9 100644 Binary files a/docs/basic-manual.pdf and b/docs/basic-manual.pdf differ diff --git a/rtl/basys3_terminal.v b/rtl/basys3_terminal.v index fd18a73..6ebb586 100644 --- a/rtl/basys3_terminal.v +++ b/rtl/basys3_terminal.v @@ -53,7 +53,8 @@ module basys3_terminal #( input PS2Clk, input PS2Data, output QspiCSn, - inout [3:0] QspiDB + inout [3:0] QspiDB, + output [3:0] JA // PmodAMP2: audio in, gain, -, shutdown (low) ); localparam [7:0] XON = 8'h11, XOFF = 8'h13; @@ -349,9 +350,14 @@ module basys3_terminal #( .scroll_x(scroll_x), .scroll_y(scroll_y), .g_border(g_border), .sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on), .sp_pix(sp_pix), .sp_ftick(pix_frame), .keys(keys_down), - .fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1]) + .fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1]), + .audio(JA[0]) ); + // PmodAMP2 on JA's top row: gain high is 6 dB (low would be 12 dB), and + // the amplifier is on unless its shutdown pin is low. + assign JA[3:1] = 3'b101; + // The flash clock is the configuration clock pin, reached through STARTUPE2. // DQ2/DQ3 (write protect, hold) stay high. assign QspiDB[1] = 1'bz; diff --git a/rtl/sound.v b/rtl/sound.v new file mode 100644 index 0000000..c1f2946 --- /dev/null +++ b/rtl/sound.v @@ -0,0 +1,173 @@ +`timescale 1ns / 1ps +// A small sound chip for the Z-80, in the style of the 8-bit era's +// programmable sound generators, played through a Digilent PmodAMP2. +// +// Three square-wave voices and a noise source. Each voice plays its tone, +// the noise, or both (the mixer), at one of 16 volumes on a log scale (about +// 3 dB a step). A volume write starts an envelope: an optional rise from 0 at +// the attack rate, then an optional fall to 0 at the decay rate, so a game +// can fire off a shot or an explosion with a few OUTs and leave it. +// +// 90h/91h voice 0 pitch, low byte / high 4 bits frequency = 125000 / pitch Hz +// 92h/93h voice 1 pitch (pitch 0 counts as 1) +// 94h/95h voice 2 pitch +// 96h noise pitch, 0-31: higher is lower and rougher +// 97h mixer: bits 0-2 tone on for voices 0-2, bits 3-5 noise on for them +// 98h-9Ah voice 0-2 volume 0-15, which (re)starts its envelope; reading +// gives the volume it is at now, so 0 means the sound has ended +// 9Bh-9Dh voice 0-2 envelope: high nibble attack, low nibble decay, each +// n x 4 ms a volume step (0 = jump straight up / hold) +// +// A voice with neither its tone nor the noise on outputs a steady level, as +// on the old chips. `hush` (CLS) and reset silence everything. +// +// The three voices are added up and turned into a single bit by a first-order +// sigma-delta modulator at the full clock rate; the PmodAMP2 filters it and +// drives headphones or a speaker. +module sound #( + parameter CPU_HZ = 90_000_000 +) ( + input clk, + input rst, + input hush, + input io_wr, + input [7:0] io_port, + input [7:0] io_wd, + output reg [7:0] io_q, // for reads of 98h-9Ah + output reg audio = 1'b0 +); + localparam TICK_DIV = CPU_HZ / 250_000; // tone counters run at 250 kHz + localparam MS4_DIV = CPU_HZ / 250; // envelope steps in 4 ms units + + reg [11:0] pitch [0:2]; + reg [4:0] npitch; + reg [5:0] mixer; + reg [3:0] vol [0:2]; // the volume asked for + reg [3:0] cur [0:2]; // the volume now + reg [3:0] att [0:2], dec [0:2]; + reg rising [0:2]; + + // ---------------------------------------------------------------- ticks + reg [9:0] tdiv = 10'd0; + reg tick = 1'b0; + reg [19:0] ediv = 20'd0; + reg etick = 1'b0; + always @(posedge clk) begin + tick <= tdiv == 0; + tdiv <= tdiv == 0 ? TICK_DIV - 1 : tdiv - 1'b1; + etick <= ediv == 0; + ediv <= ediv == 0 ? MS4_DIV - 1 : ediv - 1'b1; + end + + // --------------------------------------------------------------- voices + reg [11:0] tcnt [0:2]; + reg [2:0] tone = 3'd0; + reg [4:0] ncnt = 5'd0; + reg [16:0] lfsr = 17'd1; + reg [3:0] ecnt [0:2]; + integer i; + initial for (i = 0; i < 3; i = i + 1) begin + tcnt[i] = 12'd0; ecnt[i] = 4'd0; cur[i] = 4'd0; vol[i] = 4'd0; + end + + always @(posedge clk) begin + if (rst || hush) begin + for (i = 0; i < 3; i = i + 1) begin + vol[i] <= 4'd0; cur[i] <= 4'd0; rising[i] <= 1'b0; + if (rst) begin pitch[i] <= 12'd0; att[i] <= 4'd0; dec[i] <= 4'd0; end + end + mixer <= 6'd0; + if (rst) begin npitch <= 5'd0; lfsr <= 17'd1; tone <= 3'd0; end + end else begin + if (io_wr) case (io_port) + 8'h90: pitch[0][7:0] <= io_wd; + 8'h91: pitch[0][11:8] <= io_wd[3:0]; + 8'h92: pitch[1][7:0] <= io_wd; + 8'h93: pitch[1][11:8] <= io_wd[3:0]; + 8'h94: pitch[2][7:0] <= io_wd; + 8'h95: pitch[2][11:8] <= io_wd[3:0]; + 8'h96: npitch <= io_wd[4:0]; + 8'h97: mixer <= io_wd[5:0]; + 8'h9B: {att[0], dec[0]} <= io_wd; + 8'h9C: {att[1], dec[1]} <= io_wd; + 8'h9D: {att[2], dec[2]} <= io_wd; + default: ; + endcase + + // tones: each output flips every `pitch` ticks + if (tick) begin + for (i = 0; i < 3; i = i + 1) + if (tcnt[i] <= 12'd1) begin + tcnt[i] <= pitch[i]; + tone[i] <= ~tone[i]; + end else + tcnt[i] <= tcnt[i] - 1'b1; + if (ncnt == 0) begin + ncnt <= npitch; + lfsr <= {lfsr[0] ^ lfsr[3], lfsr[16:1]}; + end else + ncnt <= ncnt - 1'b1; + end + + // envelopes + for (i = 0; i < 3; i = i + 1) begin + if (io_wr && io_port == 8'h98 + i) begin + vol[i] <= io_wd[3:0]; + ecnt[i] <= 4'd0; + if (att[i] != 0 && io_wd[3:0] != 0) begin + cur[i] <= 4'd0; rising[i] <= 1'b1; + end else begin + cur[i] <= io_wd[3:0]; rising[i] <= 1'b0; + end + end else if (etick) begin + if (rising[i]) begin + if (ecnt[i] + 1'b1 >= att[i]) begin + ecnt[i] <= 4'd0; + cur[i] <= cur[i] + 1'b1; + if (cur[i] + 1'b1 >= vol[i]) rising[i] <= 1'b0; + end else + ecnt[i] <= ecnt[i] + 1'b1; + end else if (dec[i] != 0 && cur[i] != 0) begin + if (ecnt[i] + 1'b1 >= dec[i]) begin + ecnt[i] <= 4'd0; + cur[i] <= cur[i] - 1'b1; + end else + ecnt[i] <= ecnt[i] + 1'b1; + end + end + end + end + end + + always @* case (io_port) + 8'h98: io_q = {4'd0, cur[0]}; + 8'h99: io_q = {4'd0, cur[1]}; + 8'h9A: io_q = {4'd0, cur[2]}; + default: io_q = 8'hFF; + endcase + + // ---------------------------------------------------------------- mixing + function [7:0] level(input [3:0] v); // about 3 dB a step + case (v) + 4'd0: level = 8'd0; 4'd1: level = 8'd2; 4'd2: level = 8'd3; 4'd3: level = 8'd4; + 4'd4: level = 8'd6; 4'd5: level = 8'd8; 4'd6: level = 8'd11; 4'd7: level = 8'd16; + 4'd8: level = 8'd23; 4'd9: level = 8'd32; 4'd10: level = 8'd45; 4'd11: level = 8'd64; + 4'd12: level = 8'd90; 4'd13: level = 8'd128; 4'd14: level = 8'd181; default: level = 8'd255; + endcase + endfunction + + wire [2:0] on; + genvar g; + generate for (g = 0; g < 3; g = g + 1) begin : mix + assign on[g] = (tone[g] | ~mixer[g]) & (lfsr[0] | ~mixer[g + 3]); + end endgenerate + + reg [9:0] sum = 10'd0; + reg [10:0] acc = 11'd0; + always @(posedge clk) begin + sum <= (on[0] ? level(cur[0]) : 10'd0) + (on[1] ? level(cur[1]) : 10'd0) + + (on[2] ? level(cur[2]) : 10'd0); + acc <= rst ? 11'd0 : {1'b0, acc[9:0]} + {1'b0, sum}; // carry out = the output bit + audio <= acc[10]; + end +endmodule diff --git a/rtl/z80_system.v b/rtl/z80_system.v index ae66d9f..e945cc3 100644 --- a/rtl/z80_system.v +++ b/rtl/z80_system.v @@ -85,7 +85,9 @@ module z80_system #( output fl_cs_n, output fl_sck, output fl_mosi, - input fl_miso + input fl_miso, + // sound, for a PmodAMP2 + output audio ); wire [15:0] ma, io_addr, pc_dbg; wire we, io_rd, io_wr, ins_start; @@ -186,6 +188,13 @@ module z80_system #( .rd_on(sp_on), .pix(sp_pix), .gfx_pix(gfx_vpix), .ftick(sp_ftick) ); + // ---------------------------------------------------------------- sound + wire [7:0] snd_q; + sound #(.CPU_HZ(CPU_HZ)) snd ( + .clk(clk), .rst(rst), .hush(io_wr && io_addr[7:0] == 8'h34 && !io_wd[7]), + .io_wr(io_wr), .io_port(io_addr[7:0]), .io_wd(io_wd), .io_q(snd_q), .audio(audio) + ); + // ---------------------------------------------------------- held keys // The keyboard's clock writes these; a bit that changes while it is read // just reads as before or after. @@ -296,6 +305,8 @@ module z80_system #( io_din = spr_q; 8'h88: io_din = joy; 8'h89: io_din = {7'd0, keys[key_sel]}; + 8'h98, 8'h99, 8'h9A: + io_din = snd_q; default: io_din = 8'hFF; endcase diff --git a/scripts/sim-sound.sh b/scripts/sim-sound.sh new file mode 100755 index 0000000..f906baf --- /dev/null +++ b/scripts/sim-sound.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# Run the sound chip's testbench (sim/tb_sound.v) in Vivado's simulator. +set -e +cd "$(dirname "$0")/.." +ROOT=$(pwd) +source scripts/vivado_env.sh +mkdir -p sim/work-sound +cd sim/work-sound +xvlog "$ROOT"/rtl/sound.v "$ROOT"/sim/tb_sound.v +xelab -debug typical tb_sound -s tb_sound +xsim tb_sound -R diff --git a/sim/tb_sound.v b/sim/tb_sound.v new file mode 100644 index 0000000..90dd7ff --- /dev/null +++ b/sim/tb_sound.v @@ -0,0 +1,142 @@ +`timescale 1ns / 1ps +// Testbench for rtl/sound.v at the Z-80's 90 MHz clock: pitch, mixer, noise, +// volume levels, the attack and decay envelopes, reading back and hushing. +// scripts/sim-sound.sh +module tb_sound; + reg clk = 0, rst = 1, hush = 0, io_wr = 0; + reg [7:0] port = 0, wd = 0; + wire [7:0] q; + wire audio; + integer errors = 0; + + sound #(.CPU_HZ(90_000_000)) dut ( + .clk(clk), .rst(rst), .hush(hush), .io_wr(io_wr), .io_port(port), .io_wd(wd), + .io_q(q), .audio(audio) + ); + always #5.556 clk = ~clk; + + task out(input [7:0] p, input [7:0] v); + begin + @(posedge clk) begin port <= p; wd <= v; io_wr <= 1; end + @(posedge clk) io_wr <= 0; + end + endtask + + task rd(input [7:0] p); // q then holds the port's value + begin port = p; #1; end + endtask + + task check(input [8*48-1:0] what, input ok); + begin + if (ok) $display("PASS %0s", what); + else begin $display("FAIL %0s", what); errors = errors + 1; end + end + endtask + + // count rising edges of voice 0's tone over a time, and the output's duty + integer edges, ones, total; + real t0, duty; + task measure(input real ms); + begin + edges = 0; ones = 0; total = 0; + t0 = $realtime; + while ($realtime - t0 < ms * 1e6) begin + @(posedge clk); + total = total + 1; + ones = ones + audio; + end + duty = 1.0 * ones / total; + end + endtask + reg t_prev = 0; + integer tone_edges = 0; + always @(posedge clk) begin + t_prev <= dut.tone[0]; + if (dut.tone[0] && !t_prev) tone_edges = tone_edges + 1; + end + + real d15, d7; + integer n0, v, k, rose_at, fell_at; + initial begin + repeat (10) @(posedge clk); + rst = 0; + repeat (10) @(posedge clk); + measure(1.0); + check("no output after reset", ones == 0); + + // voice 0 at 440 Hz: 125000 / 284 = 440.1 + out(8'h90, 284 % 256); out(8'h91, 284 / 256); + out(8'h97, 8'b000_001); + out(8'h98, 8'd15); + n0 = tone_edges; + measure(50.0); + $display(" voice 0: %0d cycles in 50 ms (%.1f Hz), duty %.3f", tone_edges - n0, + (tone_edges - n0) / 0.05, duty); + check("pitch 284 plays about 440 Hz", tone_edges - n0 >= 21 && tone_edges - n0 <= 23); + d15 = duty; + check("one voice at full volume: high 1/8 of the time", d15 > 0.11 && d15 < 0.13); + out(8'h98, 8'd7); + measure(50.0); + d7 = duty; + $display(" volume 15 duty %.4f, volume 7 duty %.4f (ratio %.1f)", d15, d7, d15 / d7); + check("volume 7 is about 24 dB quieter", d15 / d7 > 12 && d15 / d7 < 20); + rd(8'h98); + check("volume reads back", q == 8'd7); + + // decay: 15 steps of 2 x 4 ms from 15 = 120 ms + out(8'h9B, 8'h02); + out(8'h98, 8'd15); + t0 = $realtime; + wait (dut.cur[0] == 0); + $display(" decay from 15 took %.1f ms", ($realtime - t0) / 1e6); + check("decay 2 fades 15 steps in 120 ms", ($realtime - t0) / 1e6 > 115 && ($realtime - t0) / 1e6 < 125); + rd(8'h98); + check("a finished sound reads back 0", q == 8'd0); + + // attack then decay: up 12 steps of 1 x 4 ms, then down + out(8'h9B, 8'h13); + out(8'h98, 8'd12); + t0 = $realtime; + check("attack starts from 0", dut.cur[0] == 0); + wait (dut.cur[0] == 12); + rose_at = ($realtime - t0) / 1e6; + wait (dut.cur[0] == 0); + fell_at = ($realtime - t0) / 1e6; + $display(" attack reached 12 at %0d ms, faded out at %0d ms", rose_at, fell_at); + check("attack 1 rises 12 steps in 48 ms", rose_at >= 46 && rose_at <= 50); + check("decay 3 then falls 12 steps in 144 ms", fell_at - rose_at >= 140 && fell_at - rose_at <= 150); + + // noise on voice 1, no tone + out(8'h96, 8'd4); + out(8'h97, 8'b010_000); + out(8'h9C, 8'h00); + out(8'h99, 8'd15); + n0 = 0; + for (k = 0; k < 2000; k = k + 1) begin + repeat (360) @(posedge clk); + n0 = n0 + dut.lfsr[0]; + end + $display(" noise was high %0d of 2000 samples", n0); + check("noise is about half high, half low", n0 > 800 && n0 < 1200); + + // all three voices and CLS + out(8'h92, 8'd142); out(8'h94, 8'd71); + out(8'h97, 8'b000_111); + out(8'h98, 8'd15); out(8'h99, 8'd15); out(8'h9A, 8'd15); + measure(20.0); + check("three voices make sound", duty > 0.2); + @(posedge clk) hush <= 1; + @(posedge clk) hush <= 0; + repeat (4) @(posedge clk); // what was already in the mixer + measure(2.0); + $display(" after CLS: output high %0d times in 2 ms", ones); + rd(8'h99); + check("CLS silences everything", ones == 0 && q == 0); + rd(8'h97); + check("other ports read FFh", q == 8'hFF); + + if (errors == 0) $display("ALL SOUND TESTS PASSED"); + else $display("%0d SOUND TESTS FAILED", errors); + $finish; + end +endmodule