-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageToCDReadThreadUnit.pas
More file actions
2952 lines (2634 loc) · 122 KB
/
Copy pathImageToCDReadThreadUnit.pas
File metadata and controls
2952 lines (2634 loc) · 122 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
{ ***************************************************************************
A thread for reading data from a CD image, transferring into a cyclic write
buffer and preparing them for MMC writing mode, raw DA0 2448 bytes/sector
(write type 3).
*************************************************************************** }
unit ImageToCDReadThreadUnit;
interface
{ Uncomment below for debug mode.
The CD sector data written to a file:
OUTPUT.BIN. }
//{$DEFINE DEBUG} //Debug mode.
{ Uncomment for debug mode options. These will affect the CD write thread debug
mode file W_OUTPUT.BIN. }
//{$DEFINE DEBUG_W_OUTPUT_DEINT_SUBS} //Deinterlace sub-channel data.
//{$DEFINE DEBUG_W_OUTPUT_UNSCRAMBLED} //If CDROM format, leave main data unscrambled.
uses
Windows,
Classes,
SysUtils,
Math,
MMC1Unit,
SPC_Unit,
CommonCDSettingsUnit,
CRC_Unit,
CDROMTableSectorScramblerUnit,
TOCUnit,
EDC_ECC_Unit,
Tracks_Unit,
CDROM_struct_Unit,
ImageToCD_DispStatus_Thread_Unit,
ImageToCDWriteThreadUnit,
Tools_Unit;
{ My own constants for selecting lead-in data format type. }
const
U_TRACK_MODE_AUDIO=0;
U_TRACK_MODE_DATA=1;
U_TRACK_MODE_USE_TOC=2;
type Ptr_text_file=^TextFile;
type T_array16_byte=array[1..16] of Byte;
T_P_byte=^Byte;
type T_SSP_info=Record
SSP_start : LongInt;
SSP_len : LongWord;
End;
type T_SSP_write_info=Record
start : LongInt;
len : LongWord;
End;
type
TImageToCDReadThread = class(TThread)
private
{ Private declarations }
procedure ShowErrMsg;
procedure ShowConfirmToMsg;
public
confirm_from_msg : Boolean;
start_MMCLBA : LongInt;
write_speed : Word;
lead_in_file_type : Byte;
f_leadin_TOC : TFileStream;
f_data_area : TFileStream;
f_data_area_subch : TFileStream;
f_pregap1 : TFileStream;
f_LO : TFileStream;
SSP_file : Ptr_text_file;
SSP_method : Byte;
SSP_info : T_SSP_info;
SSP_list : TList;
SSP_write_list : TList;
total_n_sectors : Cardinal;
total_n_sectors_remaining : Cardinal;
{$IFDEF DEBUG}
debugf : TFileStream;
{$ENDIF}
MMCLBA_for_writing : LongInt;
LBA_for_writing : LongInt;
correct_LBA_for_writing : LongInt;
skipped_LBA_for_writing : LongInt;
subch_deint_PW_96 : T_subch_deint_PW_96;
s : String;
Out_ATIP_desc : T_out_read_T_P_A_ATIP_desc;
TOC : T_TOC;
tracks : T_tracks;
lead_in_TOC_len : LongInt;
pre_gap_len : LongInt;
post_gap_len : LongInt;
lead_out_len : LongInt;
lead_in_type : Byte;
Out_CD_cap_mech_st : T_pub_CD_cap_mech_st;
wait_for_ready_to_read : Boolean;
ImageToCD_DispStatus_Thread : T_ImageToCD_DispStatus_Thread;
ImageToCDWriteThread : TImageToCDWriteThread;
Constructor Create(Thread_done_proc : TNotifyEvent;
in_start_MMCLBA : Integer;
in_write_speed : Word;
in_lead_in_file_type : Byte;
in_lead_in_type : Byte;
in_f_leadin_TOC : TFileStream;
in_f_data_area : TFileStream;
in_f_data_area_subch : TFileStream;
in_f_pregap1 : TFileStream;
in_f_LO : TFileStream;
in_SSP_file : Pointer;
in_SSP_method : Byte);
procedure Free;
procedure Execute; override;
procedure GetNextSkipSect;
procedure DoWeSkipSect(write_param : T_P_write_param; in_curr_abs_LBA_to_write : Longint);
procedure Read_SSP_write_into_list;
function GetStartMMCLBAFromATIP : LongInt;
procedure Read_file_2448(file_to_read : TFileStream;
no_of_sectors_to_read : LongWord;
data_track_mode : Byte;
track_no : Byte;
in_s : String);
procedure Make_lead_in_from_TOC_file;
procedure Make_pre_gap;
procedure Read_file_2352_96;
procedure Make_post_gap;
procedure Make_lead_out;
end;
function Compare_SSP_list(Item1, Item2: Pointer): Integer;
implementation
Uses
MainFormUnit;
Constructor TImageToCDReadThread.Create(Thread_done_proc : TNotifyEvent;
in_start_MMCLBA : Integer;
in_write_speed : Word;
in_lead_in_file_type : Byte;
in_lead_in_type : Byte;
in_f_leadin_TOC : TFileStream;
in_f_data_area : TFileStream;
in_f_data_area_subch : TFileStream;
in_f_pregap1 : TFileStream;
in_f_LO : TFileStream;
in_SSP_file : Pointer;
in_SSP_method : Byte);
Var i : LongWord;
buf_offset : T_P_write_param;
Begin
start_MMCLBA:=in_start_MMCLBA;
write_speed:=in_write_speed;
lead_in_file_type:=in_lead_in_file_type;
lead_in_type:=in_lead_in_type;
f_leadin_TOC :=in_f_leadin_TOC;
f_data_area:=in_f_data_area;
f_data_area_subch:=in_f_data_area_subch;
f_pregap1:=in_f_pregap1;
f_LO:=in_f_LO;
SSP_method:=in_SSP_method;
wait_for_ready_to_read:=False;
{$IFDEF DEBUG}
debugf:=TFileStream.Create('OUTPUT.BIN', fmCreate);
{$ENDIF}
{ 1/7th of system RAM: 7*2448=17136. Maximum no. of sectors that is allowed to be buffered.
This is the amount of dynamic memory to use for the secondary cyclic buffer. }
Form1.CommonCDSettings.sector_buffer_block_len:=GetTotalPhysMemory Div 17136;
Form1.CommonCDSettings.current_start_loc_of_reading:=0; //Location of buffer for reading (sector unit).
Form1.CommonCDSettings.current_start_loc_of_writing:=0; //Location of buffer for writing (sector unit).
Form1.CommonCDSettings.max_no_of_blocks_to_read_synch:=428; //Must be >= 1 and should be <= no_of_blocks_to_wait_b4_reading.
If (in_SSP_method=1) Or
(in_SSP_method=2) Or
(in_SSP_method=3) Then
Begin
SSP_file:=in_SSP_file;
Form1.CommonCDSettings.max_no_of_blocks_to_write_synch:=1; //Must be 1 for skipping sectors.
End
Else
Begin
Form1.CommonCDSettings.max_no_of_blocks_to_write_synch:=
Form1.SCSI.SPC_any_link.Get_data_buf_size DIV 2448; //Must be >= 1.
End;
Form1.CommonCDSettings.no_of_blocks_to_wait_b4_reading:=428; //Must be >= 1 and should be < sector_buffer_block_len.
Form1.CommonCDSettings.reading_done:=False;
//Allocate memory for the secondary cyclic buffers.
GetMem(Form1.CommonCDSettings.P_write_buffer, Form1.CommonCDSettings.sector_buffer_block_len*2448);
GetMem(Form1.CommonCDSettings.P_write_param_buffer, Form1.CommonCDSettings.sector_buffer_block_len*SizeOf(T_write_param));
//Initialise to indicate that write buffer does not contain any writable data.
buf_offset:=Form1.CommonCDSettings.P_write_param_buffer;
For i:=1 To Form1.CommonCDSettings.sector_buffer_block_len Do
Begin
buf_offset^.is_do_sync_cache:=False;
buf_offset^.is_block_for_reading:=True;
Inc(buf_offset);
End;
OnTerminate:=Thread_done_proc;
//FreeOnTerminate := True; // Automatically destroy itself.
Inherited Create(False); //Call the original create method.
End;
procedure TImageToCDReadThread.Free;
begin
If ImageToCDWriteThread<>nil Then
Begin
ImageToCDWriteThread.Free;
End;
Inherited Free;
end;
procedure TImageToCDReadThread.Execute;
var
i : Integer;
t : T_track_entry;
begin
{ ** Sets the writing speed ** }
{Form1.SCSI.MMC1.Do_set_CD_speed(MMC_SET_CD_SPEED_MAX, write_speed);
Form1.SCSI.MMC1.Do_sense10_CD_cap_mech_st_out(Out_CD_cap_mech_st);
If Form1.SCSI.SCSI_interface.FA_SRB_status_OK Then
Begin
s:=IntToStr(Out_CD_cap_mech_st.CurrWriteSpeed)+
' ('+
IntToStr(Trunc(SimpleRoundTo(Out_CD_cap_mech_st.CurrWriteSpeed / C_1X_KBYTES_CDSPEED, 0)))+
'X)';
If Not Terminated Then
Begin
Form1.Form5.ImageToCDProgressForm.LBL_curr_write_speed.Caption:=s;
End;
End;}
SSP_write_list:=TList.Create;
If (SSP_method=1) Or
(SSP_method=2) Then
Begin
GetNextSkipSect
End;
If (SSP_method=3) then
Begin
Read_SSP_write_into_list;
End;
{ Reads the TOC in a lead-in file and represent it as a unique TOC
that is stored in a TOC object. }
TOC:=T_TOC.Create(f_leadin_TOC, 96, lead_in_file_type);
//The TOC object read the file, so we should seek back to 0.
f_leadin_TOC.Seek(0, soFromBeginning);
{ Uses the TOC object and main sector image file to determine the
individual track types and times. }
If lead_in_file_type=TOC_FILE_TYPE_2448_FULL_DAO_FILE Then
Begin
//Find the lead-in TOC length in the file.
lead_in_TOC_len:=Find_track_no_from_2448_file(f_leadin_TOC, 1);
//Find the 1st pre-gap length in the file.
pre_gap_len:=Find_p_value_from_2448_file(f_leadin_TOC, 0);
//The Find functions above seeked the file, so we should seek back to 0.
f_leadin_TOC.Seek(0, soFromBeginning);
//s:='Debug: CD image bin:' + IntToStr(leadin_TOC_len) + ' ' + IntToStr(pregap1_len);
//Synchronize(ShowErrMsg);
{ Use the TOC object & CD image file to find track information for all
tracks listed in the TOC object. }
tracks:=T_tracks.Create(f_leadin_TOC, 2448, lead_in_TOC_len, TOC);
//t:=tracks.Get_entry(0);
//s:='Debug: CDROM struct unit:' + IntToStr(t.First_MSF.M) + ' ' +
// IntToStr(t.First_MSF.S) + ' ' +
// IntToStr(t.First_MSF.F);
//s:='CDROM struct: '+IntToStr(tracks.Tracks.Count);
///Synchronize(ShowErrMsg);
End
Else
Begin
{ Use the TOC object & CD image file to find track information for all
tracks listed in the TOC object. }
tracks:=T_tracks.Create(f_data_area, 2352, 0, TOC);
//s:='CDROM struct: '+IntToStr(TOC.TOC_data.Count);
//Synchronize(ShowErrMsg);
End;
If (SSP_method=3) And (SSP_write_list.Count>0) Then
Begin
{ This section of if choice is to write in sections from outer sections
to inner sections of CD (i.e. write sections backwards). This will
allow sector positions to be skipped. This is asynchronous
writing. }
For i:=SSP_write_list.Count-1 To 0 Do
Begin
End;
{ Destroys all dynamic memory used by pointers in the list. }
For i:=0 To SSP_write_list.Count-1 Do
Begin
Dispose(SSP_write_list.Items[i]);
End;
End
Else
Begin
{ This section of if choice is to write normally. This is
synchronous writing. }
{ Running sector count that is used as the parameter for the MMC write
command. Takes into account of user specified starting sector and
sector skips (if any). This is in MMCLBA sector style, so it has a
negative range after 89:59:74. }
MMCLBA_for_writing:=start_MMCLBA;
{ Running sector count that is the same as MMCLBA, but does not
include the sector skips (if any). This is in normal LBA sector
style, so it has a positive range after 89:59:74. This is needed to
give correct addresses to be used for generation of sector data. }
LBA_for_writing:=start_MMCLBA;
{ Running sector count that is like a mirror of MMCLBA, except it is
in normal LBA sector style, so it has a positive range after
89:59:74. This is needed so that sectors can be easily counted, and
is intended for conversion to the MMCLBA_sector variable, which has
a negative range after 89:59:74. }
skipped_LBA_for_writing:=start_MMCLBA;
{ There are different lead-in or TOC file formats. }
Case lead_in_file_type Of
TOC_FILE_TYPE_2448_FULL_DAO_FILE:
Begin
//s:='Debug: CD image bin';
//Synchronize(ShowErrMsg);
//leadin_TOC_len:=Find_track_no_from_2448_file(f_leadin_TOC, 1);
//f_leadin_TOC.Seek(0, soFromBeginning);
confirm_from_msg:=true;
{ Check whether the user had specified a starting sector which gives
a lead-in length that is different from the length in the file.. }
If MMCLBA_for_writing>=-pre_gap_len Then
Begin
s:='Warning: starting sector will create a 0 lead-in TOC length,' + Chr(13) +
'so the lead-in TOC area on the disc will be unwritten (blank).' + Chr(13) +
'All sectors will be written shifted forward.' + Chr(13) +
'The lead-in TOC length in the file is: ' +
IntToStr(lead_in_TOC_len) + ' sectors.' + Chr(13) +
'Do you still want to continue writing?';
Synchronize(ShowConfirmToMsg);
End;
If confirm_from_msg Then
Begin
{ Running sector count that is the same as MMCLBA, but does
not include the user specified starting sector and the
sector skips (if any). This is in normal LBA sector style,
so it has a positive range after 89:59:74. It is needed to
give correct addresses to be used for generation of sector
data. }
correct_LBA_for_writing:=-(lead_in_TOC_len+150);
//Total no. of sectors to write to disc.
total_n_sectors:=f_leadin_TOC.Size DIV 2448;
//A decreasing counter for total no. of sectors remaining.
total_n_sectors_remaining:=total_n_sectors;
{ Create a thread to show the reading/writing progress. }
ImageToCD_DispStatus_Thread:=T_ImageToCD_DispStatus_Thread.Create;
{ Create a thread to do the writing to disc. Note that it
only uses the secondary cyclic buffer for reading, no CD
image files are being read here. }
ImageToCDWriteThread:=TImageToCDWriteThread.Create(write_speed,
total_n_sectors,
ImageToCD_DispStatus_Thread);
{ Read the CD image file and processes it into the secondary
cyclic buffer, for the thread above. }
If Not Terminated Then
Read_file_2448(f_leadin_TOC, total_n_sectors, lead_in_type, 0, '');
End;
End;
TOC_FILE_TYPE_2448_FULL_LI_FILE:
Begin
confirm_from_msg:=true;
{ Check whether the user had specified a starting sector which gives
a lead-in length that is different from the length in the file.. }
If MMCLBA_for_writing>=-pre_gap_len Then
Begin
s:='Warning: starting sector will create a 0 lead-in TOC length,' + Chr(13) +
'so the lead-in TOC area on the disc will be unwritten (blank).' + Chr(13) +
'All sectors will be written shifted forward.' + Chr(13) +
'The lead-in TOC length in the file is: ' +
IntToStr(f_leadin_TOC.Size DIV 2448) + ' sectors.' + Chr(13) +
'Do you still want to continue writing?';
Synchronize(ShowConfirmToMsg);
End;
If confirm_from_msg Then
Begin
//MMCLBA_sector:=-((f.Size+f_pregap1.Size) DIV 2448);
//curr_abs_LBA_to_write:=MMCLBA_sector;
pre_gap_len:=f_pregap1.Size DIV 2448;
post_gap_len:=150;
lead_in_TOC_len:=f_leadin_TOC.Size DIV 2448;
lead_out_len:=f_LO.Size DIV 2448;
//Total no. of sectors to write to disc.
total_n_sectors:=lead_in_TOC_len+
pre_gap_len+
(f_data_area.Size DIV 2352)+
lead_out_len;
//A decreasing counter for total no. of sectors remaining.
total_n_sectors_remaining:=total_n_sectors;
{ Running sector count that is the same as MMCLBA, but does
not include the user specified starting sector and the
sector skips (if any). This is in normal LBA sector style,
so it has a positive range after 89:59:74. It is needed to
give correct addresses to be used for generation of sector
data. }
correct_LBA_for_writing:=-(lead_in_TOC_len+150);
{ Create a thread to show the reading/writing progress. }
ImageToCD_DispStatus_Thread:=T_ImageToCD_DispStatus_Thread.Create;
{ Create a thread to do the writing to disc. Note that it
only uses the secondary cyclic buffer for reading, no CD
image files are being read here. }
ImageToCDWriteThread:=TImageToCDWriteThread.Create(write_speed,
total_n_sectors,
ImageToCD_DispStatus_Thread);
{ Read the CD image files and processes it into the secondary
cyclic buffer, for the thread above.. }
If Not Terminated Then
Read_file_2448(f_leadin_TOC, lead_in_TOC_len, lead_in_type, 0, 'Lead-in TOC: ');
If Not Terminated Then
Read_file_2448(f_pregap1, pre_gap_len, U_TRACK_MODE_USE_TOC, 1, 'Pre-gap: ');
If Not Terminated Then
Read_file_2352_96;
If Not Terminated Then
Read_file_2448(f_LO, lead_out_len, U_TRACK_MODE_USE_TOC, $A2, 'Lead-out: ');
End;
End;
TOC_FILE_TYPE_96_REPEATED_DEINT,
TOC_FILE_TYPE_96_UNIQUE_DEINT,
TOC_FILE_TYPE_96_UNIQUE_INT:
Begin
{ Running sector count that is the same as MMCLBA, but does
not include the user specified starting sector and the
sector skips (if any). This is in normal LBA sector style,
so it has a positive range after 89:59:74. It is needed to
give correct addresses to be used for generation of sector
data. }
correct_LBA_for_writing:=GetStartMMCLBAFromATIP;
pre_gap_len:=150;
post_gap_len:=150;
If lead_in_file_type=TOC_FILE_TYPE_96_REPEATED_DEINT Then
lead_in_TOC_len:=(f_leadin_TOC.Size DIV 96)
Else
lead_in_TOC_len:=Abs(correct_LBA_for_writing+150);
confirm_from_msg:=true;
{ Check whether the user has specified a starting sector which gives
a lead-in length that is different from the length in the file.. }
If MMCLBA_for_writing>=-pre_gap_len Then
Begin
s:='Warning: starting sector will create a 0 lead-in TOC length,' + Chr(13) +
'so the lead-in TOC area on the disc will be unwritten (blank).' + Chr(13) +
'All sectors will be written shifted forward.' + Chr(13) +
'The lead-in TOC length in the file is: ' +
IntToStr(lead_in_TOC_len) + ' sectors.' + Chr(13) +
'Do you still want to continue writing?';
Synchronize(ShowConfirmToMsg);
End;
If confirm_from_msg Then
Begin
lead_out_len:=6750;
//Total no. of sectors to write to disc.
total_n_sectors:=lead_in_TOC_len+
pre_gap_len+
(f_data_area.Size DIV 2352)+
lead_out_len;
//A decreasing counter for total no. of sectors remaining.
total_n_sectors_remaining:=total_n_sectors;
{ Create a thread to show the reading/writing progress. }
ImageToCD_DispStatus_Thread:=T_ImageToCD_DispStatus_Thread.Create;
{ Create a thread to do the writing to disc. Note that it
only uses the secondary cyclic buffer for reading, no CD
image files are being read here. }
ImageToCDWriteThread:=TImageToCDWriteThread.Create(write_speed,
total_n_sectors,
ImageToCD_DispStatus_Thread);
{ Read the CD image files and processes it into the secondary
cyclic buffer, for the thread above.. }
If Not Terminated Then
Make_lead_in_from_TOC_file;
//If Not Terminated Then
//Make_post_gap; //ECMA-130 describes a post gap - but this creates an unreadable CD.
If Not Terminated Then
Make_pre_gap; //Instead it must be a pre-gap after the TOC in lead-in.
If Not Terminated Then
Read_file_2352_96;
//If Not Terminated Then
//Make_post_gap; //Post gap is assumed to be included in main file.
If Not Terminated Then
Make_lead_out;
End;
End;
End;
End;
//The thread may not have been created, so check first.
If ImageToCDWriteThread<>nil Then
Begin
//Form already terminated the write thread.
//If Terminated Then
// ImageToCDWriteThread.Terminate;
ImageToCDWriteThread.WaitFor; //Wait for the write thread to finish.
End;
//The Write_remaining_cache function in the write thread already terminated
//this thread.
{If ImageToCD_DispStatus_Thread<>nil Then
Begin
ImageToCD_DispStatus_Thread.Terminate;
ImageToCD_DispStatus_Thread.WaitFor;
End;}
TOC.Free;
tracks.Free;
{$IFDEF DEBUG}
debugf.Free;
{$ENDIF}
//Frees the memory for the secondary cyclic buffer.
FreeMem(Form1.CommonCDSettings.P_write_buffer);
FreeMem(Form1.CommonCDSettings.P_write_param_buffer);
// Free the list object.
SSP_write_list.Free;
end;
Procedure TImageToCDReadThread.GetNextSkipSect;
{ Reads a line from .SSP file and saves the start and len in variables:
SSP_start and SSP_len. }
Var s : String;
i : Integer;
Begin
If EOF(SSP_file^) Then
Begin
SSP_info.SSP_len:=0;
End
Else
Begin
ReadLn(SSP_file^, s);
i:=Pos(',', s);
If i<>0 Then
Begin
SSP_info.SSP_start:=StrToInt(Copy(s, 1, i-1));
SSP_info.SSP_len:=StrToInt(Copy(s, i+1, Length(s)-i));
End
Else
Begin
SSP_info.SSP_len:=0;
End;
End;
End;
Procedure TImageToCDReadThread.DoWeSkipSect(write_param : T_P_write_param; in_curr_abs_LBA_to_write : Longint);
Begin
If (SSP_method=1) Or (SSP_method=2) Then
Begin
If SSP_info.SSP_len>0 Then
Begin
If in_curr_abs_LBA_to_write=SSP_info.SSP_start Then
Begin
skipped_LBA_for_writing:=skipped_LBA_for_writing+SSP_info.SSP_len;
write_param^.starting_MMCLBA_sector_to_write:=Form1.SCSI.MMC1_any_link.Get_MMC1.LBA_to_MMCLBA(skipped_LBA_for_writing);
GetNextSkipSect;
If SSP_method=2 Then
write_param^.is_do_sync_cache:=True;
End;
End;
End;
End;
procedure TImageToCDReadThread.Read_SSP_write_into_list;
var
Ptr_SSP_write_info : ^T_SSP_write_info;
s : String;
i : Integer;
Next_start : LongInt;
total_SSP_sector_writes : LongInt;
begin
Next_start:=-lead_in_TOC_len;
total_SSP_sector_writes:=0;
while (not EOF(SSP_file^)) do
begin
ReadLn(SSP_file^, s);
i:=Pos(',', s);
if i<>0 then
begin
New(Ptr_SSP_write_info);
SSP_info.SSP_start:=StrToInt(Copy(s, 1, i-1));
SSP_info.SSP_len:=StrToInt(Copy(s, i+1, Length(s)-i));
T_SSP_write_info(Ptr_SSP_write_info^).start:=Next_start;
T_SSP_write_info(Ptr_SSP_write_info^).len:=SSP_info.SSP_start-
Next_start;
Next_start:=SSP_info.SSP_start+SSP_info.SSP_len;
SSP_list.Add(Ptr_SSP_write_info);
total_SSP_sector_writes:=total_SSP_sector_writes+
T_SSP_write_info(Ptr_SSP_write_info^).len;
end
end;
if SSP_list.Count>0 then
begin
New(Ptr_SSP_write_info);
T_SSP_write_info(Ptr_SSP_write_info^).start:=Next_start;
T_SSP_write_info(Ptr_SSP_write_info^).len:=total_n_sectors-
total_SSP_sector_writes;
SSP_list.Add(Ptr_SSP_write_info);
end;
end;
Procedure TImageToCDReadThread.ShowErrMsg;
Begin
Form1.Form5.ImageToCDProgressForm.ShowErrMsg(s);
End;
Procedure TImageToCDReadThread.ShowConfirmToMsg;
Begin
confirm_from_msg:=Form1.Form5.ImageToCDProgressForm.ConfirmToMsg(s);
End;
Function TImageToCDReadThread.GetStartMMCLBAFromATIP : LongInt;
Begin
Form1.SCSI.MMC1_any_link.Do_read_T_P_A_ATIP_out_CDB10(Out_ATIP_desc);
If Form1.SCSI.SPC_any_link.Get_is_sendcmd_OK Then
Begin
GetStartMMCLBAFromATIP:=Form1.SCSI.MMC1_any_link.Get_MMC1.MSF_to_MMCLBA(Out_ATIP_desc.StartMin,
Out_ATIP_desc.StartSec,
Out_ATIP_desc.StartFrame);
End
Else
GetStartMMCLBAFromATIP:=0;
End;
procedure TImageToCDReadThread.Read_file_2448(file_to_read : TFileStream;
no_of_sectors_to_read : LongWord;
data_track_mode : Byte;
track_no : Byte;
in_s : String);
{ Reads a raw 2448 bytes/sector file and transfers it into the cyclic secondary
write buffer. Sub-channel data are interlaced and main data is scrambled if
necessary. }
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;
i, j : LongWord;
stop_looping : Boolean;
P_write_buffer_offset : Pointer;
buf_offset : T_P_write_param;
buf_offset2 : Pointer;
buf_offset_to_check : LongWord;
out_TOC_entry : T_TOC_entry;
is_data_track : Boolean;
next_track_LBA : LongInt;
i_track_no : Byte;
is_next_track_lead_out : Boolean;
P_write_param : ^T_write_param;
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;
j:=no_of_sectors_to_read;
i_track_no:=track_no;
is_next_track_lead_out:=False;
//Check if starting track no. is lead-in track (i.e. track no. 0)
If i_track_no=0 Then
Begin
Case data_track_mode Of
U_TRACK_MODE_AUDIO:
Begin
is_data_track:=False;
End;
U_TRACK_MODE_DATA:
Begin
is_data_track:=True;
End;
U_TRACK_MODE_USE_TOC:
Begin
//Use track 1 TOC entry to decide the lead-in track format, which then
//determines the use of scrambling sectors.
If TOC.Find_mode_point_entry(out_TOC_entry, 1, 1) Then
Begin
//Determine the CD data mode from the track TOC entry.
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
//Unknown CD data mode, so assume it's a CDDA track.
is_data_track:=False;
End;
End
Else
Begin
//Seems we have an invalid TOC, so we'll assume it's
//a CDDA track mode lead-in.
is_data_track:=False;
End;
End;
End;
End
Else
Begin
//Use the current track no. TOC entry to find out the track format.
If TOC.Find_mode_point_entry(out_TOC_entry, 1, i_track_no) Then
Begin
//Determine the CD data mode from the track TOC entry.
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
//Unknown CD data mode, so assume it's a CDDA track.
is_data_track:=False;
End;
End
Else
Begin
//Seems we have an invalid TOC, so we'll assume it's a CDDA track.
is_data_track:=False;
End;
End;
//Check if we are at lead-out
If i_track_no=$A2 Then
Begin
//Use 99:59:74 + 1 -> last possible position + 1.
next_track_LBA:=449850;
End
Else
Begin
{ Use the next track n TOC entry to find out the next track start LBA. }
If TOC.Find_mode_point_entry(out_TOC_entry, 1, i_track_no+1) Then
Begin
next_track_LBA:=Form1.SCSI.MMC1_any_link.Get_MMC1.MSF_to_LBA(T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_M,
T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_S,
T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_F);
//In the lead-in track, the TOC area does not include the 1st pregap.
If i_track_no=0 Then next_track_LBA:=next_track_LBA-150;
End
Else
Begin
//No next track n TOC entry, so find the lead-out track TOC entry.
is_next_track_lead_out:=True;
If TOC.Find_mode_point_entry(out_TOC_entry, 1, $A2) Then
Begin
next_track_LBA:=Form1.SCSI.MMC1_any_link.Get_MMC1.MSF_to_LBA(T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_M,
T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_S,
T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_F);
End
Else
Begin
//No lead-out track TOC entry, so use 99:59:74 + 1 -> last possible position + 1.
next_track_LBA:=449850;
End;
End;
End;
stop_looping:=False;
Repeat
If (Form1.CommonCDSettings.current_start_loc_of_reading+
Form1.CommonCDSettings.max_no_of_blocks_to_read_synch)<
Form1.CommonCDSettings.sector_buffer_block_len Then
Begin
N_Sect_blocks_to_write:=Form1.CommonCDSettings.max_no_of_blocks_to_read_synch;
If j<N_Sect_blocks_to_write Then N_Sect_blocks_to_write:=j;
If (Form1.CommonCDSettings.no_of_blocks_to_wait_b4_reading<j) And
(Form1.CommonCDSettings.no_of_blocks_to_wait_b4_reading>N_Sect_blocks_to_write) Then
Begin
If (Form1.CommonCDSettings.current_start_loc_of_reading+
Form1.CommonCDSettings.no_of_blocks_to_wait_b4_reading-1)<
Form1.CommonCDSettings.sector_buffer_block_len Then
Begin
buf_offset_to_check:=Form1.CommonCDSettings.current_start_loc_of_reading+
Form1.CommonCDSettings.no_of_blocks_to_wait_b4_reading-1;
End
Else
Begin
buf_offset_to_check:=Form1.CommonCDSettings.sector_buffer_block_len-1;
End;
End
Else
Begin
buf_offset_to_check:=Form1.CommonCDSettings.current_start_loc_of_reading+
N_Sect_blocks_to_write-1;
End;
End
Else
Begin
N_Sect_blocks_to_write:=Form1.CommonCDSettings.sector_buffer_block_len-
Form1.CommonCDSettings.current_start_loc_of_reading;
If j<N_Sect_blocks_to_write Then N_Sect_blocks_to_write:=j;
buf_offset_to_check:=Form1.CommonCDSettings.current_start_loc_of_reading+
N_Sect_blocks_to_write-1;
End;
P_write_buffer_offset:=Form1.CommonCDSettings.P_write_buffer;
Inc(T_P_byte(P_write_buffer_offset),
Form1.CommonCDSettings.current_start_loc_of_reading*Data_1block_size);
buf_offset:=Form1.CommonCDSettings.P_write_param_buffer;
Inc(buf_offset, buf_offset_to_check);
wait_for_ready_to_read:=True;
While((Not Terminated) And wait_for_ready_to_read) Do
Begin
If buf_offset^.is_block_for_reading Then
Begin
wait_for_ready_to_read:=False;
End
Else
Begin
Suspend;
End;
End;
If Not Terminated Then
Begin
s:=in_s+
IntToStr(LBA_for_writing) + '..' +
IntToStr(LBA_for_writing+N_Sect_blocks_to_write-1) + ',' +
IntToStr(N_Sect_blocks_to_write);
If is_data_track Then
s:=s+' (data track).'
Else
s:=s+' (audio track).';
Form1.Form5.ImageToCDProgressForm.LBL_read_sect.Caption:=s;
//Display_status;
//s:='Debug: ' + IntToStr(buf_offset_to_check);
//Synchronize(ShowErrMsg);
file_to_read.ReadBuffer(P_write_buffer_offset^, Data_1block_size*N_Sect_blocks_to_write);
{$IFDEF DEBUG}
debugf.WriteBuffer(P_write_buffer_offset^, Data_1block_size*N_Sect_blocks_to_write);
{$ENDIF}
i:=0;
buf_offset2:=P_write_buffer_offset;
Repeat
//<><><> Start of scrambling CDROM data sectors <><><>
If is_data_track Then
Begin
{$IFNDEF DEBUG_W_OUTPUT_UNSCRAMBLED}
Scramble(buf_offset2);
{$ENDIF}
End;
//Go to the start of de-interlaced subch data
Inc(T_P_byte(buf_offset2), Sector_block_size);
//<><><> End of scrambling CDROM data sectors <><><>
//<><><> Start of interlacing subchannel data <><><>
//Copy de-interlaced subch data from dynamic buffer to a variable
subch_deint_PW_96:=T_subch_deint_PW_96(buf_offset2^);
{$IFNDEF DEBUG_W_OUTPUT_DEINT_SUBS}
//Interlace the subch data from variable, leaving result in dynamic buffer
Form1.CommonCDSettings.Int_subs(@subch_deint_PW_96, buf_offset2);
{$ELSE}
//For debugging only, MMC write type 3 does not need this. Don't interlace sub-channel data.
T_subch_deint_PW_96(buf_offset2^):=subch_deint_PW_96;
{$ENDIF}
//Go to the start of next sector
Inc(T_P_byte(buf_offset2), SubCh_block_size);
//<><><> End of interlacing subchannel data <><><>
i:=i+1;
Until(i=N_Sect_blocks_to_write);
buf_offset2:=Form1.CommonCDSettings.P_write_param_buffer;
Inc(T_P_write_param(buf_offset2), Form1.CommonCDSettings.current_start_loc_of_reading);
//ImageToCDWriteThread.Suspend;
For i:=0 To N_Sect_blocks_to_write-1 Do
Begin
T_write_param(buf_offset2^).starting_MMCLBA_sector_to_write:=
Form1.SCSI.MMC1_any_link.Get_MMC1.LBA_to_MMCLBA(skipped_LBA_for_writing);
T_write_param(buf_offset2^).no_of_sectors_to_write:=
N_Sect_blocks_to_write-i;
//Do we skip sector position area?
DoWeSkipSect(buf_offset2, LBA_for_writing);
LBA_for_writing:=LBA_for_writing+1;
correct_LBA_for_writing:=correct_LBA_for_writing+1;
skipped_LBA_for_writing:=skipped_LBA_for_writing+1;
T_write_param(buf_offset2^).is_block_for_reading:=False;
Inc(T_P_write_param(buf_offset2), 1);
If LBA_for_writing=next_track_LBA Then
Begin
//Next track no.
If is_next_track_lead_out=True Then
Begin
i_track_no:=$A2;
End
Else
Begin
i_track_no:=i_track_no+1;
End;
//Use the current track no. TOC entry to find out the track format.
If TOC.Find_mode_point_entry(out_TOC_entry, 1, i_track_no) Then
Begin
//Determine the CD data mode from the track TOC entry.
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
//Unknown CD data mode, so assume it's a CDDA track.
is_data_track:=False;
End;
End
Else
Begin
//Seems we have an invalid TOC, so we'll assume it's a CDDA track.
is_data_track:=False;
End;
//Check if we are at the lead-out track.
If i_track_no=$A2 Then
Begin
//Use 99:59:74 + 1 -> last possible position + 1.
next_track_LBA:=449850;
End
Else
Begin
{ Use the next track n TOC entry to find out the next track start LBA. }
If TOC.Find_mode_point_entry(out_TOC_entry, 1, i_track_no+1) Then
Begin
next_track_LBA:=Form1.SCSI.MMC1_any_link.Get_MMC1.MSF_to_LBA(T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_M,
T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_S,
T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_F);
End
Else
Begin
//No next track n TOC entry, so find the lead-out track TOC entry.
is_next_track_lead_out:=True;
If TOC.Find_mode_point_entry(out_TOC_entry, 1, $A2) Then
Begin
next_track_LBA:=Form1.SCSI.MMC1_any_link.Get_MMC1.MSF_to_LBA(T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_M,
T_TOC_data_PQ((@out_TOC_entry)^).Q.Abs_or_Point_S,