PICSim 0.8.1
PICsim - PIC Simulator
All Data Structures Files Functions Variables Typedefs Macros Modules Pages
picsim.h
Go to the documentation of this file.
1/* ########################################################################
2
3 PICsim - PIC simulator
4
5 ########################################################################
6
7 Copyright (c) : 2008-2022 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
67#ifdef __cplusplus
68extern "C"
69{
70#endif
71
72#ifndef PICSIM
73#define PICSIM
74
75#ifdef _WIN_
76#include <windows.h>
77#endif
78
79#include "bitbang_uart.h"
80#include "p16fxxx_defs.h"
81#include "p16fxxxe_defs.h"
82#include "p18fxxx_defs.h"
83
84#define SERIAL_MAX 2
85#define BUFFMAX 8192 // 4K word
86
92 typedef struct
93 {
94 unsigned char ptype;
95 unsigned char dir;
96 unsigned char value;
97 unsigned char lvalue;
98 char pord;
99 unsigned char *port;
100 float avalue;
101 unsigned char ovalue;
102 float oavalue;
103 unsigned char lsvalue;
104 } picpin;
105
111 typedef struct
112 {
113 unsigned char pin;
114 unsigned char ovalue;
115 unsigned char cap_ps;
116 } ccp_t;
117
123 typedef struct
124 {
125 // USART
126 unsigned char recb;
127 int serialc;
128 unsigned char txtemp[2];
129 char txtc;
130 unsigned char RCREG2;
131 // serial emulation
132 char SERIALDEVICE[100];
133 unsigned int serialbaud; // serial baud emulated
134 float serialexbaud; // exac serial baud
135#ifdef _WIN_
136 HANDLE serialfd;
137#else
138 int serialfd;
139#endif
140 int s_open;
141 int flowcontrol;
142 int ctspin;
143 int rtspin;
144 unsigned char buff[BUFFMAX];
145 int bc;
146 unsigned char *serial_TXSTA;
147 unsigned char *serial_PIR;
148 unsigned char *serial_PIE;
149 unsigned char RXIF_mask;
150 unsigned char TXIF_mask;
151 unsigned char *serial_RCSTA;
152 unsigned char *serial_SPBRG;
153 unsigned char *serial_SPBRGH;
154 unsigned char *serial_BAUDCTL;
155 unsigned char *serial_RCREG;
156 unsigned char *serial_TXREG;
157 unsigned short serial_TXREG_ADDR;
158 unsigned short serial_RCSTA_ADDR;
159 unsigned short serial_RCREG_ADDR;
160 unsigned char *serial_TRIS_RX;
161 unsigned char serial_TRIS_RX_MASK;
162 bb_uart_t bbuart;
163 } _serial;
164
169#define PD_OUT 0x00
170#define PD_IN 0x01
177#define PT_POWER 0x00
178#define PT_DIGITAL 0x01
179#define PT_ANALOG 0x04
180#define PT_ANAREF 0x05
181#define PT_USART 0x06
182#define PT_NC 0xFF
189// family
190#define P16 1
191#define P18 2
192#define P16E 3
195// FIXME remove all IDs
196
197// P16 processors
198#define P16F84A 0x0560
199
200/*
201 #define P16F628 0x07C0
202#define P16F877 0x09A0
203#define P16F628A 0x1060
204#define P16F648A 0x1100
205#define P16F648AICD 0x1120
206#define P16F877A 0x0E20
207 */
208#define P16F777 0x0DE0
209/*
210 //P16E processors
211#define P16F1619 0x307D
212#define P16F1788 0x302B
213#define P16F1789 0x302A
214 */
215#define P16F18855 0X306C
216/*
217#define P16F1939 0x23C0
218#define P16F18324 0x303A
219
220 //P18 processors
221 */
222#define P18F452 0x0420
223/*
224#define P18F4520 0x1080
225#define P18F4620 0x0C00
226#define P18F4550 0x1200
227#define P18F45K50 0x5C00
228#define P18F27K40 0x6960
229#define P18F47K40 0x6900
230 */
231
236// CFG bits
237#define CFG_MCLR 0x01
238#define CFG_WDT 0x02
239#define CFG_DEBUG 0x04
240#define CFG_WDT_DIV 0x08
247// PACKAGING
248#define PDIP 0
249#define QFN 1
256// HEX load Errors
257#define HEX_OK 0
258#define HEX_NFOUND 1
259#define HEX_CHKSUM 2
260#define HEX_NWRITE 3
263 extern unsigned char NO_IO[6];
264
265// pin without IO
266#define P_VDD &NO_IO[0]
267#define P_VSS &NO_IO[1]
268#define P_RST &NO_IO[2]
269#define P_OSC &NO_IO[3]
270#define P_USB &NO_IO[4]
271#define P_NC &NO_IO[5]
272
278 typedef struct _pic _pic;
279
280 struct _pic
281 {
282 unsigned char print;
283 unsigned int RAMSIZE;
284 unsigned int ROMSIZE;
285 unsigned int EEPROMSIZE;
286 unsigned int IDSIZE;
287 unsigned int CONFIGSIZE;
288 unsigned char STACKSIZE;
289 unsigned char PINCOUNT;
290 unsigned char CCPCOUNT;
291 unsigned char ADCCOUNT;
292 unsigned char USARTCOUNT;
293 unsigned char WDT_MS;
294 unsigned char family;
295 unsigned int processor;
296 unsigned short debugv[8];
297 unsigned char *ram;
298 unsigned short *prog;
299 unsigned short *id;
300 unsigned char *eeprom;
301 unsigned short *config;
302 unsigned int pc;
303 unsigned int jpc;
304 unsigned short lram;
305 unsigned short rram;
306 unsigned int *stack;
307 unsigned char w;
308 unsigned char wdt;
309 unsigned char s2;
310 unsigned long long cycles;
311 float freq;
312 unsigned char sleep;
313 unsigned char pkg;
314 picpin *pins;
315 unsigned char mclr;
316 ccp_t *ccp;
317 unsigned char *adc;
318 unsigned char *usart_rx;
319 unsigned char *usart_tx;
320 unsigned char pgc;
321 unsigned char pgd;
322 unsigned char sck;
323 unsigned char sdo;
324 unsigned char sdi;
325 unsigned char t0cki;
326 unsigned char t1cki;
327 unsigned char intlv;
328 // on chip debbuger
329 unsigned char ocd_pgca;
330 unsigned char ocd_pgc;
331 // periferic
332 unsigned char debug;
333 unsigned char sstep;
334 unsigned char dbg;
335 unsigned char frst;
336 // timers
337 int cp0;
338 int cp1;
339 int cp2;
340 int cp2_;
341 int cp3;
342 int t2pr;
343 // timer input
344 unsigned char t0cki_;
345 unsigned char t1cki_;
346 // interrupt
347 unsigned char int0;
348 unsigned char int1;
349 unsigned char int2;
350 // pin value
351 unsigned char int0v;
352 unsigned char int1v;
353 unsigned char int2v;
354 // portb int mismatch
355 unsigned char portbm;
356 // adc
357 float vcc;
358 int adcstep;
359 unsigned char adcon1;
360 // wdt
361 float twdt;
362 // eeprom
363 int ee_wr;
364 // ports
365 unsigned char p16latch[5];
366 unsigned char porta;
367 unsigned char portb;
368 unsigned char portc;
369 unsigned char portd;
370 unsigned char porte;
371 unsigned char portf;
372 unsigned char portg;
373 unsigned char trisa;
374 unsigned char trisb;
375 unsigned char trisc;
376 unsigned char trisd;
377 unsigned char trise;
378 unsigned char trisf;
379 unsigned char trisg;
380 unsigned char ioupdated;
381 unsigned char port_mirror;
382 // MSSP
383 unsigned char ssp_ck;
384 unsigned char ssp_sck;
385 unsigned char ssp_scka;
386 unsigned char sspsr;
387 unsigned char ssp_bit;
388
389 _serial serial[SERIAL_MAX];
390
391 void (*reset)(_pic *pic);
392 void (*mmap)(_pic *pic);
393 int (*getconf)(_pic *pic, unsigned int);
394 void (*disable_debug)(_pic *pic);
395 void (*periferic)(_pic *pic);
396 int (*interrupt)(_pic *pic);
397 void (*stop)(_pic *pic);
398
399 union
400 {
401 P16map_t P16map;
402 P16Emap_t P16Emap;
403 P18map_t P18map;
404 };
405 };
406
420 int pic_set_serial(_pic *pic_, int nser, const char *name, int flowcontrol,
421 int ctspin, int rtspin);
422
435 int pic_init(_pic *pic_, int processor, const char *fname, int leeprom,
436 float freq);
437
446 int pic_reset(_pic *pic_, int flags);
447
453
460 void pic_step(_pic *pic_);
461
466 void pic_end(_pic *pic_);
467
474 unsigned char pic_get_pin(_pic *pic_, unsigned char pin);
475
483 int pic_set_pin(_pic *pic_, unsigned char pin, unsigned char value);
484
492 int pic_set_apin(_pic *pic_, unsigned char pin, float value);
493
500 unsigned char pic_get_pin_type(_pic *pic_, unsigned char pin);
501
508 unsigned char pic_get_pin_dir(_pic *pic_, unsigned char pin);
509
516 unsigned char pic_get_pin_DOV(_pic *pic_, unsigned char pin);
517
525 int pic_set_pin_DOV(_pic *pic_, unsigned char pin, unsigned char value);
526
530 void pic_icsp_init(_pic *pic_);
531
536 int pic_icsp(_pic *pic_);
537
542
547 int pic_icsp18(_pic *pic_);
548
556 int read_ihx(_pic *pic_, const char *fname, int leeprom);
557
565 int read_ihx_16e(_pic *pic_, const char *fname, int leeprom);
566
574 int read_ihx_18(_pic *pic_, const char *fname, int leeprom);
575
582 int write_ihx(_pic *pic_, const char *fname);
583
590 int write_ihx16e(_pic *pic_, const char *fname);
591
598 int write_ihx18(_pic *pic_, const char *fname);
599
606 int getproclist(char list[][30], int size);
607
614 unsigned int getprocbyname(const char *str);
615
622 unsigned int getfprocbyname(const char *str);
623
630 unsigned int getfprocbynumber(int proc);
631
639 char *getnamebyproc(int proc, char *str);
640
647 const char *getFSRname_16(unsigned int addr);
648
655 const char *getFSRname_16E(unsigned int addr);
656
663 const char *getFSRname_16E2(unsigned int addr);
664
671 const char *getFSRname_18(unsigned int addr);
672
681 const char *getPinName(_pic *pic, int pin, char *pname);
682
689#define sfr_addr(fsr) (fsr - pic->ram)
690
691 typedef struct
692 {
693 unsigned short ID;
694 char name[30];
695 unsigned char family;
696 void (*start)(_pic *pic);
697 } pic_desc;
698
699#define PMAX 35
700
701 extern pic_desc PICS[PMAX];
702 extern int PIC_count;
703
704#define init_pic(function, family, ID) \
705 static pic_desc desc_##function = {ID, #function, family, function##_start}; \
706 static void __attribute__((constructor)) init_##function(void); \
707 static void init_##function(void) \
708 { \
709 pic_register(desc_##function); \
710 }
711
712 void pic_register(pic_desc picd);
713
714#endif
715
716#ifdef __cplusplus
717}
718#endif
void pic_icsp_init18(_pic *pic_)
Initialize P18 ICSP support.
unsigned int getfprocbynumber(int proc)
Return processor family ID by ID.
int pic_set_pin(_pic *pic_, unsigned char pin, unsigned char value)
Set digital value of one pin.
unsigned char pic_get_pin_dir(_pic *pic_, unsigned char pin)
Get dir of one pin.
int pic_init(_pic *pic_, int processor, const char *fname, int leeprom, float freq)
Initialize one pic object.
int pic_set_pin_DOV(_pic *pic_, unsigned char pin, unsigned char value)
Set Default Open Value of one pin (external PULL UP)
unsigned char pic_get_pin_type(_pic *pic_, unsigned char pin)
Get type of one pin.
const char * getFSRname_18(unsigned int addr)
Return P18 FSR name by address.
int read_ihx_18(_pic *pic_, const char *fname, int leeprom)
Read HEX file to P18 microcontroller.
int write_ihx(_pic *pic_, const char *fname)
Dump all non volatile P16 microcontroller memory to HEX file.
int pic_icsp(_pic *pic_)
Run P16 ICSP (must be pooling)
int write_ihx16e(_pic *pic_, const char *fname)
Dump all non volatile P16E microcontroller memory to HEX file.
unsigned int getfprocbyname(const char *str)
Return processor family ID by name.
void pic_end(_pic *pic_)
finalize and free pic object internal memory
const char * getPinName(_pic *pic, int pin, char *pname)
Return pin name.
int pic_icsp18(_pic *pic_)
Run P18 ICSP (must be pooling)
int read_ihx(_pic *pic_, const char *fname, int leeprom)
Read HEX file to P16 microcontroller.
unsigned char pic_get_pin(_pic *pic_, unsigned char pin)
Get digital value of one pin.
int pic_set_apin(_pic *pic_, unsigned char pin, float value)
Set digital value of one pin.
unsigned int getprocbyname(const char *str)
Return processor ID by name.
int pic_reset(_pic *pic_, int flags)
Reset the microcontroller.
const char * getFSRname_16E2(unsigned int addr)
Return P16E2 FSR name by address.
const char * getFSRname_16E(unsigned int addr)
Return P16E FSR name by address.
void pic_erase_flash(_pic *pic_)
erase the microcontroller FLASH
int getproclist(char list[][30], int size)
Return list name of supported processors.
int write_ihx18(_pic *pic_, const char *fname)
Dump all non volatile P18 microcontroller memory to HEX file.
void pic_step(_pic *pic_)
Execute one simulation step.
const char * getFSRname_16(unsigned int addr)
Return P16 FSR name by address.
char * getnamebyproc(int proc, char *str)
Return processor name by ID.
void pic_icsp_init(_pic *pic_)
Initialize P16 ICSP support.
int pic_set_serial(_pic *pic_, int nser, const char *name, int flowcontrol, int ctspin, int rtspin)
Configure the serial port connection.
int read_ihx_16e(_pic *pic_, const char *fname, int leeprom)
Read HEX file to P16E microcontroller.
unsigned char pic_get_pin_DOV(_pic *pic_, unsigned char pin)
Get Default Open Value of one pin (external PULL UP)
Definition picsim.h:281
unsigned short lram
loaded register
Definition picsim.h:304
float vcc
Microcontroller VCC used by ADC.
Definition picsim.h:357
unsigned char sleep
sleep mode on
Definition picsim.h:312
float freq
frequency
Definition picsim.h:311
unsigned int processor
procid
Definition picsim.h:295
unsigned char print
print information ON/OFF
Definition picsim.h:282
unsigned char family
pic family
Definition picsim.h:294
unsigned short rram
readed register
Definition picsim.h:305
unsigned char pkg
packaging type
Definition picsim.h:313
PICsim serial structure.
Definition picsim.h:124
PICsim P16E memory map structure.
Definition p16fxxxe_defs.h:42
PICsim P16 memory map structure.
Definition p16fxxx_defs.h:35
PICsim P18 memory map structure.
Definition p18fxxx_defs.h:37
Definition bitbang_uart.h:35
PICsim pin structure.
Definition picsim.h:112
unsigned char pin
pin
Definition picsim.h:113
unsigned char cap_ps
capture pre scaler
Definition picsim.h:115
unsigned char ovalue
old value
Definition picsim.h:114
Definition picsim.h:692
PICsim pin structure.
Definition picsim.h:93
unsigned char lvalue
latch value
Definition picsim.h:97
unsigned char ptype
pin types
Definition picsim.h:94
unsigned char lsvalue
last set value (used by open collector simulation)
Definition picsim.h:103
unsigned char value
value
Definition picsim.h:96
unsigned char dir
pin dir
Definition picsim.h:95
char pord
pin port number
Definition picsim.h:98
unsigned char * port
port address
Definition picsim.h:99
unsigned char ovalue
defaut open pin value
Definition picsim.h:101
float oavalue
analog output value
Definition picsim.h:102
float avalue
analog input value
Definition picsim.h:100