Files
CoCo3_Plus_MiSTer/rtl/ssc/ssc_ay8913.sv
T
bholman cb40fed3b9 Introduce Coco 3 Plus for MiSTer
Add original-firmware Sound & Speech, four configurable Multi-Pak sockets,
bounded per-slot ROM allocation and a nine-page menu organized around devices
and media. Repair inherited clock/reset/CPU-memory/video paths and add explicit
conditional external-I/O timing contracts. Preserve native Main startup-file
ordering, saved layouts, stock-core separation and the coco3plus media profile.

Provide native Windows tool downloads, pinned ToolShed ROM assembly, fresh
Quartus builds, functional and fitted timing checks, reviewed release packaging
and passwordless SSH installation with backups. Retain original component
notices, document contribution copyright and keep SSC ROM inputs outside Git.

Validation: Quartus17.0.2 Build602 seed5, zero errors; 15 functional jobs,
368 both-corner timing scopes and eight tooling-integrity checks pass.
The renamed build reaches Disk BASIC, PRINT1+1 returns2 and SSC loads ready.
Physical cold power cycling and conditional board-delay assumptions remain
separate. Full original upstream histories are preserved before this single
consolidated implementation commit.
2026-11-18 16:00:45 -07:00

107 lines
3.8 KiB
Systemverilog

// Original Coco 3 Plus contributions: Copyright (c) 2026 Brian K. Holman.
// Original component notices/terms retained; see COPYRIGHT.md and LICENSING.md.
// SPDX-License-Identifier: BSD-3-Clause
// AY-3-8913 behavioral hardware port for Coco 3 Plus.
// Reference: VCCPlus a8a84f22 src/devices/ssc/ay8913.{h,cpp}; see LICENSE.
// The reference volume table is a logarithmic approximation, not silicon data.
// clock_enable pulses once per AY INPUT clock; internal generators divide by 8.
// Same-edge register writes follow generator advancement using old registers.
// sample_sum is the unnormalized three-channel sum, with two pipeline stages.
// The future SSC mixer applies /3 normalization, gain, filtering and routing.
module ssc_ay8913(
input wire clk,reset,clock_enable,
input wire reg_select,reg_write,
input wire [7:0] bus_data,
output wire [7:0] read_data,
output reg [17:0] sample_sum
);
reg [7:0] registers[0:15];
reg [3:0] selected;
reg [2:0] divider;
reg [11:0] tone_counter[0:2];
reg [2:0] tone_output;
reg [4:0] noise_counter;
reg [16:0] noise_lfsr;
reg [15:0] env_counter;
reg [5:0] env_step;
reg env_up,env_holding;
reg [15:0] level[0:2];
wire [11:0] tone_period[0:2];
wire [4:0] noise_period=registers[6][4:0];
wire [15:0] env_period={registers[12],registers[11]};
wire [4:0] env_position=env_up?env_step[4:0]:~env_step[4:0];
wire [3:0] env_level=(!registers[13][3] && env_holding)?4'd0:env_position[4:1];
assign read_data=registers[selected];
function [15:0] volume(input [3:0] index);
begin
case(index)
0:volume=0;1:volume=1304;2:volume=1642;3:volume=2067;
4:volume=2603;5:volume=3277;6:volume=4125;7:volume=5193;
8:volume=6538;9:volume=8231;10:volume=10362;11:volume=13045;
12:volume=16422;13:volume=20675;14:volume=26028;15:volume=32767;
endcase
end
endfunction
genvar ch;
generate for(ch=0;ch<3;ch=ch+1) begin:channels
assign tone_period[ch]={registers[ch*2+1][3:0],registers[ch*2]};
wire gate=(registers[7][ch] || tone_output[ch]) &&
(registers[7][ch+3] || noise_lfsr[0]);
wire [3:0] amplitude=registers[ch+8][4]?env_level:registers[ch+8][3:0];
always @(posedge clk) begin
if(reset) level[ch]<=0;
else level[ch]<=gate?volume(amplitude):16'd0;
end
end endgenerate
integer i;
always @(posedge clk) begin
if(reset) begin
selected<=0;divider<=0;tone_output<=3'b111;
noise_counter<=0;noise_lfsr<=17'd1;
env_counter<=0;env_step<=0;env_up<=1;env_holding<=0;
sample_sum<=0;
for(i=0;i<16;i=i+1) registers[i]<=0;
for(i=0;i<3;i=i+1) tone_counter[i]<=0;
end else begin
sample_sum<={2'b0,level[0]}+{2'b0,level[1]}+{2'b0,level[2]};
if(clock_enable) begin
divider<=divider+1'b1;
if(divider==3'd7) begin
for(i=0;i<3;i=i+1) begin
if(tone_counter[i]>=(tone_period[i]==0?12'd0:tone_period[i]-12'd1)) begin
tone_counter[i]<=0;tone_output[i]<=!tone_output[i];
end else tone_counter[i]<=tone_counter[i]+1'b1;
end
if(noise_counter>=(noise_period==0?5'd0:noise_period-5'd1)) begin
noise_counter<=0;
noise_lfsr<={noise_lfsr[0]^noise_lfsr[3],noise_lfsr[16:1]};
end else noise_counter<=noise_counter+1'b1;
if(env_counter>=(env_period==0?16'd0:env_period-16'd1)) begin
env_counter<=0;
if(!env_holding) begin
if(env_step==6'd31) begin
if(!registers[13][3]) begin env_step<=32;env_holding<=1;end
else if(registers[13][0]) begin
env_step<=31;env_holding<=1;
if(registers[13][1]) env_up<=!env_up;
end else begin
env_step<=0;
if(registers[13][1]) env_up<=!env_up;
end
end else env_step<=env_step+1'b1;
end
end else env_counter<=env_counter+1'b1;
end
end
if(reg_select) selected<=bus_data[3:0];
if(reg_write) begin
registers[selected]<=bus_data;
if(selected==4'd13) begin
env_step<=0;env_holding<=0;env_up<=bus_data[2];
end
end
end
end
endmodule