-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBan.java
More file actions
1189 lines (1036 loc) · 54.7 KB
/
Ban.java
File metadata and controls
1189 lines (1036 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
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
//------------------------------------------------------------------------------
// Basic Array Machine with one register
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2025
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Btree in a block on the surface of a silicon chip.
import java.util.*;
abstract class Ban extends Test // Layout the basic array machine and execute a program in it
{int size = 0; // The size of the memory used by the basic array machine
Stack <Array> order = new Stack<>(); // The order in which the arrays were defined
TreeMap<String,Array> arrays = new TreeMap<>(); // A number of different sized arrays concatenated to make one array
TreeMap<Integer, Stack<Array>> overlaid = new TreeMap<>(); // Given a memory location, produce the arrays which overlay it
Stack<I> code = new Stack<>(); // Code of the program
Stack<Label> labels = new Stack<>(); // Labels for some instructions
int [] memory; // The concatenation of all the arrays
int maxTime = 1_000_000; // Maximum number of steps permitted while running the program
int step = 0; // Execution step
int time = 0; // Execution time
boolean running = false; // Executing if true
boolean debug = false; // Debug if true
Debug debugStep = null; // Create a debug class here to print the execution of each statement
int intermediateValue; // Written by get and used by set if no value has been supplied
Ban() // Create a basic array machine
{load(); // Load array definitions
memory = new int[size]; // Create the memory for the basic array machine
}
Ban exec() // Fork a basic array machine copying memory but not the code
{final Ban source = this, target = new Ban() {void load(){};};
target.order = source.order;
target.arrays = source.arrays;
target.overlaid = source.overlaid;
final int M = source.memory.length;
for (int i = 0; i < M; i++) target.memory[i] = source.memory[i];
return target;
}
Ban thread() // Fork a basic array machine using the same memory but not the code
{final Ban source = this, target = new Ban() {void load(){};};
target.order = source.order;
target.arrays = source.arrays;
target.memory = source.memory;
target.overlaid = source.overlaid;
return target;
}
void load() {} // Override this method to define the layout
private void wantRunning () {if (!running) stop("Too early: not running yet");} // This operation can only occur when we are running
private void wantCompiling() {if ( running) stop("Too late: not compiling");} // This operation can only occur when we are compiling
class Debug {void debug() {}} // Debug each step of the execution
class Array // Define an array in the basic array machine
{final String name; // Name of the array
final int[]dimensions; // Dimensions of the array
int base; // Base position of this array
final int length; // Total number of elements in this array
boolean quiet = false; // Do not print this field
Array(String Name, int...Dimensions) // Array definition within the basic array machine
{wantCompiling();
name = Name;
final int D = Dimensions.length;
if (D > 2) stop("Too many dimensions:", D); // Too many dimensions
dimensions = new int[D];
for (int i = 0; i < D; i++) dimensions[D-1-i] = Dimensions[i]; // Save the dimensions with first dimension varying slowest like a number. A zero dimension array is a field.
base = size;
size += length = switch(D) // Number of elements in this array
{case 0 -> 1;
case 1 -> dimensions[0];
default -> dimensions[1] * dimensions[0];
};
order.push(this); // The definition order
if (arrays.containsKey(name))
{stop("Name :", name, "has already been defined");
}
arrays.put(name, this); // Make the array accessible by name
for (int i = 0; i < length; i++) // Record which array occupies each index
{final int p = base+i; // Index occupied by the array
if (!overlaid.containsKey(p)) overlaid.put(p, new Stack<Array>()); // New location overlaid by at least one array
overlaid.get(base+i).push(this); // Record which arrays occupy each index
}
}
public String toString() // Print an array
{final StringBuilder s = new StringBuilder();
switch(dimensions.length)
{case 0: return "variable:"+name;
case 1: return "array["+dimensions[0]+"]"+name;
default: return "array["+dimensions[1]+","+dimensions[0]+"]"+name;
}
}
}
protected Array array(String Name, int...Dimensions) // Array definition within the basic array machine
{return new Array(Name, Dimensions);
}
protected Array overlay(String Name, int...Dimensions) // Overlay this array definition over the previous one
{wantCompiling();
if (order.size() == 0) stop("No previous array to overlay"); // Nothing to overlay
final Array p = order.lastElement(); // Previous array which we will overlay
size = p.base; // Overlay on previous array
final Array q = array(Name, Dimensions); // New array
size = size - q.length + max(p.length, q.length); // Account for overall size
return q; // Return overlay
}
Array getArray(String Name) // Get an array by name or complain if the name is invalid
{final Array A = arrays.get(Name);
if (A == null) stop("No such array as", Name);
return A;
}
String getArrays(int i) // Print the details of the arrays that overlay the specified location
{final Stack<Array> A = overlaid.get(i);
if (A == null) return null;
final Stack<String> s = new Stack<>();
for (Array a: A) // Each array overlaid on this location
{final int d = i - a.base;
switch(a.dimensions.length)
{case 0: s.push(a.name); continue; // Variable
case 1: s.push(a.name+"["+d+"]"); continue; // One dimensional array
default: // Two dimension
final int n = a.dimensions[0], x = d / n, y = d % n;
s.push(a.name+"["+x+","+y+"]");
}
}
return joinStrings(s, ", ");
}
private int lookUpIndex(Array Array, int Dimension, String Index) // Look up an index which will be used in the specified dimension of the specified array
{int v; // The value of the index
try
{v = Integer.parseInt(Index); // The integer has been supplied directly as a string that we can parse
}
catch (NumberFormatException e) // The index is a reference to an array
{final Array I = getArray(Index); // Hopefully the index is a field - a zero dimensional array
if (I.dimensions.length > 0) // Complain if the index is not a field
{stop("Index:", Index, "cannot itself be be an indexed array element");
}
v = memory[I.base]; // The value of the index
}
if (v < 0) stop("Index:", Index, "is negative");
if (v >= Array.dimensions[Dimension]) stop("Index:", Index, "value", v, "is greater than dimension["+Dimension+"] with extent:", Array.dimensions[Dimension], "in array:", Array.name);
return v;
}
int i() {return intermediateValue;} // Return intermediate value making it read only
void setMemory(int Value, String target, String...Indices) // Set the value of an array element to a specified value or the current value of the intermediate value outside an instruction
{wantCompiling();
final Array t = getArray(target);
final String[]i = Indices;
final int I = i.length;
final int T = t.dimensions.length;
if (T != I) stop("Wrong number of dimensions:", T, "!=", I, "for array:", target);
final int v = switch(T)
{case 0 -> memory[t.base] = Value;
case 1 -> memory[t.base+lookUpIndex(t, 0, i[0])] = Value;
default -> memory[t.base+lookUpIndex(t, 1, i[0])*t.dimensions[0]+lookUpIndex(t, 0, i[1])] = Value;
};
}
String variableName(String target, String...Indices) // The name of a variable
{final Array t = getArray(target);
final int T = t.dimensions.length;
switch(T)
{case 0: return target;
case 1: return target+"["+Indices[0]+"]";
default: return target+"["+Indices[1]+","+Indices[0]+"]";
}
}
String getMemoryName(String source, String...Indices) // Get the name of an array element in memory
{try
{final int a = Integer.parseInt(source); // The array name is an integer
return "memory["+a+"]";
}
catch (NumberFormatException e) {} // The index is a reference to an array
final Array s = getArray(source);
final String[]i = Indices;
final int I = i.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
final String n = variableName(source, Indices);
switch(S)
{case 0: return "memory["+(s.base )+"]/*"+n+"*/";
case 1: return "memory["+(s.base+"+"+getMemoryName(Indices[0]) )+"]/*"+n+"*/";
default: return "memory["+(s.base+"+"+getMemoryName(Indices[0])+"*"+s.dimensions[0]+"+"+getMemoryName(Indices[1]))+"]/*"+n+"*/";
}
}
void set(Integer Value, String target, String...Indices) // Set the value of an array element to a specified value or the current value of the intermediate value
{wantCompiling();
final Array t = getArray(target);
final String[]i = Indices;
final int I = i.length;
final int T = t.dimensions.length;
if (T != I) stop("Wrong number of dimensions:", T, "!=", I, "for array:", target);
new I() // Assign value to the indicated array element
{void a()
{final int v = Value != null ? Value : intermediateValue;
final int V = switch(T)
{case 0 -> memory[t.base] = v;
case 1 -> memory[t.base+lookUpIndex(t, 0, i[0])] = v;
default -> memory[t.base+lookUpIndex(t, 1, i[0])*t.dimensions[0]+lookUpIndex(t, 0, i[1])] = v;
};
}
String v()
{final String v = Value != null ? ""+Value : "intermediateValue";
final String n = getMemoryName(target, Indices);
switch(T)
{case 0: return n+" <= " + v + "; /* set 1 */ step <= step + 1;";
case 1: return n+" <= " + v + "; /* set 2 */ step <= step + 1;";
default: return n+" <= " + v + "; /* set 3 */ step <= step + 1;";
}
}
};
}
void set(String target, String...Indices) // Set the value of an array element from the intermediate value
{set(null, target, Indices);
}
void loadArray(String target, int...values) // Load an array
{wantCompiling();
final Array t = getArray(target);
final int N = values.length;
if (N > t.length) stop("Too many initializers for array:", t.name, "with length", t.length);
for (int i = 0; i < N; i++) memory[t.base+i] = values[i];
}
void get(String source, String...Indices) // Get the value of an array element
{wantCompiling();
final Array s = getArray(source);
final String[]i = Indices;
final int I = i.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
new I()
{void a()
{intermediateValue = switch(S)
{case 0 -> memory[s.base];
case 1 -> memory[s.base+lookUpIndex(s, 0, i[0])];
default -> memory[s.base+lookUpIndex(s, 1, i[0])*s.dimensions[0]+lookUpIndex(s, 0, i[1])];
};
}
String v()
{final String n = getMemoryName(source, Indices);
switch(S)
{case 0: return "intermediateValue <= "+n+"; /* get 1 */ step <= step + 1;";
case 1: return "intermediateValue <= "+n+"; /* get 2 */ step <= step + 1;";
default: return "intermediateValue <= "+n+"; /* get 3 */ step <= step + 1;";
}
}
};
}
int getMemory(String source, String...Indices) // Get the value of an array element from memory
{final Array s = getArray(source);
final String[]i = Indices;
final int I = i.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
return switch(S)
{case 0 -> memory[s.base];
case 1 -> memory[s.base+lookUpIndex(s, 0, i[0])];
default -> memory[s.base+lookUpIndex(s, 1, i[0])*s.dimensions[0]+lookUpIndex(s, 0, i[1])];
};
}
void clear(int Value, String target, String...Indices) // Clear the indicated part of the specified array to the the specified value presumed to be a constant, typically -1 or 0
{wantCompiling();
final Array t = getArray(target);
final String[]i = Indices;
final int I = i.length;
final int T = t.dimensions.length;
if (I > T) stop("Too many dimensions:", I, ">", T, "for array:", target);
if (I == 0) // Clear entire array
{for (int j = 0; j < t.length; j++)
{final int J = j;
new I()
{void a() {memory[t.base + J] = Value;}
String v() {return "memory["+(t.base + J)+"] <= "+Value+"; /* clear 1 */ step <= step + 1;";}
};
}
return;
}
if ((I == 1 && T == 1) || (I == 2 && T == 2)) // Clear single element of one or two dimensional array
{set(Value, target, Indices);
return;
}
if (I == 1 && T == 2) // Clear indicated sub array
{final int l = t.dimensions[0]; // Size of sub array
for (int j = 0; j < l; j++) // Clear sub array in two dimensional array
{final int J = j;
new I()
{void a() { memory[ t.base + l * lookUpIndex(t, 1, i[0]) + J ] = Value;}
String v() {return "memory["+(t.base + l * lookUpIndex(t, 1, i[0]) + J)+"] <= "+Value+"; /* clear 2 */ step <= step + 1;";}
};
}
return;
}
}
void zero(String source, String...Indices) {clear( 0, source, Indices);} // Zero the indicated part of the specified array
void ones(String source, String...Indices) {clear(-1, source, Indices);} // Set the indicated part of the specified array to all ones
void move(String target, String source, String...Indices) // Copy the indexed source field to the indexed target fields
{wantCompiling();
final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I, "for arrays:", target+",", source);
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
get(source, si);
set(target, ti);
}
void compare(int immediate, String source, String...Indices) // Compare the indexed source field to the immediate value setting the intermediate value to -1 if is less that source, 0 if equal otherwise +1
{wantCompiling();
final Array s = getArray(source);
final int I = Indices.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
get(source, Indices);
new I()
{void a()
{final int a = immediate;
final int b = intermediateValue;
intermediateValue = Integer.valueOf(b).compareTo(a);
}
String v()
{final String a = ""+immediate;
final String b = "intermediateValue";
return "intermediateValue <= "+a+" < intermediateValue ? -1 : "+a+" == intermediateValue ? 0 : +1; /* compare 1 */ step <= step + 1;";
}
};
}
void compare(String target, String source, String...Indices) // Compare the indexed source fields setting the intermediate value to -1 if source is less that target, 0 if equal otherwise +1
{wantCompiling();
final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I); // Check we have the right number of indices
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
new I()
{void a()
{final int b = getMemory(source, si);
final int a = getMemory(target, ti);
intermediateValue = Integer.valueOf(a).compareTo(b);
}
String v()
{final String b = getMemoryName(source, si);
final String a = getMemoryName(target, ti);
return "intermediateValue <= "+a+" < "+b+" ? -1 : "+a+" == "+b+" ? 0 : +1; /* compare 2 */ step <= step + 1;";
}
};
}
void gt() {new I() {void a(){intermediateValue = intermediateValue > 0 ? 1 : 0;} String v() {return "intermediateValue <= intermediateValue > 0 ? 1 : 0; /* gt */ step <= step + 1;";}};} // Set the intermediate value to one if it is currently greater than zero else zero
void ge() {new I() {void a(){intermediateValue = intermediateValue >= 0 ? 1 : 0;} String v() {return "intermediateValue <= intermediateValue >= 0 ? 1 : 0; /* ge */ step <= step + 1;";}};} // Set the intermediate value to one if it is currently greater than or equal to zero else zero
void lt() {new I() {void a(){intermediateValue = intermediateValue < 0 ? 1 : 0;} String v() {return "intermediateValue <= intermediateValue < 0 ? 1 : 0; /* lt */ step <= step + 1;";}};} // Set the intermediate value to one if it is currently less than zero else zero
void le() {new I() {void a(){intermediateValue = intermediateValue <= 0 ? 1 : 0;} String v() {return "intermediateValue <= intermediateValue <= 0 ? 1 : 0; /* le */ step <= step + 1;";}};} // Set the intermediate value to one if it is currently less than or equal to zero else zero
void eq() {new I() {void a(){intermediateValue = intermediateValue == 0 ? 1 : 0;} String v() {return "intermediateValue <= intermediateValue == 0 ? 1 : 0; /* eq */ step <= step + 1;";}};} // Set the intermediate value to one if it is currently equal to zero else zero
void ne() {new I() {void a(){intermediateValue = intermediateValue != 0 ? 1 : 0;} String v() {return "intermediateValue <= intermediateValue != 0 ? 1 : 0; /* ne */ step <= step + 1;";}};} // Set the intermediate value to one if it is currently less not equal to zero else zero
void add(int immediate, String source, String...Indices) // Add a constant value to the source field
{wantCompiling();
final Array s = getArray(source);
final int I = Indices.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
get(source, Indices);
new I()
{void a() { intermediateValue = immediate + intermediateValue;}
String v() {return "intermediateValue <= "+immediate+" + intermediateValue; /* add 1 */ step <= step + 1;";}
};
set(source, Indices);
}
void add(String target, String source, String...Indices) // Add the source to the target replacing the target
{wantCompiling();
final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I); // Check we have the right number of indices
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
new I()
{void a() { intermediateValue = getMemory (source, si) + getMemory (target, ti);}
String v() {return "intermediateValue <= "+getMemoryName(source, si)+" + "+getMemoryName(target, ti)+"; /* add2 */ step <= step + 1;";}
};
set(target, ti);
}
void inc(String source, String...Indices) {add(+1, source, Indices);} // Increment a field
void dec(String source, String...Indices) {add(-1, source, Indices);} // Increment a field
void subtract(String target, String source, String...Indices) // Subtract the source from the target and replace the target
{wantCompiling();
final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I); // Check we have the right number of indices
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
new I()
{void a() { intermediateValue = getMemory (target, ti) - getMemory (source, si); }
String v() {return "intermediateValue <= "+getMemoryName(target, ti)+" - "+getMemoryName(source, si)+"; /* subtract */ step <= step + 1;";}
};
set(target, ti);
}
void multiply(String target, String source, String...Indices) // Multiply the target by the source and replace the target
{wantCompiling();
final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I); // Check we have the right number of indices
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
new I()
{void a() { intermediateValue = getMemory (source, si) * getMemory (target, ti); }
String v() {return "intermediateValue <= "+getMemoryName(target, ti)+" * "+getMemoryName(source, si)+"; /* multiply */ step <= step + 1;";}
};
set(target, ti);
}
void modulus(String target, String source, String...Indices) // Save the target modulus the source into the source
{wantCompiling();
final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I); // Check we have the right number of indices
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
new I()
{void a() { intermediateValue = getMemory (source, si) % getMemory (target, ti); }
String v() {return "intermediateValue <= "+getMemoryName(target, ti)+" % "+getMemoryName(source, si)+"; /* modulus */ step <= step + 1;";}
};
set(target, ti);
}
void shiftRight(String source, String...Indices) // Shift the source by one place to the right inserti g a one if the number os neagtive else a zero
{wantCompiling();
final Array s = getArray(source);
final int I = Indices.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I); // Check we have the right number of indices
get(source, Indices);
new I()
{void a() { intermediateValue = intermediateValue >> 1; }
String v() {return "intermediateValue <= intermediateValue >> 1; /* shift right */ step <= step + 1;";}
};
set(source, Indices);
}
//D1 Print // Print a layout
public String toString() // Print layout supressing zero entries
{wantCompiling();
final StringBuilder s = new StringBuilder();
int l = 0;
for (Array a: order)
{if (a.quiet) continue;
if (a.dimensions.length == 0)
{ final int v = memory[a.base];
if (v == 0) continue;
s.append(String.format("%4d %4d %4d %s %s\n", l++, a.base, memory[a.base], a.name, "-"));
}
else if (a.dimensions.length == 1)
{for (int i = 0; i < a.dimensions[0]; i++)
{ final int v = memory[a.base+i];
if (v == 0) continue;
s.append(String.format("%4d %4d %4d %4d %s %s\n", l++, a.base, i, memory[a.base+i], a.name, i == 0 ? "-" : "|"));
}
}
else if (a.dimensions.length == 2)
{for (int j = 0; j < a.dimensions[1]; j++)
{for (int i = 0; i < a.dimensions[0]; i++)
{final int v = memory[a.base+j*a.dimensions[0]+i];
if (v == 0) continue;
s.append(String.format("%4d %4d %4d %4d %4d %s %s\n", l++, a.base, j, i, memory[a.base+j*a.dimensions[0]+i], a.name, i == 0 && j == 0 ? "-" : i == 0 ? "+" : "|"));
}
}
}
}
return ""+s;
}
String print(String Source, String...Indices) // Print the indicated part of the array
{wantCompiling();
final Array s = getArray(Source);
final String[]i = Indices;
final StringBuilder t = new StringBuilder();
final int I = i.length;
final int S = s.dimensions.length;
if (I > S) stop("Too many dimensions:", I, ">", S, "for array:", Source);
if (I == 0) // Print entire array
{if (s.dimensions.length == 0)
{return ""+getMemory(Source, Indices);
}
if (s.dimensions.length == 1)
{for (int j = 0; j < s.length; j++) t.append(""+memory[s.base + j]+", ");
t.setLength(t.length()-2);
return ""+t;
}
for (int k = 0; k < s.dimensions[1]; k++)
{t.append("[");
for (int j = 0; j < s.dimensions[0]; j++)
{t.append(""+memory[s.base + k*s.dimensions[0]+ j]+", ");
}
t.setLength(t.length()-2);
t.append("],\n");
}
t.setLength(t.length()-2);
return ""+t+"\n";
}
if ((I == 1 && S == 1) || (I == 2 && S == 2)) // Print single element of one or two dimensional array
{return ""+getMemory(Source, Indices);
}
if (I == 1 && S == 2) // Print indicated sub array
{final int l = s.dimensions[0]; // Size of sub array
final int p = l * lookUpIndex(s, 1, i[0]); // Start of sub array
for (int j = 0; j < l; j++) t.append(""+memory[s.base+ p + j]+", "); // Clear sub array in two dimensional array
t.setLength(t.length()-2);
return ""+t;
}
return "";
}
class Label // Label definition
{int instruction; // The instruction to which this labels applies
Label() {set(); labels.push(this);} // A label assigned to an instruction
void set() {wantCompiling(); instruction = code.size();} // Reassign the label to an instruction
}
class I // Instruction definition
{int instructionNumber; // Instruction number
final String definition = traceBack(); // Location of code that defined this instruction
I() // Define an instruction
{if (running) stop("Cannot define instructions during program execution",
definition);
instructionNumber = code.size(); code.push(this);
}
void a() {} // Action performed by instruction
String v() {Test.stop("No verilog supplied\n"+definition); return "";} // Initialization for instruction
String n() {return "instruction";} // Instruction name
}
//D1 Execute // Execute the program
void run() // Run the program
{wantCompiling();
z();
running = true;
final int N = code.size();
for (step = 0, time = 0; step < N && time < maxTime; step++, time++)
{z();
if (debugStep != null) debugStep.debug();
code.elementAt(step).a();
}
running = false;
if (time >= maxTime) stop("Out of time:", time);
}
void stop(String em) // Stop everything with an explanatory message
{z();
// new I() {void a() {Test.stop(em);} String v() {return "$finish(1);";}};
new I() {void a() {Test.stop(em);} String v() {return "stopped <= 1;";}};
}
void clearCode() {z(); code.clear(); running = false;} // Clear the program code
//D1 Blocks // Blocks of code used to implement if statements and for loops
void Goto(Label label) {new I() {void a() { step = label.instruction-1;} String v() {return "step = label.instruction; /* Goto */" ;}};} // The program execution for loop will increment first
void GoEq(Label label) {new I() {void a() {if (intermediateValue == 0) step = label.instruction-1;} String v() {return "if (intermediateValue == 0) step = label.instruction; else step = step + 1;/* GoEq */";}};} // Go to a specified label if the intermediate value is equal to zero
void GoNe(Label label) {new I() {void a() {if (intermediateValue != 0) step = label.instruction-1;} String v() {return "if (intermediateValue != 0) step = label.instruction; else step = step + 1;/* GoNe */";}};} // Go to a specified label if the intermediate value is not equal to zero
void GoGt(Label label) {new I() {void a() {if (intermediateValue > 0) step = label.instruction-1;} String v() {return "if (intermediateValue > 0) step = label.instruction; else step = step + 1;/* GoGt */";}};} // Go to a specified label if the intermediate value is greater than zero
void GoGe(Label label) {new I() {void a() {if (intermediateValue >= 0) step = label.instruction-1;} String v() {return "if (intermediateValue >= 0) step = label.instruction; else step = step + 1;/* GoGe */";}};} // Go to a specified label if the intermediate value is greater than or equal to zero
void GoLt(Label label) {new I() {void a() {if (intermediateValue < 0) step = label.instruction-1;} String v() {return "if (intermediateValue < 0) step = label.instruction; else step = step + 1;/* GoLt */";}};} // Go to a specified label if the intermediate value is less than zero
void GoLe(Label label) {new I() {void a() {if (intermediateValue <= 0) step = label.instruction-1;} String v() {return "if (intermediateValue <= 0) step = label.instruction; else step = step + 1;/* GoLe */";}};} // Go to a specified label if the intermediate value is less than or equal to zero
abstract class Block // A block that can be continued or exited
{final Label Start = new Label(), End = new Label(); // Labels at start and end of block to facilitate continuing or exiting
Block()
{code();
End.set();
}
abstract void code(); // Override this method to supply the code of the block
void start () {new I() {void a() { step = Start.instruction-1;} String v() {return "step <= "+(Start.instruction)+"; /* start */" ;}};} // Restart the block
void startIfEq() {new I() {void a() {if (intermediateValue == 0) step = Start.instruction-1;} String v() {return "if (intermediateValue == 0) step <= "+(Start.instruction)+"; else step = step + 1;/* startIfEq */";}};} // Restart the block if the intermediate value is equal to zero
void startIfNe() {new I() {void a() {if (intermediateValue != 0) step = Start.instruction-1;} String v() {return "if (intermediateValue != 0) step <= "+(Start.instruction)+"; else step = step + 1;/* startIfNe */";}};} // Restart the block if the intermediate value is not equal to zero
void startIfGt() {new I() {void a() {if (intermediateValue > 0) step = Start.instruction-1;} String v() {return "if (intermediateValue > 0) step <= "+(Start.instruction)+"; else step = step + 1;/* startIfGt */";}};} // Restart the block if the intermediate value is greater than zero
void startIfGe() {new I() {void a() {if (intermediateValue >= 0) step = Start.instruction-1;} String v() {return "if (intermediateValue >= 0) step <= "+(Start.instruction)+"; else step = step + 1;/* startIfGe */";}};} // Restart the block if the intermediate value is greater than or equal to zero
void startIfLt() {new I() {void a() {if (intermediateValue < 0) step = Start.instruction-1;} String v() {return "if (intermediateValue < 0) step <= "+(Start.instruction)+"; else step = step + 1;/* startIfLt */";}};} // Restart the block if the intermediate value is less than zero
void startIfLe() {new I() {void a() {if (intermediateValue <= 0) step = Start.instruction-1;} String v() {return "if (intermediateValue <= 0) step <= "+(Start.instruction)+"; else step = step + 1;/* startIfLe */";}};} // Restart the block if the intermediate value is less than or equal to zero
void end () {new I() {void a() { step = End.instruction-1;} String v() {return "step <= "+(End.instruction)+"; /* end */" ;}};} // End the block
void endIfEq() {new I() {void a() {if (intermediateValue == 0) step = End.instruction-1;} String v() {return "if (intermediateValue == 0) step <= "+(End.instruction)+"; else step = step + 1;/* endIfEq*/ ";}};} // End the block if the intermediate value is equal to zero
void endIfNe() {new I() {void a() {if (intermediateValue != 0) step = End.instruction-1;} String v() {return "if (intermediateValue != 0) step <= "+(End.instruction)+"; else step = step + 1;/* endIfNe*/ ";}};} // End the block if the intermediate value is not equal to zero
void endIfGt() {new I() {void a() {if (intermediateValue > 0) step = End.instruction-1;} String v() {return "if (intermediateValue > 0) step <= "+(End.instruction)+"; else step = step + 1;/* endIfGt*/ ";}};} // End the block if the intermediate value is greater than zero
void endIfGe() {new I() {void a() {if (intermediateValue >= 0) step = End.instruction-1;} String v() {return "if (intermediateValue >= 0) step <= "+(End.instruction)+"; else step = step + 1;/* endIfGe*/ ";}};} // End the block if the intermediate value is greater than or equal to zero
void endIfLt() {new I() {void a() {if (intermediateValue < 0) step = End.instruction-1;} String v() {return "if (intermediateValue < 0) step <= "+(End.instruction)+"; else step = step + 1;/* endIfLt*/ ";}};} // End the block if the intermediate value is less than zero
void endIfLe() {new I() {void a() {if (intermediateValue <= 0) step = End.instruction-1;} String v() {return "if (intermediateValue <= 0) step <= "+(End.instruction)+"; else step = step + 1;/* endIfLe*/ ";}};} // End the block if the intermediate value is less than or equal to zero
}
//D1 Verilog // Generate verilog for the program
class Verilog // Generate Verilog code representing the progrma suitable for inserting into a test harness
{final String opCodes;
final String declareOpCodes;
final String declareMemory;
final String initializeOpCodes;
final String initializeMemory;
Verilog(String Name, int Width) // Generate verilog for the program with the op codes named as specified for integers of the specified width
{final StringBuilder v = new StringBuilder(); // Generated verilog
final StringToNumbers ops = new StringToNumbers(); // Collapse identical instructions when writing verilog
final int N = code.size();
for(int i = 0; i < N; ++i)
{final String c = code.elementAt(i).v();
ops.put(c, i);
}
ops.order(); // Order the instructions
for(StringToNumbers.Order o : ops.outputOrder) // I shall say each instruction only once
{final int n = o.ordinal; // Step
final String i = o.string; // Instruction before next added
v.append(String.format(" %4d : begin %s end\n", n, i));
}
opCodes = ""+v;
declareOpCodes = ops.declareOpCodes(Name); // Write op code map
declareMemory = declareVerilogMemory(Width); // Declare memory in verilog
initializeOpCodes = ops.initializeOpCodes(Name, 6); // Initialize memory in verilog
initializeMemory = initializeVerilogMemory(); // Initialize memory in verilog
}
String verilog22() // Generate verilog for the program
{final StringBuilder v = new StringBuilder(); // Generated verilog
final int N = code.size();
for(int i = 0; i < N; ++i)
{final String c = code.elementAt(i).v();
v.append(" "+String.format("%4d", i)+": begin "+c+" end\n");
}
return ""+v;
}
String declareVerilogMemory(int width) // Declare memory in verilog
{final int N = memory.length;
return "reg ["+width+"-1:0] memory ["+N+"-1: 0]; /* declareVerilogMemory */";
}
String initializeVerilogMemory() // Initialize memory in verilog
{final StringBuilder v = new StringBuilder(); // Generated verilog
final int N = memory.length;
String z = """
begin /* initilizeVerilogMemory */
integer i;
for (i = 0; i < NNN; i = i + 1) begin
memory[i] <= 0;
end
end
""";
v.append(z.replace("NNN", ""+N)); // Zero code
for(int i = 0; i < N; ++i)
{final int m = memory[i];
if (m == 0) continue;
final String a = getArrays(i);
v.append(" "+String.format("memory[%4d] <= %4d; /* %s */\n", i, m, a)); // Non zero elements
}
return ""+v;
}
}
//D0 Tests // Testing
static Ban test_layout() // A test layout
{return new Ban()
{void load()
{array("c0");
array("c1");
array("c2");
array("c3");
array("a", 4);
array("b", 6);
array("A", 2, 4);
array("B", 4, 3);
}
};
}
static Ban test_clear()
{Ban l = test_layout();
l.clear( 0, "c0");
l.clear( 1, "c1");
l.clear( 2, "c2");
l.clear( 3, "c3");
l.clear( 4, "a");
l.clear( 5, "a", "c2");
l.clear( 6, "b", "c2");
l.clear( 7, "b", "c2");
l.clear( 8, "A", "c1");
l.clear( 9, "A", "c1", "c2");
l.clear(11, "B", "c1");
l.clear(22, "B", "c2");
l.clear(33, "B", "c1", "c1");
l.clear(44, "B", "c2", "c2");
l.run();
//stop(l);
ok(""+l, """
0 1 1 c1 -
1 2 2 c2 -
2 3 3 c3 -
3 4 0 4 a -
4 4 1 4 a |
5 4 2 5 a |
6 4 3 4 a |
7 8 2 7 b |
8 14 1 0 8 A +
9 14 1 1 8 A |
10 14 1 2 9 A |
11 14 1 3 8 A |
12 22 1 0 11 B +
13 22 1 1 33 B |
14 22 1 2 11 B |
15 22 2 0 22 B +
16 22 2 1 22 B |
17 22 2 2 44 B |
""");
return l;
}
static void test_get()
{Ban l = test_clear();
l.get("B", "c2", "c2"); l.run(); ok(l.intermediateValue, 44);
l.get("B", "c1", "c1"); l.run(); ok(l.intermediateValue, 33);
l.get("a", "c3"); l.run(); ok(l.intermediateValue, 4);
}
static Ban test_move()
{Ban l = test_clear();
l.move("B", "a", "c3", "c1", "c1");
l.move("B", "a", "c3", "c2", "c2");
l.run();
//stop(l);
ok(""+l, """
0 1 1 c1 -
1 2 2 c2 -
2 3 3 c3 -
3 4 0 4 a -
4 4 1 4 a |
5 4 2 5 a |
6 4 3 4 a |
7 8 2 7 b |
8 14 1 0 8 A +
9 14 1 1 8 A |
10 14 1 2 9 A |
11 14 1 3 8 A |
12 22 1 0 11 B +
13 22 1 1 33 B |
14 22 1 2 11 B |
15 22 2 0 22 B +
16 22 2 1 22 B |
17 22 2 2 44 B |
18 22 3 1 4 B |
19 22 3 2 5 B |
""");
return l;
}
static void test_compare()
{Ban l = test_move();
l.compare(4, "B", "c3", "c2"); l.run(); ok(l.intermediateValue, +1);
l.compare(5, "B", "c3", "c2"); l.run(); ok(l.intermediateValue, 0);
l.compare(6, "B", "c3", "c2"); l.run(); ok(l.intermediateValue, -1);
l.compare("B", "B", "c3", "c2", "c3", "c1"); l.run(); ok(l.intermediateValue, +1);
l.compare("B", "B", "c3", "c2", "c2", "c2"); l.run(); ok(l.intermediateValue, -1);
l.compare("B", "a", "c3", "c2", "c2"); l.run(); ok(l.intermediateValue, 0);
}
static void test_compare_int() // Encode some indices directly instead of indirectly
{Ban l = test_move();
l.compare(4, "B", "3", "c2"); l.run(); ok(l.intermediateValue, +1);
l.compare(5, "B", "3", "c2"); l.run(); ok(l.intermediateValue, 0);
l.compare(6, "B", "3", "c2"); l.run(); ok(l.intermediateValue, -1);
l.compare("B", "B", "3", "c2", "3", "c1"); l.run(); ok(l.intermediateValue, +1);
l.compare("B", "B", "3", "c2", "c2", "c2"); l.run(); ok(l.intermediateValue, -1);
l.compare("B", "a", "3", "c2", "c2"); l.run(); ok(l.intermediateValue, 0);
}
static void test_add()
{Ban l = test_move();
l.add(1, "B", "c3", "c1");
l.get("B", "c3", "c1");
l.run();
ok(l.intermediateValue, 5);
l.add("B", "B", "c3", "c2", "c3", "c1");
l.get("B", "c3", "c2");
l.run();
ok(l.intermediateValue, 10);
}
static void test_shiftRight()
{Ban l = test_move();
l.shiftRight("A", "c1", "c2"); l.run(); ok(l.intermediateValue, 4);
l.shiftRight("A", "c1", "c2"); l.run(); ok(l.intermediateValue, 2);
}
static Ban test_overlay() // A test layout with overlays
{final int M = 4, N = 3;
final Ban l = new Ban()
{void load()
{array("c1");
array("c2");
array("c3");
array("a", M*N);
overlay("b", M, N);
}
};
for (int i = 0; i < M*N; i++)
{l.set(i, "c1");
l.set(i, "a", "c1");
}
l.run();
for (int i = 0; i < M; i++)
{for (int j = 0; j < N; j++)
{l.clearCode();
l.set(i, "c1");
l.set(j, "c2");
l.get("b", "c1", "c2");
l.run();
ok(l.intermediateValue, i*N+j);
}
}
return l;
}
static void test_set()
{final int M = 4, N = 3;
final Ban l = new Ban()
{void load()
{array("c0");
array("c1");
array("c2");
array("c3");
array("a", M);
array("b", M, N);
}
};
for (int i = 0; i < M; i++) l.set(i, "c"+i);
for (int i = 0; i < M; i++) l.set(i, "a", ""+i);
for (int i = 0; i < M; i++)
{for (int j = 0; j < N; j++)
{l.set(i*N+j, "b", "c"+i, "c"+j);
}
}
l.run();
//stop(l);
ok(""+l, """
0 1 1 c1 -
1 2 2 c2 -
2 3 3 c3 -
3 4 1 1 a |
4 4 2 2 a |
5 4 3 3 a |
6 8 0 1 1 b |
7 8 0 2 2 b |
8 8 1 0 3 b +
9 8 1 1 4 b |
10 8 1 2 5 b |
11 8 2 0 6 b +
12 8 2 1 7 b |
13 8 2 2 8 b |
14 8 3 0 9 b +
15 8 3 1 10 b |
16 8 3 2 11 b |
""");
}
static Ban test_initialize()
{final Ban l = new Ban()
{void load()
{array("a");
array("b", 2);
array("c", 2, 3);
}
};
l.loadArray("a", 1);
l.loadArray("b", 1, 2);
l.loadArray("c", 11, 12, 13, 21, 22, 23);
l.run();
//stop(l);
ok(""+l, """
0 0 1 a -
1 1 0 1 b -
2 1 1 2 b |
3 3 0 0 11 c -
4 3 0 1 12 c |
5 3 0 2 13 c |
6 3 1 0 21 c +
7 3 1 1 22 c |
8 3 1 2 23 c |
""");
return l;
}
static void test_print()
{final Ban l = test_initialize();
ok(l.print("a"), "1");
ok(l.print("b"), "1, 2");
ok(l.print("c"), """
[11, 12, 13],
[21, 22, 23]