summaryrefslogtreecommitdiff
path: root/src/drv/ps1/time
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-07-07 13:22:53 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-07-25 14:16:41 +0200
commit14f60e4fd65c42f126eaee7e09cb4251c167c6ed (patch)
tree313b5e16d7d99cf1518c953e2efe5e5fc920dfbf /src/drv/ps1/time
parent48a61c16eaa6dcfc75d00dba302537ce1492db98 (diff)
downloadwnix-tty.tar.gz
wiptty
Diffstat (limited to 'src/drv/ps1/time')
-rw-r--r--src/drv/ps1/time/CMakeLists.txt22
-rw-r--r--src/drv/ps1/time/include/drv/ps1/time.h24
-rw-r--r--src/drv/ps1/time/private_include/drv/ps1/time/types.h26
-rw-r--r--src/drv/ps1/time/src/CMakeLists.txt23
-rw-r--r--src/drv/ps1/time/src/getres.c34
-rw-r--r--src/drv/ps1/time/src/gettime.c37
-rw-r--r--src/drv/ps1/time/src/globals.c22
-rw-r--r--src/drv/ps1/time/src/settime.c37
-rw-r--r--src/drv/ps1/time/src/tick.c42
9 files changed, 267 insertions, 0 deletions
diff --git a/src/drv/ps1/time/CMakeLists.txt b/src/drv/ps1/time/CMakeLists.txt
new file mode 100644
index 0000000..2746810
--- /dev/null
+++ b/src/drv/ps1/time/CMakeLists.txt
@@ -0,0 +1,22 @@
+# wanix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+add_library(drv_ps1_time)
+add_subdirectory(src)
+target_include_directories(drv_ps1_time PUBLIC include PRIVATE private_include)
+target_link_libraries(drv_ps1_time
+ PUBLIC c
+ PRIVATE drv drv_ps1_bios drv_ps1_interrupt drv_ps1_rcnt)
diff --git a/src/drv/ps1/time/include/drv/ps1/time.h b/src/drv/ps1/time/include/drv/ps1/time.h
new file mode 100644
index 0000000..d08ff39
--- /dev/null
+++ b/src/drv/ps1/time/include/drv/ps1/time.h
@@ -0,0 +1,24 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef DRV_PS1_TIME_H
+#define DRV_PS1_TIME_H
+
+int drv_ps1_time_tick(void);
+
+#endif
diff --git a/src/drv/ps1/time/private_include/drv/ps1/time/types.h b/src/drv/ps1/time/private_include/drv/ps1/time/types.h
new file mode 100644
index 0000000..ad420c6
--- /dev/null
+++ b/src/drv/ps1/time/private_include/drv/ps1/time/types.h
@@ -0,0 +1,26 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef DRV_PS1_TIME_TYPES_H
+#define DRV_PS1_TIME_TYPES_H
+
+#include <time.h>
+
+extern volatile struct timespec drv_ps1_time;
+
+#endif
diff --git a/src/drv/ps1/time/src/CMakeLists.txt b/src/drv/ps1/time/src/CMakeLists.txt
new file mode 100644
index 0000000..0ab540e
--- /dev/null
+++ b/src/drv/ps1/time/src/CMakeLists.txt
@@ -0,0 +1,23 @@
+# wanix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+target_sources(drv_ps1_time PRIVATE
+ gettime.c
+ getres.c
+ globals.c
+ settime.c
+ tick.c
+)
diff --git a/src/drv/ps1/time/src/getres.c b/src/drv/ps1/time/src/getres.c
new file mode 100644
index 0000000..8dad204
--- /dev/null
+++ b/src/drv/ps1/time/src/getres.c
@@ -0,0 +1,34 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <drv/time.h>
+#include <errno.h>
+#include <time.h>
+
+int drv_time_getres(const clockid_t id, struct timespec *const ts)
+{
+ if (id != CLOCK_REALTIME)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+ else if (ts)
+ *ts = (const struct timespec){.tv_nsec = 1000000};
+
+ return 0;
+}
diff --git a/src/drv/ps1/time/src/gettime.c b/src/drv/ps1/time/src/gettime.c
new file mode 100644
index 0000000..f01030f
--- /dev/null
+++ b/src/drv/ps1/time/src/gettime.c
@@ -0,0 +1,37 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <drv/time.h>
+#include <drv/ps1/time/types.h>
+#include <drv/ps1/bios.h>
+#include <errno.h>
+#include <time.h>
+
+int drv_time_gettime(const clockid_t id, struct timespec *const ts)
+{
+ if (id != CLOCK_REALTIME)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ EnterCriticalSection();
+ *ts = drv_ps1_time;
+ ExitCriticalSection();
+ return 0;
+}
diff --git a/src/drv/ps1/time/src/globals.c b/src/drv/ps1/time/src/globals.c
new file mode 100644
index 0000000..cb2a825
--- /dev/null
+++ b/src/drv/ps1/time/src/globals.c
@@ -0,0 +1,22 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <drv/ps1/time/types.h>
+#include <time.h>
+
+volatile struct timespec drv_ps1_time;
diff --git a/src/drv/ps1/time/src/settime.c b/src/drv/ps1/time/src/settime.c
new file mode 100644
index 0000000..25de76f
--- /dev/null
+++ b/src/drv/ps1/time/src/settime.c
@@ -0,0 +1,37 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <drv/time.h>
+#include <drv/ps1/time/types.h>
+#include <drv/ps1/bios.h>
+#include <errno.h>
+#include <time.h>
+
+int drv_time_settime(const clockid_t id, const struct timespec *const ts)
+{
+ if (id != CLOCK_REALTIME)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ EnterCriticalSection();
+ drv_ps1_time = *ts;
+ ExitCriticalSection();
+ return 0;
+}
diff --git a/src/drv/ps1/time/src/tick.c b/src/drv/ps1/time/src/tick.c
new file mode 100644
index 0000000..b80174b
--- /dev/null
+++ b/src/drv/ps1/time/src/tick.c
@@ -0,0 +1,42 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <drv/ps1/time.h>
+#include <drv/ps1/time/types.h>
+#include <drv/ps1/interrupt.h>
+#include <time.h>
+
+int drv_ps1_time_tick(void)
+{
+ int ret = 0;
+ struct timespec ts;
+ volatile struct timespec *const t = &drv_ps1_time;
+
+ if (clock_getres(CLOCK_REALTIME, &ts))
+ ret = -1;
+ else if (t->tv_nsec >= 1000000000 - ts.tv_nsec)
+ {
+ t->tv_sec++;
+ t->tv_nsec = 0;
+ }
+ else
+ t->tv_nsec += ts.tv_nsec;
+
+ I_STAT->bits.tmr2 = 1;
+ return ret;
+}