PICSim  0.8.1
PICsim - PIC Simulator
picsim.h
Go to the documentation of this file.
1 /* ########################################################################
2 
3  PICsim - PIC simulator
4 
5  ########################################################################
6 
7  Copyright (c) : 2008-2020 Luis Claudio Gamboa Lopes
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 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 
61 #ifndef PICSIM
62 #define PICSIM
63 
64 #ifdef _WIN_
65 #include <windows.h>
66 #endif
67 
68 #include"p16fxxx_defs.h"
69 #include"p16fxxxe_defs.h"
70 #include"p18fxxx_defs.h"
71 
72 #define SERIAL_MAX 2
73 #define BUFFMAX 8192 //4K word
74 
80  typedef struct {
81  unsigned char ptype;
82  unsigned char dir;
83  unsigned char value;
84  unsigned char lvalue;
85  char pord;
86  unsigned char * port;
87  float avalue;
88  unsigned char ovalue;
89  float oavalue;
90  } picpin;
91 
97 typedef struct{
98  //USART
99  int sr;
100  unsigned char recb;
101  int serialc;
102  unsigned char txtemp[2];
103  char txtc;
104  unsigned char RCREG2;
105  //serial emulation
106  char SERIALDEVICE[100];
107  unsigned int serialbaud; //serial baud emulated
108  float serialexbaud; //exac serial baud
109 #ifdef _WIN_
110  HANDLE serialfd;
111 #else
112  int serialfd;
113 #endif
114  int s_open;
115  int flowcontrol;
116  int ctspin;
117  int rtspin;
118  unsigned char buff[BUFFMAX];
119  int bc;
120  unsigned char * serial_TXSTA;
121  unsigned char * serial_PIR;
122  unsigned char * serial_PIE;
123  unsigned char RXIF_mask;
124  unsigned char TXIF_mask;
125  unsigned char * serial_RCSTA;
126  unsigned char * serial_SPBRG;
127  unsigned char * serial_RCREG;
128  unsigned char * serial_TXREG;
129  unsigned short serial_TXREG_ADDR;
130  unsigned short serial_RCSTA_ADDR;
131  unsigned short serial_RCREG_ADDR;
132  unsigned char * serial_TRIS_RX;
133  unsigned char serial_TRIS_RX_MASK;
134  } _serial;
135 
140 #define PD_OUT 0x00
141 #define PD_IN 0x01
142 
148 #define PT_CMOS 0x01
149 #define PT_TTL 0x02
150 #define PT_ANALOG 0x04
151 #define PT_ANAREF 0x05
152 #define PT_USART 0x06
153 #define PT_NC 0xFF
154 
160  //family
161 #define P16 1
162 #define P18 2
163 #define P16E 3
164 
166  //FIXME remove all IDs
167 
168  //P16 processors
169 #define P16F84A 0x0560
170 
171 /*
172  #define P16F628 0x07C0
173 #define P16F877 0x09A0
174 #define P16F628A 0x1060
175 #define P16F648A 0x1100
176 #define P16F648AICD 0x1120
177 #define P16F877A 0x0E20
178  */
179 #define P16F777 0x0DE0
180 /*
181  //P16E processors
182 #define P16F1619 0x307D
183 #define P16F1788 0x302B
184 #define P16F1789 0x302A
185  */
186 #define P16F18855 0X306C
187 /*
188 #define P16F1939 0x23C0
189 #define P16F18324 0x303A
190 
191  //P18 processors
192  */
193 #define P18F452 0x0420
194 /*
195 #define P18F4520 0x1080
196 #define P18F4620 0x0C00
197 #define P18F4550 0x1200
198 #define P18F45K50 0x5C00
199 #define P18F27K40 0x6960
200 #define P18F47K40 0x6900
201  */
202 
207  //CFG bits
208 #define CFG_MCLR 0x01
209 #define CFG_WDT 0x02
210 #define CFG_DEBUG 0x04
211 #define CFG_WDT_DIV 0x08
212 
218  //PACKAGING
219 #define PDIP 0
220 #define QFN 1
221 
227  //HEX load Errors
228 #define HEX_OK 0
229 #define HEX_NFOUND 1
230 #define HEX_CHKSUM 2
231 #define HEX_NWRITE 3
232 
235 extern unsigned char NO_IO[6];
236 
237  //pin without IO
238 #define P_VDD &NO_IO[0]
239 #define P_VSS &NO_IO[1]
240 #define P_RST &NO_IO[2]
241 #define P_OSC &NO_IO[3]
242 #define P_USB &NO_IO[4]
243 #define P_NC &NO_IO[5]
244 
250  typedef struct {
251  unsigned char print;
252  unsigned int RAMSIZE;
253  unsigned int ROMSIZE;
254  unsigned int EEPROMSIZE;
255  unsigned int IDSIZE;
256  unsigned int CONFIGSIZE;
257  unsigned char STACKSIZE;
258  unsigned char PINCOUNT;
259  unsigned char CCPCOUNT;
260  unsigned char ADCCOUNT;
261  unsigned char USARTCOUNT;
262  unsigned char WDT_MS;
263  unsigned char family;
264  unsigned int processor;
265  unsigned short debugv[8];
266  unsigned char *ram;
267  unsigned short *prog;
268  unsigned short *id;
269  unsigned char *eeprom;
270  unsigned short *config;
271  unsigned int pc;
272  unsigned int jpc;
273  unsigned short lram;
274  unsigned short rram;
275  unsigned int *stack;
276  unsigned char w;
277  unsigned char wdt;
278  unsigned char s2;
279  float freq;
280  unsigned char sleep;
281  unsigned char pkg;
282  picpin *pins;
283  unsigned char mclr;
284  unsigned char *ccp;
285  unsigned char *adc;
286  unsigned char *usart_rx;
287  unsigned char *usart_tx;
288  unsigned char pgc;
289  unsigned char pgd;
290  unsigned char sck;
291  unsigned char sdo;
292  unsigned char sdi;
293  unsigned char t0cki;
294  unsigned char t1cki;
295  unsigned char intlv;
296  //on chip debbuger
297  unsigned char ocd_pgca;
298  unsigned char ocd_pgc;
299  //periferic
300  unsigned char debug;
301  unsigned char sstep;
302  unsigned char dbg;
303  unsigned char frst;
304  //timers
305  int cp0;
306  int cp1;
307  int cp2;
308  int cp2_;
309  int cp3;
310  int t2pr;
311  //timer input
312  unsigned char t0cki_;
313  unsigned char t1cki_;
314  //interrupt
315  unsigned char int0;
316  unsigned char int1;
317  unsigned char int2;
318  //pin value
319  unsigned char int0v;
320  unsigned char int1v;
321  unsigned char int2v;
322  //portb int mismatch
323  unsigned char portbm;
324  //adc
325  int adcstep;
326  unsigned char adcon1;
327  //wdt
328  float twdt;
329  //eeprom
330  int ee_wr;
331  //ports
332  unsigned char p16latch[5];
333  unsigned char porta;
334  unsigned char portb;
335  unsigned char portc;
336  unsigned char portd;
337  unsigned char porte;
338  unsigned char trisa;
339  unsigned char trisb;
340  unsigned char trisc;
341  unsigned char trisd;
342  unsigned char trise;
343  //MSSP
344  unsigned char ssp_ck;
345  unsigned char ssp_sck;
346  unsigned char ssp_scka;
347  unsigned char sspsr;
348  unsigned char ssp_bit;
349 
350  _serial serial[SERIAL_MAX];
351 
352  void (*reset)(void);
353  void (*mmap)(void);
354  int (*getconf)(unsigned int);
355  void (*periferic)(void);
356  int (*interrupt)(void);
357  void (*stop)(void);
358 
359  union {
360  P16map_t P16map;
361  P16Emap_t P16Emap;
362  P18map_t P18map;
363  };
364  } _pic;
365 
366  extern _pic * pic;
367 
368 
382  int pic_set_serial(_pic * pic_, int nser , const char * name, int flowcontrol, int ctspin, int rtspin);
383 
396  int pic_init(_pic * pic_, int processor, const char * fname, int leeprom, float freq);
397 
406  int pic_reset(int flags);
407 
412  void pic_erase_flash(void);
413 
420  void pic_step(void);
421 
426  void pic_end(void);
427 
434  unsigned char pic_get_pin(unsigned char pin);
435 
443  int pic_set_pin(unsigned char pin, unsigned char value);
444 
452  int pic_set_apin(unsigned char pin, float value);
453 
460  unsigned char pic_get_pin_type(unsigned char pin);
461 
468  unsigned char pic_get_pin_dir(unsigned char pin);
469 
476  unsigned char pic_get_pin_DOV(unsigned char pin);
477 
485  int pic_set_pin_DOV(unsigned char pin, unsigned char value);
486 
490  void pic_icsp_init(void);
491 
496  int pic_icsp(void);
497 
501  void pic_icsp_init18(void);
502 
507  int pic_icsp18(void);
508 
516  int read_ihx(const char * fname, int leeprom);
517 
525  int read_ihx_16e(const char * fname, int leeprom);
526 
534  int read_ihx_18(const char * fname, int leeprom);
535 
542  int write_ihx(const char * fname);
543 
550  int write_ihx16e(const char * fname);
551 
558  int write_ihx18(const char * fname);
559 
566  int getproclist(char list[][30], int size);
567 
574  unsigned int getprocbyname(const char *str);
575 
582  unsigned int getfprocbyname(const char *str);
583 
590  unsigned int getfprocbynumber(int proc);
591 
599  char * getnamebyproc(int proc, char *str);
600 
607  const char * getFSRname_16(unsigned int addr);
608 
615  const char * getFSRname_16E(unsigned int addr);
616 
623  const char * getFSRname_16E2(unsigned int addr);
624 
631  const char * getFSRname_18(unsigned int addr);
632 
641  const char * getPinName(_pic * pic, int pin, char * pname);
642 
649 #define sfr_addr(fsr) (fsr - pic->ram)
650 
651  typedef struct {
652  unsigned short ID;
653  char name [30];
654  unsigned char family;
655  void (*start) (void);
656  } pic_desc;
657 
658 
659 #define PMAX 20
660 
661  extern pic_desc PICS[PMAX];
662  extern int PIC_count;
663 
664 #define init_pic(function, family, ID) \
665  static pic_desc desc_ ## function = { ID , #function , family , function ## _start };\
666  static void __attribute__((constructor)) init_ ## function(void);\
667  static void init_ ## function (void){ \
668  pic_register(desc_ ## function);}
669 
670 
671  void pic_register(pic_desc picd);
672 
673 #endif
674 
675 #ifdef __cplusplus
676 }
677 #endif
678 
pic_get_pin_DOV
unsigned char pic_get_pin_DOV(unsigned char pin)
Get Default Open Value of one pin (external PULL UP)
pic_get_pin_dir
unsigned char pic_get_pin_dir(unsigned char pin)
Get dir of one pin.
pic_set_apin
int pic_set_apin(unsigned char pin, float value)
Set digital value of one pin.
pic_erase_flash
void pic_erase_flash(void)
erase the microcontroller FLASH
picpin::ptype
unsigned char ptype
pin types
Definition: picsim.h:81
P16Emap_t
PICsim P16E memory map structure.
Definition: p16fxxxe_defs.h:41
picpin::avalue
float avalue
analog input value
Definition: picsim.h:87
getFSRname_18
const char * getFSRname_18(unsigned int addr)
Return P18 FSR name by address.
pic_step
void pic_step(void)
Execute one simulation step.
write_ihx18
int write_ihx18(const char *fname)
Dump all non volatile P18 microcontroller memory to HEX file.
P18map_t
PICsim P18 memory map structure.
Definition: p18fxxx_defs.h:35
picpin::lvalue
unsigned char lvalue
latch value
Definition: picsim.h:84
_pic
PICsim PIC structure.
Definition: picsim.h:250
P16map_t
PICsim P16 memory map structure.
Definition: p16fxxx_defs.h:35
read_ihx_16e
int read_ihx_16e(const char *fname, int leeprom)
Read HEX file to P16E microcontroller.
pic_set_pin_DOV
int pic_set_pin_DOV(unsigned char pin, unsigned char value)
Set Default Open Value of one pin (external PULL UP)
_pic::processor
unsigned int processor
procid
Definition: picsim.h:264
getFSRname_16E2
const char * getFSRname_16E2(unsigned int addr)
Return P16E2 FSR name by address.
getprocbyname
unsigned int getprocbyname(const char *str)
Return processor ID by name.
getPinName
const char * getPinName(_pic *pic, int pin, char *pname)
Return pin name.
picpin::dir
unsigned char dir
pin dir
Definition: picsim.h:82
pic_end
void pic_end(void)
finalize and free pic object internal memory
_pic::family
unsigned char family
pic family
Definition: picsim.h:263
picpin::value
unsigned char value
value
Definition: picsim.h:83
getnamebyproc
char * getnamebyproc(int proc, char *str)
Return processor name by ID.
getFSRname_16
const char * getFSRname_16(unsigned int addr)
Return P16 FSR name by address.
_pic::print
unsigned char print
print information ON/OFF
Definition: picsim.h:251
_pic::rram
unsigned short rram
readed register
Definition: picsim.h:274
pic_set_pin
int pic_set_pin(unsigned char pin, unsigned char value)
Set digital value of one pin.
picpin::oavalue
float oavalue
analog output value
Definition: picsim.h:89
_serial
PICsim serial structure.
Definition: picsim.h:97
_pic::freq
float freq
frequency
Definition: picsim.h:279
_pic::sleep
unsigned char sleep
sleep mode on
Definition: picsim.h:280
read_ihx
int read_ihx(const char *fname, int leeprom)
Read HEX file to P16 microcontroller.
picpin::port
unsigned char * port
port address
Definition: picsim.h:86
getFSRname_16E
const char * getFSRname_16E(unsigned int addr)
Return P16E FSR name by address.
pic_icsp18
int pic_icsp18(void)
Run P18 ICSP (must be pooling)
picpin
PICsim pin structure.
Definition: picsim.h:80
pic
_pic * pic
global pointer
_pic::pkg
unsigned char pkg
packaging type
Definition: picsim.h:281
picpin::ovalue
unsigned char ovalue
defaut open pin value
Definition: picsim.h:88
pic_get_pin
unsigned char pic_get_pin(unsigned char pin)
Get digital value of one pin.
_pic::lram
unsigned short lram
loaded register
Definition: picsim.h:273
picpin::pord
char pord
pin port number
Definition: picsim.h:85
pic_init
int pic_init(_pic *pic_, int processor, const char *fname, int leeprom, float freq)
Initialize one pic object.
pic_desc
Definition: picsim.h:651
pic_set_serial
int pic_set_serial(_pic *pic_, int nser, const char *name, int flowcontrol, int ctspin, int rtspin)
Configure the serial port connection.
getproclist
int getproclist(char list[][30], int size)
Return list name of supported processors
pic_reset
int pic_reset(int flags)
Reset the microcontroller.
pic_icsp_init18
void pic_icsp_init18(void)
Initialize P18 ICSP support.
pic_icsp
int pic_icsp(void)
Run P16 ICSP (must be pooling)
write_ihx
int write_ihx(const char *fname)
Dump all non volatile P16 microcontroller memory to HEX file.
getfprocbyname
unsigned int getfprocbyname(const char *str)
Return processor family ID by name.
write_ihx16e
int write_ihx16e(const char *fname)
Dump all non volatile P16E microcontroller memory to HEX file.
read_ihx_18
int read_ihx_18(const char *fname, int leeprom)
Read HEX file to P18 microcontroller.
pic_icsp_init
void pic_icsp_init(void)
Initialize P16 ICSP support.
pic_get_pin_type
unsigned char pic_get_pin_type(unsigned char pin)
Get type of one pin.
getfprocbynumber
unsigned int getfprocbynumber(int proc)
Return processor family ID by ID.