Add a three-voice sound chip for the PmodAMP2 on JA
Ports 90h-9Dh: three square-wave voices and a noise source, a mixer, 16 log volumes with attack/decay envelopes, summed and played through a sigma-delta output on JA[0]. CLS and reset hush it. Includes a testbench (scripts/sim-sound.sh), a BASIC example and manual section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b629cbec00
commit
2d1be15786
11 files changed
+428
-3
No files matched your search
+173
@@ -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
|
||||
Reference in new issue
Block a user