-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32ca262
commit 3b2f12b
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {getLocalPathByDirAndFile} from '../Examples.js'; | ||
import {BufferAccess} from '../common/BufferAccess.js'; | ||
import {convert} from './ConverterManager.js'; | ||
import * as fs from 'fs'; | ||
|
||
test('ConverterManager calls convert function of ConverterDefinition', () => { | ||
const data = BufferAccess.createFromNodeBuffer(fs.readFileSync(getLocalPathByDirAndFile('kc851_tap', 'rl.com'))); | ||
|
||
const result = convert(data, 'kctap', {}); | ||
|
||
const expectedData = BufferAccess.createFromNodeBuffer(fs.readFileSync(getLocalPathByDirAndFile('kc851_tap', 'rl.tap'))); | ||
expect(result.asHexDump()).toBe(expectedData.asHexDump()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {type BufferAccess} from '../common/BufferAccess.js'; | ||
import {FormatNotFoundError} from '../common/Exceptions.js'; | ||
import {OptionContainer, type OptionValues} from '../encoding/Options.js'; | ||
import {converters} from './ConverterProvider.js'; | ||
import {type ConverterDefinition} from './converter/ConverterDefinition.js'; | ||
|
||
export function convert(data: BufferAccess, identifier: string, options: OptionValues): BufferAccess { | ||
const chosenConverters = converters.filter((c: ConverterDefinition) => c.identifier === identifier); | ||
|
||
if (chosenConverters.length === 0) { | ||
throw new FormatNotFoundError(identifier); | ||
} | ||
|
||
const converter = chosenConverters[0]; | ||
|
||
return converter.convert(data, new OptionContainer(options)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {type ConverterDefinition} from './converter/ConverterDefinition.js'; | ||
import KcTapConverter from './converter/KcTapConverter.js'; | ||
|
||
export const converters: ConverterDefinition[] = [ | ||
KcTapConverter, | ||
]; |
9 changes: 9 additions & 0 deletions
9
retroload-lib/src/conversion/converter/ConverterDefinition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {type BufferAccess} from '../../common/BufferAccess.js'; | ||
import {type OptionContainer, type PublicOptionDefinition} from '../../encoding/Options.js'; | ||
|
||
export type ConverterDefinition = { | ||
readonly name: string; | ||
readonly identifier: string; | ||
readonly options: PublicOptionDefinition[]; | ||
readonly convert: (ba: BufferAccess, options: OptionContainer) => BufferAccess; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {BufferAccess} from '../../common/BufferAccess.js'; | ||
import {type OptionContainer} from '../../encoding/Options.js'; | ||
import {type ConverterDefinition} from './ConverterDefinition.js'; | ||
|
||
const definition: ConverterDefinition = { | ||
name: 'KC .TAP-File', | ||
identifier: 'kctap', | ||
options: [], | ||
convert, | ||
}; | ||
export default definition; | ||
|
||
function convert(data: BufferAccess, _options: OptionContainer): BufferAccess { | ||
const blocks = data.chunksPadded(128, 0x00); | ||
const outBa = BufferAccess.create(16 + (1 + blocks.length) * 129); | ||
|
||
// TODO: use options | ||
const header = createHeader( | ||
'RL', | ||
'COM', | ||
0x0300, | ||
0x0300 + data.length() - 1, | ||
0x0300, | ||
); | ||
|
||
// TAP header | ||
outBa.writeUint8(0xc3); | ||
outBa.writeAsciiString('KC-TAPE by AF. '); | ||
|
||
// header block (FCB) | ||
outBa.writeUint8(0); | ||
outBa.writeBa(header); | ||
|
||
// data blocks | ||
for (let i = 0; i < blocks.length; i++) { | ||
outBa.writeUint8(i === blocks.length - 1 ? 0xff : i + 1); | ||
outBa.writeBa(blocks[i]); | ||
} | ||
|
||
return outBa; | ||
} | ||
|
||
function createHeader(name: string, fileType: string, loadAddress: number, endAddress: number, startAddress: number): BufferAccess { | ||
const header = BufferAccess.create(128); | ||
|
||
header.writeAsciiString(name, 8, 0x00); | ||
header.writeAsciiString(fileType); | ||
header.writeUint8(0x00); // reserved | ||
header.writeUint8(0x00); // reserved | ||
header.writeUint16Le(0x0000); // PSUM (block checksum(?)) | ||
header.writeUint8(0x00); // ARB (internal working cell(?)) | ||
header.writeUint8(0x03); // BLNR (block number(?)) (count?) | ||
header.writeUint16Le(loadAddress); // AADR | ||
header.writeUint16Le(endAddress); // EADR | ||
header.writeUint16Le(startAddress); // SADR | ||
header.writeUint8(0x00); // SBY (protection byte) | ||
|
||
return header; | ||
} |