aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUri Shaked2022-02-21 17:49:10 +0200
committerUri Shaked2022-02-21 17:49:10 +0200
commit0f2a3b0abf058d04f5368d7c1ce55131d5e319a0 (patch)
tree67727dbe707e3d49a1f9071bce2487774084596f
parentMerge pull request #116 from Dudeplayz/master (diff)
downloadavr8js-0f2a3b0abf058d04f5368d7c1ce55131d5e319a0.tar.gz
avr8js-0f2a3b0abf058d04f5368d7c1ce55131d5e319a0.tar.bz2
avr8js-0f2a3b0abf058d04f5368d7c1ce55131d5e319a0.zip
fix(timer): OCRH masking #117
-rw-r--r--src/peripherals/timer.spec.ts11
-rw-r--r--src/peripherals/timer.ts21
2 files changed, 29 insertions, 3 deletions
diff --git a/src/peripherals/timer.spec.ts b/src/peripherals/timer.spec.ts
index 27ef7fb..9330168 100644
--- a/src/peripherals/timer.spec.ts
+++ b/src/peripherals/timer.spec.ts
@@ -1245,6 +1245,17 @@ describe('timer', () => {
expect(cpu.readData(R19)).toEqual(0x5);
expect(cpu.readData(R20)).toEqual(0x3);
});
+
+ it('should mask the unused bits of OCR1A when using fixed top values', () => {
+ const cpu = new CPU(new Uint16Array(0x1000));
+ new AVRTimer(cpu, timer1Config);
+ cpu.writeData(TCCR1A, WGM10 | WGM11); // WGM: FastPWM, top 0x3ff
+ cpu.writeData(TCCR1B, WGM12);
+ cpu.writeData(OCR1AH, 0xff);
+ cpu.writeData(OCR1A, 0xff);
+ expect(cpu.readData(OCR1A)).toEqual(0xff);
+ expect(cpu.readData(OCR1AH)).toEqual(0x03);
+ });
});
describe('External clock', () => {
diff --git a/src/peripherals/timer.ts b/src/peripherals/timer.ts
index 53777e2..6167394 100644
--- a/src/peripherals/timer.ts
+++ b/src/peripherals/timer.ts
@@ -388,11 +388,16 @@ export class AVRTimer {
const updateTempRegister = (value: u8) => {
this.highByteTemp = value;
};
+ const updateOCRHighRegister = (value: u8, old: u8, addr: u16) => {
+ this.highByteTemp = value & (this.ocrMask >> 8);
+ cpu.data[addr] = this.highByteTemp;
+ return true;
+ };
this.cpu.writeHooks[config.TCNT + 1] = updateTempRegister;
- this.cpu.writeHooks[config.OCRA + 1] = updateTempRegister;
- this.cpu.writeHooks[config.OCRB + 1] = updateTempRegister;
+ this.cpu.writeHooks[config.OCRA + 1] = updateOCRHighRegister;
+ this.cpu.writeHooks[config.OCRB + 1] = updateOCRHighRegister;
if (this.hasOCRC) {
- this.cpu.writeHooks[config.OCRC + 1] = updateTempRegister;
+ this.cpu.writeHooks[config.OCRC + 1] = updateOCRHighRegister;
}
this.cpu.writeHooks[config.ICR + 1] = updateTempRegister;
}
@@ -481,6 +486,16 @@ export class AVRTimer {
}
}
+ get ocrMask() {
+ switch (this.topValue) {
+ case TopOCRA:
+ case TopICR:
+ return 0xffff;
+ default:
+ return this.topValue;
+ }
+ }
+
private updateWGMConfig() {
const { config, WGM } = this;
const wgmModes = config.bits === 16 ? wgmModes16Bit : wgmModes8Bit;
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.