Skip to content

Commit

Permalink
Add mnemonic for PLIC and CLINT
Browse files Browse the repository at this point in the history
  • Loading branch information
chiangkd committed May 27, 2024
1 parent c6411d3 commit 4518267
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
28 changes: 16 additions & 12 deletions aclint.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,64 @@ static bool aclint_reg_read(aclint_state_t *aclint,
uint32_t addr,
uint32_t *value)
{
#define _(reg) ACLINT_##reg
switch (addr) {
case 0x0:
case _(SSWI):
/* sswi */
*value = aclint->setssip;
return true;
case 0x1000:
case _(MTIMECMP_LO):
/* mtimecmp */
*value = aclint->mtimecmp;
return true;
case 0x1001:
case _(MTIMECMP_HI):
/* mtimecmph */
*value = aclint->mtimecmph;
return true;
case 0x1FFE:
case _(MTIME_LO):
/* mtime */
*value = aclint->vm->mtime;
return true;
case 0x1FFF:
case _(MTIME_HI):
/* mtimeh */
*value = aclint->vm->mtimeh;
return true;
default:
return false;
}
#undef _
}

static bool aclint_reg_write(aclint_state_t *aclint,
uint32_t addr,
uint32_t value)
{
#define _(reg) ACLINT_##reg
switch (addr) {
case 0x0:
case _(SSWI):
/* sswi */
aclint->setssip = value;
return true;
case 0x1000:
case _(MTIMECMP_LO):
/* mtimecmp */
aclint->mtimecmp = value;
aclint->mtimecmp_lo = value;
return true;
case 0x1001:
case _(MTIMECMP_HI):
/* mtimecmph */
aclint->mtimecmph = value;
aclint->mtimecmp_hi = value;
return true;
case 0x1FFE:
case _(MTIME_LO):
/* mtime */
aclint->vm->mtime = value;
return true;
case 0x1FFF:
case _(MTIME_HI):
/* mtimeh */
aclint->vm->mtimeh = value;
return true;
default:
return false;
}
#undef _
}

void aclint_read(vm_t *vm,
Expand Down
24 changes: 24 additions & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ void ram_write(vm_t *core,
const uint32_t value);

/* ACLINT */
#define ACLINT_REG_LIST \
_(SSWI, 0x0000) \
_(MTIMECMP_LO, 0x4000) \
_(MTIMECMP_HI, 0x4004) \
_(MTIME_LO, 0x7FF8) \
_(MTIME_HI, 0x7FFC)

enum {
#define _(reg, addr) ACLINT_##reg = addr >> 2,
ACLINT_REG_LIST
#undef _
};

typedef struct {
vm_t *vm;
Expand All @@ -45,6 +57,18 @@ void aclint_write(vm_t *core,
void aclint_send_ipi(vm_t *vm, aclint_state_t *aclint, uint32_t target_hart);

/* PLIC */
#define PLIC_REG_LIST \
_(InterruptPending, 0x1000) \
_(InterruptEnable, 0x2000) \
_(PriorityThresholds, 0x200000) \
_(InterruptClaim, 0x200004) \
_(InterruptCompletion, 0x200004)

enum {
#define _(reg, addr) PLIC_##reg = addr >> 2,
PLIC_REG_LIST
#undef _
};

typedef struct {
uint32_t masked;
Expand Down
20 changes: 11 additions & 9 deletions plic.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ static bool plic_reg_read(plic_state_t *plic, uint32_t addr, uint32_t *value)
/* no priority support: source priority hardwired to 1 */
if (1 <= addr && addr <= 31)
return true;

#define _(reg) PLIC_##reg
switch (addr) {
case 0x400:
case _(InterruptPending):
*value = plic->ip;
return true;
case 0x800:
case _(InterruptEnable):
*value = plic->ie;
return true;
case 0x80000:
case _(PriorityThresholds):
*value = 0;
/* no priority support: target priority threshold hardwired to 0 */
return true;
case 0x80001:
case _(InterruptClaim):
/* claim */
*value = 0;
uint32_t candidates = plic->ip & plic->ie;
Expand All @@ -45,30 +45,32 @@ static bool plic_reg_read(plic_state_t *plic, uint32_t addr, uint32_t *value)
default:
return false;
}
#undef _
}

static bool plic_reg_write(plic_state_t *plic, uint32_t addr, uint32_t value)
{
/* no priority support: source priority hardwired to 1 */
if (1 <= addr && addr <= 31)
return true;

#define _(reg) PLIC_##reg
switch (addr) {
case 0x800:
case _(InterruptEnable):
value &= ~1;
plic->ie = value;
return true;
case 0x80000:
case _(PriorityThresholds):
/* no priority support: target priority threshold hardwired to 0 */
return true;
case 0x80001:
case _(InterruptCompletion):
/* completion */
if (plic->ie & (1 << value))
plic->masked &= ~(1 << value);
return true;
default:
return false;
}
#undef _
}

void plic_read(vm_t *vm,
Expand Down

0 comments on commit 4518267

Please sign in to comment.