summaryrefslogtreecommitdiff
path: root/entry.cpp
blob: e819c781fb5e92f12411974c8444706c01e389a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdint.h>
#include "rcc.h"

int main();

typedef void (*funcp_t)();

// Symbols from linker script.
extern uint32_t _data_rom;
extern uint32_t _data_start;
extern uint32_t _data_end;
extern uint32_t _bss_start;
extern uint32_t _bss_end;
extern funcp_t _init_array_start;
extern funcp_t _init_array_end;
extern funcp_t _fini_array_start;
extern funcp_t _fini_array_end;

void __attribute__((naked)) entry() {
	// Initialize clock.
	rcc_init();
	
	// Load .data from rom image.
	uint32_t* rp = &_data_rom;
	uint32_t* wp = &_data_start;
	
	while(wp < &_data_end) {
		*wp++ = *rp++;
	}
	
	// Clear .bss.
	wp = &_bss_start;
	
	while(wp < &_bss_end) {
		*wp++ = 0;
	}
	
	// Call constructors.
	funcp_t* fp = &_init_array_start;
	
	while(fp < &_init_array_end) {
		(*fp++)();
	}
	
	// Call main().
	main();
	
	// Call destructors.
	fp = &_fini_array_start;
	
	while(fp < &_fini_array_end) {
		(*fp++)();
	}
	
	// Halt.
	while(1);
}