From a5810d3d11af3e857b8457f38fd99a090298b9f5 Mon Sep 17 00:00:00 2001
From: Mats Engstrom <mats@bergus.se>
Date: Sat, 15 Apr 2023 12:28:04 +0200
Subject: [PATCH] Add support for macOS

---
 README.md                 |  2 ++
 ch32v003fun/ch32v003fun.h |  5 +++++
 minichlink/Makefile.macos | 19 +++++++++++++++++++
 minichlink/README.md      |  2 ++
 4 files changed, 28 insertions(+)
 create mode 100644 minichlink/Makefile.macos

diff --git a/README.md b/README.md
index 4b866fb..71d484e 100644
--- a/README.md
+++ b/README.md
@@ -48,6 +48,8 @@ On WSL or Debian based OSes `apt-get install build-essential libnewlib-dev gcc-r
 
 On Windows, download and install (to system) this copy of GCC10. https://gnutoolchains.com/risc-v/
 
+On macOS install the RISC-V toolchain with homebrew following the instructions at https://github.com/riscv-software-src/homebrew-riscv
+
 You can use the pre-compiled minichlink or 
 
 ## Running
diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h
index 628640f..fc9fa09 100644
--- a/ch32v003fun/ch32v003fun.h
+++ b/ch32v003fun/ch32v003fun.h
@@ -4822,8 +4822,13 @@ extern "C" {
 #define DELAY_US_TIME (SYSTEM_CORE_CLOCK / 8000000)
 #define DELAY_MS_TIME (SYSTEM_CORE_CLOCK / 8000)
 
+#if defined(__APPLE__) && defined(__MACH__)
+void handle_reset()            __attribute__((naked)) __attribute((section("__TEXT,.text.ha_re"))) __attribute__((used));
+void DefaultIRQHandler( void ) __attribute__((section("__TEXT,.text.ve_ha"))) __attribute__((naked)) __attribute__((used));
+#else
 void handle_reset()            __attribute__((naked)) __attribute((section(".text.handle_reset"))) __attribute__((used));
 void DefaultIRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute__((naked)) __attribute__((used));
+#endif
 
 void DelaySysTick( uint32_t n );
 
diff --git a/minichlink/Makefile.macos b/minichlink/Makefile.macos
new file mode 100644
index 0000000..d19f24d
--- /dev/null
+++ b/minichlink/Makefile.macos
@@ -0,0 +1,19 @@
+TOOLS:=minichlink
+
+all : $(TOOLS)
+
+CFLAGS:=-O0 -Wall -Wno-asm-operand-widths -Wno-deprecated-declarations -Wno-deprecated-non-prototype -D__MACOSX__
+LDFLAGS:=-lpthread -lusb-1.0 -framework CoreFoundation -framework IOKit
+
+INCLUDES=-I /opt/homebrew/Cellar/libusb/1.0.26/include/libusb-1.0
+LIBINCLUDES=-L /opt/homebrew/Cellar/libusb/1.0.26/lib
+
+minichlink : minichlink.c pgm-wch-linke.c pgm-esp32s2-ch32xx.c
+	gcc -o $@ $^ $(LDFLAGS) $(CFLAGS) $(INCLUDES) $(LIBINCLUDES)
+
+inspect_bootloader : minichlink
+	./minichlink -r test.bin launcher 0x780
+	riscv64-unknown-elf-objdump -S -D test.bin -b binary -m riscv:rv32 | less
+
+clean :
+	rm -rf $(TOOLS)
diff --git a/minichlink/README.md b/minichlink/README.md
index 6b8d8c6..85b01b0 100644
--- a/minichlink/README.md
+++ b/minichlink/README.md
@@ -8,6 +8,8 @@ On Windows, if you need to you can install the WinUSB driver over the WCH interf
 
 The exe here is about 12kB and contains everything except for the libusb driver.  In Linux you need `libusb-1.0-dev`.
 
+On macOS compile using `make -f Makefile.macos`, you need libusb installed which can be done with homebrew `brew install libusb`.
+
 ## Usage
 
 ```
-- 
GitLab