-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuNBCop.pas
More file actions
564 lines (507 loc) · 12.5 KB
/
uNBCop.pas
File metadata and controls
564 lines (507 loc) · 12.5 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
{
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 uNBCop;
interface
uses uNBTypes, classes, upccomms, uNBCassette;
{$I 'dsp.inc'}
Const
CASSCOM = $80;
CASSLOD = $8C;
PASSCOM = $90;
DISPCOM = $A0;
TIMCOM = $B0;
PDNCOM = $C0;
NULLCOM = $D0;
RESCOM = $F0;
BRKBIT = $04;
REGINT = $00; // copstatus bits here
CASSERR = $10;
CASSIN = $20;
KBD = $30;
CASSOUT = $40;
PLAYBK = 4;
RECRD = 0;
Type
TNBDevice = (NBUnknown, NBTape1, NBTape2, NBPrn, NBComm);
TTapeStatus = (TSNone, TSstart, TSCountL, TSCountH, TSBlock, TSType,
TSChkL, TSChkH);
TTapeBlock = record
Start: byte;
Count: smallint;
TPType: byte;
chksum: smallint;
end;
TCop420 = Class
private
Tapestatus: TTapeStatus;
Tapeblock: TTapeBlock;
Fvfbuf: string;
NextCopOutIsData: boolean;
NextCopInIsData: boolean;
TapeCounter: Integer;
procedure OpenPrinter;
procedure WritePrinter(b: byte);
procedure DoWriteCopByte(Value: byte);
procedure setvfbuf(const Value: string);
public
LastCopCommand: byte;
Device: TNBDevice;
ComBuf: Array [1 .. 12] of byte;
StartCommInput: boolean;
Bytes: Integer;
f: textfile;
prnopened: boolean;
CommOpened: boolean;
bithasset: boolean;
rom3d: Integer;
Cass1: TNBCassette;
Cass2: TNBCassette;
function Cassette: TNBCassette;
procedure DoCopCommand(Value: byte);
function GetFromCop: byte;
procedure TranslateBuffer;
function KBEnabled: boolean;
constructor Create; virtual;
procedure ClosePrinter;
destructor Destroy; Override;
property vfbuf:string read Fvfbuf write setvfbuf;
End;
Var
COP420: TCop420; // some cop functions
implementation
uses jcllogic, z80baseclass, windows,
uNBTapes, uNbMemory, sysutils, new, forms,
math, FileCtrl, uNBScreen, uNBCPM, uNBIO;
constructor TCop420.Create;
begin
inherited;
freeandnil(NBDiscCtrl);
if fNewBrain.WithCPM1.Checked then
NBDiscCtrl := TNBDISCCtrl.Create
else
NBDiscCtrl := nil;
Cass1 := TNBCassette.Create;
Cass1.TapeNo := 1;
Cass2 := TNBCassette.Create;
Cass2.TapeNo := 2;
end;
// returns the current tape
function TCop420.Cassette: TNBCassette;
begin
Result := nil;
case Device of
NBTape1:
Result := Cass1;
NBTape2:
Result := Cass2;
end;
end;
function TCop420.GetFromCop: byte;
// Var
// DoBreak: boolean;
procedure filegetbyte;
begin
// get 1 byte
try
Result := Cassette.ReadComm;
{$IFDEF NBTAPEDEBUG}
ODS('$' + inttohex(prepc) + ' COP BYTE:' + inttostr(Result) + ' ' +
inttohex(Result, 2) + ' [' + inttohex(nbio.EnableReg) + ']');
{$ENDIF}
except
end;
end;
procedure KBgetbyte;
begin
if nbio.kbint then
begin
nbio.kbint := false;
Result := nbio.keypressed;
nbio.keypressed := $80;
// if DoBreak then
// nbio.BrkPressed := false;
exit;
end;
end;
procedure getInterruptVector;
begin
{$IFDEF NBDEBUG}
ODS('*********Int VECTOR**********');
{$ENDIF}
// BREAK KEY HANDLER
if nbio.BrkPressed then
Begin
Result := Result or BRKBIT;
nbio.BrkPressed := false;
exit;
end;
// TAPE ERROR HANDLER
if (Cassette <> nil) and Cassette.Loading and Cassette.CassError then
begin
Result := CASSERR;
Cassette.Loading := false;
Tapestatus := TSNone;
exit;
end;
// KEYBOARD HANDLER
// this is special case casue the screen is enabled so we give data too
if nbio.KBStatus = SendKey then // after loop read the real key
Begin
nbio.KBStatus := NoKey;
KBgetbyte;
end
else if nbio.KBStatus = SigKey then
// NB expects byte dif from $80 to leave the loop
begin
Result := $85; // just to leave the loop
nbio.KBStatus := SendKey;
end
else if nbio.kbint then
begin
nbio.KBStatus := SigKey; // signal cop has a key
Result := Result or KBD; // 3X means kbd int
end;
// TAPE HANDLER
if (Cassette <> nil) and Cassette.Loading then
begin
Result := CASSIN or (LastCopCommand and $F) // 0
end;
if (Cassette <> nil) and Cassette.Saving then
Result := Result or CASSOUT;
end;
procedure getData;
var
i, t: Integer;
begin
{$IFDEF NBDEBUG}
ODS('*********GETDATA**********');
{$ENDIF}
if (Cassette <> nil) and Cassette.Loading then
begin
filegetbyte;
NextCopInIsData := false;
case Tapestatus of
TSstart:
begin
Tapeblock.Start := Result;
Tapestatus := TSCountL;
end;
TSCountL:
begin
Tapeblock.Count := Result;
Tapestatus := TSCountH;
end;
TSCountH:
begin
Tapeblock.Count := Tapeblock.Count + Result * 256;
Tapestatus := TSBlock;
end;
TSBlock:
begin
Tapeblock.Count := Tapeblock.Count - 1;
if Tapeblock.Count = 0 then
Tapestatus := TSType;
end;
TSType:
begin
Tapeblock.TPType := Result;
Tapestatus := TSChkL;
end;
TSChkL:
begin
Tapeblock.chksum := Result;
Tapestatus := TSChkH;
end;
TSChkH:
begin
Tapeblock.chksum := Tapeblock.chksum + Result * 256;
Tapestatus := TSNone;
Cassette.Loading := false; // block loaded
t := Result;
for i := 1 to 9 do // read 9 zeroes
filegetbyte;
Result := t;
end;
end;
end
else
NextCopInIsData := false;
end;
begin
Result := $0;
if (nbio.EnableReg shr 4 <> 0) or (prepc = $E255) or NextCopInIsData then
getData
else
begin
getInterruptVector;
if Tapestatus <> TSNone then
NextCopInIsData := true;
end;
{$IFDEF NBDEBUG}
ODS('$' + inttohex(prepc) + ' COP IN:' + inttostr(Result) + ' (' +
inttohex(Result, 2) + ') ENREG=[' + inttohex(nbio.EnableReg) + ']');
{$ENDIF}
exit;
end;
// this is a byte that cop needs to write somewhere (tape or vf)
procedure TCop420.DoWriteCopByte(Value: byte);
var
s: char;
begin
if (Cassette <> nil) and Cassette.Saving then // write a byte to file
begin
Cassette.Writecomm(Value);
NextCopOutIsData := false;
exit;
end;
If (LastCopCommand = $A0) and (Length(vfbuf) < 18) then
Begin
ods(inttohex(z80_get_reg(Z80_REG_IFF1), 4));
s := chr(Value);
if (length(vfbuf)=0) and (s=#160) then
s:=' '
else
vfbuf := s + vfbuf;
{$IFDEF NBDEBUG}
ODS('COP DISP INP ' + inttostr(Value));
{$ENDIF}
exit;
End
else
begin
nbscreen.PaintLeds(vfbuf); // Refresh vf disp
LastCopCommand := $80;
NextCopOutIsData := false;
{$IFDEF NBDEBUG}
ODS('VFBUF= ' + vfbuf)
{$ENDIF}
;
exit;
end;
{$IFDEF NBDEBUG}
ODS('??????LOST OUT ' + inttohex(LastCopCommand) + ' Value:' +
inttostr(Value))
{$ENDIF}
end;
procedure TCop420.DoCopCommand(Value: byte);
var
CopCMD: byte;
CopData: byte;
begin
if NextCopOutIsData then
begin
{$IFDEF NBDEBUG}
ODS('COP Write Byte ' + inttostr(Value));
{$ENDIF}
DoWriteCopByte(Value);
exit;
end
else
begin
{$IFDEF NBDEBUG}
ODS('COP Command Byte ' + inttohex(Value) + ' ENREG=' +
inttohex(nbio.EnableReg));
{$ENDIF}
end;
CopCMD := Value shr 4; // Hi 4 bits
CopData := Value and $0F; // low 4 bits
case CopCMD of
$08: // CASSCOM
begin
{$IFDEF NBDEBUG}
s := 'CASSCOM' + ' ENREG:' + inttohex(nbio.EnableReg);
// CASSCOM 1
{$ENDIF}
if CopData and $08 = $08 then // TAPE 1 Command
Device := NBTape1
else if CopData and $02 = $02 then // TAPE 2 Command
Device := NBTape2
else
begin
ODS('TAPE CMD:' + inttohex(CopData));
exit;
end;
{$IFDEF NBTAPEDEBUG}
ODS('TAPE:' + inttostr(TapeNo));
{$ENDIF}
if CopData and PLAYBK = PLAYBK then // Loading
begin
{$IFDEF NBDEBUG}
s := 'CASSLOAD' + ' ENREG:' + inttohex(nbio.EnableReg);
// CASSCOM 1
{$ENDIF}
if not Cassette.Loading then
Begin
if Cassette.ResetTape then
Cassette.DoResetTape;
Cassette.Loading := true;
NextCopInIsData := false;
Tapestatus := TSstart;
TapeCounter := 0;
try
Cassette.CassError := false;
Cassette.OpenCassette(true);
except
Cassette.CassError := true;
Tapestatus := TSNone;
end;
{$IFDEF NBTAPEDEBUG}
ODS('----------------START LOADING----------------');
{$ENDIF}
End;
end
else if CopData and RECRD = RECRD then // Saving
begin
if not Cassette.Saving then
begin
if Cassette.ResetTape then
Cassette.DoResetTape;
Cassette.Saving := true;
NextCopOutIsData := false;
Cassette.OpenCassette;
Cassette.Writecomm(0);
end
else
NextCopOutIsData := true;
end;
end;
$09: {$IFDEF NBDEBUG} s := 'PASSCOM' + ' ENREG:' + inttohex(nbio.EnableReg)
{$ENDIF}; // PASSCOM
$0A: // DISPCOM
begin
{$IFDEF NBDEBUG}
s := 'DISPCOM ' + ' ENREG:' + inttohex(nbio.EnableReg); {$ENDIF} // DISPCOM 1 for vf display
vfbuf := '';
ods(inttohex(z80_get_reg(Z80_REG_IFF1), 4));
NextCopOutIsData := true; // Cop will get 18 bytes
end;
$0B: {$IFDEF NBDEBUG} s := 'TIMCOM' {$ENDIF}; // TIMCOM 1
$0C: {$IFDEF NBDEBUG} s := 'PDNCOM' {$ENDIF}; // PDNCOM
$0D: // NULLCOM
begin
{$IFDEF NBDEBUG}
s := 'NULLCOM' + ' ENREG:' + inttohex(nbio.EnableReg); {$ENDIF}
if (Cassette <> nil) and Cassette.Saving then
begin
Cassette.closecomm;
Cassette.Saving := false;
end;
end;
$0E0: {$IFDEF NBDEBUG} s := '???COM' {$ENDIF};
$0F0: {$IFDEF NBDEBUG} s := 'RESCOM' {$ENDIF}; // RESCOM
else
Begin
{$IFDEF NBDEBUG}
ODS('LastCop Cmd=' + s + ' ' + inttostr(Value)); {$ENDIF}
exit;
End;
end; // end case
{$IFDEF NBDEBUG}
if Value <> $0D0 then
ODS(s);
{$ENDIF}
LastCopCommand := Value;
end;
procedure TCop420.ClosePrinter;
Begin
if prnopened then
closefile(f);
prnopened := false;
End;
function TCop420.KBEnabled: boolean;
begin
Result := true;
// Result := not loading and not saving;
end;
procedure TCop420.OpenPrinter; // Obsolete Look uPCComms
var
fname: String;
Begin
if prnopened then
exit;
fname := extractfilepath(application.exename);
fname := fname + 'printer.txt';
prnopened := true;
assignFile(f, fname);
if not fileexists(fname) then
rewrite(f)
else
Append(f);
Writeln(f, '');
Writeln(f, '');
Writeln(f, '--------------------------------');
Writeln(f, '--NewBrain Printer [' + datetimetostr(now) + '] ---------');
Writeln(f, '--------------------------------');
Writeln(f, '');
End;
procedure TCop420.setvfbuf(const Value: string);
begin
fvfbuf:=value;
end;
procedure TCop420.TranslateBuffer;
Var
tbit: byte;
Function GetByte: byte;
Var
i: byte;
Begin
Result := 0;
if Device = NBPrn then
tbit := 7
else
tbit := 5;
For i := 0 to 7 do
if TestBit(ComBuf[i + 3], tbit) then
Result := SetBit(Result, i);
End;
Var
b: byte;
Begin
if TestBit(ComBuf[1], 5) then
Device := NBComm // comms
else
Device := NBPrn; // printer
b := GetByte;
Case Device of
NBTape1:
;
NBTape2:
;
NBPrn:
begin
OpenPrinter;
WritePrinter(b);
end;
NBComm:
begin
// OpenComm; //todo:
// WriteComm(b);
end;
End;
End;
procedure TCop420.WritePrinter(b: byte);
Begin
Write(f, char(b));
End;
destructor TCop420.Destroy;
begin
ClosePrinter;
Cass1.closecomm;
Cass2.closecomm;
Cass1.free;
Cass2.free;
inherited;
end;
end.