-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmpsc.c
2012 lines (1782 loc) · 79.3 KB
/
cmpsc.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CMPSC.C (c) Bernard van der Helm, 2000-2015 */
/* S/390 compression call instruction */
/*----------------------------------------------------------------------------*/
/* Implementation of the S/390 compression call instruction described in */
/* SA22-7208-01: Data Compression within the Hercules S/390 emulator. */
/* This implementation couldn't be done without the test programs from */
/* Mario Bezzi. Thanks Mario! Also special thanks to Greg Smith who */
/* introduced iregs, needed when a page fault occurs. */
/* */
/* Please pay attention to the Q Public License Version 1. This is open */
/* source, but you are not allowed to "reuse" parts for your own purpose */
/* without the author's written permission! */
/* */
/* Implemented "unique" features: */
/* 8 index symbol block fetching and storing, preventing cbn calculations. */
/* Expand symbol translation caching. */
/* Compression dead end determination and elimination. */
/* Proactive dead end determination and elimination. */
/* */
/* (c) Copyright Bernard van der Helm, 2000-2015 */
/* Noordwijkerhout, The Netherlands. */
/* */
/*----------------------------------------------------------------------------*/
#include "hstdinc.h"
#ifndef _HENGINE_DLL_
#define _HENGINE_DLL_
#endif /* #ifndef _HENGINE_DLL_ */
#ifndef _CMPSC_C_
#define _CMPSC_C_
#endif /* #ifndef _CMPSC_C_ */
#include "hercules.h"
#include "opcode.h"
#include "inline.h"
#ifdef FEATURE_COMPRESSION
/*============================================================================*/
/* Common */
/*============================================================================*/
/*----------------------------------------------------------------------------*/
/* Debugging options: */
/*----------------------------------------------------------------------------*/
#if 0
#define OPTION_CMPSC_DEBUG
#define TRUEFALSE(boolean) ((boolean) ? "True" : "False")
#endif /* #if 0|1 */
/*----------------------------------------------------------------------------*/
/* After a successful compression of characters to an index symbol or a */
/* successful translation of an index symbol to characters, the registers */
/* must be updated. */
/*----------------------------------------------------------------------------*/
#define ADJUSTREGS(r, regs, iregs, len) \
{ \
SET_GR_A((r), (iregs), (GR_A((r), (iregs)) + (len)) & ADDRESS_MAXWRAP((regs))); \
SET_GR_A((r) + 1, (iregs), GR_A((r) + 1, (iregs)) - (len)); \
}
/*----------------------------------------------------------------------------*/
/* Commit intermediate registers */
/*----------------------------------------------------------------------------*/
#ifdef OPTION_CMPSC_DEBUG
#define COMMITREGS(regs, iregs, r1, r2) \
__COMMITREGS((regs), (iregs), (r1), (r2)) \
logmsg("*** Regs committed\n");
#else
#define COMMITREGS(regs, iregs, r1, r2) \
__COMMITREGS((regs), (iregs), (r1), (r2))
#endif /* ifdef OPTION_CMPSC_DEBUG */
#define __COMMITREGS(regs, iregs, r1, r2) \
{ \
SET_GR_A(1, (regs), GR_A(1, (iregs))); \
SET_GR_A((r1), (regs), GR_A((r1), (iregs))); \
SET_GR_A((r1) + 1, (regs), GR_A((r1) + 1, (iregs))); \
SET_GR_A((r2), (regs), GR_A((r2), (iregs))); \
SET_GR_A((r2) + 1, (regs), GR_A((r2) + 1, (iregs))); \
}
/*----------------------------------------------------------------------------*/
/* Commit intermediate registers, except for GR1 */
/*----------------------------------------------------------------------------*/
#ifdef OPTION_CMPSC_DEBUG
#define COMMITREGS2(regs, iregs, r1, r2) \
__COMMITREGS2((regs), (iregs), (r1), (r2)) \
logmsg("*** Regs committed\n");
#else
#define COMMITREGS2(regs, iregs, r1, r2) \
__COMMITREGS2((regs), (iregs), (r1), (r2))
#endif /* #ifdef OPTION_CMPSC_DEBUG */
#define __COMMITREGS2(regs, iregs, r1, r2) \
{ \
SET_GR_A((r1), (regs), GR_A((r1), (iregs))); \
SET_GR_A((r1) + 1, (regs), GR_A((r1) + 1, (iregs))); \
SET_GR_A((r2), (regs), GR_A((r2), (iregs))); \
SET_GR_A((r2) + 1, (regs), GR_A((r2) + 1, (iregs))); \
}
/*----------------------------------------------------------------------------*/
/* Initialize intermediate registers */
/*----------------------------------------------------------------------------*/
#define INITREGS(iregs, regs, r1, r2) \
{ \
(iregs)->gr[1] = (regs)->gr[1]; \
(iregs)->gr[(r1)] = (regs)->gr[(r1)]; \
(iregs)->gr[(r1) + 1] = (regs)->gr[(r1) + 1]; \
(iregs)->gr[(r2)] = (regs)->gr[(r2)]; \
(iregs)->gr[(r2) + 1] = (regs)->gr[(r2) + 1]; \
}
#if __GEN_ARCH == 900
#undef INITREGS
#define INITREGS(iregs, regs, r1, r2) \
{ \
(iregs)->gr[1] = (regs)->gr[1]; \
(iregs)->gr[(r1)] = (regs)->gr[(r1)]; \
(iregs)->gr[(r1) + 1] = (regs)->gr[(r1) + 1]; \
(iregs)->gr[(r2)] = (regs)->gr[(r2)]; \
(iregs)->gr[(r2) + 1] = (regs)->gr[(r2) + 1]; \
(iregs)->psw.amode64 = (regs)->psw.amode64; \
}
#endif /* #if __GEN_ARCH == 900 */
/*----------------------------------------------------------------------------*/
/* General Purpose Register 0 macro's (GR0) */
/*----------------------------------------------------------------------------*/
/* cdss : compressed-data symbol size */
/* e : expansion operation */
/* f1 : format-1 sibling descriptors */
/* st : symbol-translation option */
/* zp : zero padding */
/* */
/* We recognize the zp flag and do conform POP nothing! We do something */
/* better: Eight index symbol processing. It saves a lot of time in cbn */
/* processing, index symbol fetching and storing. Please contact me if a */
/* 100% equal functionality is needed. I doubt it! */
/*----------------------------------------------------------------------------*/
#define GR0_cdss(regs) (((regs)->GR_L(0) & 0x0000F000) >> 12)
#define GR0_e(regs) ((regs)->GR_L(0) & 0x00000100)
#define GR0_f1(regs) ((regs)->GR_L(0) & 0x00000200)
#define GR0_st(regs) ((regs)->GR_L(0) & 0x00010000)
#define GR0_zp(regs) ((regs)->GR_L(0) & 0x00020000)
/*----------------------------------------------------------------------------*/
/* General Purpose Register 0 macro's (GR0) derived */
/*----------------------------------------------------------------------------*/
/* dcten : # dictionary entries */
/* dctsz : dictionary size */
/* smbsz : symbol size */
/*----------------------------------------------------------------------------*/
#define GR0_dcten(regs) (0x100 << GR0_cdss((regs)))
#define GR0_dctsz(regs) (0x800 << GR0_cdss((regs)))
#define GR0_smbsz(regs) (GR0_cdss((regs)) + 8)
/*----------------------------------------------------------------------------*/
/* General Purpose Register 1 macro's (GR1) */
/*----------------------------------------------------------------------------*/
/* cbn : compressed-data bit number */
/* dictor: compression dictionary or expansion dictionary */
/* sttoff: symbol-translation-table offset */
/*----------------------------------------------------------------------------*/
#define GR1_cbn(regs) (((regs)->GR_L(1) & 0x00000007))
#define GR1_dictor(regs) (GR_A(1, (regs)) & ((GREG) 0xFFFFFFFFFFFFF000ULL))
#define GR1_sttoff(regs) (((regs)->GR_L(1) & 0x00000FF8) << 4)
/*----------------------------------------------------------------------------*/
/* Set the compressed bit number in GR1 */
/*----------------------------------------------------------------------------*/
#define GR1_setcbn(regs, cbn) ((regs)->GR_L(1) = ((regs)->GR_L(1) & 0xFFFFFFF8) | ((cbn) & 0x00000007))
/*----------------------------------------------------------------------------*/
/* Constants */
/*----------------------------------------------------------------------------*/
#define MINPROC_SIZE 32768 /* Minumum processing size */
/*----------------------------------------------------------------------------*/
/* Typedefs and prototypes */
/*----------------------------------------------------------------------------*/
#ifndef NO_2ND_COMPILE
struct cc /* Compress context */
{
BYTE *cce; /* Character entry under investigation */
int cr; /* Characters read to check #261 */
unsigned dctsz; /* Dictionary size */
BYTE deadadm[8192][0x100 / 8]; /* Dead end administration */
BYTE deadend; /* Dead end indicator */
BYTE *dest; /* Destination MADDR address */
BYTE *dict[32]; /* Dictionary MADDR addresses */
GREG dictor; /* Dictionary origin */
BYTE *edict[32]; /* Expansion dictionary MADDR addrs */
int f1; /* Indication format-1 sibling descr */
REGS *iregs; /* Intermediate registers */
U16 is[8]; /* Cache for 8 index symbols */
unsigned ofst; /* Latest fetched offset */
int r1; /* Guess what */
int r2; /* Yep */
REGS *regs; /* Registers */
BYTE searchadm[1][0x100 / 8]; /* Search administration */
unsigned smbsz; /* Symbol size */
BYTE *src; /* Source MADDR page address */
unsigned srclen; /* Source length left in page */
BYTE st; /* Symbol translation */
};
struct ec /* Expand context */
{
BYTE *dest; /* Destination MADDR page address */
BYTE *dict[32]; /* Dictionary MADDR addresses */
GREG dictor; /* Dictionary origin */
BYTE ec[8192 * 7]; /* Expanded index symbol cache */
int eci[8192]; /* Index within cache for is */
int ecl[8192]; /* Size of expanded is */
int ecwm; /* Water mark */
REGS *iregs; /* Intermediate registers */
BYTE oc[8 * 260]; /* Output cache */
unsigned ocl; /* Output cache length */
int r1; /* Guess what */
int r2; /* Yep */
REGS *regs; /* Registers */
unsigned smbsz; /* Symbol size */
BYTE *src; /* Source MADDR page address */
#ifdef OPTION_CMPSC_DEBUG
unsigned dbgac; /* Alphabet characters */
unsigned dbgbi; /* bytes in */
unsigned dbgbo; /* bytes out */
unsigned dbgch; /* Cache hits */
unsigned dbgiss; /* Expanded iss */
#endif /* #ifdef OPTION_CMPSC_DEBUG */
};
#endif /* #ifndef NO_2ND_COMPILE */
static void ARCH_DEP(cmpsc_compress)(int r1, int r2, REGS *regs, REGS *iregs);
static int ARCH_DEP(cmpsc_compress_single_is)(struct cc *cc);
static void ARCH_DEP(cmpsc_expand)(int r1, int r2, REGS *regs, REGS *iregs);
static void ARCH_DEP(cmpsc_expand_is)(struct ec *ec, U16 is);
static int ARCH_DEP(cmpsc_expand_single_is)(struct ec *ec);
static BYTE *ARCH_DEP(cmpsc_fetch_cce)(struct cc *cc, unsigned index);
static int ARCH_DEP(cmpsc_fetch_ch)(struct cc *cc);
static int ARCH_DEP(cmpsc_fetch_is)(struct ec *ec, U16 *is);
static void ARCH_DEP(cmpsc_fetch_iss)(struct ec *ec, U16 is[8]);
#ifdef OPTION_CMPSC_DEBUG
static void cmpsc_print_cce(BYTE *cce);
static void cmpsc_print_ece(BYTE *ece);
static void cmpsc_print_sd(int f1, BYTE *sd1, BYTE *sd2);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
static int ARCH_DEP(cmpsc_search_cce)(struct cc *cc, U16 *is);
static int ARCH_DEP(cmpsc_search_sd)(struct cc *cc, U16 *is);
static int ARCH_DEP(cmpsc_store_is)(struct cc *cc, U16 is);
static void ARCH_DEP(cmpsc_store_iss)(struct cc *cc);
static int ARCH_DEP(cmpsc_test_ec)(struct cc *cc, BYTE *cce);
static int ARCH_DEP(cmpsc_vstore)(struct ec *ec, BYTE *buf, unsigned len);
/*----------------------------------------------------------------------------*/
/* B263 CMPSC - Compression Call [RRE] */
/*----------------------------------------------------------------------------*/
DEF_INST(compression_call)
{
REGS iregs; /* Intermediate registers */
int r1; /* Guess what */
int r2; /* Yep */
RRE(inst, regs, r1, r2);
#ifdef OPTION_CMPSC_DEBUG
logmsg("CMPSC: compression call\n");
logmsg(" r1 : GR%02d\n", r1);
logmsg(" address : " F_VADR "\n", regs->GR(r1));
logmsg(" length : " F_GREG "\n", regs->GR(r1 + 1));
logmsg(" r2 : GR%02d\n", r2);
logmsg(" address : " F_VADR "\n", regs->GR(r2));
logmsg(" length : " F_GREG "\n", regs->GR(r2 + 1));
logmsg(" GR00 : " F_GREG "\n", regs->GR(0));
logmsg(" zp : %s\n", TRUEFALSE(GR0_zp(regs)));
logmsg(" st : %s\n", TRUEFALSE(GR0_st(regs)));
logmsg(" cdss : %d\n", GR0_cdss(regs));
logmsg(" f1 : %s\n", TRUEFALSE(GR0_f1(regs)));
logmsg(" e : %s\n", TRUEFALSE(GR0_e(regs)));
logmsg(" GR01 : " F_GREG "\n", regs->GR(1));
logmsg(" dictor: " F_GREG "\n", GR1_dictor(regs));
logmsg(" sttoff: %08X\n", GR1_sttoff(regs));
logmsg(" cbn : %d\n", GR1_cbn(regs));
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Check the registers on even-odd pairs and valid compression-data symbol size */
if(unlikely(r1 & 0x01 || r2 & 0x01 || !GR0_cdss(regs) || GR0_cdss(regs) > 5))
ARCH_DEP(program_interrupt)(regs, PGM_SPECIFICATION_EXCEPTION);
/* Check for empty input */
if(unlikely(!GR_A(r2 + 1, regs)))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg(" Zero input, returning cc0\n");
#endif /* #ifdef OPTION_CMPSC_DEBUG */
regs->psw.cc = 0;
return;
}
/* Check for empty output */
if(unlikely(!GR_A(r1 + 1, regs)))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg(" Zero output, returning cc1\n");
#endif /* #ifdef OPTION_CMPSC_DEBUG */
regs->psw.cc = 1;
return;
}
/* Initialize intermediate registers */
INITREGS(&iregs, regs, r1, r2);
/* Now go to the requested function */
if(likely(GR0_e(regs)))
ARCH_DEP(cmpsc_expand)(r1, r2, regs, &iregs);
else
ARCH_DEP(cmpsc_compress)(r1, r2, regs, &iregs);
}
/*============================================================================*/
/* Compress */
/*============================================================================*/
/*----------------------------------------------------------------------------*/
/* Compression Character Entry macro's (CCE) */
/*----------------------------------------------------------------------------*/
/* act : additional-extension-character count */
/* cct : child count */
/* cptr : child pointer; index of first child */
/* d : double-character entry */
/* x(i) : examine child bit for children 1 to 5 */
/* y(i) : examine child bit for 6th/13th and 7th/14th sibling */
/*----------------------------------------------------------------------------*/
#define CCE_act(cce) ((cce)[1] >> 5)
#define CCE_cct(cce) ((cce)[0] >> 5)
#define CCE_cptr(cce) ((((cce)[1] & 0x1f) << 8) | (cce)[2])
#define CCE_d(cce) ((cce)[1] & 0x20)
#define CCE_x(cce, i) ((cce)[0] & (0x10 >> (i)))
#define CCE_y(cce, i) ((cce)[1] & (0x80 >> (i)))
/*----------------------------------------------------------------------------*/
/* Compression Character Entry macro's (CCE) derived */
/*----------------------------------------------------------------------------*/
/* cc(i) : child character */
/* ccc(i) : indication consecutive child character */
/* ccs : number of child characters */
/* ec(i) : additional extension character */
/* ecs : number of additional extension characters */
/* mcc : indication if siblings follow child characters */
/*----------------------------------------------------------------------------*/
#define CCE_cc(cce, i) ((cce)[3 + CCE_ecs((cce)) + (i)])
#define CCE_ccc(cce, i) (CCE_cc((cce), (i)) == CCE_cc((cce), 0))
#define CCE_ccs(cce) (CCE_cct((cce)) - (CCE_mcc((cce)) ? 1 : 0))
#define CCE_ec(cce, i) ((cce)[3 + (i)])
#define CCE_ecs(cce) ((CCE_cct((cce)) <= 1) ? CCE_act((cce)) : (CCE_d((cce)) ? 1 : 0))
#define CCE_mcc(cce) ((CCE_cct((cce)) + (CCE_d((cce)) ? 1 : 0) == 6))
/*----------------------------------------------------------------------------*/
/* Format-0 Sibling Descriptors macro's (SD0) */
/*----------------------------------------------------------------------------*/
/* sct : sibling count */
/* y(i) : examine child bit for siblings 1 to 5 */
/*----------------------------------------------------------------------------*/
#define SD0_sct(sd0) ((sd0)[0] >> 5)
#define SD0_y(sd0, i) ((sd0)[0] & (0x10 >> (i)))
/*----------------------------------------------------------------------------*/
/* Format-0 Sibling Descriptors macro's (SD0) derived */
/*----------------------------------------------------------------------------*/
/* ccc(i) : indication consecutive child character */
/* ecb(i) : examine child bit, if y then 6th/7th fetched from parent */
/* msc : indication if siblings follows last sibling */
/* sc(i) : sibling character */
/* scs : number of sibling characters */
/*----------------------------------------------------------------------------*/
#define SD0_ccc(sd0, i) (SD0_sc((sd0), (i)) == SD0_sc((sd0), 0))
#define SD0_ecb(sd0, i, cce, y) (((i) < 5) ? SD0_y((sd0), (i)) : (y) ? CCE_y((cce), ((i) - 5)) : 1)
#define SD0_msc(sd0) (!SD0_sct((sd0)))
#define SD0_sc(sd0, i) ((sd0)[1 + (i)])
#define SD0_scs(sd0) (SD0_msc((sd0)) ? 7 : SD0_sct((sd0)))
/*----------------------------------------------------------------------------*/
/* Format-1 Sibling Descriptors macro's (SD1) */
/*----------------------------------------------------------------------------*/
/* sct : sibling count */
/* y(i) : examine child bit for sibling 1 to 12 */
/*----------------------------------------------------------------------------*/
#define SD1_sct(sd1) ((sd1)[0] >> 4)
#define SD1_y(sd1, i) ((i) < 4 ? ((sd1)[0] & (0x08 >> (i))) : ((sd1)[1] & (0x800 >> (i))))
/*----------------------------------------------------------------------------*/
/* Format-1 Sibling Descriptors macro's (SD1) derived */
/*----------------------------------------------------------------------------*/
/* ccc(i) : indication consecutive child character */
/* ecb(i) : examine child bit, if y then 13th/14th fetched from parent */
/* msc : indication if siblings follows last sibling */
/* sc(i) : sibling character */
/* scs : number of sibling characters */
/*----------------------------------------------------------------------------*/
#define SD1_ccc(sd1, sd2, i) (SD1_sc((sd1), (sd2), (i)) == SD1_sc((sd1), (sd2), 0))
#define SD1_ecb(sd1, i, cce, y) (((i) < 12) ? SD1_y((sd1), (i)) : (y) ? CCE_y((cce), ((i) - 12)) : 1)
#define SD1_msc(sd1) ((SD1_sct((sd1)) == 15))
#define SD1_sc(sd1, sd2, i) ((i) < 6 ? (sd1)[2 + (i)] : (sd2)[(i) - 6])
#define SD1_scs(sd1) (SD1_msc((sd1)) ? 14 : SD1_sct((sd1)))
/*----------------------------------------------------------------------------*/
/* Format independent sibling descriptor macro's */
/*----------------------------------------------------------------------------*/
#define SD_ccc(f1, sd1, sd2, i) ((f1) ? SD1_ccc((sd1), (sd2), (i)) : SD0_ccc((sd1), (i)))
#define SD_ecb(f1, sd1, i, cce, y) ((f1) ? SD1_ecb((sd1), (i), (cce), (y)) : SD0_ecb((sd1), (i), (cce), (y)))
#define SD_msc(f1, sd1) ((f1) ? SD1_msc((sd1)) : SD0_msc((sd1)))
#define SD_sc(f1, sd1, sd2, i) ((f1) ? SD1_sc((sd1), (sd2), (i)) : SD0_sc((sd1), (i)))
#define SD_scs(f1, sd1) ((f1) ? SD1_scs((sd1)) : SD0_scs((sd1)))
/*----------------------------------------------------------------------------*/
/* ADJUSTREGS in compression context */
/*----------------------------------------------------------------------------*/
#define ADJUSTREGSC(cc, r, regs, iregs, len) \
{ \
ADJUSTREGS((r), (regs), (iregs), (len)) \
if(likely((cc)->srclen > (len))) \
{ \
(cc)->src += (len); \
(cc)->srclen -= (len); \
} \
else \
{ \
(cc)->src = NULL; \
(cc)->srclen = 0; \
} \
}
#define BIT_get(array, is, ch) ((array)[(is)][(ch) / 8] & (0x80 >> ((ch) % 8)))
#define BIT_set(array, is, ch) ((array)[(is)][(ch) / 8] |= (0x80 >> ((ch) % 8)))
/*============================================================================*/
/*----------------------------------------------------------------------------*/
/* cmpsc_compress */
/*----------------------------------------------------------------------------*/
static void ARCH_DEP(cmpsc_compress)(int r1, int r2, REGS *regs, REGS *iregs)
{
struct cc cc; /* Compression context */
int i; /* Index */
U16 is; /* Last matched index symbol */
int j; /* Index */
GREG srclen; /* Source length */
/* Initialize compression context */
cc.dctsz = GR0_dctsz(regs);
memset(cc.deadadm, 0, sizeof(cc.deadadm));
cc.dest = NULL;
memset(cc.dict, 0, sizeof(cc.dict));
memset(cc.edict, 0, sizeof(cc.edict));
cc.dictor = GR1_dictor(iregs);
cc.f1 = GR0_f1(regs);
cc.iregs = iregs;
cc.r1 = r1;
cc.r2 = r2;
cc.regs = regs;
cc.smbsz = GR0_smbsz(regs);
cc.src = NULL;
cc.srclen = 0;
cc.st = GR0_st(regs) ? 1 : 0;
/* Initialize values */
srclen = GR_A(cc.r2 + 1, cc.iregs);
/*--------------------------------------------------------------------------*/
/* Process individual index symbols until cbn becomes zero */
while(unlikely(GR1_cbn(cc.iregs)))
{
if(unlikely(ARCH_DEP(cmpsc_compress_single_is)(&cc)))
return;
}
/*--------------------------------------------------------------------------*/
/* Block processing, cbn stays zero */
while(likely(GR_A(cc.r1 + 1, cc.iregs) >= cc.smbsz))
{
for(i = 0; i < 8; i++)
{
/* Get the next character, return on end of source */
if(unlikely(!cc.src && ARCH_DEP(cmpsc_fetch_ch)(&cc)))
{
/* Write individual found index symbols */
for(j = 0; j < i; j++)
ARCH_DEP(cmpsc_store_is)(&cc, cc.is[j]);
COMMITREGS(cc.regs, cc.iregs, cc.r1, cc.r2);
return;
}
#ifdef OPTION_CMPSC_DEBUG
logmsg("fetch_ch : %02X at " F_VADR "\n", *cc.src, GR_A(cc.r2, cc.iregs));
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Set the alphabet entry, adjust registers and initiate charcters read */
is = *cc.src;
ADJUSTREGSC(&cc, cc.r2, cc.regs, cc.iregs, 1);
cc.cr = 1;
/* Check for alphabet entry ch dead end combination */
if(unlikely(!(cc.src && BIT_get(cc.deadadm, is, *cc.src))))
{
/* Get the alphabet entry and try to find a child */
cc.cce = ARCH_DEP(cmpsc_fetch_cce)(&cc, is);
while(ARCH_DEP(cmpsc_search_cce)(&cc, &is))
{
/* Check for other dead end combination */
if(unlikely(cc.src && BIT_get(cc.deadadm, is, *cc.src)))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("dead end : %04X %02X encountered\n", is, *cc.src);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
break;
}
}
/* Registrate possible found dead ends */
if(unlikely(cc.deadend && cc.src))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("dead end : %04X in combination with", is);
for(j = 0; j < 0x100; j++)
{
if(!(j % 16))
logmsg("\n :");
if(BIT_get(cc.searchadm, 0, j))
logmsg(" ");
else
logmsg(" %02X", j);
}
logmsg("\n");
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Registrate all discovered dead ends */
for(j = 0; j < 0x100 / 8; j++)
cc.deadadm[is][j] = ~cc.searchadm[0][j];
}
}
#ifdef OPTION_CMPSC_DEBUG
else
logmsg("dead end : %04X %02X encountered\n", is, *cc.src);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Write the last match */
cc.is[i] = is;
#ifdef OPTION_CMPSC_DEBUG
logmsg("compress : is %04X (%d)\n", is, i);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
}
/* Write index symbols and commit */
ARCH_DEP(cmpsc_store_iss)(&cc);
COMMITREGS2(cc.regs, cc.iregs, cc.r1, cc.r2);
/* Return with cc3 on interrupt pending after a minumum size of processing */
if(unlikely(srclen - GR_A(cc.r2 + 1, cc.iregs) >= MINPROC_SIZE && INTERRUPT_PENDING(cc.regs)))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("Interrupt pending, commit and return with cc3\n");
#endif /* #ifdef OPTION_CMPSC_DEBUG */
cc.regs->psw.cc = 3;
return;
}
}
/*--------------------------------------------------------------------------*/
/* Process individual index symbols until end of destination (or source) */
while(!likely(ARCH_DEP(cmpsc_compress_single_is)(&cc)));
}
/*----------------------------------------------------------------------------*/
/* cmpsc_compress_single_is */
/*----------------------------------------------------------------------------*/
static int ARCH_DEP(cmpsc_compress_single_is)(struct cc *cc)
{
int i; /* Index */
U16 is; /* index symbol */
/* Get the next character, return -1 on end of source */
if(unlikely(!cc->src && ARCH_DEP(cmpsc_fetch_ch)(cc)))
return(-1);
#ifdef OPTION_CMPSC_DEBUG
logmsg("fetch_ch : %02X at " F_VADR "\n", *cc->src, GR_A(cc->r2, cc->iregs));
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Set the alphabet entry, adjust registers and initiate characters read */
is = *cc->src;
ADJUSTREGSC(cc, cc->r2, cc->regs, cc->iregs, 1);
cc->cr = 1;
/* Search for child when no src and no dead end combination */
if(unlikely(!(cc->src && BIT_get(cc->deadadm, is, *cc->src))))
{
/* Get the alphabet entry and try to find a child */
cc->cce = ARCH_DEP(cmpsc_fetch_cce)(cc, is);
while(ARCH_DEP(cmpsc_search_cce)(cc, &is))
{
/* Check for (found cce entry + ch) dead end combination */
if(unlikely(cc->src && BIT_get(cc->deadadm, is, *cc->src)))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("dead end : %04X %02X encountered\n", is, *cc->src);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
break;
}
}
/* Registrate possible found dead ends */
if(unlikely(cc->deadend && cc->src))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("dead end : %04X in combination with", is);
for(i = 0; i < 0x100; i++)
{
if(!(i % 16))
logmsg("\n :");
if(BIT_get(cc->searchadm, 0, i))
logmsg(" ");
else
logmsg(" %02X", i);
}
logmsg("\n");
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Registrate all discovered dead ends */
for(i = 0; i < 0x100 / 8; i++)
cc->deadadm[is][i] = ~cc->searchadm[0][i];
}
}
#ifdef OPTION_CMPSC_DEBUG
else
logmsg("dead end : %04X %02X encountered\n", is, *cc->src);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Write the last match, return on end of destination */
if(unlikely(ARCH_DEP(cmpsc_store_is)(cc, is)))
return(-1);
/* Commit registers */
COMMITREGS(cc->regs, cc->iregs, cc->r1, cc->r2);
return(0);
}
/*----------------------------------------------------------------------------*/
/* cmpsc_fetch_cce (compression character entry) */
/*----------------------------------------------------------------------------*/
static BYTE *ARCH_DEP(cmpsc_fetch_cce)(struct cc *cc, unsigned index)
{
BYTE *cce; /* Compression child entry */
unsigned cct; /* Child count */
index *= 8;
if(unlikely(!cc->dict[index / 0x800]))
cc->dict[index / 0x800] = MADDR((cc->dictor + (index / 0x800) * 0x800) & ADDRESS_MAXWRAP(cc->regs), cc->r2, cc->regs, ACCTYPE_READ, cc->regs->psw.pkey);
cce = &cc->dict[index / 0x800][index % 0x800];
ITIMER_SYNC((cc->dictor + index) & ADDRESS_MAXWRAP(cc->regs), 8 - 1, cc->regs);
#ifdef OPTION_CMPSC_DEBUG
logmsg("fetch_cce: index %04X\n", index / 8);
cmpsc_print_cce(cce);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Check for data exception */
cct = CCE_cct(cce);
if(cct < 2)
{
if(unlikely(CCE_act(cce) > 4))
{
cc->regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt)(cc->regs, PGM_DATA_EXCEPTION);
}
}
else
{
if(!CCE_d(cce))
{
if(unlikely(cct == 7))
{
cc->regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt)(cc->regs, PGM_DATA_EXCEPTION);
}
}
else
{
if(unlikely(cct > 5))
{
cc->regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt)(cc->regs, PGM_DATA_EXCEPTION);
}
}
}
return(cce);
}
/*----------------------------------------------------------------------------*/
/* cmpsc_fetch_ch (character) */
/*----------------------------------------------------------------------------*/
static int ARCH_DEP(cmpsc_fetch_ch)(struct cc *cc)
{
/* Check for end of source condition */
if(unlikely(!GR_A(cc->r2 + 1, cc->iregs)))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("fetch_ch : reached end of source\n");
#endif /* #ifdef OPTION_CMPSC_DEBUG */
cc->regs->psw.cc = 0;
return(-1);
}
/* Calculate source length in page */
cc->srclen = 0x800 - (GR_A(cc->r2, cc->iregs) & 0x7ff);
if(unlikely(GR_A(cc->r2 + 1, cc->iregs) < cc->srclen))
cc->srclen = GR_A(cc->r2 + 1, cc->iregs);
/* Get address */
cc->src = MADDR(GR_A(cc->r2, cc->iregs) & ADDRESS_MAXWRAP(cc->regs), cc->r2, cc->regs, ACCTYPE_READ, cc->regs->psw.pkey);
return(0);
}
#ifndef NO_2ND_COMPILE
#ifdef OPTION_CMPSC_DEBUG
/*----------------------------------------------------------------------------*/
/* cmpsc_print_cce (compression character entry) */
/*----------------------------------------------------------------------------*/
static void cmpsc_print_cce(BYTE *cce)
{
int j; /* Index */
int prt_detail; /* Switch for detailed printing */
logmsg(" cce : ");
prt_detail = 0;
for(j = 0; j < 8; j++)
{
if(!prt_detail && cce[j])
prt_detail = 1;
logmsg("%02X", cce[j]);
}
logmsg("\n");
if(prt_detail)
{
logmsg(" cct : %d\n", CCE_cct(cce));
switch(CCE_cct(cce))
{
case 0:
{
logmsg(" act : %d\n", (int) CCE_act(cce));
if(CCE_act(cce))
{
logmsg(" ec(s) :");
for(j = 0; j < CCE_ecs(cce); j++)
logmsg(" %02X", CCE_ec(cce, j));
logmsg("\n");
}
break;
}
case 1:
{
logmsg(" x1 : %c\n", (int) (CCE_x(cce, 0) ? '1' : '0'));
logmsg(" act : %d\n", (int) CCE_act(cce));
logmsg(" cptr : %04X\n", CCE_cptr(cce));
if(CCE_act(cce))
{
logmsg(" ec(s) :");
for(j = 0; j < CCE_ecs(cce); j++)
logmsg(" %02X", CCE_ec(cce, j));
logmsg("\n");
}
logmsg(" cc : %02X\n", CCE_cc(cce, 0));
break;
}
default:
{
logmsg(" x1..x5 : ");
for(j = 0; j < 5; j++)
logmsg("%c", (int) (CCE_x(cce, j) ? '1' : '0'));
logmsg("\n y1..y2 : ");
for(j = 0; j < 2; j++)
logmsg("%c", (int) (CCE_y(cce, j) ? '1' : '0'));
logmsg("\n d : %s\n", TRUEFALSE(CCE_d(cce)));
logmsg(" cptr : %04X\n", CCE_cptr(cce));
if(CCE_d(cce))
logmsg(" ec : %02X\n", CCE_ec(cce, 0));
logmsg(" ccs :");
for(j = 0; j < CCE_ccs(cce); j++)
logmsg(" %02X", CCE_cc(cce, j));
logmsg("\n");
break;
}
}
}
}
/*----------------------------------------------------------------------------*/
/* cmpsc_print_sd (sibling descriptor) */
/*----------------------------------------------------------------------------*/
static void cmpsc_print_sd(int f1, BYTE *sd1, BYTE *sd2)
{
int j; /* Index */
int prt_detail; /* Switch for detailed printing */
if(f1)
{
logmsg(" sd1 : ");
prt_detail = 0;
for(j = 0; j < 8; j++)
{
if(!prt_detail && sd1[j])
prt_detail = 1;
logmsg("%02X", sd1[j]);
}
for(j = 0; j < 8; j++)
{
if(!prt_detail && sd2[j])
prt_detail = 1;
logmsg("%02X", sd2[j]);
}
logmsg("\n");
if(prt_detail)
{
logmsg(" sct : %d\n", SD1_sct(sd1));
logmsg(" y1..y12: ");
for(j = 0; j < 12; j++)
logmsg("%c", (SD1_y(sd1, j) ? '1' : '0'));
logmsg("\n sc(s) :");
for(j = 0; j < SD1_scs(sd1); j++)
logmsg(" %02X", SD1_sc(sd1, sd2, j));
logmsg("\n");
}
}
else
{
logmsg(" sd0 : ");
prt_detail = 0;
for(j = 0; j < 8; j++)
{
if(!prt_detail && sd1[j])
prt_detail = 1;
logmsg("%02X", sd1[j]);
}
logmsg("\n");
if(prt_detail)
{
logmsg(" sct : %d\n", SD0_sct(sd1));
logmsg(" y1..y5 : ");
for(j = 0; j < 5; j++)
logmsg("%c", (SD0_y(sd1, j) ? '1' : '0'));
logmsg("\n sc(s) :");
for(j = 0; j < SD0_scs(sd1); j++)
logmsg(" %02X", SD0_sc(sd1, j));
logmsg("\n");
}
}
}
#endif /* #ifdef OPTION_CMPSC_DEBUG */
#endif /* #ifndef NO_2ND_COMPILE */
/*----------------------------------------------------------------------------*/
/* cmpsc_search_cce (compression character entry) */
/*----------------------------------------------------------------------------*/
static int ARCH_DEP(cmpsc_search_cce)(struct cc *cc, U16 *is)
{
BYTE *ccce; /* Child compression character entry */
int ccs; /* Number of child characters */
int i; /* Child character index */
int ind_search_siblings; /* Indicator for searching siblings */
/* Initialize values */
ccs = CCE_ccs(cc->cce);
/* Get the next character when there are children */
if(likely(ccs))
{
if(unlikely(!cc->src && ARCH_DEP(cmpsc_fetch_ch(cc))))
return(0);
#ifdef OPTION_CMPSC_DEBUG
logmsg("fetch_ch : %02X at " F_VADR "\n", *cc->src, GR_A(cc->r2, cc->iregs));
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Check for reading character 261 */
cc->cr++;
if(unlikely(cc->cr > 260))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("Trying to read character #%d\n", cc->cr);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
cc->regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt)(cc->regs, PGM_DATA_EXCEPTION);
}
memset(cc->searchadm, 0, sizeof(cc->searchadm));
cc->deadend = 1;
ind_search_siblings = 1;
/* Now check all children in parent */
for(i = 0; i < ccs; i++)
{
/* Stop searching when child tested and no consecutive child character */
if(unlikely(!ind_search_siblings && !CCE_ccc(cc->cce, i)))
return(0);
/* Compare character with child */
if(unlikely(*cc->src == CCE_cc(cc->cce, i)))
{
/* Child is tested, so stop searching for siblings and no dead end */
ind_search_siblings = 0;
cc->deadend = 0;
/* Check if child should not be examined */
if(unlikely(!CCE_x(cc->cce, i)))
{
/* No need to examine child, found the last match */
ADJUSTREGSC(cc, cc->r2, cc->regs, cc->iregs, 1);
*is = CCE_cptr(cc->cce) + i;
return(0);
}
/* Found a child get the character entry and check if additional extension characters match */
ccce = ARCH_DEP(cmpsc_fetch_cce)(cc, CCE_cptr(cc->cce) + i);
if(likely(!CCE_ecs(ccce) || !ARCH_DEP(cmpsc_test_ec)(cc, ccce)))
{
/* Set last match */
ADJUSTREGSC(cc, cc->r2, cc->regs, cc->iregs, (U32) CCE_ecs(ccce) + 1);
*is = CCE_cptr(cc->cce) + i;
#ifdef OPTION_CMPSC_DEBUG
logmsg("search_cce index %04X parent\n", *is);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
/* Found a matching child, make it parent and keep searching */
cc->cce = ccce;
return(1);
}
}
BIT_set(cc->searchadm, 0, CCE_cc(cc->cce, i));
}
/* Are there siblings? */
if(likely(CCE_mcc(cc->cce)))
return(ARCH_DEP(cmpsc_search_sd)(cc, is));
}
else
{
/* No children, no extension character, always a dead end */
if(!CCE_act(cc->cce))
{
#ifdef OPTION_CMPSC_DEBUG
logmsg("dead end : %04X permanent dead end discovered\n", *is);
#endif /* #ifdef OPTION_CMPSC_DEBUG */
memset(&cc->deadadm[*is], 0xff, 0x100 / 8);
}
cc->deadend = 0;
}
/* No siblings, write found index symbol */
return(0);
}
/*----------------------------------------------------------------------------*/
/* cmpsc_search_sd (sibling descriptor) */
/*----------------------------------------------------------------------------*/
static int ARCH_DEP(cmpsc_search_sd)(struct cc *cc, U16 *is)
{
BYTE *ccce; /* Child compression character entry */
int i; /* Sibling character index */
int ind_search_siblings; /* Indicator for keep searching */
U16 index; /* Index within dictionary */
int scs; /* Number of sibling characters */
BYTE *sd1; /* Sibling descriptor fmt-0|1 part 1 */
BYTE *sd2 = NULL; /* Sibling descriptor fmt-1 part 2 */
int sd_ptr; /* Pointer to sibling descriptor */
int y_in_parent; /* Indicator if y bits are in parent */