Skip to content

Commit

Permalink
Simplify C64Encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanschramm committed Nov 16, 2023
1 parent 48821fe commit 7761161
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions retroload-lib/src/encoding/encoder/C64Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const fileTypePrg = 0x03;
// const fileTypeSeqFileHeader = 0x04;
// const fileTypeEndOfTapeMarker = 0x05;

const pulseShort = 8 * 0x2f;
const pulseMedium = 8 * 0x42;
const pulseLong = 8 * 0x56;

/**
* Encoder for C64 and VIC-20
*
Expand Down Expand Up @@ -50,66 +54,68 @@ export class C64Encoder extends AbstractEncoder {
}
}

recordShortPulse() {
this.recordPulse(8 * 0x2f);
public recordBasic(startAddress: number, filenameBuffer: string, dataBa: BufferAccess) {
// TODO: test
this.recordBasicOrPrg(fileTypeBasic, startAddress, filenameBuffer, dataBa);
}

recordMediumPulse() {
this.recordPulse(8 * 0x42);
public recordPrg(startAddress: number, filenameBuffer: string, dataBa: BufferAccess) {
this.recordBasicOrPrg(fileTypePrg, startAddress, filenameBuffer, dataBa);
}

recordLongPulse() {
this.recordPulse(8 * 0x56);
public recordData(_filenameBuffer: string, _dataBa: BufferAccess) {
// TODO: implement + test
throw new InternalError('recordData not implemented yet');
}

recordBit(value: number) {
override recordBit(value: number) {
if (value) {
this.recordMediumPulse();
this.recordShortPulse();
this.recordPulse(pulseMedium);
this.recordPulse(pulseShort);
} else {
this.recordShortPulse();
this.recordMediumPulse();
this.recordPulse(pulseShort);
this.recordPulse(pulseMedium);
}
}

override recordByte(byte: number) {
this.recordNewDataMarker();
let checkBit = 1;
for (let i = 0; i < 8; i += 1) {
const bit = ((byte & (1 << i)) === 0) ? 0 : 1;
checkBit ^= bit;
this.recordBit(bit);
}
this.recordBit(checkBit);
}

recordNewDataMarker() {
this.recordLongPulse();
this.recordMediumPulse();
private recordNewDataMarker() {
this.recordPulse(pulseLong);
this.recordPulse(pulseMedium);
}

recordEndOfDataMarker() {
this.recordLongPulse();
this.recordShortPulse();
private recordEndOfDataMarker() {
this.recordPulse(pulseLong);
this.recordPulse(pulseShort);
}

recordPilot(pulses: number) {
private recordPilot(pulses: number) {
for (let i = 0; i < pulses; i++) {
this.recordShortPulse();
this.recordPulse(pulseShort);
}
}

recordSyncChain() {
private recordSyncChain() {
const syncChain = new Uint8Array([0x89, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81]);
this.recordBytes(BufferAccess.createFromUint8Array(syncChain));
}

recordSyncChainRepeated() {
private recordSyncChainRepeated() {
const syncChain = new Uint8Array([0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01]);
this.recordBytes(BufferAccess.createFromUint8Array(syncChain));
}

override recordByte(byte: number) {
this.recordNewDataMarker();
let checkBit = 1;
for (let i = 0; i < 8; i += 1) {
const bit = ((byte & (1 << i)) === 0) ? 0 : 1;
checkBit ^= bit;
this.recordBit(bit);
}
this.recordBit(checkBit);
}

recordDataWithCheckByte(dataBa: BufferAccess) {
private recordDataWithCheckByte(dataBa: BufferAccess) {
let checkByte = 0;
for (let i = 0; i < dataBa.length(); i++) {
const byte = dataBa.getUint8(i);
Expand All @@ -119,21 +125,7 @@ export class C64Encoder extends AbstractEncoder {
this.recordByte(checkByte);
}

recordBasic(startAddress: number, filenameBuffer: string, dataBa: BufferAccess) {
// TODO: test
this.recordBasicOrPrg(fileTypeBasic, startAddress, filenameBuffer, dataBa);
}

recordPrg(startAddress: number, filenameBuffer: string, dataBa: BufferAccess) {
this.recordBasicOrPrg(fileTypePrg, startAddress, filenameBuffer, dataBa);
}

recordData(_filenameBuffer: string, _dataBa: BufferAccess) {
// TODO: implement + test
throw new InternalError('recordData not implemented yet');
}

recordBasicOrPrg(fileType: number, startAddress: number, filenameBuffer: string, dataBa: BufferAccess) {
private recordBasicOrPrg(fileType: number, startAddress: number, filenameBuffer: string, dataBa: BufferAccess) {
const headerBa = BufferAccess.create(192);
headerBa.writeUint8(fileType); // 1 byte: file type: prg or basic file
headerBa.writeUint16Le(startAddress); // 2 bytes: start address
Expand Down

0 comments on commit 7761161

Please sign in to comment.