all repos — xyw @ e3c0ff321521aa6c923e4f2827b27e080f4ba86a

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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
#include <time.h>
#include <stdlib.h>
#include <stdio.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_RANDOM 0x8
#define CLOCK_TIMER 0x9
#define CLOCK_ON_TIMER_ELAPSED 0x9
void clock_init(xyw_byte *data)
{
    (void)data;
    // Initialize random generator seed
    srand(time(NULL));
}

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;
    case CLOCK_RANDOM:
        return (xyw_byte)(rand() & 0xFF);
    default:
        return 0;
    }
}