23843568f76eec4bf474141185bc912f3c3102cf
[fw/sdcc] / device / lib / z80 / crt0.s
1         ;; Generic crt0.s for a Z80
2         .globl  _main
3
4         .area _HEADER (ABS)
5         ;; Reset vector
6         .org    0
7         jp      init
8
9         .org    0x08
10         reti
11         .org    0x10
12         reti
13         .org    0x18
14         reti
15         .org    0x20
16         reti
17         .org    0x28
18         reti
19         .org    0x30
20         reti
21         .org    0x38
22         reti
23         
24         .org    0x100
25 init:
26         ;; Stack at the top of memory.
27         ld      sp,#0xffff        
28
29         ;; Initialise global variables
30         call    gsinit
31         call    _main
32         jp      _exit
33
34         ;; Ordering of segments for the linker.
35         .area   _CODE
36         .area   _GSINIT
37         .area   _GSFINAL
38         
39         .area   _DATA
40         .area   _BSS
41
42         .area   _CODE
43 __clock::
44         ld      a,#2
45         rst     0x08
46         ret
47         
48 _getsp::
49         ld      hl,#0
50         add     hl,sp
51         ret
52
53 __printTStates::        
54         ld      a,#3
55         out     (0xff),a
56         ret
57
58 _exit::
59         ;; Exit - special code to the emulator
60         ld      a,#0
61         rst     0x08
62 1$:
63         halt
64         jr      1$
65
66         ;; Special RLE decoder used for initing global data
67 __initrleblock::
68         ;; Pop the return address
69         pop     hl
70         ;; Save registers
71         push    bc
72         push    de
73
74         ;; Pull the destination address out
75         ld      c,(hl)
76         inc     hl
77         ld      b,(hl)
78         inc     hl
79 1$:
80         ;; Fetch the run
81         ld      e,(hl)
82         inc     hl
83         ;; Negative means a run
84         bit     7,e
85         jp      z,2$
86         ;; Code for expanding a run
87         ld      a,(hl)
88         inc     hl
89 3$:
90         ld      (bc),a
91         inc     bc
92         inc     e
93         jp      nz,3$
94         jp      1$
95 2$:
96         ;; Zero means end of a block
97         xor     a
98         or      e
99         jp      z,4$
100         ;; Code for expanding a block
101 5$:     
102         ld      a,(hl)        
103         inc     hl
104         ld      (bc),a
105         inc     bc
106         dec     e
107         jp      nz,5$
108         jp      1$
109 4$:     
110         pop     de
111         pop     bc
112
113         ;; Push the return address back onto the stack
114         push    hl
115         ret
116
117         .area   _GSINIT
118 gsinit::        
119
120         .area   _GSFINAL
121         ret