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.
243 lines
9.8 KiB
Systemverilog
243 lines
9.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
|
|
// SP0256-AL2 cartridge engine. Public microsequencer/LPC algorithm derived from
|
|
// MAME Joseph Zbiciak, Tim Lindner, through pinned VCCPlus a8a84f22. See LICENSE.
|
|
// No mask ROM embedded. AL2's on-chip 2 KiB occupies byte addresses1000..17ff;
|
|
// unpopulated serial-ROM space reads zero. The cartridge has no host FIFO port.
|
|
// sample_ce is native speech clock/312; clk must allow completion before next CE.
|
|
module ssc_sp0256(input wire clk,reset,speech_reset,sample_ce,
|
|
input wire ald,input wire [5:0] allophone,
|
|
input wire rom_begin,rom_write,input wire [10:0] rom_address,input wire [7:0] rom_data,
|
|
output wire rom_loaded,load_error,output reg data_request,standby,
|
|
output reg signed [15:0] sample,output reg sample_valid,output wire busy,
|
|
output reg overrun,fault,output wire [18:0] debug_pc,output reg [31:0] frame_count);
|
|
`include "ssc_sp0256_tables.vh"
|
|
localparam [5:0] IDLE=0,MICRO=1,IMM=2,OPCODE=3,BRANCH=4,
|
|
CONTROL=5,FIELD_START=6,FIELD_APPLY=7,FIELD_NEXT=8,REGDEC=9,
|
|
COEF_B=10,COEF_F=11,EXCITE=12,FILTER_MULT=13,FILTER_SUM=14,LIMIT=15,
|
|
GET0=16,GET1=17,GET2=18,GET3=19,GET4=20,FILTER_PICK=21,COEF_DECODE=22,COEF_STORE=23;
|
|
reg [5:0] state,return_state;
|
|
reg [18:0] pc,stack,page;
|
|
assign debug_pc=pc;
|
|
assign busy=state!=IDLE;
|
|
reg halted,pending,fifo_selected,initialized;
|
|
reg [5:0] command,mode;
|
|
reg [3:0] immediate,opcode,bit_length;
|
|
reg [7:0] bit_value,control_index,control_last,byte0;
|
|
reg [15:0] control_word;
|
|
reg [15:0] next_byte;
|
|
reg first_valid,second_valid;
|
|
reg [10:0] read_address;
|
|
wire [7:0] read_data;
|
|
ssc_sp0256_memory memory(clk,reset,rom_begin,rom_write,rom_address,rom_data,
|
|
rom_loaded,load_error,read_address,read_data);
|
|
reg [7:0] r[0:15];
|
|
reg signed [8:0] rpt;
|
|
reg signed [10:0] cnt;
|
|
reg [7:0] period;
|
|
reg [11:0] amplitude;
|
|
reg [14:0] rng;
|
|
reg interpolate;
|
|
reg signed [9:0] bcoef[0:5],fcoef[0:5];
|
|
reg signed [15:0] z0[0:5],z1[0:5];
|
|
reg signed [15:0] excitation;
|
|
reg signed [25:0] product_b,product_f;
|
|
reg signed [9:0] operand_b,operand_f,decoded_coefficient;
|
|
reg signed [15:0] operand_z0,operand_z1;
|
|
reg [7:0] coefficient_input;
|
|
reg coefficient_forward;
|
|
wire signed [31:0] filter_sum=$signed(excitation)+($signed(product_b)>>>9)+($signed(product_f)>>>8);
|
|
reg [2:0] section;
|
|
reg [12:0] micro_steps;
|
|
integer j;
|
|
wire pulse=period!=0 ? cnt<=0 : cnt<=1;
|
|
wire [7:0] next_amplitude=r[0]+r[14];
|
|
wire [7:0] next_period=r[1]+r[15];
|
|
wire [7:0] signed_field=control_word[12] && bit_value[control_word[3:0]-1] ?
|
|
(bit_value | (8'hff<<control_word[3:0])) : bit_value;
|
|
wire [7:0] shifted_field=signed_field<<control_word[7:4];
|
|
wire [15:0] rom_bit_window=({second_valid?read_data:8'd0,byte0}>>pc[2:0]) & ((16'h1<<bit_length)-16'd1);
|
|
function [11:0] amp_decode(input [7:0] value);
|
|
begin amp_decode={7'd0,value[4:0]}<<value[7:5];end
|
|
endfunction
|
|
function signed [9:0] coefficient(input [7:0] value);
|
|
reg [7:0] negated;
|
|
begin negated=-value;
|
|
coefficient=value[7]?$signed({1'b0,sp_quant(negated[6:0])}):-$signed({1'b0,sp_quant(value[6:0])});
|
|
end
|
|
endfunction
|
|
function [3:0] reverse4(input [3:0] value);
|
|
begin reverse4={value[0],value[1],value[2],value[3]};end
|
|
endfunction
|
|
function [7:0] reverse8(input [7:0] value);
|
|
begin reverse8={value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7]};end
|
|
endfunction
|
|
task bits(input [3:0] length,input [5:0] destination);
|
|
begin bit_length<=length;return_state<=destination;state<=GET0;end
|
|
endtask
|
|
always @(posedge clk)begin
|
|
if(reset || rom_begin || !rom_loaded || speech_reset)begin
|
|
state<=IDLE;return_state<=IDLE;pc<=0;stack<=0;page<=19'h08000;
|
|
halted<=1;pending<=0;fifo_selected<=0;initialized<=0;command<=0;mode<=0;
|
|
immediate<=0;opcode<=0;bit_length<=0;bit_value<=0;control_index<=0;control_last<=0;
|
|
control_word<=0;byte0<=0;next_byte<=0;first_valid<=0;second_valid<=0;read_address<=0;
|
|
rpt<=-9'sd1;cnt<=0;period<=0;amplitude<=0;rng<=1;interpolate<=0;
|
|
excitation<=0;product_b<=0;product_f<=0;section<=0;micro_steps<=0;
|
|
operand_b<=0;operand_f<=0;operand_z0<=0;operand_z1<=0;decoded_coefficient<=0;
|
|
coefficient_input<=0;coefficient_forward<=0;
|
|
data_request<=rom_loaded&&!reset&&!rom_begin;standby<=1;sample<=0;sample_valid<=0;
|
|
overrun<=0;fault<=0;frame_count<=0;
|
|
for(j=0;j<16;j=j+1)r[j]<=0;
|
|
for(j=0;j<6;j=j+1)begin bcoef[j]<=0;fcoef[j]<=0;z0[j]<=0;z1[j]<=0;end
|
|
end else begin
|
|
sample_valid<=0;
|
|
if(!initialized)begin initialized<=1;data_request<=1;end
|
|
if(sample_ce && busy)overrun<=1;
|
|
case(state)
|
|
IDLE:if(sample_ce)begin micro_steps<=0;state<=rpt<=0?MICRO:EXCITE;end
|
|
MICRO:begin
|
|
micro_steps<=micro_steps+13'd1;
|
|
if(micro_steps==4095)begin
|
|
fault<=1;halted<=1;data_request<=1;pending<=0;standby<=1;
|
|
sample<=0;sample_valid<=1;state<=IDLE;
|
|
end else if(halted && pending)begin
|
|
pc<=19'h08000|{9'd0,command,4'd0};halted<=0;pending<=0;
|
|
data_request<=1;fifo_selected<=0;
|
|
for(j=0;j<16;j=j+1)r[j]<=0;
|
|
end else if(halted)begin
|
|
rpt<=1;data_request<=1;pending<=0;standby<=1;
|
|
for(j=0;j<16;j=j+1)r[j]<=0;
|
|
state<=EXCITE;
|
|
end else bits(4,IMM);
|
|
end
|
|
IMM:begin immediate<=bit_value[3:0];bits(4,OPCODE);end
|
|
OPCODE:begin
|
|
opcode<=bit_value[3:0];
|
|
if(bit_value!=1)mode<=mode&6'h0f;
|
|
case(bit_value[3:0])
|
|
0:begin
|
|
if(immediate!=0)page<={reverse4(immediate),15'd0};
|
|
else begin
|
|
stack<=0;pc<=stack;fifo_selected<=stack==19'h0c000;
|
|
if(stack==0)halted<=1;
|
|
end
|
|
state<=MICRO;
|
|
end
|
|
1:begin mode<={immediate[1:0],1'b0,immediate[2],immediate[3],1'b0};state<=MICRO;end
|
|
13,14:bits(8,BRANCH);
|
|
default:begin
|
|
if({mode[5:4],immediate}==0)state<=MICRO;
|
|
else begin
|
|
rpt<=$signed({3'd0,mode[5:4],immediate})+9'sd1;
|
|
control_index<=sp_index({bit_value[3:0],mode[2:1],1'b0});
|
|
control_last<=sp_index({bit_value[3:0],mode[2:1],1'b1});state<=CONTROL;
|
|
end
|
|
end
|
|
endcase
|
|
end
|
|
BRANCH:begin
|
|
if(opcode==13)stack<=(pc+19'd7)&19'h7fff8;
|
|
pc<=page|{4'd0,reverse4(immediate),11'd0}|{8'd0,reverse8(bit_value),3'd0};
|
|
fifo_selected<=(page|{4'd0,reverse4(immediate),11'd0}|{8'd0,reverse8(bit_value),3'd0})==19'h0c000;
|
|
state<=MICRO;
|
|
end
|
|
CONTROL:begin
|
|
if(control_index>176 || control_last>176)begin fault<=1;sample<=0;sample_valid<=1;state<=IDLE;end
|
|
else begin control_word<=sp_control(control_index);state<=FIELD_START;end
|
|
end
|
|
FIELD_START:begin
|
|
if(control_word[15])for(j=0;j<16;j=j+1)r[j]<=0;
|
|
if(control_word[14])begin r[12]<=0;r[13]<=0;end
|
|
if(control_word[3:0]!=0)bits(control_word[3:0],FIELD_APPLY);
|
|
else state<=FIELD_NEXT;
|
|
end
|
|
FIELD_APPLY:begin
|
|
if(control_word[13])r[control_word[11:8]]<=
|
|
(r[control_word[11:8]] & ~(8'hff<<control_word[7:4])) | shifted_field;
|
|
else if(control_word[12])r[control_word[11:8]]<=r[control_word[11:8]]+shifted_field;
|
|
else r[control_word[11:8]]<=shifted_field;
|
|
state<=FIELD_NEXT;
|
|
end
|
|
FIELD_NEXT:begin
|
|
if(control_index==control_last)begin
|
|
if(opcode==15)r[1]<=64;
|
|
state<=REGDEC;
|
|
end else begin control_index<=control_index+8'd1;state<=CONTROL;end
|
|
end
|
|
REGDEC:begin
|
|
amplitude<=amp_decode(r[0]);period<=r[1];cnt<=0;
|
|
interpolate<=r[14]!=0||r[15]!=0;section<=0;frame_count<=frame_count+32'd1;state<=COEF_B;
|
|
end
|
|
COEF_B:begin coefficient_input<=r[2+{section,1'b0}];coefficient_forward<=0;state<=COEF_DECODE;end
|
|
COEF_F:begin coefficient_input<=r[3+{section,1'b0}];coefficient_forward<=1;state<=COEF_DECODE;end
|
|
COEF_DECODE:begin decoded_coefficient<=coefficient(coefficient_input);state<=COEF_STORE;end
|
|
COEF_STORE:begin
|
|
if(coefficient_forward)begin
|
|
fcoef[section]<=decoded_coefficient;
|
|
if(section==5)state<=EXCITE;else begin section<=section+3'd1;state<=COEF_B;end
|
|
end else begin bcoef[section]<=decoded_coefficient;state<=COEF_F;end
|
|
end
|
|
EXCITE:begin
|
|
if(period!=0)begin
|
|
if(pulse)begin cnt<=cnt+$signed({3'd0,period});excitation<=$signed({4'd0,amplitude});end
|
|
else begin cnt<=cnt-11'sd1;excitation<=0;end
|
|
end else begin
|
|
cnt<=pulse?11'sd64:cnt-11'sd1;
|
|
rng<=(rng>>1)^(rng[0]?15'h4001:15'd0);
|
|
excitation<=rng[0]?$signed({4'd0,amplitude}):-$signed({4'd0,amplitude});
|
|
end
|
|
if(pulse)begin
|
|
rpt<=rpt-9'sd1;
|
|
for(j=0;j<6;j=j+1)begin z0[j]<=0;z1[j]<=0;end
|
|
if(interpolate)begin
|
|
r[0]<=next_amplitude;r[1]<=next_period;
|
|
amplitude<=amp_decode(next_amplitude);period<=next_period;
|
|
end
|
|
end
|
|
section<=0;state<=(pulse && rpt<=1)?MICRO:FILTER_PICK;
|
|
end
|
|
FILTER_PICK:begin
|
|
operand_b<=bcoef[section];operand_f<=fcoef[section];
|
|
operand_z0<=z0[section];operand_z1<=z1[section];state<=FILTER_MULT;
|
|
end
|
|
FILTER_MULT:begin
|
|
product_b<=$signed(operand_b)*$signed(operand_z1);
|
|
product_f<=$signed(operand_f)*$signed(operand_z0);state<=FILTER_SUM;
|
|
end
|
|
FILTER_SUM:begin
|
|
excitation<=filter_sum[15:0];z1[section]<=z0[section];z0[section]<=filter_sum[15:0];
|
|
if(section==5)state<=LIMIT;else begin section<=section+3'd1;state<=FILTER_PICK;end
|
|
end
|
|
LIMIT:begin
|
|
sample<=excitation > 16'sd8191 ? 16'sd32764 :
|
|
excitation < -16'sd8192 ? 16'sh8000 : excitation<<<2;
|
|
sample_valid<=1;state<=IDLE;
|
|
end
|
|
GET0:begin
|
|
if(fifo_selected)begin bit_value<=0;state<=return_state;end
|
|
else begin
|
|
read_address<=pc[13:3];first_valid<=pc[18:14]==2;
|
|
next_byte<=pc[18:3]+16'd1;state<=GET1;
|
|
end
|
|
end
|
|
GET1:state<=GET2;
|
|
GET2:begin
|
|
byte0<=first_valid?read_data:8'd0;read_address<=next_byte[10:0];
|
|
second_valid<=next_byte[15:11]==2;state<=GET3;
|
|
end
|
|
GET3:state<=GET4;
|
|
GET4:begin
|
|
bit_value<=rom_bit_window[7:0];
|
|
pc<=pc+{15'd0,bit_length};state<=return_state;
|
|
end
|
|
default:begin fault<=1;state<=IDLE;end
|
|
endcase
|
|
// A newly accepted ALD wins over a simultaneous idle/microsequencer update.
|
|
if(ald && data_request && initialized)begin
|
|
command<=allophone;pending<=1;data_request<=0;standby<=0;
|
|
end
|
|
end
|
|
end
|
|
endmodule
|