1 /* 2 Copyright (C) 1998,1999,2000 T. Scott Dattalo 3 4 This file is part of the libgpsim library of gpsim 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 This library is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, see 18 <http://www.gnu.org/licenses/lgpl-2.1.html>. 19 */ 20 21 22 /* 23 modules.h 24 25 The base class for modules is defined here. 26 27 Include this file into yours for creating custom modules. 28 */ 29 30 31 #ifndef SRC_MODULES_H_ 32 #define SRC_MODULES_H_ 33 34 #include <cstdlib> 35 #include <cstring> 36 #include <list> 37 #include <string> 38 #include <map> 39 40 #include "gpsim_object.h" 41 #include "gpsim_classes.h" 42 #include "symbol.h" 43 44 class Module; 45 class Module_Types; 46 class ModuleInterface; 47 class IOPIN; 48 class XrefObject; 49 class Package; 50 class ICommandHandler; 51 52 typedef Module * (*Module_FPTR)(); 53 typedef Module_Types * (*Module_Types_FPTR)(); 54 55 56 enum SIMULATION_MODES { 57 eSM_INITIAL, 58 eSM_STOPPED, 59 eSM_RUNNING, 60 eSM_SLEEPING, 61 eSM_SINGLE_STEPPING, 62 eSM_STEPPING_OVER, 63 eSM_RUNNING_OVER 64 }; 65 66 67 //------------------------------------------------------------------------ 68 // 69 // ModuleLibrary 70 // 71 // A module library is an OS dependent dynamically loadable library of 72 // gpsim Modules. A Module (see below) can range from anything as simple 73 // as a resistor to as complicated as microcontroller. However, the interface 74 // to loading libraries and instantiating modules is kept simple: 75 76 class ModuleLibrary { 77 public: 78 static int LoadFile(std::string &sLibraryName); 79 static int InstantiateObject(std::string &sObjectName, std::string &sInstantiatedName); 80 static void ListLoadableModules(); 81 }; 82 83 84 /* 85 * interface is a Module class member variable in gpsim, 86 * in WIN32 Platform SDK it is a macro, defined in BaseTypes.h 87 * the WIN32 Platform SDK definition should be undefined 88 */ 89 90 #ifdef interface 91 #undef interface 92 #endif 93 94 //------------------------------------------------------------------------ 95 // 96 /// Module - Base class for all gpsim behavior models. 97 98 class Module : public gpsimObject { 99 public: 100 Package *package = nullptr; // A package for the module 101 ModuleInterface *interface = nullptr; // An interface to the module. 102 SIMULATION_MODES simulation_mode; // describes the simulation state for this module 103 104 XrefObject *xref; // Updated when the module changes 105 106 /// I/O pin specific 107 108 virtual int get_pin_count(); 109 virtual std::string &get_pin_name(unsigned int pin_number); 110 virtual int get_pin_state(unsigned int pin_number); 111 virtual IOPIN *get_pin(unsigned int pin_number); 112 virtual void assign_pin(unsigned int pin_number, IOPIN *pin); 113 virtual void create_pkg(unsigned int number_of_pins); get_Vdd()114 virtual double get_Vdd() 115 { 116 return Vdd; 117 } set_Vdd(double v)118 virtual void set_Vdd(double v) 119 { 120 Vdd = v; 121 } 122 123 /// Symbols 124 /// Each module has its own symbol table. The global symbol 125 /// table can access this table too. getSymbolTable()126 SymbolTable_t & getSymbolTable() 127 { 128 return mSymbolTable; 129 } 130 int addSymbol(gpsimObject *, std::string *AliasedName = nullptr); 131 gpsimObject *findSymbol(const std::string &); 132 int removeSymbol(gpsimObject *); 133 int removeSymbol(const std::string &); 134 int deleteSymbol(const std::string &); 135 int deleteSymbol(gpsimObject *); 136 137 /// Registers - mostly processors, but can apply to complex modules register_mask()138 virtual unsigned int register_mask() const 139 { 140 return 0xff; 141 } register_size()142 virtual unsigned int register_size() const 143 { 144 return 1; 145 } 146 147 /// Reset 148 149 virtual void reset(RESET_TYPE r); 150 151 /// Version get_version()152 virtual char *get_version() 153 { 154 return version; 155 } 156 157 /// gui 158 /// The simulation engine doesn't know anything about the gui. 159 /// However, the set_widget and get_widget provide a mechanism 160 /// for the gui to associate a pointer with a module. 161 set_widget(void * a_widget)162 virtual void set_widget(void * a_widget) 163 { 164 widget = a_widget; 165 } get_widget()166 virtual void *get_widget() 167 { 168 return widget; 169 } 170 171 /// cli 172 /// Modules can have gpsim CLI scripts associated with them. 173 /// add_command will add a single CLI command to a script 174 void add_command(std::string &script_name, std::string &command); 175 176 /// run_script will pass a script to the gpsim CLI. This script 177 /// executes immediately (i.e. it'll execute before any commands 178 /// that may already be queued). 179 void run_script(std::string &script_name); 180 type()181 const char *type() 182 { 183 return module_type.c_str(); 184 } set_module_type(std::string type)185 void set_module_type(std::string type) 186 { 187 module_type = type; 188 } 189 190 Module(const char *_name = nullptr, const char *desc = nullptr); 191 virtual ~Module(); 192 193 /// Functions to support actual hardware isHardwareOnline()194 virtual bool isHardwareOnline() 195 { 196 return true; 197 } 198 199 private: 200 void *widget = nullptr; // GtkWidget * that is put in the breadboard. 201 std::string module_type; 202 203 // Storage for scripts specifically associated with this module. 204 class ModuleScript { 205 public: 206 explicit ModuleScript(const std::string &name_); 207 ~ModuleScript(); 208 void add_command(const std::string &command); 209 void run(ICommandHandler *); 210 void concatenate(ModuleScript *); 211 private: 212 std::string name; 213 std::list<std::string *> m_commands; 214 }; 215 216 std::map<std::string, ModuleScript *> m_scripts; 217 218 protected: 219 double Vdd = 5.0; 220 // Derived modules should assign more reasonable values for this. 221 char *version = nullptr; 222 SymbolTable_t mSymbolTable; 223 }; 224 225 class Module_Types { 226 public: 227 const char *names[2]; 228 Module * (*module_constructor)(const char *module_name); 229 }; 230 231 #ifndef SWIG 232 const int Module_Types_Name_Count = sizeof(((Module_Types*)NULL)->names) / sizeof(char*); 233 234 235 /** 236 * CFileSearchPath 237 * Implemented in os_dependent.cc 238 */ 239 class CFileSearchPath : public std::list<std::string> { 240 public: CFileSearchPath()241 CFileSearchPath() {} 242 void AddPathFromFilePath(std::string &sFolder, std::string &sFile); 243 const char * Find(std::string &path); 244 }; 245 246 /***************************************************************************** 247 * 248 * Helper functions 249 * 250 *****************************************************************************/ 251 void GetFileName(std::string &sPath, std::string &sName); 252 void GetFileNameBase(std::string &sPath, std::string &sName); 253 void FixupLibraryName(std::string &sPath); 254 void * load_library(const char *library_name, const char **pszError); 255 void * get_library_export(const char *name, void *library_handle, const char **pszError); 256 void free_library(void *handle); 257 void free_error_message(const char * pszError); 258 #endif 259 260 class DynamicModuleLibraryInfo { 261 public: 262 DynamicModuleLibraryInfo(std::string &sCanonicalName, 263 std::string &sUserSuppliedName, 264 void *pHandle); 265 user_name()266 inline std::string user_name() 267 { 268 return m_sCanonicalName; 269 } mod_list()270 inline Module_Types_FPTR mod_list() 271 { 272 return get_mod_list; 273 } 274 275 protected: 276 std::string m_sCanonicalName; 277 std::string m_sUserSuppliedName; 278 void *m_pHandle; 279 Module_Types * (*get_mod_list)(void) = nullptr; 280 }; 281 282 typedef std::map<std::string, DynamicModuleLibraryInfo *> ModuleLibraries_t; 283 284 #endif // SRC_MODULES_H_ 285