Skip to content

Commit c90cd20

Browse files
committed
Implement Sstc extension
1 parent 98f2d2b commit c90cd20

File tree

8 files changed

+172
-48
lines changed

8 files changed

+172
-48
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Additionally, the machine trap vector CSR `mtvec` is initialized to `0xfffffff0`
9999

100100
## ISA
101101

102-
`RV32IMAZicsr_Zifencei_Zihintpause_Svade`
102+
`RV32IMAZicntr_Zicsr_Zifencei_Zihintpause_Sstc_Svade`
103103

104104
Supported privilege levels: M, S, U
105105

@@ -116,6 +116,7 @@ Supported address translation schemes: Bare, Sv32
116116
| Zicsr | 2.0 |
117117
| Zifencei\* | 2.0 |
118118
| Zihintpause | 2.0 |
119+
| Sstc | 1.0.0 |
119120
| Svade | 1.0 |
120121
| Xmlogsys | N/A |
121122

asm/stimer.s

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.option norvc
2+
.section .data
3+
.section .text.init
4+
.global _start
5+
6+
_start:
7+
li a0, 0
8+
9+
# enable counters for S-mode
10+
li t0, -1
11+
csrw mcounteren, t0
12+
13+
# set mstatus.MPP to S
14+
li t0, 0b1000000000000
15+
csrc mstatus, t0
16+
li t0, 0b0100000000000
17+
csrs mstatus, t0
18+
19+
# switch to supervisor mode
20+
la t0, supervisor
21+
csrw mepc, t0
22+
mret
23+
24+
supervisor:
25+
# set timer for 1 second in the future
26+
rdtime t0
27+
addi t0, t0, 1000
28+
csrw stimecmp, t0
29+
csrwi stimecmph, 0
30+
31+
# set up trap vector
32+
la t0, strap
33+
csrw stvec, t0
34+
35+
# set sstatus.SPP to U
36+
li t0, 0b100000000
37+
csrc sstatus, t0
38+
39+
# enable sie.STIE
40+
li t0, 0b100000
41+
csrs sie, t0
42+
43+
# switch to user mode
44+
la t1, user
45+
csrw sepc, t1
46+
sret
47+
48+
user:
49+
j user
50+
51+
strap:
52+
# increment counter
53+
addi a0, a0, 1
54+
55+
# set timer for 1 second in the future
56+
rdtime t0
57+
addi t0, t0, 1000
58+
csrw stimecmp, t0
59+
csrwi stimecmph, 0
60+
61+
sret

python/src/mlogv32/preprocessor/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"sie": 0x104,
1818
"stvec": 0x105,
1919
"scounteren": 0x106,
20+
"stimecmp": 0x14D,
21+
"stimecmph": 0x15D,
2022
"senvcfg": 0x10A,
2123
"scountinhibit": 0x120,
2224
"sscratch": 0x140,

riscof/sail_cSim/env/sail_config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"supported": false
8989
},
9090
"Zicntr": {
91-
"supported": false
91+
"supported": true
9292
},
9393
"Zicsr": {
9494
"supported": true
@@ -239,7 +239,7 @@
239239
"supported": false
240240
},
241241
"Sstc": {
242-
"supported": false
242+
"supported": true
243243
},
244244
"Svinval": {
245245
"supported": false

src/cpu/controller.mlog.jinja

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ end_slow_init:
199199
set csr_mtimeh 0
200200
set csr_mtimecmp 0
201201
set csr_mtimecmph 0
202+
set csr_stimecmp 0
203+
set csr_stimecmph 0
202204
set csr_minstret 0
203205
set csr_minstreth 0
204206
write 0 {{CSRS}} "{{ 'mcycle'|csr }}"
@@ -420,6 +422,8 @@ set _ csr_mtime
420422
set _ csr_mtimeh
421423
set _ csr_mtimecmp
422424
set _ csr_mtimecmph
425+
set _ csr_stimecmp
426+
set _ csr_stimecmph
423427
set _ csr_mcycle
424428
set _ csr_mcycleh
425429
set _ csr_minstret

src/cpu/cpu.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ csrs:
159159
scounteren:
160160
read: csrs
161161
write: csrs
162+
stimecmp:
163+
read: LABEL
164+
write: LABEL
165+
stimecmph:
166+
read: LABEL
167+
write: LABEL
162168

163169
# configuration
164170
senvcfg:

src/cpu/worker.mlog.jinja

Lines changed: 87 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
read csr_mtimecmph prev_proc "csr_mtimecmph"
1515
read csr_mtimeh prev_proc "csr_mtimeh"
1616
read csr_satp prev_proc "csr_satp"
17+
read csr_stimecmp prev_proc "csr_stimecmp"
18+
read csr_stimecmph prev_proc "csr_stimecmph"
1719
read effective_privilege_mode prev_proc "effective_privilege_mode"
1820
read icache_payload prev_proc "icache_payload"
1921
read icache_ram prev_proc "icache_ram"
@@ -93,7 +95,7 @@
9395

9496
#% set labels.next_tick = 14
9597

96-
#% set labels.end_instruction_with_rd_and_poll_interrupts = 148
98+
#% set labels.end_instruction_with_rd_and_poll_interrupts = 157
9799
#% set labels.end_instruction_with_rd_and_fire_interrupts = _labels_vals|last + 1
98100
#% set labels.end_instruction_with_rd = _labels_vals|last + 1
99101
#% set labels.end_instruction = _labels_vals|last + 2
@@ -163,10 +165,21 @@ poll_interrupts:
163165
op or $mip.mtip $mip.mtip $high_gt
164166
op shl $mip.mtip $mip.mtip 7
165167

168+
# supervisor timer interrupt
169+
op greaterThan $high_gt csr_mtimeh csr_stimecmph
170+
op equal $high_eq csr_mtimeh csr_stimecmph
171+
op greaterThanEq $low_ge csr_mtime csr_stimecmp
172+
173+
op and $mip.stip $high_eq $low_ge
174+
op or $mip.stip $mip.stip $high_gt
175+
op shl $mip.stip $mip.stip 5
176+
166177
# poll -
167178
# mtip -
168-
op and csr_mip csr_mip 0b01111111111111111111111101111111
179+
# stip -
180+
op and csr_mip csr_mip 0b01111111111111111111111101011111
169181
op or csr_mip csr_mip $mip.mtip
182+
op or csr_mip csr_mip $mip.stip
170183

171184
# check if interrupts should fire
172185
# this runs at the start of the first worker each tick, if certain CSRs are modified, and after an xRET instruction
@@ -2283,6 +2296,7 @@ modify_csr:
22832296
jump ILLEGAL_OP equal $required_privilege 0b10
22842297

22852298
# look up the CSR labels
2299+
# NOTE: this value in _address is later used by check_xtimecmp
22862300
# csr_labels = csr_read * 1000 + csr_write
22872301
set _address imm
22882302
op add ret2 @counter 1
@@ -2370,7 +2384,7 @@ csr_read_sstatus:
23702384
csr_write_sstatus:
23712385
# insert new writable fields
23722386
op and csr_mstatus csr_mstatus 0b11111111111100111111111011011101
2373-
op and result result 0b00000000000011000000000100100010
2387+
op and result result 0b00000000000011000000000100100010
23742388
op or csr_mstatus csr_mstatus result
23752389

23762390
jump csr_xstatus always
@@ -2382,9 +2396,9 @@ csr_read_mstatus:
23822396

23832397
csr_write_mstatus:
23842398
# clear WPRI and read-only zero fields
2385-
# SD/XS/FS/VS - ---- --
2386-
# WPRI -------- - - -
2387-
# UBE -
2399+
# SD/XS/FS/VS - ---- --
2400+
# WPRI -------- - - -
2401+
# UBE -
23882402
op and csr_mstatus result 0b00000000011111100001100110101010
23892403

23902404
# if MPP was set to 10 (reserved), set it to 0
@@ -2409,9 +2423,9 @@ csr_read_sip:
24092423

24102424
csr_write_sip:
24112425
# insert new writable fields
2412-
# SSIP -
2413-
op and csr_mip csr_mip 0b11111111111111111111111111111101
2414-
op and result result 0b00000000000000000000000000000010
2426+
# SSIP -
2427+
op and csr_mip csr_mip 0b11111111111111111111111111111101
2428+
op and result result 0b00000000000000000000000000000010
24152429
op or csr_mip csr_mip result
24162430

24172431
jump end_instruction_with_rd_and_fire_interrupts always
@@ -2424,11 +2438,10 @@ csr_read_mip:
24242438

24252439
csr_write_mip:
24262440
# insert new writable fields
2427-
# SEIP -
2428-
# STIP -
2429-
# SSIP -
2430-
op and csr_mip csr_mip 0b11111111111111111111110111011101
2431-
op and result result 0b00000000000000000000001000100010
2441+
# SEIP -
2442+
# SSIP -
2443+
op and csr_mip csr_mip 0b11111111111111111111110111011101
2444+
op and result result 0b00000000000000000000001000000010
24322445
op or csr_mip csr_mip result
24332446

24342447
jump end_instruction_with_rd_and_fire_interrupts always
@@ -2445,11 +2458,11 @@ csr_read_sie:
24452458

24462459
csr_write_sie:
24472460
# insert new writable fields
2448-
# SEIE -
2449-
# STIE -
2450-
# SSIE -
2451-
op and csr_mie csr_mie 0b11111111111111111111110111011101
2452-
op and result result 0b00000000000000000000001000100010
2461+
# SEIE -
2462+
# STIE -
2463+
# SSIE -
2464+
op and csr_mie csr_mie 0b11111111111111111111110111011101
2465+
op and result result 0b00000000000000000000001000100010
24532466
op or csr_mie csr_mie result
24542467

24552468
jump end_instruction_with_rd_and_fire_interrupts always
@@ -2461,11 +2474,11 @@ csr_read_mie:
24612474

24622475
csr_write_mie:
24632476
# get new writable fields
2464-
# MEIE -
2465-
# SEIE -
2466-
# MTIE -
2467-
# STIE -
2468-
# SSIE -
2477+
# MEIE -
2478+
# SEIE -
2479+
# MTIE -
2480+
# STIE -
2481+
# SSIE -
24692482
op and csr_mie result 0b00000000000000000000101010100010
24702483
jump end_instruction_with_rd_and_fire_interrupts always
24712484

@@ -2489,6 +2502,30 @@ csr_write_satp:
24892502
# TODO: this may not be strictly required by the spec
24902503
jump end_instruction_with_rd_and_invalidate_icache always
24912504

2505+
csr_read_stimecmp:
2506+
#% do declare_locals(modify_csr_locals)
2507+
set rd csr_stimecmp
2508+
jump check_stimecmp always
2509+
2510+
csr_write_stimecmp:
2511+
set csr_stimecmp result
2512+
jump end_instruction_with_rd_and_poll_interrupts always
2513+
2514+
csr_read_stimecmph:
2515+
#% do declare_locals(modify_csr_locals)
2516+
set rd csr_stimecmph
2517+
# continue into check_stimecmp
2518+
2519+
check_stimecmp:
2520+
# stimecmp is controlled by the TM bit in mcounteren
2521+
# so we set _address to 1 and use check_xcounteren to check the TM bit
2522+
set _address 1
2523+
jump check_xcounteren always
2524+
2525+
csr_write_stimecmph:
2526+
set csr_stimecmph result
2527+
jump end_instruction_with_rd_and_poll_interrupts always
2528+
24922529
csr_read_cycle:
24932530
#% do declare_locals(modify_csr_locals)
24942531
read rd {{CSRS}} "{{ 'mcycle'|csr }}"
@@ -2549,7 +2586,8 @@ check_xcounteren:
25492586
check_xcounteren__s_mode:
25502587

25512588
# trap if the bit in xcounteren corresponding to the CSR being read is clear
2552-
op and $bit imm 31
2589+
# we use _address instead of imm here to allow check_stimecmp to change it without messing up debug info
2590+
op and $bit _address 31
25532591
op shr $bit $xcounteren $bit
25542592
op and $bit $bit 0b1
25552593
jump ILLEGAL_OP equal $bit 0
@@ -2706,33 +2744,37 @@ jump decode_BRANCH always
27062744
jump decode_JALR always
27072745
jump decode_JAL always
27082746
jump decode_SYSTEM always
2747+
jump csr_read_csrs always
27092748
jump csr_read_cycle always
2710-
jump csr_read_time always
2711-
jump csr_read_minstret always
2712-
jump csr_read_hpmcounter always
2713-
jump csr_read_zero always
27142749
jump csr_read_cycleh always
2715-
jump csr_read_timeh always
2716-
jump csr_read_minstreth always
2717-
jump csr_read_sstatus always
2718-
jump csr_read_sie always
2719-
jump csr_read_csrs always
2720-
jump csr_read_sip always
2721-
jump csr_read_satp always
2722-
jump csr_read_mstatus always
2750+
jump csr_read_hpmcounter always
27232751
jump csr_read_mideleg always
27242752
jump csr_read_mie always
2753+
jump csr_read_minstret always
2754+
jump csr_read_minstreth always
27252755
jump csr_read_mip always
2726-
jump csr_write_sstatus always
2727-
jump csr_write_sie always
2728-
jump csr_write_csrs_align_4B always
2756+
jump csr_read_mstatus always
2757+
jump csr_read_satp always
2758+
jump csr_read_sie always
2759+
jump csr_read_sip always
2760+
jump csr_read_sstatus always
2761+
jump csr_read_stimecmp always
2762+
jump csr_read_stimecmph always
2763+
jump csr_read_time always
2764+
jump csr_read_timeh always
2765+
jump csr_read_zero always
27292766
jump csr_write_csrs always
2730-
jump csr_write_readonly always
2731-
jump csr_write_sip always
2732-
jump csr_write_satp always
2733-
jump csr_write_mstatus always
2767+
jump csr_write_csrs_align_4B always
27342768
jump csr_write_mie always
2735-
jump csr_write_mip always
27362769
jump csr_write_minstret always
27372770
jump csr_write_minstreth always
2771+
jump csr_write_mip always
2772+
jump csr_write_mstatus always
2773+
jump csr_write_readonly always
2774+
jump csr_write_satp always
2775+
jump csr_write_sie always
2776+
jump csr_write_sip always
2777+
jump csr_write_sstatus always
2778+
jump csr_write_stimecmp always
2779+
jump csr_write_stimecmph always
27382780
# {% endraw %}

src/peripherals/debugger.mlog.jinja

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ loop:
159159
op shl mtimecmph mtimecmph 32
160160
op add mtimecmp mtimecmp mtimecmph
161161

162+
read stimecmp {{CPU}} "csr_stimecmp"
163+
read stimecmph {{CPU}} "csr_stimecmph"
164+
op shl stimecmph stimecmph 32
165+
op add stimecmp stimecmp stimecmph
166+
162167
read minstret {{CPU}} "csr_minstret"
163168
read minstreth {{CPU}} "csr_minstreth"
164169
op shl minstreth minstreth 32
@@ -592,6 +597,9 @@ loop__no_mark_icache:
592597
print "mtimecmp = {0}\n"
593598
format mtimecmp
594599

600+
print "stimecmp = {0}\n"
601+
format stimecmp
602+
595603
print "mtime = {0}\n"
596604
format mtime
597605

0 commit comments

Comments
 (0)