-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDToImageThreadUnit.pas
More file actions
1708 lines (1506 loc) · 66.8 KB
/
Copy pathCDToImageThreadUnit.pas
File metadata and controls
1708 lines (1506 loc) · 66.8 KB
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
unit CDToImageThreadUnit;
interface
uses
Classes,
SysUtils,
Windows,
Math,
MMC1Unit,
SPC_Unit,
CommonCDSettingsUnit,
TOCUnit,
Tracks_Unit,
CRC_Unit,
CDROM_struct_Unit;
{type
Ptr_byte=^Byte;
T_P_subch_deint_PW_96=^T_subch_deint_PW_96;
T_sub_PW_12byte_pack=Array[1..12] Of Byte;
T_subch_deint_PW_96=Record
PW_byte : Array[1..8] Of T_sub_PW_12byte_pack;
End;
T_P_sub_PW_96byte=^T_sub_PW_96byte;
T_sub_PW_96byte=Record
PW_byte : Array[1..96] Of Byte;
End;}
const
U_TRACK_MODE_AUDIO=0;
U_TRACK_MODE_CDROM=1;
U_TRACK_MODE_USE_TOC=2;
type T_array16_byte=Array[1..16] Of Byte;
type
TCDToImageThread = class(TThread)
private
{ Private declarations }
StartSect : LongInt;
LeadOutSect : LongInt;
N_Sect : LongWord;
read_data_speed : Word;
FilterMode : Byte;
SubChSelMode : Byte;
is_TEB : Boolean;
f_main : TFileStream;
f_subch : TFileStream;
f_toc : TFileStream;
f_pregap1 : TFileStream;
f_LO : TFileStream;
f_C2 : TFileStream;
C2_error_field : Byte;
C2_read_order : Byte;
lead_in_type : Byte;
lead_in_size : LongInt;
lead_out_size : LongInt;
is_deint_subs : Boolean;
show_err_msg : String;
subch_deint_PW_96 : T_subch_deint_PW_96;
sub_int_PW_96 : T_sub_int_PW_96;
TOC : T_TOC;
tracks : T_tracks;
Pre_gap_len : Word;
Post_gap_len : Word;
MMCLBA_sector : LongInt;
curr_abs_LBA_to_write : LongInt;
procedure ShowErrMsg;
protected
procedure Execute; override;
procedure ReadCDToImage(in_main_file : TFileStream;
in_subch_file : TFileStream;
in_C2_file : TFileStream);
procedure FillA0SubChEntry(First_trkNo : Byte;
Ctrl : Byte);
procedure AddA0TOCEntry(First_trkNo : Byte;
Ctrl : Byte);
procedure AddA1TOCEntry(Last_trkNo : Byte);
function Read1TOCEntryFromCD(Read_first : Boolean;
Var First_trkNo : Byte;
Var Last_trkNo : Byte;
Var Ctrl : Byte) : Boolean;
procedure ReadTOCToMem;
procedure ReadTOCToImage;
function Read1FullTOCEntryFromCD(Read_first : Boolean) : Boolean;
procedure ReadFullTOCToMem;
procedure ReadFullTOCToImage;
procedure Gen_TOC_image(in_toc_file : TFileStream);
procedure Gen_pregap(in_pregap1_file : TFileStream;
in_preceding_trackno : Byte);
procedure Gen_LO_image(in_LO_file : TFileStream);
public
Constructor Create(Thread_done_proc : TNotifyEvent;
in_StartSect : LongInt;
in_N_Sect : LongWord;
in_read_data_speed : Word;
in_FilterMode : Byte;
in_SubChSelMode : Byte;
in_is_TEB : Boolean;
in_f_main : TFileStream;
in_f_subch : TFileStream;
in_f_toc : TFileStream;
in_f_pregap1 : TFileStream;
in_f_LO : TFileStream;
in_f_C2 : TFileStream;
in_C2_error_field : Byte;
in_C2_read_order : Byte;
in_lead_in_type : Byte;
in_lead_in_size : LongInt;
in_lead_out_size : LongInt;
in_deinterleave : Boolean);
end;
implementation
Uses
MainFormUnit;
Constructor TCDToImageThread.Create(Thread_done_proc : TNotifyEvent;
in_StartSect : LongInt;
in_N_Sect : LongWord;
in_read_data_speed : Word;
in_FilterMode : Byte;
in_SubChSelMode : Byte;
in_is_TEB : Boolean;
in_f_main : TFileStream;
in_f_subch : TFileStream;
in_f_toc : TFileStream;
in_f_pregap1 : TFileStream;
in_f_LO : TFileStream;
in_f_C2 : TFileStream;
in_C2_error_field : Byte;
in_C2_read_order : Byte;
in_lead_in_type : Byte;
in_lead_in_size : LongInt;
in_lead_out_size : LongInt;
in_deinterleave : Boolean);
Begin
StartSect :=in_StartSect;
N_Sect :=in_N_Sect;
read_data_speed :=in_read_data_speed;
FilterMode :=in_FilterMode;
SubChSelMode :=in_SubChSelMode;
is_TEB :=in_is_TEB;
f_main :=in_f_main;
f_subch :=in_f_subch;
f_toc :=in_f_toc;
f_pregap1 :=in_f_pregap1;
f_LO :=in_f_LO;
f_C2 :=in_f_C2;
C2_read_order :=in_C2_read_order;
C2_error_field :=in_C2_error_field;
lead_in_type :=in_lead_in_type;
lead_in_size :=in_lead_in_size;
lead_out_size :=in_lead_out_size;
is_deint_subs :=in_deinterleave;
//GetMem(De_SubCh_buf, 96);
TOC:=T_TOC.Create;
OnTerminate:=Thread_done_proc;
//FreeOnTerminate := True;
Inherited Create(False);
End;
procedure TCDToImageThread.Execute;
begin
Pre_gap_len:=150;
Post_gap_len:=150;
Form1.SCSI.MMC1_any_link.Do_set_CD_speed_CDB12(read_data_speed, MMC_SET_CD_SPEED_MAX);
If f_toc=nil Then
Begin
//Create single *.DAO file..
ReadFullTOCToMem;
tracks:=T_tracks.Create(TOC);
Gen_TOC_image(f_main);
Gen_pregap(f_main, 0);
ReadCDToImage(f_main, f_subch, f_C2);
Gen_LO_image(f_main);
End
Else
Begin
If f_LO<>nil Then
Begin
//Create *.LI, *.PG1 file..
ReadFullTOCToMem;
tracks:=T_tracks.Create(TOC);
Gen_TOC_image(f_toc);
Gen_pregap(f_pregap1, 0);
End
Else
Begin
ReadFullTOCToImage;
tracks:=T_tracks.Create(TOC);
End;
//Create *.IMG, *.SUB file..
ReadCDToImage(f_main, f_subch, f_C2);
If f_LO<>nil Then
Begin
//Create *.LO file..
Gen_LO_image(f_LO);
End;
End;
//FreeMem(De_SubCh_buf);
TOC.Free;
tracks.Free;
end;
Procedure TCDToImageThread.ShowErrMsg;
Begin
Form1.Form4.CDToImageProgressForm.ShowErrMsg(show_err_msg);
End;
Procedure TCDToImageThread.ReadCDToImage(in_main_file : TFileStream;
in_subch_file : TFileStream;
in_C2_file : TFileStream);
Var
Sector_block_size : Word;
SubCh_block_size : Byte;
C2_block_size : Word;
Max_N_Sect_block_size : LongWord;
Data_1block_size : Word;
N_Sect_blocks_to_read : LongWord;
Total_bytes_read : LongWord;
i : LongInt;
j : LongWord;
k : LongWord;
HSA_sector : LongInt;
stop_looping : Boolean;
SubCh_buf : Ptr_byte;
buf_offset : Pointer;
buf_offset2 : Pointer;
s : String;
Out_CD_cap_mech_st : T_pub_CD_cap_mech_st;
Begin
Sector_block_size:=Form1.SCSI.MMC1_any_link.Get_MMC1.GetBlockSizeFromFilterReadFormat(MMC_SECTORTYPE_ANY,
MMC_READCD_SYNC_ALLHDR_USERDATA_EDCECC);
SubCh_block_size:=Form1.SCSI.MMC1_any_link.Get_MMC1.GetBlockSizeFromSubCh(SubChSelMode);
C2_block_size:=Form1.SCSI.MMC1_any_link.Get_MMC1.GetBlockSizeFromC2ErrorField(MMC_READCD_SYNC_ALLHDR_USERDATA_EDCECC Or C2_error_field);
Data_1block_size:=SubCh_block_size+Sector_block_size+C2_block_size;
Max_N_Sect_block_size:=Form1.SCSI.SPC_any_link.Get_data_buf_size DIV Data_1block_size;
i:=StartSect;
j:=N_Sect;
If j>Max_N_Sect_block_size Then
N_Sect_blocks_to_read:=Max_N_Sect_block_size
Else
N_Sect_blocks_to_read:=j;
Total_bytes_read:=N_Sect_blocks_to_read*Data_1block_size;
stop_looping:=False;
Repeat
s:=IntToStr(i) +
'..' +
IntToStr(i+N_Sect_blocks_to_read-1) +
',' +
IntToStr(N_Sect_blocks_to_read);
If Not Terminated Then
Begin
Form1.Form4.CDToImageProgressForm.Sect_reading_disp_Lbl.Caption:=s;
End;
Form1.SCSI.MMC1_any_link.Do_sense10_CD_cap_mech_st_out(Out_CD_cap_mech_st);
If Form1.SCSI.SPC_any_link.Get_is_sendcmd_OK Then
Begin
s:=IntToStr(Out_CD_cap_mech_st.CurrReadSpeed)+
' ('+
IntToStr(Trunc(SimpleRoundTo(Out_CD_cap_mech_st.CurrReadSpeed / C_1X_KBYTES_CDSPEED, 0)))+
'X)';
If Not Terminated Then
Begin
Form1.Form4.CDToImageProgressForm.LBL_curr_read_speed.Caption:=s;
End;
End;
Form1.SCSI.SPC_any_link.Zero_data_buf;
//Debug remove later please:
FillMemory(Form1.SCSI.SPC_any_link.Get_data_buf, N_Sect_blocks_to_read*Data_1block_size, $FF);
{
//Convert positive LBA sector to MMCLBA (MMC style LBA) sector
If i>=404850 Then
HSA_sector:=i-450000
Else
HSA_sector:=i;
}
HSA_sector:=i;
Form1.SCSI.MMC1_any_link.Do_readCD_byFormat_CDB12(HSA_sector,
N_Sect_blocks_to_read,
FilterMode,
MMC_READCD_SYNC_ALLHDR_USERDATA_EDCECC Or C2_error_field,
SubChSelMode);
If Form1.SCSI.SPC_any_link.Get_is_sendcmd_OK OR
(is_TEB AND
((Form1.SCSI.SPC_any_link.Get_sense_key=SEN_KEY_MEDIUM_ERR) OR
(Form1.SCSI.SPC_any_link.Get_sense_key=SEN_KEY_RECV_ERR))) Then
Begin
If in_subch_file=nil Then
Begin
If f_toc=nil Then
Begin
If C2_error_field=MMC_READCD_C2_NONE Then
Begin
//- .DAO file only.
If is_deint_subs Then
Begin
k:=0;
buf_offset:=Form1.SCSI.SPC_any_link.Get_data_buf;
Repeat
//Go to the start of de-interlaced subch data
Inc(Ptr_byte(buf_offset), Sector_block_size);
//<><><> Start of de-interlacing subchannel data <><><>
//Copy interlaced subch data from dynamic buffer to a variable
sub_int_PW_96:=T_sub_int_PW_96(buf_offset^);
//De-interlace the subch data from variable, leaving result in dynamic buffer
Form1.CommonCDSettings.Deint_subs(@sub_int_PW_96, buf_offset);
//Go to the start of next sector
Inc(Ptr_byte(buf_offset), SubCh_block_size);
//<><><> End of de-interlacing subchannel data <><><>
k:=k+1;
Until(k=N_Sect_blocks_to_read);
End;
in_main_file.WriteBuffer(Form1.SCSI.SPC_any_link.Get_data_buf^, N_Sect_blocks_to_read*Data_1block_size);
End
Else
Begin
//- .DAO file.
//- .C2/.C2+ error reporting file.
If is_deint_subs Then
Begin
k:=0;
buf_offset:=Form1.SCSI.SPC_any_link.Get_data_buf;
Repeat
//Start of the sector data.
buf_offset2:=buf_offset;
//Write out the main sector of 2352 bytes.
in_main_file.WriteBuffer(buf_offset^, Sector_block_size);
//Go to the end of main sector data.
Inc(Ptr_byte(buf_offset), Sector_block_size);
//If main+c2+sub order.
If C2_read_order=0 Then
Begin
//Write out the 294 or 296 bytes of C2 error pointer bits/block error bits.
in_C2_file.WriteBuffer(buf_offset^, C2_block_size);
//Go to the end of C2 error data.
Inc(Ptr_byte(buf_offset), C2_block_size);
End;
//<><><> Start of de-interlacing subchannel data <><><>
//Copy interlaced sub-ch data from dynamic buffer to a variable
sub_int_PW_96:=T_sub_int_PW_96(buf_offset^);
//De-interlace the subch data from variable, leaving result in dynamic buffer
Form1.CommonCDSettings.Deint_subs(@sub_int_PW_96, buf_offset);
//Write out the sub-ch code of 96 bytes.
in_main_file.WriteBuffer(buf_offset^, SubCh_block_size);
//Go to the end of sub-ch data.
Inc(Ptr_byte(buf_offset), SubCh_block_size);
//<><><> End of de-interlacing subchannel data <><><>
//If main+sub+c2 order.
If C2_read_order=1 Then
Begin
//Write out the 294 or 296 bytes of C2 error pointer bits/block error bits.
in_C2_file.WriteBuffer(buf_offset^, C2_block_size);
//Go to the end of C2 error data.
Inc(Ptr_byte(buf_offset), C2_block_size);
End;
k:=k+1;
Until(k=N_Sect_blocks_to_read);
End
Else
Begin
k:=0;
buf_offset:=Form1.SCSI.SPC_any_link.Get_data_buf;
Repeat
//Start of the sector data.
buf_offset2:=buf_offset;
//Write out the main sector of 2352 bytes.
in_main_file.WriteBuffer(buf_offset^, Sector_block_size);
//Go to the end of main sector data.
Inc(Ptr_byte(buf_offset), Sector_block_size);
//If main+c2+sub order.
If C2_read_order=0 Then
Begin
//Write out the 294 or 296 bytes of C2 error pointer bits/block error bits.
in_C2_file.WriteBuffer(buf_offset^, C2_block_size);
//Go to the start of next sector
Inc(Ptr_byte(buf_offset), C2_block_size);
End;
//Write out the subch code of 96 bytes.
in_main_file.WriteBuffer(buf_offset^, SubCh_block_size);
//Go to the end of sub-ch data.
Inc(Ptr_byte(buf_offset), SubCh_block_size);
//If main+sub+c2 order.
If C2_read_order=1 Then
Begin
//Write out the 294 or 296 bytes of C2 error pointer bits/block error bits.
in_C2_file.WriteBuffer(buf_offset^, C2_block_size);
//Go to the start of next sector
Inc(Ptr_byte(buf_offset), C2_block_size);
End;
k:=k+1;
Until(k=N_Sect_blocks_to_read);
End;
End;
End
Else
Begin
If C2_error_field=MMC_READCD_C2_NONE Then
Begin
//- .IMG file only.
in_main_file.WriteBuffer(Form1.SCSI.SPC_any_link.Get_data_buf^, N_Sect_blocks_to_read*Sector_block_size);
End
Else
Begin
//- .IMG file only.
//- .C2/.C2+ error reporting file.
k:=0;
buf_offset:=Form1.SCSI.SPC_any_link.Get_data_buf;
Repeat
//Write out the main sector of 2352 bytes.
in_main_file.WriteBuffer(buf_offset^, Sector_block_size);
//Go to the start of C2 data.
Inc(Ptr_byte(buf_offset), Sector_block_size);
//Write out the 294 or 296 bytes of C2 error pointer bits/block error bits.
in_C2_file.WriteBuffer(buf_offset^, C2_block_size);
//Go to the start of next sector
Inc(Ptr_byte(buf_offset), C2_block_size);
k:=k+1;
Until(k=N_Sect_blocks_to_read);
End;
End;
End
Else
Begin
If C2_error_field=MMC_READCD_C2_NONE Then
Begin
//- .IMG file.
//- .SUB file.
k:=0;
buf_offset:=Form1.SCSI.SPC_any_link.Get_data_buf;
Repeat
//Write out the main sector data.
in_main_file.WriteBuffer(buf_offset^, Sector_block_size);
//Go to the start of de-interlaced subch data
Inc(Ptr_byte(buf_offset), Sector_block_size);
//<><><> Start of de-interlacing subchannel data <><><>
If is_deint_subs Then
Begin
//De-interlace the subch data from dynamic buffer, leaving result in variable
Form1.CommonCDSettings.Deint_subs(buf_offset, @subch_deint_PW_96);
SubCh_buf:=@subch_deint_PW_96;
End
Else
Begin
SubCh_buf:=buf_offset;
End;
//Write out the subch data.
in_subch_file.WriteBuffer(SubCh_buf^, SubCh_block_size);
//Go to the end of subch data.
Inc(Ptr_byte(buf_offset), SubCh_block_size);
//<><><> End of de-interlacing subchannel data <><><>
k:=k+1;
Until(k=N_Sect_blocks_to_read);
End
Else
Begin
//- .IMG file.
//- .SUB file.
//- .C2/.C2+ error reporting file.
k:=0;
buf_offset:=Form1.SCSI.SPC_any_link.Get_data_buf;
Repeat
//Write out main sector.
in_main_file.WriteBuffer(buf_offset^, Sector_block_size);
//Go to the end of main sector.
Inc(Ptr_byte(buf_offset), Sector_block_size);
//If main+c2+sub order.
If C2_read_order=0 Then
Begin
//Write out the 294 or 296 bytes of C2 error pointer bits/block error bits.
in_C2_file.WriteBuffer(buf_offset^, C2_block_size);
//Go to the end of C2 error data.
Inc(Ptr_byte(buf_offset), C2_block_size);
End;
//<><><> Start of de-interlacing subchannel data <><><>
If is_deint_subs Then
Begin
//De-interlace the subch data from dynamic buffer, leaving result in variable
Form1.CommonCDSettings.Deint_subs(buf_offset, @subch_deint_PW_96);
SubCh_buf:=@subch_deint_PW_96;
End
Else
Begin
SubCh_buf:=buf_offset;
End;
//Write out subs.
in_subch_file.WriteBuffer(SubCh_buf^, SubCh_block_size);
//Go to the end of sub-ch data.
Inc(Ptr_byte(buf_offset), SubCh_block_size);
//<><><> End of de-interlacing subchannel data <><><>
//If main+sub+c2 order.
If C2_read_order=1 Then
Begin
//Write out the 294 or 296 bytes of C2 error pointer bits/block error bits.
in_C2_file.WriteBuffer(buf_offset^, C2_block_size);
//Go to the end of C2 error data.
Inc(Ptr_byte(buf_offset), C2_block_size);
End;
k:=k+1;
Until(k=N_Sect_blocks_to_read);
End;
End;
End
Else
Begin
show_err_msg:='Error while processing ReadCD command.' + Chr(10) + Chr(13);
show_err_msg:=show_err_msg + Form1.SCSI.MMC1_any_link.Get_err_msg;
Synchronize(ShowErrMsg);
stop_looping:=True;
End;
i:=i+N_Sect_blocks_to_read;
j:=j-N_Sect_blocks_to_read;
If j<N_Sect_blocks_to_read Then N_Sect_blocks_to_read:=j;
Total_bytes_read:=N_Sect_blocks_to_read*Data_1block_size;
Until (j=0) OR (stop_looping) OR Terminated;
curr_abs_LBA_to_write:=i;
LeadOutSect:=i;
End;
procedure TCDToImageThread.FillA0SubChEntry(First_trkNo : Byte;
Ctrl : Byte);
var
First_trk_MSF : T_MSF;
Track_format : Byte;
begin
subch_deint_PW_96.PW[2][3]:=$A0;
subch_deint_PW_96.PW[2][8]:=First_trkNo; //PMin
//Check the Ctrl to see if it's a data (CDROM) or an audio (CDDA) CD.
If (Ctrl And
MMC_READ_SUBCH_CTRL_MASK_DATA)=MMC_READ_SUBCH_CTRL_DATA Then
Begin
//Disk is a data (CDROM) CD..
//First trk start location is always 00:02:00.
First_trk_MSF.M:=0;
First_trk_MSF.S:=2;
First_trk_MSF.F:=0;
//Check the exact format of the first track.
Track_format:=Scan_format_of_data_track_from_CD(First_trk_MSF);
Case Track_format Of
CDROM_SECTORTYPE_ANY:
subch_deint_PW_96.PW[2][9]:=0;
CDROM_SECTORTYPE_MODE1:
subch_deint_PW_96.PW[2][9]:=0;
CDROM_SECTORTYPE_MODE2FORMLESS:
subch_deint_PW_96.PW[2][9]:=$20;
CDROM_SECTORTYPE_MODE2FORM1:
subch_deint_PW_96.PW[2][9]:=$20;
CDROM_SECTORTYPE_MODE2FORM2:
subch_deint_PW_96.PW[2][9]:=$20;
CDROM_SECTORTYPE_MODE0:
subch_deint_PW_96.PW[2][9]:=0;
CDROM_SECTORTYPE_UNKNOWN:
subch_deint_PW_96.PW[2][9]:=0;
End;
End
Else
Begin
//Disk is an audio (CDDA) CD..
subch_deint_PW_96.PW[2][9]:=0;
End;
end;
procedure TCDToImageThread.AddA0TOCEntry(First_trkNo : Byte;
Ctrl : Byte);
var
CRC16_val : Word;
begin
ZeroMemory(@subch_deint_PW_96, 96);
AddA0TOCEntry(First_trkNo, Ctrl);
//Calc CRC (X25 standard CRC 16). The 2 bytes of CRC for Q sub-channel data.
CRC16_val:=Calc_CRC16(@(subch_deint_PW_96.PW[2][1]), 10);
subch_deint_PW_96.PW[2][11]:=NOT (CRC16_val SHR 8);
subch_deint_PW_96.PW[2][12]:=NOT CRC16_val;
TOC.Add_entry(subch_deint_PW_96);
end;
procedure TCDToImageThread.AddA1TOCEntry(Last_trkNo : Byte);
var
CRC16_val : Word;
begin
ZeroMemory(@subch_deint_PW_96, 96);
subch_deint_PW_96.PW[2][3]:=$A1;
subch_deint_PW_96.PW[2][8]:=Last_trkNo; //PMin
//Calc CRC (X25 standard CRC 16). The 2 bytes of CRC for Q sub-channel data.
CRC16_val:=Calc_CRC16(@(subch_deint_PW_96.PW[2][1]), 10);
subch_deint_PW_96.PW[2][11]:=NOT (CRC16_val SHR 8);
subch_deint_PW_96.PW[2][12]:=NOT CRC16_val;
TOC.Add_entry(subch_deint_PW_96);
end;
function TCDToImageThread.Read1TOCEntryFromCD(Read_first : Boolean;
Var First_trkNo : Byte;
Var Last_trkNo : Byte;
Var Ctrl : Byte) : Boolean;
Var
TOC_data : T_out_read_T_P_A_TOC_desc_MSF;
TOC_entry_exists : Boolean;
CRC16_val : Word;
Begin
If Read_first Then
Begin
TOC_entry_exists:=
Form1.SCSI.MMC1_any_link.Do_read_T_P_A_TOC_MSF_first_out_CDB10(First_trkNo,
Last_trkNo,
TOC_data,
0);
Ctrl:=TOC_data.ADR_Ctrl And $0F;
ZeroMemory(@subch_deint_PW_96, 96);
End
Else
Begin
TOC_entry_exists:=
Form1.SCSI.MMC1_any_link.Do_read_T_P_A_TOC_MSF_next_out_CDB10(TOC_data);
End;
If TOC_entry_exists Then
Begin
//Place Ctrl at upper 4 bits
subch_deint_PW_96.PW[2][1]:=TOC_data.ADR_Ctrl SHL 4;
//Place ADR at lower 4 bits
subch_deint_PW_96.PW[2][1]:=subch_deint_PW_96.PW[2][1] OR (TOC_data.ADR_Ctrl SHR 4);
//subch_deint_PW_96.PW[2][2]:=0; //TNO
Case TOC_data.TrackNo Of
1..99:
Begin
subch_deint_PW_96.PW[2][3]:=TOC_data.TrackNo;
subch_deint_PW_96.PW[2][8]:=TOC_data.StartM; //PMin
subch_deint_PW_96.PW[2][9]:=TOC_data.StartS; //PSec
subch_deint_PW_96.PW[2][10]:=TOC_data.StartF; //PFrame
End;
$A0:
Begin
FillA0SubChEntry(First_trkNo, TOC_data.ADR_Ctrl And $0F);
End;
$A1:
Begin
subch_deint_PW_96.PW[2][3]:=$A1;
subch_deint_PW_96.PW[2][8]:=Last_trkNo; //PMin
End;
$AA:
Begin
subch_deint_PW_96.PW[2][3]:=$A2;
subch_deint_PW_96.PW[2][8]:=TOC_data.StartM; //PMin
subch_deint_PW_96.PW[2][9]:=TOC_data.StartS; //PSec
subch_deint_PW_96.PW[2][10]:=TOC_data.StartF; //PFrame
End;
Else
Begin
subch_deint_PW_96.PW[2][3]:=TOC_data.TrackNo;
End;
End;
//Calc CRC (X25 standard CRC 16). The 2 bytes of CRC for Q sub-channel data.
CRC16_val:=Calc_CRC16(@(subch_deint_PW_96.PW[2][1]), 10);
subch_deint_PW_96.PW[2][11]:=NOT (CRC16_val SHR 8);
subch_deint_PW_96.PW[2][12]:=NOT CRC16_val;
TOC.Add_entry(subch_deint_PW_96);
End;
Read1TOCEntryFromCD:=TOC_entry_exists;
End;
procedure TCDToImageThread.ReadTOCToMem;
Var First_trkNo : Byte;
Last_trkNo : Byte;
Ctrl : Byte;
Begin
Read1TOCEntryFromCD(True, First_trkNo, Last_trkNo, Ctrl);
Repeat
Until (Read1TOCEntryFromCD(False, First_trkNo, Last_trkNo, Ctrl)=False);
If Not TOC.Find_mode_point_entry(1, $A0) Then
Begin
AddA0TOCEntry(First_trkNo, Ctrl);
End;
If Not TOC.Find_mode_point_entry(1, $A1) Then
Begin
AddA1TOCEntry(Last_trkNo);
End;
End;
procedure TCDToImageThread.ReadTOCToImage;
var
First_trkNo : Byte;
Last_trkNo : Byte;
Ctrl : Byte;
Begin
Read1TOCEntryFromCD(True, First_trkNo, Last_trkNo, Ctrl);
Repeat
If is_deint_subs Then
Begin
//Write to TOC file
f_toc.WriteBuffer(subch_deint_PW_96, 96);
End
Else
Begin
Form1.CommonCDSettings.Int_subs(@subch_deint_PW_96, @sub_int_PW_96);
//Write to TOC file
f_toc.WriteBuffer(sub_int_PW_96, 96);
End;
Until Read1TOCEntryFromCD(False, First_trkNo, Last_trkNo, Ctrl)=False;
//Check if ADR(Q mode)=1 and point=$A0 was returned by ReadTOC command.
If Not TOC.Find_mode_point_entry(1, $A0) Then
Begin
//Not returned, so generate our own.
AddA0TOCEntry(First_trkNo, Ctrl);
If is_deint_subs Then
Begin
//Write to TOC file
f_toc.WriteBuffer(subch_deint_PW_96, 96);
End
Else
Begin
Form1.CommonCDSettings.Int_subs(@subch_deint_PW_96, @sub_int_PW_96);
//Write to TOC file
f_toc.WriteBuffer(sub_int_PW_96, 96);
End;
End;
//Check if ADR(Q mode)=1 and point=$A1 was returned by ReadTOC command.
If Not TOC.Find_mode_point_entry(1, $A1) Then
Begin
//Not returned, so generate our own.
AddA1TOCEntry(Last_trkNo);
If is_deint_subs Then
Begin
//Write to TOC file
f_toc.WriteBuffer(subch_deint_PW_96, 96);
End
Else
Begin
Form1.CommonCDSettings.Int_subs(@subch_deint_PW_96, @sub_int_PW_96);
//Write to TOC file
f_toc.WriteBuffer(sub_int_PW_96, 96);
End;
End;
End;
function TCDToImageThread.Read1FullTOCEntryFromCD(Read_first : Boolean) : Boolean;
Var
First_sessNo : Byte;
Last_sessNo : Byte;
TOC_data : T_out_read_T_P_A_full_TOC_desc;
TOC_entry_exists : Boolean;
CRC16_val : Word;
Begin
If Read_first Then
Begin
TOC_entry_exists:=
Form1.SCSI.MMC1_any_link.Do_read_T_P_A_full_TOC_first_out_CDB10(First_sessNo,
Last_sessNo,
TOC_data,
0);
ZeroMemory(@subch_deint_PW_96, 96);
End
Else
Begin
TOC_entry_exists:=
Form1.SCSI.MMC1_any_link.Do_read_T_P_A_full_TOC_next_out_CDB10(TOC_data);
End;
If TOC_entry_exists Then
Begin
subch_deint_PW_96.PW[2][1]:=TOC_data.ADR_Ctrl SHL 4;
subch_deint_PW_96.PW[2][1]:=subch_deint_PW_96.PW[2][1] OR (TOC_data.ADR_Ctrl SHR 4);
subch_deint_PW_96.PW[2][2]:=TOC_data.TNO;
subch_deint_PW_96.PW[2][3]:=TOC_data.POINT;
subch_deint_PW_96.PW[2][4]:=TOC_data.Min;
subch_deint_PW_96.PW[2][5]:=TOC_data.Sec;
subch_deint_PW_96.PW[2][6]:=TOC_data.Frame;
subch_deint_PW_96.PW[2][7]:=TOC_data.Zero;
subch_deint_PW_96.PW[2][8]:=TOC_data.PMin;
subch_deint_PW_96.PW[2][9]:=TOC_data.PSec;
subch_deint_PW_96.PW[2][10]:=TOC_data.PFrame;
//According to MMC1, bytes 2 to 10 are converted into hex by device for values 0 to 99bcd
//encountered on the media. This means that some drives will return hex values, but
//we want to always deal with decimal values. Below is a conversion from hex into decimal.
//Treat any values between 10 to 99 as hex representation, converting hex to decimal (base 10)
{For i:=2 To 10 Do
Begin
If (subch_deint_PW_96.PW[2][i]>=10) AND (subch_deint_PW_96.PW[2][i]<=99) Then
Begin
subch_deint_PW_96.PW[2][i]:=StrToInt('$'+IntToStr(subch_deint_PW_96.PW[2][i]));
End;
End;}
//Calc CRC (X25 standard CRC 16). The 2 bytes of CRC for Q sub-channel data.
CRC16_val:=Calc_CRC16(@(subch_deint_PW_96.PW[2][1]), 10);
subch_deint_PW_96.PW[2][11]:=NOT (CRC16_val SHR 8);
subch_deint_PW_96.PW[2][12]:=NOT CRC16_val;
TOC.Add_entry(subch_deint_PW_96);
End;
Read1FullTOCEntryFromCD:=TOC_entry_exists;
End;
procedure TCDToImageThread.ReadFullTOCToMem;
Begin
Read1FullTOCEntryFromCD(True);
Repeat
Until (Read1FullTOCEntryFromCD(False)=False);
End;
procedure TCDToImageThread.ReadFullTOCToImage;
Begin
Read1FullTOCEntryFromCD(True);
Repeat
If is_deint_subs Then
Begin
//Write to TOC file
f_toc.WriteBuffer(subch_deint_PW_96, 96);
End
Else
Begin
Form1.CommonCDSettings.Int_subs(@subch_deint_PW_96, @sub_int_PW_96);
//Write to TOC file
f_toc.WriteBuffer(sub_int_PW_96, 96);
End;
Until Read1FullTOCEntryFromCD(False)=False;
End;
Procedure TCDToImageThread.Gen_TOC_image(in_toc_file : TFileStream);
Var
Sector_block_size : Word;
SubCh_block_size : Byte;
Max_N_Sect_block_size : LongWord;
Data_1block_size : Word;
N_Sect_blocks_to_write : LongWord;
Total_bytes_written : LongWord;
CRC16_val : Word;
i : Word;
j : LongWord;
stop_looping : Boolean;
//subch_deint_PW_96 : T_subch_deint_PW_96;
SubCh_buf : Pointer;
Lead_in_MSF_time : T_MSF;
Sector_MSF_time : T_MSF;
Sync_header : T_array16_byte;
TOC_curr_index : Word;
TOC_mod3 : Byte;
p : ^Byte;
out_TOC_entry : T_TOC_entry;
is_data_track : Boolean;
s2 : String;
out_track_entry : T_track_entry;
data_mode : Byte;
s : String;
Begin
Sector_block_size:=2352;
SubCh_block_size:=96;
Data_1block_size:=SubCh_block_size+Sector_block_size;
Max_N_Sect_block_size:=Form1.SCSI.SPC_any_link.Get_data_buf_size DIV Data_1block_size;
curr_abs_LBA_to_write:=-lead_in_size-150;
N_Sect_blocks_to_write:=1;
j:=Lead_in_size;
{Lead_in_MSF_time.M:=0;
Lead_in_MSF_time.S:=0;
Lead_in_MSF_time.F:=0;}
Form1.SCSI.MMC1_any_link.Get_MMC1.LBA_to_MSF(curr_abs_LBA_to_write,
Lead_in_MSF_time.M,
Lead_in_MSF_time.S,
Lead_in_MSF_time.F);
Form1.SCSI.SPC_any_link.Zero_data_buf;
{Sector_MSF_time.M:=0;
Sector_MSF_time.S:=0;
Sector_MSF_time.F:=0;}
Form1.SCSI.MMC1_any_link.Get_MMC1.LBA_to_MSF(curr_abs_LBA_to_write,
Sector_MSF_time.M,
Sector_MSF_time.S,
Sector_MSF_time.F);
Case lead_in_type Of
U_TRACK_MODE_AUDIO:
Begin
is_data_track:=False;
End;
U_TRACK_MODE_CDROM:
Begin
is_data_track:=True;
End;
U_TRACK_MODE_USE_TOC:
Begin
If TOC.Find_mode_point_entry(out_TOC_entry, 1, 1) Then
Begin
Case (T_TOC_data_PQ((@out_TOC_entry)^).Q.Control_ADR AND $D0) Of
$00,$10,$80,$90: is_data_track:=False;
$40,$50: is_data_track:=True;
Else
is_data_track:=False;
End;
End;
End;
End;
TOC_curr_index:=0;
If is_data_track Then
Begin
If tracks.Find_track_no_entry_by_track_no(out_track_entry, 1) Then
Begin
//Fill_sync_block(Form1.SCSI.SPC_any_link.Get_data_buf, CD_mode_to_data_mode(out_track_entry.CD_mode), True, Sector_MSF_time);
Sync_header[1]:=0;
For i:=2 To 11 Do
Begin