-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.pas
2212 lines (2018 loc) · 53 KB
/
common.pas
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
ESCAPE, Environment for the Simulation of Computer Architectures
for the Purpose of Education
Copyright (C) 1998-2001 Peter Verplaetse, ELIS Department, Ghent University
Copyright (C) 2010-2015 Jonas Maebe, ELIS Department, Ghent University
Copyright (C) 1996-1998 Jan Van Campenhout, ELIS Department, Ghent University
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 <http://www.gnu.org/licenses/>.
}
{ Project: Escape }
{ Version: 1.1 }
{ Author: Peter Verplaetse }
{ Date: 22 July 1998 }
{ This file contains routines common to both architectures }
unit Common;
{$MODE Delphi}
interface
uses StdCtrls, SysUtils, Dialogs, Classes, Compos, Graphics, Forms, {WinProcs,}
ClipBrd, Menus, Spin, NewStr;
const
{ A fixed-size array is used for the data inputs of muxes and buses; 20 should be enough }
MaxMuxDataInputs = 20;
{ Maximum number of rewind steps }
TrackBufferSize = 1024;
{ ALU operations }
NOP = 0;
ADD = 1;
SUB = 2;
RSUB = 3;
BITAND = 4;
BITOR = 5;
BITXOR = 6;
SLL = 7;
SRL = 8;
SRA = 9;
PASSS1 = 10;
PASSS2 = 11;
S2S1 = 12;
MULT = 13;
DIVI = 14;
AluOperation: array [0..14] of string = ('NOP','ADD','SUB','RSUB','AND',
'OR','XOR','SLL','SRL','SRA','S1','S2','S2S1','MUL','DIV');
{ Comparator Operations }
ONE = 1;
EQ = 2;
NE = 3;
LT = 4;
GT = 5;
LE = 6;
GE = 7;
CompOperation: array [0..7] of string = ('False','True','=0?','<>0?','<0?','>0?',
'<=0?','>=0?');
CompOperation2: array [0..7] of string = ('','True','EQ','NE','LT','GT',
'LE','GE');
{ Memory Operations }
READBYTE = 4;
READHALF = 5;
READWORD = 6;
WRITEBYTE = 8;
WRITEHALF = 9;
WRITEWORD = 10;
MemOperation: array [0..6] of string = ('RB','RH','RW','','WB','WH','WW');
type
{ For von Neumann and Harvard architectures: memory can contain code, data or both }
TContentType = (ctCode, ctData, ctBoth);
{ Warnings/Errors that can occur during simulation }
TError = (erFuncUnstable,erMARUnstable,erMDRUnstable,erNotAligned,
erOutOfRange, erBreak);
TErrorSet = set of TError;
TMemoryInterface = class
protected
Content: array [0..32768 div 4 -1] of LongInt;
CType: TContentType;
public
{ Check if the address is valid }
{ Groupsize = 0 for bytes; 1 for halves and 2 for words }
function CheckAddress(Address: LongInt; GroupSize: Integer): TErrorSet;
{ Read and Write check the address too, but throw an exception when the address is invalid }
function Read(Address: LongInt; GroupSize: Integer): LongInt;
procedure Write(Address, Value: LongInt; GroupSize: Integer);
{ Call these routines if the content of one or more memory locations has changed -> this will reflect the
change in the viewer or viewers in case of a von Neumann architecture }
procedure ChangedValue(Address: LongInt);
procedure ChangedRegion(BeginRegion, EndRegion: LongInt);
procedure ChangedAll;
procedure MemoryTouched;
constructor Create(ContentType: TContentType);
end;
TUnravel = class
{ This class unravels code words }
private
opc: Integer;
reg: Integer;
regmask: Integer;
imm1mask: LongInt;
imm2mask: LongInt;
public
Imm1Size: Integer;
Imm2Size: Integer;
Imm1Width: Integer;
Imm2Width: Integer;
{ Call UpdateFields to ensure the field widths are valid }
procedure UpdateFields;
{ Get the opcode number }
function Opcode(Instr: LongInt): Integer;
{ Get the actual register of formal i (1<=i<=6) }
function Register(Instr: LongInt; i: Integer): Integer;
{ Size of immediate 1 }
function Imm1(Instr: LongInt; Signed: Boolean): LongInt;
{ Size of immediate 2 }
function Imm2(Instr: LongInt; Signed: Boolean): LongInt;
{ Inverse of unravel: encode opcode }
function AddOpcode(Opcode: Integer): LongInt;
{ Encode register }
function AddRegister(Register,i: Integer): LongInt;
{ Encode immediate 1 }
function AddImm1(Imm: LongInt): LongInt;
{ Encode immediate 2 }
function AddImm2(Imm: LongInt): LongInt;
end;
TSeqSource = class;
{ The following classes are for the implementation of the rewind function.
For each clock period the changes in state are recorded. TIncrementalChange
is the datastructure used to record these changes. The semantics for Data1
and Data2 depend on the type of the Source object }
TIncrementalChange = class
Source: TSeqSource;
Data1: LongInt;
Data2: LongInt;
{ This is really a node of a unidirectional linked list }
Next: TIncrementalChange;
constructor Create;
end;
{ All the changes for 1 clock period }
TIncrementalState = class
private
{ The head of the linked list }
FirstChange: TIncrementalChange;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Rewind;
function ReportChange: TIncrementalChange;
end;
{ All the changes for the past 1024 clock periods }
TTrackBuffer = class
private
Buffer: array [0..TrackBufferSize-1] of TIncrementalState;
StartTime: LongInt;
StartIndex: Integer;
ActualIndex: Integer;
public
constructor Create;
destructor Destroy; override;
{ Call this to clear the buffer and set the start time to the actual clock }
procedure Reset;
{ Increment the clock }
procedure Clock;
{ Call this to call the rewind function of all the objects that changed a state and thus rewind the clock }
procedure Rewind;
{ False when no earlier changes have been recorded }
function Rewindable: Boolean;
{ Returns an empty TIncrementalChange to be filled in by the source object }
function ReportChange: TIncrementalChange;
end;
{ The simulator is value-driven and cycle-based. Each object has zero or more inputs
and always a single output of type LongInt. }
TDataSource = class
protected
{ The viewer can be anything and is object specific }
Viewer: TComponent;
public
{ The Name is used for pop-up content boxes }
Name: string;
constructor Create(View: TComponent; DName: string); virtual;
{ After creation, call Connect to connect the inputs }
procedure Connect(Inputs: array of TDataSource); virtual;
{ Returns the LongInt value }
function Value: LongInt; virtual;
{ Returns the value as a string (used for pop-up content boxes, etc...) }
function StrValue: string; virtual;
end;
{ TCombiSource is a combinational object (no state) }
TCombiSource = class(TDataSource)
protected
RecentValue: LongInt;
RecentClock: LongInt;
{ Implement this function in the derived objects }
function GetValue: LongInt; virtual;
public
{ Value uses result-caching to speed up the simulation }
function Value: LongInt; override;
{ Result caching introduces a state. Therefore you need to reset these objects to when resetting the system }
procedure Reset;
end;
{ TSeqSource is a sequential object (with state) }
TSeqSource = class(TDataSource)
public
{ RestoreState is called when rewinding the clock }
procedure RestoreState(Change: TIncrementalChange); virtual;
procedure Reset; virtual;
{ A clock period consists of two phases: Evaluate and Propagate.
Evaluate evaluates the input but keeps the current output and state.
Propagate propagates the evaluated input and thus overwrites the current
output and state. Evaluate can be called as many times as you want,
but Propagate only once. Visualization is done during the Evaluate phase }
procedure Evaluate; virtual;
procedure Propagate; virtual;
end;
{ An object that always returns the same value (constant) }
TConstant = class(TDataSource)
private
Constant: LongInt;
public
procedure SetConstant(C: LongInt);
function Value: LongInt; override;
end;
{ This is, guess what, an or-gate }
TOrGate = class(TCombiSource)
private
S1: TDataSource;
S2: TDataSource;
public
procedure Connect(Inputs: array of TDataSource); override;
function GetValue: LongInt; override;
end;
{ Yep, a multiplexer. Could be a bus as well. }
TMux = class(TCombiSource)
private
Select: TDataSource;
Data: array [0..MaxMuxDataInputs-1] of TDataSource;
NumDataInputs: Integer;
public
{ Connect: First the select input, then the data inputs }
procedure Connect(Inputs: array of TDataSource); override;
function GetValue: LongInt; override;
end;
{ This is an ALU }
TAlu = class(TCombiSource)
private
Func: TDataSource;
S1: TDataSource;
S2: TDataSource;
public
{ Connect: Function, S1, S2 }
procedure Connect(Inputs: array of TDataSource); override;
function GetValue: LongInt; override;
end;
{ Comparator }
TComparator = class(TCombiSource)
private
Func: TDataSource;
S: TDataSource;
public
{ Connect: Function, S }
procedure Connect(Inputs: array of TDataSource); override;
function GetValue: LongInt; override;
{ Not the default 8 digit number, since the value can only be 0 or 1 }
function StrValue: string; override;
end;
{ Incrementor }
TIncrementor = class(TCombiSource)
private
Constant: LongInt;
S: TDataSource;
public
{ Increments used are 1 and 4 }
procedure SetIncrement(Val: LongInt);
procedure Connect(Inputs: array of TDataSource); override;
function GetValue: LongInt; override;
end;
{ Sign Extender. The input bit size is derived from the instruction type stored in the code word }
TSignExtend = class(TCombiSource)
private
OutputBitSize: TDataSource;
S: TDataSource;
public
{ Connect: OutputBitSize, CodeWord }
procedure Connect(Inputs: array of TDataSource); override;
function GetValue: LongInt; override;
end;
{ A register with enable input }
TReg = class(TSeqSource)
protected
En: TDataSource;
S: TDataSource;
NewValue: LongInt;
CurrentValue: LongInt;
ShowSize: Integer;
public
constructor Create(View: TComponent; DName: string); override;
{ ShowSize is the number of digits used by StrValue (8 by default).
If ShowSize is 0, the content is not shown }
procedure SetShowSize(Size: Integer);
{ Connect: Enable, DataIn }
procedure Connect(Inputs: array of TDataSource); override;
procedure RestoreState(Change: TIncrementalChange); override;
procedure Reset; override;
procedure Evaluate; override;
procedure Propagate; override;
function Value: LongInt; override;
function StrValue: string; override;
procedure Show; virtual;
end;
{ RegIR is a register to be used for holding code words. It has a shadow register
with the associated PC and shows dissassembled text instead of an 8 digit hex number }
TRegIR = class(TSeqSource)
private
En: TDataSource;
S: TDataSource;
PC: TDataSource;
NewValue: LongInt;
CurrentValue: LongInt;
NewPC: LongInt;
CurrentPC: LongInt;
public
{ Connect: Enable, CodeIn, PCIn }
procedure Connect(Inputs: array of TDataSource); override;
procedure RestoreState(Change: TIncrementalChange); override;
procedure Reset; override;
procedure Evaluate; override;
procedure Propagate; override;
function Value: LongInt; override;
function StrValue: string; override;
procedure Show;
end;
{ This is the second output of TRegIR: the delayed PC }
TRegIRPC = class(TDataSource)
private
RegIR: TRegIR;
public
procedure Connect(Inputs: array of TDataSource); override;
function Value: LongInt; override;
end;
{ A Register File. Has two output ports }
TRegFile = class(TSeqSource)
private
RR1: TDataSource;
WR: TDataSource;
S: TDataSource;
NewReg: Integer;
NewValue: LongInt;
ShowReg: Integer;
function Line(var Reg: Integer; Cols, Length: Integer): string;
public
{ The number of registers (0-256) }
NumRegisters: Integer;
Regs: array [0..255] of LongInt;
constructor Create(View: TComponent; DName: string); override;
{ Connect: ReadPort1Register, WriteRegister, WritePort }
procedure Connect(Inputs: array of TDataSource); override;
procedure RestoreState(Change: TIncrementalChange); override;
procedure Reset; override;
procedure Evaluate; override;
procedure Propagate; override;
procedure Show(Reg: Integer);
function Value: LongInt; override;
end;
{ This is the RegFile's second read port }
TRegFileBis = class(TDataSource)
private
RR2: TDataSource;
RegFile: TRegFile;
public
function Value: LongInt; override;
{ Connect: ReadPort2Register, RegFile }
procedure Connect(Inputs: array of TDataSource); override;
end;
{ This is a memory with fixed access time }
TRWMem = class(TSeqSource)
private
SMDR: TDataSource;
Memory: TMemoryInterface;
NewSMDR: LongInt;
NewMAR: LongInt;
NewFunc: Integer;
NewOperation: Boolean;
LastSMDR: LongInt;
RecentClock: LongInt;
RecentValue: LongInt;
public
Func: TDataSource;
MAR: TDataSource;
LastMAR: LongInt;
LastFunc: Integer;
Counter: Integer;
Cycles: Integer;
{ Set the memory interface after creation with this function }
procedure SetMemory(Mem: TMemoryInterface);
{ Set the speed with this function. AccessTime must be at least 1.
AccessTime-1 is the number of cycles you have to wait before the output
is valid. An Accesstime of 1 makes the memory pure combinational for read
operations }
procedure SetSpeed(AccessTime: Integer);
{ Connect: Function, DataIn }
procedure Connect(Inputs: array of TDataSource); override;
procedure RestoreState(Change: TIncrementalChange); override;
procedure Reset; override;
procedure Evaluate; override;
procedure Propagate; override;
function Value: LongInt; override;
function GetValue: LongInt;
procedure ResetRecentClock;
end;
{ TRWMem's second output: 1 when the memory is busy and thus no new memory operation can be started this cycle }
TRWMemBusy = class(TCombiSource)
private
RWMem: TRWMem;
public
function GetValue: LongInt; override;
procedure Connect(Inputs: array of TDataSource); override;
end;
{ TRWMem's third output: the read address associated with the current output value }
TRWMemAddress = class(TCombiSource)
private
RWMem: TRWMem;
public
function GetValue: LongInt; override;
procedure Connect(Inputs: array of TDataSource); override;
end;
{ This is the common part of the simulators for both architectures }
TSimulator = class
protected
RewindButton: TButton;
RewindCheck: TMenuItem;
GenerateTrace: TMenuItem;
MultiCycle: TSpinEdit;
MultiCycleCheck: TCheckBox;
SimForm: TForm;
Clock: TEditInteger;
RegFile: TRegFile;
PC: TReg;
LastPC: LongInt;
MaxClock: LongInt;
procedure Reset; virtual;
procedure Evaluate; virtual;
procedure Propagate; virtual;
procedure Rewind(Clocks: LongInt); virtual;
procedure Simulate(Clocks: LongInt); virtual;
procedure Trace(P: LongInt);
procedure CheckBreak;
public
{ The simulator enables buttons and menu items, and reads values from spinedits, etc... }
constructor Create(Parent: TForm; RwndBtn: TButton; RwndChk: TMenuItem; GenTrace: TMenuItem;
MltCyc: TSpinEdit; MltCycChk: TCheckBox; Clk: TEditInteger;
ChkStab: Boolean); virtual;
destructor Destroy; override;
procedure RewindClick;
procedure ResetClick;
procedure RewindCheckClick;
procedure ClockClick;
procedure ClockChangedByUser;
end;
{ This is an interface to the clipboard (text only) }
TClipBoardBuffer = class
private
Buffer: PChar;
Pointer: PChar;
public
constructor Create;
destructor Destroy; override;
{ Clear the buffer }
procedure Clear;
{ Write a line to the buffer }
procedure AddLine(line: string);
{ Read a line from the buffer}
function GetLine(var line: string): Boolean;
{ How many lines are in the buffer? }
function NumLines(MaxLines: Integer): Integer;
{ Put buffer on clipboard }
procedure ToClipBoard;
{ Get buffer from clipboard }
procedure FromClipBoard;
end;
{ This class is used for all file I/O }
TFileIO = class
private
FileName: string;
public
{ The file handle is public, this way Readln and Writeln and such can be done directly }
F: TextFile;
{ AssignFile, but keeps the Filename -- to work around the Reset bug }
procedure UseFile(TFName: string);
{ Write boolean }
procedure WriteYesNo(Item: string; Value: Boolean);
{ Write integer }
procedure WriteInteger(Item: string; Value: Integer);
{ Write string }
procedure WriteString(Item: string; Value: string);
{ Read boolean }
function ReadYesNo(Item: string; var Error: Boolean): Boolean;
{ Read integer }
function ReadInteger(Item: string; var Error: Boolean): Integer;
{ Read string }
function ReadString(Item: string; var Error: Boolean): string;
{ Find a string. If not found: rewind the file and search again from the top }
function FindString(Item: string; var Line: string): Boolean;
end;
{ ShowModal AboutBox }
procedure AboutBox;
{ Measure dimensions of various fonts }
function MSSansSerif8Width: Integer;
function MSSansSerif8Height: Integer;
function Courier9Width: Integer;
function Courier9Height: Integer;
function Courier10Width: Integer;
function Courier10Height: Integer;
{ The following 6 outines are usefull for parsing strings. pos is the position (1=beginning) }
procedure SkipSpaces(line: string; var pos: Integer);
{ Skip characters until a '|' is found. Skip that one too }
procedure SkipPastPipe(line: string; var pos: Integer);
{ Skip spaces and then read an identifier (sequence of alphanumeric characters }
function ReadIdentifier(line: string; var pos: Integer): string;
{ Like skipping, but the '|' is not skipped and the skipped characters are returned as a string }
function ReadUntilPipe(line: string; var pos: Integer): string;
{ Read an integer }
function ReadInt(line: string; var pos: Integer): LongInt;
{ Returns True when pos=Length(line) }
function EndLine(line: string; var pos: Integer): Boolean;
{ Returns a random LongInt }
function RandomLongInt: LongInt;
{ Returns the maximum number of characters needed to print a register in the register file box }
function MaxRegisterLength(NumRegisters: Integer): Integer;
{ Show a message box with the error }
function ReportError(Error: TErrorSet): Boolean;
{ Show a message box }
function AppliMessage(Msg,Cap: string; Flags: Word): Integer;
{ Set Modify on the current simulation form }
procedure SetModifyOnSimForm;
var
Unravel: TUnravel;
CodeMemory: TMemoryInterface;
DataMemory: TMemoryInterface;
ActualClock: LongInt;
ShowValues: Boolean;
TrackChanges: Boolean;
Tracker: TTrackBuffer;
SimError: TErrorSet;
CheckStability: Boolean;
ErrorAddress: LongInt;
CodeCaptionBase: string;
DataCaptionBase: string;
TraceDialog: TSaveDialog;
FileIO: TFileIO;
implementation
uses Config, InstrMem, DataMem, BrkForm, Micro, Pipe, About;
procedure SetModifyOnSimForm;
begin
if MSimulator<>nil then
MicroForm.SetModify;
if PSimulator<>nil then
PipeForm.SetModify
end;
procedure SkipSpaces(line: string; var pos: Integer);
begin
while (pos<=Length(line)) and (line[pos]=' ') do
pos:=pos+1
end;
procedure SkipPastPipe(line: string; var pos: Integer);
begin
while (pos<=Length(line)) and (line[pos]<>'|') do
pos:=pos+1;
if pos<=Length(line) then
pos:=pos+1
end;
function ReadIdentifier(line: string; var pos: Integer): string;
var
start: Integer;
begin
SkipSpaces(line,pos);
start:=pos;
while pos<=Length(line) do
case line[pos] of
'0'..'9','a'..'z','A'..'Z': pos:=pos+1
else
break
end;
Result:=Copy(line,start,pos-start)
end;
function ReadUntilPipe(line: string; var pos: Integer): string;
var
start: Integer;
begin
SkipSpaces(line,pos);
start:=pos;
while pos<=Length(line) do
if line[pos]<>'|' then
pos:=pos+1
else
break;
Result:=Copy(line,start,pos-start)
end;
function ReadInt(line: string; var pos: Integer): LongInt;
var
digit: Integer;
negative: Boolean;
begin
SkipSpaces(line,pos);
if (pos<Length(line)-1) and (line[pos]='0') and (line[pos+1]='x') then
begin
pos:=pos+2;
Result:=0;
while (pos<=Length(line)) do
begin
digit:=HexToInt(line[pos]);
if digit<0 then
break;
Result:=Result shl 4 +digit;
pos:=pos+1
end
end
else begin
negative:=false;
if (pos<Length(line)) and (line[pos]='-') then
begin
negative:=true;
pos:=pos+1
end
else if (pos<Length(line)) and (line[pos]='+') then
pos:=pos+1;
Result:=0;
while (pos<=Length(line)) do
begin
digit:=Ord(line[pos])-Ord('0');
if (digit<0) or (digit>9) then
break;
Result:=(Result*5) shl 1 +digit;
pos:=pos+1
end;
if negative then
Result:=-Result
end
end;
function EndLine(line: string; var pos: Integer): Boolean;
begin
Result:=(pos>Length(line))
end;
procedure TFileIO.UseFile(TFName: string);
begin
AssignFile(F,TFName);
FileName:=TFName
end;
procedure TFileIO.WriteYesNo(Item: string; Value: Boolean);
begin
Write(F,Item+'=');
if Value then
Writeln(F,'yes')
else
Writeln(F,'no')
end;
procedure TFileIO.WriteInteger(Item: string; Value: Integer);
begin
Writeln(F,Item+'='+IntToStr(Value))
end;
function TFileIO.ReadInteger(Item: string; var Error: Boolean): Integer;
var
L: string;
p: Integer;
begin
Result:=0;
if FindString(Item,L) then
begin
p:=Pos('=',L);
if p>0 then
begin
p:=p+1;
Result:=ReadInt(L,p)
end else
Error:=true
end else
Error:=true
end;
procedure TFileIO.WriteString(Item: string; Value: string);
begin
Writeln(F,Item+'='+Value)
end;
function TFileIO.ReadString(Item: string; var Error: Boolean): string;
var
L: string;
p: Integer;
begin
Result:='';
if FindString(Item,L) then
begin
p:=Pos('=',L);
if p>0 then
begin
p:=p+1;
SkipSpaces(L,p);
Result:=Copy(L,p,1000)
end else
Error:=true
end else
Error:=true
end;
function TFileIO.ReadYesNo(Item: string; var Error: Boolean): Boolean;
var
L: string;
p: Integer;
begin
Result:=false;
if FindString(Item,L) then
begin
p:=Pos('=',L);
if p>0 then
begin
L:=UpperCase(Copy(L,p+1,1000));
Result:=(Pos('Y',L)>0)
end else
Error:=true
end else
Error:=true
end;
function TFileIO.FindString(Item: string; var Line: string): Boolean;
var
q: LongInt;
L: string;
begin
Item:=UpperCase(Item);
repeat
Readln(F,Line);
L:=UpperCase(Line);
q:=Pos(Item,L)
until (q>0) or SeekEof(F);
if q=0 then
begin
CloseFile(F);
AssignFile(F,FileName); { a simple Reset doesn't seem to rewind the file }
Reset(F);
repeat
Readln(F,Line);
L:=UpperCase(Line);
q:=Pos(Item,L)
until (q>0) or SeekEof(F);
end;
Result:=(q>0);
end;
function MSSansSerif8Width: Integer;
begin
Result:=ConfigForm.MSSansSerif82.Width-ConfigForm.MSSansSerif81.Width;
end;
function MSSansSerif8Height: Integer;
begin
Result:=ConfigForm.MSSansSerif81.Height;
end;
function Courier9Width: Integer;
begin
Result:=ConfigForm.Courier92.Width-ConfigForm.Courier91.Width;
end;
function Courier9Height: Integer;
begin
Result:=ConfigForm.Courier91.Height;
end;
function Courier10Width: Integer;
begin
Result:=ConfigForm.Courier102.Width-ConfigForm.Courier101.Width;
end;
function Courier10Height: Integer;
begin
Result:=ConfigForm.Courier101.Height;
end;
constructor TMemoryInterface.Create(ContentType: TContentType);
begin
inherited Create;
CType:=ContentType
end;
function TMemoryInterface.CheckAddress(Address: LongInt; GroupSize: Integer): TErrorSet;
begin
Result:=[];
if ((CType in [ctData,ctBoth]) and
((Address<0) or
(Address>=ConfigForm.DmemSize.Value))) or
((CType=ctCode) and
((Address<0) or
(Address>=ConfigForm.ImemSize.Value))) then
Result:=[erOutOfRange];
if (Address and (1 shl GroupSize -1))<>0 then
Result:=[erNotAligned];
if Result<>[] then
ErrorAddress:=Address
end;
function TMemoryInterface.Read(Address: LongInt; GroupSize: Integer): LongInt;
begin
if not ReportError(CheckAddress(Address, GroupSize)) then
begin
Result:=Content[Address div 4];
case GroupSize of
0: Result:=(Result shr (24-(8*(Address and 3)))) and $FF;
1: Result:=(Result shr (16-(8*(Address and 2)))) and $FFFF
end
end else
Result:=-1
end;
procedure TMemoryInterface.Write(Address, Value: LongInt; GroupSize: Integer);
begin
if not ReportError(CheckAddress(Address, GroupSize)) then
begin
case GroupSize of
0: Content[Address div 4]:= Content[Address div 4]
and (-1-($FF shl (24-(8*(Address and 3)))))
or ((Value and $FF) shl (24-(8*(Address and 3))));
1: Content[Address div 4]:= Content[Address div 4]
and (-1-($FFFF shl (16-(8*(Address and 2)))))
or ((Value and $FFFF) shl (16-(8*(Address and 2))));
2: Content[Address div 4]:=Value;
end
end
end;
procedure TMemoryInterface.ChangedValue(Address: LongInt);
begin
if ((CType=ctCode) or (CType=ctBoth)) and
(Address<ConfigForm.ImemSize.Value)then
ImemForm.ShowSingleAddress(Address);
if ((CType=ctData) or (CType=ctBoth)) and
(Address<ConfigForm.DmemSize.Value) then
DmemForm.ShowSingleAddress(Address);
MemoryTouched
end;
procedure TMemoryInterface.ChangedRegion(BeginRegion, EndRegion: LongInt);
begin
if (CType=ctCode) or (CType=ctBoth) then
ImemForm.ShowRegion(BeginRegion,EndRegion);
if (CType=ctData) or (CType=ctBoth) then
DmemForm.ShowRegion(BeginRegion,EndRegion);
MemoryTouched
end;
procedure TMemoryInterface.ChangedAll;
begin
if (CType=ctCode) or (CType=ctBoth) then
ImemForm.ShowAll;
if (CType=ctData) or (CType=ctBoth) then
DmemForm.ShowAll;
MemoryTouched
end;
procedure TMemoryInterface.MemoryTouched;
begin
if (CType=ctCode) then
begin
PSimulator.Imem.ResetRecentClock;
PSimulator.IR.Evaluate
end
else if (CType=ctData) then
begin
PSimulator.Dmem.ResetRecentClock;
PSimulator.LMDR.Evaluate
end
else
begin
MSimulator.Mem.ResetRecentClock;
MSimulator.MDR.Evaluate;
MSimulator.IR.Evaluate
end
end;
function RandomLongInt: LongInt;
begin
Result:=Random($1000);
Result:=(Result shl 12 + Random($1000)) shl 8 + Random($100)
end;
function MaxRegisterLength(NumRegisters: Integer): Integer;
begin
case NumRegisters of
0..9: Result:=12;
10..99: Result:=13
else
Result:=14
end
end;
function ReportError(Error: TErrorSet): Boolean;
var
Cap: string;
Flag: Word;
begin
Cap:='Error';
Flag:=$30;
if erFuncUnstable in Error then
AppliMessage('Memory function input is unstable',Cap,Flag);
if erMARUnstable in Error then
AppliMessage('Memory address input is unstable',Cap,Flag);
if erMDRUnstable in Error then
AppliMessage('Memory data input is unstable',Cap,Flag);
if erOutOfRange in Error then
AppliMessage('Address 0x'+IntToHex(ErrorAddress,8)+' out of range',Cap,Flag);
if erNotAligned in Error then
AppliMessage('Address 0x'+IntToHex(ErrorAddress,8)+' not aligned',Cap,Flag);
if erBreak in Error then
AppliMessage('Breakpoint condition met','Note',$40);
Result:=(Error <> [])
end;
Function AppliMessage(Msg,Cap: string; Flags: Word): Integer;
var
Buf1,Buf2: PChar;
begin
Buf1:=StrAlloc(Length(Msg)+1);
Buf2:=StrAlloc(Length(Cap)+1);
Result:=Application.MessageBox(StrPCopy(Buf1,Msg),StrPCopy(Buf2,Cap),Flags);
StrDispose(Buf1);
StrDispose(Buf2)
end;
procedure TUnravel.UpdateFields;
begin
opc:=ConfigForm.NumOpcodes.Exponent;
Imm2Size:=32-opc;
reg:=ConfigForm.NumRegisters.Exponent;
regmask:=1 shl reg -1;
Imm1Size:=Imm2Size-2*reg;
imm1mask:=LongInt(1) shl Imm1Size -1;
imm2mask:=LongInt(1) shl Imm2Size -1;
Imm1Width:=(Imm1Size+3) div 4;
Imm2Width:=(Imm2Size+3) div 4
end;
function TUnravel.Opcode(Instr: LongInt): Integer;
begin
Result:=Instr shr Imm2Size
end;