--- drivers/tilib_api.c.orig	2024-06-09 00:24:35
+++ drivers/tilib_api.c	2024-06-09 00:43:06
@@ -16,8 +16,10 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <limits.h>
 #include <stddef.h>
 #include <string.h>
+#include <stdio.h>
 #include "util/output.h"
 #include "tilib_api.h"
 #include "dynload.h"
@@ -28,6 +30,9 @@
 #if defined(__Windows__) || defined(__CYGWIN__)
 static const char tilib_filename[] = "MSP430.DLL";
 #define TIDLL __stdcall
+#elif defined(__APPLE__)
+static const char tilib_filename[] = "libmsp430.dylib";
+#define TIDLL
 #else
 static const char tilib_filename[] = "libmsp430.so";
 #define TIDLL
@@ -757,12 +762,23 @@
 {
 	int ret;
 
+#if defined(__MACPORTS__)
+  char libpath[PATH_MAX];
+  snprintf(libpath, sizeof(libpath), "%s/%s", MACPORTS_LIB_DIR, tilib_filename);
+  lib_handle = dynload_open(libpath);
+	if (!lib_handle) {
+		printc_err("tilib_api: can't find %s: %s\n",
+			   tilib_filename, dynload_error());
+		return -1;
+	}
+#else
 	lib_handle = dynload_open(tilib_filename);
 	if (!lib_handle) {
 		printc_err("tilib_api: can't find %s: %s\n",
 			   tilib_filename, dynload_error());
 		return -1;
 	}
+#endif
 
 	if (dynload_sym(lib_handle, "MSP430_HIL_MEMAP")) {
 		printc_dbg("Using new (SLAC460L+) API\n");
