revamping datetime draft

This commit is contained in:
Luxferre
2022-07-31 10:25:53 +03:00
parent 626b31bb4b
commit f2bad1455f
3 changed files with 56 additions and 14 deletions
+38 -9
View File
@@ -172,19 +172,48 @@ function ESOPExtensions() {
//syscalls
//thanks to Toastrackenigma for the idea
function isDST(d, y) {
var jan = new Date(y, 0, 1).getTimezoneOffset(),
jul = new Date(y, 6, 1).getTimezoneOffset()
return Math.max(jan, jul) !== d.getTimezoneOffset()
}
//thanks to user2501097 for the idea
function daysIntoYear(y, m, d) {
return (Date.UTC(y, m, d) - Date.UTC(y, 0, 0)) / 86400000
}
function fillDTbuf(addr) {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth() + 1,
day = now.getDate(),
hour = now.getHours(),
minute = now.getMinutes(),
second = now.getSeconds(),
dotw = now.getDay(),
doty = daysIntoYear(year, month - 1, day),
dstflag = 0|isDST(now, year)
vm.setram(addr, year >> 8)
vm.setram(addr+1, year&255)
vm.setram(addr+2, month)
vm.setram(addr+3, day)
vm.setram(addr+4, hour)
vm.setram(addr+5, minute)
vm.setram(addr+6, second)
vm.setram(addr+7, dotw)
vm.setram(addr+8, doty >> 8)
vm.setram(addr+9, doty&255)
vm.setram(addr+10, dstflag)
}
function runSyscall(vm, buf) {
var call = buf[0]
if(call === 0x00) //simulate writing a byte to the debug port
console.log('[DBG] ' + ('00' + buf[1].toString(16)).slice(-2))
else if(call === 0x06) { //read datetime info into 5 bytes starting from addr
//not very optimal but a future-proof approach for now
var tm = ('00' + (0|(Date.now()/1000)).toString(16)).slice(-10).match(/.{2}/g).map(function(x){return parseInt(x, 16)}),
targetaddr = (buf[1]<<8)|buf[2]
vm.setram(targetaddr, tm[0])
vm.setram(targetaddr+1, tm[1])
vm.setram(targetaddr+2, tm[2])
vm.setram(targetaddr+3, tm[3])
vm.setram(targetaddr+4, tm[4])
else if(call === 0x06) { //read datetime info into 10 bytes starting from addr
fillDTbuf((buf[1]<<8)|buf[2])
}
else if(call === 0x1f) //halt call, required for all implementations
vm.active = false
+1 -3
View File
@@ -4,15 +4,13 @@ function main() {
var devCfg = {
audio: null,
canvas: document.getElementById('C') //JS-specific implementation canvas DOM reference
}
}, vm = UxnCore(), ext = ESOPExtensions()
document.getElementById('apprun').addEventListener('click', function() {
var fileObj = document.getElementById('appselect').files[0]
if(fileObj.name.endsWith('.eso')) {
var rdr = new FileReader()
rdr.onload = function() {
var vm = UxnCore()
var ext = ESOPExtensions()
devCfg.audio = new (window.AudioContext || window.webkitAudioContext)
vm.boot()
ext.setup(vm, devCfg)