1 //============================================================================
2 //
3 //   SSSS    tt          lll  lll
4 //  SS  SS   tt           ll   ll
5 //  SS     tttttt  eeee   ll   ll   aaaa
6 //   SSSS    tt   ee  ee  ll   ll      aa
7 //      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator"
8 //  SS  SS   tt   ee      ll   ll  aa  aa
9 //   SSSS     ttt  eeeee llll llll  aaaaa
10 //
11 // Copyright (c) 1995-2014 by Bradford W. Mott, Stephen Anthony
12 // and the Stella Team
13 //
14 // See the file "License.txt" for information on usage and redistribution of
15 // this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 //
17 // $Id: SaveKey.cxx 2838 2014-01-17 23:34:03Z stephena $
18 //============================================================================
19 
20 #include "MT24LC256.hxx"
21 #include "System.hxx"
22 #include "SaveKey.hxx"
23 
24 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SaveKey(Jack jack,const Event & event,const System & system,const string & eepromfile)25 SaveKey::SaveKey(Jack jack, const Event& event, const System& system,
26                  const string& eepromfile)
27   : Controller(jack, event, system, Controller::SaveKey),
28     myEEPROM(NULL)
29 {
30   myEEPROM = new MT24LC256(eepromfile, system);
31 
32   myDigitalPinState[One] = myDigitalPinState[Two] = true;
33   myAnalogPinValue[Five] = myAnalogPinValue[Nine] = maximumResistance;
34 }
35 
36 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
~SaveKey()37 SaveKey::~SaveKey()
38 {
39   delete myEEPROM;
40 }
41 
42 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
read(DigitalPin pin)43 bool SaveKey::read(DigitalPin pin)
44 {
45   // We need to override the Controller::read() method, since the timing
46   // of the actual read is important for the EEPROM (we can't just read
47   // 60 times per second in the ::update() method)
48   switch(pin)
49   {
50     // Pin 3: EEPROM SDA
51     //        input data from the 24LC256 EEPROM using the I2C protocol
52     case Three:
53       return myDigitalPinState[Three] = myEEPROM->readSDA();
54 
55     default:
56       return Controller::read(pin);
57   }
58 }
59 
60 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
write(DigitalPin pin,bool value)61 void SaveKey::write(DigitalPin pin, bool value)
62 {
63   // Change the pin state based on value
64   switch(pin)
65   {
66     // Pin 3: EEPROM SDA
67     //        output data to the 24LC256 EEPROM using the I2C protocol
68     case Three:
69       myDigitalPinState[Three] = value;
70       myEEPROM->writeSDA(value);
71       break;
72 
73     // Pin 4: EEPROM SCL
74     //        output clock data to the 24LC256 EEPROM using the I2C protocol
75     case Four:
76       myDigitalPinState[Four] = value;
77       myEEPROM->writeSCL(value);
78       break;
79 
80     default:
81       break;
82   }
83 }
84 
85 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
systemCyclesReset()86 void SaveKey::systemCyclesReset()
87 {
88   // The EEPROM keeps track of cycle counts, and needs to know when the
89   // cycles are reset
90   myEEPROM->systemCyclesReset();
91 }
92