Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
christo committed Dec 19, 2024
1 parent 87032c3 commit bcb2b7a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions client/src/machine/asm/Disassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class Disassembler {
*/
edictAwareInstruction(currentByte: number, lc: LabelsComments): InstructionLike {

// TODO refactor bigly, this is insane-o

// check if there's an edict n ahead of currentIndex
const edictAhead = (n: number) => this.disMeta.getEdict(this.currentIndex + n) !== undefined;

Expand Down Expand Up @@ -257,17 +259,23 @@ class Disassembler {
*/
private mkInstruction(opcode: number, labelsComments: LabelsComments) {
const numInstructionBytes = Mos6502.ISA.numBytes(opcode) || 1
// interpret as instruction
let firstOperandByte = 0;
let secondOperandByte = 0;
if (numInstructionBytes > 1) {
firstOperandByte = this.eatByteOrDie();
}
if (numInstructionBytes === 3) {
secondOperandByte = this.eatByteOrDie();
const bytesRemaining = this.fb.getBytes().length - this.currentIndex;
if (numInstructionBytes <= bytesRemaining) {
// default operands are 0
let firstOperandByte = 0;
let secondOperandByte = 0;
if (numInstructionBytes === 2) {
firstOperandByte = this.fb.read8(this.currentIndex + 1);
} else if (numInstructionBytes === 3) {
secondOperandByte = this.fb.read8(this.currentIndex + 2);
}
this.currentIndex += (numInstructionBytes - 1); // already consumed opcode
const il = new FullInstruction(this.iset.instruction(opcode), firstOperandByte, secondOperandByte);
return new FullInstructionLine(il, labelsComments);
} else {
throw Error(`Not enough bytes to disassemble instruction at index ${this.currentIndex}`);
}
const il = new FullInstruction(this.iset.instruction(opcode), firstOperandByte, secondOperandByte);
return new FullInstructionLine(il, labelsComments);

}

/**
Expand Down

0 comments on commit bcb2b7a

Please sign in to comment.