|
1 | | -## Timer Overview |
2 | | - |
3 | | -The Pokémon mini offers 3 general purpose timer units. Each timer is broken down into several blocks to provide it with variable clock rates, the ability to be broken down into two independent 8-bit timers, and each timer can generate two unique interrupts. |
4 | | - |
5 | | -## Timer Control |
6 | | - |
7 | | -Each timer is configured using 6 registers, TIM_SCALE\*, TIM_OSCI\*, TIM_CTL\*_L, TIM_CTL\*_H, TIM_PRE\*_L and TIM_PRE\*_H. These registers provide the ability to change the clock rate of both the low and high 8-bits of the counter, set if there is a borrow chain to the upper 8-bits (16-bit counter mode) as well as enable and reset the timer (load counter from the preset). Beginning with the TIM_OSCI\*, each timer has the ability to run from oscillator 1 (System Clock) or oscillator 2 (32768 Hz RTC Clock). The pre-scale is further decided by selecting one of 8 different pre-scale values from a table in the TIM_SCALE\* register. |
8 | | - |
9 | | -Further more, timers must be enabled individually (by setting the enable flag in TIM_SCALE\* TIM_CTL\*_L and TIM_CTL\*_H) as well as by group (Upper half of TIM_ENA_OSCI1). TIM_ENA_OSCI1 disables oscillator 1 or 2 if either respective bit is clear ($10 and $20). |
10 | | - |
11 | | -**Timer Prescale (Oscillator 1)** |
12 | | -| Prescale | Clk Div. | Hz | |
13 | | -| -------- | ---------- | -------- | |
14 | | -| 0 | CPU / 2 | 2000000 | |
15 | | -| 1 | CPU / 8 | 500000 | |
16 | | -| 2 | CPU / 32 | 125000 | |
17 | | -| 3 | CPU / 64 | 62500 | |
18 | | -| 4 | CPU / 128 | 31250 | |
19 | | -| 5 | CPU / 256 | 15625 | |
20 | | -| 6 | CPU / 1024 | 3906.25 | |
21 | | -| 7 | CPU / 4096 | 976.5625 | |
22 | | - |
23 | | -**Timer Prescale (Oscillator 2)** |
24 | | -| Prescale | Clk Div. | Hz | |
25 | | -| -------- | ----------- | ----- | |
26 | | -| 0 | 32768 / 1 | 32768 | |
27 | | -| 1 | 32768 / 2 | 16384 | |
28 | | -| 2 | 32768 / 4 | 8192 | |
29 | | -| 3 | 32768 / 8 | 4096 | |
30 | | -| 4 | 32768 / 16 | 2048 | |
31 | | -| 5 | 32768 / 32 | 1024 | |
32 | | -| 6 | 32768 / 64 | 512 | |
33 | | -| 7 | 32768 / 128 | 256 | |
34 | | - |
35 | | -The timer control registers affect the values of the timers themselves. Enable must be set for timing, this means there are a total of 3 bits that must be enabled for any timer to begin counting. writing a logical 1 to a reset bit in a control register will cause that respective 8-bit section to copy the respective value out of preset. All timers count down. When any timer underflows, it's value is copied from the preset value. |
36 | | - |
37 | | -## 16-bit mode |
38 | | - |
39 | | -When a timer is operating in 16-bit mode, all the upper-8 bit settings are unceremoniously ignored. Enables, reset and and everything no longer control the behavior of the timer. They remain writable, but they no longer actively function. This includes enables, resets and pre-scale values. The lower-8 bit configuration is effective over the full 16-bit value. Additionally, all lower-8 underflow IRQs are effectively disabled. The timer only presets when the full 16-bit value underflows. |
40 | | - |
41 | | -## IRQ Operation |
42 | | - |
43 | | -Each timer provides two irqs. These IRQs appear to be fixed function, which provides the only known difference between Timers 1-2 and Timer 3. |
44 | | - |
45 | | -Timers 1-3 have a primary IRQ, this fires anytime the upper 8-bit of the counter underflows (16- or 8-bit operations) The secondary IRQ of Timer 1-2 occurs when the lower 8-bit counter underflows (8-bit mode only) The secondary IRQ of Timer 3 occurs when the value of the counter becomes less than or equal-to the value in it's comparator. In 8-bit mode only the upper 8-bit of the value is used. |
46 | | - |
47 | | -## Sound |
48 | | - |
49 | | -Timer 3 is also used for [sound](PM_Audio.md "wikilink") within the Pokémon mini. |
| 1 | +# Timers & Oscillators |
| 2 | + |
| 3 | +- [Seconds Timer](#seconds-timer) |
| 4 | +- [Clock Timer](#clock-timer) |
| 5 | +- [General Purpose Timers](#general-purpose-timers) |
| 6 | + |
| 7 | +## Oscillators |
| 8 | + |
| 9 | +There are two oscillators on the board (external to the CPU) named OSC1 and OSC3. |
| 10 | + |
| 11 | +### OSC1 |
| 12 | + |
| 13 | +This is a low-power 32768 Hz oscillator notably used for maintaining the real-time clock, but is also used elsewhere. There is no way to disable this oscillator outside of putting the system into [sleep](Standby.md#sleep) mode. |
| 14 | + |
| 15 | +This oscillator is labeled `Y1` on the circuit board but does not have any text visible on the component itself. It's not a surface-mount component. |
| 16 | + |
| 17 | +In previous documentation and code, this oscillator was once known in the community as "oscillator 2". |
| 18 | + |
| 19 | +Things left to discover: |
| 20 | + |
| 21 | +* Part number |
| 22 | +* Accuracy |
| 23 | +* Confirm that it's crystal |
| 24 | + |
| 25 | +### OSC3 |
| 26 | + |
| 27 | +This is the high-speed 4.00 MHz ceramic oscillator used for the general purpose timers. It can be disabled by writing a 0 to the OSCC register and is also disabled when the system enters [sleep](Standby.md#sleep) mode. |
| 28 | + |
| 29 | +This oscillator is labeled `Y2` on the circuit board and has text printed on the top which looks like a curved M in a box followed by `4.00` and then a single-character serial such as `L` or `J`. It is a surface-mount component. |
| 30 | + |
| 31 | +Disabling this oscillator when not needed can save power. You can also adjust the speed this runs at by writing to register VD1C according to the following table: |
| 32 | + |
| 33 | +| VD1C1 | VD1C0 | Operating voltage | Oscillation | |
| 34 | +|:-----:|:-----:| ----------------- | ------------------ | |
| 35 | +| 1 | 1/0 | 3.2 V | 0.03~8.2 MHz | |
| 36 | +| 0 | 1 | 1.6 V | 0.03~1.1 MHz / off | |
| 37 | +| 0 | 0 | 2.4 V | 0.03~4.4 MHz | |
| 38 | + |
| 39 | +When switching voltages you cannot jump directly between 1.6 V and 3.2 V safely and you must wait some time after switching. See the VD1C register details for usage information. Note that the BIOS will switch between 1.6 V and 2.4 V during some operations. |
| 40 | + |
| 41 | +In previous documentation and code, this oscillator was once known in the community as "oscillator 1". |
| 42 | + |
| 43 | +#### OSC3 Specs |
| 44 | + |
| 45 | +These specs are unfortunately from the 2009 datasheet, when ideally we would like a 2001 datasheet. |
| 46 | + |
| 47 | +* Part number: [EFOS4004E5](https://www.digikey.com/en/products/detail/panasonic-electronic-components/EFO-S4004E5/160457) ([datasheet](https://media.digikey.com/pdf/Data%20Sheets/Panasonic%20Capacitors%20PDFs/EFO_3Array.pdf)) |
| 48 | + * EFO - Ceramic resonator |
| 49 | + * S - 2 to 13 MHz type with built-in capacitors and 3 terminals |
| 50 | + * 4004 - 4.00 MHz nominal oscillation frequency |
| 51 | + * E - Embossed taping style packaging |
| 52 | + * 5 - ±0.5% frequency tolerance |
| 53 | +* Built-in Capacitors: 33 pF |
| 54 | +* Oscillation frequency drift: ±0.2% overall stability |
| 55 | + * -20°C ≈ -0.1 |
| 56 | + * 20°C ≈ 0.0 |
| 57 | + * 40°C ≈ 0.02 |
| 58 | + * 60°C ≈ 0.0 |
| 59 | + * 80°C ≈ -0.04 |
| 60 | + |
| 61 | +## Seconds Timer |
| 62 | + |
| 63 | +A timer which increments once every second. It uses OSC1 as its clock source. |
| 64 | + |
| 65 | +This timer informs the real-time clock (RTC) in commercial games. As such, if homebrew resets or pauses the timer or sleeps the console, it will force commercial games to ask for the user to enter the time again. |
| 66 | + |
| 67 | +* Write 0 to STRUN to pause this timer or 1 to start it. |
| 68 | + * When reading, 0 means paused and 1 means running. |
| 69 | + * In pm.h, STRST is bit 0 of SEC_CTRL |
| 70 | +* Write 1 to STRST to reset this timer. |
| 71 | + * In pm.h, STRST is bit 1 of SEC_CTRL |
| 72 | +* Read the count from the STD register |
| 73 | + * In pm.h, STD is called SEC_CNT |
| 74 | + |
| 75 | +There are no interrupts related to this timer. |
| 76 | + |
| 77 | +## Clock Timer |
| 78 | + |
| 79 | +A timer which increments 256 times per second. It uses OSC1 as its clock source. |
| 80 | + |
| 81 | +* Write 0 to TMRUN to pause this timer or 1 to start it. |
| 82 | + * When reading, 0 means paused and 1 means running. |
| 83 | + * In pm.h, TMRUN is bit 0 of TMR256_CTRL |
| 84 | +* Write 1 to TMRST to reset this timer. |
| 85 | + * In pm.h, TMRST is bit 1 of TMR256_CTRL |
| 86 | +* Read the count from the TMD register |
| 87 | + * The individual bits decompose into different counts for different Hz. That is, each bit increments at a different Hz by nature of the whole byte incrementing 256 times per second. |
| 88 | + * TMD7 - overflows every 1 Hz, increments every 2 Hz |
| 89 | + * TMD6 - overflows every 2 Hz, increments every 4 Hz |
| 90 | + * TMD5 - overflows every 4 Hz, increments every 8 Hz |
| 91 | + * TMD4 - overflows every 8 Hz, increments every 16 Hz |
| 92 | + * TMD3 - overflows every 16 Hz, increments every 32 Hz |
| 93 | + * TMD2 - overflows every 32 Hz, increments every 64 Hz |
| 94 | + * TMD1 - overflows every 64 Hz, increments every 128 Hz |
| 95 | + * TMD0 - overflows every 128 Hz, increments every 256 Hz |
| 96 | + * In pm.h, TMD is called TMR256_CNT |
| 97 | + |
| 98 | +There are four interrupts tripped by this timer at different Hz. |
| 99 | + |
| 100 | +* Clock timer 1 Hz interrupt |
| 101 | + * Triggers every second, when TMD7 overflows. |
| 102 | + * Write 1 to ECTM1 to enable, 0 to disable. |
| 103 | + * When reading, 1 means enabled and 0 means disabled. |
| 104 | + * With pm.h, use `IRQ_ENA2 |= IRQ2_1HZ;` to enable, `IRQ_ENA2 &= ~IRQ2_1HZ;` to disable. |
| 105 | + * Write 1 to FCTM1 to reset the activation. |
| 106 | + * When reading, 1 means the interrupt is active (ready to trigger), 0 otherwise. |
| 107 | + * With pm.h, use `IRQ_ACT2 |= IRQ2_1HZ;` to reset, use `IRQ_ACT2 & IRQ2_1HZ` as a boolean to read. |
| 108 | +* Clock timer 2 Hz interrupt |
| 109 | + * Triggers every 500 ms, when TMD6 overflows. |
| 110 | + * Registers are: ECTM2, FCTM2, and IRQ2_2HZ in pm.h |
| 111 | +* Clock timer 8 Hz interrupt |
| 112 | + * Triggers every 125 ms, when TMD4 overflows. |
| 113 | + * Registers are: ECTM8, FCTM8, and IRQ2_8HZ in pm.h |
| 114 | +* Clock timer 32 Hz interrupt |
| 115 | + * Triggers every 31250 µs, when TMD2 overflows. |
| 116 | + * Registers are: ECTM32, FCTM32, and IRQ2_32HZ in pm.h |
| 117 | +* Write to PCTM to set the priority for all clock timer interrupts. |
| 118 | + * You can read out the current priority as well. |
| 119 | + * With pm.h, use `IRQ_PRI2 &= ~PRI2_TIM256(3);` to clear it then `IRQ_PRI2 |= PRI2_TIM256(x);` to set the new priority, where x can be 0-3 (0 = disabled). |
| 120 | + * When setting priorities in initial setup, you don't need to clear anything and can instead use `IRQ_PRI2 = PRI2_TIM256(x) | ...;` where `...` is anything else that needs to be set in IRQ_PRI2. |
| 121 | + |
| 122 | +In previous documentation and code, this timer was once known in the community as the "256Hz Timer". |
| 123 | + |
| 124 | +## General Purpose Timers |
| 125 | + |
| 126 | +TODO |
| 127 | + |
| 128 | +## Timer Overview |
| 129 | + |
| 130 | +The Pokémon mini offers 3 general purpose timer units. Each timer is broken down into several blocks to provide it with variable clock rates, the ability to be broken down into two independent 8-bit timers, and each timer can generate two unique interrupts. |
| 131 | + |
| 132 | +## Timer Control |
| 133 | + |
| 134 | +Each timer is configured using 6 registers, TIM_SCALE\*, TIM_OSCI\*, TIM_CTL\*_L, TIM_CTL\*_H, TIM_PRE\*_L and TIM_PRE\*_H. These registers provide the ability to change the clock rate of both the low and high 8-bits of the counter, set if there is a borrow chain to the upper 8-bits (16-bit counter mode) as well as enable and reset the timer (load counter from the preset). Beginning with the TIM_OSCI\*, each timer has the ability to run from oscillator 1 (System Clock) or oscillator 2 (32768 Hz RTC Clock). The pre-scale is further decided by selecting one of 8 different pre-scale values from a table in the TIM_SCALE\* register. |
| 135 | + |
| 136 | +Further more, timers must be enabled individually (by setting the enable flag in TIM_SCALE\* TIM_CTL\*_L and TIM_CTL\*_H) as well as by group (Upper half of TIM_ENA_OSCI1). TIM_ENA_OSCI1 disables oscillator 1 or 2 if either respective bit is clear ($10 and $20). |
| 137 | + |
| 138 | +**Timer Prescale (Oscillator 1)** |
| 139 | +| Prescale | Clk Div. | Hz | |
| 140 | +| -------- | ---------- | -------- | |
| 141 | +| 0 | CPU / 2 | 2000000 | |
| 142 | +| 1 | CPU / 8 | 500000 | |
| 143 | +| 2 | CPU / 32 | 125000 | |
| 144 | +| 3 | CPU / 64 | 62500 | |
| 145 | +| 4 | CPU / 128 | 31250 | |
| 146 | +| 5 | CPU / 256 | 15625 | |
| 147 | +| 6 | CPU / 1024 | 3906.25 | |
| 148 | +| 7 | CPU / 4096 | 976.5625 | |
| 149 | + |
| 150 | +**Timer Prescale (Oscillator 2)** |
| 151 | +| Prescale | Clk Div. | Hz | |
| 152 | +| -------- | ----------- | ----- | |
| 153 | +| 0 | 32768 / 1 | 32768 | |
| 154 | +| 1 | 32768 / 2 | 16384 | |
| 155 | +| 2 | 32768 / 4 | 8192 | |
| 156 | +| 3 | 32768 / 8 | 4096 | |
| 157 | +| 4 | 32768 / 16 | 2048 | |
| 158 | +| 5 | 32768 / 32 | 1024 | |
| 159 | +| 6 | 32768 / 64 | 512 | |
| 160 | +| 7 | 32768 / 128 | 256 | |
| 161 | + |
| 162 | +The timer control registers affect the values of the timers themselves. Enable must be set for timing, this means there are a total of 3 bits that must be enabled for any timer to begin counting. writing a logical 1 to a reset bit in a control register will cause that respective 8-bit section to copy the respective value out of preset. All timers count down. When any timer underflows, it's value is copied from the preset value. |
| 163 | + |
| 164 | +## 16-bit mode |
| 165 | + |
| 166 | +When a timer is operating in 16-bit mode, all the upper-8 bit settings are unceremoniously ignored. Enables, reset and and everything no longer control the behavior of the timer. They remain writable, but they no longer actively function. This includes enables, resets and pre-scale values. The lower-8 bit configuration is effective over the full 16-bit value. Additionally, all lower-8 underflow IRQs are effectively disabled. The timer only presets when the full 16-bit value underflows. |
| 167 | + |
| 168 | +## IRQ Operation |
| 169 | + |
| 170 | +Each timer provides two irqs. These IRQs appear to be fixed function, which provides the only known difference between Timers 1-2 and Timer 3. |
| 171 | + |
| 172 | +Timers 1-3 have a primary IRQ, this fires anytime the upper 8-bit of the counter underflows (16- or 8-bit operations) The secondary IRQ of Timer 1-2 occurs when the lower 8-bit counter underflows (8-bit mode only) The secondary IRQ of Timer 3 occurs when the value of the counter becomes less than or equal-to the value in it's comparator. In 8-bit mode only the upper 8-bit of the value is used. |
| 173 | + |
| 174 | +## Sound |
| 175 | + |
| 176 | +Timer 3 is also used for [sound](PM_Audio.md "wikilink") within the Pokémon mini. |
0 commit comments