|
| 1 | +package cc.calliope.mini |
| 2 | + |
| 3 | +import java.io.File |
| 4 | + |
| 5 | +class HexParser(private val path: String) { |
| 6 | + private fun calculateChecksum(address: UShort, type: Int, data: ByteArray): UByte { |
| 7 | + var crc: UInt = data.size.toUByte() + |
| 8 | + (address.toUInt() shr 8).toUByte() + |
| 9 | + ((address.toUInt() and 0xFFu) + type.toUInt()).toUByte() |
| 10 | + for (b in data) { |
| 11 | + crc = (crc + b.toUByte()).toUByte().toUInt() |
| 12 | + } |
| 13 | + return ((0x100u - crc) and 0xFFu).toUByte() |
| 14 | + } |
| 15 | + |
| 16 | + fun parse(handleDataEntry: (Long, ByteArray, Int, Boolean) -> Unit) { |
| 17 | + val file = File(path) |
| 18 | + val reader = file.bufferedReader() |
| 19 | + |
| 20 | + var isUniversal = false |
| 21 | + var addressHi: UInt = 0u |
| 22 | + var dataType = 0 |
| 23 | + |
| 24 | + reader.useLines { lines -> |
| 25 | + lines.forEach { line -> |
| 26 | + var beginIndex = 0 |
| 27 | + var endIndex = 1 |
| 28 | + |
| 29 | + if (line.isEmpty() || line[beginIndex] != ':') return@forEach |
| 30 | + beginIndex = endIndex |
| 31 | + |
| 32 | + endIndex = beginIndex + 2 |
| 33 | + val length = line.substring(beginIndex, endIndex).toInt(16).toUByte() |
| 34 | + beginIndex = endIndex |
| 35 | + |
| 36 | + endIndex = beginIndex + 4 |
| 37 | + val addressLo = line.substring(beginIndex, endIndex).toUInt(16) |
| 38 | + beginIndex = endIndex |
| 39 | + |
| 40 | + endIndex = beginIndex + 2 |
| 41 | + val type = line.substring(beginIndex, endIndex).toInt(16) |
| 42 | + beginIndex = endIndex |
| 43 | + |
| 44 | + endIndex = beginIndex + 2 * length.toInt() |
| 45 | + if (endIndex > line.length) return@forEach |
| 46 | + val payload = line.substring(beginIndex, endIndex) |
| 47 | + beginIndex = endIndex |
| 48 | + |
| 49 | + endIndex = beginIndex + 2 |
| 50 | + if (endIndex > line.length) return@forEach |
| 51 | + val checksum = line.substring(beginIndex, endIndex).toUIntOrNull(16)?.toUByte() ?: return@forEach |
| 52 | + |
| 53 | + val data = payload.hexStringToByteArray() |
| 54 | + val calculatedChecksum = calculateChecksum(addressLo.toUShort(), type, data) |
| 55 | + if (checksum != calculatedChecksum) { |
| 56 | + return@forEach |
| 57 | + } |
| 58 | + |
| 59 | + when (type) { |
| 60 | + 0, 13 -> { // Data |
| 61 | + val position = addressHi + addressLo |
| 62 | + if (data.size == length.toInt()) { |
| 63 | + handleDataEntry(position.toLong(), data, dataType, isUniversal) |
| 64 | + } |
| 65 | + } |
| 66 | + 1 -> return // EOF |
| 67 | + 2 -> { // EXT SEGMENT ADDRESS |
| 68 | + val segment = payload.toUInt(16) |
| 69 | + addressHi = segment shl 4 |
| 70 | + } |
| 71 | + 3 -> { /* START SEGMENT ADDRESS */ } |
| 72 | + 4 -> { // EXT LINEAR ADDRESS |
| 73 | + val segment = payload.toUInt(16) |
| 74 | + addressHi = segment shl 16 |
| 75 | + } |
| 76 | + 5 -> { /* START LINEAR ADDRESS */ } |
| 77 | + 10 -> { // Block Start Address |
| 78 | + isUniversal = true |
| 79 | + val dataTypeField = line.substring(9, 13) |
| 80 | + dataType = when (dataTypeField) { |
| 81 | + "9900" -> 1 |
| 82 | + "9903" -> 2 |
| 83 | + else -> dataType |
| 84 | + } |
| 85 | + } |
| 86 | + else -> { /* OTHER */ } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fun String.hexStringToByteArray(): ByteArray { |
| 94 | + val len = this.length |
| 95 | + val data = ByteArray(len / 2) |
| 96 | + var i = 0 |
| 97 | + while (i < len) { |
| 98 | + val byte = this.substring(i, i + 2).toInt(16).toByte() |
| 99 | + data[i / 2] = byte |
| 100 | + i += 2 |
| 101 | + } |
| 102 | + return data |
| 103 | +} |
0 commit comments