From abdd82019a2e45d45e914221f0a903e29fdb8cac Mon Sep 17 00:00:00 2001 From: haskal Date: Tue, 22 Jun 2021 03:57:02 -0400 Subject: [PATCH] red --- libsqfake.c | 68 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/libsqfake.c b/libsqfake.c index 9d7b8e0..d346a88 100644 --- a/libsqfake.c +++ b/libsqfake.c @@ -229,6 +229,9 @@ enum { CAP_READING } sq_state = APP_INIT; +bool settings_init = false; +unsigned char sq_settings[24] = {0}; + FT_STATUS FT_Close(FT_HANDLE ftHandle) { /* TRACE("FT_Close"); */ @@ -288,14 +291,14 @@ FT_STATUS FT_OpenEx(PVOID pArg1, DWORD flags, FT_HANDLE* pHandle) { } FT_STATUS FT_Read(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpBytesReturned) { - TRACE("FT_Read"); + /* TRACE("FT_Read"); */ size_t max_bytes = dwBytesToRead; if (read_state.len - read_state.pos > max_bytes) { max_bytes = read_state.len - read_state.pos; } - printf("req size: %lu | state pos %lu state len %lu\n", max_bytes, - read_state.pos, read_state.len); + /* printf("req size: %lu | state pos %lu state len %lu\n", max_bytes, */ + /* read_state.pos, read_state.len); */ memcpy(lpBuffer, &read_state.data[read_state.pos], max_bytes); read_state.pos += max_bytes; *lpBytesReturned = max_bytes; @@ -336,6 +339,17 @@ void dump(unsigned char* buffer, size_t len) { printf("\n"); } +void dump_with_color(unsigned char* buffer, unsigned char* compare, size_t len) { + for (size_t i = 0; i < len; i++) { + if (buffer[i] == compare[i]) { + printf("%02x ", buffer[i]); + } else { + printf("\x1b[1m\x1b[31m%02x\x1b[m\x0f ", buffer[i]); + } + } + printf("\n"); +} + void print_bin(unsigned char b) { for (int i = 7; i >= 0; i--) { if ((b>>i)&0x1) { @@ -347,15 +361,15 @@ void print_bin(unsigned char b) { } FT_STATUS FT_Write(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD len, LPDWORD written) { - TRACE("FT_Write"); + /* TRACE("FT_Write"); */ unsigned char* buffer = (unsigned char*) lpBuffer; switch (buffer[0]) { case 0x94: - TRACE("bootloader reset"); + /* TRACE("bootloader reset"); */ sq_state = BOOT_INIT; break; case 0x93: - TRACE("app reset"); + /* TRACE("app reset"); */ sq_state = APP_INIT; break; case 0xf1: @@ -365,8 +379,20 @@ FT_STATUS FT_Write(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD len, LPDWORD writt ABORT("wrong code!"); } } else { - printf("settings buffer:\n"); - dump(buffer, len); + if (len != 25) { + dump(buffer, len); + ABORT("bad settings len"); + } + printf(" addr: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17\n"); + printf("settings data: "); + if (!settings_init) { + dump(&buffer[1], 24); + memcpy(sq_settings, &buffer[1], 24); + printf("saved init settings\n"); + settings_init = true; + } else { + dump_with_color(&buffer[1], sq_settings, 24); + } } break; case 0xfd: @@ -374,24 +400,24 @@ FT_STATUS FT_Write(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD len, LPDWORD writt ABORT("wrong 0xfd command"); } if (sq_state == BOOT_INIT) { - printf("boot echo\n"); + /* printf("boot echo\n"); */ init_read_state("\x01\x01\x01\x01", 4); - dump(read_state.data, read_state.len); + /* dump(read_state.data, read_state.len); */ } else { - printf("app echo\n"); + /* printf("app echo\n"); */ init_read_state("\x22\x22\x22\x22", 4); - dump(read_state.data, read_state.len); + /* dump(read_state.data, read_state.len); */ } break; case 0xf4: - TRACE("got trigger settings"); - dump(buffer, len); + /* TRACE("got trigger settings"); */ + /* dump(buffer, len); */ for (size_t i = 1; i < len; i += 4) { unsigned char cfg0 = buffer[i+0]; unsigned char cfg1 = buffer[i+1]; unsigned char cfg2 = buffer[i+2]; unsigned char cfg3 = buffer[i+3]; - printf("SLOT: "); + printf("TRIG SLOT: "); print_bin(cfg0); printf(" "); print_bin(cfg1); @@ -405,21 +431,25 @@ FT_STATUS FT_Write(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD len, LPDWORD writt case 0xf0: switch (buffer[1]) { case 0x00: - printf("state -> ready\n"); + /* printf("state -> ready\n"); */ + if (sq_state == CAP_READING) { + printf("========= end capture =========\n"); + } sq_state = CAP_READY; break; case 0x01: - printf("wait for trigger command\n"); + /* printf("wait for trigger command\n"); */ init_read_state("\x11\x00\x00\xdd", 4); break; case 0x06: - printf("start read"); + /* printf("start read"); */ + sq_state = CAP_READING; read_state.len = 16384; read_state.pos = 0; for (size_t i = 0; i < 16384; i++) { read_state.data[i] = (i & 0x1) ? 0xf0 : 0xa5; } - printf("sending to app"); + /* printf("sending to app"); */ break; default: printf("unknown capture cmd code\n");