|
| 1 | +package de.holger_oehm.pic.progmem; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | +import java.util.SortedMap; |
| 7 | +import java.util.TreeMap; |
| 8 | + |
| 9 | +public class PicMemory { |
| 10 | + private static final int CHUNK_SIZE = 0x40; |
| 11 | + private static final int CHUNK_MASK = ~(CHUNK_SIZE - 1); |
| 12 | + |
| 13 | + private static class Chunk { |
| 14 | + private final byte[] chunkBytes = new byte[CHUNK_SIZE]; |
| 15 | + |
| 16 | + public Chunk() { |
| 17 | + Arrays.fill(chunkBytes, (byte) 0xff); |
| 18 | + } |
| 19 | + |
| 20 | + private int setBytes(final int address, final byte[] bytes) { |
| 21 | + int result = 0; |
| 22 | + for (int i = 0; i < bytes.length; i++) { |
| 23 | + final byte b = bytes[i]; |
| 24 | + final int localIdx = address + i; |
| 25 | + if (localIdx >= CHUNK_SIZE) { |
| 26 | + break; |
| 27 | + } |
| 28 | + chunkBytes[localIdx] = b; |
| 29 | + result++; |
| 30 | + } |
| 31 | + return result; |
| 32 | + } |
| 33 | + |
| 34 | + private byte getByte(final int address) { |
| 35 | + return chunkBytes[address]; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private final SortedMap<Integer, Chunk> chunkMemory = new TreeMap<>(); |
| 40 | + |
| 41 | + public int getByte(final int address) { |
| 42 | + final int chunkAddress = chunkAddress(address); |
| 43 | + final Chunk chunk = chunkMemory.get(chunkAddress); |
| 44 | + if (chunk == null) { |
| 45 | + return 0xff; |
| 46 | + } |
| 47 | + final byte byteValue = chunk.getByte(address - chunkAddress); |
| 48 | + return byteValue & 0xff; |
| 49 | + } |
| 50 | + |
| 51 | + public void setBytes(final int address, final byte[] bytes) { |
| 52 | + final int chunkAddress = chunkAddress(address); |
| 53 | + final Chunk chunk; |
| 54 | + if (chunkMemory.containsKey(chunkAddress)) { |
| 55 | + chunk = chunkMemory.get(chunkAddress); |
| 56 | + } else { |
| 57 | + chunk = new Chunk(); |
| 58 | + chunkMemory.put(chunkAddress, chunk); |
| 59 | + } |
| 60 | + final int wrote = chunk.setBytes(address - chunkAddress, bytes); |
| 61 | + if (wrote < bytes.length) { |
| 62 | + setBytes(address + wrote, Arrays.copyOfRange(bytes, wrote, bytes.length)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private int chunkAddress(final int address) { |
| 67 | + return address & CHUNK_MASK; |
| 68 | + } |
| 69 | + |
| 70 | + public void setBytes(final int address, final int... integers) { |
| 71 | + final byte[] bytes = new byte[integers.length]; |
| 72 | + for (int i = 0; i < integers.length; i++) { |
| 73 | + final int value = integers[i]; |
| 74 | + if (0 > value || value > 255) { |
| 75 | + throw new IllegalArgumentException("all values must be between 0 and 255: " + Arrays.toString(integers)); |
| 76 | + } |
| 77 | + bytes[i] = (byte) value; |
| 78 | + } |
| 79 | + setBytes(address, bytes); |
| 80 | + } |
| 81 | + |
| 82 | + public List<Integer> getChunkAddresses() { |
| 83 | + final List<Integer> result = new ArrayList<>(); |
| 84 | + result.addAll(chunkMemory.keySet()); |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments