-
Notifications
You must be signed in to change notification settings - Fork 6
/
setjmp.h
90 lines (83 loc) · 1.72 KB
/
setjmp.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @file setjmp.h
* @brief Non-local jumps.
* @author Miguel I. Garcia Lopez / FloppySoftware
*
* Non-local jumps functions, for MESCC (Mike's Enhanced
* Small C Compiler for Z80 & CP/M).
*
* Only one jmp_buf is allowed.
*
* Call to setjmp is performed as - ie: setjmp(env):
* LD HL,(env)
* PUSH HL
* CALL setjmp
* POP BC
*
* Call to longjmp is performed as - ie: longjmp(env, 1):
*
* LD HL,(env)
* PUSH HL
* LD HL,1
* PUSH HL
* CALL longjmp
* POP BC
* POP BC
*
* Defined macros:
* - jmp_buf
*
* Revisions:
* - 21 Ago 2015 : Initial version.
* - 22 Ago 2015 : Changed jmp_buf from char to int.
* - 15 Ago 2016 : Documented. GPL v3.
*
* Copyright (c) 2015-2016 Miguel I. Garcia Lopez / FloppySoftware.
*
* Licensed under the GNU General Public License v3.
*
* http://www.floppysoftware.es
* floppysoftware@gmail.com
*/
#ifndef SETJMP_H
#define SETJMP_H
#define jmp_buf int // Just something
WORD setjmp_rt; // Return address
WORD setjmp_sp; // SP
/**
* @fn int setjmp (jmp_buf env)
* @brief Save state information for later use of longjmp().
* @param env - buffer for state data
* @return 0 from direct call, other values from a longjmp call
*/
#asm
setjmp
POP HL
LD (setjmp_rt), HL
LD (setjmp_sp), SP
PUSH HL
LD HL, 0
RET
#endasm
/**
* @fn void longjmp (jmp_buf env, int rv)
* @brief Resume execution after setjmp().
*
* This function resumes the execution after setjmp(), restoring
* the previously stored state in env.
*
* @param env - buffer with state data
* @param rv - value to return; must be != 0
* @return rv value
*/
#asm
longjmp
POP BC
POP HL
LD SP,(setjmp_sp)
LD DE,(setjmp_rt)
PUSH DE
RET
#endasm
#endif