;;; -*- asm -*-
;;;
;;; Copyright 2003 Free Software Foundation, Inc.
;;; 
;;; This file is part of GNU Radio
;;; 
;;; GNU Radio 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 3, or (at your option)
;;; any later version.
;;; 
;;; GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
;;; the Free Software Foundation, Inc., 51 Franklin Street,
;;; Boston, MA 02110-1301, USA.

    
;;; The default external memory initialization provided by sdcc is not
;;; appropriate to the FX2.  This is derived from the sdcc code, but uses 
;;; the FX2 specific _MPAGE sfr.


	.area XISEG   (XDATA)	; the initialized external data area
	.area XINIT   (CODE)	; the code space consts to init XISEG
	.area XSEG    (XDATA)	; zero initialized xdata
	.area USBDESCSEG (XDATA); usb descriptors

	
	;; BIG TIME KLUDGE!
	;; Look at usrp_main.rst and count the bytes from our
	;; "normal return location" to the first instruction following
	;; the comment:	"_mcs51_getRAMCLEAR () start"
	
	INSTRUCTION_BYTES_TO_SKIP = 0x29	 ; valid for sdcc 2.4.0 
	

	.area CSEG    (CODE)

	;; sfr that sets upper address byte of MOVX using @r0 or @r1
	_MPAGE	=	0x0092

__sdcc_external_startup::
;	_mcs51_genXINIT() start
	mov	r1,#l_XINIT
	mov	a,r1
	orl	a,#(l_XINIT >> 8)
	jz	00003$
	mov	r2,#((l_XINIT+255) >> 8)
	mov	dptr,#s_XINIT
	mov	r0,#s_XISEG
	mov	_MPAGE,#(s_XISEG >> 8)
00001$:	clr	a
	movc	a,@a+dptr
	movx	@r0,a
	inc	dptr
	inc	r0
	cjne	r0,#0,00002$
	inc	_MPAGE
00002$:	djnz	r1,00001$
	djnz	r2,00001$
	mov	_MPAGE,#0xFF
00003$:

	;; Danger! Total KLUDGE!
	;; We pop the return address, add a magic number to it
	;; then jump to that address.  Believe it or not, this
	;; looks like the least kludgy way to handle this,
	;; short of patching the compiler...

	pop	dph
	pop	dpl
	mov	a,#INSTRUCTION_BYTES_TO_SKIP
	jmp	@a+dptr
