forked from SDL-Hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmpscdct.c
312 lines (248 loc) · 8.32 KB
/
cmpscdct.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* CMPSCDCT.C (c) Copyright "Fish" (David B. Trout), 2012 */
/* Compression Call Get Dictionary Entry functions */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
#include "hstdinc.h" // (MUST be first #include in EVERY source file)
#if !defined(_HENGINE_DLL_)
#define _HENGINE_DLL_
#endif
#if !defined(_CMPSCDCT_C_)
#define _CMPSCDCT_C_
#endif
#if !defined( NOT_HERC ) // (building Hercules?)
#include "hercules.h"
#include "opcode.h"
#include "inline.h"
#endif
#include "cmpsc.h" // (Master header)
#ifdef FEATURE_COMPRESSION
///////////////////////////////////////////////////////////////////////////////
// GetDCT: fetch 8-byte dictionary entry as a 64-bit unsigned integer
U64 (CMPSC_FASTCALL ARCH_DEP( GetDCT ))( U16 index, DCTBLK* pDCTBLK )
{
register U16 pagenum = INDEX_TO_PAGENUM( index );
register U16 pageidx = INDEX_TO_PAGEIDX( index );
if (!pDCTBLK->maddr[ pagenum ])
{
pDCTBLK->maddr[ pagenum ] = MADDR
(
pDCTBLK->pDict + PAGENUM_TO_BYTES( pagenum ),
pDCTBLK->arn,
pDCTBLK->regs,
ACCTYPE_READ,
pDCTBLK->pkey
);
}
return CSWAP64(*(U64*)(uintptr_t)(&pDCTBLK->maddr[ pagenum ][ pageidx ]));
}
///////////////////////////////////////////////////////////////////////////////
// GetECE: Extract EXPANSION Character Entry into portable ECE structure
//
// Returns: TRUE/FALSE (success/data exception)
U8 (CMPSC_FASTCALL ARCH_DEP( GetECE ))( U16 index, ECEBLK* pECEBLK )
{
register U64 ece;
register ECE* pECE = pECEBLK->pECE;
if (pECEBLK->ece[ index ].cached)
{
*pECE = pECEBLK->ece[ index ];
return TRUE;
}
ece = ARCH_DEP( GetDCT )( index, pECEBLK->pDCTBLK );
if (!(pECE->psl = ECE_U8R( 0, 3 )))
{
if (!(pECE->csl = ECE_U8R( 5, 3 )))
return FALSE;
pECE->ec_dw = CSWAP64( ece << 8 );
}
else if (pECE->psl > 5)
return FALSE;
else
{
if ((pECE->pptr = ECE_U16R( 3, 13 )) > pECEBLK->max_index)
return FALSE;
pECE->ofst = (U8)(ece);
pECE->ec_dw = CSWAP64( ece << 16 );
pECE->csl = 0;
}
pECE->cached = TRUE;
pECEBLK->ece[ index ] = *pECE;
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// GetCCE: Extract COMPRESSION Character Entry into portable CCE structure
//
// Returns: TRUE/FALSE (success/data exception)
U8 (CMPSC_FASTCALL ARCH_DEP( GetCCE ))( U16 index, CCEBLK* pCCEBLK )
{
register U64 cce;
register CCE* pCCE = pCCEBLK->pCCE;
if (pCCEBLK->cce[ index ].cached)
{
*pCCE = pCCEBLK->cce[ index ];
return TRUE;
}
cce = ARCH_DEP( GetDCT )( index, pCCEBLK->pDCTBLK );
pCCE->mc = FALSE;
if (!(pCCE->cct = CCE_U8R( 0, 3 ))) // (no children)
{
if ((pCCE->act = CCE_U8R( 8, 3 )) > 5)
return FALSE;
if (pCCE->act)
pCCE->ec_dw = CSWAP64( cce << 24 );
pCCE->cached = TRUE;
pCCEBLK->cce[ index ] = *pCCE;
return TRUE;
}
if (pCCE->cct == 1) // (only one child)
{
register U8 wrk;
if ((pCCE->act = CCE_U8R( 8, 3 )) > 4)
return FALSE;
wrk = (pCCE->act << 3);
pCCE->cc[0] = CCE_U8R( 24 + wrk, 8 );
if (pCCE->act)
pCCE->ec_dw = CSWAP64( cce << 24 );
pCCE->cptr = CCE_U16R( 11, 13 );
pCCE->ecb = CCE_U16L( 3, 1 );
}
else // (many children)
{
register U8 wrk; // (max cct)
if (!(pCCE->act = CCE_U8R( 10, 1 ))) // (act == 0)
wrk = 5;
else // (act == 1)
{
wrk = 4;
pCCE->ec[0] = CCE_U8R( 24, 8 );
}
if (pCCE->cct > (wrk + 1)) // (data exception?)
return FALSE;
if (pCCE->cct == (wrk + 1)) // (more children?)
{
pCCE->mc = TRUE; // (set flag)
pCCE->cct = wrk; // (fix cct)
}
wrk = (pCCE->act << 3); // (cc start bit - 24)
pCCE->cc_dw = CSWAP64( cce << (24 + wrk) );
pCCE->cptr = CCE_U16R( 11, 13 );
pCCE->ecb = CCE_U16L( 3, 5 );
pCCE->yy = CCE_U16L( 8, 2 );
}
pCCE->cached = TRUE;
pCCEBLK->cce[ index ] = *pCCE;
return (pCCE->cptr > pCCEBLK->max_index) ? FALSE : TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// GetSD0: Extract Format-0 Sibling Descriptor into portable SDE structure
//
// Returns: TRUE/FALSE (success/data exception)
U8 (CMPSC_FASTCALL ARCH_DEP( GetSD0 ))( U16 index, SDEBLK* pSDEBLK )
{
register U64 sd1;
register SDE* pSDE = pSDEBLK->pSDE;
if (pSDEBLK->sde[ index ].cached)
{
*pSDE = pSDEBLK->sde[ index ];
return TRUE;
}
sd1 = ARCH_DEP( GetDCT )( index, pSDEBLK->pDCTBLK );
pSDE->ms = FALSE;
if (!(pSDE->sct = SD1_U8R( 0, 3 )) || pSDE->sct >= 7)
{
pSDE->sct = 7;
pSDE->ms = TRUE;
}
// Examine child bits for children 1 to 5
pSDE->ecb = SD1_U16L( 3, 5 );
// If children exist append examine child bits for
// children 6 and 7 which are in the parent CCE so
// they're all in one place (makes things easier).
//
// Note: this only applies for the FIRST sibling
// of the parent. Examine child bits for children
// 6 and 7 do not exist for subsequent siblings
// of parent and thus must ALWAYS be examined.
if (pSDEBLK->pCCE) // (first sibling of parent?)
{
if (pSDEBLK->pCCE->cct > 1)
pSDE->ecb |= pSDEBLK->pCCE->yy >> 5;
}
else // (force child to ALWAYS be examined)
{
pSDE->ecb |= 0xFFFF >> 5;
}
pSDE->sc_dw = CSWAP64( sd1 << 8 );
pSDE->cached = TRUE;
pSDEBLK->sde[ index ] = *pSDE;
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// GetSD1: Extract Format-1 Sibling Descriptor into portable SDE structure
//
// Returns: TRUE/FALSE (success/data exception)
U8 (CMPSC_FASTCALL ARCH_DEP( GetSD1 ))( U16 index, SDEBLK* pSDEBLK )
{
register U64 sd1;
register SDE* pSDE = pSDEBLK->pSDE;
if (pSDEBLK->sde[ index ].cached)
{
*pSDE = pSDEBLK->sde[ index ];
return TRUE;
}
sd1 = ARCH_DEP( GetDCT )( index, pSDEBLK->pDCTBLK );
pSDE->ms = FALSE;
if (!(pSDE->sct = SD1_U8R( 0, 4 )) || pSDE->sct >= 15)
{
pSDE->sct = 14;
pSDE->ms = TRUE;
}
// Examine child bits for children 1 to 12
pSDE->ecb = SD1_U16L( 4, 12 );
// If children exist append examine child bits for
// children 13 and 14 which are in the parent CCE
// so they are all in one place (simpler testing).
//
// Note: this only applies for the FIRST sibling
// of the parent. Examine child bits for children
// 13 and 14 do not exist for subsequent siblings
// of parent and thus must ALWAYS be examined.
if (pSDEBLK->pCCE) // (first sibling of parent?)
{
if (pSDEBLK->pCCE->cct > 1)
pSDE->ecb |= pSDEBLK->pCCE->yy >> 12;
}
else // (force child to ALWAYS be examined)
{
pSDE->ecb |= 0xFFFF >> 12;
}
sd1 <<= 16; // (first 6 bytes)
if (pSDE->sct <= 6)
pSDE->sc_dw = CSWAP64( sd1 ); // (only 6 bytes)
else
{
register U64 sd2 = ARCH_DEP( GetDCT )( index, pSDEBLK->pDCTBLK2 );
sd1 |= sd2 >> (64-16); // (append 2 more)
pSDE->sc_dw= CSWAP64( sd1 ); // (store first 8)
sd2 <<= 16; // (next 6 bytes)
pSDE->sc_dw2 = CSWAP64( sd2 ); // (store next 6)
}
pSDE->cached = TRUE;
pSDEBLK->sde[ index ] = *pSDE;
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
#endif /* FEATURE_COMPRESSION */
#ifndef _GEN_ARCH
#ifdef _ARCHMODE2
#define _GEN_ARCH _ARCHMODE2
#include "cmpscdct.c"
#endif /* #ifdef _ARCHMODE2 */
#ifdef _ARCHMODE3
#undef _GEN_ARCH
#define _GEN_ARCH _ARCHMODE3
#include "cmpscdct.c"
#endif /* #ifdef _ARCHMODE3 */
#endif /* #ifndef _GEN_ARCH */