ref: 3d986fe3addb1fe33acba63693c00766dbbf3d33
parent: aba7c4163ebf0709d0181ee6901d51d87b44bff5
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Jan 5 14:26:17 EST 2025
combine OS-specific things into sys_SYSTEM.c
--- a/main_plan9.c
+++ /dev/null
@@ -1,13 +1,0 @@
-#include "flisp.h"
-
-extern uchar bootcode[];
-extern ulong bootlen;
-
-void
-main(int argc, char **argv)
-{
- argv0 = argv[0];
- setfcr(FPPDBL|FPRNR|FPOVFL);
- tmfmtinstall();
- flmain(bootcode, bootlen, argc, argv);
-}
--- a/main_posix.c
+++ /dev/null
@@ -1,12 +1,0 @@
-#include "flisp.h"
-
-static const uint8_t boot[] = {
-#include "flisp.boot.h"
-};
-
-int
-main(int argc, char **argv)
-{
- setlocale(LC_NUMERIC, "C");
- flmain(boot, sizeof(boot), argc, argv);
-}
--- a/meson.build
+++ b/meson.build
@@ -42,7 +42,6 @@
'htable.c',
'ios.c',
'iostream.c',
- 'main_posix.c',
'opcodes.c',
'operators.c',
'print.c',
@@ -50,9 +49,9 @@
'random.c',
'read.c',
'string.c',
+ 'sys_posix.c',
'table.c',
'terminal_posix.c',
- 'time_posix.c',
'types.c',
'utf8.c',
]
--- a/mkfile
+++ b/mkfile
@@ -33,7 +33,6 @@
htable.$O\
ios.$O\
iostream.$O\
- main_plan9.$O\
opcodes.$O\
operators.$O\
print.$O\
@@ -42,8 +41,8 @@
read.$O\
sixel_disabled.$O\
string.$O\
+ sys_plan9.$O\
table.$O\
- time_plan9.$O\
types.$O\
utf8.$O\
--- /dev/null
+++ b/sys_plan9.c
@@ -1,0 +1,80 @@
+#include "flisp.h"
+#include "timefuncs.h"
+#include <tos.h>
+
+double
+sec_realtime(void)
+{
+ vlong t = nsec();
+ vlong ns = t % 1000000000LL;
+ vlong s = (t - ns) / 1000000000LL;
+ return (double)s + (double)ns/1.0e9;
+}
+
+/*
+ * nsec() is wallclock and can be adjusted by timesync
+ * so need to use cycles() instead, but fall back to
+ * nsec() in case we can't
+ */
+uint64_t
+nanosec_monotonic(void)
+{
+ static uint64_t fasthz, xstart;
+ uint64_t x, div;
+
+ if(fasthz == ~0ULL)
+ return nsec() - xstart;
+
+ if(fasthz == 0){
+ if(_tos->cyclefreq){
+ fasthz = _tos->cyclefreq;
+ cycles(&xstart);
+ } else {
+ fasthz = ~0ULL;
+ xstart = nsec();
+ }
+ return 0;
+ }
+ cycles(&x);
+ x -= xstart;
+
+ /* this is ugly */
+ for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+
+ return x / (fasthz / div);
+}
+
+void
+timestring(double s, char *buf, int sz)
+{
+ Tm tm;
+ snprint(buf, sz, "%τ", tmfmt(tmtime(&tm, s, tzload("local")), nil));
+}
+
+double
+parsetime(const char *s)
+{
+ Tm tm;
+ if(tmparse(&tm, "?WWW, ?MM ?DD hh:mm:ss ?Z YYYY", s, tzload("local"), nil) == nil)
+ return -1;
+ return tmnorm(&tm);
+}
+
+void
+sleep_ms(int ms)
+{
+ if(ms != 0)
+ sleep(ms);
+}
+
+extern uchar bootcode[];
+extern ulong bootlen;
+
+void
+main(int argc, char **argv)
+{
+ argv0 = argv[0];
+ setfcr(FPPDBL|FPRNR|FPOVFL);
+ tmfmtinstall();
+ flmain(bootcode, bootlen, argc, argv);
+}
--- /dev/null
+++ b/sys_posix.c
@@ -1,0 +1,81 @@
+#include "flisp.h"
+#include "timefuncs.h"
+
+double
+sec_realtime(void)
+{
+ struct timespec now;
+ if(clock_gettime(CLOCK_REALTIME, &now) != 0)
+ return 0;
+ return (double)now.tv_sec + (double)now.tv_nsec/1.0e9;
+}
+
+uint64_t
+nanosec_monotonic(void)
+{
+ static int64_t z;
+ struct timespec now;
+
+ if(clock_gettime(CLOCK_MONOTONIC, &now) != 0)
+ return 0;
+ if(z == 0){
+ z = now.tv_sec*1000000000LL + now.tv_nsec;
+ return 0;
+ }
+ return now.tv_sec*1000000000LL + now.tv_nsec - z;
+}
+
+void
+timestring(double s, char *buf, int sz)
+{
+ time_t tme = (time_t)s;
+ char *fmt = "%c"; /* needed to suppress GCC warning */
+ struct tm tm;
+
+ localtime_r(&tme, &tm);
+ strftime(buf, sz, fmt, &tm);
+}
+
+double
+parsetime(const char *s)
+{
+ char *fmt = "%c"; /* needed to suppress GCC warning */
+ char *res;
+ time_t t;
+ struct tm tm;
+
+ res = strptime(s, fmt, &tm);
+ if(res != nil){
+ /* Not set by strptime(); tells mktime() to determine
+ * whether daylight saving time is in effect
+ */
+ tm.tm_isdst = -1;
+ t = mktime(&tm);
+ if(t == (time_t)-1)
+ return -1;
+ return (double)t;
+ }
+ return -1;
+}
+
+void
+sleep_ms(int ms)
+{
+ if(ms != 0){
+ struct timeval timeout;
+ timeout.tv_sec = ms/1000;
+ timeout.tv_usec = (ms % 1000) * 1000;
+ select(0, nil, nil, nil, &timeout);
+ }
+}
+
+static const uint8_t boot[] = {
+#include "flisp.boot.h"
+};
+
+int
+main(int argc, char **argv)
+{
+ setlocale(LC_NUMERIC, "C");
+ flmain(boot, sizeof(boot), argc, argv);
+}
--- a/time_plan9.c
+++ /dev/null
@@ -1,68 +1,0 @@
-#include "flisp.h"
-#include "timefuncs.h"
-#include <tos.h>
-
-double
-sec_realtime(void)
-{
- vlong t = nsec();
- vlong ns = t % 1000000000LL;
- vlong s = (t - ns) / 1000000000LL;
- return (double)s + (double)ns/1.0e9;
-}
-
-/*
- * nsec() is wallclock and can be adjusted by timesync
- * so need to use cycles() instead, but fall back to
- * nsec() in case we can't
- */
-uint64_t
-nanosec_monotonic(void)
-{
- static uint64_t fasthz, xstart;
- uint64_t x, div;
-
- if(fasthz == ~0ULL)
- return nsec() - xstart;
-
- if(fasthz == 0){
- if(_tos->cyclefreq){
- fasthz = _tos->cyclefreq;
- cycles(&xstart);
- } else {
- fasthz = ~0ULL;
- xstart = nsec();
- }
- return 0;
- }
- cycles(&x);
- x -= xstart;
-
- /* this is ugly */
- for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
-
- return x / (fasthz / div);
-}
-
-void
-timestring(double s, char *buf, int sz)
-{
- Tm tm;
- snprint(buf, sz, "%τ", tmfmt(tmtime(&tm, s, tzload("local")), nil));
-}
-
-double
-parsetime(const char *s)
-{
- Tm tm;
- if(tmparse(&tm, "?WWW, ?MM ?DD hh:mm:ss ?Z YYYY", s, tzload("local"), nil) == nil)
- return -1;
- return tmnorm(&tm);
-}
-
-void
-sleep_ms(int ms)
-{
- if(ms != 0)
- sleep(ms);
-}
--- a/time_posix.c
+++ /dev/null
@@ -1,70 +1,0 @@
-#include "flisp.h"
-#include "timefuncs.h"
-
-double
-sec_realtime(void)
-{
- struct timespec now;
- if(clock_gettime(CLOCK_REALTIME, &now) != 0)
- return 0;
- return (double)now.tv_sec + (double)now.tv_nsec/1.0e9;
-}
-
-uint64_t
-nanosec_monotonic(void)
-{
- static int64_t z;
- struct timespec now;
-
- if(clock_gettime(CLOCK_MONOTONIC, &now) != 0)
- return 0;
- if(z == 0){
- z = now.tv_sec*1000000000LL + now.tv_nsec;
- return 0;
- }
- return now.tv_sec*1000000000LL + now.tv_nsec - z;
-}
-
-void
-timestring(double s, char *buf, int sz)
-{
- time_t tme = (time_t)s;
- char *fmt = "%c"; /* needed to suppress GCC warning */
- struct tm tm;
-
- localtime_r(&tme, &tm);
- strftime(buf, sz, fmt, &tm);
-}
-
-double
-parsetime(const char *s)
-{
- char *fmt = "%c"; /* needed to suppress GCC warning */
- char *res;
- time_t t;
- struct tm tm;
-
- res = strptime(s, fmt, &tm);
- if(res != nil){
- /* Not set by strptime(); tells mktime() to determine
- * whether daylight saving time is in effect
- */
- tm.tm_isdst = -1;
- t = mktime(&tm);
- if(t == (time_t)-1)
- return -1;
- return (double)t;
- }
- return -1;
-}
-
-void
-sleep_ms(int ms)
-{
- if(ms != 0){
- struct timeval timeout;
- timeout.tv_sec = ms/1000;
- timeout.tv_usec = (ms % 1000) * 1000;
- select(0, nil, nil, nil, &timeout);
- }
-}