#
# GNU PIC Programmer & Debugger
# The PIC Microcontroler Library
#
# Copyright (C) 2003 Sylvain Giroudon
# Creation: 29-NOV-2002
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.  
#

#
# $Revision: 1.6 $
# $Date: 2004/07/19 11:26:17 $
#

.SUFFIXES: .c .o .asm .cod
-include ../Makefile.defs

#GLIB = glib
GLIB = glib-2.0

ifdef INSTALLDIR
CFLAGS = -DLICENSEFILE=\"$(INSTALLROOT)/share/doc/gpicd-$(VERSION)-$(RELEASE)/LICENSE\"
else
CFLAGS = -DLICENSEFILE=\"$(dir $(PWD))doc/LICENSE\"
endif

CFLAGS += -D__DEBUG__ -Wall -O2 -I.. $(shell pkg-config --cflags $(GLIB))
AFLAGS = -p P16F877 -D__ASM__
LDFLAGS = $(shell pkg-config --libs $(GLIB))

all: gpicd.a

clean:
	$(RM) *~ *.o *.a *.cod *.lst *.hex
	$(RM) dmon.c

gpicd.a: license.o \
	 icsp.o icd.o dbg.o errmsg.o cod.o dmon.o config_icsp.o config_proc.o config_data.o
	$(AR) -rv $@ $^

dmon.c: dmon.cod

.asm.cod:
	$(AS) $(AFLAGS) $<

.cod.c:
	echo "#define VAR(_id_) $(@:%.c=%)_ ## _id_" > $@
	gpvc -r $< | awk '/^.+: / { print "const unsigned long VAR(code_org) = 0x"$$1";" }' | head -1 | sed 's/://g' >> $@
	echo "const unsigned short VAR(code)[] = {" >> $@
	gpvc -r $< | awk '/^.+: / { print "0x"$$2",0x"$$3",0x"$$4",0x"$$5",0x"$$6",0x"$$7",0x"$$8",0x"$$9"," }' >> $@
	echo "};" >> $@
	gpvc -r $< | awk '/^using ROM / { print "const unsigned long VAR(addr) = "$$3";" }' >> $@
	gpvc -r $< | awk '/^using ROM / { print "const unsigned long VAR(top)  = "$$5";" }' >> $@
	gpvc -s $< | awk '/^SAVE_W / { print "const unsigned long VAR(W) = 0x"$$3";" }' | sed s/,// >> $@
	gpvc -s $< | awk '/^SAVE_STATUS / { print "const unsigned long VAR(STATUS) = 0x"$$3";" }' | sed s/,// >> $@
	gpvc -s $< | awk '/^SAVE_PCLATH / { print "const unsigned long VAR(PCLATH) = 0x"$$3";" }' | sed s/,// >> $@
	gpvc -s $< | awk '/^SAVE_FSR / { print "const unsigned long VAR(FSR) = 0x"$$3";" }' | sed s/,// >> $@
