1/* The startup code sample of Andes NDS32 cpu for GNU compiler
2   Copyright (C) 2012-2018 Free Software Foundation, Inc.
3   Contributed by Andes Technology Corporation.
4
5   This file is part of GCC.
6
7   GCC is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published
9   by the Free Software Foundation; either version 3, or (at your
10   option) any later version.
11
12   GCC is distributed in the hope that it will be useful, but WITHOUT
13   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15   License for more details.
16
17   Under Section 7 of GPL version 3, you are granted additional
18   permissions described in the GCC Runtime Library Exception, version
19   3.1, as published by the Free Software Foundation.
20
21   You should have received a copy of the GNU General Public License and
22   a copy of the GCC Runtime Library Exception along with this program;
23   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24   <http://www.gnu.org/licenses/>.  */
25
26!!==============================================================================
27!!
28!!      crtzero.S
29!!
30!!      This is JUST A SAMPLE of nds32 startup code !!
31!!      You can refer this content and implement
32!!      the actual one in newlib/mculib.
33!!
34!!==============================================================================
35
36!!------------------------------------------------------------------------------
37!! Jump to start up code
38!!------------------------------------------------------------------------------
39	.section	.nds32_init, "ax"
40	j	_start
41
42!!------------------------------------------------------------------------------
43!! Startup code implementation
44!!------------------------------------------------------------------------------
45	.section	.text
46	.global	_start
47	.weak	_SDA_BASE_
48	.weak	_FP_BASE_
49	.align	2
50	.func	_start
51	.type	_start, @function
52_start:
53.L_fp_gp_lp_init:
54	la	$fp, _FP_BASE_		! init $fp
55	la	$gp, _SDA_BASE_		! init $gp for small data access
56	movi	$lp, 0			! init $lp
57
58.L_stack_init:
59	la	$sp, _stack		! init $sp
60	movi	$r0, -8			! align $sp to 8-byte (use 0xfffffff8)
61	and	$sp, $sp, $r0		! align $sp to 8-byte (filter out lower 3-bit)
62
63.L_bss_init:
64	! clear BSS, this process can be 4 time faster if data is 4 byte aligned
65	! if so, use swi.p instead of sbi.p
66	! the related stuff are defined in linker script
67	la	$r0, _edata		! get the starting addr of bss
68	la	$r2, _end		! get ending addr of bss
69	beq	$r0, $r2, .L_call_main	! if no bss just do nothing
70	movi	$r1, 0			! should be cleared to 0
71.L_clear_bss:
72	sbi.p	$r1, [$r0], 1		! Set 0 to bss
73	bne	$r0, $r2, .L_clear_bss	! Still bytes left to set
74
75!.L_stack_heap_check:
76!	la	$r0, _end		! init heap_end
77!	s.w	$r0, heap_end		! save it
78
79
80!.L_init_argc_argv:
81!	! argc/argv initialization if necessary; default implementation is in crt1.o
82!	la	$r9, _arg_init		! load address of _arg_init?
83!	beqz	$r9, .L4		! has _arg_init? no, go check main()
84!	addi	$sp, $sp, -512		! allocate space for command line + arguments
85!	move	$r6, $sp		! r6 = buffer addr of cmd line
86!	move	$r0, $r6		! r0 = buffer addr of cmd line
87!	syscall	6002			! get cmd line
88!	move	$r0, $r6		! r0 = buffer addr of cmd line
89!	addi	$r1, $r6, 256		! r1 = argv
90!	jral	$r9			! init argc/argv
91!	addi	$r1, $r6, 256		! r1 = argv
92
93.L_call_main:
94	! call main() if main() is provided
95	la	$r15, main		! load address of main
96	jral	$r15			! call main
97
98.L_terminate_program:
99	syscall	0x1			! use syscall 0x1 to terminate program
100	.size	_start, .-_start
101	.end
102
103!! ------------------------------------------------------------------------
104