UNPKG

@microbit/microbit-fs

Version:

Manipulate files in a micro:bit MicroPython Intel Hex file.

91 lines 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUint64 = getUint64; exports.getUint32 = getUint32; exports.getUint16 = getUint16; exports.getUint8 = getUint8; exports.getString = getString; /** * Utilities for retrieving data from MemoryMap instances from the nrf-intel-hex * library. */ const common_js_1 = require("./common.js"); /** * Reads a 64 bit little endian number from an Intel Hex memory map. * * Any missing data in that address range that is not contained inside the * MemoryMap is filled with 0xFF. * * @param intelHexMap - Memory map of the Intel Hex data. * @param address - Start address of the 32 bit number. * @returns Number with the unsigned integer representation of those 8 bytes. */ function getUint64(intelHexMap, address) { const uint64Data = intelHexMap.slicePad(address, 8, 0xff); // Typed arrays use the native endianness, force little endian with DataView return new DataView(uint64Data.buffer).getUint32(0, true /* little endian */); } /** * Reads a 32 bit little endian number from an Intel Hex memory map. * * Any missing data in that address range that is not contained inside the * MemoryMap is filled with 0xFF. * * @param intelHexMap - Memory map of the Intel Hex data. * @param address - Start address of the 32 bit number. * @returns Number with the unsigned integer representation of those 4 bytes. */ function getUint32(intelHexMap, address) { const uint32Data = intelHexMap.slicePad(address, 4, 0xff); // Typed arrays use the native endianness, force little endian with DataView return new DataView(uint32Data.buffer).getUint32(0, true /* little endian */); } /** * Reads a 16 bit little endian number from an Intel Hex memory map. * * Any missing data in that address range that is not contained inside the * MemoryMap is filled with 0xFF. * * @param intelHexMap - Memory map of the Intel Hex data. * @param address - Start address of the 16 bit number. * @returns Number with the unsigned integer representation of those 2 bytes. */ function getUint16(intelHexMap, address) { const uint16Data = intelHexMap.slicePad(address, 2, 0xff); // Typed arrays use the native endianness, force little endian with DataView return new DataView(uint16Data.buffer).getUint16(0, true /* little endian */); } /** * Reads a 8 bit number from an Intel Hex memory map. * * If the data is not contained inside the MemoryMap it returns 0xFF. * * @param intelHexMap - Memory map of the Intel Hex data. * @param address - Start address of the 16 bit number. * @returns Number with the unsigned integer representation of those 2 bytes. */ function getUint8(intelHexMap, address) { const uint16Data = intelHexMap.slicePad(address, 1, 0xff); return uint16Data[0]; } /** * Decodes a UTF-8 null terminated string stored in the Intel Hex data at * the indicated address. * * @param intelHexMap - Memory map of the Intel Hex data. * @param address - Start address for the string. * @returns String read from the Intel Hex data. */ function getString(intelHexMap, address) { const memBlock = intelHexMap.slice(address).get(address); let iStrEnd = 0; while (iStrEnd < memBlock.length && memBlock[iStrEnd] !== 0) iStrEnd++; if (iStrEnd === memBlock.length) { // Could not find a null character to indicate the end of the string return ''; } const stringBytes = memBlock.slice(0, iStrEnd); return (0, common_js_1.bytesToStr)(stringBytes); } //# sourceMappingURL=hex-map-utils.js.map