From 4e39e662bca4e52121166ee8f3b8dae33eb568d6 Mon Sep 17 00:00:00 2001 From: vrokas Date: Wed, 24 Nov 2004 00:20:50 +0000 Subject: [PATCH] Large cummulative patch for pic16 port. * device/lib/pic16/gstack.h: NEW, user can specify its own handler to call when a stack overflow occurs, * (malloc.h): added CVS Id tag, * (pic18f{242,252,442,452}.h): added T0CONbits structure and variable, * added libc directory. The current version of LibC contains string functions, ctype functions and macros and some functions of the stdlib set (like malloc/free/atof/atoi etc...). All functions are to be extensively tested in the future. Standard disclaimer here. Library is not automatically build yet. But one can build it by invoking 'make' inside the libc directory. * added ADC library under libio. Preliminary version yet. * src/pic16/gen.h: added emitTOGC macro, to toggle Carry flag, * src/pic16/gen.c (aopForRemat): asmop size is filled by aopForRemat() now and not by pic16_aopOp(), * (pic16_popGetTempReg): removed warning messgae when allocating temporary registers, its a buggy feature and will be removed, * (pic16_popGet): set register instance field in AOP_CRY, * (pic16_outBitC): fixed for results in size greater than 1, * (genUminusFloat): fixed for pic16, ported code from mcs51, * (pic16_storeForReturn): optimized return of 0, * (genCmp): experimental code for new genCmp which uses PIC18's special compare&skip instructions. Initial tests fail some times with variables grater than 1 byte in size, so new code is disabled, * (genUnpackBits, genPackBits): more optimizations in reading/writing, a single bit, * (genCast): began a fix to optimize the casting of a bit to another bit, now assigning a bitfield to another bitfield will fail, sorry, * src/pic16/main.c: disabled the use of lr-support feature, * src/pic16/pcode.h: renamed PCASMDIR to PCAD, * added some function prototypes, added function _debugf prototype, * src/pic16/pcode.c: (pic16_get_op): fixed emitting operands of register bits with offset (case PO_GPR_BIT), * (genericPrint): don't emit INFO pcode when --pcode-verbose not in command line, * (isBankInstruction): modified to return 0 for no banking instruction, and 1 for banking instruction, * (pic16_isPCinFlow): check for PCAD (assembler directives) too, caused stop processing pCodes after a inline assembly block, * (pic16_popCopyGPR2Bit): updated to match bitfields with offset, * src/pic16/pcoderegs.c: fixed a bug with eliminating some temporary registers when it shouldn't, * src/pic16/ralloc.c (allocReg): add preliminary support for supporting a limited set of temporary registers, git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@3582 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- ChangeLog | 49 ++ device/include/pic16/adc.h | 76 +++ device/include/pic16/gstack.h | 34 + device/include/pic16/malloc.h | 3 +- device/include/pic16/pic18f242.h | 15 + device/include/pic16/pic18f252.h | 15 + device/include/pic16/pic18f442.h | 15 + device/include/pic16/pic18f452.h | 15 + device/lib/pic16/libc/Makefile | 55 ++ device/lib/pic16/libc/Makefile.rules | 48 ++ device/lib/pic16/libc/ctype/Makefile | 42 ++ device/lib/pic16/libc/ctype/iscntrl.c | 31 + device/lib/pic16/libc/ctype/isdigit.c | 31 + device/lib/pic16/libc/ctype/isgraph.c | 31 + device/lib/pic16/libc/ctype/islower.c | 31 + device/lib/pic16/libc/ctype/isprint.c | 30 + device/lib/pic16/libc/ctype/ispunct.c | 37 ++ device/lib/pic16/libc/ctype/isspace.c | 36 ++ device/lib/pic16/libc/ctype/isupper.c | 31 + device/lib/pic16/libc/ctype/isxdigit.c | 33 + device/lib/pic16/libc/stdlib/Makefile | 41 ++ device/lib/pic16/libc/stdlib/atof.c | 84 +++ device/lib/pic16/libc/stdlib/atoi.c | 49 ++ device/lib/pic16/libc/stdlib/atol.c | 49 ++ device/lib/pic16/libc/stdlib/calloc.c | 31 + device/lib/pic16/libc/stdlib/free.c | 17 + device/lib/pic16/libc/stdlib/ltoa.c | 48 ++ device/lib/pic16/libc/stdlib/malloc.c | 94 +++ device/lib/pic16/libc/stdlib/realloc.c | 79 +++ device/lib/pic16/libc/string/Makefile | 60 ++ device/lib/pic16/libc/string/memccpy.c | 46 ++ device/lib/pic16/libc/string/memchr.c | 41 ++ device/lib/pic16/libc/string/memchrpgm.c | 41 ++ device/lib/pic16/libc/string/memchrram.c | 41 ++ device/lib/pic16/libc/string/memcmp.c | 36 ++ device/lib/pic16/libc/string/memcpy.c | 40 ++ device/lib/pic16/libc/string/memcpypgm2ram.c | 40 ++ device/lib/pic16/libc/string/memcpyram2ram.c | 40 ++ device/lib/pic16/libc/string/memmove.c | 55 ++ device/lib/pic16/libc/string/memrchr.c | 43 ++ device/lib/pic16/libc/string/memset.c | 36 ++ device/lib/pic16/libc/string/strcat.c | 35 ++ device/lib/pic16/libc/string/strchr.c | 33 + device/lib/pic16/libc/string/strcmp.c | 37 ++ device/lib/pic16/libc/string/strcpy.c | 33 + device/lib/pic16/libc/string/strcspn.c | 39 ++ device/lib/pic16/libc/string/strlen.c | 33 + device/lib/pic16/libc/string/strlwr.c | 38 ++ device/lib/pic16/libc/string/strncat.c | 39 ++ device/lib/pic16/libc/string/strncmp.c | 36 ++ device/lib/pic16/libc/string/strncpy.c | 33 + device/lib/pic16/libc/string/strpbrk.c | 35 ++ device/lib/pic16/libc/string/strrchr.c | 41 ++ device/lib/pic16/libc/string/strspn.c | 40 ++ device/lib/pic16/libc/string/strstr.c | 49 ++ device/lib/pic16/libc/string/strtok.c | 54 ++ device/lib/pic16/libc/string/strupr.c | 38 ++ device/lib/pic16/libdev/pic18f242.c | 2 + device/lib/pic16/libdev/pic18f252.c | 2 + device/lib/pic16/libdev/pic18f442.c | 2 + device/lib/pic16/libdev/pic18f452.c | 2 + device/lib/pic16/libio/Makefile | 54 ++ device/lib/pic16/libio/Makefile.rules | 48 ++ device/lib/pic16/libio/adc/Makefile | 39 ++ device/lib/pic16/libio/adc/adcbusy.c | 10 + device/lib/pic16/libio/adc/adcclose.c | 11 + device/lib/pic16/libio/adc/adcconv.c | 10 + device/lib/pic16/libio/adc/adcopen.c | 39 ++ device/lib/pic16/libio/adc/adcread.c | 18 + device/lib/pic16/libio/adc/adcsetch.c | 11 + device/lib/pic16/libsdcc/Makefile | 2 +- device/lib/pic16/libsdcc/char/divschar.c | 4 + device/lib/pic16/libsdcc/char/divuchar.c | 4 + device/lib/pic16/libsdcc/char/modschar.c | 7 +- device/lib/pic16/libsdcc/float/Makefile | 1 + src/pic16/gen.c | 630 +++++++++++++++++-- src/pic16/gen.h | 5 + src/pic16/genarith.c | 4 +- src/pic16/glue.c | 2 +- src/pic16/main.c | 4 +- src/pic16/pcode.c | 100 ++- src/pic16/pcode.h | 7 +- src/pic16/pcodepeep.c | 2 +- src/pic16/pcoderegs.c | 4 +- src/pic16/peeph.def | 6 - src/pic16/ralloc.c | 33 +- 86 files changed, 3232 insertions(+), 113 deletions(-) create mode 100644 device/include/pic16/adc.h create mode 100644 device/include/pic16/gstack.h create mode 100644 device/lib/pic16/libc/Makefile create mode 100644 device/lib/pic16/libc/Makefile.rules create mode 100644 device/lib/pic16/libc/ctype/Makefile create mode 100644 device/lib/pic16/libc/ctype/iscntrl.c create mode 100644 device/lib/pic16/libc/ctype/isdigit.c create mode 100644 device/lib/pic16/libc/ctype/isgraph.c create mode 100644 device/lib/pic16/libc/ctype/islower.c create mode 100644 device/lib/pic16/libc/ctype/isprint.c create mode 100644 device/lib/pic16/libc/ctype/ispunct.c create mode 100644 device/lib/pic16/libc/ctype/isspace.c create mode 100644 device/lib/pic16/libc/ctype/isupper.c create mode 100644 device/lib/pic16/libc/ctype/isxdigit.c create mode 100644 device/lib/pic16/libc/stdlib/Makefile create mode 100644 device/lib/pic16/libc/stdlib/atof.c create mode 100644 device/lib/pic16/libc/stdlib/atoi.c create mode 100644 device/lib/pic16/libc/stdlib/atol.c create mode 100644 device/lib/pic16/libc/stdlib/calloc.c create mode 100644 device/lib/pic16/libc/stdlib/free.c create mode 100644 device/lib/pic16/libc/stdlib/ltoa.c create mode 100644 device/lib/pic16/libc/stdlib/malloc.c create mode 100644 device/lib/pic16/libc/stdlib/realloc.c create mode 100644 device/lib/pic16/libc/string/Makefile create mode 100644 device/lib/pic16/libc/string/memccpy.c create mode 100644 device/lib/pic16/libc/string/memchr.c create mode 100644 device/lib/pic16/libc/string/memchrpgm.c create mode 100644 device/lib/pic16/libc/string/memchrram.c create mode 100644 device/lib/pic16/libc/string/memcmp.c create mode 100644 device/lib/pic16/libc/string/memcpy.c create mode 100644 device/lib/pic16/libc/string/memcpypgm2ram.c create mode 100644 device/lib/pic16/libc/string/memcpyram2ram.c create mode 100644 device/lib/pic16/libc/string/memmove.c create mode 100644 device/lib/pic16/libc/string/memrchr.c create mode 100644 device/lib/pic16/libc/string/memset.c create mode 100644 device/lib/pic16/libc/string/strcat.c create mode 100644 device/lib/pic16/libc/string/strchr.c create mode 100644 device/lib/pic16/libc/string/strcmp.c create mode 100644 device/lib/pic16/libc/string/strcpy.c create mode 100644 device/lib/pic16/libc/string/strcspn.c create mode 100644 device/lib/pic16/libc/string/strlen.c create mode 100644 device/lib/pic16/libc/string/strlwr.c create mode 100644 device/lib/pic16/libc/string/strncat.c create mode 100644 device/lib/pic16/libc/string/strncmp.c create mode 100644 device/lib/pic16/libc/string/strncpy.c create mode 100644 device/lib/pic16/libc/string/strpbrk.c create mode 100644 device/lib/pic16/libc/string/strrchr.c create mode 100644 device/lib/pic16/libc/string/strspn.c create mode 100644 device/lib/pic16/libc/string/strstr.c create mode 100644 device/lib/pic16/libc/string/strtok.c create mode 100644 device/lib/pic16/libc/string/strupr.c create mode 100644 device/lib/pic16/libio/Makefile create mode 100644 device/lib/pic16/libio/Makefile.rules create mode 100644 device/lib/pic16/libio/adc/Makefile create mode 100644 device/lib/pic16/libio/adc/adcbusy.c create mode 100644 device/lib/pic16/libio/adc/adcclose.c create mode 100644 device/lib/pic16/libio/adc/adcconv.c create mode 100644 device/lib/pic16/libio/adc/adcopen.c create mode 100644 device/lib/pic16/libio/adc/adcread.c create mode 100644 device/lib/pic16/libio/adc/adcsetch.c diff --git a/ChangeLog b/ChangeLog index ce7f1335..38ba3c00 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,52 @@ +2004-11-24 Vangelis Rokas + + Large cummulative patch for pic16 port. + * device/lib/pic16/gstack.h: NEW, user can specify its own handler + to call when a stack overflow occurs, + * (malloc.h): added CVS Id tag, + * (pic18f{242,252,442,452}.h): added T0CONbits structure and + variable, + * added libc directory. The current version of LibC contains string + functions, ctype functions and macros and some functions of the + stdlib set (like malloc/free/atof/atoi etc...). All functions are to + be extensively tested in the future. Standard disclaimer here. + Library is not automatically build yet. But one can build it by + invoking 'make' inside the libc directory. + * added ADC library under libio. Preliminary version yet. + + * src/pic16/gen.h: added emitTOGC macro, to toggle Carry flag, + * src/pic16/gen.c (aopForRemat): asmop size is filled by + aopForRemat() now and not by pic16_aopOp(), + * (pic16_popGetTempReg): removed warning messgae when allocating + temporary registers, its a buggy feature and will be removed, + * (pic16_popGet): set register instance field in AOP_CRY, + * (pic16_outBitC): fixed for results in size greater than 1, + * (genUminusFloat): fixed for pic16, ported code from mcs51, + * (pic16_storeForReturn): optimized return of 0, + * (genCmp): experimental code for new genCmp which uses PIC18's + special compare&skip instructions. Initial tests fail some times + with variables grater than 1 byte in size, so new code is disabled, + * (genUnpackBits, genPackBits): more optimizations in reading/writing, + a single bit, + * (genCast): began a fix to optimize the casting of a bit to another + bit, now assigning a bitfield to another bitfield will fail, sorry, + * src/pic16/main.c: disabled the use of lr-support feature, + * src/pic16/pcode.h: renamed PCASMDIR to PCAD, + * added some function prototypes, added function _debugf prototype, + * src/pic16/pcode.c: (pic16_get_op): fixed emitting operands of register + bits with offset (case PO_GPR_BIT), + * (genericPrint): don't emit INFO pcode when --pcode-verbose not in + command line, + * (isBankInstruction): modified to return 0 for no banking instruction, + and 1 for banking instruction, + * (pic16_isPCinFlow): check for PCAD (assembler directives) too, + caused stop processing pCodes after a inline assembly block, + * (pic16_popCopyGPR2Bit): updated to match bitfields with offset, + * src/pic16/pcoderegs.c: fixed a bug with eliminating some temporary + registers when it shouldn't, + * src/pic16/ralloc.c (allocReg): add preliminary support for + supporting a limited set of temporary registers, + 2004-11-23 Erik Petrich * src/hc08/gen.c (genAssign, genPointerGetSetOfs, genDataPointerGet, diff --git a/device/include/pic16/adc.h b/device/include/pic16/adc.h new file mode 100644 index 00000000..66a3cdf2 --- /dev/null +++ b/device/include/pic16/adc.h @@ -0,0 +1,76 @@ + +/* + * A/D conversion module library header + * + * Devices implemented: + * PIC18F[24][45][28] + * + */ + +/* +** $Id$ +*/ + +#ifndef __ADC_H__ +#define __ADC_H__ + + +/* interrupt on/off flag */ +#define ADC_INT_OFF 0x00 +#define ADC_INT_ON 0x01 + + +/* output format */ +#define ADC_FRM_RJUST 0x80 +#define ADC_FRM_LJUST 0x00 + +/* oscillator frequency */ +#define ADC_FOSC_2 0x00 +#define ADC_FOSC_4 0x10 +#define ADC_FOSC_16 0x11 +#define ADC_FOSC_32 0x02 +#define ADC_FOSC_64 0x12 +#define ADC_FOSC_RC 0x03 + + +/* channel selection */ +#define ADC_CHN_1 0x00 +#define ADC_CHN_2 0x01 +#define ADC_CHN_3 0x03 +#define ADC_CHN_4 0x04 +#define ADC_CHN_5 0x05 +#define ADC_CHN_6 0x06 +#define ADC_CHN_7 0x07 + + +/* reference and pin configuration */ +#define ADC_CFG_8A_0R 0x00 +#define ADC_CFG_7A_1R 0x01 +#define ADC_CFG_5A_0R 0x02 +#define ADC_CFG_4A_1R 0x03 +#define ADC_CFG_3A_0R 0x04 +#define ADC_CFG_2A_1R 0x05 +#define ADC_CFG_0A_0R 0x06 +#define ADC_CFG_6A_2R 0x08 +#define ADC_CFG_6A_0R 0x09 +#define ADC_CFG_5A_1R 0x0a +#define ADC_CFG_4A_2R 0x0b +#define ADC_CFG_3A_2R 0x0c +#define ADC_CFG_2A_2R 0x0d +#define ADC_CFG_1A_0R 0x0e +#define ADC_CFG_1A_2R 0x0f + + +void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config); + +void adc_close(void); + +void adc_conv(void); + +char adc_busy(void); + +int adc_read(void); + +void adc_setchannel(unsigned char channel); + +#endif diff --git a/device/include/pic16/gstack.h b/device/include/pic16/gstack.h new file mode 100644 index 00000000..f5ab76dd --- /dev/null +++ b/device/include/pic16/gstack.h @@ -0,0 +1,34 @@ +/*------------------------------------------------------------------------- + + gstack.h :- debug stack tracing header + + Written for pic16 port by Vangelis Rokas, 2004 (vrokas@otenet.gr) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +/* +** $Id$ +*/ + +#ifndef __PIC16_GSTACK_H__ +#define __PIC16_GSTACK_H__ + +extern void (* _gstack_overflow_handler)(void); + +#endif /* __PIC16_GSTACK_H__ */ diff --git a/device/include/pic16/malloc.h b/device/include/pic16/malloc.h index 85941608..0fc3636e 100644 --- a/device/include/pic16/malloc.h +++ b/device/include/pic16/malloc.h @@ -7,7 +7,8 @@ /* ** $Id$ -*/ + */ + /* * Structure of memory block header: diff --git a/device/include/pic16/pic18f242.h b/device/include/pic16/pic18f242.h index d1560a4e..37b31ce4 100644 --- a/device/include/pic16/pic18f242.h +++ b/device/include/pic16/pic18f242.h @@ -636,6 +636,21 @@ typedef union { extern volatile __OSCCONbits_t at 0xfd3 OSCCONbits; extern sfr at 0xfd5 T0CON; +typedef union { + struct { + unsigned T0PS0:1; + unsigned T0PS1:1; + unsigned T0PS2:1; + unsigned PSA:1; + unsigned T0SE:1; + unsigned T0CS:1; + unsigned T08BIT:1; + unsigned TMR0ON:1; + }; +} __T0CONbits_t; + +extern volatile __T0CONbits_t at 0xfd5 T0CONbits; + extern sfr at 0xfd6 TMR0L; extern sfr at 0xfd7 TMR0H; extern sfr at 0xfd8 STATUS; diff --git a/device/include/pic16/pic18f252.h b/device/include/pic16/pic18f252.h index f4551ef1..66804d29 100644 --- a/device/include/pic16/pic18f252.h +++ b/device/include/pic16/pic18f252.h @@ -636,6 +636,21 @@ typedef union { extern volatile __OSCCONbits_t at 0xfd3 OSCCONbits; extern sfr at 0xfd5 T0CON; +typedef union { + struct { + unsigned T0PS0:1; + unsigned T0PS1:1; + unsigned T0PS2:1; + unsigned PSA:1; + unsigned T0SE:1; + unsigned T0CS:1; + unsigned T08BIT:1; + unsigned TMR0ON:1; + }; +} __T0CONbits_t; + +extern volatile __T0CONbits_t at 0xfd5 T0CONbits; + extern sfr at 0xfd6 TMR0L; extern sfr at 0xfd7 TMR0H; extern sfr at 0xfd8 STATUS; diff --git a/device/include/pic16/pic18f442.h b/device/include/pic16/pic18f442.h index 6efc0297..11976f74 100644 --- a/device/include/pic16/pic18f442.h +++ b/device/include/pic16/pic18f442.h @@ -765,6 +765,21 @@ typedef union { extern volatile __OSCCONbits_t at 0xfd3 OSCCONbits; extern sfr at 0xfd5 T0CON; +typedef union { + struct { + unsigned T0PS0:1; + unsigned T0PS1:1; + unsigned T0PS2:1; + unsigned PSA:1; + unsigned T0SE:1; + unsigned T0CS:1; + unsigned T08BIT:1; + unsigned TMR0ON:1; + }; +} __T0CONbits_t; + +extern volatile __T0CONbits_t at 0xfd5 T0CONbits; + extern sfr at 0xfd6 TMR0L; extern sfr at 0xfd7 TMR0H; extern sfr at 0xfd8 STATUS; diff --git a/device/include/pic16/pic18f452.h b/device/include/pic16/pic18f452.h index fa495374..ea7d53a0 100644 --- a/device/include/pic16/pic18f452.h +++ b/device/include/pic16/pic18f452.h @@ -765,6 +765,21 @@ typedef union { extern volatile __OSCCONbits_t at 0xfd3 OSCCONbits; extern sfr at 0xfd5 T0CON; +typedef union { + struct { + unsigned T0PS0:1; + unsigned T0PS1:1; + unsigned T0PS2:1; + unsigned PSA:1; + unsigned T0SE:1; + unsigned T0CS:1; + unsigned T08BIT:1; + unsigned TMR0ON:1; + }; +} __T0CONbits_t; + +extern volatile __T0CONbits_t at 0xfd5 T0CONbits; + extern sfr at 0xfd6 TMR0L; extern sfr at 0xfd7 TMR0H; extern sfr at 0xfd8 STATUS; diff --git a/device/lib/pic16/libc/Makefile b/device/lib/pic16/libc/Makefile new file mode 100644 index 00000000..1d54abcc --- /dev/null +++ b/device/lib/pic16/libc/Makefile @@ -0,0 +1,55 @@ +# +# Makefile - Makefile to build pic16 support libraries +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + + +DIRS = ctype \ + stdlib \ + string + +LIB = libp18c.lib + +all: build-libraries + +make-target: build-libraries + +build-libraries: + for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir ; \ + done; + gplib -c $(LIB) string/*.o + mv -v $(LIB) ../bin + + +clean-intermediate: + @for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir clean-intermediate ; \ + done ; + +clean: + for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir clean; \ + done ; + rm -fv $(LIB) + + +real-clean: clean + find -name *.adb -print | xargs -- rm -fv ; + find -name *.p -print | xargs -- rm -fv ; + find -name *.d -print | xargs -- rm -fv ; + find -name *.dump* -print | xargs -- rm -fv ; + + +dep: + for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir dep; \ + done diff --git a/device/lib/pic16/libc/Makefile.rules b/device/lib/pic16/libc/Makefile.rules new file mode 100644 index 00000000..c571aaf2 --- /dev/null +++ b/device/lib/pic16/libc/Makefile.rules @@ -0,0 +1,48 @@ +# +# Makefile.rules - Common Makefile rules to build pic16 C Library +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + + +include ../../Makefile.common + +PRJDIR = ../../../../.. + +LIBC_INC_DIR = $(PRJDIR)/device/include/pic16 + +COMPILE_FLAGS += $(MODELFLAGS) --pomit-config-words --pomit-ivt --no-peep --i-code-in-asm +CFLAGS = -I$(LIBC_INC_DIR) + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + +%.o: %.c + $(CC) $(CFLAGS) $(COMPILE_FLAGS) -c $< + + +all: build-library + +clean-intermediate: + $(RM) -f *.lst *.asm *.dump* *.p *.d *.adb + + +clean: clean-intermediate + $(RM) -f $(LIB) *.o + +dep .depend: + rm -f .depend + for temp in $(CFILES); do \ + $(CPP) $(MM) $(CFLAGS) $$temp > .tmpdepend; \ + $(SED) s/.rel/.o/g .tmpdepend >> .depend; \ + $(RM) -f .tmpdepend; \ + done; + +include .depend diff --git a/device/lib/pic16/libc/ctype/Makefile b/device/lib/pic16/libc/ctype/Makefile new file mode 100644 index 00000000..e8160f82 --- /dev/null +++ b/device/lib/pic16/libc/ctype/Makefile @@ -0,0 +1,42 @@ +# +# Makefile - Makefile to build pic16 C Library +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + +include ../Makefile.rules + +SRCS = iscntrl \ + isdigit \ + isgraph \ + islower \ + isprint \ + ispunct \ + isspace \ + isupper \ + isxdigit + + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + + +DEBUG= +#COMPILE_FLAGS += --pomit-config-words --pomit-ivt --no-peep +COMPILE_FLAGS += $(DEBUG) + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + +%.o: %.c + $(CC) $(CFLAGS) $(COMPILE_FLAGS) -c $< + +build-library: $(OFILES) + diff --git a/device/lib/pic16/libc/ctype/iscntrl.c b/device/lib/pic16/libc/ctype/iscntrl.c new file mode 100644 index 00000000..4e52696b --- /dev/null +++ b/device/lib/pic16/libc/ctype/iscntrl.c @@ -0,0 +1,31 @@ +/*------------------------------------------------------------------------- + _iscntrl.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +char iscntrl(unsigned char c) +{ + if ( c <= '\x1F' || c == '\x7f') + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/isdigit.c b/device/lib/pic16/libc/ctype/isdigit.c new file mode 100644 index 00000000..7510d2cd --- /dev/null +++ b/device/lib/pic16/libc/ctype/isdigit.c @@ -0,0 +1,31 @@ +/*------------------------------------------------------------------------- + _isdigit.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +char isdigit( unsigned char c) +{ + if ( c >= '0' && c <= '9' ) + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/isgraph.c b/device/lib/pic16/libc/ctype/isgraph.c new file mode 100644 index 00000000..2ec902fb --- /dev/null +++ b/device/lib/pic16/libc/ctype/isgraph.c @@ -0,0 +1,31 @@ +/*------------------------------------------------------------------------- + _isgraph.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +char isgraph( unsigned char c) +{ + if ( c >= '\x21' && c <= '\x7e' ) + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/islower.c b/device/lib/pic16/libc/ctype/islower.c new file mode 100644 index 00000000..3b9fc434 --- /dev/null +++ b/device/lib/pic16/libc/ctype/islower.c @@ -0,0 +1,31 @@ +/*------------------------------------------------------------------------- + _islower.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +char islower(unsigned char c) +{ + if( c >= 'a' && c <= 'z' ) + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/isprint.c b/device/lib/pic16/libc/ctype/isprint.c new file mode 100644 index 00000000..4b23b275 --- /dev/null +++ b/device/lib/pic16/libc/ctype/isprint.c @@ -0,0 +1,30 @@ +/*------------------------------------------------------------------------- + _isprint.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +char isprint (unsigned char c) +{ + if ( c >= '\x20' && c <= '\x7e') + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/ispunct.c b/device/lib/pic16/libc/ctype/ispunct.c new file mode 100644 index 00000000..0447eb9e --- /dev/null +++ b/device/lib/pic16/libc/ctype/ispunct.c @@ -0,0 +1,37 @@ +/*------------------------------------------------------------------------- + _ispunct.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +#include + +char ispunct (unsigned char c) +{ + if (isprint (c) + && !islower(c) + && !isupper(c) + && c != ' ' + && !isdigit(c)) + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/isspace.c b/device/lib/pic16/libc/ctype/isspace.c new file mode 100644 index 00000000..126e246b --- /dev/null +++ b/device/lib/pic16/libc/ctype/isspace.c @@ -0,0 +1,36 @@ +/*------------------------------------------------------------------------- + _isppace.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +char isspace (unsigned char c) +{ + if ( c == ' ' + || c == '\f' + || c == '\n' + || c == '\r' + || c == '\t' + || c == '\v' ) + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/isupper.c b/device/lib/pic16/libc/ctype/isupper.c new file mode 100644 index 00000000..d26f749a --- /dev/null +++ b/device/lib/pic16/libc/ctype/isupper.c @@ -0,0 +1,31 @@ +/*------------------------------------------------------------------------- + _isupper.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +char isupper (unsigned char c) +{ + if ( c >= 'A' && c <= 'Z') + return 1; + + return 0; +} diff --git a/device/lib/pic16/libc/ctype/isxdigit.c b/device/lib/pic16/libc/ctype/isxdigit.c new file mode 100644 index 00000000..a0dc943f --- /dev/null +++ b/device/lib/pic16/libc/ctype/isxdigit.c @@ -0,0 +1,33 @@ +/*------------------------------------------------------------------------- + _isxdigit.c - part of ctype.h + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +char isxdigit (unsigned char c) +{ + if (( c >= '0' && c <= '9') + || ( c >= 'a' && c <= 'f') + || ( c >= 'A' && c <= 'F')) + return 1; + + return 0; +} + diff --git a/device/lib/pic16/libc/stdlib/Makefile b/device/lib/pic16/libc/stdlib/Makefile new file mode 100644 index 00000000..e905dc94 --- /dev/null +++ b/device/lib/pic16/libc/stdlib/Makefile @@ -0,0 +1,41 @@ +# +# Makefile - Makefile to build pic16 C Library +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + +include ../Makefile.rules + +SRCS = atof \ + atoi \ + atol \ + calloc \ + free \ + ltoa \ + malloc \ + realloc + + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + + +DEBUG= +#COMPILE_FLAGS += --pomit-config-words --pomit-ivt --no-peep +COMPILE_FLAGS += $(DEBUG) + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + +%.o: %.c + $(CC) $(CFLAGS) $(COMPILE_FLAGS) -c $< + +build-library: $(OFILES) + diff --git a/device/lib/pic16/libc/stdlib/atof.c b/device/lib/pic16/libc/stdlib/atof.c new file mode 100644 index 00000000..0548c729 --- /dev/null +++ b/device/lib/pic16/libc/stdlib/atof.c @@ -0,0 +1,84 @@ +/* atof.c: converts an ASCII string to float + + Copyright (C) 2003 Jesus Calvino-Fraga, jesusc@ieee.org + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include +#include + +float atof(char * s) +{ + float value, fraction; + char iexp; + char sign; + + //Skip leading blanks + while (isspace(*s)) s++; + + //Get the sign + if (*s == '-') + { + sign=1; + s++; + } + else + { + sign=0; + if (*s == '+') s++; + } + + //Get the integer part + for (value=0.0; isdigit(*s); s++) + { + value=10.0*value+(*s-'0'); + } + + //Get the fraction + if (*s == '.') + { + s++; + for (fraction=0.1; isdigit(*s); s++) + { + value+=(*s-'0')*fraction; + fraction*=0.1; + } + } + + //Finally, the exponent (not very efficient, but enough for now*/ + if (toupper(*s)=='E') + { + s++; + iexp=(char)atoi(s); + { + while(iexp!=0) + { + if(iexp<0) + { + value*=0.1; + iexp++; + } + else + { + value*=10.0; + iexp--; + } + } + } + } + + if(sign) value*=-1.0; + return (value); +} diff --git a/device/lib/pic16/libc/stdlib/atoi.c b/device/lib/pic16/libc/stdlib/atoi.c new file mode 100644 index 00000000..334cf11f --- /dev/null +++ b/device/lib/pic16/libc/stdlib/atoi.c @@ -0,0 +1,49 @@ +/*--------------------------------------------------------------------- + atoi - convert a string to an integer and return it + + written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +int atoi(char * s) +{ + register int rv=0; + register char sign = 0; + + /* skip till we find either a digit or '+' or '-' */ + while (*s) { + if (*s <= '9' && *s >= '0') + break; + if (*s == '-' || *s == '+') + break; + s++; + } + + sign = (*s == '-'); + if (*s == '-' || *s == '+') s++; + + while (*s && *s >= '0' && *s <= '9') { + rv = (rv * 10) + (*s - '0'); + s++; + } + + return (sign ? -rv : rv); +} + diff --git a/device/lib/pic16/libc/stdlib/atol.c b/device/lib/pic16/libc/stdlib/atol.c new file mode 100644 index 00000000..794f8995 --- /dev/null +++ b/device/lib/pic16/libc/stdlib/atol.c @@ -0,0 +1,49 @@ +/*--------------------------------------------------------------------- + atol - convert a string to long integer and return it + + written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +long atol(char * s) +{ + register long rv=0; + register char sign = 0; + + /* skip till we find either a digit or '+' or '-' */ + while (*s) { + if (*s <= '9' && *s >= '0') + break; + if (*s == '-' || *s == '+') + break; + s++; + } + + sign = (*s == '-'); + if (*s == '-' || *s == '+') s++; + + while (*s && *s >= '0' && *s <= '9') { + rv = (rv * 10) + (*s - '0'); + s++; + } + + return (sign ? -rv : rv); +} + diff --git a/device/lib/pic16/libc/stdlib/calloc.c b/device/lib/pic16/libc/stdlib/calloc.c new file mode 100644 index 00000000..e888daae --- /dev/null +++ b/device/lib/pic16/libc/stdlib/calloc.c @@ -0,0 +1,31 @@ +/* + * malloc.c - dynamic memory allocation + * + * written by Vangelis Rokas, 2004 (vrokas@otenet.gr) + * + */ + +#include "malloc.h" + +extern unsigned char *_dynamicHeap; /* pointer to heap */ + +unsigned char *calloc(unsigned char num) //, unsigned char len) +{ + unsigned char len=num; + unsigned char total; + unsigned char *result, *ch; + + total = num * len; + if(total > MAX_BLOCK_SIZE)return ((unsigned char *)0); + result = ch = malloc( (char)(total) ); + + if(result != 0) { + while(total) { + total--; + *ch = 0; + ch++; + } + } + + return (result); +} diff --git a/device/lib/pic16/libc/stdlib/free.c b/device/lib/pic16/libc/stdlib/free.c new file mode 100644 index 00000000..4f6ec86f --- /dev/null +++ b/device/lib/pic16/libc/stdlib/free.c @@ -0,0 +1,17 @@ +/* + * malloc.c - dynamic memory allocation + * + * written by Vangelis Rokas, 2004 (vrokas@otenet.gr) + * + */ + +#include "malloc.h" + +extern char *_dynamicHeap; /* pointer to heap */ + +void free(unsigned char *buf) +{ + /* mark block as deallocated */ + ((_malloc_rec *)(buf - 1))->bits.alloc = 0; +} + diff --git a/device/lib/pic16/libc/stdlib/ltoa.c b/device/lib/pic16/libc/stdlib/ltoa.c new file mode 100644 index 00000000..30d7772c --- /dev/null +++ b/device/lib/pic16/libc/stdlib/ltoa.c @@ -0,0 +1,48 @@ +/*------------------------------------------------------------------------- + integer to string conversion + + Written by: Bela Torok, 1999 + bela.torok@kssg.ch + usage: + + _ultoa(unsigned long value, char* string, int radix) + _ltoa(long value, char* string, int radix) + + value -> Number to be converted + string -> Result + radix -> Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex) +---------------------------------------------------------------------------*/ + +#define NUMBER_OF_DIGITS 32 + +void ultoa(unsigned long value, data char* string, unsigned char radix) +{ +unsigned char index; +char buffer[NUMBER_OF_DIGITS]; /* space for NUMBER_OF_DIGITS + '\0' */ + + index = NUMBER_OF_DIGITS; + + do { + buffer[--index] = '0' + (value % radix); + if ( buffer[index] > '9') buffer[index] += 'A' - '9' - 1; + value /= radix; + } while (value != 0); + + do { + *string++ = buffer[index++]; + } while ( index < NUMBER_OF_DIGITS ); + + *string = 0; /* string terminator */ +} + +void ltoa(long value, data char* string, unsigned char radix) +{ + if (value < 0 && radix == 10) { + *string++ = '-'; + ultoa(-value, string, radix); + } + else { + ultoa(value, string, radix); + } +} + diff --git a/device/lib/pic16/libc/stdlib/malloc.c b/device/lib/pic16/libc/stdlib/malloc.c new file mode 100644 index 00000000..7c0a48a0 --- /dev/null +++ b/device/lib/pic16/libc/stdlib/malloc.c @@ -0,0 +1,94 @@ +/* + * malloc.c - dynamic memory allocation + * + * written by Vangelis Rokas, 2004 (vrokas@otenet.gr) + * + */ + +#include "malloc.h" + +extern unsigned char *_dynamicHeap; /* pointer to heap */ + +unsigned char *malloc(unsigned char len) +{ + _malloc_rec *pHeap; /* pointer to block header */ + _malloc_rec *temp; + unsigned char bLen; /* size of block */ + unsigned char eLen; + + if(len > _MAX_HEAP_SIZE) + return ((unsigned char *)0); + + pHeap = (_malloc_rec *)&_dynamicHeap; + + + while(1) { + bLen = pHeap->bits.count; + + /* if datum is zero, then last block, return NULL */ + if(pHeap->datum == 0) + return ((unsigned char *)0); + + /* if current block is allocated then proceed to next */ + if(pHeap->bits.alloc) { + pHeap += pHeap->bits.count; + continue; + } + + + /* current block is not allocated, try to allocate */ + + /* if current block is not enough for allocation, then proceed to next */ + if(bLen <= len) { + + /* current block is not enough see if we can merge some adjacent + * memory blocks to make it fit */ + temp = pHeap + pHeap->bits.count; + eLen = bLen; + while((temp->datum) && (!temp->bits.alloc) && (eLen < len)) { + eLen += temp->bits.count; + temp += temp->bits.count; + } + + if(eLen >= len) { + int i; + /* yes, there are some free blocks that can be merged, merge them... */ + + temp = pHeap; + while(eLen > 0) { + if(eLen > MAX_BLOCK_SIZE)i = MAX_BLOCK_SIZE; + else i = eLen; + + temp->bits.count = i; + temp->bits.alloc = 0; + + temp += i; + eLen -= i; + } + + bLen = pHeap->bits.count; + } else { + /* otherwise proceed with next block */ + pHeap += pHeap->bits.count; + continue; + } + } + + + /* current block is enough to hold the new block */ + + /* allocate by filling the fields */ + pHeap->bits.count = len+1; + pHeap->bits.alloc = 1; + + if(bLen > len+1) { + /* if current block size is greater than the requested one, + * create a new empty block at the end of the newly allocated */ + temp = pHeap + len+1; + temp->bits.count = bLen - len - 1; + temp->bits.alloc = 0; + } + + return ((unsigned char *)pHeap + 1); + } +} diff --git a/device/lib/pic16/libc/stdlib/realloc.c b/device/lib/pic16/libc/stdlib/realloc.c new file mode 100644 index 00000000..29705a7a --- /dev/null +++ b/device/lib/pic16/libc/stdlib/realloc.c @@ -0,0 +1,79 @@ +/* + * malloc.c - dynamic memory allocation + * + * written by Vangelis Rokas, 2004 (vrokas@otenet.gr) + * + */ + +#include "malloc.h" + +extern unsigned char *_dynamicHeap; /* pointer to heap */ + +unsigned char *realloc(unsigned char *mblock, unsigned char len) +{ + _malloc_rec *pHeap; /* pointer to block header */ + _malloc_rec *temp; + unsigned char bLen; /* size of block */ + unsigned char eLen; + + if(len > MAX_BLOCK_SIZE) + return ((unsigned char *)0); + + len++; /* increase to count header too */ + + pHeap = (_malloc_rec *)(mblock-1); + bLen = pHeap->bits.count; + + /* new size is same as old, return pointer */ + if(bLen == len)return (mblock); + + if(bLen > len) { + /* new segment is smaller than the old one, that's easy! */ + pHeap->bits.count = len; + temp = pHeap + len; + temp->bits.alloc = 0; + temp->bits.count = bLen - len; + + return (((unsigned char *)pHeap) + 1); + } + + /* so, new segment has size bigger than the old one + * we can only return a valid pointer only when after + * the block there is an empty block that can be merged + * to produce a new block of the requested size, otherwise + * we return NULL */ + + temp = pHeap + pHeap->bits.count; + eLen = bLen; + while((temp->datum) && (!temp->bits.alloc) && (eLen < len)) { + eLen += temp->bits.count; + temp += temp->bits.count; + } + + if(eLen >= len) { + int i; + /* so we found one, adjust memory blocks */ + temp = pHeap; + + temp->bits.count = len; + eLen -= len; + temp += len; + + while(eLen>0) { + if(eLen > MAX_BLOCK_SIZE)i = MAX_BLOCK_SIZE; + else i = eLen; + + temp->bits.count = i; + temp->bits.alloc = 0; + temp += i; + eLen -= i; + } + + return (((unsigned char *)pHeap) + 1); + } + + + /* no could not find a valid block, return NULL */ + + return ((unsigned char *)0); +} diff --git a/device/lib/pic16/libc/string/Makefile b/device/lib/pic16/libc/string/Makefile new file mode 100644 index 00000000..93b5c504 --- /dev/null +++ b/device/lib/pic16/libc/string/Makefile @@ -0,0 +1,60 @@ +# +# Makefile - Makefile to build pic16 C Library +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + +include ../Makefile.rules + +SRCS = memchr \ + memcmp \ + memcpy \ + memccpy \ + memmove \ + memrchr \ + memset \ + strcat \ + strchr \ + strcmp \ + strcpy \ + strcspn \ + strlen \ + strlwr \ + strncat \ + strncmp \ + strncpy \ + strpbrk \ + strrchr \ + strspn \ + strstr \ + strtok \ + strupr \ + memchrpgm \ + memchrram \ + memcpypgm2ram \ + memcpyram2ram \ + + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + + +DEBUG= +#COMPILE_FLAGS += --pomit-config-words --pomit-ivt --no-peep +COMPILE_FLAGS += $(DEBUG) + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + +%.o: %.c + $(CC) $(CFLAGS) $(COMPILE_FLAGS) -c $< + +build-library: $(OFILES) + diff --git a/device/lib/pic16/libc/string/memccpy.c b/device/lib/pic16/libc/string/memccpy.c new file mode 100644 index 00000000..9e8d03f0 --- /dev/null +++ b/device/lib/pic16/libc/string/memccpy.c @@ -0,0 +1,46 @@ +/*------------------------------------------------------------------------- + memccpy.c - part of string library functions + + Written by Vangelis Rokas, 2004 + + Based on source + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +void *memccpy (void *dst, void *src, int c, size_t acount) +{ + void *ret = dst; + char *d = dst; + char *s = src; + + /* + * copy from lower addresses to higher addresses + */ + while (acount--) { + if( *s == c) + return (++s); + + *d++ = *s++; + } + + return((void *)0x00); +} diff --git a/device/lib/pic16/libc/string/memchr.c b/device/lib/pic16/libc/string/memchr.c new file mode 100644 index 00000000..2b3130c7 --- /dev/null +++ b/device/lib/pic16/libc/string/memchr.c @@ -0,0 +1,41 @@ +/*------------------------------------------------------------------------- + memchr.c - part of string library functions + + Written by Vangelis Rokas 2004 + + Based on source + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +#include + +void *memchr(void *s, char c, size_t count) +{ + if(!count)return (void *)0x00; + + while((*(char *)s != c) && (count)) { + s = (char *)s + sizeof(char *); + count--; + } + + if(count)return s; + else return (void *)0x00; +} diff --git a/device/lib/pic16/libc/string/memchrpgm.c b/device/lib/pic16/libc/string/memchrpgm.c new file mode 100644 index 00000000..e06b06ab --- /dev/null +++ b/device/lib/pic16/libc/string/memchrpgm.c @@ -0,0 +1,41 @@ +/*------------------------------------------------------------------------- + memchrpgm.c - part of string library functions + + Written by Vangelis Rokas 2004 + + Based on source + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +#include + +code void *memchrpgm(code void *s, char c, size_t count) +{ + if(!count)return (void *)0x00; + + while((*(code char *)s != c) && (count)) { + s = (code char *)s + sizeof(code char *); + count--; + } + + if(count)return s; + else return (void *)0x00; +} diff --git a/device/lib/pic16/libc/string/memchrram.c b/device/lib/pic16/libc/string/memchrram.c new file mode 100644 index 00000000..157f856d --- /dev/null +++ b/device/lib/pic16/libc/string/memchrram.c @@ -0,0 +1,41 @@ +/*------------------------------------------------------------------------- + memchrram.c - part of string library functions + + Written by Vangelis Rokas 2004 + + Based on source + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +#include + +data void *memchrram(data void *s, char c, size_t count) +{ + if(!count)return (void *)0x00; + + while((*(data char *)s != c) && (count)) { + s = (data char *)s + sizeof(data char *); + count--; + } + + if(count)return s; + else return (void *)0x00; +} diff --git a/device/lib/pic16/libc/string/memcmp.c b/device/lib/pic16/libc/string/memcmp.c new file mode 100644 index 00000000..27f2d655 --- /dev/null +++ b/device/lib/pic16/libc/string/memcmp.c @@ -0,0 +1,36 @@ +/*------------------------------------------------------------------------- + memcmp.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +int memcmp (void * buf1, void * buf2, size_t count) +{ + if(!count)return(0); + + while (--count && *((char *)buf1) == *((char *)buf2) ) { + buf1 = (char *)buf1 + 1; + buf2 = (char *)buf2 + 1; + } + + return( *((unsigned char *)buf1) - *((unsigned char *)buf2) ); +} diff --git a/device/lib/pic16/libc/string/memcpy.c b/device/lib/pic16/libc/string/memcpy.c new file mode 100644 index 00000000..495f4004 --- /dev/null +++ b/device/lib/pic16/libc/string/memcpy.c @@ -0,0 +1,40 @@ +/*------------------------------------------------------------------------- + memcpy.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +void *memcpy (void *dst, void *src, size_t acount) +{ + void *ret = dst; /* perhaps ret should be removed, use dst instead */ + char *d = dst; + char *s = src; + + /* + * copy from lower addresses to higher addresses + */ + while (acount--) { + *d++ = *s++; + } + + return(ret); +} diff --git a/device/lib/pic16/libc/string/memcpypgm2ram.c b/device/lib/pic16/libc/string/memcpypgm2ram.c new file mode 100644 index 00000000..009cd5c4 --- /dev/null +++ b/device/lib/pic16/libc/string/memcpypgm2ram.c @@ -0,0 +1,40 @@ +/*------------------------------------------------------------------------- + memcpypgm2ram.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +void *memcpypgm2ram (data void *dst, void code *src, size_t acount) +{ + void *ret = dst; /* perhaps ret should be removed, use dst instead */ + char data *d = dst; + char *s = src; + + /* + * copy from lower addresses to higher addresses + */ + while (acount--) { + *d++ = *s++; + } + + return(ret); +} diff --git a/device/lib/pic16/libc/string/memcpyram2ram.c b/device/lib/pic16/libc/string/memcpyram2ram.c new file mode 100644 index 00000000..c8a2101c --- /dev/null +++ b/device/lib/pic16/libc/string/memcpyram2ram.c @@ -0,0 +1,40 @@ +/*------------------------------------------------------------------------- + memcpypgm2ram.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +data void *memcpyram2ram (data void *dst, data void *src, size_t acount) +{ + void data *ret = dst; /* perhaps ret should be removed, use dst instead */ + char data *d = dst; + char data *s = src; + + /* + * copy from lower addresses to higher addresses + */ + while (acount--) { + *d++ = *s++; + } + + return(ret); +} diff --git a/device/lib/pic16/libc/string/memmove.c b/device/lib/pic16/libc/string/memmove.c new file mode 100644 index 00000000..91bc6578 --- /dev/null +++ b/device/lib/pic16/libc/string/memmove.c @@ -0,0 +1,55 @@ +/*------------------------------------------------------------------------- + _memmove.c - part of string library functions + + Adapted By - Erik Petrich . epetrich@users.sourceforge.net + from _memcpy.c which was originally + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +void *memmove (void *dst, void *src, size_t acount) +{ + void *ret = dst; + char *d; + char *s; + + if(((int)src < (int)dst) && ((((int)src)+acount) > (int)dst)) { + /* + * copy from higher addresses to lower addresses + */ + d = ((char *)dst)+acount-1; + s = ((char *)src)+acount-1; + while (acount--) { + *d-- = *s--; + } + } else { + /* + * copy from lower addresses to higher addresses + */ + d = dst; + s = src; + while (acount--) { + *d++ = *s++; + } + } + + return(ret); +} diff --git a/device/lib/pic16/libc/string/memrchr.c b/device/lib/pic16/libc/string/memrchr.c new file mode 100644 index 00000000..5e53c40a --- /dev/null +++ b/device/lib/pic16/libc/string/memrchr.c @@ -0,0 +1,43 @@ +/*------------------------------------------------------------------------- + memrchr.c - part of string library functions + + Written by Vangelis Rokas 2004 + + Based on source + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +#include + +void *memrchr(void *s, char c, size_t count) +{ + if(!count)return (void *)0x00; + + s = (char *)s + sizeof(char *) * count; + + while((*(char *)s != c) && (count)) { + s = (char *)s - sizeof(char *); + count--; + } + + if(count)return s; + else return (void *)0x00; +} diff --git a/device/lib/pic16/libc/string/memset.c b/device/lib/pic16/libc/string/memset.c new file mode 100644 index 00000000..566cdb5f --- /dev/null +++ b/device/lib/pic16/libc/string/memset.c @@ -0,0 +1,36 @@ +/*------------------------------------------------------------------------- + memset.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +void *memset (void *buf, unsigned char ch, size_t count) +{ + register unsigned char *ret = buf; + + while (count--) { + *(unsigned char *) ret = ch; + ret++; + } + + return buf ; +} diff --git a/device/lib/pic16/libc/string/strcat.c b/device/lib/pic16/libc/string/strcat.c new file mode 100644 index 00000000..485ecbdc --- /dev/null +++ b/device/lib/pic16/libc/string/strcat.c @@ -0,0 +1,35 @@ +/*------------------------------------------------------------------------- + strncat.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strcat(char *dst, char *src) +{ + char *cp = dst; + + while( *cp )cp++; /* find end of dst */ + + while( *cp++ = *src++ ) ; /* Copy src to end of dst */ + + return( dst ); /* return dst */ +} diff --git a/device/lib/pic16/libc/string/strchr.c b/device/lib/pic16/libc/string/strchr.c new file mode 100644 index 00000000..be63989a --- /dev/null +++ b/device/lib/pic16/libc/string/strchr.c @@ -0,0 +1,33 @@ +/*------------------------------------------------------------------------- + strchr.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strchr(char *string, char ch) +{ + while(*string && *string != ch)string++; + + if (*string == ch)return(string); + + return ( NULL ); +} diff --git a/device/lib/pic16/libc/string/strcmp.c b/device/lib/pic16/libc/string/strcmp.c new file mode 100644 index 00000000..3bb0155d --- /dev/null +++ b/device/lib/pic16/libc/string/strcmp.c @@ -0,0 +1,37 @@ +/*------------------------------------------------------------------------- + strcmp.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +int strcmp(char * asrc, char * adst) +{ + register int ret = 0 ; + + while( ! (ret = *asrc - *adst) && *adst)++asrc, ++adst; + + if ( ret < 0 )ret = -1 ; + else if ( ret > 0 )ret = 1 ; + + return( ret ); +} + diff --git a/device/lib/pic16/libc/string/strcpy.c b/device/lib/pic16/libc/string/strcpy.c new file mode 100644 index 00000000..89a36a31 --- /dev/null +++ b/device/lib/pic16/libc/string/strcpy.c @@ -0,0 +1,33 @@ +/*------------------------------------------------------------------------- + strcpy.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strcpy(char *d, char *s) +{ + register char * d1 = d; + + while (*d1++ = *s++) ; + + return d; +} diff --git a/device/lib/pic16/libc/string/strcspn.c b/device/lib/pic16/libc/string/strcspn.c new file mode 100644 index 00000000..0a8383cf --- /dev/null +++ b/device/lib/pic16/libc/string/strcspn.c @@ -0,0 +1,39 @@ +/*------------------------------------------------------------------------- + strcspn.c - part of string library functions + + Bug fixed by Vangelis Rokas . vrokas AT otenet.gr (2004) + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +int strcspn (char *string, char *control) +{ + register int count = 0 ; + register char ch ; + + while (ch = *string) { + if (strchr(control,ch))break; + else count++ ; + string++; + } + + return count ; +} diff --git a/device/lib/pic16/libc/string/strlen.c b/device/lib/pic16/libc/string/strlen.c new file mode 100644 index 00000000..d33662ee --- /dev/null +++ b/device/lib/pic16/libc/string/strlen.c @@ -0,0 +1,33 @@ +/*------------------------------------------------------------------------- + strcpy.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +int strlen (char * str) +{ + register int i = 0 ; + + while (*str++)i++; + + return i; +} diff --git a/device/lib/pic16/libc/string/strlwr.c b/device/lib/pic16/libc/string/strlwr.c new file mode 100644 index 00000000..10257f57 --- /dev/null +++ b/device/lib/pic16/libc/string/strlwr.c @@ -0,0 +1,38 @@ +/*------------------------------------------------------------------------- + strlwr.c - part of string library functions + + Written by Vangelis Rokas, 2004 + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +#include +#include + +char *strlwr(char *str) +{ + char *ret = str; + + while(*str) { + *str = tolower( *str ); + str++; + } + + return (ret); +} diff --git a/device/lib/pic16/libc/string/strncat.c b/device/lib/pic16/libc/string/strncat.c new file mode 100644 index 00000000..8ab7c3cc --- /dev/null +++ b/device/lib/pic16/libc/string/strncat.c @@ -0,0 +1,39 @@ +/*------------------------------------------------------------------------- + strncat.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strncat(char *front, char * back, size_t count) +{ + char *start = front; + + while (*front++); + + front--; + while (count--) + if (!(*front++ = *back++))return(start); + + *front = '\0'; + + return(start); +} diff --git a/device/lib/pic16/libc/string/strncmp.c b/device/lib/pic16/libc/string/strncmp.c new file mode 100644 index 00000000..f193c7ab --- /dev/null +++ b/device/lib/pic16/libc/string/strncmp.c @@ -0,0 +1,36 @@ +/*------------------------------------------------------------------------- + _strncmp.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +int strncmp(char *first, char *last, size_t count) +{ + if (!count)return(0); + + while (--count && *first && *first == *last) { + first++; + last++; + } + + return( *first - *last ); +} diff --git a/device/lib/pic16/libc/string/strncpy.c b/device/lib/pic16/libc/string/strncpy.c new file mode 100644 index 00000000..d8dd613c --- /dev/null +++ b/device/lib/pic16/libc/string/strncpy.c @@ -0,0 +1,33 @@ +/*------------------------------------------------------------------------- + strncpy.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strncpy (char *d, char *s, size_t n) +{ + register char *d1 = d; + + while ( n-- )*d++ = *s++ ; + + return d1; +} diff --git a/device/lib/pic16/libc/string/strpbrk.c b/device/lib/pic16/libc/string/strpbrk.c new file mode 100644 index 00000000..1862ca07 --- /dev/null +++ b/device/lib/pic16/libc/string/strpbrk.c @@ -0,0 +1,35 @@ +/*------------------------------------------------------------------------- + strpbrk.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strpbrk (char * string, char *control) +{ + register char ch ; + + while(ch = *string) { + if(strchr(control, ch ))return string ; + } + + return (NULL); +} diff --git a/device/lib/pic16/libc/string/strrchr.c b/device/lib/pic16/libc/string/strrchr.c new file mode 100644 index 00000000..d4cdd514 --- /dev/null +++ b/device/lib/pic16/libc/string/strrchr.c @@ -0,0 +1,41 @@ +/*------------------------------------------------------------------------- + strrchr.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strrchr(char *string, char ch) +{ + char *start = string; + + while (*string++) /* find end of string */ + ; + + /* search towards front */ + while (--string != start && *string != ch) + ; + + if (*string == ch) /* char found ? */ + return( (char *)string ); + + return (NULL) ; +} diff --git a/device/lib/pic16/libc/string/strspn.c b/device/lib/pic16/libc/string/strspn.c new file mode 100644 index 00000000..28feace5 --- /dev/null +++ b/device/lib/pic16/libc/string/strspn.c @@ -0,0 +1,40 @@ +/*------------------------------------------------------------------------- + strspn.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +int strspn(char *string, char *control) +{ + register int count = 0; + register char ch ; + + while (ch = *string) { + if ( strchr(control,ch) ) + count++ ; + else break ; + + string++ ; + } + + return count ; +} diff --git a/device/lib/pic16/libc/string/strstr.c b/device/lib/pic16/libc/string/strstr.c new file mode 100644 index 00000000..6351bfa2 --- /dev/null +++ b/device/lib/pic16/libc/string/strstr.c @@ -0,0 +1,49 @@ +/*------------------------------------------------------------------------- + _strstr.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strstr(char *str1, char *str2) +{ + char *cp = str1; + char *s1; + char *s2; + + + if( !*str2 ) + return str1; + + while (*cp) { + s1 = cp; + s2 = str2; + + while ( *s1 && *s2 && !(*s1-*s2) ) + s1++, s2++; + + if (!*s2)return(cp); + + cp++; + } + + return (NULL) ; +} diff --git a/device/lib/pic16/libc/string/strtok.c b/device/lib/pic16/libc/string/strtok.c new file mode 100644 index 00000000..209dd578 --- /dev/null +++ b/device/lib/pic16/libc/string/strtok.c @@ -0,0 +1,54 @@ +/*------------------------------------------------------------------------- + strtok.c - part of string library functions + + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ +#include + +char *strtok(char *str, char *control) +{ + static char * s; + register char * s1; + + if (str)s = str ; + + if (!s)return NULL; + + while (*s) { + if (strchr(control,*s))s++; + else break; + } + + s1 = s ; + + while (*s) { + if (strchr(control,*s)) { + *s++ = '\0'; + return s1 ; + } + s++ ; + } + + s = NULL; + + if (*s1)return s1; + else return NULL; +} diff --git a/device/lib/pic16/libc/string/strupr.c b/device/lib/pic16/libc/string/strupr.c new file mode 100644 index 00000000..457d561a --- /dev/null +++ b/device/lib/pic16/libc/string/strupr.c @@ -0,0 +1,38 @@ +/*------------------------------------------------------------------------- + strupr.c - part of string library functions + + Written by Vangelis Rokas, 2004 + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! +-------------------------------------------------------------------------*/ + +#include +#include + +char *strupr(char *str) +{ + char *ret = str; + + while(*str) { + *str = toupper( *str ); + str++; + } + + return (ret); +} diff --git a/device/lib/pic16/libdev/pic18f242.c b/device/lib/pic16/libdev/pic18f242.c index dc281a76..d434370f 100644 --- a/device/lib/pic16/libdev/pic18f242.c +++ b/device/lib/pic16/libdev/pic18f242.c @@ -131,6 +131,8 @@ sfr at 0xfd3 OSCCON; volatile __OSCCONbits_t at 0xfd3 OSCCONbits; sfr at 0xfd5 T0CON; +volatile __T0CONbits_t at 0xfd5 T0CONbits; + sfr at 0xfd6 TMR0L; sfr at 0xfd7 TMR0H; sfr at 0xfd8 STATUS; diff --git a/device/lib/pic16/libdev/pic18f252.c b/device/lib/pic16/libdev/pic18f252.c index f85c1092..3db32a13 100644 --- a/device/lib/pic16/libdev/pic18f252.c +++ b/device/lib/pic16/libdev/pic18f252.c @@ -131,6 +131,8 @@ sfr at 0xfd3 OSCCON; volatile __OSCCONbits_t at 0xfd3 OSCCONbits; sfr at 0xfd5 T0CON; +volatile __T0CONbits_t at 0xfd5 T0CONbits; + sfr at 0xfd6 TMR0L; sfr at 0xfd7 TMR0H; sfr at 0xfd8 STATUS; diff --git a/device/lib/pic16/libdev/pic18f442.c b/device/lib/pic16/libdev/pic18f442.c index b73436c8..75c5c798 100644 --- a/device/lib/pic16/libdev/pic18f442.c +++ b/device/lib/pic16/libdev/pic18f442.c @@ -149,6 +149,8 @@ sfr at 0xfd3 OSCCON; volatile __OSCCONbits_t at 0xfd3 OSCCONbits; sfr at 0xfd5 T0CON; +volatile __T0CONbits_t at 0xfd5 T0CONbits; + sfr at 0xfd6 TMR0L; sfr at 0xfd7 TMR0H; sfr at 0xfd8 STATUS; diff --git a/device/lib/pic16/libdev/pic18f452.c b/device/lib/pic16/libdev/pic18f452.c index d8fdd318..34d0dd00 100644 --- a/device/lib/pic16/libdev/pic18f452.c +++ b/device/lib/pic16/libdev/pic18f452.c @@ -149,6 +149,8 @@ sfr at 0xfd3 OSCCON; volatile __OSCCONbits_t at 0xfd3 OSCCONbits; sfr at 0xfd5 T0CON; +volatile __T0CONbits_t at 0xfd5 T0CONbits; + sfr at 0xfd6 TMR0L; sfr at 0xfd7 TMR0H; sfr at 0xfd8 STATUS; diff --git a/device/lib/pic16/libio/Makefile b/device/lib/pic16/libio/Makefile new file mode 100644 index 00000000..45242dd0 --- /dev/null +++ b/device/lib/pic16/libio/Makefile @@ -0,0 +1,54 @@ +# +# Makefile - Makefile to build pic16 support libraries +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + + +DIRS = adc + + +LIB = libio.lib + +all: build-libraries + +make-target: build-libraries + +build-libraries: + for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir ; \ + done; + gplib -c $(LIB) adc/*.o + mv -v $(LIB) ../bin + + +clean-intermediate: + @for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir clean-intermediate ; \ + done ; + +clean: + for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir clean; \ + done ; + rm -fv $(LIB) + + +real-clean: clean + find -name *.adb -print | xargs -- rm -fv ; + find -name *.p -print | xargs -- rm -fv ; + find -name *.d -print | xargs -- rm -fv ; + find -name *.dump* -print | xargs -- rm -fv ; + + +dep: + for dir in $(DIRS) ; do \ + $(MAKE) -C $$dir dep; \ + done diff --git a/device/lib/pic16/libio/Makefile.rules b/device/lib/pic16/libio/Makefile.rules new file mode 100644 index 00000000..c571aaf2 --- /dev/null +++ b/device/lib/pic16/libio/Makefile.rules @@ -0,0 +1,48 @@ +# +# Makefile.rules - Common Makefile rules to build pic16 C Library +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + + +include ../../Makefile.common + +PRJDIR = ../../../../.. + +LIBC_INC_DIR = $(PRJDIR)/device/include/pic16 + +COMPILE_FLAGS += $(MODELFLAGS) --pomit-config-words --pomit-ivt --no-peep --i-code-in-asm +CFLAGS = -I$(LIBC_INC_DIR) + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + +%.o: %.c + $(CC) $(CFLAGS) $(COMPILE_FLAGS) -c $< + + +all: build-library + +clean-intermediate: + $(RM) -f *.lst *.asm *.dump* *.p *.d *.adb + + +clean: clean-intermediate + $(RM) -f $(LIB) *.o + +dep .depend: + rm -f .depend + for temp in $(CFILES); do \ + $(CPP) $(MM) $(CFLAGS) $$temp > .tmpdepend; \ + $(SED) s/.rel/.o/g .tmpdepend >> .depend; \ + $(RM) -f .tmpdepend; \ + done; + +include .depend diff --git a/device/lib/pic16/libio/adc/Makefile b/device/lib/pic16/libio/adc/Makefile new file mode 100644 index 00000000..03db167a --- /dev/null +++ b/device/lib/pic16/libio/adc/Makefile @@ -0,0 +1,39 @@ +# +# Makefile - Makefile to build pic16 C Library +# +# This file is part of the GNU PIC Library. +# +# January, 2004 +# The GNU PIC Library is maintained by, +# Vangelis Rokas +# +# $Id$ +# +# + +include ../Makefile.rules + +SRCS = adcbusy \ + adcclose \ + adcconv \ + adcopen \ + adcread \ + adcsetch + + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + + +DEBUG= +#COMPILE_FLAGS += --pomit-config-words --pomit-ivt --no-peep +COMPILE_FLAGS += $(DEBUG) + +CFILES = $(patsubst %,%.c,$(SRCS)) +OFILES = $(patsubst %.c,%.o,$(CFILES)) + +%.o: %.c + $(CC) $(CFLAGS) $(COMPILE_FLAGS) -c $< + +build-library: $(OFILES) + diff --git a/device/lib/pic16/libio/adc/adcbusy.c b/device/lib/pic16/libio/adc/adcbusy.c new file mode 100644 index 00000000..5328c314 --- /dev/null +++ b/device/lib/pic16/libio/adc/adcbusy.c @@ -0,0 +1,10 @@ + +#include + +#include + + +char adc_busy(void) +{ + return (ADCON0bits.GO == 1); +} diff --git a/device/lib/pic16/libio/adc/adcclose.c b/device/lib/pic16/libio/adc/adcclose.c new file mode 100644 index 00000000..e6e5b237 --- /dev/null +++ b/device/lib/pic16/libio/adc/adcclose.c @@ -0,0 +1,11 @@ + +#include + +#include + + +void adc_close(void) +{ + ADCON0bits.ADON = 0; + PIE1bits.ADIE = 0; +} diff --git a/device/lib/pic16/libio/adc/adcconv.c b/device/lib/pic16/libio/adc/adcconv.c new file mode 100644 index 00000000..279ba769 --- /dev/null +++ b/device/lib/pic16/libio/adc/adcconv.c @@ -0,0 +1,10 @@ + +#include + +#include + + +void adc_conv(void) +{ + ADCON0bits.GO = 1; +} diff --git a/device/lib/pic16/libio/adc/adcopen.c b/device/lib/pic16/libio/adc/adcopen.c new file mode 100644 index 00000000..43e8381e --- /dev/null +++ b/device/lib/pic16/libio/adc/adcopen.c @@ -0,0 +1,39 @@ + +#include + +#include + + +/* parameters are: + * channel: one of ADC_CHN_* + * fosc: one of ADC_FOSC_* + * pcfg: one of ADC_CFG_* + * config: ADC_FRM_* | ADC_INT_* + */ + +void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config) +{ + ADCON0 = 0; + ADCON1 = 0; + + /* setup channel */ + ADCON0 |= (channel & 0x07) << 3; + + /* setup fosc */ + ADCON0 |= (fosc & 0x03) << 6; + ADCON1 |= (fosc & 0x04) << 4; + + /* setup reference and pins */ + ADCON1 |= pcfg & 0x0f; + + ADCON0 |= (config & ADC_FRM_RJUST); + + if(config & ADC_INT_ON) { + PIR1bits.ADIF = 0; + PIE1bits.ADIE = 1; + INTCONbits.PEIE = 1; + } + + /* enable the A/D module */ + ADCON0bits.ADON = 1; +} diff --git a/device/lib/pic16/libio/adc/adcread.c b/device/lib/pic16/libio/adc/adcread.c new file mode 100644 index 00000000..445854f1 --- /dev/null +++ b/device/lib/pic16/libio/adc/adcread.c @@ -0,0 +1,18 @@ + +#include + +#include + + +int adc_read(void) +{ + union { + int ri; + char rb[2]; + } result; + + result.rb[0] = ADRESL; + result.rb[1] = ADRESH; + + return (result.ri); +} diff --git a/device/lib/pic16/libio/adc/adcsetch.c b/device/lib/pic16/libio/adc/adcsetch.c new file mode 100644 index 00000000..f55e77b1 --- /dev/null +++ b/device/lib/pic16/libio/adc/adcsetch.c @@ -0,0 +1,11 @@ + +#include + +#include + + +void adc_setchannel(unsigned char channel) +{ + ADCON0 &= ~(0x7 << 3); + ADCON0 |= channel << 3; +} diff --git a/device/lib/pic16/libsdcc/Makefile b/device/lib/pic16/libsdcc/Makefile index 391342be..2774f75b 100644 --- a/device/lib/pic16/libsdcc/Makefile +++ b/device/lib/pic16/libsdcc/Makefile @@ -30,7 +30,7 @@ build-libraries: for dir in $(DIRS) ; do \ $(MAKE) -C $$dir ; \ done; - gplib -c $(LIB) char/*.o int/*.o long/*.o float/*.o gptr/*.o + gplib -c $(LIB) char/*.o int/*.o long/*.o float/*.o gptr/*.o lregs/*.o mv -v $(LIB) ../bin diff --git a/device/lib/pic16/libsdcc/char/divschar.c b/device/lib/pic16/libsdcc/char/divschar.c index 57e0b7e1..d306f941 100644 --- a/device/lib/pic16/libsdcc/char/divschar.c +++ b/device/lib/pic16/libsdcc/char/divschar.c @@ -25,6 +25,10 @@ what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ +/* +** $Id$ +*/ + #include unsigned char _divuchar (unsigned char a, unsigned char b); diff --git a/device/lib/pic16/libsdcc/char/divuchar.c b/device/lib/pic16/libsdcc/char/divuchar.c index 6c99d1e8..0aee20d0 100644 --- a/device/lib/pic16/libsdcc/char/divuchar.c +++ b/device/lib/pic16/libsdcc/char/divuchar.c @@ -24,6 +24,10 @@ what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ +/* +** $Id$ +*/ + #include #define MSB_SET(x) ((x >> (8*sizeof(x)-1)) & 1) diff --git a/device/lib/pic16/libsdcc/char/modschar.c b/device/lib/pic16/libsdcc/char/modschar.c index 116ecd18..d8c966f7 100644 --- a/device/lib/pic16/libsdcc/char/modschar.c +++ b/device/lib/pic16/libsdcc/char/modschar.c @@ -24,6 +24,11 @@ what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ +/* +** $Id$ +*/ + + #include unsigned char _moduchar (unsigned char a, unsigned char b); @@ -31,7 +36,7 @@ unsigned char _moduchar (unsigned char a, unsigned char b); char _modschar (char a, char b) _IL_REENTRANT { register char r; - register char ta, tb; + char ta, tb; if(a<0)ta = -a; else ta = a; if(b<0)tb = -b; else tb = b; diff --git a/device/lib/pic16/libsdcc/float/Makefile b/device/lib/pic16/libsdcc/float/Makefile index 841273e5..58fb3f87 100644 --- a/device/lib/pic16/libsdcc/float/Makefile +++ b/device/lib/pic16/libsdcc/float/Makefile @@ -35,6 +35,7 @@ SRCS = fs2schar \ uint2fs \ ulong2fs +COMPILE_FLAGS += --obanksel=9 include ../Makefile.rules diff --git a/src/pic16/gen.c b/src/pic16/gen.c index 19d484eb..52b13c78 100644 --- a/src/pic16/gen.c +++ b/src/pic16/gen.c @@ -579,9 +579,13 @@ static void resolveIfx(resolvedIfx *resIfx, iCode *ifx) #if 1 if(IC_TRUE(ifx)) - DEBUGpic16_emitcode("; ***","ifx true is non-null"); + DEBUGpic16_emitcode("; +++","ifx true is non-null"); + else + DEBUGpic16_emitcode("; +++","ifx true is null"); if(IC_FALSE(ifx)) - DEBUGpic16_emitcode("; ***","ifx false is non-null"); + DEBUGpic16_emitcode("; +++","ifx false is non-null"); + else + DEBUGpic16_emitcode("; +++","ifx false is null"); #endif } @@ -887,7 +891,7 @@ static asmop *aopForRemat (operand *op) // x symbol *sym) if(IN_CODESPACE( SPEC_OCLS( OP_SYM_ETYPE(refop))) || viaimmd) { - DEBUGpic16_emitcode(";", "%s:%d immediate", __FILE__, __LINE__); + DEBUGpic16_emitcode(";", "%s:%d immediate, size: %d", __FILE__, __LINE__, getSize( sym->type )); aop->aopu.pcop = pic16_popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname, 0, val); @@ -898,12 +902,15 @@ static asmop *aopForRemat (operand *op) // x symbol *sym) #endif PCOI(aop->aopu.pcop)->index = val; + + aop->size = getSize( sym->type ); } else { - DEBUGpic16_emitcode(";", "%s:%d dir", __FILE__, __LINE__); + DEBUGpic16_emitcode(";", "%s:%d dir size: %d", __FILE__, __LINE__, getSize( OP_SYMBOL( IC_LEFT(ic))->type)); aop->aopu.pcop = pic16_popRegFromString(OP_SYMBOL(IC_LEFT(ic))->rname, getSize( OP_SYMBOL( IC_LEFT(ic))->type), val, op); -// aop->size = AOP_SIZE( IC_LEFT(ic) ); + + aop->size = getSize( OP_SYMBOL( IC_LEFT(ic))->type ); } @@ -1079,7 +1086,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result) if (!op) return ; -// DEBUGpic16_emitcode(";","%s %d",__FUNCTION__, __LINE__); + DEBUGpic16_emitcode(";","%s %d",__FUNCTION__, __LINE__); /* if this a literal */ if (IS_OP_LITERAL(op)) { @@ -1127,7 +1134,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result) sym = OP_SYMBOL(op); - DEBUGpic16_emitcode("; ***", "%d: symbol name = %s", __LINE__, sym->name); + DEBUGpic16_emitcode("; ***", "%d: symbol name = %s, regType = %d", __LINE__, sym->name, sym->regType); /* if the type is a conditional */ if (sym->regType == REG_CND) { aop = op->aop = sym->aop = newAsmop(AOP_CRY); @@ -1145,8 +1152,8 @@ void pic16_aopOp (operand *op, iCode *ic, bool result) if (sym->remat) { sym->aop = op->aop = aop = aopForRemat (op); - aop->size = getSize(sym->type); - //DEBUGpic16_emitcode(";"," %d: size %d, %s\n",__LINE__,aop->size,aop->aopu.aop_immd); +// aop->size = getSize(sym->type); +// DEBUGpic16_emitcode(";"," %d: size %d, %s\n",__LINE__,aop->size,aop->aopu.aop_immd); return; } @@ -1543,7 +1550,7 @@ pCodeOp *pic16_popGetTempReg(int lock) // DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); if(_TempReg_lock) { - werror(W_POSSBUG2, __FILE__, __LINE__); +// werror(W_POSSBUG2, __FILE__, __LINE__); } _TempReg_lock += lock; @@ -1577,7 +1584,7 @@ pCodeOp *pic16_popGetTempRegCond(bitVect *v, int lock) // DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); if(_TempReg_lock) { - werror(W_POSSBUG2, __FILE__, __LINE__); +// werror(W_POSSBUG2, __FILE__, __LINE__); } _TempReg_lock += lock; @@ -1914,6 +1921,8 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname) PCOR(pcop)->instance = offset; pcop->type = PCOR(pcop)->r->pc_type; + + DEBUGpic16_emitcode(";*+*", "%d\tAOP_REG type = %s\n", __LINE__, dumpPicOptype(pcop->type)); rs = aop->aopu.aop_reg[offset]->name; DEBUGpic16_emitcode(";","%d regiser idx = %d name = %s",__LINE__,rIdx,rs); return pcop; @@ -1923,6 +1932,7 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname) DEBUGpic16_emitcode(";","%d\tAOP_CRY", __LINE__); pcop = pic16_newpCodeOpBit(aop->aopu.aop_dir,-1,1, PO_GPR_REGISTER); + PCOR(pcop)->instance = offset; PCOR(pcop)->r = pic16_dirregWithName(aop->aopu.aop_dir); //if(PCOR(pcop)->r == NULL) //fprintf(stderr,"%d - couldn't find %s in allocated registers\n",__LINE__,aop->aopu.aop_dir); @@ -2449,6 +2459,7 @@ void pic16_outBitC(operand *result) int i; DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + /* if the result is bit */ if (AOP_TYPE(result) == AOP_CRY) { fprintf(stderr, "%s:%d: pic16 port warning: unsupported case\n", __FILE__, __LINE__); @@ -2460,12 +2471,31 @@ void pic16_outBitC(operand *result) pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result), i)); } pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result), 0)); - -/* - pic16_emitcode("clr","a ; %d", __LINE__); - pic16_emitcode("rlc","a"); - pic16_outAcc(result); -*/ + } +} + +/*-----------------------------------------------------------------*/ +/* pic16_outBitOp - output a bit from Op */ +/* Move to result the value of set/clr op -- VR */ +/*-----------------------------------------------------------------*/ +void pic16_outBitOp(operand *result, pCodeOp *pcop) +{ + int i; + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + /* if the result is bit */ + if (AOP_TYPE(result) == AOP_CRY) { + fprintf(stderr, "%s:%d: pic16 port warning: unsupported case\n", __FILE__, __LINE__); + pic16_aopPut(AOP(result),"c",0); + } else { + + i = AOP_SIZE(result); + while(i--) { + pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result), i)); + } + pic16_emitpcode(POC_RRCF, pcop); + pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result), 0)); } } @@ -2596,25 +2626,19 @@ release: static void genUminusFloat(operand *op,operand *result) { int size ,offset =0 ; - char *l; DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); /* for this we just need to flip the first it then copy the rest in place */ - size = AOP_SIZE(op) - 1; - l = pic16_aopGet(AOP(op),3,FALSE,FALSE); - - MOVA(l); - - pic16_emitcode("cpl","acc.7"); - pic16_aopPut(AOP(result),"a",3); + size = AOP_SIZE(op); while(size--) { - pic16_aopPut(AOP(result), - pic16_aopGet(AOP(op),offset,FALSE,FALSE), - offset); - offset++; - } + mov2f(AOP(result), AOP(op), offset); + offset++; + } + + /* toggle the MSB's highest bit */ + pic16_emitpcode(POC_BTG, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), offset-1), 7)); } /*-----------------------------------------------------------------*/ @@ -3650,6 +3674,8 @@ static void genEndFunction (iCode *ic) pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_fsr1h)); } +// sym->regsUsed = _G.fregsUsed; + /* now we need to restore the registers */ /* if any registers used */ if (sym->regsUsed) { @@ -3737,8 +3763,13 @@ static void genEndFunction (iCode *ic) void pic16_storeForReturn(operand *op, int offset, pCodeOp *dest) { if(is_LitOp(op)) { - pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(op), offset)); - if(dest->type != PO_WREG)pic16_emitpcode(POC_MOVWF, dest); + unsigned long lit = (unsigned long)floatFromVal(AOP(op)->aopu.aop_lit); + if(lit == 0) { + pic16_emitpcode(POC_CLRF, dest); + } else { + pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(op), offset)); + if(dest->type != PO_WREG)pic16_emitpcode(POC_MOVWF, dest); + } } else { if(dest->type == PO_WREG && (offset == 0)) { pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(op), offset)); @@ -4660,6 +4691,8 @@ static int genChkZeroes(operand *op, int lit, int size) /* genCmp :- greater or less than comparison */ /*-----------------------------------------------------------------*/ #if 1 + /* { */ + /* original code */ static void genCmp (operand *left,operand *right, operand *result, iCode *ifx, int sign) { @@ -5161,7 +5194,6 @@ static void genCmp (operand *left,operand *right, } - pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lp1 >> (size*8)) & 0xff)); pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size)); @@ -5376,7 +5408,370 @@ check_carry: } -#else /* old version of genCmp() */ +#else /* old version of genCmp() */ /* } else { */ + +/* check all condition and return appropriate instruction, POC_CPFSGT or POC_CPFFSLT */ +static int selectCompareOp(resolvedIfx *rIfx, iCode *ifx, + operand *result, int offset, int invert_op) +{ + /* add code here */ + + /* check condition, > or < ?? */ + if(rIfx->condition != 0)invert_op ^= 1; + + if(ifx && IC_FALSE(ifx))invert_op ^= 1; + + if(!ifx)invert_op ^= 1; + + DEBUGpic16_emitcode("; +++", "%s:%d %s] rIfx->condition= %d, ifx&&IC_FALSE(ifx)= %d, invert_op = %d", + __FILE__, __LINE__, __FUNCTION__, rIfx->condition, (ifx && IC_FALSE(ifx)), invert_op); + + /* do selection */ + if(!invert_op)return POC_CPFSGT; + else return POC_CPFSLT; +} + +static int compareAopfirstpass=1; + +static void compareAop(resolvedIfx *resIfx, iCode *ifx, symbol *falselbl, + operand *oper, int offset, operand *result, + int sign, int invert_op, pCodeOp *pcop, pCodeOp *pcop2, + symbol *tlbl) +{ + int op; + symbol *truelbl; + + /* invert if there is a result to be loaded, in order to fit, + * SETC/CLRC sequence */ + if(AOP_SIZE(result))invert_op ^= 1; + +// if(sign && !offset)invert_op ^= 1; + +// if(sign)invert_op ^= 1; + + op = selectCompareOp(resIfx, ifx, result, offset, invert_op); + + if(AOP_SIZE(result) && compareAopfirstpass) { + if(!ifx) { + if(pcop2) + pic16_emitpcode(POC_SETF, pcop2); + else + emitSETC; + } else { + if(pcop2) + pic16_emitpcode(POC_CLRF, pcop2); + else + emitCLRC; + } + } + + compareAopfirstpass = 0; + + /* there is a bug when comparing operands with size > 1, + * because higher bytes can be equal and test should be performed + * to the next lower byte, current algorithm, considers operands + * inequal in these cases! -- VR 20041107 */ + + + if(pcop) + pic16_emitpcode(op, pcop); + else + pic16_emitpcode(op, pic16_popGet(AOP(oper), offset)); + + + if((!sign || !offset) && AOP_SIZE(result)) { + if(!ifx) { + if(pcop2) + pic16_emitpcode(POC_CLRF, pcop2); + else + emitCLRC; + } else { + if(pcop2) + pic16_emitpcode(POC_SETF, pcop2); + else + emitSETC; + } + + /* don't emit final branch (offset == 0) */ + if(offset) { + + if(pcop2) + pic16_emitpcode(POC_RRCF, pcop2); + + pic16_emitpcode(POC_BNC, pic16_popGetLabel(falselbl->key)); + } + } else { + if((ifx && (IC_TRUE(ifx)!=NULL)) || (sign && !offset)) { + DEBUGpic16_emitcode ("; +++","%s: %d: ifx = %p, IC_TRUE(ifx) = %d, sign = %d, offset = %d", + __FUNCTION__, __LINE__, ifx, (ifx&&IC_TRUE(ifx)), sign, offset); + + truelbl = newiTempLabel( NULL ); + pic16_emitpcode(POC_BRA, pic16_popGetLabel(truelbl->key)); + if((!ifx || !IC_TRUE(ifx)) && (sign && !offset)) + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(tlbl->key)); + else + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(resIfx->lbl->key)); + pic16_emitpLabel(truelbl->key); + } else { + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(resIfx->lbl->key)); + } + } +} + + + + +#if 1 /* { */ +static void genCmp (operand *left, operand *right, + operand *result, iCode *ifx, int sign) +{ + int size, cmpop=1; + long lit = 0L; + resolvedIfx rFalseIfx; + symbol *falselbl, *tlbl; + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + DEBUGpic16_pic16_AopType(__LINE__,left,right,result); + + resolveIfx(&rFalseIfx, ifx); + size = max(AOP_SIZE(left), AOP_SIZE(right)); + + /* if left & right are bit variables */ + if(AOP_TYPE(left) == AOP_CRY + && AOP_TYPE(right) == AOP_CRY ) { + + pic16_emitcode("mov","c,%s",AOP(right)->aopu.aop_dir); + pic16_emitcode("anl","c,/%s",AOP(left)->aopu.aop_dir); + + werror(W_POSSBUG2, __FILE__, __LINE__); + exit(-1); + } + + /* if literal is on the right then swap with left */ + if((AOP_TYPE(right) == AOP_LIT)) { + operand *tmp = right ; +// unsigned long mask = (0x100 << (8*(size-1))) - 1; + + lit = /*(unsigned long)*/floatFromVal(AOP(right)->aopu.aop_lit); + +// lit = (lit - 1) & mask; + right = left; + left = tmp; + rFalseIfx.condition ^= 1; /* reverse compare */ + } else + if ((AOP_TYPE(left) == AOP_LIT)) { + /* float compares are handled by support functions */ + lit = /*(unsigned long)*/floatFromVal(AOP(left)->aopu.aop_lit); + } + + /* actual comparing algorithm */ +// size = AOP_SIZE( right ); + + falselbl = newiTempLabel( NULL ); + if(AOP_TYPE(left) == AOP_LIT) { + /* compare to literal */ + DEBUGpic16_emitcode ("; ***","%s: %d: compare to literal", __FUNCTION__, __LINE__); + + if(sign) { + pCodeOp *pct, *pct2; + symbol *tlbl1; + + /* signed compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: signed compare", __FUNCTION__, __LINE__); + + pct = pic16_popCopyReg(&pic16_pc_prodl); + pct2 = pic16_popCopyReg(&pic16_pc_prodh); + tlbl = newiTempLabel( NULL ); + + /* first compare signs: + * a. if both are positive, compare just like unsigned + * b. if both are negative, invert cmpop, compare just like unsigned + * c. if different signs, determine the result directly */ + + size--; + +#if 1 + /* { */ + tlbl1 = newiTempLabel( NULL ); +// pic16_emitpcode(POC_RLCFW, pic16_popGet( AOP(right), size)); /* move sign to carry */ + + if(lit > 0) { + + /* literal is zero or positive: + * a. if carry is zero, too, continue compare, + * b. if carry is set, then continue depending on cmpop ^ condition: + * 1. '<' return false (literal < variable), + * 2. '>' return true (literal > variable) */ +// pic16_emitpcode(POC_BNC, pic16_popGetLabel( tlbl1->key )); + pic16_emitpcode(POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), size), 7)); + + + if(cmpop ^ rFalseIfx.condition)pic16_emitpcode(POC_GOTO, pic16_popGetLabel( tlbl->key )); + else pic16_emitpcode(POC_GOTO, pic16_popGetLabel( rFalseIfx.lbl->key )); + } else + if(lit < 0) { + + /* literal is negative: + * a. if carry is set, too, continue compare, + * b. if carry is zero, then continue depending on cmpop ^ condition: + * 1. '<' return true (literal < variable), + * 2. '>' return false (literal > variable) */ +// pic16_emitpcode(POC_BC, pic16_popGetLabel( tlbl1->key )); + pic16_emitpcode(POC_BTFSS, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), size), 7)); + + if(!(cmpop ^ rFalseIfx.condition))pic16_emitpcode(POC_GOTO, pic16_popGetLabel( tlbl->key )); + else pic16_emitpcode(POC_GOTO, pic16_popGetLabel( rFalseIfx.lbl->key)); + } +#if 1 + else { + /* lit == 0 */ + pic16_emitpcode(POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), size), 7)); + + if(!(cmpop ^ rFalseIfx.condition))pic16_emitpcode(POC_GOTO, pic16_popGetLabel( tlbl->key )); + else pic16_emitpcode(POC_GOTO, pic16_popGetLabel( rFalseIfx.lbl->key)); + } +#endif + + + pic16_emitpLabel( tlbl1->key ); +#endif /* } */ + + compareAopfirstpass=1; +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80)); +// pic16_emitpcode(POC_ADDFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_MOVWF, pct); + +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size) + 0x80)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size))); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, pct2, tlbl); + + /* generic case */ + while( size-- ) { +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80)); +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x0)); +// pic16_emitpcode(POC_ADDFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_MOVWF, pct); + +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size) + 0x80)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size) + 0x0)); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, pct2, tlbl); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 0, pct, pct2, tlbl); + } +// } + + if(ifx)ifx->generated = 1; + + if(AOP_SIZE(result)) { + pic16_emitpLabel(tlbl->key); + pic16_emitpLabel(falselbl->key); + pic16_outBitOp( result, pct2 ); + } else { + pic16_emitpLabel(tlbl->key); + } + } else { + + + /* unsigned compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: unsigned compare", __FUNCTION__, __LINE__); + + compareAopfirstpass=1; + while(size--) { + + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size))); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, NULL, NULL); + + } + + if(ifx)ifx->generated = 1; + + + if(AOP_SIZE(result)) { + pic16_emitpLabel(falselbl->key); + pic16_outBitC( result ); + } + + } + } else { + /* compare registers */ + DEBUGpic16_emitcode ("; ***","%s: %d: compare registers", __FUNCTION__, __LINE__); + + + if(sign) { + pCodeOp *pct, *pct2; + + /* signed compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: signed compare", __FUNCTION__, __LINE__); + + pct = pic16_popCopyReg(&pic16_pc_prodl); /* first temporary register */ + pct2 = pic16_popCopyReg(&pic16_pc_prodh); /* second temporary register */ + tlbl = newiTempLabel( NULL ); + + compareAopfirstpass=1; + + size--; + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + pic16_emitpcode(POC_MOVWF, pct); + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + + /* WREG already holds left + 0x80 */ + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); + + while( size-- ) { + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + pic16_emitpcode(POC_MOVWF, pct); + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + + /* WREG already holds left + 0x80 */ + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 0, pct, pct2, tlbl); + } + + if(ifx)ifx->generated = 1; + + if(AOP_SIZE(result)) { + pic16_emitpLabel(tlbl->key); + pic16_emitpLabel(falselbl->key); + pic16_outBitOp( result, pct2 ); + } else { + pic16_emitpLabel(tlbl->key); + } + + } else { + /* unsigned compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: unsigned compare", __FUNCTION__, __LINE__); + + compareAopfirstpass=1; + while(size--) { + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), size)); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, NULL, NULL); + + } + + if(ifx)ifx->generated = 1; + if(AOP_SIZE(result)) { + + pic16_emitpLabel(falselbl->key); + pic16_outBitC( result ); + } + + } + } +} + +#else /* } else { */ /* new version of genCmp -- VR 20041012 */ static void genCmp (operand *left,operand *right, @@ -5415,7 +5810,7 @@ static void genCmp (operand *left,operand *right, // lit = (lit - 1) & mask; right = left; left = tmp; - rFalseIfx.condition ^= 1; + rFalseIfx.condition ^= 1; /* reverse compare */ } else if ((AOP_TYPE(left) == AOP_LIT)) { /* float compares are handled by support functions */ @@ -5427,6 +5822,7 @@ static void genCmp (operand *left,operand *right, /* if left & right are bit variables */ if (AOP_TYPE(left) == AOP_CRY && AOP_TYPE(right) == AOP_CRY ) { + pic16_emitcode("mov","c,%s",AOP(right)->aopu.aop_dir); pic16_emitcode("anl","c,/%s",AOP(left)->aopu.aop_dir); @@ -5441,15 +5837,17 @@ static void genCmp (operand *left,operand *right, else willCheckCarry = 0; /* Special cases */ - if((lit == 0) && (sign == 0)){ - + if((lit == 0) && (sign == 0)) { + /* unsigned compare to 0 */ + DEBUGpic16_emitcode("; unsigned compare to 0","lit = 0x%x, sign=%d",lit,sign); + size--; pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); while(size) pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),--size)); genSkipz2(&rFalseIfx,0); - if(ifx) ifx->generated = 1; + if(ifx)ifx->generated = 1; return; } @@ -5467,10 +5865,11 @@ static void genCmp (operand *left,operand *right, if(sign) { /* signed comparisons to a literal byte */ + DEBUGpic16_emitcode(";signed compare to literal","%d: lit = 0x%x, sign=%d",__LINE__, lit,sign); int lp1 = (lit+1) & 0xff; - DEBUGpic16_emitcode(";left lit","line = %d lit = 0x%x condition = %d",__LINE__,lit, rFalseIfx.condition); + DEBUGpic16_emitcode(";left lit","line = %d lit = 0x%x condition = %d lp1 = %i",__LINE__,lit, rFalseIfx.condition, lp1); switch (lp1) { case 0: rFalseIfx.condition ^= 1; @@ -5507,8 +5906,10 @@ static void genCmp (operand *left,operand *right, } } else { /* unsigned comparisons to a literal byte */ + DEBUGpic16_emitcode("; unsigned compare to literal","%d: lit = 0x%x, sign=%d",__LINE__, lit,sign); switch(lit & 0xff ) { + /* special cases */ case 0: pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); genSkipz2(&rFalseIfx,0); @@ -5517,7 +5918,6 @@ static void genCmp (operand *left,operand *right, rFalseIfx.condition ^= 1; genSkipCond(&rFalseIfx,right,0,7); break; - default: pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lit+1) & 0xff)); pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),0)); @@ -5825,7 +6225,10 @@ check_carry: } } -#endif +#endif /* } */ + + +#endif /* } */ @@ -6204,10 +6607,10 @@ static void gencjne(operand *left, operand *right, iCode *ifx) /*-----------------------------------------------------------------*/ static bool is_LitOp(operand *op) { - return (AOP_TYPE(op) == AOP_LIT) + return ((AOP_TYPE(op) == AOP_LIT) || ( (AOP_TYPE(op) == AOP_PCODE) && ( (AOP(op)->aopu.pcop->type == PO_LITERAL) - || (AOP(op)->aopu.pcop->type == PO_IMMEDIATE) )); + || (AOP(op)->aopu.pcop->type == PO_IMMEDIATE) ))); } /*-----------------------------------------------------------------*/ @@ -6215,10 +6618,10 @@ static bool is_LitOp(operand *op) /*-----------------------------------------------------------------*/ static bool is_LitAOp(asmop *aop) { - return (aop->type == AOP_LIT) + return ((aop->type == AOP_LIT) || ( (aop->type == AOP_PCODE) && ( (aop->aopu.pcop->type == PO_LITERAL) - || (aop->aopu.pcop->type == PO_IMMEDIATE) )); + || (aop->aopu.pcop->type == PO_IMMEDIATE) ))); } @@ -9849,13 +10252,57 @@ void pic16_loadFSR0(operand *op) /*-----------------------------------------------------------------*/ static void genUnpackBits (operand *result, operand *left, char *rname, int ptype) { - int shCnt ; - int rlen = 0 ; - sym_link *etype; - int offset = 0 ; + int shCnt ; + int rlen = 0 ; + sym_link *etype, *letype; + int blen=0, bstr=0; + int lbstr; + int offset = 0 ; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - etype = getSpec(operandType(result)); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + etype = getSpec(operandType(result)); + letype = getSpec(operandType(left)); + +// if(IS_BITFIELD(etype)) { + blen = SPEC_BLEN(etype); + bstr = SPEC_BSTR(etype); +// } + + lbstr = SPEC_BSTR( letype ); + +#if 1 + if((blen == 1) && (bstr < 8)) { + /* it is a single bit, so use the appropriate bit instructions */ + DEBUGpic16_emitcode (";","%s %d optimize bit read",__FUNCTION__,__LINE__); + + if(IS_BITFIELD(etype)/* && !IS_ITEMP(left)*/) { + pic16_emitpcode(POC_MOVFW, pic16_popGet( AOP(result), 0 )); + pic16_emitpcode(POC_BCF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr)); + } else { + pic16_emitpcode(POC_CLRF, pic16_popCopyReg(&pic16_pc_wreg)); + } + + if((ptype == POINTER) && (result)) { + /* workaround to reduce the extra lfsr instruction */ + pic16_emitpcode(POC_BTFSC, + pic16_popCopyGPR2Bit(pic16_popGet(AOP(left), 0), bstr)); + } else { + pic16_emitpcode(POC_BTFSC, + pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_indf0), bstr)); + } + + + if(IS_BITFIELD(etype)) { + pic16_emitpcode(POC_BSF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr)); + } else { + pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_wreg)); + } + + pic16_emitpcode(POC_MOVWF, pic16_popGet( AOP(result), 0 )); + return; + } + +#endif /* the following call to pic16_loadFSR0 is temporary until * optimization to handle single bit assignments is added @@ -10089,16 +10536,20 @@ static void genNearPointerGet (operand *left, if(!IS_BITFIELD(retype)) pic16_loadFSR0( left ); // patch 10 } else { - // set up FSR0 with address from left - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10 - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10 + if(!IS_BITFIELD(retype)) { + // set up FSR0 with address from left + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10 + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10 + } } } // else // rname = pic16_aopGet(AOP(left),0,FALSE,FALSE); - pic16_aopOp (result,ic,FALSE); + pic16_aopOp (result,ic,FALSE); + DEBUGpic16_pic16_AopType(__LINE__, left, NULL, result); + /* if bitfield then unpack the bits */ if (IS_BITFIELD(retype)) genUnpackBits (result, left, NULL, POINTER); @@ -10686,12 +11137,15 @@ static void genPackBits (sym_link *etype , operand *result, int offset = 0 ; int rLen = 0 ; int blen, bstr ; + sym_link *retype; char *l ; DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); blen = SPEC_BLEN(etype); bstr = SPEC_BSTR(etype); + retype = getSpec(operandType(right)); + if(AOP_TYPE(right) == AOP_LIT) { if((blen == 1) && (bstr < 8)) { unsigned long lit; @@ -10727,7 +11181,35 @@ static void genPackBits (sym_link *etype , operand *result, pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right), 0)); offset++; } else - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), offset++)); + if(IS_BITFIELD(retype) + && (AOP_TYPE(right) == AOP_REG || AOP_TYPE(right) == AOP_DIR) + && (blen == 1)) { + int rblen, rbstr; + + rblen = SPEC_BLEN( retype ); + rbstr = SPEC_BSTR( retype ); + + + if(IS_BITFIELD(etype)) { + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(result), 0)); + pic16_emitpcode(POC_BCF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr)); + } else { + pic16_emitpcode(POC_CLRF, pic16_popCopyReg(&pic16_pc_wreg)); + } + + pic16_emitpcode(POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), 0), rbstr)); + + if(IS_BITFIELD(etype)) { + pic16_emitpcode(POC_BSF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr)); + } else { + pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_wreg)); + } + + pic16_emitpcode(POC_MOVWF, pic16_popGet( AOP(result), 0)); + + return; + } else + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), offset++)); /* if the bit lenth is less than or */ /* it exactly fits a byte then */ @@ -10743,11 +11225,15 @@ static void genPackBits (sym_link *etype , operand *result, switch (p_type) { case FPOINTER: case POINTER: - case GPOINTER: pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0)); // pic16_emitcode ("mov","b,a"); // pic16_emitcode("mov","a,@%s",rname); break; + + case GPOINTER: + werror(W_POSSBUG2, __FILE__, __LINE__); + break; + } #if 1 pic16_emitpcode(POC_ANDLW, pic16_popGetLit( @@ -10962,16 +11448,19 @@ static void genNearPointerSet (operand *right, /* otherwise get a free pointer register */ DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if( (AOP_TYPE(result) == AOP_PCODE) - && ((AOP(result)->aopu.pcop->type == PO_IMMEDIATE) - || (AOP(result)->aopu.pcop->type == PO_DIR))) // patch 10 +// if( (AOP_TYPE(result) == AOP_PCODE) +// && ((AOP(result)->aopu.pcop->type == PO_IMMEDIATE) +// || (AOP(result)->aopu.pcop->type == PO_DIR))) // patch 10 + if(is_LitAOp( AOP(result) )) { if(!IS_BITFIELD(resetype)) pic16_loadFSR0( result ); // patch 10 } else { + if(!IS_BITFIELD(resetype)) { // set up FSR0 with address of result pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10 pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10 + } } } @@ -10980,6 +11469,8 @@ static void genNearPointerSet (operand *right, DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); +// pic16_loadFSR0( result ); + /* if bitfield then unpack the bits */ if (IS_BITFIELD(resetype)) { genPackBits (resetype, result, right, NULL, POINTER); @@ -11882,9 +12373,7 @@ static void genJumpTab (iCode *ic) // jump into the table pic16_emitpcode(POC_MOVWF , pic16_popCopyReg(&pic16_pc_pcl)); - // this goto prevents the next label from being removed... - pic16_emitpcode(POC_GOTO, jt_label); - pic16_emitpLabel(jtab->key); + pic16_emitpLabelFORCE(jtab->key); #endif pic16_freeAsmop(IC_JTCOND(ic),NULL,ic,TRUE); @@ -11892,7 +12381,7 @@ static void genJumpTab (iCode *ic) /* now generate the jump labels */ for (jtab = setFirstItem(IC_JTLABELS(ic)) ; jtab; jtab = setNextItem(IC_JTLABELS(ic))) { - pic16_emitcode("ljmp","%05d_DS_",jtab->key+100); +// pic16_emitcode("ljmp","%05d_DS_",jtab->key+100); pic16_emitpcode(POC_GOTO,pic16_popGetLabel(jtab->key)); } @@ -12038,19 +12527,23 @@ static void genCast (iCode *ic) operand *result = IC_RESULT(ic); sym_link *ctype = operandType(IC_LEFT(ic)); sym_link *rtype = operandType(IC_RIGHT(ic)); + sym_link *restype = operandType(IC_RESULT(ic)); operand *right = IC_RIGHT(ic); int size, offset ; DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__); + + /* if they are equivalent then do nothing */ - if (operandsEqu(IC_RESULT(ic),IC_RIGHT(ic))) - return ; +// if (operandsEqu(IC_RESULT(ic),IC_RIGHT(ic))) +// return ; pic16_aopOp(right,ic,FALSE) ; pic16_aopOp(result,ic,FALSE); DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result); + /* if the result is a bit */ if (AOP_TYPE(result) == AOP_CRY) { @@ -12114,6 +12607,11 @@ static void genCast (iCode *ic) goto release; } + if(IS_BITFIELD(getSpec(restype)) + && IS_BITFIELD(getSpec(rtype))) { + DEBUGpic16_emitcode("***", "%d casting a bit to another bit", __LINE__); + } + /* if they are the same size : or less */ if (AOP_SIZE(result) <= AOP_SIZE(right)) { diff --git a/src/pic16/gen.h b/src/pic16/gen.h index 8cb59d49..85a73503 100644 --- a/src/pic16/gen.h +++ b/src/pic16/gen.h @@ -153,6 +153,8 @@ extern unsigned pic16_fReturnSizePic; #define emitSETC pic16_emitpcode(POC_BSF, pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT)) #define emitSETDC pic16_emitpcode(POC_BSF, pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT)) +#define emitTOGC pic16_emitpcode(POC_BTG, pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT)) + int pic16_getDataSize(operand *op); void pic16_emitpcode(PIC_OPCODE poc, pCodeOp *pcop); void pic16_emitpLabel(int key); @@ -201,4 +203,7 @@ const char *pic16_pCodeOpType( pCodeOp *pcop); void dumpiCode(iCode *lic); +int inWparamList(char *s); + + #endif diff --git a/src/pic16/genarith.c b/src/pic16/genarith.c index ad1a172b..f8d34fbd 100644 --- a/src/pic16/genarith.c +++ b/src/pic16/genarith.c @@ -612,7 +612,7 @@ static void genAddLit (iCode *ic, int lit) if(carry_info) { switch(lo) { case 0: - D_POS(">>> IMPROVED and compacted"); + D_POS(">>> IMPROVED and compacted 0"); emitSKPNC; pic16_emitpcode(POC_INCF, pic16_popGet(AOP(result),offset)); D_POS("<<< IMPROVED and compacted"); @@ -624,7 +624,7 @@ static void genAddLit (iCode *ic, int lit) pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),offset)); break; default: - D_POS(">>> IMPROVED and compacted"); + D_POS(">>> IMPROVED and compacted - default"); pic16_emitpcode(POC_MOVLW,pic16_popGetLit(lo)); pic16_emitpcode(POC_ADDWFC, pic16_popGet(AOP(result),offset)); D_POS("<<< IMPROVED and compacted"); diff --git a/src/pic16/glue.c b/src/pic16/glue.c index b19127f7..f7095b03 100644 --- a/src/pic16/glue.c +++ b/src/pic16/glue.c @@ -576,7 +576,7 @@ void pic16_printGPointerType (const char *iname, const unsigned int itype, pic16_emitDS(buf, ptype, p); break; default: -// debugf("itype = %d\n", itype ); + debugf("itype = %d\n", itype ); assert( 0 ); } diff --git a/src/pic16/main.c b/src/pic16/main.c index e28d4caa..0ce3fedb 100644 --- a/src/pic16/main.c +++ b/src/pic16/main.c @@ -351,7 +351,7 @@ OPTION pic16_optionsTable[]= { { 0, USE_CRT, NULL, "use run-time initialization module"}, { 0, "--no-crt", &pic16_options.no_crt, "do not link any default run-time initialization module"}, { 0, "--gstack", &pic16_options.gstack, "trace stack pointer push/pop to overflow"}, - { 0, OFMSG_LRSUPPORT, NULL, "use support functions for local register store/restore"}, +// { 0, OFMSG_LRSUPPORT, NULL, "use support functions for local register store/restore"}, { 0, NULL, NULL, NULL} }; @@ -436,10 +436,12 @@ _pic16_parseOptions (int *pargc, char **argv, int *i) return TRUE; } +#if 0 if(ISOPT(OFMSG_LRSUPPORT)) { pic16_options.opt_flags |= OF_LR_SUPPORT; return TRUE; } +#endif return FALSE; } diff --git a/src/pic16/pcode.c b/src/pic16/pcode.c index ae9caeb1..93f4a9b8 100644 --- a/src/pic16/pcode.c +++ b/src/pic16/pcode.c @@ -175,12 +175,14 @@ extern pCodeOp *pic16_popCopyReg(pCodeOpReg *pc); pCodeOp *pic16_popCopyGPR2Bit(pCodeOp *pc, int bitval); void pic16_pCodeRegMapLiveRanges(pBlock *pb); void OptimizeLocalRegs(void); +pCodeOp *pic16_popGet2p(pCodeOp *src, pCodeOp *dst); char *dumpPicOptype(PIC_OPTYPE type); pCodeOp *pic16_popGetLit2(int, pCodeOp *); pCodeOp *pic16_popGetLit(int); pCodeOp *pic16_popGetWithString(char *); +extern int inWparamList(char *s); /****************************************************************/ @@ -826,7 +828,7 @@ pCodeInstruction pic16_pciCALL = { 0, // second literal operand POC_NOP, PCC_NONE, // inCond - PCC_NONE , // outCond + PCC_NONE, // outCond PCI_MAGIC }; @@ -3444,7 +3446,7 @@ unsigned long pic16_countInstructions(void) for(pb = the_pFile->pbHead; pb; pb = pb->next) { for(pc = pb->pcHead; pc; pc = pc->next) { - if(isPCI(pc) || isASMDIR(pc))isize += PCI(pc)->isize; + if(isPCI(pc) || isPCAD(pc))isize += PCI(pc)->isize; } } return (isize); @@ -3464,6 +3466,8 @@ static int RegCond(pCodeOp *pcop) if(!pcop) return 0; + if(!pcop->name)return 0; + if(pcop->type == PO_GPR_BIT && !strcmp(pcop->name, pic16_pc_status.pcop.name)) { switch(PCORB(pcop)->bit) { case PIC_C_BIT: @@ -4724,7 +4728,18 @@ char *pic16_get_op(pCodeOp *pcop,char *buffer, size_t size) SAFE_snprintf(&s,&size,"%s",pcop->name); } return buffer; + case PO_GPR_BIT: + s = buffer; + if(PCORB(pcop)->subtype == PO_GPR_TEMP) { + SAFE_snprintf(&s, &size, "%s", pcop->name); + } else { + if(PCORB(pcop)->pcor.instance) + SAFE_snprintf(&s, &size, "(%s + %d)", pcop->name, PCORB(pcop)->pcor.instance); + else + SAFE_snprintf(&s, &size, "%s", pcop->name); + } + return (buffer); default: if(pcop->name) { if(use_buffer) { @@ -4897,14 +4912,16 @@ char *pic16_pCode2str(char *str, size_t size, pCode *pc) } if(PCI(pc)->isBitInst) { - if(PCI(pc)->pcop->type == PO_GPR_BIT) { + if(PCI(pc)->pcop->type != PO_GPR_BIT) { if( (((pCodeOpRegBit *)(PCI(pc)->pcop))->inBitSpace) ) SAFE_snprintf(&s,&size,"(%s >> 3), (%s & 7)", PCI(pc)->pcop->name , PCI(pc)->pcop->name ); else - SAFE_snprintf(&s,&size,"%s,%d", pic16_get_op_from_instruction(PCI(pc)), + SAFE_snprintf(&s,&size,"%s,%d", pic16_get_op_from_instruction(PCI(pc)), +// (((pCodeOpRegBit *)(PCI(pc)->pcop))->pcor.instance), (((pCodeOpRegBit *)(PCI(pc)->pcop))->bit )); + } else if(PCI(pc)->pcop->type == PO_GPR_BIT) { SAFE_snprintf(&s,&size,"%s,%d", pic16_get_op_from_instruction(PCI(pc)),PCORB(PCI(pc)->pcop)->bit); }else @@ -5019,15 +5036,19 @@ static void genericPrint(FILE *of, pCode *pc) } } - fprintf(of, "; info ==>"); - switch(((pCodeInfo *)pc)->type) { - case INF_OPTIMIZATION: - fprintf(of, " [optimization] %s\n", OPT_TYPE_STR[ PCOO(PCINF(pc)->oper1)->type ]); - break; - case INF_LOCALREGS: - fprintf(of, " [localregs] %s\n", LR_TYPE_STR[ PCOLR(PCINF(pc)->oper1)->type ]); - break; - }; break; + if(pic16_pcode_verbose) { + fprintf(of, "; info ==>"); + switch(((pCodeInfo *)pc)->type) { + case INF_OPTIMIZATION: + fprintf(of, " [optimization] %s\n", OPT_TYPE_STR[ PCOO(PCINF(pc)->oper1)->type ]); + break; + case INF_LOCALREGS: + fprintf(of, " [localregs] %s\n", LR_TYPE_STR[ PCOLR(PCINF(pc)->oper1)->type ]); + break; + } + }; + + break; case PC_INLINE: fprintf(of,"%s\n", ((pCodeComment *)pc)->comment); @@ -6046,14 +6067,14 @@ static int isBankInstruction(pCode *pc) int bank = -1; if(!isPCI(pc)) - return -1; + return 0; if( PCI(pc)->op == POC_MOVLB || (( (reg = pic16_getRegFromInstruction(pc)) != NULL) && isBSR_REG(reg))) { bank = PCOL(pc)->lit; } - return bank; + return 1; } @@ -6220,7 +6241,7 @@ int pic16_isPCinFlow(pCode *pc, pCode *pcflow) if(!pc || !pcflow) return 0; - if(!isPCI(pc) || !PCI(pc)->pcflow || !isPCFL(pcflow) ) + if((!isPCI(pc) && !isPCAD(pc)) || !PCI(pc)->pcflow || !isPCFL(pcflow) ) return 0; if( PCI(pc)->pcflow->pc.seq == pcflow->seq) @@ -6725,16 +6746,24 @@ const char *pic16_pCodeOpSubType(pCodeOp *pcop); pCodeOp *pic16_popCopyGPR2Bit(pCodeOp *pc, int bitval) { - pCodeOp *pcop; + pCodeOp *pcop=NULL; + +// fprintf(stderr, "%s:%d pc type: %s\tname: %s\n", __FILE__, __LINE__, pic16_pCodeOpType(pc), pc->name); -// fprintf(stderr, "%s:%d pc type: %s\n", __FILE__, __LINE__, pic16_pCodeOpType(pc)); - pcop = pic16_newpCodeOpBit(pc->name, bitval, 0, pc->type); + if(pc->name) { + pcop = pic16_newpCodeOpBit(pc->name, bitval, 0, pc->type); + } else { + if(PCOR(pc)->r)pcop = pic16_newpCodeOpBit(PCOR(pc)->r->name, bitval, 0, pc->type); + } + + assert(pcop != NULL); if( !( (pcop->type == PO_LABEL) || (pcop->type == PO_LITERAL) || (pcop->type == PO_STR) )) PCOR(pcop)->r = PCOR(pc)->r; /* This is dangerous... */ PCOR(pcop)->r->wasUsed = 1; + PCOR(pcop)->instance = PCOR(pc)->instance; return pcop; } @@ -7284,9 +7313,21 @@ static void pic16_convertLocalRegs2Support(pCode *pcstart, pCode *pcend, int cou pcstart->next = pcend; pcend->prev = pcstart; - pic16_pCodeInsertAfter(pcstart, (pct=pic16_newpCode(POC_LFSR, pic16_popGetLit2(0, pic16_popGetWithString(r->name))))); pc = pct; + pc = pcstart; +// if(!entry) { +// pic16_pCodeInsertAfter(pc, pct = pic16_newpCode(POC_MOVFF, pic16_popGet2p( +// pic16_popCopyReg(&pic16_pc_fsr0l), pic16_popCopyReg(&pic16_pc_fsr2l)))); pc = pct; +// } + + pic16_pCodeInsertAfter(pc, pct=pic16_newpCode(POC_LFSR, pic16_popGetLit2(0, pic16_popGetWithString(r->name)))); pc = pct; pic16_pCodeInsertAfter(pc, pct=pic16_newpCode(POC_MOVLW, pic16_popGetLit( count ))); pc = pct; - pic16_pCodeInsertAfter(pc, pct=pic16_newpCode(POC_CALL, pic16_popGetWithString( fname[ (entry==1?0:1) ] ))); + pic16_pCodeInsertAfter(pc, pct=pic16_newpCode(POC_CALL, pic16_popGetWithString( fname[ (entry==1?0:1) ] ))); pc = pct; + +// if(!entry) { +// pic16_pCodeInsertAfter(pc, pct = pic16_newpCode(POC_MOVFF, pic16_popGet2p( +// pic16_popCopyReg(&pic16_pc_fsr2l), pic16_popCopyReg(&pic16_pc_fsr0l)))); pc = pct; +// } + { symbol *sym; @@ -7300,7 +7341,6 @@ static void pic16_convertLocalRegs2Support(pCode *pcstart, pCode *pcend, int cou } - /*-----------------------------------------------------------------*/ /* OptimizeLocalRegs - turn sequence of MOVFF instructions for */ /* local registers to a support function call */ @@ -7316,6 +7356,7 @@ void pic16_OptimizeLocalRegs(void) regs *r, *lastr=NULL, *firstr=NULL; pCode *pcstart=NULL, *pcend=NULL; int inEntry=0; + char *curFunc=NULL; /* Overview: * local_regs begin mark @@ -7340,6 +7381,10 @@ void pic16_OptimizeLocalRegs(void) inRegCount = regCount = 0; firstr = lastr = NULL; for(pc = pb->pcHead; pc; pc = pc->next) { + + /* hold current function name */ + if(pc && isPCF(pc))curFunc = PCF(pc)->fname; + if(pc && (pc->type == PC_INFO)) { pci = PCINF(pc); @@ -7364,9 +7409,14 @@ void pic16_OptimizeLocalRegs(void) pcend = pc; //pic16_findPrevInstruction(pc->prev); #if 1 - if(regCount>2) { - pic16_convertLocalRegs2Support(pcstart, pcend, regCount, - firstr, inEntry); + if(curFunc && inWparamList(curFunc+1)) { + fprintf(stderr, "sdcc: %s: warning: disabling lr-support for functionn %s\n", + filename, curFunc); + } else { + if(regCount>2) { + pic16_convertLocalRegs2Support(pcstart, pcend, regCount, + firstr, inEntry); + } } #endif firstr = lastr = NULL; diff --git a/src/pic16/pcode.h b/src/pic16/pcode.h index 2a547d76..ae1e1ea5 100644 --- a/src/pic16/pcode.h +++ b/src/pic16/pcode.h @@ -958,7 +958,7 @@ typedef struct peepCommand { #define isPCL(x) ((PCODE(x)->type == PC_LABEL)) #define isPCW(x) ((PCODE(x)->type == PC_WILD)) #define isPCCS(x) ((PCODE(x)->type == PC_CSOURCE)) -#define isASMDIR(x) ((PCODE(x)->type == PC_ASMDIR)) +#define isPCAD(x) ((PCODE(x)->type == PC_ASMDIR)) #define isCALL(x) ((isPCI(x)) && (PCI(x)->op == POC_CALL)) #define isSTATUS_REG(r) ((r)->pc_type == PO_STATUS) @@ -1018,6 +1018,9 @@ pCode * pic16_findNextpCode(pCode *pc, PC_TYPE pct); int pic16_isPCinFlow(pCode *pc, pCode *pcflow); struct regs * pic16_getRegFromInstruction(pCode *pc); struct regs * pic16_getRegFromInstruction2(pCode *pc); +char *pic16_get_op(pCodeOp *pcop,char *buffer, size_t size); +char *pic16_get_op2(pCodeOp *pcop,char *buffer, size_t size); +char *dumpPicOptype(PIC_OPTYPE type); extern void pic16_pcode_test(void); extern int pic16_debug_verbose; @@ -1025,7 +1028,7 @@ extern int pic16_pcode_verbose; #ifndef debugf //#define debugf(frm, rest...) _debugf(__FILE__, __LINE__, frm, rest) -#define debugf(frm, rest) _debug(__FILE__, __LINE__, frm, rest) +#define debugf(frm, rest) _debugf(__FILE__, __LINE__, frm, rest) #endif extern void _debugf(char *f, int l, char *frm, ...); diff --git a/src/pic16/pcodepeep.c b/src/pic16/pcodepeep.c index 7be49b9a..6660a28e 100644 --- a/src/pic16/pcodepeep.c +++ b/src/pic16/pcodepeep.c @@ -48,7 +48,7 @@ int pic16_getpCode(char *mnem,int dest); int pic16_getpCodePeepCommand(char *cmd); void pic16_pBlockMergeLabels(pBlock *pb); char *pic16_pCode2str(char *str, int size, pCode *pc); -char *pic16_get_op( pCodeOp *pcop,char *buf,int buf_size); +//char *pic16_get_op(pCodeOp *pcop,char *buf, size_t buf_size); pCodeOp *pic16_popCombine2(pCodeOp *, pCodeOp *, int); extern pCodeInstruction *pic16Mnemonics[]; diff --git a/src/pic16/pcoderegs.c b/src/pic16/pcoderegs.c index c17a9d02..df21a9a0 100644 --- a/src/pic16/pcoderegs.c +++ b/src/pic16/pcoderegs.c @@ -177,7 +177,7 @@ static void pCodeRegMapLiveRangesInFlow(pCodeFlow *pcfl) #endif -// fprintf(stderr, "trying to get first operand from pCode reg= %s\n", reg->name); +// fprintf(stderr, "%s:%d: trying to get first operand from pCode reg= %s\n", __FILE__, __LINE__, reg->name); addSetIfnotP(& (PCFL(pcfl)->registers), reg); if((PCC_REGISTER | PCC_LITERAL) & PCI(pc)->inCond) @@ -194,7 +194,7 @@ static void pCodeRegMapLiveRangesInFlow(pCodeFlow *pcfl) if(PCI(pc)->is2MemOp) { reg = pic16_getRegFromInstruction2(pc); if(reg) { -// fprintf(stderr, "trying to get second operand from pCode reg= %s\n", reg->name); +// fprintf(stderr, "%s:%d: trying to get second operand from pCode reg= %s\n", __FILE__, __LINE__, reg->name); addSetIfnotP(& (PCFL(pcfl)->registers), reg); if((PCC_REGISTER | PCC_LITERAL) & PCI(pc)->inCond) diff --git a/src/pic16/peeph.def b/src/pic16/peeph.def index e904c3c4..511e2efb 100644 --- a/src/pic16/peeph.def +++ b/src/pic16/peeph.def @@ -285,9 +285,3 @@ replace restart { movf %1,w } -replace restart { - movff %1,%1 -} by { - ; peep 12 - Removed redundant move -} - diff --git a/src/pic16/ralloc.c b/src/pic16/ralloc.c index e898f471..e25e89ca 100644 --- a/src/pic16/ralloc.c +++ b/src/pic16/ralloc.c @@ -37,6 +37,12 @@ #define STRCASECMP strcasecmp #endif +#ifndef debugf +#define debugf(frm, rest) _debugf(__FILE__, __LINE__, frm, rest) +#endif +void _debugf(char *f, int l, char *frm, ...); + + //#define USE_ONSTACK @@ -103,7 +109,9 @@ int pic16_Gstack_base_addr=0; /* The starting address of registers that * are used to pass and return parameters */ - +int _inRegAllocator=0; /* flag that marks whther allocReg happens while + * inside the register allocator function */ + static void spillThis (symbol *); int pic16_ralloc_debug = 0; @@ -486,6 +494,8 @@ pic16_allocInternalRegister(int rIdx, char * name, short po_type, int alias) return NULL; } + + /*-----------------------------------------------------------------*/ /* allocReg - allocates register of given type */ /*-----------------------------------------------------------------*/ @@ -494,6 +504,9 @@ allocReg (short type) { regs * reg=NULL; +#define MAX_P16_NREGS 6 + + #if 0 if(dynrIdx > pic16_nRegs) return NULL; @@ -503,11 +516,20 @@ allocReg (short type) reg = regFindFree( pic16_dynAllocRegs ); if(reg) { -// fprintf(stderr, "%s: found FREE register %s\n", __FILE__, reg->name); +// fprintf(stderr, "%s: [%s] found FREE register %s, rIdx: %d\n", __FILE__, (_inRegAllocator)?"ralloc":"", reg->name, reg->rIdx); } if(!reg) { reg = newReg(REG_GPR, PO_GPR_TEMP, dynrIdx++, NULL, 1, 0, NULL); +// fprintf(stderr, "%s: [%s] allocating NEW register %s, rIdx: %d\n", __FILE__, (_inRegAllocator)?"ralloc":"", reg->name, reg->rIdx); + +#if 1 + if(_inRegAllocator && (dynrIdx > MAX_P16_NREGS)) { +// debugf("allocating more registers than available\n", 0); +// return (NULL); + } +#endif + // addSet(&pic16_dynAllocRegs, reg); } @@ -2324,7 +2346,6 @@ serialRegAssign (eBBlock ** ebbs, int count) /* else we assign registers to it */ _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key); - debugLog (" %d - nRegs: %d\n", __LINE__, sym->nRegs); if(debugF) bitVectDebugOn(_G.regAssigned, debugF); @@ -2677,7 +2698,6 @@ regTypeNum () if (IS_AGGREGATE (sym->type) || sym->isptr) sym->type = aggrToPtr (sym->type, FALSE); debugLog (" %d - no reg needed - used as a return\n", __LINE__); - continue; } @@ -2755,6 +2775,7 @@ regTypeNum () /* registers for true symbols we will */ /* see how things go */ sym->nRegs = 0; + } } @@ -4150,6 +4171,8 @@ pic16_assignRegisters (eBBlock ** ebbs, int count) debugLog ("\nebbs before optimizing:\n"); dumpEbbsToDebug (ebbs, count); + _inRegAllocator = 1; + setToNull ((void *) &_G.funcrUsed); pic16_ptrRegReq = _G.stackExtend = _G.dataExtend = 0; @@ -4227,6 +4250,7 @@ pic16_assignRegisters (eBBlock ** ebbs, int count) debugLog ("ebbs after optimizing:\n"); dumpEbbsToDebug (ebbs, count); + _inRegAllocator = 0; genpic16Code (ic); @@ -4238,6 +4262,7 @@ pic16_assignRegisters (eBBlock ** ebbs, int count) /* mark all registers as free */ pic16_freeAllRegs (); + debugLog ("leaving\n<><><><><><><><><><><><><><><><><>\n"); debugLogClose (); return; -- 2.30.2