-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemoryLayoutDM.java
More file actions
2674 lines (2430 loc) · 124 KB
/
MemoryLayoutDM.java
File metadata and controls
2674 lines (2430 loc) · 124 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
//------------------------------------------------------------------------------
// Memory layout in Pseudo Assembler
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2024
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Memory layout
import java.util.*;
class MemoryLayoutDM extends Test implements Comparable<MemoryLayoutDM> // Memory layout
{final String name; // Name of the memory layout
final Layout layout; // Layout of part of memory
private Memory memory; // Memory containing layout
boolean debug; // Debug if true
ProgramDM P = new ProgramDM(); // Program containing generated code
static int numbers = 0; // Unique number for each memory layout recreated
final int number = ++numbers; // Number each memory layout
String uniqueName; // A unique name for this layout within its executing program
final BlockArray block; // The memory layout represents an array the elements of which have a width equal to a power of two
//D1 Construction // Construct a memory layout
MemoryLayoutDM(Layout Layout, String Name) // Memory with an associated layout and a name so we can generate verilog from it
{zz();
name = Name; layout = Layout;
memory = new Memory(Name, layout.size()); // Create the associated memory
block = new BlockArray();
}
public int compareTo(MemoryLayoutDM other) // A progam might access several memory layouts
{return Integer.compare(number, other.number);
}
At top() {zz(); return at(layout.top());} // A reference to the top element of a memory layout. Useful for moving the entire memory area.
class BlockArray // Memory that represents an array of elements whose widths are equal to the power of two and so the memory can be efficiently processed in blocks
{final boolean array; // The memory layout represents an array
final boolean powerOfTwo; // The elements of the array are a power of two in size
final int width; // The width of the elements of the array
final int size; // Number of elements in the array
final int log; // The log to base two of the width of the array elements
boolean blocked() {return powerOfTwo;} // A blocked array
BlockArray()
{zz();
array = layout.top() instanceof Layout.Array;
width = array ? layout.top().toArray().element.width : 0;
size = array ? layout.top().toArray().size : 1;
powerOfTwo = nextPowerOfTwo(width) == width;
log = logTwo(width);
}
}
//D1 Control // Testing, control and integrity
Memory memory() {return memory;} // Real memory used by this layout - executed very frequently
void program(ProgramDM program, boolean uniqueName) // Program in which to generate instructions. If the name is unique it will be used directly in verilog, if not unique, then a unique making number will be added to the end
{zz(); P = program; program.addMemoryLayout(this, uniqueName);
P.setUniqueNames();
}
void program(ProgramDM program) {zz(); program(program, true);} // Add this memory layout to a program with the intention of using its uqnique name to identify it in verilog
void setUniqueName() // Set a unique name for this memory layout for use when tracingmemory during program execution in Java and verilog to confirm that memory is being modified identically in the two representations.
{zz();
if (uniqueName == null) // Name has not already been set
{uniqueName = P != null && P.uniqueNames.contains(name) ? name // Name is already unique
: name+"_"+number; // Not sure if the name is unique so have to add a number to make it unique
}
}
String name() // Retrieve the unique name for this memory layout within its containing program
{if (uniqueName == null)
{stop("No unique name for memory layout with name:", name,
"and number:", number);
return name+"_"+number;
}
return uniqueName;
}
int size () {zz(); return memory.size();} // Size of associated memory
void clear() {zz(); memory.zero();} // Clear underlying memory
//D1 Get and Set // Get and set values in memory but only during testing
boolean getBit(int index) // Get a bit from the memory layout
{return memory.getBit(index);
}
void setBit(int index, boolean value) // Set a value in memory occupied by the layout
{memory.set(index, value);
}
int getInt(Layout.Field field, int...indices) // Get a value from memory occupied by the layout
{zz(); final int i = new At(field, indices).setOff().result;
return i;
}
void setInt(Layout.Field field, int value, int...indices) // Set a value in memory occupied by the layout
{zz(); final At a = new At(field, indices).setOff();
memory.set(a.at, a.width, value);
}
void setIntInstruction(Layout.Field field, int value) // Set a value in memory occupied by the layout
{zz();
P.new I()
{void a()
{final At a = new At(field).setOff();
memory.set(a.at, a.width, value);
}
String v()
{return new At(field).verilogLoad() +
" <= " + value + ";";
}
};
}
void zero() {zz(); memory.zero(0, size());} // Clear the memory associated with the layout to zeros
void ones() {zz(); memory.ones(0, size());} // Set the memory associated with the layout to zeros
void copy(MemoryLayoutDM source) // Copy as many of the bits from the source into the target as possible
{zz();
final int N = size();
P.new I()
{void a()
{for(int i = 0; i < N; ++i) setBit(i, source.getBit(i));
}
String v()
{return name()+"[0 +: "+N+"] <= "+source.name() +
"[0 +: "+N+"]; /* copy1 */";
}
};
}
void copy(final MemoryLayoutDM.At source) // Fill the target from the source starting at the referenced address in the source
{zz();
final MemoryLayoutDM m = this;
final int N = min(size(), source.width);
P.new I()
{void a()
{source.setOff();
for(int i = 0; i < N; ++i) setBit(i, source.getBit(i));
}
String v()
{return m.name()+"[0 +: "+N+"] <= "+source.ml().name()+
"["+source.verilogAddr()+" +: "+N+"]; /* copy2 */";
}
String n()
{return "copy "+N+" bits from "+source+" to "+m.name;
}
};
}
//D1 Components // Locate a variable in memory via its indices
class At
{final Layout.Field field; // Field description in layout
final int width; // Width of element in memory
final int[]indices; // Known indices to be applied directly to locate the field in memory
final At[] directs; // Fields whose location is known at the start so they can be used for indices into memory rather like registers on a chip
int at; // Location in memory
int result; // The contents of memory at this location
String verilogLoadAddr(boolean la, Integer delta) // A verilog representation of an addressed location in memory
{zz();
final Stack<String> v = new Stack<>(); // Verilog expression, index variable names
Layout.Locator L = field.locator;
final Stack<Layout.Array>A = L.arrays; // The containing arrays
for (int i = 0; i < A.size(); ++i) // Each array to index to reach this field
{final Layout.Array a = A.elementAt(i).toArray(); // Each array to index to reach this field
final int w = a.element.width; // Width of containing array element
final String o = !hasIndirection() ? ""+indices[i] : // Numeric index
directs[i].verilogLoad(); // Indirect index loaded from memory
v.push(" + " + o + " * " + w); // Access indexing field
}
final int W = field.width; // The width of the field
final String w = w(W); // The width of the field as a padded string
final String d = delta == null ? "" : // Constant delta to modify address if needed in steps of field size as in the C programming language
delta > 0 ? "+"+delta*W : ""+delta*W;
return (la ? i(field.at)+c()+joinStrings(v, "")+d : // IBM S/360 Principles of Operation: LA
name()+"["+i(field.at)+c()+joinStrings(v, "")+d+" +: "+w+"]"); // IBM S/360 Principles of Operation: L
}
String verilogLoad() {zz(); return verilogLoadAddr(false, null);} // Content of a memory location as a verilog expression
String verilogAddr() {zz(); return verilogLoadAddr(true, null);} // Address of a memory location
String i(int i) {zz(); return String.format("%8d", i);} // Format an index
String w(int w) {zz(); return String.format("%1d", w);} // Format a width
String c() // Format a field name as a comment
{final StringBuilder s = new StringBuilder(field.name);
while(s.length() % 8 > 0) s.append(" ");
return "/*" + s +"*/";
}
String p(String s) {while(s.length() % 8 > 0) s = s+" "; return s;} // Pad a string
At locateDirectAddress() // Locate a direct address and its content
{zz();
final int N = indices.length;
at = field.locator.at(indices);
result = memory.getInt(at, width);
return this;
}
At locateInDirectAddress() // Locate an indirect address and its content
{zz();
final int N = directs.length;
for (int i = 0; i < N; i++)
{indices[i] = directs[i].setOff().getInt(); // It is assumed by getInt() that setOff() will already have been called.
}
return locateDirectAddress(); // Locate the address directly now that its indices are known
}
At(Layout.Field Field) // No indices
{zz();
field = Field; indices = new int[0];
width = field.width;
if (width < 1) stop("Field", field.name, "does not have any bits");
directs = null;
locateDirectAddress(); // The indices are constant so the address will not change over time
}
At(Layout.Field Field, int...Indices) // Constant indices used for setting initial values
{zz();
field = Field; indices = Indices; width = field.width;
directs = null;
locateDirectAddress(); // The indices are constant so the address will not change over time
}
At(Layout.Field Field, At...Directs) // Variable indices used for obtaining run time values
{zz();
final int N = Directs.length, E = layout.new Locator(Field).arrays.size();// Number of indices supplied, number expected
if (N != E) stop("Got", N, "indices but expected", E);
for (int i = 0; i < N; i++)
{if (Directs[i].directs != null)
{stop("Index:", i, "must not have indices");
}
}
field = Field; width = field.width;
directs = Directs;
indices = new int[N]; // The values obtained from each indirect reference to an index will be placed here for the computation of the actual address of the indexed field
}
boolean sameSize(At b) // Check two fields are the same size
{zz(); if (field == null) return true; // Constants match any size
z(); field.sameSize(b.field);
z(); return true;
}
boolean hasIndirection() {z(); return directs != null;} // Is indirection used in this at reference ?
int width() {z(); return field.width();} // Width of the field in memory
At setOff() {z(); return setOff(true);} // Set the base address of the field from its indices confirming that we are inside an executing instruction
At setOff(boolean checkSetOff) // Set the base address of the field
{zz();
if (checkSetOff && !P.running) // Check this method is being used inside an instruction
{P.halt("Set off must be inside an instruction");
}
if (hasIndirection()) {z(); locateInDirectAddress();} // Evaluate indirect indices
else {zz(); locateDirectAddress();} // Evaluate direct indices
return this;
}
boolean getBit(int i) {zz(); return memory.getBit(at+i);} // Get a bit from memory assuming that setOff() has been called to fix the location of the field containing the bit
void setBit(int i, boolean b) {zz(); memory.set(at+i, b);} // Set a bit in memory assuming that setOff() has been called to fix the location of the field containing the bit
int getInt() {zz(); return result;} // The value in memory, at the indicated location, treated as an integer or the value of the constant, assumming setOff has been called to update the variable description
void setInt(int value) {zz(); memory.set(at, width, value);} // Set the value in memory at the indicated location, treated as an integer
public String toString() // Print field name(indices)=value or name=value if there are no indices
{final StringBuilder s = new StringBuilder();
s.append(ml().name+"."+field.name);
if (indices.length > 0)
{//setOff(false);
s.append("[");
for (int i = 0, N = indices.length; i < N; i++)
{s.append(indices[i]);
if (i < N-1) s.append(",");
}
s.append("]"+at+"="+result);
}
else s.append("@"+at+"="+result);
return s.toString();
}
MemoryLayoutDM ml() {zz(); return MemoryLayoutDM.this;} // Containing memory layout
//D2 Move // Copy data between memory locations
void moveBits(At source) // The interior of a move on Java
{zz();
final At target = this;
source.setOff();
target.setOff();
for(int i = 0; i < width; ++i)
{final boolean b = source.getBit(i);
target.setBit(i, b);
}
}
void move(At source) // Copy the specified number of bits from source to target assuming no overlap. The source and target can be in the same or a different memory.
{zz(); sameSize(source);
final At target = this;
if (target.ml() == source.ml() &&
target.field == source.field &&
target.indices.length == 0 && source.indices.length == 0) // No need to copy an unindexed field into itself
{//err("No need to move", source.field.name, target.field.name);
return;
}
P.new I()
{void a() {moveBits(source);}
String v()
{return target.verilogLoad()+" <= "+source.verilogLoad() + "/* move */;";
}
String n() {return field.name+"="+source.field.name;}
void i() {}
};
}
void moveTo(At...Targets) // Move the data to the named fields
{z();
final int N = Targets.length;
final At source = this;
for(int i = 0; i < N; ++i) source.sameSize(Targets[i]);
P.new I()
{void a()
{source.setOff();
for(int i = 0; i < N; ++i)
{final At target = Targets[i];
target.setOff();
for(int j = 0; j < width; ++j)
{z();
final boolean b = source.getBit(j);
target.setBit(j, b);
}
}
}
String v()
{final StringBuilder s = new StringBuilder();
for(int i = 0; i < N; ++i)
{return Targets[i].verilogLoad()+" <= "+source.verilogLoad() + "/* moveTo */;";
}
return s.toString();
}
String n() {return field.name+"="+source.field.name;}
};
}
private At createField(String Name, int Width) // Create a field of the specified width
{zz();
final Layout l = Layout.layout(); // Layout some memory
final Layout.Variable v = l.variable(Name, Width); // Create variable definition
MemoryLayoutDM m = new MemoryLayoutDM(l.compile(), Name); // Create memory for variable
m.program(ml().P, false); // Add memory to program
return m.at(v); // Return variable
}
private At createMoveBuffer() // Create a buffer for moving elements in an array
{zz(); if (!(field instanceof Layout.Array)) stop("Array required"); // Need an array to amke a buffer for
final Layout.Array A = field.toArray(); // Array of elements to be moved
final Layout.Field a = A.element; // Array element
final Layout b = Layout.layout(); // A field of bits the same size as the array
final Layout.Variable B = b.variable ("b", a.width * A.size); // A buffer the same size as the array
MemoryLayoutDM m = new MemoryLayoutDM(b.compile(), "buffer"); // Create a matching array buffer
m.program(P, false); // Add memory to program
return m.at(B); // Return buffer
}
private void copyMoveBuffer(At Source) // Copy a move buffer directly to the target bit by bit without regard for the structure of the source of target. This allows such moves to be performed in verilog as a single statement.
{zz(); sameSize(Source); // Check source anmd target gave the same size
final int N = width; // Width of target in bits
final Memory s = Source.ml().memory(), t = ml().memory(); // Memory for source and target
P.new I()
{void a() // Emulation
{final int S = Source.setOff().at, T = setOff().at; // Start of source and target in memory
for (int i = 0; i < N; i++) // Each bit to be moved
{final boolean b = s.getBit(S+i); // A bit to be moved
t.set(T+i, b); // Copy the bit into the target
}
}
String v() // Verilog
{return name()+"["+ verilogAddr()+"+:"+N+"] <= "+ // Target
Source.ml().name()+"["+Source.verilogAddr()+"+:"+N+"];"+ // Source
"/* copyMoveBuffer */";
}
};
}
private void upMoveBuffer(int Width) {zz(); upMoveBuffer(0, Width);} // Move all the bits in a buffer up by the specified width
private void downMoveBuffer(int Width) {zz(); downMoveBuffer(0, Width);} // Move all the bits in a buffer down by the specified width
private void upMoveBuffer(int Start, int Width) // Move the bits in a buffer up by the specified width starting at the indexed location specified in multiples of the width.
{zz();
final int S = Start * Width; // Start position
final int N = width - S - Width; // Number of bits to move
final MemoryLayoutDM l = ml(); // Memory layout containing buffer
final Memory m = l.memory(); // Memory for buffer
P.new I()
{void a() // Emulation
{for (int i = N; i > 0; i--) // Each bit to be moved
{final boolean b = getBit(S+i-1); // A bit to be moved
setBit(S+Width+i-1, b); // Copy the bit into the target
}
}
String v() // Verilog
{final String n = l.name(); // Memory name in which the move will be performed
return n+"["+S+"+"+Width+"+:"+N+"] <= "+n+"[0+:"+N+"];"+ // Parallel move
"/* upMoveBufferLinear */";
}
};
}
private void downMoveBuffer(int Start, int Width) // Move the bits in a buffer down by the specified width starting at the indexed location specified in multiples of the width.
{zz();
final int S = Start * Width; // Start position
final int N = width - S - Width; // Number of bits to move
final MemoryLayoutDM l = ml(); // Memory layout containing buffer
final Memory m = l.memory(); // Memory for buffer
P.new I()
{void a() // Emulation
{for (int i = 0; i < N; i++) // Each bit to be moved
{final boolean b = getBit(S+Width+i); // A bit to be moved
setBit(S+i, b); // Copy the bit into the target
}
}
String v() // Verilog
{final String n = l.name(); // Memory name in which the move will be performed
return n+"["+S+"+:"+N+"] <= "+n+"["+Width+"+:"+N+"];"+ // Parallel move
"/* downMoveBufferLinear */";
}
};
}
private void upMoveBuffer(At Start, int Width, int Length) // Move the bits in a buffer up by the specified width starting at the indexed location specified in multiples of the width. The length of the move is specified in multiples of the width.
{z();
final MemoryLayoutDM l = ml(); // Memory layout containing buffer
final Memory m = l.memory(); // Memory for buffer
P.new I()
{void a() // Emulation
{final int S = Start.setOff().result * Width; // Start position
final int N = Length * Width; // Number of bits to move
for (int i = N; i > 0; i--) // Each bit to be moved
{final boolean b = getBit(S+i-1); // A bit to be moved
setBit(S+Width+i-1, b); // Copy the bit into the target
}
}
String v() // Verilog
{final String S = Start.verilogLoad(); // Start position
final String N = ""+(Length * Width); // Number of bits to move
final String n = l.name(); // Memory name in which the move will be performed
return "begin " +
n+"["+S+"+"+Width+"+:"+N+"] <= "+n+"["+S+"+:"+N+"]; "+ // Parallel move
"end /* upMoveBufferBlock */";
}
};
}
private void downMoveBuffer(At Start, int Width, int Length) // Move the bits in a buffer down by the specified width starting at the indexed location specified in multiples of the width. The length of the move is specified in multiples of the width.
{z();
final MemoryLayoutDM l = ml(); // Memory layout containing buffer
final Memory m = l.memory(); // Memory for buffer
P.new I()
{void a() // Emulation
{final int S = Start.setOff().result * Width; // Start position
final int N = Length * Width; // Number of bits to move
for (int i = 0; i < N; i++) // Each bit to be moved
{final boolean b = getBit(S+Width+i-1); // A bit to be moved
setBit(S+i-1, b); // Copy the bit into the target
}
}
String v() // Verilog
{final String S = Start.verilogLoad(); // Start position
final String N = ""+(Length * Width); // Number of bits to move
final String n = l.name(); // Memory name in which the move will be performed
return "begin " +
n+"["+S+"+:"+N+"] <= "+n+"["+S+"+"+Width+"+:"+N+"]; "+ // Parallel move
"end /* downMoveBufferBlock */";
}
};
}
void moveUp() // Move the elements of an array up one position deleting the last element.
{zz();
if (!(field instanceof Layout.Array)) stop("Array required for moveUp");
final Layout.Array A = field.toArray(); // Array of elements to be moved
final At B = createMoveBuffer(); // Buffer containg a copy of the array to be moved
B.copyMoveBuffer(this);
B.upMoveBuffer(A.element.width);
copyMoveBuffer(B);
}
void moveDown() // Move the elements of an array down one position deleting the first element.
{zz();
if (!(field instanceof Layout.Array)) stop("Array required for moveDown");
final Layout.Array A = field.toArray(); // Array of elements to be moved
final At B = createMoveBuffer(); // Buffer containg a copy of the array to be moved
B.copyMoveBuffer(this);
B.downMoveBuffer(A.element.width);
copyMoveBuffer(B);
}
void moveUp(At Index) // Move the elements of an array up one position deleting the last element. A buffer of the same size is used to permit copy in parallel. Whether each element is copied is dependent on a binary "less than" which is expensive
{zz();
if (!(field instanceof Layout.Array)) stop("Array required for moveUp");
final Layout.Array A = field.toArray(); // Array of elements to be moved
final int S = logTwo(A.size)-1; // Next log 2 of size of array. Minus one because that is the size of the maximum move
final int width = A.element.width; // Width of each array element
final At B = createMoveBuffer(); // Buffer containing a copy of the array to be moved
final At p = createField("position", Index.width); // Current position in buffer
B.copyMoveBuffer(this); // Make a copy of the array to work on as this will hopefully reduce congestion
p.ml().setIntInstruction(p.field, A.size); // Position in the buffer in units of array element size
for (int i = 0; i <= S; i++) // Move each logarithmically sized block
{final int q = 1<<(S-i); // Size of block
P.new I()
{void a() // Emulation
{final int o = p.ml().getInt(p.field) - q; // Start of block to move to
if (o > Index.setOff().result) // Move block is in range
{final int S = o * width; // Start position
final int N = q * width; // Number of bits to move
for (int i = N; i > 0; --i) // Each bit to be moved
{final boolean b = B.getBit(S-width+i-1); // A bit to be moved
B.setBit(S+i-1, b); // Copy the bit into the target
}
p.ml().setInt(p.field, o); // Address remainder of area to be moved
}
}
String v() // Verilog
{final String S = p.verilogLoad(); // Start position
final String N = i8(q * width); // Number of bits to move
final String n = B.ml().name(); // Memory name in which the move will be performed
final String Q = i4(q);
return "if ("+S+" > "+Index.verilogLoad()+"+"+Q+") begin " + // Block is in range
n+"[("+S+" - "+Q+")"+"*"+width+ " +: "+N+"] <= "+
n+"[("+S+" - "+Q+")"+"*"+width+"-"+width+" +: "+N+"]; "+ // Parallel move
p.verilogLoad()+" <= "+p.verilogLoad()+"-"+Q+";"+ // Update position
"end /* moveUp */";
}
};
}
copyMoveBuffer(B); // Rewrite the array after performing the move in the buffer
}
void moveDown(At Index) // Move the elements of an array down one position deleting the last element. A buffer of the same size is used to permit copy in parallel. Whether each element is copied is dependent on a binary "less than" which is expensive
{zz();
if (!(field instanceof Layout.Array)) stop("Array required for moveUp");
final Layout.Array A = field.toArray(); // Array of elements to be moved
final int S = logTwo(A.size)-1; // Next log 2 of size of array. Minus one because that is the size of the maximum move
final int width = A.element.width; // Width of each array element
final At B = createMoveBuffer(); // Buffer containing a copy of the array to be moved
final At p = createField("position", Index.width); // Current position in buffer
B.copyMoveBuffer(this); // Make a copy of the array to work on as this will hopefully reduce congestion
p.move(Index); // Position in the buffer in units of array element size
for (int i = 0; i <= S; i++) // Move each logarithmically sized block
{final int q = 1<<(S-i); // Size of block
P.new I()
{void a() // Emulation
{final int o = p.ml().getInt(p.field); // Start of block to move to
if (o + q < A.size) // Move block is in range
{final int S = o * width; // Start position
final int N = q * width; // Number of bits to move
for (int i = 0; i < N; i++) // Each bit to be moved
{final boolean b = B.getBit(S+width+i); // A bit to be moved
B.setBit(S+i, b); // Copy the bit into the target
}
p.ml().setInt(p.field, o + q); // Address remainder of area to be moved
}
}
String v() // Verilog
{final String S = p.verilogLoad(); // Start position
final String N = i8(q * width); // Number of bits to move
final String n = B.ml().name(); // Memory name in which the move will be performed
final String Q = i8(q); // Move width
return "if ("+S+"+"+Q+" < "+A.size+") begin " +
n+"["+S+"*"+width +" +: "+N+"] <= "+
n+"["+S+"*"+width+"+"+width+" +: "+N+"]; "+ // Parallel move
p.verilogLoad()+" <= "+p.verilogLoad()+"+"+Q+";"+
"end /* moveDown */";
}
};
}
copyMoveBuffer(B); // Rewrite the array after performing the move in the buffer
}
void copy(At Source, At Length, At TargetA, At SourceA, At LengthL) // Copy the specified number of bits from the location addressed by the source to the location addressed by the target using the specified memory locations to hold the source and target locations and the remaining length
{zz();
final At Target = this;
final int size = Source.ml().size();
P.new I() // Initialize the indexes and length to describe the copy
{void a()
{Target.setOff(); // Target of move
Source.setOff(); // Source of move
Length.setOff(); // Length of move
SourceA.setInt(Source.at); // A variable that indexes the current location in the source
TargetA.setInt(Target.at); // A variable that indexes the current location in the target
LengthL.setInt(Length.getInt()); // The remaining amount of data to be moved
}
String v() // Logarithmic move
{final StringBuilder v = new StringBuilder("/* copy start */\n");
final String l = Length.verilogLoad(), L = LengthL.verilogLoad(),
s = Source.verilogAddr(), S = SourceA.verilogLoad(),
t = Target.verilogAddr(), T = TargetA.verilogLoad();
v.append(L + " <= " + l + "; /* copy11 */\n"); // Initialize at the start of the move
v.append(S + " <= " + s + "; /* copy12 */\n");
v.append(T + " <= " + t + "; /* copy13 */\n");
return v.toString();
}
};
for(int i = prevPowerOfTwo(size); i > 0; i = i >> 1) // Copy in logarithmically descending blocks
{final int I = i; // Length of move
P.new I()
{void a()
{final int
l = LengthL.setOff().getInt(), // Remaining length
s = SourceA.setOff().getInt(), // Current position in source memory
t = TargetA.setOff().getInt(); // Current position in target memory
final Memory sm = Source.ml().memory(); // Memory containing source
final Memory tm = Target.ml().memory(); // Memory containing target
if (l >= I)
{for (int j = 0; j < I; ++j) // Move a block of power of two size
{final boolean b = sm.getBit(s + j);
tm.set(t+j, b);
}
LengthL.setInt(l - I); // Update remaining length
SourceA.setInt(s + I); // Latest position in source
TargetA.setInt(t + I); // Latest position in target
}
}
String v() // Logarithmic move
{final StringBuilder v = new StringBuilder("/* MemoryLayoutDM.copy "+I+" */\n");
final String l = LengthL.verilogLoad(), // Size of this move
s = SourceA.verilogLoad(), // Position in source memory
t = TargetA.verilogLoad(), // position in target memory
S = Source.ml().name(), // Memory containing source
T = Target.ml().name(); // Memory containing target
v.append("if (" +l+" >= "+I+") begin\n");
v.append(" " +T+"["+t+" +: " +I+"] <= "+S+"["+s+" +: "+I+"]; /* copy21 */\n");
v.append(" " +l+" <= "+l+" - "+I+"; /* copy22 */\n"); // These assigns have to be made immediately else each block has to be executed one after another to drive the length and pointers sequentially.
v.append(" " +s+" <= "+s+" + "+I+"; /* copy23 */\n");
v.append(" " +t+" <= "+t+" + "+I+"; /* copy24 */\n");
v.append("end\n");
return v.toString();
}
};
}
}
void copy(MemoryLayoutDM Source) // Copy the source to the location addressed by the target
{zz();
final int N = Source.size();
P.new I()
{void a()
{setOff();
for(int i = 0; i < N; ++i)
{final boolean b = Source.getBit(i);
try
{setBit(i, b);
}
catch(Exception e)
{stop(e, traceBack);
}
}
}
String v()
{final int n = min(size(), N);
return ml().name()+"["+verilogAddr()+" +: "+n+"] <= "+
Source.name()+"[0 +: "+n+"]; /* copy3 */";
}
};
}
void copy(final MemoryLayoutDM.At source) // Fill the target at the referenced address from the source starting at the referenced address
{zz(); sameSize(source);
final int N = width;
P.new I()
{void a()
{setOff(); source.setOff();
for(int i = 0; i < N; ++i) setBit(i, source.getBit(i));
}
String v()
{final int n = min(width, source.width);
return ml().name() + "["+verilogAddr()+" +: "+n +"] <= "+
source.ml().name() + "["+source.verilogAddr()+" +: "+n+
"]; /* copy4 */";
}
};
}
//D2 Bits // Bit operations in a memory.
void zero() // Zero some memory
{zz();
P.new I()
{void a()
{setOff(); memory.zero(at, width);
}
String v()
{return verilogLoad()+" <= 0; /* zero */";
}
String n()
{return field.name+" <= 0";
}
};
}
void one() // Set memory to one
{zz();
P.new I()
{void a()
{setOff(); setInt(1);
}
String v()
{return verilogLoad()+" <= 1; /* ones */";
}
String n()
{return field.name+" <= 1;";
}
};
}
void ones() // Ones some memory
{zz();
final String one = field.verilogOnes();
P.new I()
{void a()
{setOff(); memory.ones(at, width);
}
String v()
{return verilogLoad()+" <= "+one+ "; /* ones */";
}
String n()
{return field.name+" <= "+one;
}
};
}
void invert(At a) // Invert the specified bits
{z();
P.new I()
{void a()
{setOff(); memory.invert(a.result, a.width());
}
String v()
{return verilogLoad()+" <= ~"+verilogLoad()+ "; /* invert */";
}
String n()
{return field.name+" <= ~"+field.name;
}
};
}
boolean isAllZero() {z(); setOff(); return memory.isAllZero(at, width);} // Check that the specified memory is all zeros
boolean isAllOnes() {z(); setOff(); return memory.isAllOnes(at, width);} // Check that the specified memory is all ones
//D1 Boolean // Boolean operations on fields held in memories.
boolean isZero() // Whether the field is all zero
{zz();
for(int i = 0; i < width; ++i)
{zz(); if (getBit(i)) {z(); return false;}
}
z(); return true;
}
void isZero(At result) // Whether a field is all zeros
{zz();
final At target = this;
P.new I()
{void a()
{result.setOff().setInt(setOff().isZero() ? 1 : 0);
}
String v()
{return result.verilogLoad()+" <= "+
target.verilogLoad()+" == 0; /* isZero */";
}
String n() {return result.field.name+" <= isZero "+field.name;}
};
}
boolean isOnes() // Whether the field is all ones
{z();
for(int i = 0; i < width; ++i)
{z(); if (!getBit(i)) {z(); return false;}
}
z(); return true;
}
void isOnes(At result) // Whether a field is all ones
{z();
final At target = this;
P.new I()
{void a()
{result.setOff().setInt(setOff().isOnes() ? 1 : 0);
}
String v()
{return result.verilogLoad()+" <= "+target.verilogLoad()+
" == " + target.field.verilogOnes()+ "; /* isOnes */";
}
String n() {return result.field.name+" <= isOnes "+field.name;}
};
}
private boolean equal(At b) // Whether a == b
{zz(); sameSize(b);
for(int i = 0; i < width; ++i)
{zz(); if (getBit(i) != b.getBit(i)) {z(); return false;}
}
z(); return true;
}
void equal(At b, At result) // Whether a == b
{zz();
final At a = this;
P.new I()
{void a()
{result.setOff().setInt(equal(b.setOff()) ? 1 : 0);
}
String v()
{return result.verilogLoad()+" <= "+a.verilogLoad()+
" == " + b.verilogLoad()+ "; /* equal */";
}
String n() {return result.field.name+"="+field.name+" == "+b.field.name;}
};
}
void notEqual(At b, At result) // Whether a != b
{z();
final At a = this;
P.new I()
{void a()
{result.setOff().setInt(!equal(b.setOff()) ? 1 : 0);
}
String v()
{return result.verilogLoad()+
" <= "+a.verilogLoad()+
" != "+b.verilogLoad()+ "; /* notEqual */";
}
String n() {return result.field.name+"="+field.name+"!="+b.field.name;}
};
}
private boolean lessThan(At b) // Whether a < b
{zz(); sameSize(b);
for(int i = width; i > 0; --i)
{zz();
if (!getBit(i-1) && b.getBit(i-1)) {z(); return true;}
if ( getBit(i-1) && !b.getBit(i-1)) {z(); return false;}
}
z(); return false;
}
void lessThan(At b, At result) // Whether a < b
{zz(); sameSize(b);
final At a = this;
P.new I()
{void a()
{result.setOff().setInt(lessThan(b.setOff()) ? 1 : 0);
}
String v()
{return result.verilogLoad()+
" <= "+a.verilogLoad()+
" < "+b.verilogLoad()+ "; /* lessThan */";
}
String n() {return result.field.name+"="+field.name+"<"+b.field.name;}
};
}
void lessThanOrEqual(At b, At result) // Whether a <= b
{z(); sameSize(b);
final At a = this;
P.new I()
{void a()
{result.setOff().setInt(lessThan(b.setOff()) || equal(b) ? 1 : 0);
}
String v()
{return result.verilogLoad()+
" <= "+a.verilogLoad()+
" <= "+b.verilogLoad()+ "; /* lessThanOrEqual */";
}
String n() {return result.field.name+"="+field.name+"<="+b.field.name;}
};
}
void greaterThan(At b, At result) // Whether a > b
{zz(); sameSize(b);
final At a = this;
P.new I()
{void a()
{result.setOff().setInt(!lessThan(b.setOff()) && !equal(b) ? 1 : 0);
}
String v()
{return result.verilogLoad()+
" <= "+a.verilogLoad()+
" > " +b.verilogLoad()+ "; /* greaterThan */";
}
String n() {return result.field.name+"="+field.name+">"+b.field.name;}
};
}
void greaterThanOrEqual(At b, At result) // Whether a >= b
{zz(); sameSize(b);
final At a = this;
P.new I()
{void a()
{result.setOff().setInt(!lessThan(b.setOff()) ? 1 : 0);
}
String v()
{return result.verilogLoad()+
" <= "+a.verilogLoad()+
" >= "+b.verilogLoad()+ ";/* greaterThanOrEqual */";
}
String n() {return result.field.name+"="+field.name+">="+b.field.name;}
};
}
//D1 Arithmetic // Arithmetic on integers
//D2 Binary // Arithmetic on binary integers
void inc() // Increment a variable treated as an unsigned binary integer with wrap around on overflow. Return the result after the increment.
{zz();
final At a = this;
P.new I()
{void a()
{setOff();
final int i = getInt()+1;
setInt(i);
}
String v()
{final String o = ""+width+"'d1";
return a.verilogLoad()+" <= "+a.verilogLoad()+"+"+o+"; /* inc */";
}
String n()
{return "++"+field.name;
}
};
}
void dec() // Decrement a variable treated as an unsigned binary integer with wrap around on overflow. Return the result after the increment.
{zz();
final At a = this;
P.new I()
{void a()
{setOff();
final int i = getInt()-1;
setInt(i);
}
String v()
{final String o = ""+width+"'d1";
return a.verilogLoad()+" <= "+a.verilogLoad()+"-"+o+"; /* dec */";
}
String n()
{return "--"+field.name;
}
};
}
void add(At source, int constant) // Add a constant to the source and store in the target
{zz(); sameSize(source);
if (constant == 0) stop("Use nop() instead"); // There is no point in adding zero
if (logTwo(constant) > width)
{stop("Constant too big for field. Constant:", constant,
"too big for a field of size:", width);
}
final At target = this;
P.new I()
{void a()
{final int v = source.setOff().getInt();
target.setOff().setInt(v + constant);
}
String v()
{final String c = (constant > 0 ? "+" : "-")+width+"'d"+abs(constant); // Create a signed constant of the same width
final String t = target.verilogLoad(), s = source.verilogLoad();
return t+" <= " + s + c + "; /* add1 */";
}
String n() {return field.name+"="+source.field.name + "+ "+ constant;}
void i() {}
};
}
void add(At a, At b) // Add two variables and store in the target
{z(); sameSize(a); sameSize(b);
final At target = this;
P.new I()
{void a()
{final int A = a.setOff().getInt();