Skip to content

Commit 2f53c77

Browse files
committed
Make mtime frequency configurable
1 parent e807954 commit 2f53c77

File tree

8 files changed

+40
-15
lines changed

8 files changed

+40
-15
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Addresses `0xf0000000` - `0xffffffff` are reserved for system purposes such as M
4343

4444
### Machine Timers
4545

46-
The `mtime` and `mtimecmp` registers are mapped to `0xf0000000` and `0xf0000008` respectively, and have a period of 1 μs. The timer registers are not accessible to privilege modes lower than M-mode.
46+
The `mtime` and `mtimecmp` registers are mapped to `0xf0000000` and `0xf0000008` respectively. The frequency is configurable, defaulting to a period of 1 μs. The timer registers are not accessible to privilege modes lower than M-mode.
4747

4848
### UART
4949

@@ -149,10 +149,6 @@ CSR values are stored either in a RAM processor (CSRS), in a variable in the CPU
149149

150150
The `[m]cycle[h]` counter is incremented at the start of each worker's tick, just before it jumps back into code execution. The period should be something like `number of workers * 1/60 seconds`, but it will vary based on FPS.
151151

152-
### `time`
153-
154-
The `[m]time[h]` counter is based on the `@time` value in Mindustry. It has a period of 1 μs, and is incremented once per tick by the controller based on the time delta since the previous tick.
155-
156152
## riscv-arch-test
157153

158154
mlogv32 currently passes all compliance tests for the `RV32IMASUZicsr_Zifencei` ISA.
0 Bytes
Binary file not shown.

linux/buildroot/board/mlogv32/dts/mlogv32.dts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
cpus {
4242
#address-cells = <1>;
4343
#size-cells = <0>;
44-
timebase-frequency = <1000000>;
44+
timebase-frequency = <1000>;
4545

4646
cpu@0 {
4747
device_type = "cpu";

linux/buildroot/opensbi/platform/mlogv32/platform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <sbi_utils/serial/xlnx_uartlite.h>
2020
#include <sbi_utils/timer/aclint_mtimer.h>
2121

22-
#define MLOGV32_MTIME_FREQ 1000000
22+
#define MLOGV32_MTIME_FREQ 1000
2323
#define MLOGV32_MTIME_ADDR 0xf0000000
2424
#define MLOGV32_MTIME_SIZE 0x8
2525
#define MLOGV32_MTIMECMP_ADDR 0xf0000008

src/config/base.mlog.jinja

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
set UART_FIFO_CAPACITY {{# UART_FIFO_CAPACITY }} # UART TX/RX FIFO capacity in bytes (max 253)
2626

27+
set MTIME_FREQUENCY {{# MTIME_FREQUENCY }} # mtime frequency in Hz (eg. 1000 -> ms, 1000000 -> μs)
28+
2729
# computed values
2830
#%+ set RAM_START = '0x80000000'
2931

@@ -49,6 +51,8 @@ done_icache:
4951

5052
op add UART_FIFO_MODULO UART_FIFO_CAPACITY 1 # actual number of indices used for each FIFO, keeping one empty to check if the buffer is full (max 254)
5153

54+
op div MTIME_SCALE MTIME_FREQUENCY 1000 # mtime multiplier from @time (eg. 1 -> ms, 1000 -> μs)
55+
5256
stop
5357

5458
#%- if false
@@ -60,6 +64,7 @@ set _ MEMORY_WIDTH
6064
set _ ROM_SIZE
6165
set _ PROGRAM_ROM_ICACHE_SIZE
6266
set _ UART_FIFO_MODULO
67+
set _ MTIME_SCALE
6368
set _ MEMORY_X
6469
set _ MEMORY_Y
6570
# {% endraw %}

src/config/configs.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ template: base.mlog.jinja
77
defaults:
88
UART_FIFO_CAPACITY: 253
99
DATA_ROM_ROWS: 0
10+
MTIME_FREQUENCY: 1_000_000
1011

1112
configs:
1213
riscv-arch-test:
@@ -42,3 +43,4 @@ configs:
4243
DATA_ROM_ROWS: 14
4344
RAM_ROWS: 14
4445
ICACHE_ROWS: 14
46+
MTIME_FREQUENCY: 1000

src/cpu/controller.mlog.jinja

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ reset:
4545
read RAM_END {{CONFIG}} "RAM_END"
4646
read ICACHE_END {{CONFIG}} "ICACHE_END"
4747
read UART_FIFO_MODULO {{CONFIG}} "UART_FIFO_MODULO"
48+
read MTIME_SCALE {{CONFIG}} "MTIME_SCALE"
4849

4950
op idiv RAM_START_PROC ROM_SIZE {{ROM_PROC_BYTES}}
5051

@@ -296,14 +297,14 @@ end_breakpoint:
296297
# update mtime
297298
# TODO: handle mtimeh overflow
298299

299-
op sub delta_ms @time last_mtime_update
300-
op mul delta_ms delta_ms 1000 # ms -> μs
300+
op sub _delta @time last_mtime_update
301+
op mul _delta _delta MTIME_SCALE
301302

302-
op add csr_mtime csr_mtime delta_ms
303+
op add csr_mtime csr_mtime _delta
303304
op floor csr_mtime csr_mtime
304305

305-
op shr overflow csr_mtime 32
306-
op add csr_mtimeh csr_mtimeh overflow
306+
op shr _overflow csr_mtime 32
307+
op add csr_mtimeh csr_mtimeh _overflow
307308

308309
op mod csr_mtime csr_mtime 0x100000000
309310

src/peripherals/debugger.mlog.jinja

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ loop:
220220

221221
read UART_FIFO_MODULO {{CPU}} "UART_FIFO_MODULO"
222222

223+
read MTIME_SCALE {{CPU}} "MTIME_SCALE"
224+
op mul MTIME_FREQUENCY MTIME_SCALE 1000
225+
223226
read uart_flags {{CPU}} "uart_flags"
224227

225228
set addr1 ""
@@ -614,11 +617,17 @@ loop__no_mark_icache:
614617

615618
print "{{ '{0}\\n' * 5 }}"
616619

617-
format mtimecmp
620+
set n mtimecmp
621+
op add ret @counter 1
622+
jump format_time always
618623

619-
format stimecmp
624+
set n stimecmp
625+
op add ret @counter 1
626+
jump format_time always
620627

621-
format mtime
628+
set n mtime
629+
op add ret @counter 1
630+
jump format_time always
622631

623632
format mcycle
624633

@@ -981,6 +990,18 @@ format_privilege_mode:
981990
format "reserved"; set @counter ret
982991
format "machine"; set @counter ret
983992

993+
format_time:
994+
jump format_unknown equal n -1
995+
996+
op mod _rem n MTIME_FREQUENCY
997+
op idiv n n MTIME_FREQUENCY
998+
jump format_unknown equal _rem 0
999+
1000+
format "{0}.{0}"
1001+
format n
1002+
format _rem
1003+
set @counter ret
1004+
9841005
# creates a marker on the ram proc containing a given address
9851006
# the caller should print a format string with a placeholder for the address
9861007
# address ->

0 commit comments

Comments
 (0)