-
Notifications
You must be signed in to change notification settings - Fork 1
/
tzxblock13.inc
208 lines (168 loc) · 4.5 KB
/
tzxblock13.inc
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
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$ifdef tzx_header_section}
{ TTzxBlock13 }
TTzxBlock13 = class (TTzxBlock)
strict private
FLen: Integer;
NumOfPulses: Integer; // pulses needed
PulsesLengths: array of Integer;
TicksNeeded: Int64;
CurrentPulseNumber: Integer;
FDetails: String;
FDetailsFilled: Boolean;
protected
function GetTicksNextEdge: Int64; override;
function IsReallyPlayableBlock: Boolean; override;
public
constructor Create(ATapePlayer: TTapePlayer); override;
class function GetBlockId: DWord; override;
{returns whole block length, without id byte}
function GetBlockLength: Integer; override;
function LoadBlock(const Stream: TStream): Boolean; override;
class function GetBlockDescription: String; override;
function GetNextPulse(): Boolean; override;
procedure Start; override;
procedure Details(out S: String); override;
end;
{$else}
function TTzxBlock13.GetTicksNextEdge: Int64;
begin
Result := TicksNeeded;
end;
function TTzxBlock13.IsReallyPlayableBlock: Boolean;
begin
Result := True;
end;
constructor TTzxBlock13.Create(ATapePlayer: TTapePlayer);
begin
inherited Create(ATapePlayer);
FDetailsFilled := False;
FLen := 0;
NumOfPulses := 0;
SetLength(PulsesLengths, 0);
end;
class function TTzxBlock13.GetBlockId: DWord;
begin
Result := $13;
end;
function TTzxBlock13.GetBlockLength: Integer;
begin
Result := FLen;
end;
function TTzxBlock13.LoadBlock(const Stream: TStream): Boolean;
var
I: Integer;
B: Byte;
W: Word;
begin
Result := False;
FDetailsFilled := False;
if Stream.Size > Stream.Position then begin
if Stream.Read(B{%H-}, 1) = 1 then begin
NumOfPulses := B;
FLen := NumOfPulses * 2 + 1;
Result := NumOfPulses = 0;
if not Result then begin
if Stream.Size - FLen >= Stream.Position - 1 then begin
SetLength(PulsesLengths, NumOfPulses);
I := 0;
while (I < NumOfPulses) and (Stream.Read(W{%H-}, 2) = 2) do begin
W := LEtoN(W);
PulsesLengths[I] := W;
Inc(I);
end;
Result := I = NumOfPulses;
end;
end;
end;
end;
end;
class function TTzxBlock13.GetBlockDescription: String;
begin
Result := 'Pulse sequence';
end;
function TTzxBlock13.GetNextPulse: Boolean;
var
ProcTicks: Int64;
TicksNeeded0: Int64;
begin
if State = TPlayState.psFinished then
Exit(False);
ProcTicks := GetCurrentTotalSpectrumTicks;
if ProcTicks >= TicksNeeded then begin
if State <> TPlayState.psStart then
FTapePlayer.ActiveBit := FTapePlayer.ActiveBit xor %01000000;
if CurrentPulseNumber < NumOfPulses then begin
TicksNeeded0 := PulsesLengths[CurrentPulseNumber];
AdjustTicksIfNeeded(TicksNeeded0);
TicksNeeded := ProcTicks + TicksNeeded0;
State := TPlayState.psPilot;
end else begin
State := TPlayState.psFinished;
end;
Inc(CurrentPulseNumber);
end;
Result := True;
end;
procedure TTzxBlock13.Start;
begin
inherited Start;
State := TPlayState.psStart;
TicksNeeded := TicksNeeded.MinValue;
CurrentPulseNumber := 0;
if NumOfPulses = 0 then
State := TPlayState.psFinished;
end;
procedure TTzxBlock13.Details(out S: String);
procedure FillDetails;
const
MaxLines = 5;
MaxPulsesInFirstLine = 8;
MaxPulsesInLine = 10;
var
I: Integer;
N: Integer;
K: Integer;
M: Integer;
S2: String;
begin
FDetails := 'Number of pulses: ' + NumOfPulses.ToString;
if NumOfPulses > 0 then begin
K := MaxPulsesInFirstLine;
M := MaxPulsesInLine;
if NumOfPulses > K + (MaxLines - 1) * M then begin
N := K + (MaxLines - 2) * M;
S2 := #13 + Format(' ... (and %d more)', [NumOfPulses - N]);
end else begin
S2 := '';
repeat
I := NumOfPulses + M - K;
N := I mod M;
if (N = 0) or (I div M >= M - N) then
Break;
Dec(K);
Dec(M);
until False;
N := NumOfPulses;
end;
FDetails := FDetails + #13 + 'Pulse lengths: ' + PulsesLengths[0].ToString;
for I := 1 to N - 1 do begin
Dec(K);
if K = 0 then begin
FDetails := FDetails + ',' + #13 + ' ';
K := M;
end else
FDetails := FDetails + ', ';
FDetails := FDetails + PulsesLengths[I].ToString;
end;
FDetails := FDetails + S2;
end;
FDetailsFilled := True;
end;
begin
if not FDetailsFilled then
FillDetails;
S := FDetails;
end;
{$endif}