PICSimLab - Programmable IC Simulator Laboratory 0.9.3
PICSimLab - API
Loading...
Searching...
No Matches
picsimlab.h
1/* ########################################################################
2
3 PICSimLab - Programmable IC Simulator Laboratory
4
5 ########################################################################
6
7 Copyright (c) : 2010-2026 Luis Claudio GambĂ´a Lopes <lcgamboa@yahoo.com>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 For e-mail suggestions : lcgamboa@yahoo.com
24 ######################################################################## */
25
26#ifndef PICSIMLAB
27#define PICSIMLAB
28
29#define BASETIMER 100 // timer period in ms
30#define NSTEPKF (4000.0 / BASETIMER) // Freq constant 4.0*timer_freq
31#define NSTEPKT (1e6 / NSTEPKF) // TIMER constant 1MHz/(4.0*timer_freq)
32#define DEFAULTJS 100 // IO refresh rate
33
34extern char SERIALDEVICE[100];
35
36#include <atomic>
37#include "board.h"
38#include "draw.h"
39#include "types.h"
40#include "util.h"
41
42class CPICSimLab {
43public:
44 CPICSimLab();
45
46 void Init(void);
47
51 std::string GetSharePath(void) { return SHARE; };
52 void SetSharePath(std::string spath) { SHARE = spath; };
53
54 std::string GetLibPath(void) { return libpath; };
55 void SetLibPath(std::string lpath) { libpath = lpath; };
56
57 std::string GetHomePath(void) { return HOME; };
58 void SetHomePath(std::string home) { HOME = home; };
59
60 std::string GetPath(void) { return PATH; };
61 void SetPath(std::string path) { PATH = path; };
62
63 std::string GetFNAME(void) { return FNAME; };
64 void SetFNAME(std::string fname) { FNAME = fname; };
65
66 std::string GetOldPath(void) { return OldPath; };
67 void SetOldPath(std::string op) { OldPath = op; };
68
69 std::string GetPWVscodePath(void) { return pw_vscode_path; };
70 void SetPWVscodePath(std::string path) { pw_vscode_path = path; };
71
72 std::string GetPWMplabxPath(void) { return pw_mplabx_path; };
73 void SetPWMplabxPath(std::string path) { pw_mplabx_path = path; };
74
75 std::string GetProcessorName(void) { return proc_; };
76 void SetProcessorName(std::string pn) { proc_ = pn; };
77
78 long int GetNSTEP(void) { return NSTEP; };
79 void SetNSTEP(long int ns) { NSTEP = ns; };
80
84 long int GetNSTEPJ(void) { return NSTEPJ; };
85 void SetNSTEPJ(long int nsj) { NSTEPJ = nsj; };
86
90 int GetJUMPSTEPS(void) { return JUMPSTEPS; };
91 void SetJUMPSTEPS(int js);
92
96 board* GetBoard(void) { return pboard; };
97 void SetBoard(board* b) { pboard = b; };
98 void DeleteBoard(void);
99
103 int GetMcuPwr(void) { return mcupwr; };
104
108 int GetMcuRst(void) { return mcurst; };
109
110 int GetMcuDbg(void) { return mcudbg; };
111
115 void SetMcuPwr(int pp) {
116 mcupwr = pp;
117 if (mcupwr)
118 SetCpuState(CPU_RUNNING);
119 else
120 SetCpuState(CPU_POWER_OFF);
121 };
122
123 void SetCpuState(const PICSimlabCPUState cs);
124
128 void SetMcuRst(int pr) { mcurst = pr; };
129
130 char GetCpuState(void) { return cpustate; };
131
132 void Set_mcudbg(int pd);
133
134 void SetMcuRun(int mr) { mcurun = mr; };
135 int GetMcuRun(void) { return mcurun; };
136
140 unsigned short GetDebugPort(void) { return debug_port + Instance; };
141
145 void SetDebugPort(unsigned short dp);
146
150 unsigned short GetRemotecPort(void) { return remotec_port + Instance; };
151
155 void SetRemotecPort(unsigned short rcp);
156
160 int GetInstanceNumber(void) { return Instance; };
161
165 int GetDebugType(void) { return debug_type; };
166
170 void SetDebugType(int dt) { debug_type = dt; };
171
175 int GetDebugStatus(void);
176
180 void SetDebugStatus(int dbs);
181
182 void SetClock(const float clk, const int update = 1);
183 float GetClock(void);
184
188 int GetCheckForDevelVersionUpdate(void) { return check_for_devel; };
189
193 void SetCheckForDevelVersionUpdate(int cfd) { check_for_devel = cfd; };
194
198 void SavePrefs(std::string name, std::string value);
199
200 void PrefsClear(void) { prefs.clear(); };
201 int PrefsSaveToFile(std::string fname) { return SaveToFile(prefs, fname.c_str()); };
202 int PrefsLoadFromFile(std::string fname) { return LoadFromFile(prefs, fname.c_str()); };
203 unsigned int PrefsGetLinesCount(void) { return prefs.size(); };
204 std::string PrefsGetLine(int ln) { return prefs.at(ln); };
205
206 void OpenLoadHexFileDialog(void);
207
208 void SetNeedReboot(int nr = 1);
209 int GetNeedReboot(void) { return NeedReboot; };
210
211 void RegisterError(const std::string module, const std::string error);
212 int GetErrorCount(void) { return Errors.size(); };
213
214 std::string GetError(int en) { return Errors.at(en); };
215
216 void DeleteError(int en) { Errors.erase(Errors.begin() + en); };
217
218 void EndSimulation(int saveold = 0, const char* newpath = NULL);
219
220 std::string GetWorkspaceFileName(void) { return Workspacefn; };
221
222 void SetWorkspaceFileName(const std::string fname) { Workspacefn = fname; };
223
224 std::string GetLastWorkspaceFileName(void) { return LastWorkspacefn; };
225
226 void SetLastWorkspaceFileName(const std::string fname) { LastWorkspacefn = fname; };
227
228 void SetLabs(const int lb, const int lb_) {
229 lab = lb;
230 lab_ = lb_;
231 };
232
233 int GetLab(void) { return lab; };
234 int GetLab_(void) { return lab_; };
235
236 int LoadHexFile(std::string fname, const int saveold = 0);
237
238 void LoadWorkspace(std::string fnpzw, const int show_readme = 1);
239 int SaveWorkspace(std::string fnpzw);
240
241 void SetSimulationRun(int run);
242 int GetSimulationRun(void);
243
244 double GetScale(void) { return scale; };
245 void SetScale(double s) { scale = ((int)(100 * s)) / 100.0; };
246
247 int GetNeedResize(void) { return need_resize; };
248 void SetNeedResize(int nr) { need_resize = nr; };
249
250 void Configure(const char* home, int use_default_board = 0, int create = 0, const char* lfile = NULL,
251 const int disable_debug = 0);
252
253 void SetplWidth(int pw) { plWidth = pw; };
254
255 void SetplHeight(int ph) { plHeight = ph; };
256
257 double GetIdleMs(void);
258 void SetIdleMs(double im);
259
260 int GetUseDSRReset(void) { return use_dsr_reset; };
261 void SetUseDSRReset(int udsr) { use_dsr_reset = udsr; };
262
263 void SetToDestroy(int reason = RC_EXIT);
264 int GetToDestroy(void) { return settodestroy; };
265
266 void SetSync(unsigned char s) { sync = s; };
267 unsigned char GetSync(void) { return sync; };
268
269 char* GetPzwTmpdir(void) { return pzwtmpdir; };
270
271 char* GetLastPzwTmpdir(void) { return lastpzwtmpdir; };
272
273 std::string GetSupportedBoards(void);
274
275 void UpdateStatus(const PICSimlabStatus field, const std::string msg);
276
277 void* UpdateGUI(const int id, const PICSimlabGUIType type, const PICSimlabGUIAction action, const void* arg);
278
279 void ConfigMenuGUI(const PICSimlabGUIMenu type);
280
281 static int CanvasCmd(const CanvasCmd_t cmd);
282
283 static int WindowCmd(const int id, const char* ControlName, const PICSimLabWindowAction action, const char* Value,
284 void* ReturnBuff = NULL);
285
286 static int SystemCmd(const PICSimLabSystemCmd cmd, const char* Arg, void* ReturnBuff = NULL);
287
288 std::atomic<unsigned short int> status;
289
290 std::atomic<int> tgo;
291
292 int plWidth;
293 int plHeight;
294 int use_dsr_reset;
295
296 void (*OnUpdateStatus)(const int field, const std::string msg);
297 void (*OnConfigure)(void);
298 void (*OnClockSet)(const float clk, const int update);
299 void (*OnReadPreferences)(const char* name, const char* value, const int create);
300 void (*OnSavePrefs)(void);
301 void (*OnLoadHexFile)(const std::string fname);
302 void (*OnOpenLoadHexFileDialog)(void);
303 void (*OnEndSimulation)(void);
304 void* (*OnUpdateGUI)(const int id, const PICSimlabGUIType type, const PICSimlabGUIAction action, const void* arg);
305 void (*OnConfigMenuGUI)(const PICSimlabGUIMenu type);
306
307 int (*OnCanvasCmd)(const CanvasCmd_t cmd);
308 int (*OnWindowCmd)(const int id, const char* ControlName, const PICSimLabWindowAction action, const char* Value,
309 void* ReturnBuff);
310 int (*OnSystemCmd)(const PICSimLabSystemCmd cmd, const char* Arg, void* ReturnBuff);
311
312private:
313 void StartRControl(void);
314 board* pboard;
315 int lab;
316 int lab_;
317 std::string SHARE;
318 std::string libpath;
319 long int NSTEP;
320 long int NSTEPJ;
321 int JUMPSTEPS;
322 int mcurun;
323 int mcupwr;
324 int mcurst;
325 int mcudbg;
326 PICSimlabCPUState cpustate;
327 unsigned short debug_port;
328 unsigned short remotec_port;
329 std::string HOME;
330 std::string PATH;
331 std::string FNAME;
332 std::string OldPath;
333 std::string proc_;
334 std::string pzw_ver;
335 int Instance;
336 int debug_type;
337 int debug;
338 int need_resize;
339 std::vector<std::string> prefs;
340 int NeedReboot;
341 std::vector<std::string> Errors;
342 std::string Workspacefn;
343 std::string LastWorkspacefn;
344 double scale;
345 double idle_ms;
346 int settodestroy;
347 unsigned char sync;
348 char pzwtmpdir[1024];
349 char lastpzwtmpdir[1024];
350 int check_for_devel;
351 std::string pw_vscode_path;
352 std::string pw_mplabx_path;
353 std::string pzw_creation_date;
354};
355
356extern CPICSimLab PICSimLab;
357
358#ifdef _WIN_
359#define msleep(x) Sleep(x)
360// extern void usleep(unsigned int usec);
361#include <unistd.h>
362#else
363#define msleep(x) usleep(x * 1000)
364#endif
365
366#define ST_T1 0x01
367#define ST_T2 0x02
368#define ST_TH 0x04
369#define ST_DI 0x80 // DISABLE
370
371#ifdef _WIN_
372#define NULLFILE "\\\\.\\NUL"
373#else
374#define NULLFILE "/dev/null"
375#endif
376
377#endif // PICSIMLAB
Definition picsimlab.h:42
void SetMcuRst(int pr)
Set mcu rst flag (inform simulator about mcu reset state).
Definition picsimlab.h:128
int GetCheckForDevelVersionUpdate(void)
Return the selected debugger type.
Definition picsimlab.h:188
int GetMcuRst(void)
Retunr if microcontroller reset pin is enabled.
Definition picsimlab.h:108
void SetDebugStatus(int dbs)
Set debug status flag.
Definition picsimlab.cc:223
long int GetNSTEPJ(void)
Get the number of steps in 100ms of simulation.
Definition picsimlab.h:84
void SetRemotecPort(unsigned short rcp)
Set remote control TCP port.
Definition picsimlab.cc:211
std::string GetSharePath(void)
Get the file path of resources.
Definition picsimlab.h:51
void SetMcuPwr(int pp)
Set the power status of microcontroller ON/OFF.
Definition picsimlab.h:115
void SetDebugPort(unsigned short dp)
Set debug TCP port.
Definition picsimlab.cc:207
int GetDebugStatus(void)
Get debug status (On/Off).
Definition picsimlab.cc:219
int GetMcuPwr(void)
Return actual power status of microcontroller ON/OFF.
Definition picsimlab.h:103
void SetCheckForDevelVersionUpdate(int cfd)
Set debug type (MDB or GDB).
Definition picsimlab.h:193
unsigned short GetRemotecPort(void)
Return the selected remote control port.
Definition picsimlab.h:150
int GetInstanceNumber(void)
Return the program instance number.
Definition picsimlab.h:160
int GetDebugType(void)
Return the selected debugger type.
Definition picsimlab.h:165
int GetJUMPSTEPS(void)
Get the number of steps to be skipped in board update.
Definition picsimlab.h:90
board * GetBoard(void)
Return a pointer to board object.
Definition picsimlab.h:96
unsigned short GetDebugPort(void)
Return the selected debugger port.
Definition picsimlab.h:140
void SetDebugType(int dt)
Set debug type (MDB or GDB).
Definition picsimlab.h:170
void SavePrefs(std::string name, std::string value)
Save the preferences.
Definition picsimlab.cc:259
Board class.
Definition board.h:114