serial_protocol: add maple messages

This commit is contained in:
Zack Buhman 2024-11-08 23:03:33 -06:00
parent 77cdb3554f
commit 66f8eeeda2
4 changed files with 107 additions and 86 deletions

View File

@ -70,10 +70,10 @@ static inline void prestart_read()
}
struct state_arglen_reply command_list[] = {
{command::write, reply::write, fsm_state::write},
{command::read , reply::read , fsm_state::read },
{command::jump , reply::jump , fsm_state::jump },
{command::speed, reply::speed, fsm_state::speed},
{command::_write, reply::_write, fsm_state::write},
{command::_read , reply::_read , fsm_state::read },
{command::_jump , reply::_jump , fsm_state::jump },
{command::_speed, reply::_speed, fsm_state::speed},
};
constexpr uint32_t command_list_length = (sizeof (command_list)) / (sizeof (command_list[0]));
@ -89,13 +89,13 @@ void recv(uint8_t c)
uint32_t crc = crc32(&state.buf.u8[0], 12);
if (crc == state.buf.crc) {
// valid command, do the transition
if (state.buf.cmd == command::write) prestart_write();
if (state.buf.cmd == command::_write) prestart_write();
state.fsm_state = sar.fsm_state;
state.len = 0;
union command_reply reply = command_reply(sar.reply, state.buf.arg[0], state.buf.arg[1]);
serial::string(reply.u8, 16);
if (state.buf.cmd == command::read) prestart_read();
if (state.buf.cmd == command::_read) prestart_read();
return;
} else {
// do nothing
@ -130,7 +130,7 @@ void tick()
}
if (chcr1 & dmac::chcr::te::transfers_completed) {
state.write_crc.value ^= 0xffffffff;
union command_reply reply = write_crc_reply(state.write_crc.value);
union command_reply reply = reply::write_crc(state.write_crc.value);
serial::string(reply.u8, 16);
sh7091.DMAC.CHCR1 = 0;
@ -153,7 +153,7 @@ void tick()
if (chcr1 & dmac::chcr::te::transfers_completed) {
state.read_crc.value ^= 0xffffffff;
union command_reply reply = read_crc_reply(state.read_crc.value);
union command_reply reply = reply::read_crc(state.read_crc.value);
serial::string(reply.u8, 16);
sh7091.DMAC.CHCR1 = 0;

View File

@ -21,34 +21,6 @@ consteval uint32_t gen_cmd(const char * s)
return x;
}
namespace command {
constexpr uint32_t write = gen_cmd("WRTE");
constexpr uint32_t read = gen_cmd("READ");
constexpr uint32_t jump = gen_cmd("JUMP");
constexpr uint32_t speed = gen_cmd("SPED");
static_assert(write == 0x2cc46ed8);
static_assert(read == 0xf18d57c7);
static_assert(jump == 0xa6696f38);
static_assert(speed == 0x27a7a9f4);
}
namespace reply {
constexpr uint32_t write = gen_cmd("wrte");
constexpr uint32_t read = gen_cmd("read");
constexpr uint32_t jump = gen_cmd("jump");
constexpr uint32_t speed = gen_cmd("sped");
constexpr uint32_t write_crc = gen_cmd("crcW");
constexpr uint32_t read_crc = gen_cmd("crcR");
static_assert(write == 0x8c661aaa);
static_assert(read == 0x512f23b5);
static_assert(jump == 0x06cb1b4a);
static_assert(speed == 0x8705dd86);
static_assert(write_crc == 0x1cced074);
static_assert(read_crc == 0xb9ce82f4);
}
union command_reply {
uint8_t u8[16];
uint32_t u32[4];
@ -78,54 +50,92 @@ constexpr union command_reply command_reply(uint32_t cmd, uint32_t arg0, uint32_
return command;
}
constexpr union command_reply write_command(uint32_t dest, uint32_t size)
namespace command {
constexpr uint32_t _write = gen_cmd("WRTE");
constexpr uint32_t _read = gen_cmd("READ");
constexpr uint32_t _jump = gen_cmd("JUMP");
constexpr uint32_t _speed = gen_cmd("SPED");
constexpr uint32_t _maple_list = gen_cmd("MPLS");
static_assert(_write == 0x2cc46ed8);
static_assert(_read == 0xf18d57c7);
static_assert(_jump == 0xa6696f38);
static_assert(_speed == 0x27a7a9f4);
constexpr union command_reply write(uint32_t dest, uint32_t size)
{
return command_reply(command::write, dest, size);
return command_reply(_write, dest, size);
}
constexpr union command_reply read_command(uint32_t dest, uint32_t size)
constexpr union command_reply read(uint32_t dest, uint32_t size)
{
return command_reply(command::read, dest, size);
return command_reply(_read, dest, size);
}
constexpr union command_reply jump_command(uint32_t dest)
constexpr union command_reply jump(uint32_t dest)
{
return command_reply(command::jump, dest, 0);
return command_reply(_jump, dest, 0);
}
constexpr union command_reply speed_command(uint32_t speed)
constexpr union command_reply speed(uint32_t speed)
{
return command_reply(command::speed, speed, 0);
return command_reply(_speed, speed, 0);
}
constexpr union command_reply write_reply(uint32_t dest, uint32_t size)
constexpr union command_reply maple_list(uint32_t function_type)
{
return command_reply(reply::write, dest, size);
return command_reply(_maple_list, function_type, 0);
}
}
constexpr union command_reply read_reply(uint32_t dest, uint32_t size)
namespace reply {
constexpr uint32_t _write = gen_cmd("wrte");
constexpr uint32_t _read = gen_cmd("read");
constexpr uint32_t _jump = gen_cmd("jump");
constexpr uint32_t _speed = gen_cmd("sped");
constexpr uint32_t _write_crc = gen_cmd("crcw");
constexpr uint32_t _read_crc = gen_cmd("crcr");
constexpr uint32_t _maple_list = gen_cmd("mpls");
constexpr uint32_t _maple_list_crc = gen_cmd("mlcs");
static_assert(_write == 0x8c661aaa);
static_assert(_read == 0x512f23b5);
static_assert(_jump == 0x06cb1b4a);
static_assert(_speed == 0x8705dd86);
static_assert(_write_crc == 0x3cccc074);
static_assert(_read_crc == 0x99cc92f4);
constexpr union command_reply write(uint32_t dest, uint32_t size)
{
return command_reply(reply::read, dest, size);
return command_reply(_write, dest, size);
}
constexpr union command_reply jump_reply(uint32_t dest)
constexpr union command_reply read(uint32_t dest, uint32_t size)
{
return command_reply(reply::jump, dest, 0);
return command_reply(_read, dest, size);
}
constexpr union command_reply speed_reply(uint32_t speed)
constexpr union command_reply jump(uint32_t dest)
{
return command_reply(reply::speed, speed, 0);
return command_reply(_jump, dest, 0);
}
constexpr union command_reply write_crc_reply(uint32_t crc)
constexpr union command_reply speed(uint32_t speed)
{
return command_reply(reply::write_crc, crc, 0);
return command_reply(_speed, speed, 0);
}
constexpr union command_reply read_crc_reply(uint32_t crc)
constexpr union command_reply write_crc(uint32_t crc)
{
return command_reply(reply::read_crc, crc, 0);
return command_reply(_write_crc, crc, 0);
}
constexpr union command_reply read_crc(uint32_t crc)
{
return command_reply(_read_crc, crc, 0);
}
}
}

View File

@ -17,6 +17,8 @@ commands = [
"READ",
"JUMP",
"SPED",
"MPLS",
]
replies = [
@ -24,15 +26,24 @@ replies = [
"read",
"jump",
"sped",
"crcW",
"crcR",
"crcw",
"crcr",
"mpls",
"mlcs",
]
seen = set()
for c in commands:
x = gen_cmd(c.encode('ascii'))
assert x not in seen
seen.add(x)
print(c, f"{x:08x}")
for c in replies:
x = gen_cmd(c.encode('ascii'))
assert x not in seen
seen.add(x)
print(c, f"{x:08x}")

View File

@ -243,11 +243,11 @@ int do_write(struct ftdi_context * ftdi, const uint32_t dest, const uint8_t * bu
return -1;
}
union serial_load::command_reply command = serial_load::write_command(dest, size);
union serial_load::command_reply command = serial_load::command::write(dest, size);
res = ftdi_write_data(ftdi, command.u8, (sizeof (command)));
assert(res == (sizeof (command)));
union serial_load::command_reply reply;
res = read_reply(ftdi, serial_load::reply::write, reply);
res = read_reply(ftdi, serial_load::reply::_write, reply);
if (res != 0) {
return -2;
}
@ -273,7 +273,7 @@ int do_write(struct ftdi_context * ftdi, const uint32_t dest, const uint8_t * bu
uint32_t buf_crc = crc32(buf, size);
union serial_load::command_reply crc_reply;
res = read_reply(ftdi, serial_load::reply::write_crc, crc_reply);
res = read_reply(ftdi, serial_load::reply::_write_crc, crc_reply);
clock_res = clock_gettime(CLOCK_MONOTONIC, &end2);
assert(clock_res == 0);
if (res != 0) {
@ -307,12 +307,12 @@ int do_jump(struct ftdi_context * ftdi, const uint32_t dest)
{
int res;
union serial_load::command_reply command = serial_load::jump_command(dest);
union serial_load::command_reply command = serial_load::command::jump(dest);
res = ftdi_write_data(ftdi, command.u8, (sizeof (command)));
assert(res == (sizeof (command)));
union serial_load::command_reply reply;
res = read_reply(ftdi, serial_load::reply::jump, reply);
res = read_reply(ftdi, serial_load::reply::_jump, reply);
if (res != 0) {
return -2;
}
@ -454,11 +454,11 @@ int do_read(struct ftdi_context * ftdi, const uint32_t src, uint8_t * buf, const
return -1;
}
union serial_load::command_reply command = serial_load::read_command(src, size);
union serial_load::command_reply command = serial_load::command::read(src, size);
res = ftdi_write_data(ftdi, command.u8, (sizeof (command)));
assert(res == (sizeof (command)));
union serial_load::command_reply reply;
res = read_reply(ftdi, serial_load::reply::read, reply);
res = read_reply(ftdi, serial_load::reply::_read, reply);
if (res != 0) {
return -2;
}
@ -481,7 +481,7 @@ int do_read(struct ftdi_context * ftdi, const uint32_t src, uint8_t * buf, const
uint32_t buf_crc = crc32((uint8_t*)buf, size);
union serial_load::command_reply crc_reply;
res = read_reply(ftdi, serial_load::reply::read_crc, crc_reply);
res = read_reply(ftdi, serial_load::reply::_read_crc, crc_reply);
if (res != 0) {
return -1;
}
@ -501,12 +501,12 @@ int do_speed(struct ftdi_context * ftdi, uint32_t scbrr)
return -1;
}
union serial_load::command_reply command = serial_load::speed_command(scbrr);
union serial_load::command_reply command = serial_load::command::speed(scbrr);
res = ftdi_write_data(ftdi, command.u8, (sizeof (command)));
assert(res == (sizeof (command)));
union serial_load::command_reply reply;
res = read_reply(ftdi, serial_load::reply::speed, reply);
res = read_reply(ftdi, serial_load::reply::_speed, reply);
if (res != 0) {
return -2;
}