ID photo of Ciro Santilli taken in 2013 right eyeCiro Santilli OurBigBook logoOurBigBook.com  Sponsor 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
TODO: the interrupt is firing only once:
Adapted from: danielmangum.com/posts/risc-v-bytes-timer-interrupts/
Tested on Ubuntu 23.10:
sudo apt install binutils-riscv64-unknown-elf qemu-system-misc gdb-multiarch
cd riscv
make
Then on shell 1:
qemu-system-riscv64 -machine virt -cpu rv64 -smp 1 -s -S -nographic -bios none -kernel timer.elf
and on shell 2:
gdb-multiarch timer.elf -nh -ex "target remote :1234" -ex 'display /i $pc' -ex 'break *mtrap' -ex 'display *0x2004000' -ex 'display *0x200BFF8'
GDB should break infinitel many times on mtrap as interrupts happen.
riscv/timer.S
/* Adapted from: https://danielmangum.com/posts/risc-v-bytes-timer-interrupts/ */

.option norvc
.section .text
.global _start
_start:
    /* MSTATUS.PRIV = 0 */
    li t0, (0b11 << 7)
    csrs mstatus, t0

    /* MTVEC = mtrap
      Where to jump after each timer interrupt. */
    la t0, mtrap
    csrw mtvec, t0

    /* setup timer */
    /* mtime */
    li t1, 0x200BFF8
    lw t0, 0(t1)
    li t2, 50000
    add t0, t0, t2
    /* mtimecmp */
    li t1, 0x2004000
    sw t0, 0(t1)

    /* MSTATUS.MIE = 1 */
    li t0, (1 << 3)
    csrs mstatus, t0

    /* MIE.MTIE = 1 */
    li t0, (1 << 7)
    csrs mie, t0
spin:
    j spin

mtrap:
    /* setup timer */
    /* mtime */
    li t1, 0x200BFF8
    ld t0, 0(t1)
    li t2, 50000
    add t0, t0, t2
    /* mtimecmp */
    li t1, 0x2004000
    sd t0, 0(t1)

    j spin

Ancestors (12)

  1. RISC-V timer
  2. RISC-V
  3. List of instruction set architectures
  4. Instruction set architecture
  5. Processor (computing)
  6. Computer hardware component type
  7. Computer hardware
  8. Computer
  9. Information technology
  10. Area of technology
  11. Technology
  12. Home