-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobj.awk
164 lines (136 loc) · 3.58 KB
/
obj.awk
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function obj_address(obj) {
return hdr_object_table_offset + 62 + (obj - 1) * 9
}
function obj_attr_address(obj, attr) {
return obj_address(obj) + int(attr / 8)
}
function obj_attr(obj, attr, b) {
b = mem_read_u8(obj_attr_address(obj, attr))
return test_bit(b, 7 - (attr % 8))
}
function obj_set_attr(obj, attr, b, addr, bit) {
addr = obj_attr_address(obj, attr)
bit = 7 - (attr % 8)
b = mem_read_u8(addr)
if(!test_bit(b, bit)) {
mem_write_u8(addr, b + (2 ^ bit))
}
}
function obj_clear_attr(obj, attr, b, addr, bit) {
addr = obj_attr_address(obj, attr)
bit = 7 - (attr % 8)
b = mem_read_u8(addr)
if(test_bit(b, bit)) {
mem_write_u8(addr, b - (2 ^ bit))
}
}
function obj_parent(obj) {
return mem_read_u8(obj_address(obj) + 4)
}
function obj_set_parent(obj, parent) {
mem_write_u8(obj_address(obj) + 4, parent)
}
function obj_sibling(obj) {
return mem_read_u8(obj_address(obj) + 5)
}
function obj_set_sibling(obj, sibling) {
mem_write_u8(obj_address(obj) + 5, sibling)
}
function obj_child(obj) {
return mem_read_u8(obj_address(obj) + 6)
}
function obj_set_child(obj, child) {
mem_write_u8(obj_address(obj) + 6, child)
}
function obj_prev_sibling(obj, p, prev) {
p = obj_parent(obj)
if(p == 0) {
return 0
}
for(p = obj_child(p); p; p = obj_sibling(p)) {
if(p == obj) {
return prev
}
prev = p
}
return 0
}
function obj_name_addr(obj) {
return mem_read_u16(obj_address(obj) + 7) + 1
}
function obj_name_byte_len(obj) {
return mem_read_u8(obj_name_addr(obj) - 1) * 2
}
function obj_properties_addr(obj) {
return obj_name_addr(obj) + obj_name_byte_len(obj)
}
function obj_first_prop(obj) {
return mem_read_u8(obj_properties_addr(obj)) % 32
}
function obj_next_prop(obj, prop, addr) {
addr = obj_prop_addr(obj, prop)
return obj_addr_prop(addr + obj_addr_prop_len(addr) + 1)
}
function obj_prop_addr(obj, prop, addr, p) {
addr = obj_properties_addr(obj) + 1
while(1) {
p = obj_addr_prop(addr)
if(p == prop) {
return addr
} else if(p == 0) {
return 0
}
addr += obj_addr_prop_len(addr) + 1
}
}
function obj_addr_prop(addr) {
return mem_read_u8(addr - 1) % 32
}
function obj_addr_prop_len(addr) {
if(addr == 0) {
return 0
}
return int(mem_read_u8(addr - 1) / 32) + 1
}
function obj_prop(obj, prop, addr) {
addr = obj_prop_addr(obj, prop)
if(addr == 0) {
return mem_read_u16(hdr_object_table_offset + 2 * (prop - 1))
} else if(obj_addr_prop_len(addr) == 1) {
return mem_read_u8(addr)
} else {
return mem_read_u16(addr)
}
}
function obj_set_prop(obj, prop, val, addr, len) {
addr = obj_prop_addr(obj, prop)
if(addr == 0) {
printf("Invalid property %d for object %d\n", prop, obj)
return 0
}
len = obj_addr_prop_len(addr)
if(len == 1) {
mem_write_u8(addr, val)
} else if(len == 2) {
mem_write_u16(addr, val)
} else {
printf("Invalid property len %d\n", len)
}
}
function obj_remove(obj) {
if(obj_parent(obj)) {
if(obj_prev_sibling(obj)) {
obj_set_sibling(obj_prev_sibling(obj), obj_sibling(obj))
} else {
obj_set_child(obj_parent(obj), obj_sibling(obj))
}
obj_set_sibling(obj, 0)
obj_set_parent(obj, 0)
}
}
function obj_insert(obj, dst) {
obj_remove(obj)
obj_set_sibling(obj, obj_child(dst))
obj_set_child(dst, obj)
obj_set_parent(obj, dst)
}