-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemory.java
366 lines (312 loc) · 13.1 KB
/
Memory.java
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
//------------------------------------------------------------------------------
// Memory
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2024
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Simulate a silicon chip.
import java.util.*;
class Memory extends Test // Memory provided in bits
{final boolean[]bits; // The memory in bits
final String name; // The name of the memory
//D1 Construct // Memory provided in bits
Memory(String Name, int Size) // Create memory
{name = Name; bits = new boolean[Size];
}
static Memory memory(String Name, int Size) // Create memory
{z(); return new Memory(Name, Size);
}
public String toString() // Print memory in hex
{final int N = 256;
final String T = "4... 4... 4... 4... "+
"3... 3... 3... 3... "+
"2... 2... 2... 2... "+
"1... 1... 1... 1...";
final String t = (""+new StringBuilder("0123 4567 89AB CDEF ".repeat(4)).reverse()).trim();
final StringBuilder B = new StringBuilder(); // One block a multiple of N in length
for(int i = 0; i < bits.length; i++) B.append(getBit(i) ? '1' : '0');
for(int i = 0; i < N && B.length() % N > 0; i++) B.append('0');
final StringBuilder S = new StringBuilder(); // Final result
for (int i = 0, n = B.length() / N; i < n; ++i) // Lines of hex
{z();
final StringBuilder s = new StringBuilder(); // Line of hex
for (int j = 0; j < N; j += 4) // Blocks of 4 bits
{final StringBuilder H = new StringBuilder(B.substring(i*N+j, i*N+j+4)); // Bits to convert
final int h = Integer.parseInt(""+H.reverse(), 2); // Low endian
s.append("0123456789abcdef".charAt(h));
if (j % 16 == 12) s.append(" ");
}
S.append(String.format("%4d ", i)+(""+s.reverse()).trim()+"\n");
}
return "Memory: "+name+"\n "+T+"\nLine "+t+"\n"+S;
}
int size() {z(); return bits.length;} // Size of memory
//D1 Operations // Operations on memory
//D2 Basic // Basic memory access
void check(int start, int width) // Check a request is in the range of the memory
{z();
if (start < 0) stop("Too small:", start);
if (start + width > size()) stop("Out of range. Addressing:", start+width,
"in memory of size:", size());
if (width < 1) stop("Width must be one or more, not:", width);
}
//D3 Set // Set a bit
void set(int start, boolean value) // Set a bit from a boolean
{z();
//check(start, 1);
bits[start] = value;
}
void set(int start, int width, int value) // Set some memory from an integer
{//check(start, width);
z();
final int w = min(Integer.SIZE-1, width);
for(int i = 0; i < w; ++i)
{z();
final int n = value & (1 << i);
set(start+i, n != 0);
}
}
//D3 Get // Get a bit
boolean getBit(int start) // Get a boolean from memory
{//check(start, 1);
z(); return bits[start];
}
int getInt(int start, int width) // Get an int from memory
{//check(start, width);
z();
if (start < 0 || start + width > size()) stop("Out of range in memory", name, "Start:", start, "width:", width, "size:", size());
z();
if (width < 1) stop("width must be one or more, not:", width);
z();
final int w = min(Integer.SIZE-1, width);
int n = 0;
for(int i = 0; i < w; ++i)
{z();
if (getBit(start+i)) {z(); n |= (1 << i);}
}
return n;
}
//D2 Composite // Composite memory access
void zero() {z(); for(int i = 0; i < bits.length; ++i) {z(); set(i, false);}} // Zero all memory
void ones() {z(); for(int i = 0; i < bits.length; ++i) {z(); set(i, true);}} // Ones all memory
void zero(int start, int width) // Zero some memory
{z(); for(int i = 0; i < width; ++i) {z(); set(start+i, false);}
}
void ones(int start, int width) // Ones some memory
{z(); for(int i = 0; i < width; ++i) {z(); set(start+i, true);}
}
boolean isAllZero(int start, int width) // Check that the specified memory is all zeros
{z();
for(int i = 0; i < width; ++i)
{z();
if (getBit(start+i))
{z();
return false;
}
}
return true;
}
boolean isAllOnes(int start, int width) // Check that the specified memory is all ones
{z();
for(int i = 0; i < width; ++i)
{z();
if (!getBit(start+i)) {z(); return false;}
}
return true;
}
void copy(int target, int source, int width) // Copy the specified number of bits from source to target low bits first
{z();
for(int i = 0; i < width; ++i)
{z();
set(target+i, getBit(source+i));
}
}
void copyHigh(int target, int source, int width) // Copy the specified number of bits from source to target high bits first
{z();
for(int i = width; i > 0; --i)
{z();
set(target+i-1, getBit(source+i-1));
}
}
void invert(int start, int width) // Invert the specified bits
{z();
for(int i = 0; i < width; ++i)
{z();
set(start+i, !getBit(start+i));
}
}
//D2 Boolean operations
boolean equals(int a, int b, int width) // Whether a == b
{z();
for(int i = 0; i < width; ++i)
{z();
if (getBit(a+i) != getBit(b+i))
{z();
return false;
}
}
z();
return true;
}
boolean notEquals(int a, int b, int width) // Whether a != b
{z(); return !equals(a, b, width);
}
boolean lessThan(int a, int b, int width) // Whether a < b
{z();
for(int i = width; i > 0; --i)
{z();
if (!getBit(a+i-1) && getBit(b+i-1)) {z(); return true;}
if ( getBit(a+i-1) && !getBit(b+i-1)) {z(); return false;}
}
z();
return false;
}
boolean lessThanOrEqual(int a, int b, int width) // Whether a <= b
{z();
return lessThan(a, b, width) || equals(a, b, width);
}
boolean greaterThan(int a, int b, int width) // Whether a > b
{z();
return !lessThan(a, b, width) && !equals(a, b, width);
}
boolean greaterThanOrEqual(int a, int b, int width) // Whether a >= b
{z();
return !lessThan(a, b, width);
}
//D1 Patterns // Pattern the memory to make testing more interesting
void alternating(int b) // Alternate between 0 and 1 in blocks of the specified size
{z();
final int N = size();
boolean v = false;
for(int i = 0, j = 0; i < N; ++i, ++j)
{z(); if (j == b) {j = 0; v = !v;}
set(i, v);
}
}
//D0 // Tests
static void test_set_get()
{Memory m = memory(currentTestName(), 8);
m.set(2, 2, 3);
m.set(5, 1, 1);
ok(m.getInt(5, 2), 1);
ok(m.getInt(2, 2), 3);
ok(m.getInt(2, 4), 11);
ok(m.isAllOnes(2, 2)); ok(!m.isAllOnes(2, 4));
ok(m.isAllZero(0, 2)); ok(!m.isAllZero(0, 4));
}
static void test_zero_ones()
{Memory m = memory(currentTestName(), 8);
m.ones(2, 4);
m.zero(4, 1);
ok(m.getInt(2, 4), 11);
}
static void test_copy()
{Memory m = memory(currentTestName(), 8);
m.ones(1, 2);
//stop(m);
ok(""+m, """
Memory: test_copy
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0006
""");
m.copyHigh(5, 1, 2);
//stop(m);
ok(""+m, """
Memory: test_copy
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0066
""");
}
static void test_invert()
{Memory m = memory(currentTestName(), 8);
m.ones(1, 2);
//stop(m);
ok(""+m, """
Memory: test_invert
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0006
""");
m.invert(2, 2);
//stop(m);
ok(""+m, """
Memory: test_invert
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 000a
""");
}
static void test_boolean()
{for (int i = 0; i <= 2; i++)
{for (int j = 0; j <= 2; j++)
{Memory m = memory(currentTestName(), 8);
m.set(0, 4, i);
m.set(4, 4, j);
ok(m.equals (0, 4, 4) == (i == j));
ok(m.notEquals (0, 4, 4) == (i != j));
ok(m.lessThan (0, 4, 4) == (i < j));
ok(m.lessThanOrEqual (0, 4, 4) == (i <= j));
ok(m.greaterThan (0, 4, 4) == (i > j));
ok(m.greaterThanOrEqual(0, 4, 4) == (i >= j));
}
}
}
static void test_alternating()
{Memory m = memory(currentTestName(), 256);
m.alternating(4);
//stop(m);
ok(""+m, """
Memory: test_alternating
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0
""");
}
static void test_all_zeros_and_ones()
{Memory m = memory(currentTestName(), 256);
m.alternating(4);
m.ones();
//stop(m);
ok(""+m, """
Memory: test_all_zeros_and_ones
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff
""");
m.zero();
//stop(m);
ok(""+m, """
Memory: test_all_zeros_and_ones
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
""");
}
static void oldTests() // Tests thought to be in good shape
{test_set_get();
test_zero_ones();
test_copy();
test_invert();
test_boolean();
test_alternating();
test_all_zeros_and_ones();
}
static void newTests() // Tests being worked on
{oldTests();
}
public static void main(String[] args) // Test if called as a program
{try // Get a traceback in a format clickable in Geany if something goes wrong to speed up debugging.
{if (github_actions) oldTests(); else newTests(); // Tests to run
if (github_actions) // Coverage analysis
{coverageAnalysis(sourceFileName(), 12);
}
testSummary(); // Summarize test results
System.exit(testsFailed);
}
catch(Exception e) // Get a traceback in a format clickable in Geany
{System.err.println(e);
System.err.println(fullTraceBack(e));
System.exit(1);
}
}
}