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#include <picolibc.h>
34
35#ifdef __RL78_G10__
36; clobberable
37r8	=	0xffec8
38r9	=	0xffec9
39r10	=	0xffeca
40r11	=	0xffecb
41r12	=	0xffecc
42r13	=	0xffecd
43r14	=	0xffece
44r15	=	0xffecf
45; preserved
46r16	=	0xffed0
47r17	=	0xffed1
48r18	=	0xffed2
49r19	=	0xffed3
50r20	=	0xffed4
51r21	=	0xffed5
52r22	=	0xffed6
53r23	=	0xffed7
54#else
55; clobberable
56r8	=	0xffef0
57r9	=	0xffef1
58r10	=	0xffef2
59r11	=	0xffef3
60r12	=	0xffef4
61r13	=	0xffef5
62r14	=	0xffef6
63r15	=	0xffef7
64; preserved
65r16	=	0xffee8
66r17	=	0xffee9
67r18	=	0xffeea
68r19	=	0xffeeb
69r20	=	0xffeec
70r21	=	0xffeed
71r22	=	0xffeee
72r23	=	0xffeef
73#endif
74
75/* The jump buffer has the following structure:
76	R0 .. R23  3*8 bytes
77	SP	   2 bytes
78	ES	   1 byte
79	CS	   1 byte
80	PC         4 bytes
81*/
82
83	.macro	_saveb	ofs,reg
84	mov	a,\reg
85	mov	[hl+\ofs],a
86	.endm
87	.macro	_save	ofs,reg
88	movw	ax,\reg
89	movw	[hl+\ofs],ax
90	.endm
91
92	.global	_setjmp
93	.type	_setjmp, @function
94_setjmp:
95	;; R8 = setjmp (jmp_buf *[sp+4].w)
96	;; must return zero !!
97	push	ax
98	push	hl
99	push	ax
100	movw	ax, [sp+10]
101	movw	hl, ax
102	pop	ax
103	movw	[hl], ax
104	_save	2, bc
105	_save	4, de
106	pop	ax
107	movw	[hl+6], ax
108	_save	8, r8
109	_save	10, r10
110	_save	12, r12
111	_save	14, r14
112	_save	16, r16
113	_save	18, r18
114	_save	20, r20
115	_save	22, r22
116
117	;; The sp we have now includes one more pushed reg, plus $PC
118	movw	ax, sp
119	addw	ax, #6
120	movw	[hl+24], ax
121
122	_saveb	26, es
123	_saveb	27, cs
124	_save	28, [sp+2]
125	_save	30, [sp+4]
126
127	clrw	ax
128	movw	r8, ax
129	pop	ax
130	ret
131
132	.size	_setjmp, . - _setjmp
133
134	.macro	_loadb	ofs,reg
135	mov	a,[hl+\ofs]
136	mov	\reg,a
137	.endm
138	.macro	_load	ofs,reg
139	movw	ax,[hl+\ofs]
140	movw	\reg,ax
141	.endm
142	.macro	_push	ofs
143	movw	ax,[hl+\ofs]
144	push	ax
145	.endm
146
147	.global	_longjmp
148	.type _longjmp, @function
149_longjmp:
150	;; noreturn longjmp (jmp_buf *[sp+4].w, int [sp+6].w)
151	movw	ax, [sp+6]
152	cmpw	ax,#0
153	sknz
154	onew	ax
155	movw	r8, ax
156
157	movw	ax, [sp+4]
158	movw	hl, ax
159	movw	ax, [hl+24]
160	movw	sp, ax		; this is the *new* stack
161
162	_push	30		; high half of PC
163	_push	28		; low half of PC
164	_push	6		; HL
165	_push	0		; AX
166
167	_load	2, bc
168	_load	4, de
169
170	_load	10, r10
171	_load	12, r12
172	_load	14, r14
173	_load	16, r16
174	_load	18, r18
175	_load	20, r20
176	_load	22, r22
177
178	_loadb	26, es
179	_loadb	27, cs
180
181	pop	ax
182	pop	hl
183
184
185	ret	; pops PC (4 bytes)
186
187	.size	_longjmp, . - _longjmp
188
189