version 4.1.0 from http://fpga4u.epfl.ch/wiki/FX2
[fx2fw-sdcc] / startup.a51
1 ;;; -*- asm -*-\r
2 \r
3 ;;;\r
4 \r
5 ;;;-----------------------------------------------------------------------------\r
6 \r
7 ;;; Startup code\r
8 \r
9 ;;;-----------------------------------------------------------------------------\r
10 \r
11 ;;; Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,\r
12 \r
13 ;;; Copyright 2003 Free Software Foundation, Inc.\r
14 \r
15 ;;;-----------------------------------------------------------------------------\r
16 \r
17 ;;; This code is part of usbjtag. usbjtag is free software; you can redistribute\r
18 \r
19 ;;; it and/or modify it under the terms of the GNU General Public License as\r
20 \r
21 ;;; published by the Free Software Foundation; either version 2 of the License,\r
22 \r
23 ;;; or (at your option) any later version. usbjtag is distributed in the hope\r
24 \r
25 ;;; that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
26 \r
27 ;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
28 \r
29 ;;; GNU General Public License for more details.  You should have received a\r
30 \r
31 ;;; copy of the GNU General Public License along with this program in the file\r
32 \r
33 ;;; COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
34 \r
35 ;;; St, Fifth Floor, Boston, MA  02110-1301  USA\r
36 \r
37 ;;;-----------------------------------------------------------------------------\r
38 \r
39 \r
40 \r
41 ;;; The default external memory initialization provided by sdcc is not\r
42 \r
43 ;;; appropriate to the FX2.  This is derived from the sdcc code, but uses \r
44 \r
45 ;;; the FX2 specific _MPAGE sfr.\r
46 \r
47 \r
48 \r
49 \r
50 \r
51         ;; .area XISEG   (XDATA)  ; the initialized external data area\r
52 \r
53         ;; .area XINIT   (CODE)          ; the code space consts to init XISEG\r
54 \r
55         .area XSEG    (XDATA)          ; zero initialized xdata\r
56 \r
57         .area USBDESCSEG (XDATA)  ; usb descriptors\r
58 \r
59 \r
60 \r
61         \r
62 \r
63         .area CSEG    (CODE)\r
64 \r
65 \r
66 \r
67         ;; sfr that sets upper address byte of MOVX using @r0 or @r1\r
68 \r
69         _MPAGE        =        0x0092\r
70 \r
71 \r
72 \r
73 __sdcc_external_startup::\r
74 \r
75         ;; This system is now compiled with the --no-xinit-opt \r
76 \r
77         ;; which means that any initialized XDATA is handled\r
78 \r
79         ;; inline by code in the GSINIT segs emitted for each file.\r
80 \r
81         ;; \r
82 \r
83         ;; We zero XSEG and all of the internal ram to ensure \r
84 \r
85         ;; a known good state for uninitialized variables.\r
86 \r
87 \r
88 \r
89 ;        _mcs51_genRAMCLEAR() start\r
90 \r
91         mov        r0,#l_XSEG\r
92 \r
93         mov        a,r0\r
94 \r
95         orl        a,#(l_XSEG >> 8)\r
96 \r
97         jz        00002$\r
98 \r
99         mov        r1,#((l_XSEG + 255) >> 8)\r
100 \r
101         mov        dptr,#s_XSEG\r
102 \r
103         clr     a\r
104 \r
105         \r
106 \r
107 00001$:        movx        @dptr,a\r
108 \r
109         inc        dptr\r
110 \r
111         djnz        r0,00001$\r
112 \r
113         djnz        r1,00001$\r
114 \r
115         \r
116 \r
117         ;; We're about to clear internal memory.  This will overwrite\r
118 \r
119         ;; the stack which contains our return address.\r
120 \r
121         ;; Pop our return address into DPH, DPL\r
122 \r
123 00002$:        pop        dph\r
124 \r
125         pop        dpl\r
126 \r
127         \r
128 \r
129         ;; R0 and A contain 0.  This loop will execute 256 times.\r
130 \r
131         ;; \r
132 \r
133         ;; FWIW the first iteration writes direct address 0x00,\r
134 \r
135         ;; which is the location of r0.  We get lucky, we're \r
136 \r
137         ;; writing the correct value (0)\r
138 \r
139         \r
140 \r
141 00003$:        mov        @r0,a\r
142 \r
143         djnz        r0,00003$\r
144 \r
145 \r
146 \r
147         push        dpl                ; restore our return address\r
148 \r
149         push        dph\r
150 \r
151 \r
152 \r
153         mov        dpl,#0                ; indicate that data init is still required\r
154 \r
155         ret\r
156 \r