-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpack.cc
199 lines (154 loc) · 4.7 KB
/
unpack.cc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int rcr(uint16_t *bp, const char *addr, const char *reg = "bp");
int rcl(uint16_t *bp, const char *addr, const char *reg = "bp");
int shr(uint16_t *bp, const char *addr, const char *reg = "bp");
int carry;
uint16_t unpack(const uint8_t *ds, uint8_t *es)
{
if (memcmp(ds, "FAB", 3)) {
printf("Not a FAB section\n");
return -1;
}
if (ds[3] < 0x0a || ds[3] > 0x0d) {
printf("PANIC! don't know how to handle this FAB type. (0x%02x)\n", ds[3]);
exit(EXIT_FAILURE);
}
uint16_t cx = 0x10 - ds[0x03]; //cx is set as 0x10 - byte following FAB
assert(cx == 0x04);
uint16_t dx = 0x10; //XXX dx is set as 0x10 (first occurance in coloview: 0f47:04c8)
uint16_t si = 4; //skipping FAB\x0c
uint16_t di = 0;
carry = 1; //XXX where does this come from?
uint16_t bp = *(uint16_t *) &ds[si]; si += 2; //fprintf(stderr, "11c8:04fc NEW bp(%04x) %d\n", bp, carry);
for (;;) {
if (--dx == 0) {
//fprintf(stderr, "11c8:04e4 dx(%02x)\n", dx);
dx = 0x10; //11c8:04e8
shr(&bp, "11c8:04ea");
bp = *(uint16_t *) &ds[si]; si += 2; //fprintf(stderr, "11c8:04ec NEW bp(%04x) %d\n", bp, carry);
rcl(&bp, "11c8:04ee");
} else {
//fprintf(stderr, "11c8:04e4 dx(%02x)\n", dx);
}
if (rcr(&bp, "11c8:04f0")) {
es[di++] = ds[si++]; //fprintf(stderr, "11c8:04f4 es[%02x]: %02x\n", di-1, es[di-1]);
//assert(es[di - 1] == terrain_s1[di - 1]);
continue;
}
cx = 0; //11c8:04f7
if (--dx == 0) { //11c8:04f9
//fprintf(stderr, "11c8:04f9 dx(%02x)\n", dx);
dx = 0x10; //11c8:04fd
shr(&bp, "11c8:04ff");
bp = *(uint16_t *) &ds[si]; si += 2; //fprintf(stderr, "11c8:04fc NEW bp(%04x) %d\n", bp, carry);
rcl(&bp, "11c8:0503");
} else {
//fprintf(stderr, "11c8:04f9 dx(%02x)\n", dx);
}
uint16_t bx;
if (rcr(&bp, "11c8:0505")) { //THIS IS WHERE IT GETS WEIRD
uint8_t cs[] = {0x04, 0x0f, 0xf0}; //11c8:{045e,045f,0460}
uint16_t ax = bx = *(uint16_t *) &ds[si]; si += 2; //fprintf(stderr, "11c8:0533 NEW bx(%04x) %d\n", bx, carry);
cx = cs[0];
bx = (bx & 0x00ff) + (((bx & 0xff00) >> (cx & 0x00ff)) & 0xff00); //fprintf(stderr, "11c8:053b shr bx(%04x)\n", bx);
bx |= (cs[2] << 8); //fprintf(stderr, "11c8:053d bx(%04x)\n", bx);
ax = ((ax & 0xff00) & (cs[1] << 8)) + (ax & 0x00ff); //fprintf(stderr, "11c8:0542 ax(%04x)\n", ax);
if (0 == (ax & 0xff00)) { //11c8:0547
ax = ds[si++];
if (ax == 0) {
//printf("\nStage 1 complete!\n");
return di;
}
if (ax == 1) {
printf("\nStage 1 error!\n");
exit(EXIT_FAILURE);
}
cx = ax; //fprintf(stderr, "11c8:055e cx(%04x)\n", cx);
++cx; //11c8:0560
} else {
cx = (ax >> 8);// fprintf(stderr, "11c8:0549 cx(%04x)\n", cx);
cx += 2;
}
} else {
if (--dx == 0) { //11c8:0509
//fprintf(stderr, "11c8:0509 dx(%02x)\n", dx);
dx = 0x10; //11c8:050d
shr(&bp, "11c8:050f");
bp = *(uint16_t *) &ds[si]; si += 2; //fprintf(stderr, "11c8:0511 NEW bp(%04x) %d\n", bp, carry);
rcl(&bp, "11c8:0513");
} else {
//fprintf(stderr, "11c8:0509 dx(%02x)\n", dx);
}
rcr(&bp, "11c8:0515");
rcl(&cx, "11c8:0517", "cx");
if (--dx == 0) { //11c8:0519
//fprintf(stderr, "11c8:0519 dx(%02x)\n", dx);
dx = 0x10;
shr(&bp, "11c8:051f");
bp = *(uint16_t *) &ds[si]; si += 2; //fprintf(stderr, "11c8:0521 NEW bp(%04x) %d\n", bp, carry);
rcl(&bp, "11c8:0523");
} else {
//fprintf(stderr, "11c8:0519 dx(%02x)\n", dx);
}
rcr(&bp, "11c8:0525");
rcl(&cx, "11c8:0527", "cx");
cx += 2; //fprintf(stderr, "11c8:052a cx(%02x)\n", cx);
bx = 0xff00; bx += ds[si++];
}
int tmp_di = bx + di - 0x10000; //11c8:054d
for (cx; cx--; ) {
es[di++] = es[tmp_di++]; //fprintf(stderr, "11c8:0550 es[%02x]: %02x\n", di-1, es[di-1]);
//assert(es[di - 1] == terrain_s1[di - 1]);
}
}
return -1;
}
int rcr(uint16_t *bp, const char *addr, const char *reg)
{
int c = 0;
if (*bp & 1)
c = 1;
*bp = *bp >> 1;
if (carry)
*bp += 0x8000;
//fprintf(stderr, "%s rcr %s(%04x) %d\n", addr, reg, *bp, c);
return carry = c;
}
int rcl(uint16_t *bp, const char *addr, const char *reg)
{
int c = 0;
if (*bp & 0x8000)
c = 1;
*bp = *bp << 1;
if (carry)
*bp += 1;
//fprintf(stderr, "%s rcl %s(%04x) %d\n", addr, reg, *bp, c);
return carry = c;
}
int shr(uint16_t *bp, const char *addr, const char *reg)
{
int c = 0;
if (*bp & 1)
c = 1;
*bp = *bp >> 1;
//fprintf(stderr, "%s shr %s(%04x) %d\n", addr, reg, *bp, c);
return carry = c;
}
/*
const char *binary(uint16_t bp)
{
static char binary[17];
if (bp & 0x8000)
itoa(bp, binary, 2);
else {
itoa(bp + 0x8000, binary, 2);
binary[0] = '0';
}
return binary;
}
printf("\033[22;31m%c\033[0m", binary[i]);
*/