99import { CPU } from './cpu' ;
1010import { avrInterrupt } from './interrupt' ;
1111
12- const dividers = {
12+ const timer01Dividers = {
1313 0 : 0 ,
1414 1 : 1 ,
1515 2 : 8 ,
@@ -35,6 +35,17 @@ const OCIEB = 4;
3535
3636type u8 = number ;
3737
38+ interface ITimerDividers {
39+ 0 : number ;
40+ 1 : number ;
41+ 2 : number ;
42+ 3 : number ;
43+ 4 : number ;
44+ 5 : number ;
45+ 6 : number ;
46+ 7 : number ;
47+ }
48+
3849interface AVRTimerConfig {
3950 bits : 8 | 16 ;
4051 captureInterrupt : u8 ;
@@ -52,6 +63,8 @@ interface AVRTimerConfig {
5263 TCCRB : u8 ;
5364 TCCRC : u8 ;
5465 TIMSK : u8 ;
66+
67+ dividers : ITimerDividers ;
5568}
5669
5770export const timer0Config : AVRTimerConfig = {
@@ -68,7 +81,8 @@ export const timer0Config: AVRTimerConfig = {
6881 TCCRA : 0x44 ,
6982 TCCRB : 0x45 ,
7083 TCCRC : 0 , // not available
71- TIMSK : 0x6e
84+ TIMSK : 0x6e ,
85+ dividers : timer01Dividers
7286} ;
7387
7488export const timer1Config : AVRTimerConfig = {
@@ -85,7 +99,8 @@ export const timer1Config: AVRTimerConfig = {
8599 TCCRA : 0x80 ,
86100 TCCRB : 0x81 ,
87101 TCCRC : 0x82 ,
88- TIMSK : 0x6f
102+ TIMSK : 0x6f ,
103+ dividers : timer01Dividers
89104} ;
90105
91106export const timer2Config : AVRTimerConfig = {
@@ -102,7 +117,17 @@ export const timer2Config: AVRTimerConfig = {
102117 TCCRA : 0xb0 ,
103118 TCCRB : 0xb1 ,
104119 TCCRC : 0 , // not available
105- TIMSK : 0x70
120+ TIMSK : 0x70 ,
121+ dividers : {
122+ 0 : 1 ,
123+ 1 : 1 ,
124+ 2 : 8 ,
125+ 3 : 32 ,
126+ 4 : 64 ,
127+ 5 : 128 ,
128+ 6 : 256 ,
129+ 7 : 1024
130+ }
106131} ;
107132
108133export class AVRTimer {
@@ -169,7 +194,7 @@ export class AVRTimer {
169194 }
170195
171196 tick ( ) {
172- const divider = dividers [ this . CS ] ;
197+ const divider = this . config . dividers [ this . CS ] ;
173198 const delta = this . cpu . cycles - this . lastCycle ;
174199 if ( divider && delta >= divider ) {
175200 const counterDelta = Math . floor ( delta / divider ) ;
0 commit comments