Skip to content

Commit e319f6f

Browse files
committed
fix(eeprom): EEPROM write fails after first attempt
close #54
1 parent 7466e56 commit e319f6f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/peripherals/eeprom.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,38 @@ describe('EEPROM', () => {
160160
expect(eepromBackend.memory[15]).toEqual(0x55);
161161
expect(eepromBackend.memory[16]).toEqual(0xff);
162162
});
163+
164+
it('should write two bytes sucessfully', () => {
165+
const cpu = new CPU(new Uint16Array(0x1000));
166+
const eepromBackend = new EEPROMMemoryBackend(1024);
167+
const eeprom = new AVREEPROM(cpu, eepromBackend);
168+
169+
// Write 0x55 to address 15
170+
cpu.writeData(EEDR, 0x55);
171+
cpu.writeData(EEARL, 15);
172+
cpu.writeData(EEARH, 0);
173+
cpu.writeData(EECR, EEMPE);
174+
cpu.writeData(EECR, EEPE);
175+
eeprom.tick();
176+
expect(cpu.cycles).toEqual(2);
177+
178+
// wait long enough time for the first write to finish
179+
cpu.cycles += 10000000;
180+
eeprom.tick();
181+
182+
// Write 0x66 to address 16
183+
cpu.writeData(EEDR, 0x66);
184+
cpu.writeData(EEARL, 16);
185+
cpu.writeData(EEARH, 0);
186+
cpu.writeData(EECR, EEMPE);
187+
cpu.writeData(EECR, EEPE);
188+
eeprom.tick();
189+
190+
// Ensure both writes took place
191+
expect(cpu.cycles).toEqual(10000004);
192+
expect(eepromBackend.memory[15]).toEqual(0x55);
193+
expect(eepromBackend.memory[16]).toEqual(0x66);
194+
});
163195
});
164196

165197
describe('EEPROM erase', () => {

src/peripherals/eeprom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class AVREEPROM {
101101
return true;
102102
}
103103
// Check for write-in-progress
104-
if (this.writeCompleteCycles) {
104+
if (this.cpu.cycles < this.writeCompleteCycles) {
105105
return true;
106106
}
107107

0 commit comments

Comments
 (0)