204 lines
6.3 KiB
C++
204 lines
6.3 KiB
C++
#include <cstdint>
|
|
#include <bit>
|
|
|
|
#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_texture_wink_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)
|
|
{
|
|
const uint8_t * src = reinterpret_cast<const uint8_t *>(&_binary_wink_data_start);
|
|
uint8_t * dst = reinterpret_cast<uint8_t *>(buf);
|
|
|
|
uint32_t ix = 0;
|
|
dst[ix] = 0;
|
|
for (int i = 0; i < 48 * 32; i++) {
|
|
dst[ix] |= ((src[i] & 1) << (7 - (i % 8)));
|
|
|
|
if (i % 8 == 7) {
|
|
ix++;
|
|
dst[ix] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
inline void copy(T * dst, const T * src, const int32_t n) noexcept
|
|
{
|
|
int32_t n_t = n / (sizeof (T));
|
|
while (n_t > 0) {
|
|
*dst++ = *src++;
|
|
n_t--;
|
|
}
|
|
}
|
|
|
|
void send_wink(uint8_t port, uint8_t lm)
|
|
{
|
|
uint8_t send_buf[1024] __attribute__((aligned(32)));
|
|
uint8_t recv_buf[1024] __attribute__((aligned(32)));
|
|
|
|
using command_type = maple::block_write<uint8_t[0]>;
|
|
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<command_type, response_type>(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<uint16_t>(0x0000);
|
|
|
|
copy<uint8_t>(data_fields.written_data, reinterpret_cast<uint8_t *>(wink_buf), wink_size);
|
|
|
|
maple::dma_start(send_buf, writer.send_offset,
|
|
recv_buf, writer.recv_offset);
|
|
maple::dma_wait_complete();
|
|
|
|
serial::integer<uint8_t>(host_response->bus_data.command_code);
|
|
serial::integer<uint8_t>(host_response->bus_data.destination_ap);
|
|
serial::integer<uint8_t>(host_response->bus_data.source_ap);
|
|
serial::integer<uint8_t>(host_response->bus_data.data_size);
|
|
}
|
|
|
|
void do_lm_request(uint8_t port, uint8_t lm)
|
|
{
|
|
uint8_t send_buf[1024] __attribute__((aligned(32)));
|
|
uint8_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<command_type, response_type>(host_port_select,
|
|
destination_ap,
|
|
true); // end_flag
|
|
|
|
maple::dma_start(send_buf, writer.send_offset,
|
|
recv_buf, writer.recv_offset);
|
|
maple::dma_wait_complete();
|
|
|
|
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<uint8_t>(port, ' ');
|
|
serial::integer<uint8_t>(lm);
|
|
} else {
|
|
serial::string(" lm: ");
|
|
serial::integer<uint8_t>(lm);
|
|
serial::string(" ft: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.ft));
|
|
serial::string(" fd[0]: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.fd[0]));
|
|
serial::string(" fd[1]: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.fd[1]));
|
|
serial::string(" fd[2]: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.fd[2]));
|
|
serial::string(" source_ap.lm_bus: ");
|
|
serial::integer<uint8_t>(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()
|
|
{
|
|
uint8_t send_buf[1024] __attribute__((aligned(32)));
|
|
uint8_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<command_type, response_type>();
|
|
|
|
maple::dma_start(send_buf, writer.send_offset,
|
|
recv_buf, writer.recv_offset);
|
|
maple::dma_wait_complete();
|
|
|
|
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<uint8_t>(port);
|
|
serial::string(" disconnected\n");
|
|
} else {
|
|
serial::string("port: ");
|
|
serial::integer<uint8_t>(port);
|
|
serial::string(" ft: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.ft));
|
|
serial::string(" fd[0]: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.fd[0]));
|
|
serial::string(" fd[1]: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.fd[1]));
|
|
serial::string(" fd[2]: ");
|
|
serial::integer<uint32_t>(std::byteswap(data_fields.device_id.fd[2]));
|
|
serial::string(" source_ap.lm_bus: ");
|
|
serial::integer<uint8_t>(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);
|
|
}
|