d5d68dd829064e767967eeb7c43a0b0cffb97bfb
[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 _exit::
49         ;; Exit - special code to the emulator
50         ld      a,#0
51         rst     0x08
52 1$:
53         halt
54         jr      1$
55
56         ;; Special RLE decoder used for initing global data
57 __initrleblock::
58         ;; Pop the return address
59         pop     hl
60         ;; Save registers
61         push    bc
62         push    de
63
64         ;; Pull the destination address out
65         ld      c,(hl)
66         inc     hl
67         ld      b,(hl)
68         inc     hl
69 1$:
70         ;; Fetch the run
71         ld      e,(hl)
72         inc     hl
73         ;; Negative means a run
74         bit     7,e
75         jp      z,2$
76         ;; Code for expanding a run
77         ld      a,(hl)
78         inc     hl
79 3$:
80         ld      (bc),a
81         inc     bc
82         inc     e
83         jp      nz,3$
84         jp      1$
85 2$:
86         ;; Zero means end of a block
87         xor     a
88         or      e
89         jp      z,4$
90         ;; Code for expanding a block
91 5$:     
92         ld      a,(hl)        
93         inc     hl
94         ld      (bc),a
95         inc     bc
96         dec     e
97         jp      nz,5$
98         jp      1$
99 4$:     
100         pop     de
101         pop     bc
102
103         ;; Push the return address back onto the stack
104         push    hl
105         ret
106
107         .area   _GSINIT
108 gsinit::        
109
110         .area   _GSFINAL
111         ret