32x: add preliminary hint emulation
[picodrive.git] / pico / eeprom.c
CommitLineData
45f2f245 1/*\r
2 * rarely used EEPROM code\r
cff531af 3 * (C) notaz, 2007-2009\r
4 *\r
5 * This work is licensed under the terms of MAME license.\r
6 * See COPYING file in the top-level directory.\r
45f2f245 7 *\r
8 * (see Genesis Plus for Wii/GC code and docs for info,\r
9 * full game list and better code).\r
10 */\r
11\r
12#include "pico_int.h"\r
13\r
14static unsigned int last_write = 0xffff0000;\r
15\r
16// eeprom_status: LA.. s.la (L=pending SCL, A=pending SDA,\r
17// s=started, l=old SCL, a=old SDA)\r
18static void EEPROM_write_do(unsigned int d) // ???? ??la (l=SCL, a=SDA)\r
19{\r
20 unsigned int sreg = Pico.m.eeprom_status, saddr = Pico.m.eeprom_addr;\r
21 unsigned int scyc = Pico.m.eeprom_cycle, ssa = Pico.m.eeprom_slave;\r
22\r
23 elprintf(EL_EEPROM, "eeprom: scl/sda: %i/%i -> %i/%i, newtime=%i", (sreg&2)>>1, sreg&1,\r
24 (d&2)>>1, d&1, SekCyclesDoneT() - last_write);\r
25 saddr &= 0x1fff;\r
26\r
27 if(sreg & d & 2) {\r
28 // SCL was and is still high..\r
29 if((sreg & 1) && !(d&1)) {\r
30 // ..and SDA went low, means it's a start command, so clear internal addr reg and clock counter\r
31 elprintf(EL_EEPROM, "eeprom: -start-");\r
32 //saddr = 0;\r
33 scyc = 0;\r
34 sreg |= 8;\r
35 } else if(!(sreg & 1) && (d&1)) {\r
36 // SDA went high == stop command\r
37 elprintf(EL_EEPROM, "eeprom: -stop-");\r
38 sreg &= ~8;\r
39 }\r
40 }\r
41 else if((sreg & 8) && !(sreg & 2) && (d&2))\r
42 {\r
43 // we are started and SCL went high - next cycle\r
44 scyc++; // pre-increment\r
45 if(SRam.eeprom_type) {\r
46 // X24C02+\r
47 if((ssa&1) && scyc == 18) {\r
48 scyc = 9;\r
49 saddr++; // next address in read mode\r
50 /*if(SRam.eeprom_type==2) saddr&=0xff; else*/ saddr&=0x1fff; // mask\r
51 }\r
52 else if(SRam.eeprom_type == 2 && scyc == 27) scyc = 18;\r
53 else if(scyc == 36) scyc = 27;\r
54 } else {\r
55 // X24C01\r
56 if(scyc == 18) {\r
57 scyc = 9; // wrap\r
58 if(saddr&1) { saddr+=2; saddr&=0xff; } // next addr in read mode\r
59 }\r
60 }\r
61 elprintf(EL_EEPROM, "eeprom: scyc: %i", scyc);\r
62 }\r
63 else if((sreg & 8) && (sreg & 2) && !(d&2))\r
64 {\r
65 // we are started and SCL went low (falling edge)\r
66 if(SRam.eeprom_type) {\r
67 // X24C02+\r
68 if(scyc == 9 || scyc == 18 || scyc == 27); // ACK cycles\r
69 else if( (SRam.eeprom_type == 3 && scyc > 27) || (SRam.eeprom_type == 2 && scyc > 18) ) {\r
70 if(!(ssa&1)) {\r
71 // data write\r
72 unsigned char *pm=SRam.data+saddr;\r
73 *pm <<= 1; *pm |= d&1;\r
74 if(scyc == 26 || scyc == 35) {\r
75 saddr=(saddr&~0xf)|((saddr+1)&0xf); // only 4 (?) lowest bits are incremented\r
76 elprintf(EL_EEPROM, "eeprom: write done, addr inc to: %x, last byte=%02x", saddr, *pm);\r
77 }\r
78 SRam.changed = 1;\r
79 }\r
80 } else if(scyc > 9) {\r
81 if(!(ssa&1)) {\r
82 // we latch another addr bit\r
83 saddr<<=1;\r
84 if(SRam.eeprom_type == 2) saddr&=0xff; else saddr&=0x1fff; // mask\r
85 saddr|=d&1;\r
86 if(scyc==17||scyc==26) {\r
87 elprintf(EL_EEPROM, "eeprom: addr reg done: %x", saddr);\r
88 if(scyc==17&&SRam.eeprom_type==2) { saddr&=0xff; saddr|=(ssa<<7)&0x700; } // add device bits too\r
89 }\r
90 }\r
91 } else {\r
92 // slave address\r
93 ssa<<=1; ssa|=d&1;\r
94 if(scyc==8) elprintf(EL_EEPROM, "eeprom: slave done: %x", ssa);\r
95 }\r
96 } else {\r
97 // X24C01\r
98 if(scyc == 9); // ACK cycle, do nothing\r
99 else if(scyc > 9) {\r
100 if(!(saddr&1)) {\r
101 // data write\r
102 unsigned char *pm=SRam.data+(saddr>>1);\r
103 *pm <<= 1; *pm |= d&1;\r
104 if(scyc == 17) {\r
105 saddr=(saddr&0xf9)|((saddr+2)&6); // only 2 lowest bits are incremented\r
106 elprintf(EL_EEPROM, "eeprom: write done, addr inc to: %x, last byte=%02x", saddr>>1, *pm);\r
107 }\r
108 SRam.changed = 1;\r
109 }\r
110 } else {\r
111 // we latch another addr bit\r
112 saddr<<=1; saddr|=d&1; saddr&=0xff;\r
113 if(scyc==8) elprintf(EL_EEPROM, "eeprom: addr done: %x", saddr>>1);\r
114 }\r
115 }\r
116 }\r
117\r
118 sreg &= ~3; sreg |= d&3; // remember SCL and SDA\r
119 Pico.m.eeprom_status = (unsigned char) sreg;\r
120 Pico.m.eeprom_cycle = (unsigned char) scyc;\r
121 Pico.m.eeprom_slave = (unsigned char) ssa;\r
122 Pico.m.eeprom_addr = (unsigned short)saddr;\r
123}\r
124\r
125static void EEPROM_upd_pending(unsigned int d)\r
126{\r
127 unsigned int d1, sreg = Pico.m.eeprom_status;\r
128\r
129 sreg &= ~0xc0;\r
130\r
131 // SCL\r
132 d1 = (d >> SRam.eeprom_bit_cl) & 1;\r
133 sreg |= d1 << 7;\r
134\r
135 // SDA in\r
136 d1 = (d >> SRam.eeprom_bit_in) & 1;\r
137 sreg |= d1 << 6;\r
138\r
139 Pico.m.eeprom_status = (unsigned char) sreg;\r
140}\r
141\r
142void EEPROM_write16(unsigned int d)\r
143{\r
144 // this diff must be at most 16 for NBA Jam to work\r
145 if (SekCyclesDoneT() - last_write < 16) {\r
146 // just update pending state\r
147 elprintf(EL_EEPROM, "eeprom: skip because cycles=%i",\r
148 SekCyclesDoneT() - last_write);\r
149 EEPROM_upd_pending(d);\r
150 } else {\r
151 int srs = Pico.m.eeprom_status;\r
152 EEPROM_write_do(srs >> 6); // execute pending\r
153 EEPROM_upd_pending(d);\r
154 if ((srs ^ Pico.m.eeprom_status) & 0xc0) // update time only if SDA/SCL changed\r
155 last_write = SekCyclesDoneT();\r
156 }\r
157}\r
158\r
159void EEPROM_write8(unsigned int a, unsigned int d)\r
160{\r
161 unsigned char *wb = Pico.m.eeprom_wb;\r
162 wb[a & 1] = d;\r
163 EEPROM_write16((wb[0] << 8) | wb[1]);\r
164}\r
165\r
166unsigned int EEPROM_read(void)\r
167{\r
168 unsigned int shift, d;\r
169 unsigned int sreg, saddr, scyc, ssa, interval;\r
170\r
171 // flush last pending write\r
172 EEPROM_write_do(Pico.m.eeprom_status>>6);\r
173\r
174 sreg = Pico.m.eeprom_status; saddr = Pico.m.eeprom_addr&0x1fff; scyc = Pico.m.eeprom_cycle; ssa = Pico.m.eeprom_slave;\r
175 interval = SekCyclesDoneT() - last_write;\r
176 d = (sreg>>6)&1; // use SDA as "open bus"\r
177\r
178 // NBA Jam is nasty enough to read <before> raising the SCL and starting the new cycle.\r
179 // this is probably valid because data changes occur while SCL is low and data can be read\r
180 // before it's actual cycle begins.\r
181 if (!(sreg&0x80) && interval >= 24) {\r
182 elprintf(EL_EEPROM, "eeprom: early read, cycles=%i", interval);\r
183 scyc++;\r
184 }\r
185\r
186 if (!(sreg & 8)); // not started, use open bus\r
187 else if (scyc == 9 || scyc == 18 || scyc == 27) {\r
188 elprintf(EL_EEPROM, "eeprom: r ack");\r
189 d = 0;\r
190 } else if (scyc > 9 && scyc < 18) {\r
191 // started and first command word received\r
192 shift = 17-scyc;\r
193 if (SRam.eeprom_type) {\r
194 // X24C02+\r
195 if (ssa&1) {\r
196 elprintf(EL_EEPROM, "eeprom: read: addr %02x, cycle %i, reg %02x", saddr, scyc, sreg);\r
197 if (shift==0) elprintf(EL_EEPROM, "eeprom: read done, byte %02x", SRam.data[saddr]);\r
198 d = (SRam.data[saddr]>>shift)&1;\r
199 }\r
200 } else {\r
201 // X24C01\r
202 if (saddr&1) {\r
203 elprintf(EL_EEPROM, "eeprom: read: addr %02x, cycle %i, reg %02x", saddr>>1, scyc, sreg);\r
204 if (shift==0) elprintf(EL_EEPROM, "eeprom: read done, byte %02x", SRam.data[saddr>>1]);\r
205 d = (SRam.data[saddr>>1]>>shift)&1;\r
206 }\r
207 }\r
208 }\r
209\r
210 return (d << SRam.eeprom_bit_out);\r
211}\r
212\r