Skip to content

Commit 07e7244

Browse files
committed
sw-sysemu: add rvtimer past-compare test
1 parent 56a447e commit 07e7244

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*-------------------------------------------------------------------------
2+
* Copyright (c) 2025 Ainekko, Co.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*-------------------------------------------------------------------------*/
5+
6+
/*
7+
* Test: MTIMECMP written "in the past" immediately asserts MTIP.
8+
* Expected: PASS (mip.MTIP set immediately after write)
9+
*/
10+
11+
#include "test.h"
12+
#include <stdint.h>
13+
14+
#define ESR_MTIME 0x80F40200ull
15+
#define ESR_MTIMECMP 0x80F40208ull
16+
#define ESR_TIME_CONFIG 0x80F40210ull
17+
#define ESR_MTIME_LOCAL_TARGET 0x80F40218ull
18+
19+
#define MIP_MTIP (1UL << 7)
20+
21+
static inline uint64_t read_mip(void)
22+
{
23+
uint64_t val;
24+
asm volatile("csrr %0, mip" : "=r"(val));
25+
return val;
26+
}
27+
28+
int main(void)
29+
{
30+
volatile uint64_t *mtime = (volatile uint64_t *)ESR_MTIME;
31+
volatile uint64_t *mtimecmp = (volatile uint64_t *)ESR_MTIMECMP;
32+
volatile uint64_t *time_cfg = (volatile uint64_t *)ESR_TIME_CONFIG;
33+
volatile uint64_t *mtime_target = (volatile uint64_t *)ESR_MTIME_LOCAL_TARGET;
34+
35+
/* Enable timer interrupt delivery for minion 0. */
36+
*mtime_target = 0x1;
37+
38+
/* Slow down the timer to avoid a tick during the immediate check. */
39+
*time_cfg = 0x7f;
40+
41+
/* Start from a known state with no pending timer interrupt. */
42+
*mtime = 1000;
43+
*mtimecmp = 2000;
44+
45+
if (read_mip() & MIP_MTIP) {
46+
TEST_FAIL;
47+
}
48+
49+
/* Write MTIMECMP "in the past": should assert MTIP immediately. */
50+
*mtimecmp = 500;
51+
52+
if (read_mip() & MIP_MTIP) {
53+
TEST_PASS;
54+
}
55+
56+
TEST_FAIL;
57+
return 0;
58+
}

0 commit comments

Comments
 (0)