Skip to content

Commit

Permalink
fix(eeprom): EEPROM write fails after first attempt
Browse files Browse the repository at this point in the history
close #54
  • Loading branch information
urish committed Jul 16, 2020
1 parent 7466e56 commit e319f6f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/peripherals/eeprom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,38 @@ describe('EEPROM', () => {
expect(eepromBackend.memory[15]).toEqual(0x55);
expect(eepromBackend.memory[16]).toEqual(0xff);
});

it('should write two bytes sucessfully', () => {
const cpu = new CPU(new Uint16Array(0x1000));
const eepromBackend = new EEPROMMemoryBackend(1024);
const eeprom = new AVREEPROM(cpu, eepromBackend);

// Write 0x55 to address 15
cpu.writeData(EEDR, 0x55);
cpu.writeData(EEARL, 15);
cpu.writeData(EEARH, 0);
cpu.writeData(EECR, EEMPE);
cpu.writeData(EECR, EEPE);
eeprom.tick();
expect(cpu.cycles).toEqual(2);

// wait long enough time for the first write to finish
cpu.cycles += 10000000;
eeprom.tick();

// Write 0x66 to address 16
cpu.writeData(EEDR, 0x66);
cpu.writeData(EEARL, 16);
cpu.writeData(EEARH, 0);
cpu.writeData(EECR, EEMPE);
cpu.writeData(EECR, EEPE);
eeprom.tick();

// Ensure both writes took place
expect(cpu.cycles).toEqual(10000004);
expect(eepromBackend.memory[15]).toEqual(0x55);
expect(eepromBackend.memory[16]).toEqual(0x66);
});
});

describe('EEPROM erase', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/peripherals/eeprom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class AVREEPROM {
return true;
}
// Check for write-in-progress
if (this.writeCompleteCycles) {
if (this.cpu.cycles < this.writeCompleteCycles) {
return true;
}

Expand Down

0 comments on commit e319f6f

Please sign in to comment.