Skip to content

Commit 6d591be

Browse files
ihex: Add convertExtSegToLinAddressRecord() function.
1 parent db915b0 commit 6d591be

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/__tests__/ihex.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Tests for ihex module.
3+
*
4+
* (c) 2020 Micro:bit Educational Foundation and contributors.
5+
* SPDX-License-Identifier: MIT
6+
*/
17
import * as ihex from '../ihex';
28

39
describe('Test createRecord() for standard records', () => {
@@ -397,6 +403,43 @@ describe('Test convertRecordTo()', () => {
397403
});
398404
});
399405

406+
describe('Test convertExtSegToLinAddressRecord()', () => {
407+
it('Converts valid Extended Segment Address Records into Linear', () => {
408+
expect(ihex.convertExtSegToLinAddressRecord(':020000020000FC')).toEqual(
409+
':020000040000FA'
410+
);
411+
expect(ihex.convertExtSegToLinAddressRecord(':020000021000EC')).toEqual(
412+
':020000040001F9'
413+
);
414+
expect(ihex.convertExtSegToLinAddressRecord(':020000022000DC')).toEqual(
415+
':020000040002F8'
416+
);
417+
expect(ihex.convertExtSegToLinAddressRecord(':020000023000CC')).toEqual(
418+
':020000040003F7'
419+
);
420+
expect(ihex.convertExtSegToLinAddressRecord(':020000024000BC')).toEqual(
421+
':020000040004F6'
422+
);
423+
expect(ihex.convertExtSegToLinAddressRecord(':0200000270008C')).toEqual(
424+
':020000040007F3'
425+
);
426+
});
427+
428+
it('Throws error with an invalid Extended Segment Address', () => {
429+
expect(() => {
430+
ihex.convertExtSegToLinAddressRecord(':0200000270018C');
431+
}).toThrow('Invalid Extended Segment Address record');
432+
433+
expect(() => {
434+
ihex.convertExtSegToLinAddressRecord(':0300000271008C');
435+
}).toThrow('Invalid Extended Segment Address record');
436+
437+
expect(() => {
438+
ihex.convertExtSegToLinAddressRecord(':030000027000FF8C');
439+
}).toThrow('Invalid Extended Segment Address record');
440+
});
441+
});
442+
400443
describe('Test iHexToRecordStrs()', () => {
401444
it('Normal hex file string', () => {
402445
expect(

src/ihex.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,28 @@ function convertRecordTo(iHexRecord: string, recordType: RecordType): string {
360360
return `${START_CODE_STR}${recordContentStr}${checksumStr}`;
361361
}
362362

363+
/**
364+
* Converts and Extended Segment Linear Address record to an Extended Linear
365+
* Address record.
366+
*
367+
* @throws {Error} When the record does not contain exactly 2 bytes.
368+
* @throws {Error} When the Segmented Address is not a multiple of 0x1000.
369+
*
370+
* @param iHexRecord Intel hex record line without line terminator.
371+
*/
372+
function convertExtSegToLinAddressRecord(iHexRecord: string): string {
373+
const segmentAddress = getRecordData(iHexRecord);
374+
if (
375+
segmentAddress.length !== 2 ||
376+
segmentAddress[0] & 0xf || // Only process multiples of 0x1000
377+
segmentAddress[1] !== 0
378+
) {
379+
throw new Error(`Invalid Extended Segment Address record ${iHexRecord}`);
380+
}
381+
const startAddress = segmentAddress[0] << 12;
382+
return extLinAddressRecord(startAddress);
383+
}
384+
363385
/**
364386
* Separates an Intel Hex file (string) into an array of Record strings.
365387
*
@@ -386,8 +408,8 @@ function iHexToRecordStrs(iHexStr: string): string[] {
386408
* This is useful to identify the expected max size of the data records for an
387409
* Intel Hex, and then be able to generate new custom records of the same size.
388410
*
389-
* @param iHexRecords Array of Intel Hex Records
390-
* @returns Number of data bytes th
411+
* @param iHexRecords Array of Intel Hex Records.
412+
* @returns Number of data bytes in a full record.
391413
*/
392414
function findDataFieldLength(iHexRecords: string[]): number {
393415
let maxDataBytes = 16;
@@ -423,6 +445,7 @@ export {
423445
blockEndRecord,
424446
paddedDataRecord,
425447
convertRecordTo,
448+
convertExtSegToLinAddressRecord,
426449
iHexToRecordStrs,
427450
findDataFieldLength,
428451
};

0 commit comments

Comments
 (0)