PICSimLab - Programmable IC Simulator Laboratory 0.9.2
PICSimLab - API
Loading...
Searching...
No Matches
board.h
1/* ########################################################################
2
3 PICSimLab - Programmable IC Simulator Laboratory
4
5 ########################################################################
6
7 Copyright (c) : 2010-2025 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 BOARD_H
27#define BOARD_H
28
29#include <picsim/picsim.h>
30#include <stdint.h>
31
32#include <stdlib.h>
33#include <string.h>
34#include <string>
35
36#define INCOMPLETE \
37 printf("Incomplete: %s -> %s :%i\n", __func__, __FILE__, __LINE__); \
38 exit(-1);
39
40enum { ARCH_P16, ARCH_P16E, ARCH_P18, ARCH_AVR8, ARCH_STM32, ARCH_STM8, ARCH_C51, ARCH_Z80, ARCH_UNKNOWN };
41
46typedef struct {
47 float x1;
48 float x2;
49 float y1;
50 float y2;
51 float cx;
52 float cy;
53 char name[10];
54 unsigned short id;
55 void* status;
56 unsigned char* update;
57 union {
58 unsigned char value;
59 short value_s;
60 float value_f;
61 };
62} input_t;
63
68typedef struct {
69 float x1;
70 float x2;
71 float y1;
72 float y2;
73 float cx;
74 float cy;
75 float r;
76 char name[10];
77 unsigned short id;
78 void* status;
79 unsigned char update;
80 union {
81 unsigned char value;
82 short value_s;
83 float value_f;
84 };
85} output_t;
86
87#define MAX_TIMERS 256
88
89#define MAX_IDS 128
90
91#define INVALID_ID (MAX_IDS - 1)
92
93#define IOINIT 110 // IOINIT is the max CPU pins number
94#define MAX_PIN_COUNT 256 // MAX_PIN_COUNT- IOINIT is the max spare parts extra pins number
95
100typedef struct {
101 uint32_t Timer;
102 uint32_t Reload;
103 void* Arg;
104 void (*Callback)(void* arg);
105 int Enabled;
106 double Tout; // in us
107} Timers_t;
108
114class board {
115public:
119 virtual void Draw(void) = 0;
120
124 virtual void Run_CPU(void) = 0;
125
129 virtual std::string GetSupportedDevices(void) = 0;
130
134 virtual std::string GetPictureFileName(void);
135
139 virtual std::string GetMapFile(void);
140
144 virtual void Reset(void) = 0;
145
149 virtual void EvMouseButtonPress(unsigned int button, unsigned int x, unsigned int y, unsigned int state) = 0;
150
154 virtual void EvMouseButtonRelease(unsigned int button, unsigned int x, unsigned int y, unsigned int state) = 0;
155
159 virtual void EvMouseMove(unsigned int button, unsigned int x, unsigned int y, unsigned int state) {};
160
164 virtual void EvKeyPress(unsigned int key, unsigned int mask) = 0;
165
169 virtual void EvKeyRelease(unsigned int key, unsigned int mask) = 0;
170
174 virtual void EvOnShow(void);
175
179 virtual void EvThreadRun(void) {};
180
184 virtual void RefreshStatus(void);
185
189 virtual void WritePreferences(void) {};
190
194 virtual void ReadPreferences(char* name, char* value) {};
195
199 virtual unsigned short GetInputId(char* name) = 0;
200
204 virtual unsigned short GetOutputId(char* name) = 0;
205
209 virtual void board_Event(const char* controlname) {};
210
214 virtual void board_ButtonEvent(const char* controlname, unsigned int button, unsigned int x, unsigned int y,
215 unsigned int state) {};
216
220 board(void);
221
225 virtual ~board(void);
226
230 void SetUseOscilloscope(int uo);
231
235 void SetUseSpareParts(int sp);
236
240 int GetUseOscilloscope(void);
241
245 int GetUseSpareParts(void);
246
250 virtual std::string GetName(void) = 0;
251
255 void SetProcessorName(std::string proc);
256
260 std::string GetProcessorName(void);
261
265 int GetInputCount(void);
266
270 input_t* GetInput(int n);
271
275 int GetOutputCount(void);
276
280 output_t* GetOutput(int n);
281
285 virtual int DebugInit(int dtyppe) = 0;
286
290 virtual std::string GetDebugName(void) = 0;
291
295 virtual std::string GetAboutInfo(void) = 0;
296
300 virtual void DebugLoop(void) = 0;
301
305 virtual int CpuInitialized(void) = 0;
306
310 virtual void MSetSerial(const char* port) = 0;
311
315 virtual int MInit(const char* processor, const char* fname, float freq) = 0;
316
320 virtual void MEnd(void) = 0;
321
325 virtual int MGetArchitecture(void) = 0;
326
330 virtual void EndServers(void) {};
331
335 virtual int MDumpMemory(const char* fname) = 0;
336
340 virtual void MEraseFlash(void) = 0;
341
345 virtual void MSetFreq(float freq) = 0;
346
350 virtual float MGetFreq(void) = 0;
351
355 virtual void MSetVCC(float vcc) = 0;
356
360 virtual float MGetVCC(void) = 0;
361
365 virtual float MGetInstClockFreq(void) = 0;
366
371
375 virtual int MGetPinCount(void) = 0;
376
380 virtual std::string MGetPinName(int pin) = 0;
381
385 virtual void MSetPin(int pin, unsigned char value) = 0;
386
390 virtual void MSetPinDOV(int pin, unsigned char ovalue) = 0;
391
395 virtual void MSetAPin(int pin, float value) = 0;
396
400 virtual void MSetPinOAV(int pin, float value) = 0;
401
405 virtual unsigned char MGetPin(int pin) = 0;
406
410 virtual const picpin* MGetPinsValues(void) = 0;
411
415 virtual float* MGetPinOAVPtr(int pin) = 0;
416
420 virtual void MStep(void) = 0;
421
425 virtual void MStepResume(void) = 0;
426
430 virtual int MReset(int flags) = 0;
431
435 virtual int MGetResetPin(void) = 0;
436
440 virtual int MGetIOUpdated(void) = 0;
441
445 virtual void MClearIOUpdated(void) = 0;
446
450 virtual unsigned short* DBGGetProcID_p(void) {
451 INCOMPLETE;
452 return NULL;
453 };
454
458 virtual unsigned int DBGGetPC(void) {
459 INCOMPLETE;
460 return 0;
461 };
462
466 virtual void DBGSetPC(unsigned int pc) { INCOMPLETE; };
467
471 virtual unsigned char* DBGGetRAM_p(void) {
472 INCOMPLETE;
473 return NULL;
474 };
475
479 virtual unsigned char* DBGGetROM_p(void) {
480 INCOMPLETE;
481 return NULL;
482 };
483
487 virtual unsigned char* DBGGetCONFIG_p(void) {
488 INCOMPLETE;
489 return NULL;
490 };
491
495 virtual unsigned char* DBGGetID_p(void) {
496 INCOMPLETE;
497 return NULL;
498 };
499
503 virtual unsigned char* DBGGetEEPROM_p(void) {
504 INCOMPLETE;
505 return NULL;
506 };
507
511 virtual unsigned int DBGGetRAMSize(void) {
512 INCOMPLETE;
513 return 0;
514 };
515
519 virtual unsigned int DBGGetROMSize(void) {
520 INCOMPLETE;
521 return 0;
522 };
523
527 virtual unsigned int DBGGetCONFIGSize(void) {
528 INCOMPLETE;
529 return 0;
530 };
531
535 virtual unsigned int DBGGetIDSize(void) {
536 INCOMPLETE;
537 return 0;
538 };
539
543 virtual unsigned int DBGGetEEPROM_Size(void) {
544 INCOMPLETE;
545 return 0;
546 };
547
551 virtual unsigned int DBGGetRAMLAWR(void) {
552 INCOMPLETE;
553 return 0;
554 };
555
559 virtual unsigned int DBGGetRAMLARD(void) {
560 INCOMPLETE;
561 return 0;
562 };
563
567 unsigned char CalcAngle(int i, int x, int y);
568
572 virtual void SetScale(double scale);
573
577 double GetScale(void);
578
583 virtual int GetDefaultClock(void) { return 2; };
584
589 std::string GetDefaultProcessor(void) { return DProc; };
590
595 void SetDefaultProcessor(std::string dproc) { DProc = dproc; };
596
600 uint32_t GetInstCounter(void) { return InstCounter; };
601
605 uint32_t GetInstCounter_us(const uint32_t start);
606
610 uint32_t GetInstCounter_ms(const uint32_t start);
611
615 int TimerRegister_us(const double micros, void (*Callback)(void* arg), void* arg);
616
620 int TimerRegister_ms(const double miles, void (*Callback)(void* arg), void* arg);
621
625 int TimerUnregister(const int timer);
626
630 int TimerChange_us(const int timer, const double micros);
631
635 int TimerChange_ms(const int timer, const double miles);
636
640 int TimerSetState(const int timer, const int enabled);
641
645 uint64_t TimerGet_ns(const int timer);
646
650 void TimerUpdateFrequency(float freq);
651
655 virtual void IoLockAccess(void) {};
656
660 virtual void IoUnlockAccess(void) {};
661
665 virtual int GetUARTRX(const int uart_num) { return 0; };
666
670 virtual int GetUARTTX(const int uart_num) { return 0; };
671
672 virtual std::string GetUARTStrStatus(const int uart_num) { return "Not implemented!"; };
673
677 virtual std::string GetClkLabel(void) { return "Clk (Mhz)"; };
678
679protected:
683 virtual void RegisterRemoteControl(void) {};
684
688 void InstCounterInc(void);
689
690 std::string Proc;
691 std::string DProc;
692 input_t input[MAX_IDS];
693 input_t* input_ids[MAX_IDS];
694 output_t output[MAX_IDS];
696 int inputc;
700 unsigned char p_RST;
701 float Scale;
702
706 void ReadMaps(void);
707
711 void StartThread(void);
712
716 void StopThread(void);
717
718private:
719 uint32_t InstCounter;
720 int TimersCount;
721 Timers_t Timers[MAX_TIMERS];
722 Timers_t* TimersList[MAX_TIMERS];
723
727 void ReadInputMap(std::string fname);
728
732 void ReadOutputMap(std::string fname);
733};
734
735extern int ioupdated;
736
737#endif /* BOARD_H */
738
739#ifndef BOARDS_DEFS_H
740#define BOARDS_DEFS_H
741
742#define board_init(name, function) \
743 static board* function##_create(void) { \
744 board* b = new function(); \
745 b->SetDefaultProcessor(b->GetProcessorName()); \
746 return b; \
747 }; \
748 static void __attribute__((constructor)) function##_init(void); \
749 static void function##_init(void) { \
750 board_register(name, function##_create); \
751 }
752
753typedef board* (*board_create_func)(void);
754
755void board_register(const char* name, board_create_func bcreate);
756
757// boards object creation
758board* create_board(int* lab, int* lab_);
759
760#define BOARDS_MAX 25
761
762extern int BOARDS_LAST;
763
764typedef struct {
765 char name[30]; // name
766 char name_[30]; // name without spaces
767 board_create_func bcreate;
768} board_desc;
769
770extern board_desc boards_list[BOARDS_MAX];
771
772// Arduino Uno is the dafault board
773#define DEFAULT_BOARD 2
774
775#endif /* BOARDS_DEFS_H */
Board class.
Definition board.h:114
virtual unsigned int DBGGetRAMLARD(void)
board microcontroller get last ram read address
Definition board.h:559
virtual unsigned int DBGGetROMSize(void)
board microcontroller get ROM (FLASH) memory size
Definition board.h:519
virtual unsigned short GetOutputId(char *name)=0
return the output ids numbers of names used in output map
virtual unsigned short GetInputId(char *name)=0
return the input ids numbers of names used in input map
int TimerChange_ms(const int timer, const double miles)
Modify timer value with us.
Definition board.cc:489
input_t * GetInput(int n)
Get board input.
Definition board.cc:315
virtual std::string GetName(void)=0
Get board name registered in PICSimLab.
virtual void EvMouseButtonPress(unsigned int button, unsigned int x, unsigned int y, unsigned int state)=0
Event on the board.
output_t output[MAX_IDS]
output map elements
Definition board.h:694
void StartThread(void)
Start parallel thread.
Definition board.cc:382
virtual int DebugInit(int dtyppe)=0
Start debug support.
virtual unsigned char * DBGGetCONFIG_p(void)
board microcontroller get CONFIG memory pointer
Definition board.h:487
virtual void board_ButtonEvent(const char *controlname, unsigned int button, unsigned int x, unsigned int y, unsigned int state)
Called when window side controls are activated.
Definition board.h:214
input_t * input_ids[MAX_IDS]
input map elements by id order
Definition board.h:693
virtual void EvKeyRelease(unsigned int key, unsigned int mask)=0
Event on the board.
virtual unsigned int DBGGetRAMLAWR(void)
board microcontroller get last ram write address
Definition board.h:551
virtual std::string GetAboutInfo(void)=0
Return the about information of part.
virtual void Reset(void)=0
Reset board status.
virtual std::string GetClkLabel(void)
Return the description of clk label.
Definition board.h:677
virtual unsigned int DBGGetCONFIGSize(void)
board microcontroller get CONFIG memory size
Definition board.h:527
int use_oscope
use oscilloscope window
Definition board.h:698
virtual void MSetPin(int pin, unsigned char value)=0
board microcontroller set digital pin
virtual int GetUARTRX(const int uart_num)
Return the UART N RX pin number.
Definition board.h:665
virtual unsigned short * DBGGetProcID_p(void)
board microcontroller get pointer to processor ID
Definition board.h:450
void ReadOutputMap(std::string fname)
Read the Output Map.
Definition board.cc:206
virtual void RegisterRemoteControl(void)
Register remote control variables.
Definition board.h:683
virtual void EvThreadRun(void)
Event on the board.
Definition board.h:179
virtual void MSetPinOAV(int pin, float value)=0
board microcontroller set digital pin output average value
virtual void ReadPreferences(char *name, char *value)
Called whe configuration file load preferences.
Definition board.h:194
virtual float MGetInstClockFreq(void)=0
board microcontroller get cpu internal clock (in PIC frequency/4)
virtual void IoLockAccess(void)
Lock IO to others threads access.
Definition board.h:655
virtual unsigned int DBGGetIDSize(void)
board microcontroller get internal IDS memory size
Definition board.h:535
virtual void Draw(void)=0
Called ever 100ms to draw board.
virtual int MGetIOUpdated(void)=0
get microcontroler status IO updated
virtual unsigned char * DBGGetID_p(void)
board microcontroller get internal IDS memory pointer
Definition board.h:495
std::string DProc
Name of default board processor.
Definition board.h:691
void SetDefaultProcessor(std::string dproc)
Set board default processor.
Definition board.h:595
int TimerRegister_us(const double micros, void(*Callback)(void *arg), void *arg)
Register a new timer with time in us (default enabled)
Definition board.cc:415
output_t * GetOutput(int n)
Get board input.
Definition board.cc:326
virtual unsigned char * DBGGetRAM_p(void)
board microcontroller get RAM memory pointer
Definition board.h:471
virtual unsigned char MGetPin(int pin)=0
board microcontroller get digital pin value
double GetScale(void)
Get board draw scale.
Definition board.cc:364
int GetOutputCount(void)
Get board output count.
Definition board.cc:322
int GetInputCount(void)
Get board input count.
Definition board.cc:311
int TimerSetState(const int timer, const int enabled)
Enable or disable timer.
Definition board.cc:502
virtual void EndServers(void)
board servers shutdown
Definition board.h:330
void InstCounterInc(void)
Increment the Intructions Counter.
Definition board.cc:402
int outputc
output map elements counter
Definition board.h:697
virtual void DBGSetPC(unsigned int pc)
board microcontroller set PC
Definition board.h:466
void SetUseOscilloscope(int uo)
Enable/disable oscilloscope measurement.
Definition board.cc:287
virtual void MSetFreq(float freq)=0
board microcontroller set frequency
virtual void EvMouseButtonRelease(unsigned int button, unsigned int x, unsigned int y, unsigned int state)=0
Event on the board.
virtual int MInit(const char *processor, const char *fname, float freq)=0
board microcontroller init
virtual unsigned char * DBGGetROM_p(void)
board microcontroller get ROM (FLASH) memory pointer
Definition board.h:479
virtual std::string GetPictureFileName(void)
Return the filename of board picture.
Definition board.cc:374
void SetProcessorName(std::string proc)
Set board processor.
Definition board.cc:303
virtual int MGetPinCount(void)=0
board microcontroller pin count
virtual int MGetArchitecture(void)=0
Return board microcontroller architecture.
virtual int CpuInitialized(void)=0
return true if microcontroller is initialized
virtual int MDumpMemory(const char *fname)=0
board microcontroller save non volatile memory to hex file
output_t * output_ids[MAX_IDS]
output map elements by id order
Definition board.h:695
virtual void WritePreferences(void)
Called to save board preferences in configuration file.
Definition board.h:189
virtual const picpin * MGetPinsValues(void)=0
board microcontroller get all pins list struct
int TimerChange_us(const int timer, const double micros)
Modify timer value with us.
Definition board.cc:476
unsigned char p_RST
board /RESET pin state
Definition board.h:700
virtual float * MGetPinOAVPtr(int pin)=0
board microcontroller get pointer to digital pin average value
int MGetClocksPerInstructions(void)
board microcontroller get Clock pulses per instruction (in PIC equal to 4)
Definition board.cc:333
virtual void MEraseFlash(void)=0
board microcontroller erase flash memory (program)
input_t input[MAX_IDS]
input map elements
Definition board.h:692
virtual void MSetPinDOV(int pin, unsigned char ovalue)=0
board microcontroller set Default Open Value (external pull)
virtual std::string MGetPinName(int pin)=0
board microcontroller pin name
virtual std::string GetDebugName(void)=0
Get debug interface name.
uint32_t GetInstCounter_ms(const uint32_t start)
Get elapsed time from instruction counter in us.
Definition board.cc:524
virtual float MGetVCC(void)=0
board microcontroller get vcc
virtual void MClearIOUpdated(void)=0
clear microcontroler status IO updated
board(void)
Called once on board creation.
Definition board.cc:34
virtual void board_Event(const char *controlname)
Called when window side controls are activated.
Definition board.h:209
uint64_t TimerGet_ns(const int timer)
Get elapsed time from instruction counter in ns.
Definition board.cc:513
std::string GetProcessorName(void)
Get board processor in use.
Definition board.cc:307
virtual void Run_CPU(void)=0
Paralle thread called ever 100ms to run cpu code.
virtual unsigned char * DBGGetEEPROM_p(void)
board microcontroller get data EEPROM memory pointer
Definition board.h:503
virtual void RefreshStatus(void)
Called ever 1s to refresh status.
Definition board.cc:283
void StopThread(void)
Stop parallel thread.
Definition board.cc:392
virtual void SetScale(double scale)
Set board draw scale.
Definition board.cc:360
virtual int MReset(int flags)=0
board microcontroller reset
virtual void MEnd(void)=0
board microcontroller end
virtual int MGetResetPin(void)=0
Get the Reset pin of microcontroller.
std::string GetDefaultProcessor(void)
Get board default processor.
Definition board.h:589
virtual void MSetAPin(int pin, float value)=0
board microcontroller set analog pin
int use_spare
use spare parts window
Definition board.h:699
int TimerUnregister(const int timer)
Unregister timer.
Definition board.cc:455
virtual void MStepResume(void)=0
board microcontroller run one or two steps to resume instruction
virtual int GetUARTTX(const int uart_num)
Return the UART N TX pin number.
Definition board.h:670
virtual std::string GetSupportedDevices(void)=0
Return a list of supported microcontrollers.
int GetUseSpareParts(void)
Get if spare parts is in use.
Definition board.cc:299
virtual void IoUnlockAccess(void)
Unlock IO to others threads access.
Definition board.h:660
unsigned char CalcAngle(int i, int x, int y)
Calc rotary potentiometer angle.
Definition board.cc:337
virtual void MSetSerial(const char *port)=0
Set serial port name to use.
virtual void MStep(void)=0
board microcontroller run one step
virtual std::string GetMapFile(void)
Return the filename of board picture map.
Definition board.cc:378
int TimerRegister_ms(const double miles, void(*Callback)(void *arg), void *arg)
Register a new timer with time in ms (default enabled)
Definition board.cc:435
int inputc
input map elements counter
Definition board.h:696
virtual float MGetFreq(void)=0
board microcontroller get frequency
int GetUseOscilloscope(void)
Get if oscilloscope is in use.
Definition board.cc:295
virtual ~board(void)
Called once on board destruction.
Definition board.cc:59
virtual int GetDefaultClock(void)
Get board default clock in MHz.
Definition board.h:583
virtual unsigned int DBGGetEEPROM_Size(void)
board microcontroller get data EEPROM memory size
Definition board.h:543
virtual void EvMouseMove(unsigned int button, unsigned int x, unsigned int y, unsigned int state)
Event on the board.
Definition board.h:159
uint32_t GetInstCounter(void)
Get instruction counter.
Definition board.h:600
virtual unsigned int DBGGetRAMSize(void)
board microcontroller get RAM memory size
Definition board.h:511
void ReadMaps(void)
Read maps.
Definition board.cc:61
virtual void DebugLoop(void)=0
debug step (pooling)
void SetUseSpareParts(int sp)
Enable/disable spare parts.
Definition board.cc:291
virtual void MSetVCC(float vcc)=0
board microcontroller set vcc
virtual void EvOnShow(void)
Event on the board.
Definition board.cc:368
virtual unsigned int DBGGetPC(void)
board microcontroller get PC
Definition board.h:458
virtual void EvKeyPress(unsigned int key, unsigned int mask)=0
Event on the board.
uint32_t GetInstCounter_us(const uint32_t start)
Get elapsed time from instruction counter in us.
Definition board.cc:520
void ReadInputMap(std::string fname)
Read the Input Map.
Definition board.cc:76
std::string Proc
Name of processor in use.
Definition board.h:690
void TimerUpdateFrequency(float freq)
Update Timer counters on frequency change.
Definition board.cc:528
internal timer struct
Definition board.h:100
Definition board.h:764
input map struct
Definition board.h:46
float x2
x2 position
Definition board.h:48
float value_f
updated value float
Definition board.h:60
short value_s
updated value short
Definition board.h:59
float cx
center x position
Definition board.h:51
unsigned char * update
output need draw update
Definition board.h:56
float y2
y2 position
Definition board.h:50
float x1
x1 position
Definition board.h:47
void * status
rcontrol status
Definition board.h:55
unsigned char value
updated value
Definition board.h:58
unsigned short id
region ID
Definition board.h:54
float y1
y1 position
Definition board.h:49
float cy
center y position
Definition board.h:52
output map struct
Definition board.h:68
float r
radius
Definition board.h:75
float x2
x2 position
Definition board.h:70
short value_s
updated value short
Definition board.h:82
unsigned char value
updated value
Definition board.h:81
float y2
y2 position
Definition board.h:72
float y1
y1 position
Definition board.h:71
void * status
rcontrol status
Definition board.h:78
unsigned short id
region ID
Definition board.h:77
float x1
x1 position
Definition board.h:69
float value_f
updated value float
Definition board.h:83
unsigned char update
need draw update
Definition board.h:79
float cx
center x position
Definition board.h:73
float cy
center y position
Definition board.h:74