1 /*
2    Copyright (C) 2006 T. Scott Dattalo
3    Copyright (C) 2006 Roy R Rankin
4 
5 This file is part of the libgpsim_modules library of gpsim
6 
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11 
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, see
19 <http://www.gnu.org/licenses/lgpl-2.1.html>.
20 */
21 
22 
23 #include "../config.h"    // get the definition for HAVE_GUI
24 #ifdef HAVE_GUI
25 #include <gtk/gtk.h>
26 #endif
27 
28 class Processor;
29 #include "../src/i2c-ee.h"
30 #include "i2c-eeprom.h"
31 #include "../src/stimuli.h"
32 #include "../src/packages.h"
33 
34 #include <string>
35 
36 namespace I2C_EEPROM_Modules {
37 
38 class I2C_ENABLE : public IOPIN {
39 public:
40   I2C_ENABLE(const char *name, unsigned int bit, I2C_EE_Module *pParent);
41 
42   virtual void setDrivenState(bool);
43 
44 private:
45   I2C_EE_Module *m_pParent;
46   unsigned int m_bit;
47 };
48 
49 
I2C_ENABLE(const char * name,unsigned int bit,I2C_EE_Module * pParent)50 I2C_ENABLE::I2C_ENABLE(const char *name, unsigned int bit,
51                        I2C_EE_Module *pParent)
52   : IOPIN(name), m_pParent(pParent), m_bit(bit)
53 {
54 }
55 
56 
setDrivenState(bool bNewState)57 void I2C_ENABLE::setDrivenState(bool bNewState)
58 {
59   IOPIN::setDrivenState(bNewState);
60 
61   if (m_pParent) {
62     m_pParent->setEnable(bNewState, m_bit);
63   }
64 }
65 
66 
I2C_EE_Module(const char * _name)67 I2C_EE_Module::I2C_EE_Module(const char *_name)
68   : Module(_name, "EEProm")
69 {
70   //initializeAttributes();
71   chip_select = 0;
72 }
73 
74 
~I2C_EE_Module()75 I2C_EE_Module::~I2C_EE_Module()
76 {
77   removeSymbol(m_wp);
78   removeSymbol(m_A[0]);
79   removeSymbol(m_A[1]);
80   removeSymbol(m_A[2]);
81   removeSymbol((IOPIN *)(m_eeprom->sda));
82   removeSymbol((IOPIN *)(m_eeprom->scl));
83   m_eeprom->sda = nullptr;
84   m_eeprom->scl = nullptr;
85   delete att_eeprom;
86   delete m_eeprom;
87 }
88 
89 
construct_2k(const char * _new_name)90 Module *I2C_EE_Module::construct_2k(const char *_new_name)
91 {
92 // FIXME: att_name is unused, unlike in the other constructors
93 
94   std::string att_name = _new_name;
95   I2C_EE_Module *pEE = new I2C_EE_Module(_new_name);
96   // I2C_EE size in bytes prom size in bits
97   (pEE->m_eeprom) = new I2C_EE((Processor *)pEE, 256, 16, 1, 0xe, 0, 0);
98   pEE->create_iopin_map();
99   att_name += ".eeprom";
100   pEE->att_eeprom = new PromAddress(pEE->m_eeprom, "eeprom", "Address I2C_EE");
101   pEE->addSymbol(pEE->att_eeprom);
102   return pEE;
103 }
104 
105 
construct_16k(const char * _new_name)106 Module *I2C_EE_Module::construct_16k(const char *_new_name)
107 {
108   std::string att_name = _new_name;
109   I2C_EE_Module *pEE = new I2C_EE_Module(_new_name);
110   // I2C_EE size in bytes prom size in bits
111   (pEE->m_eeprom) = new I2C_EE((Processor *)pEE, 2048, 16, 1, 0, 0xe, 1);
112   pEE->create_iopin_map();
113   att_name += ".eeprom";
114   pEE->att_eeprom = new PromAddress(pEE->m_eeprom, att_name.c_str(), "Address I2C_EE");
115   pEE->addSymbol(pEE->att_eeprom);
116   return pEE;
117 }
118 
119 
construct_256k(const char * _new_name)120 Module *I2C_EE_Module::construct_256k(const char *_new_name)
121 {
122   std::string att_name = _new_name;
123   I2C_EE_Module *pEE = new I2C_EE_Module(_new_name);
124   // I2C_EE size in bytes prom size in bits
125   (pEE->m_eeprom) = new I2C_EE((Processor *)pEE, 32768, 64, 2, 0xe, 0, 0);
126   pEE->create_iopin_map();
127   att_name += ".eeprom";
128   pEE->att_eeprom = new PromAddress(pEE->m_eeprom, att_name.c_str(), "Address I2C_EE");
129   pEE->addSymbol(pEE->att_eeprom);
130   return pEE;
131 }
132 
133 
create_iopin_map()134 void I2C_EE_Module::create_iopin_map()
135 {
136   m_wp  =  new I2C_ENABLE("WP", 0, this);
137   addSymbol(m_wp);
138   m_A[0] = new I2C_ENABLE("A0", 1, this);
139   addSymbol(m_A[0]);
140   m_A[1] = new I2C_ENABLE("A1", 2, this);
141   addSymbol(m_A[1]);
142   m_A[2] = new I2C_ENABLE("A2", 3, this);
143   addSymbol(m_A[2]);
144   addSymbol((IOPIN *)(m_eeprom->sda));
145   addSymbol((IOPIN *)(m_eeprom->scl));
146   package = new Package(8);
147   package->assign_pin(1, m_A[0]);
148   package->assign_pin(2, m_A[1]);
149   package->assign_pin(3, m_A[2]);
150   package->assign_pin(5, (IOPIN *)(m_eeprom->sda));
151   package->assign_pin(6, (IOPIN *)(m_eeprom->scl));
152   package->assign_pin(7, m_wp);
153 }
154 
155 
156 // WP or A0-A2 has changed
setEnable(bool NewState,unsigned int bit)157 void I2C_EE_Module::setEnable(bool NewState, unsigned int bit)
158 {
159   if (NewState) {
160     chip_select |= 1 << bit;
161 
162   } else {
163     chip_select &= ~(1 << bit);
164   }
165 
166   m_eeprom->set_chipselect(chip_select);
167 }
168 
169 } // end of namespace I2C_EEEPROM_Modules
170