Skip to content

Commit

Permalink
Refactor KcBasicType option definition
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanschramm committed Nov 19, 2023
1 parent 3241cd5 commit 57f6e8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
37 changes: 23 additions & 14 deletions retroload-lib/src/encoding/adapter/KcBasicGenericAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@ import {InvalidArgumentError} from '../../common/Exceptions.js';
import {type RecorderInterface} from '../recorder/RecorderInterface.js';
import {type AdapterDefinition} from './AdapterDefinition.js';

const typeMap = {
program: 0xd3,
data: 0xd4,
ascii: 0xd5,
};
enum KcBasicType {
program = 'program',
data = 'data',
ascii = 'ascii',
}
const kcBasicTypeDefault = KcBasicType.program;
type KcBasicTypeString = keyof typeof KcBasicType;
const kcBasicTypeList = Object.keys(KcBasicType).join(', ');

const kcBasicTypeOption: ArgumentOptionDefinition<string> = {
const kcBasicTypeOption: ArgumentOptionDefinition<KcBasicType> = {
name: 'kcbasictype',
label: 'BASIC data type',
description: `KC BASIC: Type of BASIC data to be loaded. Possible types: ${Object.keys(typeMap).join(', ')}`,
description: `KC BASIC: Type of BASIC data to be loaded. Possible types: ${kcBasicTypeList}. Default: ${kcBasicTypeDefault}`,
argument: 'type',
common: false,
required: false,
type: 'text',
enum: Object.keys(typeMap),
enum: Object.keys(KcBasicType),
parse(v) {
if (v === undefined || v === '') {
return 'program';
if (v === '') {
return KcBasicType.program;
}
if (!Object.keys(typeMap).includes(v)) {
throw new InvalidArgumentError(this.name, `Option ${this.name} is expected to be one of: ${Object.keys(typeMap).join(', ')}`);
const vCasted = v as KcBasicTypeString;
if (Object.keys(KcBasicType).includes(v)) {
return KcBasicType[vCasted];
}

return v;
throw new InvalidArgumentError(this.name, `Option ${this.name} is expected to be one of: ${kcBasicTypeList}`);
},
};

Expand Down Expand Up @@ -61,6 +65,11 @@ export default definition;
const headerSize = 3 + 8; // basic header + filename
const blockSize = 128;
const maxFileNameLength = 8;
const typeMap = {
program: 0xd3,
data: 0xd4,
ascii: 0xd5,
};

function identify(_filename: string, _ba: BufferAccess) {
return unidentifiable;
Expand All @@ -73,7 +82,7 @@ function encode(recorder: RecorderInterface, ba: BufferAccess, options: OptionCo
throw new InvalidArgumentError('name', `Maximum length of filename (${maxFileNameLength}) exceeded.`);
}
const copyProtected = options.isFlagSet(kcBasicProtectedOption);
const basicType = options.getArgument(kcBasicTypeOption) as keyof typeof typeMap;
const basicType = options.getArgument(kcBasicTypeOption);
const typeByte: number = typeMap[basicType] + (copyProtected ? 0x04 : 0x00);

const firstBlockBa = BufferAccess.create(128);
Expand Down
4 changes: 2 additions & 2 deletions retroload-lib/src/encoding/adapter/options/C64Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum C64MachineType {
vic20ntsc = 'vic20ntsc',
}
const c64MachineTypeDefault = C64MachineType.c64pal;
type C64MachineTypeStrings = keyof typeof C64MachineType;
type C64MachineTypeString = keyof typeof C64MachineType;
const c64MachineTypeList = Object.keys(C64MachineType).join(', ');

export const c64machineOption: ArgumentOptionDefinition<C64MachineType> = {
Expand All @@ -24,7 +24,7 @@ export const c64machineOption: ArgumentOptionDefinition<C64MachineType> = {
if (v === '') {
return c64MachineTypeDefault;
}
const vCasted = v as C64MachineTypeStrings;
const vCasted = v as C64MachineTypeString;
if (Object.keys(C64MachineType).includes(vCasted)) {
return C64MachineType[vCasted];
}
Expand Down

0 comments on commit 57f6e8f

Please sign in to comment.