Setting system clock using GR8NET NTP on OCM (SX-1 Mini+ or Zemmix Neo)

By bsittler

Expert (71)

bsittler's picture

25-09-2020, 23:31

Although the SX-1 Mini+ and Zemmix Neo are MSX 2+ in most respects, they lack battery-backed RTC. Unfortunately this also means the GR8NET RTC synchronization using NTP does not work on these machines. Does anyone know how to use NTP to set the system clock on One Chip MSX derivatives, or on other MSX machines lacking RTC?

I would like a synchronized clock so that time stamps on files are usable for later (PC-based) backup using rsync, and so that build tools (e.g. MAKE) can determine which input files have been modified more recently than the output files that are produced from those input files

As a work-around, here is a really ugly MSX-BASIC program I run from near the end of AUTOEXEC.BAT to set up the MSX system clock on these machines:

10 ON ERROR GOTO 540
20 SCREEN 0:WIDTH 40:KEY OFF
30 CLS
40 CALL NET_TSYNC
50 D$=""
60 DIM D$(10)
70 F=1
80 FOR I=0 TO 39
90 C$=CHR$(VPEEK(I))
100 D$=D$+C$
110 IF C$="," THEN C$=" "
120 IF C$=" " AND D$(F)<>"" THEN F=F+1:D$(F)="":ELSE D$(F)=D$(F)+C$
130 IF D$(F)=" " THEN D$(F)=""
140 NEXT I
150 IF D$(1) <> CHR$(253) THEN GOTO 470
160 M$=D$(2)
170 IF M$="Jan" THEN MM$="01"
180 IF M$="Feb" THEN MM$="02"
190 IF M$="Mar" THEN MM$="03"
200 IF M$="Apr" THEN MM$="04"
210 IF M$="May" THEN MM$="05"
220 IF M$="Jun" THEN MM$="06"
230 IF M$="Jul" THEN MM$="07"
240 IF M$="Aug" THEN MM$="08"
250 IF M$="Sep" THEN MM$="09"
260 IF M$="Oct" THEN MM$="10"
270 IF M$="Nov" THEN MM$="11"
280 IF M$="Dec" THEN MM$="12"
290 DD$=D$(3):IF LEN(DD$)=1 THEN DD$="0"+DD$
300 YY$=D$(4):IF LEN(YY$)=4 THEN YY$=MID$(YY$,3,2)
310 TM$=D$(5)
320 D1$="01/01/01":T1$="23:59:59"
330 SET DATE D1$:SET TIME T1$
340 T1 =TIME
350 T2 =TIME
360 GET DATE D2$
370 IF D1$=D2$ AND T1 <>0 AND T2 >=T1  AND T1 +60>T2  THEN GOTO 350
380 GET DATE D2$
390 DT$=YY$+"/"+MM$+"/"+DD$
400 IF D2$="01/02/01" THEN DT$=MM$+"/"+DD$+"/"+YY$
410 IF D2$="02/01/01" THEN DT$=DD$+"/"+MM$+"/"+YY$
420 SET TIME TM$:SET DATE DT$:SET TIME TM$:SET DATE DT$
430 PRINT
440 PRINTDT$" "TM$
450 GET DATE DT$:GET TIME TM$
460 PRINTDT$" "TM$
470 PRINT"Press space or button 1 in the next 3 seconds to avoid running %afterntp%"
480 T1=TIME
490 T2=TIME
500 IF STRIG(0)+STRIG(1)+STRIG(2)<>0 THEN GOTO 580
510 IF T2<T1 OR (T1+300)>T2 THEN GOTO 490
520 PRINT"Running %afterntp%"
530 CALL SYSTEM("%afterntp%")
540 RESUME 550
550 ON ERROR GOTO 0
560 PRINT"Unable to set time and date using NTP"
570 GOTO 470
580 PRINT"Not running %afterntp%"
590 CALL SYSTEM

While it works, I'm dissatisfied with it, in particular:

  1. The NTP-fetched time and date must be read and parsed from VRAM!
  2. The month names are matched textually, and it might break in other MSX machines/locales (I don't know?)
  3. The order of the fields in the MSX-BASIC date string is determined in a very hacky way — right now mm/dd/yy and dd/mm/yy are supported
  4. If the MSX-BASIC date string requires 12 hour time with AM/PM this program won't work at all
  5. At the end I wish to return to MSX-DOS and (unless a particular key was pressed) "continue" the AUTOEXEC.BAT, however right now that is also done in a very hacky way using an environment variable %afterntp%
  6. I don't know how to return to the video mode and color scheme in use prior to running the program. Is there some way to save and restore this?
  7. At the moment date and time are read and written separately, in close proximity, but there might be some cases where the end of a day is reached while reading the time or something, so there is some wasteful looping to attempt to avoid this. Is there a better way, perhaps one that allows reading both date and time at once in a synchronized way?

Are there better ways to solve any of these problems? Any pointers are appreciated, thanks!

NOTE: You must have time zone and NTP server set up in your GR8NET, and a working internet connection ready by the time this program runs. I am using a Wi-Fi to Ethernet bridge to connect mine (with absurdly high GR8NET DHCP retry count), and sometimes it doesn't complete WPA2 set-up + bridge DHCP + MSX DHCP in time to have working connectivity, so the program may fail to set the time in this scenario and will exit without doing anything useful

Login or register to post comments

By ducasp

Champion (368)

ducasp's picture

26-09-2020, 00:48

bsittler wrote:

Although the SX-1 Mini+ and Zemmix Neo are MSX 2+ in most respects, they lack battery-backed RTC. Unfortunately this also means the GR8NET RTC synchronization using NTP does not work on these machines. Does anyone know how to use NTP to set the system clock on One Chip MSX derivatives, or on other MSX machines lacking RTC?

I would like a synchronized clock so that time stamps on files are usable for later (PC-based) backup using rsync, and so that build tools (e.g. MAKE) can determine which input files have been modified more recently than the output files that are produced from those input files

As a work-around, here is a really ugly MSX-BASIC program I run from near the end of AUTOEXEC.BAT to set up the MSX system clock on these machines:

10 ON ERROR GOTO 540
20 SCREEN 0:WIDTH 40:KEY OFF
30 CLS
40 CALL NET_TSYNC
50 D$=""
60 DIM D$(10)
70 F=1
80 FOR I=0 TO 39
90 C$=CHR$(VPEEK(I))
100 D$=D$+C$
110 IF C$="," THEN C$=" "
120 IF C$=" " AND D$(F)<>"" THEN F=F+1:D$(F)="":ELSE D$(F)=D$(F)+C$
130 IF D$(F)=" " THEN D$(F)=""
140 NEXT I
150 IF D$(1) <> CHR$(253) THEN GOTO 470
160 M$=D$(2)
170 IF M$="Jan" THEN MM$="01"
180 IF M$="Feb" THEN MM$="02"
190 IF M$="Mar" THEN MM$="03"
200 IF M$="Apr" THEN MM$="04"
210 IF M$="May" THEN MM$="05"
220 IF M$="Jun" THEN MM$="06"
230 IF M$="Jul" THEN MM$="07"
240 IF M$="Aug" THEN MM$="08"
250 IF M$="Sep" THEN MM$="09"
260 IF M$="Oct" THEN MM$="10"
270 IF M$="Nov" THEN MM$="11"
280 IF M$="Dec" THEN MM$="12"
290 DD$=D$(3):IF LEN(DD$)=1 THEN DD$="0"+DD$
300 YY$=D$(4):IF LEN(YY$)=4 THEN YY$=MID$(YY$,3,2)
310 TM$=D$(5)
320 D1$="01/01/01":T1$="23:59:59"
330 SET DATE D1$:SET TIME T1$
340 T1 =TIME
350 T2 =TIME
360 GET DATE D2$
370 IF D1$=D2$ AND T1 <>0 AND T2 >=T1  AND T1 +60>T2  THEN GOTO 350
380 GET DATE D2$
390 DT$=YY$+"/"+MM$+"/"+DD$
400 IF D2$="01/02/01" THEN DT$=MM$+"/"+DD$+"/"+YY$
410 IF D2$="02/01/01" THEN DT$=DD$+"/"+MM$+"/"+YY$
420 SET TIME TM$:SET DATE DT$:SET TIME TM$:SET DATE DT$
430 PRINT
440 PRINTDT$" "TM$
450 GET DATE DT$:GET TIME TM$
460 PRINTDT$" "TM$
470 PRINT"Press space or button 1 in the next 3 seconds to avoid running %afterntp%"
480 T1=TIME
490 T2=TIME
500 IF STRIG(0)+STRIG(1)+STRIG(2)<>0 THEN GOTO 580
510 IF T2<T1 OR (T1+300)>T2 THEN GOTO 490
520 PRINT"Running %afterntp%"
530 CALL SYSTEM("%afterntp%")
540 RESUME 550
550 ON ERROR GOTO 0
560 PRINT"Unable to set time and date using NTP"
570 GOTO 470
580 PRINT"Not running %afterntp%"
590 CALL SYSTEM

While it works, I'm dissatisfied with it, in particular:

  1. The NTP-fetched time and date must be read and parsed from VRAM!
  2. The month names are matched textually, and it might break in other MSX machines/locales (I don't know?)
  3. The order of the fields in the MSX-BASIC date string is determined in a very hacky way — right now mm/dd/yy and dd/mm/yy are supported
  4. If the MSX-BASIC date string requires 12 hour time with AM/PM this program won't work at all
  5. At the end I wish to return to MSX-DOS and (unless a particular key was pressed) "continue" the AUTOEXEC.BAT, however right now that is also done in a very hacky way using an environment variable %afterntp%
  6. I don't know how to return to the video mode and color scheme in use prior to running the program. Is there some way to save and restore this?
  7. At the moment date and time are read and written separately, in close proximity, but there might be some cases where the end of a day is reached while reading the time or something, so there is some wasteful looping to attempt to avoid this. Is there a better way, perhaps one that allows reading both date and time at once in a synchronized way?

Are there better ways to solve any of these problems? Any pointers are appreciated, thanks!

NOTE: You must have time zone and NTP server set up in your GR8NET, and a working internet connection ready by the time this program runs. I am using a Wi-Fi to Ethernet bridge to connect mine (with absurdly high GR8NET DHCP retry count), and sometimes it doesn't complete WPA2 set-up + bridge DHCP + MSX DHCP in time to have working connectivity, so the program may fail to set the time in this scenario and will exit without doing anything useful

I don't think that GR8NET would have any issue setting date and time on an OCM, SM-X is an OCM derivative that has a built-in interface and you can use Konamiman's SNTP client in autoexec.bat to do it... Also I've been working on a new version of its firmware and driver (SM-X WiFi) with KdL help and that driver, that now is a bios, can set the time and date at boot. OCM doesn't have a battery to save SRAM (and doesn't have SRAM either :) ) but it emulates the RTC faithfully.

So, as GR8NET is UNAPI compliant, you should be able to use Konamiman's SNTP.COM, and, also, somehow I find it difficult to believe that if GR8NET has a auto-clock set at boot function, it is not able to set it on OCM, I don't have a GR8NET so can't help you with that, but I believe it has such function by a quick look at its manual and someone else will be able to help you setting it up to automatically update clock when booting. ;)

By bsittler

Expert (71)

bsittler's picture

27-09-2020, 03:21

Thank you! Indeed SNTP.COM works great, only wrinkle is it doesn't share the GR8NET time zone configuration