-
Notifications
You must be signed in to change notification settings - Fork 1
/
tzxblock15.inc
187 lines (154 loc) · 4.05 KB
/
tzxblock15.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
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$ifdef tzx_header_section}
{ TTzxBlock15 }
TTzxBlock15 = class (TTzxBlock)
strict private
FLen: Integer;
PulseLen: Integer; // (pulse length - ticks needed for one pulse)
UsedBitsInLastByte: Integer;
UnusedBitsInLastByte: Integer;
PauseAfterBlock: Integer;
TicksNeeded: Int64;
TicksNeeded0: Int64;
Data: PByte;
P: PByte;
PEnd: PByte;
BitPosition: Integer;
public
constructor Create(ATapePlayer: TTapePlayer); override;
destructor Destroy; 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;
end;
{$else}
constructor TTzxBlock15.Create(ATapePlayer: TTapePlayer);
begin
inherited Create(ATapePlayer);
P := nil;
FLen := 0;
PulseLen := 0;
UsedBitsInLastByte := 8;
PauseAfterBlock := 1000;
end;
destructor TTzxBlock15.Destroy;
begin
if Data <> nil then
Freemem(Data);
inherited Destroy;
end;
class function {TTzxPlayer.}TTzxBlock15.GetBlockId: DWord;
begin
Result := $15;
end;
function {TTzxPlayer.}TTzxBlock15.GetBlockLength: Integer;
begin
Result := FLen;
end;
function {TTzxPlayer.}TTzxBlock15.LoadBlock(const Stream: TStream): Boolean;
var
W: Word;
B: Byte;
L: Int32;
begin
Result := False;
if Stream.Size - Stream.Position >= 8 then begin
if Stream.Read(W{%H-}, 2) = 2 then begin
W := LEtoN(W);
PulseLen := W;
if Stream.Read(W, 2) = 2 then begin
W := LEtoN(W);
PauseAfterBlock := W;
if Stream.Read(B{%H-}, 1) = 1 then begin
UsedBitsInLastByte := B;
UnusedBitsInLastByte := 8 - UsedBitsInLastByte mod 8;
if ReadThreeBytes(Stream, L) then begin
Result := L = 0;
if not Result then begin
if Stream.Size - Stream.Position >= L then begin
//Stream.Seek(Int64(L), TSeekOrigin.soCurrent);
Getmem(Data, L);
try
if Stream.Read(Data^, L) = L then begin
PEnd := Data + (L - 1);
Result := True;
end;
finally
if not Result then
FreeMemAndNil(Data);
end;
end;
end;
FLen := 8 + L;
end;
end;
end;
end;
end;
end;
class function {TTzxPlayer.}TTzxBlock15.GetBlockDescription: String;
begin
Result := 'Direct Recording';
end;
function {TTzxPlayer.}TTzxBlock15.GetNextPulse: Boolean;
var
ProcTicks: Int64;
procedure ToPause;
begin
if PauseAfterBlock > 0 then
FTapePlayer.StartPauseBlock(PauseAfterBlock);
State := TPlayState.psFinished;
end;
procedure GetNextDataBit;
begin
if (P < PEnd) or ((P = PEnd) and (BitPosition >= UnusedBitsInLastByte)) then begin
if ((P^ shr BitPosition) and 1) = 0 then
FTapePlayer.ActiveBit := 0
else
FTapePlayer.ActiveBit := %01000000;
TicksNeeded := ProcTicks + TicksNeeded0;
if BitPosition > 0 then
Dec(BitPosition)
else begin
BitPosition := 7;
Inc(P);
end;
end else
ToPause;
end;
begin
{ Here each bit represents a pulse! }
if State = TPlayState.psFinished then
Exit(False);
ProcTicks := GetCurrentTotalSpectrumTicks;
if ProcTicks >= TicksNeeded then begin
// set active bit here
case State of
TPlayState.psData:
GetNextDataBit;
TPlayState.psStart:
if P = nil then
ToPause
else
State := TPlayState.psData;
otherwise
end;
end;
Result := True;
end;
procedure TTzxBlock15.Start;
begin
inherited Start;
State := TPlayState.psStart;
P := Data;
TicksNeeded := TicksNeeded.MinValue;
BitPosition := 7;
TicksNeeded0 := PulseLen;
AdjustTicksIfNeeded(TicksNeeded0);
end;
{$endif}