-
Notifications
You must be signed in to change notification settings - Fork 0
/
logicalgates.pas
2060 lines (1771 loc) · 61.1 KB
/
logicalgates.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
UNIT logicalGates;
{$mode objfpc}{$H+}
INTERFACE
USES serializationUtil;
CONST WIRE_MAX_WIDTH=16;
TYPE
T_gateType=(gt_notGate,
gt_andGate,
gt_orGate,
gt_xorGate,
gt_nandGate,
gt_norGate,
gt_nxorGate,
gt_input,
gt_output,
gt_compound,
gt_clock,
gt_adapter,
gt_true,
gt_false,
gt_gatedClock,
gt_undeterminedToTrue,
gt_undeterminedToFalse,
gt_ram,
gt_rom,
gt_7segmentDummy);
T_gateTypeSet=array of T_gateType;
T_gateCount=array[T_gateType] of longint;
T_multibitWireRepresentation=(wr_binary,wr_decimal,wr_2complement);
CONST
C_multibitWireRepresentationName:array[T_multibitWireRepresentation] of string=('bin','dec','2cmp');
C_gateDefaultDescription:array [T_gateType] of string=
{gt_notGate} ('Logische Negation'+LineEnding+'Gibt das Gegenteil des Eingangs aus',
{gt_andGate} 'Logisches und'+LineEnding+'Gibt 1 aus, wenn alle Eingänge 1 sind, sonst 0',
{gt_orGate} 'Logisches oder (inklusiv)'+LineEnding+'Gibt 0 aus, wenn alle Eingänge 0 sind, sonst 1',
{gt_xorGate} 'Logisches oder (exklusiv)'+LineEnding+'Gibt 1 aus, wenn eine ungerade Anzahl von Eingängen 1 ist (parity)',
{gt_nandGate} 'Logisches nicht-und'+LineEnding+'Gibt 0 aus, wenn alle Eingänge 1 sind, sonst 1',
{gt_norGate} 'Logisches nicht-oder'+LineEnding+'Gibt 1 aus, wenn alle Eingänge 0 sind, sonst 0',
{gt_nxorGate} 'Logisches nicht-exklusiv-oder'+LineEnding+'Gibt 1 aus, wenn eine gerade Anzahl von Eingängen 1 ist',
{gt_input} 'Eingabe-Baustein',
{gt_output} 'Ausgabe-Baustein',
{gt_compound} '<compound>',
{gt_clock} 'Zeitgeber'+LineEnding+'Ausgangssignal wechselt periodisch'+LineEnding+'Über die Gattereigenschaften kann der Takt als Vielfaches des Simulationstakts gesetzt werden',
{gt_adapter} 'Ein Adapter für unterschiedliche Kabel-Breiten',
{gt_true} 'Konstant 1',
{gt_false} 'Konstant 0',
{gt_gatedClock} 'Geschalter Zeitgeber'+LineEnding+'Ausgangssignal wechselt periodisch falls am Eingang 1 anliegt'+LineEnding+'Über die Gattereigenschaften kann der Takt als Vielfaches des Simulationstakts gesetzt werden',
{gt_un....true} 'Gibt für unbestimmten Eingang 1 aus',
{gt_un...false} 'Gibt für unbestimmten Eingang 0 aus',
'Speicher'+LineEnding+'Schreibt auf fallender Flanke von clk',
'Nur-Lese-Speicher',
'7-Segment-Anzeige');
C_gateTypeName:array[T_gateType] of string=
{gt_notGate} ('not',
{gt_andGate} 'and',
{gt_orGate} 'or',
{gt_xorGate} 'xor',
{gt_nandGate} 'nand',
{gt_norGate} 'nor',
{gt_nxorGate} 'nxor',
{gt_input} 'input',
{gt_output} 'output',
{gt_compound} '<compound>',
{gt_clock} 'clock',
{gt_adapter} 'adapter',
{gt_true} 'constant true',
{gt_false} 'constant false',
{gt_gatedClock} 'gated clock',
{gt_un....true} 'tend to true',
{gt_un...false} 'tend to false',
'RAM',
'ROM',
'7seg');
C_gateTypeCaption:array[T_gateType] of string=
{gt_notGate} ('¬',
{gt_andGate} '&',
{gt_orGate} '≥1',
{gt_xorGate} '=1',
{gt_nandGate} '¬&',
{gt_norGate} '=0',
{gt_nxorGate} '≠1',
{gt_input} 'input',
{gt_output} 'output',
{gt_compound} '<compound>',
{gt_clock} 'clock',
{gt_adapter} 'adapter',
{gt_true} 'constant true',
{gt_false} 'constant false',
{gt_gatedClock} 'gated clock',
{gt_un....true} 'tend to true',
{gt_un...false} 'tend to false',
'RAM',
'ROM',
'7seg');
TYPE
T_triStateValue=(tsv_false,tsv_undetermined,tsv_true);
{ T_ioLocations }
T_ioLocation=record
leftOrRight:boolean;
positionIndex:longint;
ioLabel:string;
end;
T_ioLocations=object
numberOfLeftInputs,
numberOfTopInputs,
numberOfRightOutputs,
numberOfBottomOutputs:longint;
p:array[gt_input..gt_output] of array of T_ioLocation;
PROCEDURE init;
PROCEDURE add(CONST gt:T_gateType; CONST leftOrRight:boolean; CONST pos:longint; CONST lab:string);
end;
T_wireValue=record
bit:array[0..WIRE_MAX_WIDTH-1] of T_triStateValue;
width:byte;
end;
T_wireValueArray=array of T_wireValue;
T_romContents=array of word;
{ T_captionedAndIndexed }
P_abstractGate=^T_abstractGate;
P_captionedAndIndexed=^T_captionedAndIndexed;
T_captionedAndIndexed=object
FUNCTION getCaption:shortstring; virtual; abstract;
FUNCTION getDescription:string; virtual; abstract;
PROCEDURE setCaption(CONST s:string); virtual;
PROCEDURE setDescription(CONST s:string); virtual;
FUNCTION getIndexInPalette:longint; virtual;
FUNCTION isVisualBoard:boolean; virtual;
FUNCTION newGateFromPrototype:P_abstractGate; virtual; abstract;
end;
{ T_abstractGate }
T_abstractGate=object(T_captionedAndIndexed)
public
CONSTRUCTOR create;
DESTRUCTOR destroy; virtual;
PROCEDURE reset; virtual; abstract;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual; abstract;
FUNCTION getCaption:shortstring; virtual;
FUNCTION getDescription:string; virtual;
FUNCTION numberOfInputs :longint; virtual; abstract;
FUNCTION numberOfOutputs:longint; virtual; abstract;
FUNCTION getIoLocations:T_ioLocations; virtual;
FUNCTION inputWidth (CONST index:longint):byte; virtual;
FUNCTION outputWidth(CONST index:longint):byte; virtual;
FUNCTION gateType:T_gateType; virtual; abstract;
FUNCTION simulateStep:boolean; virtual; abstract;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual; abstract;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual; abstract;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual; abstract;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
PROCEDURE countGates(VAR gateCount:T_gateCount); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
FUNCTION behaviorEquals(CONST other:P_abstractGate):boolean; virtual;
FUNCTION newGateFromPrototype:P_abstractGate; virtual;
end;
{ T_constantGate }
P_constantGate=^T_constantGate;
T_constantGate=object(T_abstractGate)
private
c:T_triStateValue;
public
CONSTRUCTOR create(CONST constantTrue:boolean);
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
end;
T_gateConnector=object
gate:P_abstractGate;
index:longint;
FUNCTION getOutputValue:T_wireValue;
FUNCTION setInputValue(CONST v:T_wireValue):boolean;
FUNCTION inputWidth:byte;
FUNCTION outputWidth:byte;
end;
P_notGate=^T_notGate;
T_notGate=object(T_abstractGate)
private
input,output:T_triStateValue;
public
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
end;
P_inputGate=^T_inputGate;
{ T_inputGate }
T_inputGate=object(T_abstractGate)
private
io:T_wireValue;
public
ioLabel:shortstring;
ioIndex:longint;
width:byte;
onLeftOrRightSide:boolean;
positionIndex:longint;
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION getIoLocations:T_ioLocations; virtual;
FUNCTION inputWidth (CONST index:longint):byte; virtual;
FUNCTION outputWidth(CONST index:longint):byte; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
end;
P_outputGate=^T_outputGate;
T_outputGate=object(T_inputGate)
public
CONSTRUCTOR create;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION gateType:T_gateType; virtual;
end;
P_7segmentGate=^T_7segmentGate;
{ T_7segmentGate }
T_7segmentGate=object(T_outputGate)
CONSTRUCTOR create;
FUNCTION gateType:T_gateType; virtual;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
end;
P_adapter=^T_adapter;
T_adapter=object(T_abstractGate)
private
inWidth,outWidth,inCount,outCount:byte;
io:T_wireValue;
public
CONSTRUCTOR create(CONST inW,outW:byte);
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION inputWidth (CONST index:longint):byte; virtual;
FUNCTION outputWidth(CONST index:longint):byte; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
PROCEDURE setIoWidth(CONST inW,outW:byte);
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
end;
P_binaryBaseGate=^T_binaryBaseGate;
T_binaryBaseGate=object(T_abstractGate)
private
input:array[0..WIRE_MAX_WIDTH-1] of T_triStateValue;
output:T_triStateValue;
public
inputCount:byte;
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
end;
P_andGate=^T_andGate;
T_andGate=object(T_binaryBaseGate)
CONSTRUCTOR create;
FUNCTION simulateStep:boolean; virtual;
FUNCTION gateType:T_gateType; virtual;
end;
P_orGate=^T_orGate;
T_orGate=object(T_binaryBaseGate)
CONSTRUCTOR create;
FUNCTION simulateStep:boolean; virtual;
FUNCTION gateType:T_gateType; virtual;
end;
P_xorGate=^T_xorGate;
T_xorGate=object(T_binaryBaseGate)
CONSTRUCTOR create;
FUNCTION simulateStep:boolean; virtual;
FUNCTION gateType:T_gateType; virtual;
end;
P_nandGate=^T_nandGate;
T_nandGate=object(T_binaryBaseGate)
CONSTRUCTOR create;
FUNCTION simulateStep:boolean; virtual;
FUNCTION gateType:T_gateType; virtual;
end;
P_norGate=^T_norGate;
T_norGate=object(T_binaryBaseGate)
CONSTRUCTOR create;
FUNCTION simulateStep:boolean; virtual;
FUNCTION gateType:T_gateType; virtual;
end;
P_nxorGate=^T_nxorGate;
T_nxorGate=object(T_binaryBaseGate)
CONSTRUCTOR create;
FUNCTION simulateStep:boolean; virtual;
FUNCTION gateType:T_gateType; virtual;
end;
P_clock=^T_clock;
T_clock=object(T_abstractGate)
tick:boolean;
interval,counter:longint;
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
end;
P_gatedClock=^T_gatedClock;
T_gatedClock=object(T_clock)
enable:T_triStateValue;
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
end;
P_tendToTrue=^T_tendToTrue;
T_tendToTrue=object(T_abstractGate)
public
input,output:T_wireValue;
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
end;
P_tendToFalse=^T_tendToFalse;
T_tendToFalse=object(T_tendToTrue)
public
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
end;
P_RomGate=^T_romGate;
T_romGate=object(T_abstractGate)
data:T_romContents;
readAdr:T_wireValue;
outValue:T_wireValue;
CONSTRUCTOR create;
DESTRUCTOR destroy; virtual;
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION numberOfOutputs:longint; virtual;
FUNCTION getIoLocations:T_ioLocations; virtual;
FUNCTION inputWidth (CONST index:longint):byte; virtual;
FUNCTION outputWidth(CONST index:longint):byte; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
FUNCTION behaviorEquals(CONST other:P_abstractGate):boolean; virtual;
end;
P_RamGate=^T_ramGate;
{ T_ramGate }
T_ramGate=object(T_romGate)
writeAdr,
dataIn,
clockIn:T_wireValue;
clockWasHigh:dword;
CONSTRUCTOR create;
PROCEDURE reset; virtual;
FUNCTION clone(CONST includeState:boolean):P_abstractGate; virtual;
FUNCTION getCaption:shortstring; virtual;
FUNCTION numberOfInputs :longint; virtual;
FUNCTION getIoLocations:T_ioLocations; virtual;
FUNCTION inputWidth (CONST index:longint):byte; virtual;
FUNCTION outputWidth(CONST index:longint):byte; virtual;
FUNCTION gateType:T_gateType; virtual;
FUNCTION simulateStep:boolean; virtual;
FUNCTION getOutput(CONST index:longint):T_wireValue; virtual;
FUNCTION setInput(CONST index:longint; CONST value:T_wireValue):boolean; virtual;
FUNCTION getInput(CONST index:longint):T_wireValue; virtual;
PROCEDURE writeToStream(VAR stream:T_bufferedOutputStreamWrapper; CONST metaDataOnly:boolean=false); virtual;
PROCEDURE readMetaDataFromStream(VAR stream:T_bufferedInputStreamWrapper); virtual;
FUNCTION equals(CONST other:P_abstractGate):boolean; virtual;
FUNCTION behaviorEquals(CONST other:P_abstractGate):boolean; virtual;
end;
FUNCTION newBaseGate(CONST gateType:T_gateType):P_abstractGate;
OPERATOR =(CONST x,y:T_gateConnector):boolean;
OPERATOR :=(CONST x:T_triStateValue):T_wireValue;
FUNCTION isFullyDefined(CONST w:T_wireValue):boolean;
OPERATOR =(CONST x,y:T_wireValue):boolean;
FUNCTION getBinaryString (CONST wire:T_wireValue):shortstring;
FUNCTION getDecimalString (CONST wire:T_wireValue):shortstring;
FUNCTION get2ComplementString(CONST wire:T_wireValue):shortstring;
FUNCTION getWireString (CONST wire:T_wireValue; CONST mode:T_multibitWireRepresentation):shortstring;
FUNCTION parseWireBin (CONST s:string; CONST width:byte):T_wireValue;
FUNCTION parseWireDecimal (CONST s:string; CONST width:byte):T_wireValue;
FUNCTION parseWire2Complement(CONST s:string; CONST width:byte):T_wireValue;
FUNCTION parseWire (CONST s:string; CONST width:byte; CONST mode:T_multibitWireRepresentation):T_wireValue;
FUNCTION getDecimalValue (CONST wire:T_wireValue; OUT valid:boolean):longint;
FUNCTION get2ComplementValue (CONST wire:T_wireValue; OUT valid:boolean):longint;
FUNCTION gatesTotal(CONST gateCount:T_gateCount):longint;
FUNCTION serialize(CONST wireValue: T_wireValue): qword;
FUNCTION deserialize(n: qword): T_wireValue;
FUNCTION wordToWire16(w:word):T_wireValue;
FUNCTION wire16ToWord(CONST w:T_wireValue):word;
IMPLEMENTATION
USES sysutils;
FUNCTION getBinaryString(CONST wire: T_wireValue): shortstring;
VAR i:longint;
begin
result:='';
for i:=wire.width-1 downto 0 do
case wire.bit[i] of
tsv_true : result+='1';
tsv_false : result+='0';
tsv_undetermined: result+='?';
end;
end;
FUNCTION getDecimalString(CONST wire: T_wireValue): shortstring;
VAR i:longint;
k:int64=0;
begin
for i:=wire.width-1 downto 0 do begin
k:=k shl 1;
case wire.bit[i] of
tsv_true : inc(k);
tsv_false : begin end;
tsv_undetermined: exit('?');
end;
end;
result:=intToStr(k);
end;
FUNCTION getDecimalValue(CONST wire: T_wireValue; OUT valid: boolean): longint;
VAR i:longint;
k:int64=0;
begin
valid:=true;
for i:=wire.width-1 downto 0 do begin
k:=k shl 1;
case wire.bit[i] of
tsv_true : inc(k);
//tsv_false : begin end;
tsv_undetermined: valid:=false;
end;
end;
result:=k;
end;
FUNCTION get2ComplementString(CONST wire: T_wireValue): shortstring;
VAR i:longint;
k:int64=0;
maxVal:int64;
begin
for i:=wire.width-1 downto 0 do begin
k:=k shl 1;
case wire.bit[i] of
tsv_true : inc(k);
tsv_false : begin end;
tsv_undetermined: exit('?');
end;
end;
if (wire.width>1) then begin
maxVal:=1 shl (wire.width-1);
if k>maxVal then k-=maxVal+maxVal;
end;
result:=intToStr(k);
end;
FUNCTION getWireString(CONST wire: T_wireValue; CONST mode: T_multibitWireRepresentation): shortstring;
begin
case mode of
wr_binary : result:=getBinaryString(wire);
wr_decimal : result:=getDecimalString(wire);
wr_2complement: result:=get2ComplementString(wire);
end;
end;
FUNCTION get2ComplementValue(CONST wire: T_wireValue; OUT valid: boolean): longint;
VAR i:longint;
k:int64=0;
maxVal:int64;
begin
valid:=true;
for i:=wire.width-1 downto 0 do begin
k:=k shl 1;
case wire.bit[i] of
tsv_true : inc(k);
tsv_false : begin end;
tsv_undetermined: begin
valid:=false;
exit(0);
end;
end;
end;
if (wire.width>1) then begin
maxVal:=1 shl (wire.width-1);
if k>maxVal then k-=maxVal+maxVal;
end;
result:=k;
end;
FUNCTION serialize(CONST wireValue: T_wireValue): qword;
CONST BYTE_OF:array[T_triStateValue] of byte=(0,1,2);
VAR i:longint;
begin
result:=0;
for i:=wireValue.width-1 downto 0 do
result:=result*3+BYTE_OF[wireValue.bit[i]];
result*=4;
case wireValue.width of
1: result+=0;
4: result+=1;
8: result+=2;
16: result+=3;
else assert(false);
end;
end;
FUNCTION deserialize(n: qword): T_wireValue;
CONST WIRE_VALUE_OF:array[0..2] of T_triStateValue=(tsv_false,tsv_undetermined,tsv_true);
VAR i: integer;
begin
initialize(result);
case byte(n and 3) of
0: result.width:=1;
1: result.width:=4;
2: result.width:=8;
3: result.width:=16;
end;
n:=n div 4;
for i:=0 to result.width-1 do begin
result.bit[i]:=WIRE_VALUE_OF[n mod 3];
n:=n div 3;
end;
end;
FUNCTION wordToWire16(w: word): T_wireValue;
VAR i:longint;
begin
result.width:=16;
for i:=0 to 15 do begin
if odd(w) then result.bit[i]:=tsv_true
else result.bit[i]:=tsv_false;
w:=w shr 1;
end;
end;
FUNCTION wire16ToWord(CONST w: T_wireValue): word;
VAR i:longint;
begin
result:=0;
for i:=15 downto 0 do begin
result:=result shl 1;
if w.bit[i]=tsv_true then inc(result);
end;
end;
FUNCTION gatesTotal(CONST gateCount: T_gateCount): longint;
VAR gt:T_gateType;
begin
result:=0;
for gt in T_gateType do result+=gateCount[gt];
end;
FUNCTION parseWireBin(CONST s: string; CONST width: byte): T_wireValue;
VAR i:longint;
k:longint;
begin
result.width:=width;
for i:=0 to WIRE_MAX_WIDTH-1 do result.bit[i]:=tsv_false;
k:=length(s)-1;
if k>=WIRE_MAX_WIDTH then k:=WIRE_MAX_WIDTH-1;
for i:=0 to k do case s[length(s)-i] of
'1': result.bit[i]:=tsv_true;
'0': result.bit[i]:=tsv_false;
end;
end;
FUNCTION parseWireDecimal(CONST s: string; CONST width: byte): T_wireValue;
VAR n:int64;
i:longint;
begin
result.width:=width;
n:=StrToInt64Def(s,-1);
if n<0 then n:=0;
for i:=0 to WIRE_MAX_WIDTH-1 do begin
if odd(n) then result.bit[i]:=tsv_true
else result.bit[i]:=tsv_false;
n:=n shr 1;
end;
end;
FUNCTION parseWire2Complement(CONST s: string; CONST width: byte): T_wireValue;
VAR n:int64;
i:longint;
maxVal:int64;
begin
result.width:=width;
n:=StrToInt64Def(s,maxLongint+1);
if (n>maxLongint) or (n<-65536) then n:=0;
if (width>1) then begin
maxVal:=1 shl width;
while (n<0) do n+=maxVal;
end;
for i:=0 to WIRE_MAX_WIDTH-1 do begin
if odd(n) then result.bit[i]:=tsv_true
else result.bit[i]:=tsv_false;
n:=n shr 1;
end;
end;
FUNCTION parseWire(CONST s: string; CONST width: byte; CONST mode: T_multibitWireRepresentation): T_wireValue;
begin
initialize(result);
case mode of
wr_binary : result:=parseWireBin(s,width);
wr_decimal : result:=parseWireDecimal(s,width);
wr_2complement: result:=parseWire2Complement(s,width);
end;
end;
OPERATOR=(CONST x, y: T_gateConnector): boolean;
begin
result:=(x.gate=y.gate) and (x.index=y.index);
end;
FUNCTION newBaseGate(CONST gateType: T_gateType): P_abstractGate;
begin
case gateType of
gt_notGate : new(P_notGate (result),create);
gt_andGate : new(P_andGate (result),create);
gt_orGate : new(P_orGate (result),create);
gt_xorGate : new(P_xorGate (result),create);
gt_nandGate: new(P_nandGate (result),create);
gt_norGate : new(P_norGate (result),create);
gt_nxorGate: new(P_nxorGate (result),create);
gt_input : new(P_inputGate (result),create);
gt_output : new(P_outputGate(result),create);
gt_clock : new(P_clock (result),create);
gt_adapter : new(P_adapter(result),create(4,1));
gt_true :new(P_constantGate(result),create(true ));
gt_false:new(P_constantGate(result),create(false));
gt_gatedClock: new(P_gatedClock(result),create);
gt_undeterminedToFalse: new(P_tendToFalse(result),create);
gt_undeterminedToTrue : new(P_tendToTrue (result),create);
gt_ram: new(P_RamGate(result),create);
gt_rom: new(P_RomGate(result),create);
gt_7segmentDummy: new(P_7segmentGate(result),create);
else result:=nil;
end;
if result<>nil then result^.reset;
end;
{ T_7segmentGate }
CONSTRUCTOR T_7segmentGate.create;
begin
inherited;
io:=tsv_false; ioLabel:=''; width:=8; onLeftOrRightSide:=false; positionIndex:=0;
end;
FUNCTION T_7segmentGate.gateType: T_gateType;
begin
result:=gt_7segmentDummy;
end;
PROCEDURE T_7segmentGate.writeToStream(VAR stream: T_bufferedOutputStreamWrapper; CONST metaDataOnly: boolean);
begin
if not(metaDataOnly) then stream.writeByte(byte(gateType));
end;
PROCEDURE T_7segmentGate.readMetaDataFromStream(VAR stream: T_bufferedInputStreamWrapper);
begin
end;
FUNCTION T_7segmentGate.equals(CONST other: P_abstractGate): boolean;
begin
result:=other^.gateType=gateType;
end;
FUNCTION T_7segmentGate.clone(CONST includeState: boolean): P_abstractGate;
begin
new(P_7segmentGate(result),create);
end;
{ T_ramGate }
CONSTRUCTOR T_ramGate.create;
begin
inherited;
end;
PROCEDURE T_ramGate.reset;
VAR i:longint;
begin
inherited;
writeAdr.width:=16;
dataIn .width:=16;
for i:=0 to WIRE_MAX_WIDTH-1 do writeAdr.bit[i]:=tsv_undetermined;
clockIn.width:=1;
clockIn.bit[0]:=tsv_undetermined;
setLength(data,0);
clockWasHigh:=1431655765;
end;
FUNCTION T_ramGate.clone(CONST includeState: boolean): P_abstractGate;
VAR i:longint;
begin
new(P_RamGate(result),create);
setLength(P_RamGate(result)^.data,length(data));
for i:=0 to length(data)-1 do P_RomGate(result)^.data[i]:=data[i];
P_RomGate(result)^.readAdr:=readAdr;
end;
FUNCTION T_ramGate.getCaption: shortstring;
begin
result:='RAM';
end;
FUNCTION T_ramGate.numberOfInputs: longint;
begin
result:=4;
end;
FUNCTION T_ramGate.getIoLocations: T_ioLocations;
begin
result.init;
result.add(gt_input,true,0,'readAdr');
result.add(gt_input,true,1,'writeAdr');
result.add(gt_input,true,2,'data');
result.add(gt_input,true,3,'clk');
result.add(gt_output,true,0,'data');
end;
FUNCTION T_ramGate.inputWidth(CONST index: longint): byte;
begin
if index=3 then result:=1 else result:=16;
end;
FUNCTION T_ramGate.outputWidth(CONST index: longint): byte;
begin
result:=16;
end;
FUNCTION T_ramGate.gateType: T_gateType;
begin
result:=gt_ram;
end;
CONST zero:T_wireValue=(bit:(tsv_false,tsv_false,tsv_false,tsv_false,
tsv_false,tsv_false,tsv_false,tsv_false,
tsv_false,tsv_false,tsv_false,tsv_false,
tsv_false,tsv_false,tsv_false,tsv_false); width:16);
FUNCTION T_ramGate.simulateStep: boolean;
VAR writeIndex:longint;
i0,i:longint;
begin
if (clockWasHigh=4294967295) and (clockIn.bit[0]=tsv_false) and isFullyDefined(dataIn) then begin
writeIndex:=getDecimalValue(writeAdr,result);
result:=result and (writeIndex>=0) and (writeIndex<=65535);
if result then begin
i0:=length(data);
if writeIndex>=i0 then begin
setLength(data,writeIndex+1);
for i:=i0 to writeIndex-1 do data[i]:=0;
end;
data[writeIndex]:=wire16ToWord(dataIn);
end;
end else result:=(clockWasHigh<>4294967295) and (clockWasHigh<>0);
clockWasHigh:=clockWasHigh shl 1;
if clockIn.bit[0]=tsv_true then clockWasHigh:=clockWasHigh or 1;
if inherited then result:=true;
end;
FUNCTION T_ramGate.getOutput(CONST index: longint): T_wireValue;
begin
result:=inherited;
end;
FUNCTION T_ramGate.setInput(CONST index: longint; CONST value: T_wireValue): boolean;
begin
if (index<0) or (index>3) then result:=false
else case byte(index) of
0: begin
result:=readAdr<>value;
readAdr:=value;
end;
1: begin
result:=writeAdr<>value;
writeAdr:=value;
end;
2: begin
result:=dataIn<>value;
dataIn:=value;
end;
3: begin
result:=clockIn<>value;
clockIn:=value;
end;
end;
end;
FUNCTION T_ramGate.getInput(CONST index: longint): T_wireValue;
begin
case byte(index) of
0: result:=readAdr;
1: result:=writeAdr;
2: result:=dataIn;
3: result:=clockIn;
end;
end;
PROCEDURE T_ramGate.writeToStream(VAR stream: T_bufferedOutputStreamWrapper; CONST metaDataOnly: boolean);
begin
if not(metaDataOnly) then stream.writeByte(byte(gateType));
end;
PROCEDURE T_ramGate.readMetaDataFromStream( VAR stream: T_bufferedInputStreamWrapper);
begin
//Mo meta data
end;
FUNCTION T_ramGate.equals(CONST other: P_abstractGate): boolean;
begin
result:=other^.gateType=gateType;
end;
FUNCTION T_ramGate.behaviorEquals(CONST other: P_abstractGate): boolean;
begin
result:=other^.gateType=gateType;
end;
{ T_romGate }
CONSTRUCTOR T_romGate.create;
begin
inherited;
initialize(data);
reset;
end;
DESTRUCTOR T_romGate.destroy;
begin
setLength(data,0);
inherited;
end;
PROCEDURE T_romGate.reset;
VAR i:byte;
begin
readAdr.width:=16;
for i:=0 to WIRE_MAX_WIDTH-1 do readAdr.bit[i]:=tsv_undetermined;
end;
FUNCTION T_romGate.clone(CONST includeState: boolean): P_abstractGate;
VAR i:longint;
begin
new(P_RomGate(result),create);
setLength(P_RomGate(result)^.data,length(data));
for i:=0 to length(data)-1 do P_RomGate(result)^.data[i]:=data[i];
P_RomGate(result)^.readAdr:=readAdr;
end;
FUNCTION T_romGate.getCaption: shortstring;
begin
result:='ROM';
end;
FUNCTION T_romGate.numberOfInputs: longint;
begin
result:=1;
end;
FUNCTION T_romGate.numberOfOutputs: longint;
begin
result:=1;
end;
FUNCTION T_romGate.getIoLocations: T_ioLocations;
begin
result.init;
result.add(gt_input,true,0,'readAdr');
result.add(gt_output,true,0,'data');
end;