summaryrefslogtreecommitdiff
path: root/src/peripherals/avrdx-ccp.ts
blob: a4f037013b303c18ac16303716f884904502910b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// CCP - Configuration Change Protection
// When 0xD8 (IOREG) or 0x9D (SPM) is written to CCP, a 4-cycle window opens
// during which protected registers can be written.

import type { CPU } from 'avr8js/cpu/cpu';

const ADDR = 0;

const SPM  = 0x9D;
const IOREG = 0xD8;

export class AVRDxCCP {
  private unlockedUntil = -Infinity;

  constructor(private cpu: CPU, base: number) {
    cpu.writeHooks[base + ADDR] = (value) => {
      if (value === IOREG || value === SPM) {
        // TODO: adjust for cycle scaling(?)
        this.unlockedUntil = cpu.cycles + 4;
      }
      cpu.data[base + ADDR] = value;
      return true;
    };
  }

  isUnlocked(): boolean {
    return this.cpu.cycles <= this.unlockedUntil;
  }
}
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.