From 519e8b66f774e54f4fc0afab80c490c10cce7a10 Mon Sep 17 00:00:00 2001 From: cnlohr <lohr85@gmail.com> Date: Sat, 17 Jun 2023 21:16:26 -0400 Subject: [PATCH] Move .xw to header + fix compilation of b003fun --- ch32v003fun/ch32v003fun.h | 62 +++++++++++++++++++++++++++++++++++++++ minichlink/pgm-b003fun.c | 7 +++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h index 1634bcb..ba320ec 100644 --- a/ch32v003fun/ch32v003fun.h +++ b/ch32v003fun/ch32v003fun.h @@ -5082,6 +5082,68 @@ int _write(int fd, const char *buf, int size); #endif +// xw_ext.inc, thanks to @macyler, @jnk0le, @duk for this reverse engineering. + +/* +Encoder for some of the proprietary 'XW' RISC-V instructions present on the QingKe RV32 processor. +Examples: + XW_C_LBU(a3, a1, 27); // c.xw.lbu a3, 27(a1) + XW_C_SB(a0, s0, 13); // c.xw.sb a0, 13(s0) + + XW_C_LHU(a5, a5, 38); // c.xw.lhu a5, 38(a5) + XW_C_SH(a2, s1, 14); // c.xw.sh a2, 14(s1) +*/ + +// Let us do some compile-time error checking. +#define ASM_ASSERT(COND) .if (!(COND)); .err; .endif + +// Integer encodings of the possible compressed registers. +#define C_s0 0 +#define C_s1 1 +#define C_a0 2 +#define C_a1 3 +#define C_a2 4 +#define C_a3 5 +#define C_a4 6 +#define C_a5 7 + +// register to encoding +#define REG2I(X) (C_ ## X) + +// XW opcodes +#define XW_OP_LBUSP 0b1000000000000000 +#define XW_OP_STSP 0b1000000001000000 + +#define XW_OP_LHUSP 0b1000000000100000 +#define XW_OP_SHSP 0b1000000001100000 + +#define XW_OP_LBU 0b0010000000000000 +#define XW_OP_SB 0b1010000000000000 + +#define XW_OP_LHU 0b0010000000000010 +#define XW_OP_SH 0b1010000000000010 + +// The two different XW encodings supported at the moment. +#define XW_ENCODE1(OP, R1, R2, IMM) ASM_ASSERT((IMM) >= 0 && (IMM) < 32); .2byte ((OP) | (REG2I(R1) << 2) | (REG2I(R2) << 7) | \ + (((IMM) & 0b1) << 12) | (((IMM) & 0b110) << (5 - 1)) | (((IMM) & 0b11000) << (10 - 3))) + +#define XW_ENCODE2(OP, R1, R2, IMM) ASM_ASSERT((IMM) >= 0 && (IMM) < 32); .2byte ((OP) | (REG2I(R1) << 2) | (REG2I(R2) << 7) | \ + (((IMM) & 0b11) << 5) | (((IMM) & 0b11100) << (10 - 2)) + +// Compressed load byte, zero-extend result +#define XW_C_LBU(RD, RS, IMM) XW_ENCODE1(XW_OP_LBU, RD, RS, IMM) + +// Compressed store byte +#define XW_C_SB(RS1, RS2, IMM) XW_ENCODE1(XW_OP_SB, RS1, RS2, IMM) + +// Compressed load half, zero-extend result +#define XW_C_LHU(RD, RS, IMM) ASM_ASSERT(((IMM) & 1) == 0); XW_ENCODE2(XW_OP_LHU, RD, RS, ((IMM) >> 1))) + +// Compressed store half +#define XW_C_SH(RS1, RS2, IMM) ASM_ASSERT(((IMM) & 1) == 0); XW_ENCODE2(XW_OP_SH, RS1, RS2, ((IMM) >> 1))) + + + #ifdef __cplusplus }; #endif diff --git a/minichlink/pgm-b003fun.c b/minichlink/pgm-b003fun.c index 3854293..5e37aea 100644 --- a/minichlink/pgm-b003fun.c +++ b/minichlink/pgm-b003fun.c @@ -1,3 +1,4 @@ +#if 0 #include <stdint.h> #include "hidapi.h" #include "minichlink.h" @@ -103,7 +104,7 @@ static void CommitOp( struct B003FunProgrammerStruct * eps ) -void * TryInit_ESP32S2CHFUN() +void * TryInit_B003Fun() { #define VID 0x1209 #define PID 0xb003 @@ -144,4 +145,6 @@ void * TryInit_ESP32S2CHFUN() return eps; } - +#else +void * TryInit_B003Fun() { return 0; } +#endif -- GitLab