commit 0442d7e62531b0f6e01ff818631f7242fab7c2a8
parent b60fa9d6dd0a414f45d6fad81775b4161c22ceef
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date:   Fri, 26 Mar 2021 21:01:51 +0000
Added time device and example.
Diffstat:
2 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/projects/examples/dev.time.usm b/projects/examples/dev.time.usm
@@ -0,0 +1,20 @@
+;current { second 1 }
+
+( devices )
+
+|0100 ;Console { pad 8 char 1 byte 1 short 2 }
+|0190 ;Time { year 2 month 1 day 1 hour 1 minute 1 second 1 dow 1 doy 2 isdst 1 get 1 }
+|01F0 .RESET .FRAME .ERROR ( vectors )
+|01F8 [ 13fd 1ef3 1bf2 ] ( palette )
+
+( program )
+
+|0200 @RESET BRK
+
+@FRAME
+	#00 =Time.get
+	~Time.second ~current.second NEQ #01 JNZ BRK
+	~Time.second DUP =current.second =Console.byte
+
+@ERROR BRK
+
diff --git a/src/emulator.c b/src/emulator.c
@@ -1,5 +1,6 @@
 #include <SDL2/SDL.h>
 #include <stdio.h>
+#include <time.h>
 
 /*
 Copyright (c) 2021 Devine Lu Linvega
@@ -418,6 +419,26 @@ midi_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
 }
 
 Uint8
+time_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
+{
+	Uint8 *m = u->ram.dat;
+	time_t seconds = time(NULL);
+	struct tm *t = localtime(&seconds);
+	m[ptr +  0] = (t->tm_year & 0xff00) >> 8;
+	m[ptr +  1] = t->tm_year & 0xff;
+	m[ptr +  2] = t->tm_mon;
+	m[ptr +  3] = t->tm_mday;
+	m[ptr +  4] = t->tm_hour;
+	m[ptr +  5] = t->tm_min;
+	m[ptr +  6] = t->tm_sec;
+	m[ptr +  7] = t->tm_wday;
+	m[ptr +  8] = (t->tm_yday & 0x100) >> 8;
+	m[ptr +  9] = t->tm_yday && 0xff;
+	m[ptr + 10] = t->tm_isdst;
+	return b1;
+}
+
+Uint8
 system_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
 {
 	Uint8 *m = u->ram.dat;
@@ -496,7 +517,7 @@ main(int argc, char **argv)
 	portuxn(&u, "file", file_poke);
 	portuxn(&u, "audio", audio_poke);
 	portuxn(&u, "midi", ppnil);
-	portuxn(&u, "---", ppnil);
+	portuxn(&u, "time", time_poke);
 	portuxn(&u, "---", ppnil);
 	portuxn(&u, "---", ppnil);
 	portuxn(&u, "---", ppnil);