aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/timer.spec.ts15
-rw-r--r--src/timer.ts35
2 files changed, 44 insertions, 6 deletions
diff --git a/src/timer.spec.ts b/src/timer.spec.ts
index dd45cee..4d631a7 100644
--- a/src/timer.spec.ts
+++ b/src/timer.spec.ts
@@ -1,5 +1,5 @@
import { CPU } from './cpu';
-import { AVRTimer, timer0Config } from './timer';
+import { AVRTimer, timer0Config, timer2Config } from './timer';
describe('timer', () => {
let cpu: CPU;
@@ -188,4 +188,17 @@ describe('timer', () => {
expect(cpu.pc).toEqual(0x1e);
expect(cpu.cycles).toEqual(3);
});
+
+ it('timer2 should count every 256 ticks when prescaler is 6 (issue #5)', () => {
+ const timer = new AVRTimer(cpu, timer2Config);
+ cpu.data[0xb1] = 0x6; // TCCR1B.CS <- 6
+
+ cpu.cycles = 511;
+ timer.tick();
+ expect(cpu.data[0xb2]).toEqual(1); // TCNT2 should be 2
+
+ cpu.cycles = 512;
+ timer.tick();
+ expect(cpu.data[0xb2]).toEqual(2); // TCNT2 should be 2
+ });
});
diff --git a/src/timer.ts b/src/timer.ts
index 1406eda..ac82ce8 100644
--- a/src/timer.ts
+++ b/src/timer.ts
@@ -9,7 +9,7 @@
import { CPU } from './cpu';
import { avrInterrupt } from './interrupt';
-const dividers = {
+const timer01Dividers = {
0: 0,
1: 1,
2: 8,
@@ -35,6 +35,17 @@ const OCIEB = 4;
type u8 = number;
+interface ITimerDividers {
+ 0: number;
+ 1: number;
+ 2: number;
+ 3: number;
+ 4: number;
+ 5: number;
+ 6: number;
+ 7: number;
+}
+
interface AVRTimerConfig {
bits: 8 | 16;
captureInterrupt: u8;
@@ -52,6 +63,8 @@ interface AVRTimerConfig {
TCCRB: u8;
TCCRC: u8;
TIMSK: u8;
+
+ dividers: ITimerDividers;
}
export const timer0Config: AVRTimerConfig = {
@@ -68,7 +81,8 @@ export const timer0Config: AVRTimerConfig = {
TCCRA: 0x44,
TCCRB: 0x45,
TCCRC: 0, // not available
- TIMSK: 0x6e
+ TIMSK: 0x6e,
+ dividers: timer01Dividers
};
export const timer1Config: AVRTimerConfig = {
@@ -85,7 +99,8 @@ export const timer1Config: AVRTimerConfig = {
TCCRA: 0x80,
TCCRB: 0x81,
TCCRC: 0x82,
- TIMSK: 0x6f
+ TIMSK: 0x6f,
+ dividers: timer01Dividers
};
export const timer2Config: AVRTimerConfig = {
@@ -102,7 +117,17 @@ export const timer2Config: AVRTimerConfig = {
TCCRA: 0xb0,
TCCRB: 0xb1,
TCCRC: 0, // not available
- TIMSK: 0x70
+ TIMSK: 0x70,
+ dividers: {
+ 0: 1,
+ 1: 1,
+ 2: 8,
+ 3: 32,
+ 4: 64,
+ 5: 128,
+ 6: 256,
+ 7: 1024
+ }
};
export class AVRTimer {
@@ -169,7 +194,7 @@ export class AVRTimer {
}
tick() {
- const divider = dividers[this.CS];
+ const divider = this.config.dividers[this.CS];
const delta = this.cpu.cycles - this.lastCycle;
if (divider && delta >= divider) {
const counterDelta = Math.floor(delta / divider);
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.