-
Notifications
You must be signed in to change notification settings - Fork 14
/
BlueCheck.bsv
2173 lines (1777 loc) · 63.9 KB
/
BlueCheck.bsv
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
/* BlueCheck 2016-02-01
*
* Copyright 2015 Matthew Naylor
* Copyright 2015 Nirav Dave
* Copyright 2016 Andy Wright
* All rights reserved.
*
* This software was developed by SRI International and the University of
* Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
* ("CTSRD"), as part of the DARPA CRASH research programme.
*
* This software was developed by SRI International and the University of
* Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249
* ("MRC2"), as part of the DARPA MRC research programme.
*
* This software was developed by the University of Cambridge Computer
* Laboratory as part of the Rigorous Engineering of Mainstream
* Systems (REMS) project, funded by EPSRC grant EP/K008528/1.
*
* @BERI_LICENSE_HEADER_START@
*
* Licensed to BERI Open Systems C.I.C. (BERI) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. BERI licenses this
* file to you under the BERI Hardware-Software License, Version 1.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.beri-open-systems.org/legal/license-1-0.txt
*
* Unless required by applicable law or agreed to in writing, Work distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @BERI_LICENSE_HEADER_END@
*/
import ModuleCollect :: *;
import StmtFSM :: *;
import List :: *;
import Clocks :: *;
import FIFOF :: *;
import ConfigReg :: *;
import Vector :: *;
import DReg :: *;
// ============================================================================
// Module parameters
// ============================================================================
typedef struct {
// Display message when a chosen state is a no-op
Bool showNoOp;
// Show the time of each displayed state
Bool showTime;
// Is the wedge-detector enabled?
Bool wedgeDetect;
// Timeout before a wedge is assumed
Integer wedgeTimeout;
// Generate a checker based on an iterative deepening strategy
// (If 'False', a single random state walk is performed)
Bool useIterativeDeepening;
// This must contain valid data if 'useIterativeDeepening' is true
ID_Params id;
// Interactive iterative deepening (simulation only, must be
// disabled for synthesis)
Bool interactive;
// Attempt to shrink a counter example, if one is found
// (This is only valid when 'useIterativeDeepening' is true)
Bool useShrinking;
// Allow recorded counter-examples to be viewed. This is useful
// when testing on FPGA, and when behaviour on FPGA and in simulation
// is not equivalent.
Bool allowViewing;
// Number of testing iterations to perform. For iterative deepening,
// this is the number of times to increase the depth before stopping
// Otherwise, the it's the length of the random state walk
Bit#(32) numIterations;
// For synthesis, we use a FIFO for saving the state
// of the checker, rather than a file on the local filesystem.
Maybe#(FIFOF#(Bit#(8))) outputFIFO;
} BlueCheck_Params;
// Sub-parameters for iterative deepening
typedef struct {
// Iterative deepening requires ability to reset the circuit under test
MakeResetIfc rst;
// The number of states to be explored in a single 'test'
Bit#(32) initialDepth;
// The number of tests to perform at each depth
Bit#(32) testsPerDepth;
// A function to increase the depth
(function Bit#(32) f(Bit#(32) currentDepth)) incDepth;
} ID_Params;
// ============================================================================
// States of the BlueCheck-generated checker
// ============================================================================
// The maximum number of states in the equivalance checker.
// You will get a compile-time error message if this parameter
// is not big enough, but it's not likely, unless you use very
// large method frequencies.
typedef 16 LogMaxStates;
typedef Bit#(LogMaxStates) State;
// The frequency that the checker will move to particular state.
typedef Integer Freq;
// ============================================================================
// Guarded statements
// ============================================================================
// Typedef for stmt with a guard
typedef struct {
Stmt stmt;
Bool guard;
} GuardedStmt;
// Just like when for actions, but adds a guard to a stmt
function GuardedStmt stmtWhen( Bool guard, Stmt stmt );
return GuardedStmt{ stmt: stmt, guard: guard };
endfunction
// ============================================================================
// BlueCheck collection data type
// ============================================================================
// A BlueCheck module implicitly collects various items that allow
// automatic creation of an equivalance checker.
typedef ModuleCollect#(Item) BlueCheck;
typedef ModuleCollect#(Item) Specification;
// BlueCheck modules collect items of the following type.
typedef union tagged {
Tuple3#(Freq, App, Action) ActionItem;
Tuple3#(Freq, App, GuardedStmt) StmtItem;
Tuple2#(Freq, List#(String)) ParItem;
Action GenItem;
List#(PRNG16) PRNGItem;
Tuple2#(Fmt, Bool) InvariantItem;
Tuple2#(Bool, Reg#(Bool)) EnsureItem;
Tuple2#(App, Stmt) PreStmtItem;
Tuple2#(App, Stmt) PostStmtItem;
Classification ClassifyItem;
} Item;
// Functions for extracting items.
function List#(a) single(a x) = Cons(x, Nil);
function List#(Tuple3#(Freq, App, Action)) getActionItem(Item item) =
item matches tagged ActionItem .x ? single(x) : Nil;
function List#(Tuple3#(Freq, App, GuardedStmt)) getStmtItem(Item item) =
item matches tagged StmtItem .x ? single(x) : Nil;
function List#(Tuple2#(Freq, List#(String))) getParItem(Item item) =
item matches tagged ParItem .x ? single(x) : Nil;
function List#(Action) getGenItem(Item item) =
item matches tagged GenItem .x ? single(x) : Nil;
function List#(PRNG16) getPRNGItem(Item item) =
item matches tagged PRNGItem .xs ? xs : Nil;
function List#(Tuple2#(Fmt, Bool)) getInvariantItem(Item item) =
item matches tagged InvariantItem .x ? single(x) : Nil;
function List#(Tuple2#(Bool, Reg#(Bool))) getEnsureItem(Item item) =
item matches tagged EnsureItem .x ? single(x) : Nil;
function List#(Tuple2#(App, Stmt)) getPreStmtItem(Item item) =
item matches tagged PreStmtItem .x ? single(x) : Nil;
function List#(Tuple2#(App, Stmt)) getPostStmtItem(Item item) =
item matches tagged PostStmtItem .x ? single(x) : Nil;
function List#(Classification) getClassifyItem(Item item) =
item matches tagged ClassifyItem .x ? single(x) : Nil;
// ============================================================================
// For displaying function applications
// ============================================================================
typedef struct {
String name;
List#(Fmt) args;
} App;
function String getName(App app) = app.name;
function Fmt formatApp(App app);
if (app.args matches tagged Nil)
return $format("%s", app.name);
else
return ($format("%s", app.name) + fshow("(") +
formatArgs(app.args) + fshow(")"));
endfunction
function Fmt formatArgs(List#(Fmt) args);
if (List::tail(args) matches tagged Nil)
return List::head(args);
else
return (List::head(args) + fshow(",") + formatArgs(List::tail(args)));
endfunction
function App appendArg(App app, Fmt arg) =
App { name: app.name, args: List::append(app.args, Cons(arg, Nil)) };
// ============================================================================
// Psuedo-random number generation
// ============================================================================
// This is a fairly standard 16-bit LCG.
interface PRNG16;
method Action seed(Bit#(32) s);
method Action stall;
method Bit#(32) value;
method Bit#(16) out;
endinterface
module mkPRNG16 (PRNG16);
// State of the generator
Reg#(Bit#(31)) state <- mkReg(0);
// Signals from the methods to the following rule
Wire#(Maybe#(Bit#(32))) seedWire <- mkDWire(Invalid);
// Stall the generator for the current cycle
PulseWire stallWire <- mkPulseWire;
// The rule ('seed' takes priority)
rule step;
if (seedWire matches tagged Valid .s)
state <= s[30:0];
else if (! stallWire)
state <= state*1103515245 + 12345;
endrule
// Set the seed
method Action seed(Bit#(32) s);
seedWire <= tagged Valid s;
endmethod
// Output to use as psuedo-random number
method Bit#(16) out = reverseBits(state[30:15]);
// Obtain the current state
method Bit#(32) value = {0, state};
method Action stall = stallWire.send;
endmodule
// ============================================================================
// Generators
// ============================================================================
// Generate values of a given type.
interface Gen#(type t);
method ActionValue#(t) gen;
endinterface
// The following standard generator works for any type in Bits and
// uses PRNGs to give psuedo-random data.
module [BlueCheck] mkGenDefault (Gen#(t))
provisos (Bits#(t, n));
// Number of 16-bit PRNGs needed.
Integer numPRNGs = (valueOf(n)+15)/16;
// Create as many 16-bit PRNGs as needed.
PRNG16 prngs[numPRNGs];
List#(PRNG16) prnglist = Nil;
for (Integer i = 0; i < numPRNGs; i=i+1) begin
let prng <- mkPRNG16;
prngs[i] = prng;
prnglist = Cons(prng, prnglist);
end
// Expose these PRNGs to BlueCheck (which will seed them).
addToCollection(tagged PRNGItem prnglist);
// Generate a value using the PRNGs.
method ActionValue#(t) gen;
Bit#(n) x = 0;
for (Integer i = 0; i < numPRNGs; i=i+1) begin
x = truncate({x,prngs[i].out});
end
return unpack(x);
endmethod
endmodule
// ============================================================================
// MkGen class (like Arbitrary in QuickCheck)
// ============================================================================
// The MkGen class defines a generator for each type.
typeclass MkGen#(type t);
module [BlueCheck] mkGen (Gen#(t));
endtypeclass
// Standard instances
instance MkGen#(void);
mkGen = mkGenDefault;
endinstance
instance MkGen#(Bool);
mkGen = mkGenDefault;
endinstance
instance MkGen#(Ordering);
mkGen = mkGenDefault;
endinstance
instance MkGen#(Bit#(n));
mkGen = mkGenDefault;
endinstance
instance MkGen#(Int#(n));
mkGen = mkGenDefault;
endinstance
instance MkGen#(UInt#(n));
mkGen = mkGenDefault;
endinstance
instance MkGen#(Maybe#(t)) provisos (MkGen#(t));
module [BlueCheck] mkGen (Gen#(Maybe#(t)));
Gen#(Bool) boolGen <- mkGen;
Gen#(t) tGen <- mkGen;
method ActionValue#(Maybe#(t)) gen;
let v <- boolGen.gen;
let x <- tGen.gen;
return (v ? Valid(x) : Nothing);
endmethod
endmodule
endinstance
instance MkGen#(Vector#(n, t)) provisos (MkGen#(t));
module [BlueCheck] mkGen (Gen#(Vector#(n, t)));
Vector#(n, Gen#(t)) gens <- replicateM(mkGen);
method ActionValue#(Vector#(n, t)) gen;
Vector#(n, t) v;
for (Integer i = 0; i < valueOf(n); i=i+1) begin
let x <- gens[i].gen;
v[i] = x;
end
return v;
endmethod
endmodule
endinstance
instance MkGen#(Tuple2#(a, b))
provisos (MkGen#(a), MkGen#(b));
module [BlueCheck] mkGen (Gen#(Tuple2#(a, b)));
Gen#(a) aGen <- mkGen;
Gen#(b) bGen <- mkGen;
method ActionValue#(Tuple2#(a, b)) gen;
let x <- aGen.gen;
let y <- bGen.gen;
return tuple2(x, y);
endmethod
endmodule
endinstance
instance MkGen#(Tuple3#(a, b, c))
provisos (MkGen#(a), MkGen#(b), MkGen#(c));
module [BlueCheck] mkGen (Gen#(Tuple3#(a, b, c)));
Gen#(a) aGen <- mkGen;
Gen#(b) bGen <- mkGen;
Gen#(c) cGen <- mkGen;
method ActionValue#(Tuple3#(a, b, c)) gen;
let x <- aGen.gen;
let y <- bGen.gen;
let z <- cGen.gen;
return tuple3(x, y, z);
endmethod
endmodule
endinstance
// Default instance. If none of the above instances match a given
// type, we use the following default instance. This instance
// exploits the "overlapping instances" feature and I'm still unsure
// as to whether we actually want it.
instance MkGen#(t)
provisos (Bits#(t, n));
module [BlueCheck] mkGen (Gen#(t));
let gen <- mkGenDefault;
return gen;
endmodule
endinstance
// ============================================================================
// Adding properties
// ============================================================================
// Add a property to be checked.
typeclass Prop#(type a);
module [BlueCheck] addProp#(Freq fr, App app, a f) ();
endtypeclass
// Short-hand for a unit frequency.
module [BlueCheck] prop#(String name, a f) ()
provisos(Prop#(a));
addProp(1, App { name: name, args: Nil}, f);
endmodule
// Short-hand for a specified frequency.
module [BlueCheck] propf#(Freq freq, String name, a f) ()
provisos(Prop#(a));
addProp(freq, App { name: name, args: Nil}, f);
endmodule
// Base case 1: an action.
instance Prop#(Action);
module [BlueCheck] addProp#(Freq freq, App app, Action a) ();
addToCollection(tagged ActionItem (tuple3(freq, app, a)));
endmodule
endinstance
// Base case 2: a statement.
// Without a guard
instance Prop#(Stmt);
module [BlueCheck] addProp#(Freq freq, App app, Stmt s) ();
addToCollection(tagged StmtItem (tuple3(freq, app, stmtWhen(True, s))));
endmodule
endinstance
// With a guard
instance Prop#(GuardedStmt);
module [BlueCheck] addProp#(Freq freq, App app, GuardedStmt s) ();
addToCollection(tagged StmtItem (tuple3(freq, app, s)));
endmodule
endinstance
// Base case 3: a boolean.
instance Prop#(Bool);
module [BlueCheck] addProp#(Freq freq, App app, Bool b) ();
// This wire will relax scheduling constraints in case the boolean came
// from a guarded method
Wire#(Bool) success <- mkDWire(True);
rule check;
success <= b;
endrule
Fmt msg = $format(formatApp(app), "\nProperty does not hold");
addToCollection(tagged InvariantItem (tuple2(msg, success)));
endmodule
endinstance
// Base case 4: an action-value returning a boolean.
instance Prop#(ActionValue#(Bool));
module [BlueCheck] addProp#(Freq freq, App app, ActionValue#(Bool) a) ();
Wire#(Bool) success <- mkDWire(True);
Fmt msg = $format("Property does not hold");
Action act =
action
Bool s <- a;
if (!s) success <= False;
endaction;
addToCollection(tagged ActionItem (tuple3(freq, app, act)));
addToCollection(tagged InvariantItem (tuple2(msg, success)));
endmodule
endinstance
// Base case 5: an action-value returning some other type
instance Prop#(ActionValue#(t));
module [BlueCheck] addProp#(Freq freq, App app, ActionValue#(t) a) ();
Action act = action t s <- a; endaction;
addToCollection(tagged ActionItem (tuple3(freq, app, act)));
endmodule
endinstance
// Recursive case: generate input.
instance Prop#(function b f(a x))
provisos(Prop#(b), Bits#(a, n), MkGen#(a), FShow#(a));
module [BlueCheck] addProp#(Freq freq, App app, function b f(a x))();
Reg#(a) aReg <- mkRegU;
Gen#(a) aRandom <- mkGen;
Action genRandom =
action
let a <- aRandom.gen;
aReg <= a;
endaction;
addToCollection(tagged GenItem genRandom);
addProp(freq, appendArg(app, fshow(aReg)), f(aReg));
endmodule
endinstance
// ============================================================================
// Adding equivalences
// ============================================================================
// Add an equivalence to be checked.
typeclass Equiv#(type a);
module [BlueCheck] addEquiv#(Freq freq, App app, a f, a g) ();
endtypeclass
// Short-hand for a unit frequency.
module [BlueCheck] equiv#(String name, a f, a g) ()
provisos(Equiv#(a));
addEquiv(1, App { name: name, args: Nil}, f, g);
endmodule
// Short-hand for a specified frequency.
module [BlueCheck] equivf#(Freq freq, String name, a f, a g) ()
provisos(Equiv#(a));
addEquiv(freq, App { name: name, args: Nil}, f, g);
endmodule
// Base case 1: two actions.
instance Equiv#(Action);
module [BlueCheck] addEquiv#(Freq fr, App app, Action a, Action b) ();
Action both = action a; b; endaction;
addToCollection(tagged ActionItem (tuple3(fr, app, both)));
endmodule
endinstance
// Base case 2: two statements.
// Neither guarded
instance Equiv#(Stmt);
module [BlueCheck] addEquiv#(Freq fr, App app, Stmt a, Stmt b) ();
Stmt s = par a; b; endpar;
GuardedStmt gs = stmtWhen(True, s);
addToCollection(tagged StmtItem (tuple3(fr, app, gs)));
endmodule
endinstance
// Both guarded
instance Equiv#(GuardedStmt);
module [BlueCheck] addEquiv#(Freq fr, App app, GuardedStmt a,
GuardedStmt b) ();
Stmt s = par a.stmt; b.stmt; endpar;
Bool g = a.guard && b.guard;
GuardedStmt gs = stmtWhen(g, s);
addToCollection(tagged StmtItem (tuple3(fr, app, gs)));
endmodule
endinstance
// Base case 3: two action-values
instance Equiv#(ActionValue#(t))
provisos(Eq#(t), Bits#(t, n), FShow#(t));
module [BlueCheck] addEquiv#(Freq fr, App app
, ActionValue#(t) a
, ActionValue#(t) b) ();
Wire#(Bool) success <- mkDWire(True);
Wire#(t) aWire <- mkDWire(?);
Wire#(t) bWire <- mkDWire(?);
Fmt msg = fshow("Not equal: ") + fshow(aWire)
+ fshow(" versus ") + fshow(bWire);
Action check =
action
t aVal <- a; aWire <= aVal;
t bVal <- b; bWire <= bVal;
if (aVal != bVal) success <= False;
endaction;
addToCollection(tagged ActionItem (tuple3(fr, app, check)));
addToCollection(tagged InvariantItem (tuple2(msg, success)));
endmodule
endinstance
// Recursive case: generate input
instance Equiv#(function b f(a x))
provisos(Equiv#(b), Bits#(a, n), MkGen#(a), FShow#(a));
module [BlueCheck] addEquiv#(Freq freq, App app
, function b f(a x)
, function b g(a y)) ();
Reg#(a) aReg <- mkRegU;
Gen#(a) aRandom <- mkGen;
Action genRandom =
action
let a <- aRandom.gen;
aReg <= a;
endaction;
addToCollection(tagged GenItem genRandom);
addEquiv(freq, appendArg(app, fshow(aReg)), f(aReg), g(aReg));
endmodule
endinstance
// Base case 4 (fall through): check that two values are equal
instance Equiv#(a) provisos(Eq#(a), FShow#(a));
module [BlueCheck] addEquiv#(Freq fr, App app, a x, a y) ();
Wire#(Bool) success <- mkDWire(True);
Fmt fmt = formatApp(app) + fshow(" failed: ")
+ fshow(x) + fshow(" v ") + fshow(y);
rule check;
if (x != y) success <= False;
endrule
addToCollection(tagged InvariantItem (tuple2(fmt, success)));
endmodule
endinstance
// ============================================================================
// Adding pre/post statements
// ============================================================================
// Pre or post statement?
typedef enum { PRE, POST } PreOrPost deriving (Eq);
// Add a property to be checked.
typeclass PrePost#(type a);
module [BlueCheck] addPrePost#(PreOrPost p, App app, a f) ();
endtypeclass
// Short-hand for pre statement.
module [BlueCheck] pre#(String name, a f) ()
provisos(PrePost#(a));
addPrePost(PRE, App { name: name, args: Nil}, f);
endmodule
// Short-hand for post statement.
module [BlueCheck] post#(String name, a f) ()
provisos(PrePost#(a));
addPrePost(POST, App { name: name, args: Nil}, f);
endmodule
// Base case 1: an action.
instance PrePost#(Action);
module [BlueCheck] addPrePost#(PreOrPost p, App app, Action a) ();
Stmt s = seq a; endseq;
addPrePost(p, app, s);
endmodule
endinstance
// Base case 2: a statement.
instance PrePost#(Stmt);
module [BlueCheck] addPrePost#(PreOrPost p, App app, Stmt s) ();
if (p == PRE)
addToCollection(tagged PreStmtItem (tuple2(app, s)));
else
addToCollection(tagged PostStmtItem (tuple2(app, s)));
endmodule
endinstance
// Recursive case: generate input.
instance PrePost#(function b f(a x))
provisos(PrePost#(b), Bits#(a, n), MkGen#(a), FShow#(a));
module [BlueCheck] addPrePost#(PreOrPost p, App app, function b f(a x))();
Reg#(a) aReg <- mkRegU;
Gen#(a) aRandom <- mkGen;
Action genRandom =
action
let a <- aRandom.gen;
aReg <= a;
endaction;
addToCollection(tagged GenItem genRandom);
addPrePost(p, appendArg(app, fshow(aReg)), f(aReg));
endmodule
endinstance
// ============================================================================
// For classifying test data
// ============================================================================
typedef struct {
String name;
Reg#(Bit#(32)) positive;
Reg#(Bit#(32)) total;
} Classification;
typedef (function Action f(Bool cond)) Classifier;
module [BlueCheck] mkClassifier#(String text) (Classifier);
// Create classify function
Reg#(Bit#(32)) positive <- mkReg(0);
Reg#(Bit#(32)) total <- mkReg(0);
Classification c;
c.name = text;
c.positive = positive;
c.total = total;
function Action classifyFunc(Bool cond) =
action total <= total+1; if (cond) positive <= positive+1; endaction;
addToCollection(tagged ClassifyItem c);
return classifyFunc;
endmodule
function Action displayClassifications(List#(Classification) cs) =
action
function Action f(Classification c) =
action
$display("%0d%%", (c.positive*100)/c.total, " ", c.name);
endaction;
let _ <- List::mapM(f, cs);
endaction;
// ============================================================================
// Assertions
// ============================================================================
// 'ensure' functions allow assertions to be made inside actions or
// statements of properties or equivalences.
// Obtain a function to make assertions with.
typedef (function Action f(Bool cond)) Ensure;
module [BlueCheck] getEnsure (Ensure);
// Create ensure function
Wire#(Bool) ok <- mkDWire(True);
Reg#(Bool) showMsg <- mkReg(False);
function Action ensureFunc(Bool cond) = action ok <= cond; endaction;
addToCollection(tagged EnsureItem (tuple2(ok, showMsg)));
return ensureFunc;
endmodule
module [BlueCheck] mkEnsure (Ensure);
Ensure ensure <- getEnsure;
return ensure;
endmodule
// Similar to above, but the assertion function also takes a message
// to be displayed if the assertion fails.
typedef (function Action f(Bool cond, Fmt msg)) EnsureMsg;
module [BlueCheck] getEnsureMsg (EnsureMsg);
// Create ensure function
Wire#(Bool) ok <- mkDWire(True);
Reg#(Bool) showMsg <- mkReg(False);
function Action ensureFunc(Bool cond, Fmt msg) =
action ok <= cond; if (!cond && showMsg) $display(msg); endaction;
addToCollection(tagged EnsureItem (tuple2(ok, showMsg)));
return ensureFunc;
endmodule
// ============================================================================
// Parallel properties
// ============================================================================
// Specify that a list of equivalences/properties can run in parallel.
module [BlueCheck] parallel#(List#(String) names) (Empty);
addToCollection(tagged ParItem (tuple2(1, names)));
endmodule
module [BlueCheck] parallelf#(Freq fr, List#(String) names) (Empty);
addToCollection(tagged ParItem (tuple2(fr, names)));
endmodule
// ============================================================================
// Friendly list construction
// ============================================================================
// The following type-class allows convenient construction of lists, e.g.
//
// List#(String) xs = list("push", "pop", "top");
typeclass MkList#(type a, type b) dependencies (a determines b);
function a mkList(List#(b) acc);
endtypeclass
instance MkList#(List#(a), a);
function List#(a) mkList(List#(a) acc) = List::reverse(acc);
endinstance
instance MkList#(function b f(a elem), a) provisos (MkList#(b, a));
function mkList(acc, elem) = mkList(Cons(elem, acc));
endinstance
function b list() provisos (MkList#(b, a));
return mkList(Nil);
endfunction
// ============================================================================
// Misc functions
// ============================================================================
// Is a list empty?
function Bool isEmpty(List#(a) xs);
if (xs matches tagged Nil) return True; else return False;
endfunction
// Function for assigning a value to a register.
function Action assignReg(t x, Reg#(t) r) = action r <= x; endaction;
// Function to sum a list.
function Integer sum(List#(Integer) xs);
if (xs matches tagged Nil) return 0;
else return (List::head(xs) + sum(List::tail(xs)));
endfunction
// Decide whether or not to display an application.
function Bool shouldDisplay(App app) =
app.name != "" && stringHead(app.name) != "_";
// Sequence a list of statements.
function Stmt seqList(Bool show, List#(Tuple2#(App, Stmt)) xs);
if (xs matches tagged Nil)
return (seq delay(1); endseq);
else begin
let t = List::head(xs);
let app = tpl_1(t);
Stmt s =
seq
action
if (show && shouldDisplay(app)) $display(formatApp(app));
endaction
tpl_2(t);
endseq;
return (seq s; seqList(show, List::tail(xs)); endseq);
end
endfunction
// ============================================================================
// ASCII encoding/decoding
// ============================================================================
// For transferring data over the UART, we encode each 4-bit nibble as
// a ASCII hex digit.
function Bit#(8) hexEncode(Bit#(4) x);
Bit#(8) y = x <= 9 ? 48 : 55;
return y+extend(x);
endfunction
function Bit#(4) hexDecode(Bit#(8) x);
Bit#(8) y = x >= 65 ? 55 : 48;
return (x-y)[3:0];
endfunction
// ============================================================================
// File I/O
// ============================================================================
function Action putNibble(File f, Bit#(4) data) =
action
$fwrite(f, "%c", hexEncode(data));
endaction;
function Action putHalfWord(File f, Bit#(16) data) =
action
$fwrite(f, "%c", hexEncode(data[15:12]));
$fwrite(f, "%c", hexEncode(data[11:8]));
$fwrite(f, "%c", hexEncode(data[7:4]));
$fwrite(f, "%c", hexEncode(data[3:0]));
endaction;
function ActionValue#(Bit#(4)) getNibble(File f) =
actionvalue
int c <- $fgetc(f);
return hexDecode(pack(c)[7:0]);
endactionvalue;
function ActionValue#(Bit#(16)) getHalfWord(File f) =
actionvalue
Bit#(16) x;
int c0 <- $fgetc(f);
int c1 <- $fgetc(f);
int c2 <- $fgetc(f);
int c3 <- $fgetc(f);
x[15:12] = hexDecode(pack(c0)[7:0]);
x[11:8] = hexDecode(pack(c1)[7:0]);
x[7:4] = hexDecode(pack(c2)[7:0]);
x[3:0] = hexDecode(pack(c3)[7:0]);
return x;
endactionvalue;
function Action putWord(File f, Bit#(32) data) =
action
putHalfWord(f, data[31:16]);
putHalfWord(f, data[15:0]);
endaction;
function ActionValue#(Bit#(32)) getWord(File f) =
actionvalue
Bit#(16) x <- getHalfWord(f);
Bit#(16) y <- getHalfWord(f);
return {x, y};
endactionvalue;
// The filename used to store a counter-example on the filesystem.
String filename = "State.txt";
// ============================================================================
// Rotating Queue
// ============================================================================
// Each PRNG has a 'shadow register'. These shadow registers can be
// used to save or restore the state of all the PRNGs. Since we want
// to be able to easily serialise this state, for transfer to a file
// or over a UART, we using the following rotating queue structure.
// A rotating queue is a list of registers with support for:
// * inserting (by rotation) an element at one end
// * reading (by rotation) an element from the other end
// * loading and storing the values of all the registers
interface RotatingQueue#(type t);
method Action put(t in);
method ActionValue#(t) get;
method List#(t) load;
method Action store(List#(t) inputs);
endinterface
module [Module] mkRotatingQueue#(Integer size) (RotatingQueue#(t))
provisos (Bits#(t, n));
// Registers
// ---------
List#(Reg#(t)) regs <- List::replicateM(size, mkRegU);
// Wires
// -----
Wire#(Bool) rotWire <- mkDWire(False);
Wire#(Maybe#(t)) putWire <- mkDWire(Invalid);
Wire#(Bool) loadWire <- mkDWire(False);
List#(Wire#(t)) loadVals <- List::replicateM(size, mkDWire(?));
// Rules
// -----
rule rotate (rotWire || loadWire);
t insert;
if (putWire matches tagged Valid .x)
insert = x;
else
insert = readReg(List::last(regs));
List#(Reg#(t)) rs = regs;
List#(Wire#(t)) vs = loadVals;
for (Integer i = 0; i < size; i=i+1) begin
if (loadWire)
rs[0] <= vs[0];
else
rs[0] <= insert;
insert = rs[0];
rs = List::tail(rs);
vs = List::tail(vs);
end
endrule