// 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 // Coco 3 Plus: PIC7040 external ports and 2 KiB SRAM. // Behavioral reference: VCCPlus a8a84f22 src/devices/ssc/ssc_core.cpp; // MAME CoCo SSC wiring copyright Tim Lindner. Original notices: LICENSE. // Chip execution/audio is external to this block. All signals use clk. // Hold request/payload until io_ready, then withdraw request before another // access. Chip strobes occur once even if a completed request remains asserted. module ssc_glue ( input wire clk, reset, input wire io_request, io_write, input wire [1:0] io_port, // A=0, B=1, C=2, D=3; GPIO values after PIC DDR input wire [7:0] io_write_data, output reg io_ready, output reg [7:0] io_read_data, output reg init_done, input wire [7:0] command_data, output reg command_read, output wire pic_busy_line, output reg ay_select, ay_write, output reg [7:0] ay_bus_data, input wire [7:0] ay_read_data, output reg speech_ald, output reg [5:0] speech_code ); localparam [1:0] INITIALIZE=0, IDLE=1, EXECUTE=2, WAIT_RELEASE=3; reg [1:0] state; reg [10:0] clear_address; reg access_write; reg [1:0] access_port; reg [7:0] access_data; reg [7:0] port_b, port_c, port_d; reg [7:0] ram [0:2047]; reg [7:0] ram_read; wire ram_clear = state == INITIALIZE; wire ram_store = state == EXECUTE && access_write && access_port == 2 && !access_data[4] && !access_data[3]; wire [10:0] ram_write_address = ram_clear ? clear_address : {access_data[2:0],port_b}; wire [7:0] ram_write_data = ram_clear ? 8'h00 : port_d; wire ram_selected = !port_c[4] && port_c[3]; wire ay_selected_read = !port_c[6] && !port_c[3] && port_c[0]; // AY read overrides SRAM if both devices are selected, matching the reference. wire [7:0] read_d = ay_selected_read ? ay_read_data : ram_selected ? ram_read : port_d; assign pic_busy_line = port_c[7]; // Synchronous read/write inference, with a hardware scrub instead of resetting // every memory element. Keep the future PIC in reset until init_done. always @(posedge clk) begin ram_read <= ram[{port_c[2:0],port_b}]; if (!reset && (ram_clear || ram_store)) ram[ram_write_address] <= ram_write_data; end always @(posedge clk) begin if (reset) begin state <= INITIALIZE; clear_address <= 0; init_done <= 0; io_ready <= 0; io_read_data <= 0; command_read <= 0; ay_select <= 0; ay_write <= 0; ay_bus_data <= 0; speech_ald <= 0; speech_code <= 0; access_write <= 0; access_port <= 0; access_data <= 0; // Observable state after SscCore::Reset and the PIC's reset callbacks. port_b <= 8'hFF; port_c <= 8'h00; port_d <= 8'h00; end else begin command_read <= 0; ay_select <= 0; ay_write <= 0; speech_ald <= 0; case (state) INITIALIZE: begin if (clear_address == 11'd2047) begin init_done <= 1; state <= IDLE; end else clear_address <= clear_address + 1'b1; end IDLE: if (io_request) begin access_write <= io_write; access_port <= io_port; access_data <= io_write_data; state <= EXECUTE; end EXECUTE: begin if (access_write) begin case (access_port) 1: port_b <= access_data; 2: begin port_c <= access_data; if (!access_data[6] && access_data[3]) begin ay_bus_data <= port_d; ay_select <= access_data[0]; ay_write <= !access_data[0]; end if (port_c[5] && !access_data[5] && port_d < 8'd64) begin speech_ald <= 1; speech_code <= port_d[5:0]; end end 3: port_d <= access_data; endcase end else begin case (access_port) 0: begin io_read_data <= command_data; command_read <= 1; end 1: io_read_data <= port_b; 2: io_read_data <= port_c; 3: begin io_read_data <= read_d; port_d <= read_d; end endcase end io_ready <= 1; state <= WAIT_RELEASE; end WAIT_RELEASE: if (!io_request) begin io_ready <= 0; state <= IDLE; end endcase end end endmodule