/* program in MIX 'C' to set real-time-clock located at ports 30h through 33h in Morrow MD 2/3 rev. 2 main board. Mike Allen 8/10/86 */ #include "stdio" #include "stdlib.h" #define CLS 'Z'-0x40 /* Clear Screen (^Z) */ #define PA 0x30 /* 8255 A register */ #define PB 0x31 /* 8255 B register */ #define PC 0x32 /* 8255 C register */ #define CNTRL 0x33 /* 8255 Control register */ main() { int rtc[13]; /* MSM8532 registers */ int i, /* general loop variable */ civil_time, /* 24hr, AM or PM indicator */ new_in; /* new values */ char junque[3]; /* input string */ putchar(CLS); /* Clear the screen */ outp(CNTRL,'\220'); /* Set up 8255 */ outp(PC,'\040'); /* Put MSM5832 in 'read' mode */ for(i=2;i<13;i++) { /* read top 11 MSM5832 registers */ outp(PB,i); /* output the register address */ rtc[i]=inp(PA)&0xf; /* read the register and strip the 4 top bits */ } outp(PC,'\000'); /* Take the MSM5832 out of the read mode */ if(rtc[5]>7) /* check for 24 hr time. */ civil_time=0; else if(rtc[5]>3) /* not 24 hr, check for PM */ civil_time=2; else /* not 24 hr or PM. Must be AM. */ civil_time=1; do { new_in = 10*rtc[12]+rtc[11]; /* old value of years */ printf("Year? <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 0 || new_in > 99); /* make sure it's valid */ rtc[12] = new_in/10; /* set up 10s of years */ rtc[11] = new_in%10; /* set up 1s of years */ do { new_in = rtc[8]/4; /* old leap year flag */ printf("Leap year? [1=yes] <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 0 || new_in > 1); /* make sure it's valid */ rtc[8] = (rtc[8] & 0x3) | (new_in*4); /* set/reset leap year flag */ do { new_in = 10*rtc[10]+rtc[9]; /* old value of month */ printf("Month? <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 1 || new_in > 12); /* make sure it's valid */ rtc[10] = new_in/10; /* set up 10s of months */ rtc[9] = new_in%10; /* set up 1s of months */ do { new_in = 10*(rtc[8] & 0x3)+rtc[7]; /* old value of day */ printf("Day? <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 1 || new_in > 31); /* make sure it's valid */ rtc[8] = (rtc[8] & 0x4) | (new_in/10); /* set up 10s of days */ rtc[7] = new_in%10; /* set up 1s of years */ do { new_in = rtc[6]; /* old value of day of week */ printf("Day of week? [0=Sunday, 6=Saturday] <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 0 || new_in > 6); /* make sure it's valid */ rtc[6] = new_in; /* set up day of week */ do { new_in = civil_time; printf("24 hr[0], AM[1] or PM[2]? <%d> ",new_in); /* show 24hr, AM, PM flags */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 0 || new_in > 2); /* make sure it's valid */ civil_time = new_in; new_in = 10*(rtc[5] & 0x3) + rtc[4]; /* get hours */ rtc[5] = rtc[5] & 0x3; /* clear 24hr & PM flags */ switch(civil_time) { case 0: rtc[5] |= 0x8; /* set 24hr flag */ break; case 2: rtc[5] |= 0x4; /* set PM flag */ break; default: break; } if (civil_time) { /* make sure 12hr times are valid */ if (new_in > 12) new_in -=12; else if (new_in == 0) new_in = 12; rtc[5] = (rtc[5] & 0x4) | (new_in/10); /* set 10s of hours */ rtc[4] = new_in%10; /* set 1s of hours */ do { new_in = 10*(rtc[5] & 0x3)+rtc[4]; /* old value of hours */ printf("Hours? <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 1 || new_in > 12); /* make sure it's valid */ } else { do { new_in = 10*(rtc[5] & 0x3)+rtc[4]; /* old value of hours */ printf("Hours? <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 0 || new_in > 23); /* make sure it's valid */ } rtc[5] = (rtc[5] & 0xC) | (new_in/10); /* set up 10s of hours */ rtc[4] = new_in%10; /* set up 1s of hours */ do { new_in = 10*rtc[3]+rtc[2]; /* old value of minutes */ printf("Minutes? <%d> ",new_in); /* show it */ gets(junque); /* get input */ if (strlen(junque)) /* see if new value */ new_in = atoi(junque); /* if so, make integer */ } while( new_in < 0 || new_in > 59); /* make sure it's valid */ rtc[3] = new_in/10; /* set up 10s of minutes */ rtc[2] = new_in%10; /* set up 1s of minutes */ puts("Press RETURN to start clock on the minute."); /* prompt user */ gets(junque); /* load the clock */ outp(CNTRL,'\200'); /* set up 8255 */ outp(PC,'\020'); /* turn on the hold */ for(i=0;i<15;i++); /* wait a LONG time */ for(i=0;i<13;i++) { /* write ALL 13 MSM5832 registers to reset seconds */ outp(PB,i); /* output the register address */ outp(PA,(rtc[i] & 0xF)); /* output the bottom 4 bits of the register */ outp(PC,'\120'); /* strobe the write line */ outp(PC,'\020'); } outp(PC,'\000'); /* trun off the hold line */ outp(CNTRL,'\220'); /* restore the 8255 */ exit(0); /* We done! We bad! */ }