2018-02-04 15:41:29 +08:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
|
|
|
|
* this notice you can do whatever you want with this stuff. If we meet some day,
|
|
|
|
* and you think this stuff is worth it, you can buy me a beer in return.
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
2017-03-04 17:08:03 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2017-11-07 14:54:28 -08:00
|
|
|
#include <string.h>
|
2017-11-09 13:24:21 -08:00
|
|
|
#include <time.h>
|
2017-03-04 17:08:03 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int lastClkVal;
|
|
|
|
int pos;
|
2017-11-07 14:54:28 -08:00
|
|
|
uint16_t cmd;
|
2017-03-04 17:08:03 +08:00
|
|
|
uint8_t mem[32];
|
|
|
|
} Rtc;
|
|
|
|
|
|
|
|
static Rtc rtc;
|
|
|
|
|
|
|
|
void rtcTick() {
|
|
|
|
int x;
|
|
|
|
for (x=0; x<3; x++) {
|
|
|
|
rtc.mem[x]++;
|
2017-11-09 13:24:21 -08:00
|
|
|
if (rtc.mem[x]!=0) break;
|
2017-03-04 17:08:03 +08:00
|
|
|
}
|
2017-11-09 13:24:21 -08:00
|
|
|
for (int i=0; i<4; i++) rtc.mem[i+4]=rtc.mem[i]; //undocumented; mac needs it tho'.
|
2017-03-04 17:08:03 +08:00
|
|
|
}
|
|
|
|
|
2017-09-04 09:25:24 +08:00
|
|
|
extern void saveRtcMem(char *mem);
|
|
|
|
|
2017-03-04 17:08:03 +08:00
|
|
|
int rtcCom(int en, int dat, int clk) {
|
|
|
|
int ret=0;
|
|
|
|
clk=clk?1:0;
|
|
|
|
if (en) {
|
|
|
|
rtc.pos=0;
|
|
|
|
rtc.cmd=0;
|
|
|
|
} else {
|
2017-11-07 14:54:28 -08:00
|
|
|
if (clk!=rtc.lastClkVal && clk) {
|
2017-03-04 17:08:03 +08:00
|
|
|
if (rtc.pos<8 || (rtc.pos<16 && ((rtc.cmd&0x8000)==0)) ) {
|
2017-11-07 14:54:28 -08:00
|
|
|
//First 8 bits, or all 16 bits if write: accumulate data
|
2017-03-04 17:08:03 +08:00
|
|
|
if (dat) rtc.cmd|=(1<<(15-rtc.pos));
|
2017-11-07 14:54:28 -08:00
|
|
|
}
|
|
|
|
// printf("RTC: clocktick %d, dataline %d, cmd %x\n", rtc.pos, dat, rtc.cmd);
|
|
|
|
if (rtc.cmd&0x8000) { //read
|
2017-03-04 17:08:03 +08:00
|
|
|
if (rtc.pos==8) {
|
|
|
|
rtc.cmd|=rtc.mem[(rtc.cmd&0x7C00)>>10];
|
2017-11-09 13:24:21 -08:00
|
|
|
// printf("RTC: Read cmd %x val %x\n", rtc.cmd>>8, (rtc.cmd&0xff));
|
2017-03-04 17:08:03 +08:00
|
|
|
}
|
|
|
|
ret=((rtc.cmd&(1<<(15-rtc.pos)))?1:0);
|
|
|
|
} else if (rtc.pos==15) {
|
2017-11-09 13:24:21 -08:00
|
|
|
// printf("RTC: Write cmd %x\n", rtc.cmd>>8);
|
2017-11-07 14:54:28 -08:00
|
|
|
rtc.mem[(rtc.cmd&0x7C00)>>10]=rtc.cmd&0xff;
|
|
|
|
saveRtcMem((char*)rtc.mem);
|
2017-03-04 17:08:03 +08:00
|
|
|
}
|
|
|
|
rtc.pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rtc.lastClkVal=clk;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-04 09:25:24 +08:00
|
|
|
void rtcInit(char *mem) {
|
|
|
|
memcpy(rtc.mem, mem, 32);
|
|
|
|
}
|