forked from dosbox-staging/dosbox-staging
/
_alpha_waves.cpp
975 lines (809 loc) · 24.4 KB
/
_alpha_waves.cpp
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
#include "_alpha_waves.hpp"
#include "emu.hpp"
#include "regs.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <functional>
#include <stack>
namespace {
uint8_t *stack_ptr(size_t offset_)
{
return MemBase + PhysMake(SegValue(ss), offset_);
}
template <typename ValueType>
ValueType stack_value(size_t offset_)
{
return *(ValueType *)stack_ptr(offset_);
}
#pragma pack(push, 1)
struct far_ptr_t {
uint16_t offset;
uint16_t segment;
};
#pragma pack(pop)
std::string to_lower(const std::string &str_)
{
std::string data = str_;
std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c) {
return std::tolower(c);
});
return data;
}
uint8_t *s_game_image_begin = nullptr;
std::string hex_string(const void *const p_buffer, const size_t &p_size,
const bool &p_as_stream)
{
std::string tmp;
if (p_size != 0) {
const size_t byte_width = p_as_stream ? 2 : 3;
const size_t string_size = (p_size * byte_width) -
(p_as_stream ? 0 : 1);
tmp.resize(string_size, ' ');
const uint8_t *const in_buffer = (uint8_t *)p_buffer;
uint8_t *const out_buffer = (uint8_t *)&tmp[0];
for (size_t i = 0; i < p_size; ++i) {
const char hex_digits[] = "0123456789ABCDEF";
const uint8_t value = in_buffer[i];
char *const chr = (char *)(out_buffer + (i * byte_width));
chr[0] = hex_digits[value >> 4];
chr[1] = hex_digits[value & 0xF];
}
}
return tmp;
}
void write_binary_file(const std::string &file_path_, const void *const data_,
size_t size_)
{
FILE *fp = fopen(file_path_.c_str(), "wb+");
assert(fp);
size_t written = fwrite(data_, 1, size_, fp);
assert(written = size_);
fclose(fp);
}
void write_string_file(const std::string &file_path_, const char *str_)
{
FILE *fp = fopen(file_path_.c_str(), "w+");
assert(fp);
fprintf(fp, "%s", str_);
fclose(fp);
}
} // namespace
namespace alpha_waves {
uint8_t *by_IDA_EA_offset(size_t ea_)
{
constexpr size_t IDA_BASE = 0x10000;
return s_game_image_begin + (ea_ - IDA_BASE);
};
uint8_t *s_compress_proc_begin{};
void detect_game_exe(const std::string &program_name_, PhysPt loadaddress_)
{
if (to_lower(program_name_) == "vga.exe") {
// gets set on every game.exe start
// loadseg = exe image start, right after_input PSP
s_game_image_begin = MemBase + loadaddress_;
std::array<uint8_t, 9> buffer{};
memcpy(buffer.data(), s_game_image_begin + 3, buffer.size());
// std::string x = hex_string(buffer.data(), buffer.size(),
// buffer.size());
const std::array<uint8_t, 9> original{
0x2E, 0x89, 0x16, 0xBA, 0x01, 0xB4, 0x30, 0xCD, 0x21};
if (buffer != original) {
LOG_MSG("!!!!!!!! ALPHA WAVES: not the correct vga.exe");
}
LOG_MSG("!!!!!!!! ALPHA WAVES: vga.exe startet");
// get pointer of UNCOMPRESS_sub_1BAE7 ==> seg000(cs):BAE7
s_compress_proc_begin = by_IDA_EA_offset(0x1BAE7);
}
}
namespace {
int state = 0;
// 0 = init // first_instruction
// 1 = first call of UNCOMPRESS_sub_1BAE7 - waiting for last_instruction, can
// happen in loop
int block = 0;
} // namespace
void detect_code_run(Bitu cs_, Bitu ip_)
{
if (!s_game_image_begin) {
return;
}
const uint8_t *current = MemBase + PhysMake(cs_, ip_);
/*
first: IDA-EA: 0x1BAE7
seg000:BAE7 06 push es
seg000:BAE8 57 push di
seg000:BAE9 B9 80 00 mov cx, 80h ;
'Ç' seg000:BAEC 8C D8 mov ax, ds
seg000:BAEE 8E C0 mov es, ax
*/
static const std::string path = "d:/temp/alpha";
auto block_info = []() { return "block_ " + std::to_string(block); };
const uint8_t *first_instruction = by_IDA_EA_offset(0x1BAE7);
if (current == first_instruction) {
std::array<uint8_t, 9> buffer{};
memcpy(buffer.data(), current, buffer.size());
const std::array<uint8_t, 9> original{
0x06, 0x57, 0xB9, 0x80, 0x00, 0x8C, 0xD8, 0x8E, 0xC0};
assert(buffer == original);
if (state == 0) {
state = 1;
// store registers (DS:SI, ES:DI, CS => block_n_regs.txt)
char buffer[1024]{};
sprintf_s(buffer,
"DS=0x%04X, SI=0x%04X, ES=0x%04X, DI=0x%04X, CS = 0x%04X\n",
SegValue(ds),
reg_si,
SegValue(es),
reg_di,
SegValue(cs));
write_string_file(path + "/" + block_info() + "_regs_before.txt",
buffer);
// store 1MB memory (block_n_dump_before.bin)
write_binary_file(path + "/" + block_info() + "_dump_before.bin",
MemBase,
1024 * 1024);
printf("begin: UNCOMPRESS_sub_1BAE7\n");
}
}
/*
last: IDA-EA: 0x1BBE0
seg000:BBE0 locret_1BBE0: ; CODE XREF:
UNCOMPRESS_sub_1BAE7+F4j seg000:BBE0 C3 retn seg000:BBE1 ;
---------------------------------------------------------------------------
seg000:BBE1
seg000:BBE1 loc_1BBE1: ; CODE XREF:
UNCOMPRESS_sub_1BAE7+ECj seg000:BBE1 1E push ds seg000:BBE2 2E 8B 36
A6 BA mov si, word ptr cs:dword_1BAA4+2
*/
const uint8_t *last_instruction = by_IDA_EA_offset(0x1BBE0);
if (current == last_instruction) {
std::array<uint8_t, 7> buffer{};
memcpy(buffer.data(), current, buffer.size());
const std::array<uint8_t, 7> original{
0xC3, 0x1E, 0x2E, 0x8B, 0x36, 0xA6, 0xBA};
assert(buffer == original);
if (state == 1) {
printf("end: UNCOMPRESS_sub_1BAE7\n");
// store 1MB result memory )
write_binary_file(path + "/" + block_info() + "_dump_after.bin",
MemBase,
1024 * 1024);
state = 0;
++block;
}
}
}
// replace call at
// seg000:BADC E8 08 00 call UNCOMPRESS_sub_1BAE7
// to
// native code
void UNCOMPRESS_sub_1BAE7_cleanup(emu_t &e);
void UNCOMPRESS_sub_1BAE7(emu_t &e);
bool detect_drv_call(Bitu cs_, Bitu ip_)
{
if (!s_compress_proc_begin) {
return false;
}
const uint8_t *current = MemBase + PhysMake(cs_, ip_);
if (s_compress_proc_begin == current) {
printf("compression called\n");
emu_t e;
// copy memory
::memcpy(e.memory().data(), MemBase, 1 * 1024 * 1024);
e.cs = SegValue(cs);
e.ds = SegValue(ds);
e.es = SegValue(es);
e.si = reg_si;
e.di = reg_di;
#if 1
UNCOMPRESS_sub_1BAE7_cleanup(e);
#else
UNCOMPRESS_sub_1BAE7(e);
#endif
::memcpy(MemBase, e.memory().data(), 1 * 1024 * 1024);
return true;
}
return false;
}
// seg000:B172 ; int __cdecl
// READ_CC1_BLOCK_sub_1B172(char *s, int maybe_block_nr) seg000:B172
// READ_CC1_BLOCK_sub_1B172 proc near ; CODE XREF: sub_1789B+26p
// seg000:B172 ; sub_18E2C+Fp ...
// seg000:B185 FF 76 06 push word ptr [bp+s+2]
// seg000:B188 FF 76 04 push word ptr [bp+s] ;
// src
// s is far_ptr
// seg000:B214 8B 5E 08 mov bx,
// [bp+maybe_block_nr]
// block_nr is word
/*
uint16_t bp = reg_sp - 2;
far_ptr_t string = stack_value<far_ptr_t>(bp + 4);
uint16_t bock_nr = stack_value<uint16_t>(bp + 8);
*/
// make offset as small as possible
void normalize_ptr(uint16_t &seg_, uint16_t &ofs_)
{
seg_ += ofs_ / 16;
ofs_ = ofs_ % 16;
}
constexpr uint8_t LAST_BLOCK = 0;
constexpr uint8_t NOT_LAST_BLOCK = 1;
#pragma pack(push, 1)
struct block_t {
uint8_t packed_size{}; // 0xBA9A
uint8_t flag{}; // 0 = last block, 1 = more blocks comming -> 0xBA9B
uint16_t data_len{}; // (un)compressed data -> 0xBA9C
};
static_assert(sizeof(block_t) == 4, "invalid size");
#pragma pack(pop)
struct stack_item_t {
uint8_t a{};
uint8_t b{};
};
void DEBUG_prepare_result_ptrs(const uint8_t *input_ptr, emu_t::ptr16_t &input_ptr16,
emu_t &e, uint8_t *output_ptr)
{
// DEBUG/TEST: getting "processed" input/output size
// from outside this routine
// set es:di to point to output end (as original code
// behave)
const auto es_di = e.ptr_to_ptr16(output_ptr);
e.es = es_di.segment;
e.di = es_di.offset;
input_ptr16 = e.ptr_to_ptr16(input_ptr); // overwrite
// far ptr with
// new position
}
#define PRINT_STACK() (false)
struct tables_t {
//uint8_t *byte_000{};
std::vector<uint8_t> table0;
//uint8_t *byte_100{};
std::vector<uint8_t> table1;
//uint8_t *byte_301{};
std::vector<uint8_t> table3;
//uint8_t *byte_402{};
std::vector<uint8_t> table4;
};
constexpr uint8_t UNPACKED_VAL = 0;
void UNCOMPRESS_sub_1BAE7_cleanup_uncompress_part(
const uint8_t start_val_, const uint8_t *&input_ptr, uint8_t *&output_ptr,
const tables_t& tables)
{
std::stack<stack_item_t> stack;
uint8_t var2 = start_val_;
outer_loop:
// var1 is not available here, later defined
// printf("restart_loop var2: % 2u\n", var2);
assert(var2 > 0);
stack.push({tables.table1[var2], var2});
#if PRINT_STACK()
printf("%spush a: % 2u b: % 2u\n",
std::string(stack.size() * 2, ' ').c_str(),
stack.top().a,
stack.top().b);
#endif
uint8_t var1 = tables.table0[var2];
assert(var1 >= 0);
while (true) {
const uint8_t old_var1 = var1;
const uint8_t table3_val = tables.table3[var1];
assert(table3_val >= 0);
if (table3_val == UNPACKED_VAL) {
goto store_then_end_or_loop;
} else if (var2 > table3_val) {
var2 = table3_val;
goto outer_loop;
} else {
var1 = var2;
var2 = table3_val;
while (true) {
assert(var2 > 0);
var2 = tables.table4[var2];
if (var2 == 0) { // also UNPACKED_VAL?
var1 = old_var1;
goto store_then_end_or_loop;
} else if (var2 < var1) {
goto outer_loop;
}
}
}
// ---------------------------------------------------------------------------
store_then_end_or_loop:
// printf("store_then_end_or_loop\n");
*output_ptr++ = var1;
if (stack.size() == 0) {
return;
}
const stack_item_t item = stack.top();
#if PRINT_STACK()
printf("%spop a: % 2u b: % 2u\n",
std::string(stack.size() * 2, ' ').c_str(),
stack.top().a,
stack.top().b);
#endif
stack.pop();
// from stack
var1 = item.a;
var2 = item.b;
}
}
tables_t prepare_tables(emu_t& e, const block_t& block, const uint8_t *&input_ptr)
{
assert(block.packed_size != 0);
//----------
// only for uncompressing
//uint8_t *byte_000 = e.byte_ptr(e.ds, 0x000);
//uint8_t *byte_001 = e.byte_ptr(e.ds, 0x001);
//uint8_t *byte_100 = e.byte_ptr(e.ds, 0x100);
//uint8_t *byte_101 = e.byte_ptr(e.ds, 0x101);
//uint8_t *byte_200 = e.byte_ptr(e.ds, 0x200);
//uint8_t *table2 = e.byte_ptr(e.ds, 0x201);
//uint8_t *byte_301 = e.byte_ptr(e.ds, 0x301);
//uint8_t *byte_402 = e.byte_ptr(e.ds, 0x402);
//----------
// read & prepare uncompress-helper tables
std::vector<uint8_t> table0(1 + block.packed_size);
std::vector<uint8_t> table1(1 + block.packed_size);
std::vector<uint8_t> table2(block.packed_size); // only needed for initialization, not for uncompression
std::vector<uint8_t> table3(256);
std::vector<uint8_t> table4(1 + block.packed_size);
{
::memcpy(table2.data(), input_ptr, block.packed_size);
input_ptr += block.packed_size;
table0[0] = 0xFF; // unused, never read
::memcpy(&table0[1], input_ptr, block.packed_size);
input_ptr += block.packed_size;
table1[0] = 0xFF; // unused, never read
::memcpy(&table1[1], input_ptr, block.packed_size);
input_ptr += block.packed_size;
// its currently, unclear what the max-packed_size could be
// packed_size is uint8_t so max would be 255
for (int i = 0; i < block.packed_size; ++i) {
const uint8_t ofs = table2[i];
const uint8_t index = i + 1; // (0..255)+1
assert(ofs >= 0);
uint8_t *value = &table3[ofs]; // [0] is used
table4[index] = *value; //1+256 [0] ignored, [1-256]
*value = index;
}
table4[0] = 0xFF; // unused, never read
}
const tables_t tables{table0, table1, table3, table4};
return tables;
}
void UNCOMPRESS_sub_1BAE7_cleanup_uncompress_block(
const block_t &block, const uint8_t *&input_ptr, uint8_t *&output_ptr, emu_t &e)
{
const tables_t tables = prepare_tables(e, block, input_ptr);
for (int i = 0; i < block.data_len; ++i)
{
const uint8_t var1 = *input_ptr++;
const uint8_t var2 = tables.table3[var1]; // var1 0..n
if (var2 == UNPACKED_VAL) { // uncompressed part
*output_ptr++ = var1; // just store value
} else { // compressed part
UNCOMPRESS_sub_1BAE7_cleanup_uncompress_part(var2,
input_ptr,
output_ptr,
tables);
}
}
}
void UNCOMPRESS_sub_1BAE7_cleanup(emu_t &e)
{
uint16_t *word_BAA2 = e.word_ptr(e.cs, 0xBAA2);
emu_t::ptr16_t &input_ptr16 = *e.memory<emu_t::ptr16_t>(e.cs, 0xBAA4);
const uint8_t *input_ptr = e.byte_ptr(input_ptr16);
uint8_t *output_ptr = e.byte_ptr(e.es, e.di);
while (true) {
block_t block{};
::memcpy(&block, input_ptr, sizeof(block));
input_ptr += sizeof(block);
assert(block.flag == LAST_BLOCK || block.flag == NOT_LAST_BLOCK);
if (block.packed_size == 0) { // is not packed?
::memcpy(output_ptr, input_ptr, block.data_len);
input_ptr += block.data_len;
output_ptr += block.data_len;
} else {
// biggest block.packed_size so far: 223
UNCOMPRESS_sub_1BAE7_cleanup_uncompress_block(block, input_ptr, output_ptr, e);
}
if (block.flag == LAST_BLOCK) {
DEBUG_prepare_result_ptrs(input_ptr, input_ptr16, e, output_ptr);
return; // the-end
}
}
}
struct data_block_t {
uint32_t packed_size{};
uint32_t unpacked_size{};
std::vector<uint8_t> data;
};
inline uint8_t lo(uint16_t value_)
{
return value_ & 0xFF;
}
inline uint8_t hi(uint16_t value_)
{
return value_ >> 8;
}
inline uint16_t lo(uint32_t value_)
{
return value_ & 0xFFFF;
}
inline uint16_t hi(uint32_t value_)
{
return value_ >> 16;
}
inline uint16_t swap(const uint16_t value_)
{
return (value_ << 8) + (value_ >> 8);
}
inline uint32_t swap(const uint32_t value_)
{
const uint16_t lv = lo(value_);
const uint16_t hv = hi(value_);
return (swap(lv) << 16) + swap(hv);
}
std::vector<uint8_t> read_binary_file(const std::string &filename_)
{
std::ifstream file(filename_, std::ios::binary);
assert(file);
return std::vector<uint8_t>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
}
std::vector<data_block_t> read_cc1_file(const std::string &filepath_)
{
const std::vector<uint8_t> content = read_binary_file(filepath_);
const uint8_t *current = content.data();
uint16_t offset_count = swap(*(uint16_t *)current);
current += sizeof(uint16_t);
std::vector<uint32_t> offset_table(offset_count);
for (size_t i = 0; i < offset_count; ++i) {
uint32_t offset = swap(*(uint32_t *)current);
current += sizeof(uint32_t);
offset_table[i] = offset;
}
std::vector<data_block_t> data_blocks(offset_count);
for (size_t i = 0; i < offset_count; ++i) {
uint32_t data_size = swap(*(uint32_t *)current);
current += sizeof(uint32_t);
uint32_t packed_data_size = swap(*(uint32_t *)current);
current += sizeof(uint32_t);
std::vector<uint8_t> data(data_size);
::memcpy(data.data(), current, data_size);
current += data_size;
data_blocks[i] = {data_size, packed_data_size, data};
}
// size of the parts + offsets fits exact the file size? no gaps?
size_t result_size = sizeof(uint16_t) + offset_count * sizeof(uint32_t);
for (size_t i = 0; i < offset_count; ++i) {
result_size += sizeof(uint32_t) + sizeof(uint32_t) +
data_blocks[i].packed_size;
}
assert(result_size == content.size());
return data_blocks;
}
std::vector<uint8_t> run_test(std::function<void(emu_t &e)> test_func_,
const data_block_t &db, size_t *used_input_bytes,
size_t *used_output_bytes)
{
// memory
emu_t e;
e.cs = 0;
e.ds = 0x1000;
e.si = 0;
// output
e.es = 0xE000;
e.di = 0;
uint8_t *output_data = e.byte_ptr(e.es, e.di);
::memset(output_data, 0, db.unpacked_size);
uint16_t *word_BAA2 = e.word_ptr(e.cs, 0xBAA2);
*word_BAA2 = 0;
// input
far_ptr_t *ptr_BAA4 = e.memory<far_ptr_t>(e.cs, 0xBAA4);
ptr_BAA4->segment = 0xC000;
ptr_BAA4->offset = 0;
const auto before_input = *ptr_BAA4;
const auto before_output = e.offset32(e.es, e.di);
uint8_t *input_data = e.byte_ptr(ptr_BAA4->segment, ptr_BAA4->offset);
::memcpy(input_data, db.data.data(), db.data.size());
test_func_(e);
const auto after_input = *ptr_BAA4;
const auto after_output = e.offset32(e.es, e.di);
*used_input_bytes = e.offset32(after_input.segment, after_input.offset) -
e.offset32(before_input.segment, before_input.offset);
*used_output_bytes = after_output - before_output;
return {output_data, output_data + db.unpacked_size};
}
void cc1_read_test()
{
#if 1
const std::string game_root = R"(F:\projects\fun\dos_games_rev\alpha_waves_dev\tests\alpha)";
const std::vector<std::string> files{"PROGS.CC1",
"GRAPHS.CC1",
"MUSIC_A.CC1",
"MUSIC_B.CC1",
"MUSIC_T.CC1",
"TEXTES.CC1"};
#else
const std::string game_root = R"(F:\projects\fun\dos_games_rev\alpha_waves_dev\tests\000135_alone_in_the_dark)";
const std::vector<std::string> files{"disk4\\INDARK.CC1",
"disk1\\INFO.CC1"};
#endif
for (const auto &file : files) {
const std::string in_filepath = game_root + "\\" + file;
const std::string out_filepath = game_root +
"\\cc1_extract\\ported\\" + file;
printf("%s\n", in_filepath.c_str());
std::vector<data_block_t> data_blocks = read_cc1_file(in_filepath);
#if 1
// print info
printf("blocks\n");
for (size_t i = 0; i < data_blocks.size(); ++i) {
const auto &db = data_blocks[i];
printf(" [%u] packed_size: %u, unpacked_size: %u\n",
i,
db.packed_size,
db.unpacked_size);
const uint16_t word_BAA2 = 0;
const uint8_t *input = db.data.data();
std::vector<uint8_t> unpacked(db.unpacked_size);
uint8_t *output = unpacked.data();
size_t used_input_bytes = 0;
size_t used_output_bytes = 0;
{
size_t used_input_bytes1 = 0;
size_t used_output_bytes1 = 0;
auto unpacked1 = run_test(UNCOMPRESS_sub_1BAE7,
db,
&used_input_bytes1,
&used_output_bytes1);
size_t used_input_bytes2 = 0;
size_t used_output_bytes2 = 0;
auto unpacked2 = run_test(UNCOMPRESS_sub_1BAE7_cleanup,
db,
&used_input_bytes2,
&used_output_bytes2);
assert(used_input_bytes1 == used_input_bytes2);
assert(used_output_bytes1 == used_output_bytes2);
assert(unpacked1 == unpacked2);
#if 0
char filename[256]{};
sprintf( filename, "%s_block%u.bin", out_filepath.c_str(), i );
write_binary_file( filename, unpacked2.data(), unpacked2.size() );
#endif
}
// PROGS.CC1
/*
sound.pc_buz
sound.tandy
sound.adlib
gfx.cga_hercules
gfx.ega_vga
gfx.tandy
*/
// if (used_input_bytes != db.data.size()) {
// printf(" ===> used_input_bytes[[%u]] !=
// db.data.size()[[%u]]\n", used_input_bytes,
// db.data.size());
//}
// if (used_output_bytes != unpacked.size()) {
// printf(" ===> used_output_bytes[[%u]] !=
// unpacked.size()[[%u]]\n",
// used_output_bytes,unpacked.size() );
//}
# if 0
char filename[256]{};
sprintf( filename, "%s_block%u.bin", out_filepath.c_str(), i );
write_binary_file( filename, unpacked.data(), unpacked.size() );
# endif
}
#endif
}
int brk = 1;
}
struct static_test_t {
static_test_t()
{
cc1_read_test();
}
};
static_test_t test;
void UNCOMPRESS_sub_1BAE7(emu_t &e)
{
start:
e.push(e.es);
e.push(e.di);
e.cx = 0x80;
e.ax = e.ds;
e.es = e.ax;
e.di = 0x301;
e.xor (e.ax, e.ax);
e.rep_stosw();
e.pop(e.di);
e.pop(e.es);
e.sub(e.di, *e.word_ptr(e.cs, 0xBAA2));
e.ax = e.di;
e.shr(e.ax, 1);
e.shr(e.ax, 1);
e.shr(e.ax, 1);
e.shr(e.ax, 1);
e.cx = e.es;
e.add(e.cx, e.ax);
e.es = e.cx;
e.and (e.di, 0x0F);
e.add(e.di, *e.word_ptr(e.cs, 0xBAA2));
e.push(e.ds);
e.push(e.es);
e.push(e.si);
e.push(e.di);
e.cx = 4;
e.di = 0xBA9A; // offset byte_1BA9A; ???
e.ax = e.cs; // seg seg000 // cs register; ???
e.es = e.ax;
e.lds(e.si, *e.dword_ptr(e.cs, 0xBAA4));
e.ax = e.si;
e.shr(e.ax, 1);
e.shr(e.ax, 1);
e.shr(e.ax, 1);
e.shr(e.ax, 1);
e.dx = e.ds;
e.add(e.ax, e.dx);
e.ds = e.ax;
e.and (e.si, 0x0F);
*e.word_ptr(e.cs, 0xBAA4) = e.si;
*e.word_ptr(e.cs, 0xBAA4 + 2) = e.ds;
e.add(*e.word_ptr(e.cs, 0xBAA4), e.cx);
e.rep_movsb();
e.pop(e.di);
e.pop(e.si);
e.pop(e.es);
e.pop(e.ds);
e.dx = *e.word_ptr(e.cs, 0xBA9C);
e.inc(e.dx);
e.cmp(*e.byte_ptr(e.cs, 0xBA9A), 0);
if (e.jnz())
goto loc_1BB63;
goto loc_1BC52;
// ---------------------------------------------------------------------------
loc_1BB63:
e.push(e.ds);
e.push(e.es);
e.push(e.di);
e.xor (e.ch, e.ch);
e.cl = *e.byte_ptr(e.cs, 0xBA9A);
e.di = 0x201;
e.ax = e.ds;
e.es = e.ax;
e.ds = *e.word_ptr(e.cs, 0xBAA4 + 2);
e.si = *e.word_ptr(e.cs, 0xBAA4);
e.add(*e.word_ptr(e.cs, 0xBAA4), e.cx);
e.rep_movsb();
e.cl = *e.byte_ptr(e.cs, 0xBA9A);
e.xor (e.ch, e.ch);
e.di = 1;
e.add(*e.word_ptr(e.cs, 0xBAA4), e.cx);
e.rep_movsb();
e.cl = *e.byte_ptr(e.cs, 0xBA9A);
e.di = 0x101;
e.add(*e.word_ptr(e.cs, 0xBAA4), e.cx);
e.rep_movsb();
e.pop(e.di);
e.pop(e.es);
e.pop(e.ds);
e.xor (e.ch, e.ch);
e.cl = *e.byte_ptr(e.cs, 0xBA9A);
e.xor (e.ah, e.ah);
e.bx = 1;
loc_1BBB4:
e.al = *e.byte_ptr(e.ds, e.bx + 0x200);
e.si = e.ax;
e.dl = *e.byte_ptr(e.ds, e.si + 0x301);
*e.byte_ptr(e.ds, e.bx + 0x402) = e.dl;
*e.byte_ptr(e.ds, e.si + 0x301) = e.bl;
e.inc(e.bx);
if (e.loop())
goto loc_1BBB4;
e.dx = *e.word_ptr(e.cs, 0xBA9C);
e.inc(e.dx);
e.cx = 1;
loc_1BBD2:
e.dec(e.dx);
if (e.jnz())
goto loc_1BBE1;
loc_1BBD5:
e.cmp(*e.byte_ptr(e.cs, 0xBA9B), 0);
if (e.jz())
goto locret_1BBE0;
goto start;
// ---------------------------------------------------------------------------
locret_1BBE0:
return;
// ---------------------------------------------------------------------------
loc_1BBE1:
e.push(e.ds);
e.si = *e.word_ptr(e.cs, 0xBAA4 + 2);
e.ds = e.si;
e.si = *e.word_ptr(e.cs, 0xBAA4);
e.lodsb();
*e.word_ptr(e.cs, 0xBAA4) = e.si;
e.pop(e.ds);
e.bx = e.ax;
e.cmp(*e.byte_ptr(e.ds, e.bx + 0x301), 0);
if (e.jnz())
goto loc_1BC01;
e.stosb();
goto loc_1BBD2;
// ---------------------------------------------------------------------------
loc_1BC01:
e.bl = *e.byte_ptr(e.ds, e.bx + 0x301);
e.xor (e.ax, e.ax);
e.push(e.ax);
goto loc_1BC35;
// ---------------------------------------------------------------------------
loop_x:
e.bp = e.ax;
e.cmp(*e.byte_ptr(e.ds, e.bp + 0x301), 0);
if (e.jz())
goto loc_1BC44;
e.cmp(e.bl, *e.byte_ptr(e.ds, e.bp + 0x301));
if (e.ja())
goto loc_1BC30;
e.al = e.bl;
e.bl = *e.byte_ptr(e.ds, e.bp + 0x301);
loc_1BC22:
e.bl = *e.byte_ptr(e.ds, e.bx + 0x402);
e.or (e.bl, e.bl);
if (e.jz())
goto loc_1BC42;
e.cmp(e.bl, e.al);
if (e.jb())
goto loc_1BC35;
goto loc_1BC22;
// ---------------------------------------------------------------------------
loc_1BC30:
e.bl = *e.byte_ptr(e.ds, e.bp + 0x301);
loc_1BC35:
e.al = *e.byte_ptr(e.ds, e.bx + 0x100);
e.ah = e.bl;
e.push(e.ax);
e.xor (e.ah, e.ah);
e.al = *e.byte_ptr(e.ds, e.bx);
goto loop_x;
// ---------------------------------------------------------------------------
loc_1BC42:
e.ax = e.bp;
loc_1BC44:
e.stosb();
e.pop(e.ax);
e.or (e.ax, e.ax);
if (e.jnz())
goto loc_1BC4C;
goto loc_1BBD2;
// ---------------------------------------------------------------------------
loc_1BC4C:
e.bl = e.ah;
e.xor (e.ah, e.ah);
goto loop_x;
// ---------------------------------------------------------------------------
loc_1BC52:
e.push(e.ds);
e.push(e.es);
e.cx = *e.word_ptr(e.cs, 0xBA9C);
e.push(e.cx);
e.ds = *e.word_ptr(e.cs, 0xBAA4 + 2);
e.si = *e.word_ptr(e.cs, 0xBAA4);
e.add(*e.word_ptr(e.cs, 0xBAA4), e.cx);
e.rep_movsb();
e.pop(e.cx);
e.pop(e.es);
e.pop(e.ds);
goto loc_1BBD5;
}
} // namespace alpha_waves