/
Util.java
395 lines (336 loc) · 13.1 KB
/
Util.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
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
/*
* Util
* Copyright (c) 2018-2019 Federico Berti
* Last modified: 17/10/19 10:50
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package omegadrive.util;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import omegadrive.Device;
import omegadrive.memory.IMemoryProvider;
import omegadrive.memory.IMemoryRom;
import org.slf4j.Logger;
import java.io.*;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.zip.CRC32;
public class Util {
private final static Logger LOG = LogHelper.getLogger(Util.class.getSimpleName());
public static boolean verbose = false;
public static final int GEN_NTSC_MCLOCK_MHZ = 53693175;
public static final int GEN_PAL_MCLOCK_MHZ = 53203424;
public static final String OS_NAME = System.getProperty("os.name").toLowerCase();
public static final String NATIVE_SUBDIR = OS_NAME.contains("win") ? "windows" :
(OS_NAME.contains("mac") ? "osx" : "linux");
public static final long SECOND_IN_NS = Duration.ofSeconds(1).toNanos();
public static final long MILLI_IN_NS = Duration.ofMillis(1).toNanos();
public static final boolean randomInitRam =
Boolean.parseBoolean(System.getProperty("helios.random.init.ram", "false"));
public static final Random random;
static final int CACHE_LIMIT = Short.MIN_VALUE;
public static final VarHandle SHORT_BYTEARR_HANDLE = MethodHandles.byteArrayViewVarHandle(short[].class, ByteOrder.BIG_ENDIAN);
public static final VarHandle INT_BYTEARR_HANDLE = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.BIG_ENDIAN);
public static final VarHandle SHORT_BYTEBUF_HANDLE = MethodHandles.byteBufferViewVarHandle(short[].class, ByteOrder.BIG_ENDIAN);
public static final VarHandle INT_BYTEBUF_HANDLE = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.BIG_ENDIAN);
static final Integer[] negativeCache = new Integer[Short.MAX_VALUE + 2];
public static final ExecutorService executorService = Executors.newSingleThreadExecutor(new PriorityThreadFactory("util"));
static {
for (int i = 0, j = 0; i < negativeCache.length; i++) {
negativeCache[i] = j--;
}
long seed = System.currentTimeMillis();
random = new Random(seed);
LOG.info("Creating Random with seed: {}", seed);
LOG.info("Init RAM with random values: {}", randomInitRam);
Thread.setDefaultUncaughtExceptionHandler(Util::uncaughtException);
}
private static void uncaughtException(Thread t, Throwable e) {
LOG.error("uncaughtException fot thread: {}", t.getName(), e);
e.printStackTrace();
}
public static void sleep(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private static final Object lock = new Object();
public static void waitForever() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public static void waitOnBarrier(CyclicBarrier barrier, boolean verbose) {
try {
barrier.await();
} catch (Exception e) {
if (verbose) {
LOG.warn("Error on barrier", e);
} else {
LOG.info("Leaving barrier: {}", e.getClass().getName());
}
barrier.reset();
}
}
public static void waitOnBarrier(CyclicBarrier barrier) {
waitOnBarrier(barrier, true);
}
public static boolean trySignalCondition(Lock lock, Condition condition) {
if (lock.tryLock()) {
try {
condition.signal();
return true;
} finally {
lock.unlock();
}
}
return false;
}
public static void waitOnCondition(Lock lock, Condition condition) {
try {
lock.lock();
try {
condition.await();
} catch (Exception e) {
LOG.warn("Error on condition", e);
}
} finally {
lock.unlock();
}
}
public static void waitOnObject(Object object, long ms) {
synchronized (object) {
try {
object.wait(ms);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public static void waitOnObject(Object object) {
synchronized (object) {
try {
object.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public static byte[] initMemoryRandomBytes(byte[] mem) {
if (!randomInitRam) {
return mem;
}
for (int i = 0; i < mem.length; i++) {
mem[i] = (byte) Util.random.nextInt(0x100);
}
return mem;
}
public static ByteBuffer initMemoryRandomBytes(ByteBuffer mem) {
if (!randomInitRam) {
return mem;
}
for (int i = 0; i < mem.capacity(); i++) {
mem.put((byte) Util.random.nextInt(0x100));
}
return mem;
}
public static void registerJmx(Object object) {
JmxBridge.registerJmx(object);
}
//bit 1 -> true
public static boolean bitSetTest(long number, int position) {
return ((number & (1 << position)) != 0);
}
public static int readBufferByte(ByteBuffer b, int pos) {
return b.get(pos);
}
public static int readBufferWord(ByteBuffer b, int pos) {
assert (pos & 1) == 0;
return b.getShort(pos);
}
public static int readBufferLong(ByteBuffer b, int pos) {
assert (pos & 1) == 0;
return b.getInt(pos);
}
public static int readDataMask(byte[] src, int address, final int mask, Size size) {
return readData(src, address & mask, size);
}
public static int readData(byte[] src, int address, Size size) {
return switch (size) {
case WORD -> ((src[address] & 0xFF) << 8) | (src[address + 1] & 0xFF);
case LONG -> ((src[address] & 0xFF) << 24) | (src[address + 1] & 0xFF) << 16 |
(src[address + 2] & 0xFF) << 8 | (src[address + 3] & 0xFF);
case BYTE -> src[address];
};
}
public static void writeData(byte[] dest, int address, int data, Size size) {
switch (size) {
case WORD -> SHORT_BYTEARR_HANDLE.set(dest, address, (short) data);
case LONG -> INT_BYTEARR_HANDLE.set(dest, address, data);
case BYTE -> dest[address] = (byte) data;
}
}
public static void writeDataMask(byte[] dest, int address, int data, final int mask, Size size) {
writeData(dest, address & mask, data, size);
}
@Deprecated
public static int computeChecksum(IMemoryProvider memoryProvider) {
int res = 0;
//checksum is computed starting from byte 0x200
int i = 0x200;
int size = memoryProvider.getRomSize();
final int mask = memoryProvider.getRomMask();
for (; i < size - 1; i += 2) {
int val = Util.readDataMask(memoryProvider.getRomData(), i, mask, Size.WORD);
res = (res + val) & 0xFFFF;
}
//read final byte ??
res = size % 2 != 0 ? (res + memoryProvider.readRomByte(i)) & 0xFFFF : res;
return res;
}
public static String computeSha1Sum(byte[] data) {
Hasher h = Hashing.sha1().newHasher();
IntStream.range(0, data.length).forEach(i -> h.putByte(data[i]));
return BaseEncoding.base16().lowerCase().encode(h.hash().asBytes());
}
public static String computeSha1Sum(IMemoryRom rom){
return computeSha1Sum(rom.getRomData());
}
public static String computeCrc32(byte[] data) {
CRC32 crc32 = new CRC32();
IntStream.range(0, data.length).forEach(i -> crc32.update(data[i]));
return Long.toHexString(crc32.getValue());
}
public static String computeCrc32(IMemoryRom rom) {
return computeCrc32(rom.getRomData());
}
public static int log2(int n) {
if (n <= 0) throw new IllegalArgumentException();
return 31 - Integer.numberOfLeadingZeros(n);
}
public static String toStringValue(byte... data) {
return new String(data);
}
public static String th(int pos) {
return Integer.toHexString(pos);
}
public static String th(long pos) {
return Long.toHexString(pos);
}
public static byte[] getPaddedRom(byte[] data) {
int romSize = data.length;
int mask = getRomMask(romSize);
if (mask >= romSize) {
byte[] newrom = new byte[mask + 1];
Arrays.fill(newrom, (byte) 0xFF);
System.arraycopy(data, 0, newrom, 0, romSize);
return newrom;
}
return data;
}
public static int getRomMask(int size) {
int log2 = Util.log2(size);
int pow2 = (int) Math.pow(2, log2);
if (size == pow2) {
//size = 0x4000, mask = 0x3fff
return size - 1;
}
return (int) Math.pow(2, log2 + 1) - 1;
}
public static Integer getFromIntegerCache(int val) {
if (val < 0 && val >= CACHE_LIMIT) {
return negativeCache[-val];
}
return val;
}
public static byte[] serializeObject(Serializable obj) {
byte[] res = new byte[0];
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)
) {
oos.writeObject(obj);
oos.flush();
res = bos.toByteArray();
} catch (Exception e) {
LOG.error("Unable to serialize object: {}, {}", obj.getClass().getSimpleName(), e.getMessage());
}
if (res.length == 0) {
LOG.error("Unable to serialize object: {}", obj.getClass().getSimpleName());
}
return res;
}
public static Serializable deserializeObject(ByteBuffer data) {
return deserializeObject(data.array(), 0, data.capacity());
}
public static Serializable deserializeObject(byte[] data) {
return deserializeObject(data, 0, data.length);
}
public static Serializable deserializeObject(byte[] data, int offset, int len) {
if (data == null || data.length == 0 || offset < 0 || len > data.length) {
LOG.error("Unable to deserialize object of len: {}", data != null ? data.length : "null");
return null;
}
Serializable res = null;
try (
ByteArrayInputStream bis = new ByteArrayInputStream(data, offset, len);
ObjectInput in = new ObjectInputStream(bis)
) {
res = (Serializable) in.readObject();
} catch (Exception e) {
LOG.error("Unable to deserialize object of len: {}, {}", data.length, e.getMessage());
}
return res;
}
public static <T, V extends Device> Optional<V> getDeviceIfAny(Collection<T> deviceSet, Class<V> clazz) {
return deviceSet.stream().filter(t -> clazz.isAssignableFrom(t.getClass())).findFirst().map(clazz::cast);
}
public static <T, V extends Device> Set<V> getAllDevices(Collection<T> deviceSet, Class<V> clazz) {
return deviceSet.stream().filter(t -> clazz.isAssignableFrom(t.getClass())).map(clazz::cast).collect(Collectors.toSet());
}
//from Guava, Files::getNameWithoutExtension
public static String getNameWithoutExtension(String file) {
Objects.requireNonNull(file);
String fileName = new File(file).getName();
int dotIndex = fileName.lastIndexOf('.');
return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
}
public static int getBitFromByte(byte b, int bitPos) {
assert bitPos >= 0 && bitPos < 8;
return (b >> bitPos) & 1;
}
public static int getBitFromWord(short b, int bitPos) {
assert bitPos >= 0 && bitPos < 16;
return (b >> bitPos) & 1;
}
}