git: 9front

Download patch

ref: a3f7629993aa6e04dda726d7ea8b329b43b0cfce
parent: 83550d836c6a99d1193da81efb18a6f00a464b57
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Feb 18 12:51:10 EST 2026

libaml: implement Timer opcode, make amldelay argument 64-bit

User should provide a amltime() function,
returning tick in 100ns period (10MHz).
Default implementation always returns 0.

As amldelay() is used for sleep, which
uses millisecond resolution, the delay
value can be quite large. Make it uvlong.

--- a/sys/man/2/aml
+++ b/sys/man/2/aml
@@ -288,6 +288,11 @@
 is called by the interpreter with the number of microseconds
 to sleep.
 .TP
+.BI amltime()
+.I Amltime
+is called by the interpreter to get the monotonic increasing time
+tick in 100ns units (10MHz).
+.TP
 \f5amlalloc(\fIn\f5) \f5amlfree(\fIp\f5)
 .I Amlalloc
 and
--- a/sys/src/9/pc/archacpi.c
+++ b/sys/src/9/pc/archacpi.c
@@ -1038,7 +1038,20 @@
 }
 
 void
-amldelay(int us)
+amldelay(uvlong us)
 {
+	while(us > 1000){
+		us -= 1000;
+		microdelay(1000);
+	}
 	microdelay(us);
+}
+
+uvlong
+amltime(void)
+{
+	uvlong hz;
+
+	/* can't use tod yet, use fastticks() */
+	return (fastticks(&hz) * 10000000ULL) / hz;
 }
--- a/sys/src/cmd/aux/acpi.c
+++ b/sys/src/cmd/aux/acpi.c
@@ -648,3 +648,15 @@
 amlunmapio(Amlio *)
 {
 }
+
+void
+amldelay(uvlong us)
+{
+	sleep(us/1000);
+}
+
+uvlong
+amltime(void)
+{
+	return nsec()/100;
+}
--- a/sys/src/libaml/aml.c
+++ b/sys/src/libaml/aml.c
@@ -151,7 +151,7 @@
 	Ocfld, Ocfld0, Ocfld1, Ocfld2, Ocfld4, Ocfld8,
 	Oif, Oelse, Owhile, Obreak, Oret, Ocall, 
 	Ostore, Oderef, Ootype, Osize, Oref, Ocref, Ocat, Ocatr, Omid,
-	Oacq, Osignal, Orel, Ostall, Osleep, Oload, Ounload,
+	Oacq, Osignal, Orel, Ostall, Osleep, Otimer, Oload, Ounload,
 	Otodec, Otohex, Otoint, Otostr, Onotify,
 };
 
@@ -2060,6 +2060,12 @@
 }
 
 static void*
+evaltimer(void)
+{
+	return mki(amltime());
+}
+
+static void*
 evalconv(void)
 {
 	void *r, *a;
@@ -2218,6 +2224,7 @@
 	[Orel]		"Release",		"@",		evalnop,
 	[Ostall]	"Stall",		"i",		evalstall,
 	[Osleep]	"Sleep",		"i",		evalsleep,
+	[Otimer]	"Timer",		"",		evaltimer,
 	[Oload] 	"Load", 		"*@}", 		evalload,
 	[Ounload]	"Unload",		"@",		evalnop,
 
@@ -2271,7 +2278,7 @@
 /* 18 */	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,
 /* 20 */	Oload,	Ostall,	Osleep,	Oacq,	Osignal,Obad,	Obad,	Orel,
 /* 28 */	Obad,	Obad,	Ounload,Obad,	Obad,	Obad,	Obad,	Obad,
-/* 30 */	Obad,	Odebug,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,
+/* 30 */	Obad,	Odebug,	Obad,	Otimer,	Obad,	Obad,	Obad,	Obad,
 /* 38 */	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,
 /* 40 */	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,
 /* 48 */	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,	Obad,
--- a/sys/src/libaml/amldelay.c
+++ b/sys/src/libaml/amldelay.c
@@ -3,6 +3,6 @@
 #include <aml.h>
 
 void
-amldelay(int)
+amldelay(uvlong)
 {
 }
--- /dev/null
+++ b/sys/src/libaml/amltime.c
@@ -1,0 +1,9 @@
+#include <u.h>
+#include <libc.h>
+#include <aml.h>
+
+uvlong
+amltime(void)
+{
+	return 0;
+}
--- a/sys/src/libaml/mkfile
+++ b/sys/src/libaml/mkfile
@@ -7,6 +7,7 @@
 	amlunmapio.$O\
 	amlalloc.$O\
 	amldelay.$O\
+	amltime.$O\
 
 HFILES=/sys/include/aml.h
 
--