Files
Basys3RiscV/sim/tb_sound.v
T
brad ArantandClaude Opus 5.5 5930b958b4 Basys 3 RISC-V computer
The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:09:18 -08:00

143 lines
5.1 KiB
Verilog

`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