1/*
2
3Copyright (c) 2011 Red Hat Incorporated.
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9    Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11
12    Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15
16    The name of Red Hat Incorporated may not be used to endorse
17    or promote products derived from this software without specific
18    prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23DISCLAIMED.  IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY
24DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31*/
32
33#ifdef __RL78_G10__
34; clobberable
35r8	=	0xffec8
36r9	=	0xffec9
37r10	=	0xffeca
38r11	=	0xffecb
39r12	=	0xffecc
40r13	=	0xffecd
41r14	=	0xffece
42r15	=	0xffecf
43; preserved
44r16	=	0xffed0
45r17	=	0xffed1
46r18	=	0xffed2
47r19	=	0xffed3
48r20	=	0xffed4
49r21	=	0xffed5
50r22	=	0xffed6
51r23	=	0xffed7
52#else
53; clobberable
54r8	=	0xffef0
55r9	=	0xffef1
56r10	=	0xffef2
57r11	=	0xffef3
58r12	=	0xffef4
59r13	=	0xffef5
60r14	=	0xffef6
61r15	=	0xffef7
62; preserved
63r16	=	0xffee8
64r17	=	0xffee9
65r18	=	0xffeea
66r19	=	0xffeeb
67r20	=	0xffeec
68r21	=	0xffeed
69r22	=	0xffeee
70r23	=	0xffeef
71#endif
72
73/* The jump buffer has the following structure:
74	R0 .. R23  3*8 bytes
75	SP	   2 bytes
76	ES	   1 byte
77	CS	   1 byte
78	PC         4 bytes
79*/
80
81	.macro	_saveb	ofs,reg
82	mov	a,\reg
83	mov	[hl+\ofs],a
84	.endm
85	.macro	_save	ofs,reg
86	movw	ax,\reg
87	movw	[hl+\ofs],ax
88	.endm
89
90	.global	_setjmp
91	.type	_setjmp, @function
92_setjmp:
93	;; R8 = setjmp (jmp_buf *[sp+4].w)
94	;; must return zero !!
95	push	ax
96	push	hl
97	push	ax
98	movw	ax, [sp+10]
99	movw	hl, ax
100	pop	ax
101	movw	[hl], ax
102	_save	2, bc
103	_save	4, de
104	pop	ax
105	movw	[hl+6], ax
106	_save	8, r8
107	_save	10, r10
108	_save	12, r12
109	_save	14, r14
110	_save	16, r16
111	_save	18, r18
112	_save	20, r20
113	_save	22, r22
114
115	;; The sp we have now includes one more pushed reg, plus $PC
116	movw	ax, sp
117	addw	ax, #6
118	movw	[hl+24], ax
119
120	_saveb	26, es
121	_saveb	27, cs
122	_save	28, [sp+2]
123	_save	30, [sp+4]
124
125	clrw	ax
126	movw	r8, ax
127	pop	ax
128	ret
129
130	.size	_setjmp, . - _setjmp
131
132	.macro	_loadb	ofs,reg
133	mov	a,[hl+\ofs]
134	mov	\reg,a
135	.endm
136	.macro	_load	ofs,reg
137	movw	ax,[hl+\ofs]
138	movw	\reg,ax
139	.endm
140	.macro	_push	ofs
141	movw	ax,[hl+\ofs]
142	push	ax
143	.endm
144
145	.global	_longjmp
146	.type _longjmp, @function
147_longjmp:
148	;; noreturn longjmp (jmp_buf *[sp+4].w, int [sp+6].w)
149	movw	ax, [sp+6]
150	cmpw	ax,#0
151	sknz
152	onew	ax
153	movw	r8, ax
154
155	movw	ax, [sp+4]
156	movw	hl, ax
157	movw	ax, [hl+24]
158	movw	sp, ax		; this is the *new* stack
159
160	_push	30		; high half of PC
161	_push	28		; low half of PC
162	_push	6		; HL
163	_push	0		; AX
164
165	_load	2, bc
166	_load	4, de
167
168	_load	10, r10
169	_load	12, r12
170	_load	14, r14
171	_load	16, r16
172	_load	18, r18
173	_load	20, r20
174	_load	22, r22
175
176	_loadb	26, es
177	_loadb	27, cs
178
179	pop	ax
180	pop	hl
181
182
183	ret	; pops PC (4 bytes)
184
185	.size	_longjmp, . - _longjmp
186
187