all repos — xyw @ 846f9715bd78b9352ebdc1a4bf6f5589e28ea513

A minimal virtual machine and assembler for terminals.

devices/clock.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
#include <time.h>
#include "../xyw.h"

/* clock device */
#define CLOCK_YEAR 0x0
#define CLOCK_MONTH 0x2
#define CLOCK_MONTHDAY 0x3
#define CLOCK_WEEKDAY 0x4
#define CLOCK_CLOCK_HOUR 0x5
#define CLOCK_MINUTE 0x6
#define CLOCK_SECOND 0x7
#define CLOCK_TIMER 0x8
#define CLOCK_ON_TIMER_ELAPSED 0xA

xyw_byte clock_input(xyw_byte *data, xyw_byte addr, xyw_byte *error)
{
    (void)error;
    (void)data;
    time_t now = time(NULL);
    struct tm *lt = localtime(&now);
    if (!lt)
        return 0;

    switch (addr)
    {
    case CLOCK_YEAR:
        return (xyw_byte)((lt->tm_year + 1900) % 100);
    case CLOCK_MONTH:
        // 1-12
        return (xyw_byte)(lt->tm_mon + 1);
    case CLOCK_MONTHDAY:
        // 1-31
        return (xyw_byte)lt->tm_mday;
    case CLOCK_WEEKDAY:
        // 0=Sunday ... 6=Saturday
        return (xyw_byte)lt->tm_wday;
    case CLOCK_CLOCK_HOUR:
        // 0-23
        return (xyw_byte)lt->tm_hour;
    case CLOCK_MINUTE:
        return (xyw_byte)lt->tm_min;
    case CLOCK_SECOND:
        return (xyw_byte)lt->tm_sec;
    default:
        return 0;
    }
}