diff --git a/projects/examples/dev.time.usm b/projects/examples/dev.time.usm new file mode 100644 index 0000000..100597b --- /dev/null +++ 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 index a83dc9b..6d9e78a 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -1,5 +1,6 @@ #include #include +#include /* Copyright (c) 2021 Devine Lu Linvega @@ -417,6 +418,26 @@ midi_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) return 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) { @@ -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);