version 4.2.0
[fx2fw-sdcc] / startup.a51
1 ;;; -*- asm -*-\r
2 ;;;\r
3 ;;;-----------------------------------------------------------------------------\r
4 ;;; Startup code\r
5 ;;;-----------------------------------------------------------------------------\r
6 ;;; Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,\r
7 ;;; Copyright 2003 Free Software Foundation, Inc.\r
8 ;;;-----------------------------------------------------------------------------\r
9 ;;; This code is part of usbjtag. usbjtag is free software; you can redistribute\r
10 ;;; it and/or modify it under the terms of the GNU General Public License as\r
11 ;;; published by the Free Software Foundation; either version 2 of the License,\r
12 ;;; or (at your option) any later version. usbjtag is distributed in the hope\r
13 ;;; that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
14 ;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 ;;; GNU General Public License for more details.  You should have received a\r
16 ;;; copy of the GNU General Public License along with this program in the file\r
17 ;;; COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
18 ;;; St, Fifth Floor, Boston, MA  02110-1301  USA\r
19 ;;;-----------------------------------------------------------------------------\r
20 \r
21 ;;; The default external memory initialization provided by sdcc is not\r
22 ;;; appropriate to the FX2.  This is derived from the sdcc code, but uses \r
23 ;;; the FX2 specific _MPAGE sfr.\r
24 \r
25 \r
26         ;; .area XISEG   (XDATA)  ; the initialized external data area\r
27         ;; .area XINIT   (CODE)          ; the code space consts to init XISEG\r
28         .area XSEG    (XDATA)          ; zero initialized xdata\r
29         .area USBDESCSEG (XDATA)  ; usb descriptors\r
30 \r
31 \r
32         .area CSEG    (CODE)\r
33 \r
34         ;; sfr that sets upper address byte of MOVX using @r0 or @r1\r
35         _MPAGE        =        0x0092\r
36 \r
37 __sdcc_external_startup::\r
38         ;; This system is now compiled with the --no-xinit-opt \r
39         ;; which means that any initialized XDATA is handled\r
40         ;; inline by code in the GSINIT segs emitted for each file.\r
41         ;; \r
42         ;; We zero XSEG and all of the internal ram to ensure \r
43         ;; a known good state for uninitialized variables.\r
44 \r
45 ;        _mcs51_genRAMCLEAR() start\r
46         mov        r0,#l_XSEG\r
47         mov        a,r0\r
48         orl        a,#(l_XSEG >> 8)\r
49         jz        00002$\r
50         mov        r1,#((l_XSEG + 255) >> 8)\r
51         mov        dptr,#s_XSEG\r
52         clr     a\r
53         \r
54 00001$:        movx        @dptr,a\r
55         inc        dptr\r
56         djnz        r0,00001$\r
57         djnz        r1,00001$\r
58         \r
59         ;; We're about to clear internal memory.  This will overwrite\r
60         ;; the stack which contains our return address.\r
61         ;; Pop our return address into DPH, DPL\r
62 00002$:        pop        dph\r
63         pop        dpl\r
64         \r
65         ;; R0 and A contain 0.  This loop will execute 256 times.\r
66         ;; \r
67         ;; FWIW the first iteration writes direct address 0x00,\r
68         ;; which is the location of r0.  We get lucky, we're \r
69         ;; writing the correct value (0)\r
70         \r
71 00003$:        mov        @r0,a\r
72         djnz        r0,00003$\r
73 \r
74         push        dpl                ; restore our return address\r
75         push        dph\r
76 \r
77         mov        dpl,#0                ; indicate that data init is still required\r
78         ret\r