-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.c
80 lines (72 loc) · 2.15 KB
/
variables.c
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
/*
Zerp: a Z-machine interpreter
variables.c : z-machine variable access
*/
#include <stdio.h>
#ifdef TARGET_OS_MAC
#include <GlkClient/glk.h>
#else
#include "glk.h"
#endif /* TARGET_OS_MAC */
#include "zerp.h"
#include "opcodes.h"
#include "stack.h"
zword_t variable_get(zbyte_t variable) {
zword_t value;
if (variable == 0) {
/* pop stack */
value = stack_pop();
} else if (variable > 0 && variable < 0x10) {
/* read a local */
value = zFP->locals[variable - 1];
} else {
/* read a global */
value = get_word(zGlobals + ((variable - 0x10) * 2));
}
LOG(ZDEBUG, "\nRead variable #%x (value %i)", variable, value);
return value;
}
zword_t indirect_variable_get(zbyte_t variable) {
zword_t value;
if (variable == 0) {
/* just peek at the stack */
value = stack_peek();
} else if (variable > 0 && variable < 0x10) {
/* read a local */
value = zFP->locals[variable - 1];
} else {
/* read a global */
value = get_word(zGlobals + ((variable - 0x10) * 2));
}
LOG(ZDEBUG, "\nRead variable #%x (value %i)", variable, value);
return value;
}
int variable_set(zbyte_t variable, zword_t value) {
LOG(ZDEBUG, "\nStoring %i in Variable #%x", value, variable);
if (variable == 0) {
/* push stack */
return stack_push(value);
} else if (variable > 0 && variable < 0x10) {
/* write a local */
return zFP->locals[variable - 1] = value;
} else {
/* write a global */
store_word(zGlobals + ((variable - 0x10) *2), value);
return value;
}
}
int indirect_variable_set(zbyte_t variable, zword_t value) {
LOG(ZDEBUG, "\nStoring %i in Variable #%x", value, variable);
if (variable == 0) {
/* poke stack */
return stack_poke(value);
} else if (variable > 0 && variable < 0x10) {
/* write a local */
return zFP->locals[variable - 1] = value;
} else {
/* write a global */
store_word(zGlobals + ((variable - 0x10) *2), value);
// LOG(ZDEBUG, "\nWrite G%02x (%04x)", variable - 0x10, value);
return value;
}
}