-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuNBCassette.pas
More file actions
429 lines (383 loc) · 8.4 KB
/
uNBCassette.pas
File metadata and controls
429 lines (383 loc) · 8.4 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
{
Grundy NewBrain Emulator Pro Made by Despsoft
Copyright (c) 2004-Today , Despoinidis Chris
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
}
unit uNBCassette;
interface
uses uNBTapes, classes;
Type
TCassetteState = (csIdle, csLoading, csSaving);
TNBCassette = class
constructor Create;
destructor Destroy; override;
private
FFilename: string;
cf: File of byte;
state: TCassetteState;
comcnt: Integer;
size: Integer;
Commopened: Boolean;
FTapeNo: Integer;
function getLoading: Boolean;
function getSaving: Boolean;
function CheckFileEnd: Boolean;
function CurrentExt: string;
function CheckReadEOF: Boolean;
function getFileName: String;
function FindFileName: string;
procedure NBRenameFile(Fname: string);
procedure setLoading(const Value: Boolean);
procedure setSaving(const Value: Boolean);
procedure DeleteNoName;
procedure SetFilename(Value: String);
procedure setTapeNo(const Value: Integer);
procedure UpdateVi;
public
CassError: Boolean;
ResetTape: Boolean;
FileIsBinary: Boolean;
TapeInfo: TTapeInfo;
wrcnt: Integer;
function Root: string;
procedure DoResetTape;
procedure OpenCassette(ToRead: Boolean = false);
procedure CloseComm;
function ReadComm: byte;
procedure Writecomm(b: byte);
property loading: Boolean read getLoading write setLoading;
property saving: Boolean read getSaving write setSaving;
property Filename: String read FFilename write SetFilename;
property TapeNo: Integer read FTapeNo write setTapeNo;
end;
implementation
uses sysutils, forms, new, uNbTypes, uNBIO;
{ TNBCassette }
constructor TNBCassette.Create;
begin
TapeInfo := TTapeInfo.Create;
Filename := 'NoName';
DeleteNoName;
state := csIdle;
ResetTape := false;
TapeNo := 0;
end;
destructor TNBCassette.Destroy;
begin
TapeInfo.free;
inherited;
end;
function TNBCassette.getLoading: Boolean;
begin
result := state = csLoading;
end;
function TNBCassette.getSaving: Boolean;
begin
result := state = csSaving;
end;
function TNBCassette.Root: string;
begin
if TapeInfo.TapeLoaded then
result := TapeInfo.Directory
Else
result := Extractfilepath(application.exename) + 'Basic\';
end;
procedure TNBCassette.setLoading(const Value: Boolean);
begin
if Value then
state := csLoading
else
state := csIdle;
end;
procedure TNBCassette.setSaving(const Value: Boolean);
begin
if Value then
state := csSaving
else
state := csIdle;
end;
procedure TNBCassette.setTapeNo(const Value: Integer);
begin
FTapeNo := Value;
TapeInfo.TapeNo := Value;
end;
function TNBCassette.CurrentExt: string;
begin
if FileIsBinary then
result := '.bin'
else
result := '.bas';
end;
function TNBCassette.FindFileName: string;
Var
Fname: String;
f: String;
i: Integer;
Begin
for i := 1 to 10000 do
Begin
f := 'UnKnown' + inttostr(i);
Fname := Root + f + CurrentExt;
if not fileexists(Fname) then
break;
End;
fnewbrain.WriteP2('Saved as ' + f + CurrentExt);
result := f;
End;
Function TNBCassette.getFileName: String;
Var
ln: tpair;
s: String;
i: Integer;
Fname: string;
r: byte;
Begin
if pos('.', Filename) = 0 then
Begin
Fname := Root + Filename + CurrentExt
End
Else
Fname := Root + Filename;
assignfile(cf, Fname);
reset(cf);
seek(cf, 1);
ln.l := ReadComm;
ln.h := ReadComm;
s := '';
if ln.w > 0 then
Begin
for i := 1 to ln.w do
begin
read(cf, r);
s := s + char(r); // char(ReadComm); //was ReadComm
end;
End;
if s = '' then
Begin
if sametext(Filename, 'noname') then
s := FindFileName
else
s := Filename;
end;
i := pos(':', s);
if i > 0 then
s := copy(s, i + 1, maxint);
closefile(cf);
result := s;
End;
procedure TNBCassette.UpdateVi;
begin
if TapeNo = 1 then
fnewbrain.lblTape1Info.Caption := Filename + CurrentExt
else
fnewbrain.lblTape2Info.Caption := Filename + CurrentExt;
end;
procedure TNBCassette.SetFilename(Value: String);
Var
k: Integer;
begin
k := pos('.bin', Lowercase(Value));
if k > 0 then
Begin
FileIsBinary := true;
Value := copy(Value, 1, k - 1);
End;
k := pos('.bas', Lowercase(Value));
if k > 0 then
Begin
FileIsBinary := false;
Value := copy(Value, 1, k - 1);
End;
if FFilename <> Value then
begin
FFilename := Value;
UpdateVi;
end;
end;
procedure TNBCassette.NBRenameFile(Fname: string);
begin
if fileexists(Root + Fname + CurrentExt) then
Begin
renameFile(Root + Fname + CurrentExt, Root + Fname + '.bak');
fnewbrain.WriteP2(Fname + ' was replaced');
End;
renameFile(Root + Filename + CurrentExt, Root + Fname + CurrentExt);
Filename := Root + Fname + CurrentExt;
end;
Procedure TNBCassette.DeleteNoName;
Begin
if fileexists(Root + 'Noname.bas') then
deletefile(Root + 'Noname.bas');
if fileexists(Root + 'Noname.bin') then
deletefile(Root + 'Noname.bin');
End;
procedure TNBCassette.OpenCassette(ToRead: Boolean = false);
var
Fname: String;
Begin
if Commopened then
exit;
if not ToRead then
Filename := 'Noname';
if ToRead and TapeInfo.TapeLoaded then
Begin
Fname := TapeInfo.getFileName;
End
Else
Fname := Root + Filename + CurrentExt;
if ToRead and not fileexists(Fname) then
Begin
Filename := 'NotFound';
Fname := Root + 'NotFound' + CurrentExt;
ResetTape := true;
End;
Commopened := true;
assignfile(cf, Fname);
if ToRead then
Begin
reset(cf);
size := FileSize(cf);
End
else
begin
if fileexists(Fname) then
Begin
filemode := 2;
reset(cf);
size := FileSize(cf);
seek(cf, size);
End
else
begin
Rewrite(cf);
wrcnt := 0;
end;
end;
End;
procedure TNBCassette.CloseComm;
Var
s: String;
Begin
if Commopened then
Begin
closefile(cf);
if saving then
Begin
if CheckFileEnd then
Begin
s := getFileName;
if not sametext(Filename, s) then
NBRenameFile(s);
if TapeInfo.TapeLoaded then
Begin
TapeInfo.AddFile(Filename + CurrentExt);
TapeInfo.Lastfile;
End;
Filename := 'NoName';
saving := false;
End;
End;
End;
if TapeInfo.TapeLoaded and not saving then
Begin
TapeInfo.NextFile;
Filename := TapeInfo.GetNextFileName;
End;
Commopened := false;
NBIO.CopInt := false;
End;
procedure TNBCassette.DoResetTape;
Begin
try
CassError := false;
CloseComm;
comcnt := 0;
size := 0;
loading := false;
saving := false;
try
closefile(cf);
except
end;
except
CassError := true;
Commopened := false;
end;
ResetTape := false;
End;
function TNBCassette.CheckReadEOF: Boolean;
Begin
result := false;
if size = comcnt then
Begin
if size > 0 then
CloseComm;
comcnt := 0;
size := 0;
loading := false;
result := true;
if not TapeInfo.TapeLoaded then
Filename := 'Noname';
End;
End;
function TNBCassette.ReadComm: byte;
Var
f: Boolean;
Begin
try
f := eof(cf);
Except
f := true;
ResetTape := true;
End;
if not f then
Begin
read(cf, result);
inc(comcnt);
fnewbrain.WriteP2(inttostr(comcnt));
CheckReadEOF;
End
else
Begin
comcnt := 0;
size := 0;
Filename := 'Noname';
loading := false;
End;
End;
procedure TNBCassette.Writecomm(b: byte);
Begin
inc(wrcnt);
Write(cf, b);
fnewbrain.WriteP2(inttostr(wrcnt));
End;
function TNBCassette.CheckFileEnd: Boolean;
Var
Fname: String;
sz: Integer;
b: byte;
Begin
result := false;
if saving then
Begin
Fname := Root + Filename + CurrentExt;
assignfile(cf, Fname);
reset(cf);
sz := FileSize(cf);
seek(cf, sz - 12);
b := ReadComm;
closefile(cf);
if b and 64 = 64 then // bit 6 set =last block written
result := true;
End;
End;
end.