-
Notifications
You must be signed in to change notification settings - Fork 0
/
pd4990adc.c
353 lines (313 loc) · 8.06 KB
/
pd4990adc.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Emulation for the NEC PD4990A.
*
* The PD4990A is a serial I/O Calendar & Clock IC used in the
* NEO GEO and probably a couple of other machines.
*/
/*
######################################################################
# Dream neo cd for DreamCast V 1.0 #
######################################################################
# including : Dreamneo cd v1.0 Beta #
# Ported by Ian Micheal Based on Neogeo cd SDL by fosters #
# #
# 2004 team and credits #
# #code and co coders quzar black aura troy GPF blue crab #
######################################################################
# #
# Makefile (c)2020 from Ian Micheal #
######################################################################
*/
//-- Include Files -----------------------------------------------------------
#include <stdio.h>
#include "pd4990a.h"
#include <kos.h>
#include <time.h>
/* Set the data in the chip to Monday 09/09/73 00:00:00 */
/* If you ever read this Leejanne, you know what I mean :-) */
static struct pd4990a_s pd4990a =
{
0x00, /* seconds BCD */
0x00, /* minutes BCD */
0x00, /* hours BCD */
0x09, /* days BCD */
9, /* month Hexadecimal form */
0x73, /* year BCD */
1 /* weekday BCD */
};
static int shiftlo,shifthi;
//static unsigned shiftlo,shifthi;
static int retraces = 0; /* Assumes 60 retraces a second */
//static int testwaits = 0;
static int maxwaits = 1;
static int testbit = 0; /* Pulses a bit in order to simulate */
/* test output */
static int outputbit = 0;
static int bitno = 0;
static char reading=0;
static char writing=0;
//static int clock_line=0;
//static int command_line=0; //??
#define DATA_BIT 0x1
#define CLOCK_BIT 0x2
#define END_BIT 0x4
void pd4990a_addretrace()
{
testbit ^= 1;
retraces++;
if (retraces == 60) {
retraces = 0;
pd4990a.seconds++;
if ((pd4990a.seconds & 0x0f) == 10) {
pd4990a.seconds &= 0xf0;
pd4990a.seconds += 0x10;
if (pd4990a.seconds == 0x60) {
pd4990a.seconds = 0;
pd4990a.minutes++;
if ((pd4990a.minutes & 0x0f) == 10) {
pd4990a.minutes &= 0xf0;
pd4990a.minutes += 0x10;
if (pd4990a.minutes == 0x60) {
pd4990a.minutes = 0;
pd4990a.hours++;
if ((pd4990a.hours & 0x0f) == 10) {
pd4990a.hours &= 0xf0;
pd4990a.hours += 0x10;
}
if (pd4990a.hours == 0x24) {
pd4990a.hours = 0;
pd4990a_increment_day();
}
}
}
}
}
}
}
void pd4990a_increment_day(void)
{
int real_year;
pd4990a.days++;
if ((pd4990a.days & 0x0f) >= 10)
{
pd4990a.days &= 0xf0;
pd4990a.days += 0x10;
}
pd4990a.weekday++;
if (pd4990a.weekday == 7)
pd4990a.weekday=0;
switch(pd4990a.month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (pd4990a.days == 0x32)
{
pd4990a.days = 1;
pd4990a_increment_month();
}
break;
case 2:
real_year = (pd4990a.year>>4)*10 + (pd4990a.year&0xf);
if ((real_year % 4) && (!(real_year % 100) || (real_year % 400)))
{
if (pd4990a.days == 0x29)
{
pd4990a.days = 1;
pd4990a_increment_month();
}
}
else
{
if (pd4990a.days == 0x30)
{
pd4990a.days = 1;
pd4990a_increment_month();
}
}
break;
case 4: case 6: case 9: case 11:
if (pd4990a.days == 0x31)
{
pd4990a.days = 1;
pd4990a_increment_month();
}
break;
}
}
void pd4990a_increment_month(void)
{
pd4990a.month++;
if (pd4990a.month == 13)
{
pd4990a.month = 1;
pd4990a.year++;
if ( (pd4990a.year & 0x0f) >= 10 )
{
pd4990a.year &= 0xf0;
pd4990a.year += 0x10;
}
if (pd4990a.year == 0xA0)
pd4990a.year = 0;
}
}
int pd4990a_testbit_r(void)
{
return (testbit);
}
int pd4990a_databit_r(void)
{
return (outputbit);
}
static void pd4990a_readbit(void)
{
switch(bitno)
{
case 0x00: case 0x01: case 0x02: case 0x03:
case 0x04: case 0x05: case 0x06: case 0x07:
outputbit=(pd4990a.seconds >> bitno) & 0x01;
break;
case 0x08: case 0x09: case 0x0a: case 0x0b:
case 0x0c: case 0x0d: case 0x0e: case 0x0f:
outputbit=(pd4990a.minutes >> (bitno-0x08)) & 0x01;
break;
case 0x10: case 0x11: case 0x12: case 0x13:
case 0x14: case 0x15: case 0x16: case 0x17:
outputbit=(pd4990a.hours >> (bitno-0x10)) & 0x01;
break;
case 0x18: case 0x19: case 0x1a: case 0x1b:
case 0x1c: case 0x1d: case 0x1e: case 0x1f:
outputbit=(pd4990a.days >> (bitno-0x18)) & 0x01;
break;
case 0x20: case 0x21: case 0x22: case 0x23:
outputbit=(pd4990a.weekday >> (bitno-0x20)) & 0x01;
break;
case 0x24: case 0x25: case 0x26: case 0x27:
outputbit=(pd4990a.month >> (bitno-0x24)) & 0x01;
break;
case 0x28: case 0x29: case 0x2a: case 0x2b:
case 0x2c: case 0x2d: case 0x2e: case 0x2f:
outputbit=(pd4990a.year >> (bitno-0x28)) & 0x01;
break;
case 0x30: case 0x31: case 0x32: case 0x33:
/*unknown */
break;
}
}
static void pd4990a_resetbitstream(void)
{
shiftlo=0;
shifthi=0;
bitno=0;
}
static void pd4990a_writebit(unsigned char bit)
{
if(bitno<=31) /*low part */
shiftlo|=bit<<bitno;
else /*high part */
shifthi|=bit<<(bitno-32);
}
static void pd4990a_nextbit(void)
{
++bitno;
if(reading)
pd4990a_readbit();
if(reading && bitno==0x34)
{
reading=0;
pd4990a_resetbitstream();
}
}
static unsigned char pd4990a_getcommand(void)
{
/*Warning: problems if the 4 bits are in different */
/*parts, It's very strange that this case could happen. */
if(bitno<=31)
return shiftlo>>(bitno-4);
else
return shifthi>>(bitno-32-4);
}
static void pd4990a_update_date(void)
{
pd4990a.seconds=(shiftlo>>0 )&0xff;
pd4990a.minutes=(shiftlo>>8 )&0xff;
pd4990a.hours =(shiftlo>>16)&0xff;
pd4990a.days =(shiftlo>>24)&0xff;
pd4990a.weekday=(shifthi>>0 )&0x0f;
pd4990a.month =(shifthi>>4 )&0x0f;
pd4990a.year =(shifthi>>8 )&0xff;
}
static void pd4990a_process_command(void)
{
switch(pd4990a_getcommand())
{
case 0x1: /*load output register */
bitno=0;
if(reading)
pd4990a_readbit(); /*prepare first bit */
shiftlo=0;
shifthi=0;
break;
case 0x2:
writing=0; /*store register to current date */
pd4990a_update_date();
break;
case 0x3: /*start reading */
reading=1;
break;
case 0x7: /*switch testbit every frame */
maxwaits=1;
break;
case 0x8: /*switch testbit every half-second */
maxwaits=30;
break;
}
}
void pd4990a_serial_control(unsigned char data)
{
static int clock_line=0;
static int command_line=0; /*?? */
/*Check for command end */
if(command_line && !(data&END_BIT)) /*end of command */
{
pd4990a_process_command();
pd4990a_resetbitstream();
}
command_line=data&END_BIT;
if(clock_line && !(data&CLOCK_BIT)) /*clock lower edge */
{
pd4990a_writebit(data&DATA_BIT);
pd4990a_nextbit();
}
clock_line=data&CLOCK_BIT;
}
void pd4990a_control_w(unsigned address, unsigned data) {
pd4990a_serial_control(data&0x7);
}
void pd4990a_control_16_w (int offset, int data)
{
pd4990a_serial_control(data&0x7);
}
void pd4990a_init(void) {
/*
time_t ltime;
struct tm *today;
usleep(100);
time(<ime);
localtime_r(<ime,today); //gpf
usleep(100);
//today = localtime(<ime); fosters pc/version
usleep(100);
pd4990a.seconds = ((today->tm_sec / 10) << 4) + (today->tm_sec % 10);
usleep(100);
pd4990a.minutes = ((today->tm_min / 10) << 4) + (today->tm_min % 10);
pd4990a.hours = ((today->tm_hour / 10) << 4) + (today->tm_hour % 10);
usleep(100);
pd4990a.days = ((today->tm_mday / 10) << 4) + (today->tm_mday % 10);
usleep(100);
pd4990a.month = (today->tm_mon + 1);
pd4990a.year = ((today->tm_year / 10) << 4) + (today->tm_year % 10);
usleep(100);
pd4990a.weekday = today->tm_wday;
usleep(100);
*/
}
// printf("%d %d %d %d %d %d %d\n",pd4990a.seconds,pd4990a.minutes,pd4990a.hours,pd4990a.days,pd4990a.month,pd4990a.year,pd4990a.weekday);