diff --git a/example/maple_device_request.cpp b/example/maple_device_request.cpp index 7857d68..68ffb6e 100644 --- a/example/maple_device_request.cpp +++ b/example/maple_device_request.cpp @@ -1,37 +1,49 @@ -#include "align.hpp" +#include #include "maple/maple.hpp" -#include "maple/maple_impl.hpp" +#include "maple/maple_host_command_writer.hpp" #include "maple/maple_bus_commands.hpp" #include "sh7091/serial.hpp" -uint32_t _command_buf[(1024 + 32) / 4]; -uint32_t _receive_buf[(1024 + 32) / 4]; - void main() { - uint32_t * command_buf = align_32byte(_command_buf); - uint32_t * receive_buf = align_32byte(_receive_buf); + serial::init(4); - using command_type = device_request; - using response_type = device_status; + uint32_t send_buf[1024] __attribute__((aligned(32))); + uint32_t recv_buf[1024] __attribute__((aligned(32))); - uint32_t command_size = maple::init_host_command_all_ports(command_buf, receive_buf); + using command_type = maple::device_request; + using response_type = maple::device_status; - constexpr uint32_t host_response_size = (sizeof (maple::host_response)); + auto writer = maple::host_command_writer(send_buf, recv_buf); - maple::dma_start(command_buf, command_size, - receive_buf, host_response_size); + auto [host_command, host_response] + = writer.append_command_all_ports(); + + maple::dma_start(send_buf, writer.send_offset, + recv_buf, writer.recv_offset); - uint8_t * buf = reinterpret_cast(receive_buf); for (uint8_t port = 0; port < 4; port++) { - serial::string("port "); - serial::integer(port); - - for (uint32_t i = 0; i < host_response_size; i++) { - serial::integer(buf[port * host_response_size + i]); + auto& bus_data = host_response[port].bus_data; + auto& data_fields = bus_data.data_fields; + if (bus_data.command_code != response_type::command_code) { + serial::string("port: "); + serial::integer(port); + serial::string(" disconnected\n"); + } else { + serial::string("port: "); + serial::integer(port); + serial::string(" ft: "); + serial::integer(std::byteswap(data_fields.device_id.ft)); + serial::string(" fd[0]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[0])); + serial::string(" fd[1]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[1])); + serial::string(" fd[2]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[2])); + serial::string(" source_ap.lm_bus: "); + serial::integer(bus_data.source_ap & ap::lm_bus::bit_mask); } - serial::character('\n'); } while (1); diff --git a/example/maple_storage.cpp b/example/maple_storage.cpp index e920bff..a138ce4 100644 --- a/example/maple_storage.cpp +++ b/example/maple_storage.cpp @@ -310,11 +310,16 @@ void do_lm_request(uint8_t port, uint8_t lm) destination_ap, true); // end_flag + serial::string("lm command send_offset "); + serial::integer(writer.send_offset); + serial::hexlify(send_buf, writer.send_offset); + maple::dma_start(send_buf, writer.send_offset, recv_buf, writer.recv_offset); auto& bus_data = host_response->bus_data; auto& data_fields = bus_data.data_fields; + serial::hexlify(recv_buf, writer.recv_offset); if (bus_data.command_code != response_type::command_code) { serial::string("lm did not reply: "); serial::integer(port, ' '); @@ -581,9 +586,9 @@ void do_device_request() void main() { - // flycast needs this in HLE mode, or else it won't start the vcount - // counter. - video_output::set_mode_vga(); + serial::init(4); do_device_request(); + + while (1); } diff --git a/example/maple_wink.cpp b/example/maple_wink.cpp index 675641e..30817c4 100644 --- a/example/maple_wink.cpp +++ b/example/maple_wink.cpp @@ -1,12 +1,23 @@ #include +#include #include "maple/maple.hpp" #include "maple/maple_bus_commands.hpp" #include "maple/maple_bus_bits.hpp" +#include "maple/maple_host_command_writer.hpp" +#include "maple/maple_port.hpp" #include "align.hpp" #include "sh7091/serial.hpp" +#include "holly/video_output.hpp" + extern uint32_t _binary_wink_data_start __asm("_binary_wink_data_start"); +constexpr uint32_t width = 48; +constexpr uint32_t height = 32; +constexpr uint32_t pixels_per_byte = 8; +constexpr uint32_t wink_size = width * height / pixels_per_byte; + +uint32_t wink_buf[wink_size / 4]; void make_wink(uint32_t * buf) { @@ -25,35 +36,165 @@ void make_wink(uint32_t * buf) } } -constexpr uint32_t width = 48; -constexpr uint32_t height = 32; -constexpr uint32_t pixels_per_byte = 8; -constexpr uint32_t wink_size = width * height / pixels_per_byte; - -uint32_t _command_buf[(1024 + 32) / 4]; -uint32_t _receive_buf[(1024 + 32) / 4]; - -void main() +template +inline void copy(T * dst, const T * src, const int32_t n) noexcept { - uint32_t wink_buf[wink_size / 4]; - make_wink(wink_buf); + int32_t n_t = n / (sizeof (T)); + while (n_t > 0) { + *dst++ = *src++; + n_t--; + } +} - uint32_t * command_buf = align_32byte(_command_buf); - uint32_t * receive_buf = align_32byte(_receive_buf); +void send_wink(uint8_t port, uint8_t lm) +{ + uint32_t send_buf[1024] __attribute__((aligned(32))); + uint32_t recv_buf[1024] __attribute__((aligned(32))); - const uint32_t command_size = maple::init_block_write(command_buf, receive_buf, - host_instruction::port_select::b, - ap::de::expansion_device | ap::port_select::b | ap::lm_bus::_0, - wink_buf, - wink_size); - using response_type = device_reply; - using host_response_type = struct maple::host_response; - auto host_response = reinterpret_cast(receive_buf); - maple::dma_start(command_buf, command_size, - receive_buf, maple::sizeof_command(host_response)); + using command_type = maple::block_write; + using response_type = maple::device_reply; + + auto writer = maple::host_command_writer(send_buf, recv_buf); + + uint32_t host_port_select = host_instruction_port_select(port); + uint32_t destination_ap = ap_port_select(port) | ap::de::expansion_device | lm; + + auto [host_command, host_response] + = writer.append_command(host_port_select, + destination_ap, + true, // end_flag + wink_size, // send_trailing + 0 // recv_trailing + ); + auto& data_fields = host_command->bus_data.data_fields; + data_fields.function_type = std::byteswap(function_type::bw_lcd); + data_fields.pt = 0; + data_fields.phase = 0; + data_fields.block_number = std::byteswap(0x0000); + + copy(data_fields.written_data, reinterpret_cast(wink_buf), wink_size); + + maple::dma_start(send_buf, writer.send_offset, + recv_buf, writer.recv_offset); serial::integer(host_response->bus_data.command_code); serial::integer(host_response->bus_data.destination_ap); serial::integer(host_response->bus_data.source_ap); serial::integer(host_response->bus_data.data_size); } + +void do_lm_request(uint8_t port, uint8_t lm) +{ + uint32_t send_buf[1024] __attribute__((aligned(32))); + uint32_t recv_buf[1024] __attribute__((aligned(32))); + + auto writer = maple::host_command_writer(send_buf, recv_buf); + + uint32_t host_port_select = host_instruction_port_select(port); + uint32_t destination_ap = ap_port_select(port) | ap::de::expansion_device | lm; + + using command_type = maple::device_request; + using response_type = maple::device_status; + + auto [host_command, host_response] + = writer.append_command(host_port_select, + destination_ap, + true); // end_flag + + maple::dma_start(send_buf, writer.send_offset, + recv_buf, writer.recv_offset); + + auto& bus_data = host_response->bus_data; + auto& data_fields = bus_data.data_fields; + if (bus_data.command_code != maple::device_status::command_code) { + serial::string("lm did not reply: "); + serial::integer(port, ' '); + serial::integer(lm); + } else { + serial::string(" lm: "); + serial::integer(lm); + serial::string(" ft: "); + serial::integer(std::byteswap(data_fields.device_id.ft)); + serial::string(" fd[0]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[0])); + serial::string(" fd[1]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[1])); + serial::string(" fd[2]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[2])); + serial::string(" source_ap.lm_bus: "); + serial::integer(bus_data.source_ap & ap::lm_bus::bit_mask); + + if (std::byteswap(data_fields.device_id.ft) & function_type::bw_lcd) { + serial::string("send wink\n"); + send_wink(port, lm); + } + } +} + +void do_lm_requests(uint8_t port, uint8_t lm) +{ + if (lm & ap::lm_bus::_0) + do_lm_request(port, lm & ap::lm_bus::_0); + if (lm & ap::lm_bus::_1) + do_lm_request(port, lm & ap::lm_bus::_1); + if (lm & ap::lm_bus::_2) + do_lm_request(port, lm & ap::lm_bus::_2); + if (lm & ap::lm_bus::_3) + do_lm_request(port, lm & ap::lm_bus::_3); + if (lm & ap::lm_bus::_4) + do_lm_request(port, lm & ap::lm_bus::_4); +} + +void do_device_request() +{ + uint32_t send_buf[1024] __attribute__((aligned(32))); + uint32_t recv_buf[1024] __attribute__((aligned(32))); + + auto writer = maple::host_command_writer(send_buf, recv_buf); + + using command_type = maple::device_request; + using response_type = maple::device_status; + + auto [host_command, host_response] + = writer.append_command_all_ports(); + + maple::dma_start(send_buf, writer.send_offset, + recv_buf, writer.recv_offset); + + for (uint8_t port = 0; port < 4; port++) { + auto& bus_data = host_response[port].bus_data; + auto& data_fields = bus_data.data_fields; + if (bus_data.command_code != response_type::command_code) { + serial::string("port: "); + serial::integer(port); + serial::string(" disconnected\n"); + } else { + serial::string("port: "); + serial::integer(port); + serial::string(" ft: "); + serial::integer(std::byteswap(data_fields.device_id.ft)); + serial::string(" fd[0]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[0])); + serial::string(" fd[1]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[1])); + serial::string(" fd[2]: "); + serial::integer(std::byteswap(data_fields.device_id.fd[2])); + serial::string(" source_ap.lm_bus: "); + serial::integer(bus_data.source_ap & ap::lm_bus::bit_mask); + + do_lm_requests(port, + bus_data.source_ap & ap::lm_bus::bit_mask); + } + } +} + +void main() +{ + serial::init(4); + + make_wink(wink_buf); + + do_device_request(); + + while (1); +}