Skip to content

Commit

Permalink
Refactor C64 option definitions and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanschramm committed Nov 19, 2023
1 parent 119491a commit 3241cd5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
37 changes: 25 additions & 12 deletions retroload-lib/src/encoding/adapter/C64GenericAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,40 @@ import {unidentifiable, type FormatIdentification} from './AdapterDefinition.js'
import {type AdapterDefinition} from './AdapterDefinition.js';
import {c64machineOption} from './options/C64Options.js';

const c64typeOption: ArgumentOptionDefinition<string> = {
enum C64Type {
basic = 'basic',
data = 'data',
prg = 'prg',
}

type C64TypeStrings = keyof typeof C64Type;
const c64TypeList = Object.keys(C64Type).join(', ');

const c64typeOption: ArgumentOptionDefinition<C64Type> = {
name: 'c64type',
label: 'C64 file type',
description: 'C64: File type. Possible types: basic, data, prg',
description: `C64: File type. Possible types: ${c64TypeList}`,
argument: 'type',
required: true,
common: false,
type: 'text',
enum: ['basic', 'data', 'prg'],
parse: (v) => v,
enum: Object.keys(C64Type),
parse(v) {
const vCasted = v as C64TypeStrings;
if (!Object.keys(C64Type).includes(vCasted)) {
throw new InvalidArgumentError(c64typeOption.name, `Option c64type is required and expected to be one of the following values: ${c64TypeList}`);
}

return C64Type[vCasted];
},
};

const definition: AdapterDefinition = {
name: 'C64 (Generic data)',
internalName: 'c64generic',
targetName: C64Encoder.getTargetName(),
options: [
shortpilotOption, // (not available for .tap)
shortpilotOption,
c64typeOption,
nameOption,
loadOption,
Expand All @@ -40,10 +56,7 @@ function identify(_filename: string, _ba: BufferAccess): FormatIdentification {
}

function encode(recorder: RecorderInterface, ba: BufferAccess, options: OptionContainer) {
const type: string = options.getArgument(c64typeOption);
if (!['basic', 'data', 'prg'].includes(type)) {
throw new InvalidArgumentError(c64typeOption.name, 'Option c64type is required and expected to be set to "basic", "data" or "prg".');
}
const type = options.getArgument(c64typeOption);
const loadAddress = options.getArgument(loadOption);
const name = options.getArgument(nameOption);
if (name.length > 16) {
Expand All @@ -56,15 +69,15 @@ function encode(recorder: RecorderInterface, ba: BufferAccess, options: OptionCo
);
e.begin();
switch (type) {
case 'basic':
case C64Type.basic:
checkLoadAddress(loadAddress);
e.recordBasic(loadAddress ?? 0x1100, name.padEnd(16, ' '), ba);
break;
case 'prg':
case C64Type.prg:
checkLoadAddress(loadAddress);
e.recordPrg(loadAddress ?? 0x1100, name.padEnd(16, ' '), ba);
break;
case 'data':
case C64Type.data:
e.recordData(name.padEnd(16, ' '), ba);
break;
default:
Expand Down
9 changes: 5 additions & 4 deletions retroload-lib/src/encoding/adapter/options/C64Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ export enum C64MachineType {
vic20pal = 'vic20pal',
vic20ntsc = 'vic20ntsc',
}

const c64MachineTypeDefault = C64MachineType.c64pal;
type C64MachineTypeStrings = keyof typeof C64MachineType;
const c64MachineTypeList = Object.keys(C64MachineType).join(', ');

export const c64machineOption: ArgumentOptionDefinition<C64MachineType> = {
name: 'c64machine',
label: 'Machine',
description: 'C64: Machine type. Possible types: c64pal, c64ntsc, vic20pal, vic20ntsc. Default: c64pal',
description: `C64: Machine type. Possible types: ${c64MachineTypeList}. Default: ${c64MachineTypeDefault}`,
argument: 'machine',
required: false,
common: true,
type: 'text',
enum: Object.keys(C64MachineType),
parse(v) {
if (v === '') {
return C64MachineType.c64pal;
return c64MachineTypeDefault;
}
const vCasted = v as C64MachineTypeStrings;
if (Object.keys(C64MachineType).includes(vCasted)) {
return C64MachineType[vCasted];
}

throw new InvalidArgumentError('c64machine', `Option c64machine is expected to be one of the following values: ${Object.keys(C64MachineType).join(', ')}`);
throw new InvalidArgumentError('c64machine', `Option c64machine is expected to be one of the following values: ${c64MachineTypeList}`);
},
};

0 comments on commit 3241cd5

Please sign in to comment.