The Pedigree Project 0.1
PicElcr.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef PIC_ELCR_H
3#define PIC_ELCR_H
4#include "pedigree/kernel/processor/types.h"
5
6constexpr uint16_t PicElcrProgrammable = 0xdef8;
7
8template <class Read, class Write>
9bool updatePicElcr(uint8_t irq, bool level, Read read, Write write, uint8_t& previous) {
10 if (irq >= 16 || !(PicElcrProgrammable & (uint16_t{1} << irq)))
11 return false;
12 const size_t bank = irq / 8;
13 const uint8_t bit = 1U << (irq % 8);
14 previous = read(bank);
15 if (previous & ~static_cast<uint8_t>(PicElcrProgrammable >> (bank * 8)))
16 return false;
17 const uint8_t desired = level ? previous | bit : previous & ~bit;
18 if (desired != previous)
19 write(bank, desired);
20 if (read(bank) == desired)
21 return true;
22 write(bank, previous);
23 (void)read(bank);
24 return false;
25}
26#endif