-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtzxblock19.inc
492 lines (412 loc) · 10.9 KB
/
tzxblock19.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
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
// Copyright 2022-2025 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$ifdef tzx_header_section}
TTzxBlock19 = class (TTzxBlockStandard)
strict private
type
TSymdef = record
SymbolFlag: Byte;
PulseLengths: array of Integer;
Len: Integer;
end;
TSymdefs = array of TSymdef;
TPrle = packed record
SymbolToBeRepresented: Byte;
NumberOfRepetitions: Word;
end;
TPrles = array of TPrle;
strict private
PauseAfterBlock: Integer;
Totp: Integer;
Npp: Integer;
Asp: Integer;
Totd: Integer;
Npd: Integer;
Asd: Integer;
SymDefsPilot: TSymdefs;
SymDefsData: TSymdefs;
Prles: TPrles;
Prle: TPrle;
Data: PByte;
P, PEnd: PByte;
TicksNeeded: Int64;
PulsesNeeded: Integer;
BitPosition: Integer;
SymDef: TSymdef;
SymPos: Integer;
SymLen: Integer;
NB: Integer;
CurrentPrle: Integer;
Mask: Integer;
UnusedBitsInLastByte: Integer;
TicksNeeded0: Int64;
Start1: Boolean;
FDetails: RawByteString;
procedure FillDetails;
strict protected
function Load2(const Stream: TStream; L: Integer): Boolean; override;
protected
function IsReallyPlayableBlock: Boolean; override;
public
constructor Create(ATapePlayer: TTapePlayer); override;
destructor Destroy; override;
class function GetBlockId: DWord; override;
class function GetBlockDescription: String; override;
function GetNextPulse: Boolean; override;
procedure Start; override;
procedure Details(out S: String); override;
end;
{$else}
procedure TTzxBlock19.FillDetails;
begin
FDetails :=
'TOTP: ' + IntToStr(Totp) + #13
+ 'NPP: ' + IntToStr(Npp) + #13
+ 'ASP: ' + IntToStr(Asp) + #13
+ 'TOTD: ' + IntToStr(Totd) + #13
+ 'NPD: ' + IntToStr(Npd) + #13
+ 'ASD: ' + IntToStr(Asd) + #13
+ 'Pause after block: ' + IntToStr(PauseAfterBlock) + ' ms'
;
end;
function TTzxBlock19.Load2(const Stream: TStream; L: Integer
): Boolean;
// The length of SymDefsData is Asd. All values in data stream must point to
// a valid symdef in SymDefsData array (all values must be smaller than Asd).
// CheckData function checks that.
function CheckData(): Boolean;
var
N, M, K: Integer;
T: Integer;
begin
Result := False;
P := Data;
BitPosition := 7;
T := 0;
repeat
N := BitPosition + 1 - NB;
if (P >= PEnd) and ((P > PEnd) or (N < UnusedBitsInLastByte)) then
Break;
Inc(T);
K := P^;
if N = 0 then begin
M := K and Mask;
Inc(P);
BitPosition := 7;
end else if N > 0 then begin
M := (K shr N) and Mask;
BitPosition := BitPosition - NB;
end else begin
M := K shl (-N);
Inc(P);
K := P^;
M := (M or (K shr (N + 8))) and Mask;
BitPosition := 7 + N;
end;
if M >= Asd then
Exit; // the value points to outside of SymDefs array!
until False;
Result := ((N + NB) mod 8 = UnusedBitsInLastByte) and (T = Totd);
end;
function LoadSymDefs(var SymDefs: TSymDefs; Asx, Npx: Integer): Boolean;
var
I, J: Integer;
WA: Array[0..255] of Word;
W: Word;
B: Byte;
begin
SetLength(SymDefs, Asx);
if Asx > 0 then begin
Result := False;
if Npx = 0 then
Exit;
L := L - Asx * (2 * Npx + 1);
if L >= 0 then begin
for I := 0 to Asx - 1 do begin
if Stream.Read(B{%H-}, 1) <> 1 then
Exit;
SymDefs[I].SymbolFlag := B and %11;
if Stream.Read({%H-}WA[0], 2 * Npx) <> 2 * Npx then
Exit;
SetLength(SymDefs[I].PulseLengths, Npx);
SymDefs[I].Len := Npx;
J := 0;
while J < Npx do begin
W := LEtoN(WA[J]);
if W = 0 then begin
if J = 0 then
Exit;
SymDefs[I].Len := J;
SetLength(SymDefs[I].PulseLengths, J);
Break;
end;
SymDefs[I].PulseLengths[J] := W;
Inc(J);
end;
end;
end;
end;
Result := True;
end;
type
TBlock19Header = packed record
PauseAfterBlock: Word;
Totp: UInt32;
Npp: Byte;
Asp: Byte;
Totd: UInt32;
Npd: Byte;
Asd: Byte;
end;
const
SizeOfBlockHeader = SizeOf(TBlock19Header);
var
W: Word;
UL: UInt32;
Block19Header: TBlock19Header;
I, L2: Integer;
begin
Result := False;
if L >= SizeOfBlockHeader then begin
L := L - SizeOfBlockHeader;
if Stream.Read(Block19Header{%H-}, SizeOfBlockHeader) = SizeOfBlockHeader then begin
W := LEtoN(Block19Header.PauseAfterBlock);
PauseAfterBlock := W;
UL := LEtoN(Block19Header.Totp);
Totp := UL;
if Totp > 0 then begin
Npp := Block19Header.Npp;
Asp := Block19Header.Asp;
if Asp = 0 then
Asp := 256;
end else begin
Totp := 0;
Npp := 0;
Asp := 0;
end;
UL := LEtoN(Block19Header.Totd);
Totd := UL;
if Totd > 0 then begin
Npd := Block19Header.Npd;
if Npd = 0 then
Exit;
Asd := Block19Header.Asd;
if Asd = 0 then
Asd := 256;
end else begin
Totd := 0;
Npd := 0;
Asd := 0;
end;
if LoadSymDefs(SymDefsPilot, Asp, Npp) then begin
SetLength(Prles, Totp);
if Totp > 0 then begin
L := L - Totp * 3;
if L < 0 then
Exit;
for I := 0 to Totp - 1 do begin
if Stream.Read(Prles[I], 3) <> 3 then
Exit;
// The length of SymDefsPilot is Asp. Each prle in Prles array must
// point to allocated symbol (SymbolToBeRepresented must be less than Asp).
if Prles[I].SymbolToBeRepresented >= Asp then
Exit;
Prles[I].NumberOfRepetitions := LEtoN(Prles[I].NumberOfRepetitions);
end;
end;
UnusedBitsInLastByte := 0;
NB := 0;
if Totd > 0 then begin
if LoadSymDefs(SymDefsData, Asd, Npd) and (L > 0) then begin
if Asd = 1 then
NB := 1
else begin
L2 := Asd - 1;
while L2 > 0 do begin
Inc(NB);
L2 := L2 shr 1;
end;
end;
if (NB * Totd + 7) div 8 = L then begin
Getmem(Data, L);
PEnd := Data + (L - 1);
Mask := (1 shl NB) - 1;
UnusedBitsInLastByte := L * 8 - NB * Totd;
Result := (Stream.Read(Data^, L) = L)
and CheckData(); // check that every value in data stream points to an allocated symbol!
end;
end;
end else
Result := L = 0;
end;
FillDetails;
end
end;
end;
function TTzxBlock19.IsReallyPlayableBlock: Boolean;
begin
Result := True;
end;
constructor TTzxBlock19.Create(ATapePlayer: TTapePlayer);
begin
inherited Create(ATapePlayer);
FDetails := '';
PauseAfterBlock := 0;
Totp := 0;
Npp := 0;
Asp := 0;
Totd := 0;
Npd := 0;
Asd := 0;
SetLength(SymDefsPilot, 0);
SetLength(Prles, 0);
SetLength(SymDefsData, 0);
Data := nil;
P := nil;
end;
destructor TTzxBlock19.Destroy;
begin
if Data <> nil then
Freemem(Data);
inherited Destroy;
end;
class function TTzxBlock19.GetBlockId: DWord;
begin
Result := $19;
end;
class function TTzxBlock19.GetBlockDescription: String;
begin
Result := 'Generalized Data Block';
end;
function TTzxBlock19.GetNextPulse: Boolean;
procedure ToPause;
begin
FTapePlayer.ActiveBit := FTapePlayer.ActiveBit xor %01000000;
if PauseAfterBlock > 0 then
FTapePlayer.StartPauseBlock(PauseAfterBlock);
State := TPlayState.psFinished;
TicksNeeded0 := 0;
end;
procedure NewSymDef; inline;
begin
case SymDef.SymbolFlag of
0, 1:
if (SymDef.SymbolFlag = 0) xor Start1 then
FTapePlayer.ActiveBit := FTapePlayer.ActiveBit xor %01000000;
2:
FTapePlayer.ActiveBit := 0;
3:
FTapePlayer.ActiveBit := %01000000;
otherwise
end;
Start1 := False;
SymLen := SymDef.Len;
end;
procedure GetNextDataPulse;
var
NN, MM, K: Integer;
begin
Inc(SymPos);
if SymPos >= SymLen then begin
SymPos := 0;
NN := BitPosition + 1 - NB;
if (P < PEnd) or ((P = PEnd) and (NN >= UnusedBitsInLastByte)) then begin
K := P^;
if NN = 0 then begin
MM := K and Mask;
Inc(P);
BitPosition := 7;
end else if NN > 0 then begin
MM := (K shr NN) and Mask;
BitPosition := BitPosition - NB;
end else begin
MM := K shl (-NN);
Inc(P);
K := P^;
MM := (MM or (K shr (NN + 8))) and Mask;
BitPosition := 7 + NN;
end;
SymDef := SymDefsData[MM];
NewSymDef;
end else begin
ToPause;
Exit;
end;
end else
FTapePlayer.ActiveBit := FTapePlayer.ActiveBit xor %01000000;
TicksNeeded0 := SymDef.PulseLengths[SymPos];
PulsesNeeded := 1;
end;
var
ProcTicks: Int64;
begin
if State = TPlayState.psFinished then
Exit(False);
ProcTicks := GetCurrentTotalSpectrumTicks;
if ProcTicks >= TicksNeeded then begin
Dec(PulsesNeeded);
if PulsesNeeded = 0 then begin
repeat // never loops, but allows break
case State of
TPlayState.psData:
begin
GetNextDataPulse;
end;
TPlayState.psPilot:
begin
Inc(SymPos);
if SymPos >= SymLen then begin
SymPos := 0;
Inc(CurrentPrle);
if CurrentPrle >= Totp then begin
if Totd > 0 then begin
SymLen := -1;
State := TPlayState.psData;
GetNextDataPulse;
end else
ToPause;
Break;
end;
Prle := Prles[CurrentPrle];
SymDef := SymDefsPilot[Prle.SymbolToBeRepresented];
NewSymDef;
end else
FTapePlayer.ActiveBit := FTapePlayer.ActiveBit xor %01000000;
PulsesNeeded := Prle.NumberOfRepetitions;
TicksNeeded0 := SymDef.PulseLengths[SymPos];
end;
otherwise
ToPause;
end;
until True;
AdjustTicksIfNeeded(TicksNeeded0);
end else
FTapePlayer.ActiveBit := FTapePlayer.ActiveBit xor %01000000;
TicksNeeded := ProcTicks + TicksNeeded0;
end;
Result := True;
end;
procedure TTzxBlock19.Start;
begin
inherited Start;
Start1 := True;
TicksNeeded0 := 0;
TicksNeeded := TicksNeeded.MinValue;
PulsesNeeded := 1;
P := Data;
BitPosition := 7;
CurrentPrle := -1;
SymPos := 0;
SymLen := -1;
if Totp > 0 then
State := TPlayState.psPilot
else if Totd > 0 then
State := TPlayState.psData
else
State := TPlayState.psFinished;
end;
procedure TTzxBlock19.Details(out S: String);
begin
S := FDetails;
end;
{$endif}