1+ #include "plic.h"
2+ #include "demo_system.h"
3+ #include "dev_access.h"
4+
5+ void plic_init (void ) {
6+ // Enable machine external interrupts
7+ enable_interrupts (1 << 11 ); // Set mie.meie bit
8+
9+ // Set threshold to 0 (allow all priorities)
10+ plic_set_threshold (0 );
11+ }
12+
13+ void plic_set_priority (uint32_t source , uint32_t priority ) {
14+ uint32_t addr = PLIC_BASE + PLIC_PRIORITY_BASE + (source * 4 );
15+ DEV_WRITE (addr , priority & PLIC_PRIORITY_MASK );
16+ }
17+
18+ void plic_enable_interrupt (uint32_t source ) {
19+ uint32_t addr = PLIC_BASE + PLIC_ENABLE_BASE ;
20+ uint32_t current = DEV_READ (addr );
21+ DEV_WRITE (addr , current | (1 << source ));
22+ }
23+
24+ void plic_disable_interrupt (uint32_t source ) {
25+ uint32_t addr = PLIC_BASE + PLIC_ENABLE_BASE ;
26+ uint32_t current = DEV_READ (addr );
27+ DEV_WRITE (addr , current & ~(1 << source ));
28+ }
29+
30+ void plic_set_threshold (uint32_t threshold ) {
31+ DEV_WRITE (PLIC_BASE + PLIC_THRESHOLD_BASE , threshold & PLIC_PRIORITY_MASK );
32+ }
33+
34+ uint32_t plic_claim_interrupt (void ) {
35+ return DEV_READ (PLIC_BASE + PLIC_CLAIM_BASE );
36+ }
37+
38+ void plic_complete_interrupt (uint32_t source ) {
39+ DEV_WRITE (PLIC_BASE + PLIC_CLAIM_BASE , source );
40+ }
41+
42+ void dump_plic_regs (void ) {
43+ // Read the pending register from the PLIC.
44+ uint32_t pending = DEV_READ (PLIC_BASE + PLIC_PENDING_BASE );
45+ puts ("PLIC pending: " );
46+ puthex (pending );
47+ putchar ('\n' );
48+
49+ // Read the threshold register from the PLIC.
50+ uint32_t threshold = DEV_READ (PLIC_BASE + PLIC_THRESHOLD_BASE );
51+ puts ("PLIC threshold: " );
52+ puthex (threshold );
53+ putchar ('\n' );
54+ }
55+
0 commit comments