// 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<>pc[2:0]) & ((16'h1<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<>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