Skip to content

Commit ac46116

Browse files
committed
Recalculate max instruction cost
1 parent 213de64 commit ac46116

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/cpu/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ schematics:
77
ram: ../../schematics/ram_proc.msch
88

99
inputs:
10-
# 46 (main) + 9 (end)
11-
base_instruction_cost: 55
10+
# 42 (main) + 9 (end)
11+
base_instruction_cost: 51
1212

1313
instructions:
1414
- label: BEQ

src/cpu/worker.mlog.jinja

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44

55
#%# constants
66

7-
#%# the slowest instruction seems to be AMOMIN.W in icache-covered memory
8-
#% set slowest_op = (instructions|selectattr("label", "equalto", "AMOMIN.W")|first).cost
7+
#%# the slowest instruction seems to be an uncached AMOMIN.W in RAM, reading a branch instruction from icache-covered memory
98

10-
#%# ... + check_interrupts + 2*(decode + decode_BRANCH + end_decode) + store_ram_word_unchecked
11-
#% set MAX_INSTRUCTION_ACCUMULATOR_USAGE = (base_instruction_cost + slowest_op + 8 + 2*(23 + 16 + 33) + 23)|round(method="ceil")|int
9+
#% set amo_cost = (instructions|selectattr("label", "equalto", "AMOMIN.W")|first).cost
10+
11+
#%# decode + decode_AMO + end_decode (decode_address == null)
12+
#% set amo_decode = 23 + 9 + 4
13+
14+
#%# decode + decode_BRANCH + end_decode
15+
#% set branch_decode = 23 + 16 + 33
16+
17+
#%# ... + check_interrupts + load_ram_word_unchecked + store_ram_word_unchecked + instret overflow + a bit extra to be safe
18+
#% set MAX_INSTRUCTION_COST = (base_instruction_cost + amo_decode + amo_cost + branch_decode + 8 + 32 + 23 + 3 + 10)|round(method="ceil")|int
1219

1320
#%# linked buildings
1421
#%# IMPORTANT: the buildings must be linked in the order shown here
@@ -113,12 +120,14 @@ check_interrupts__machine:
113120
set mcause 0x80000007 # machine timer interrupt
114121
jump trap_without_mtval notEqual mie.mtie 0
115122

123+
# cost:
124+
# base: 17
125+
# fast path: 42 (base + 9 + 16)
126+
# slow path: 35 (base + 13 + 5) + dynamic
116127
main:
117128
# store the pc for the following instruction in a separate variable, so jumps and traps don't need to account for the pc being incremented at the end of an instruction
118129
op add next_pc pc 4
119130

120-
set mtval pc
121-
122131
# get the current instruction cache processor and variable
123132
jump main__read_icache notEqual icache_var null
124133

@@ -129,14 +138,28 @@ main:
129138
jump main__access_icache lessThan pc ICACHE_SIZE
130139

131140
main__slow_instruction_fetch:
141+
# update accumulator in case we take a trap while loading
142+
# 13 (instructions between main and the below comment) + 5 (load_word static cost)
143+
op sub accumulator accumulator 18
144+
132145
jump default_mtvec_handler equal pc {{SYSCON}}
133146

147+
set mtval pc
134148
set mcause 1 # instruction access fault
135149
jump trap greaterThanEq pc RAM_END # prevent executing from MMIO
136150

137151
set address pc
138152
op add ret @counter 1
139153
jump load_word always
154+
# ^ above accumulator count ends here
155+
156+
# a trap cannot occur between now and the lookup table fetch
157+
# so modify accumulator to take base_instruction_cost into account
158+
# base_instruction_cost assumes we take the fast path
159+
# at this point, we've double-counted 5 instructions on the base path
160+
# also, the static cost of the slow path is 7 instructions fewer than the fast path
161+
# so we can safely add 12 instructions back into the accumulator
162+
op add accumulator accumulator 12
140163

141164
set instruction result
142165
set decode_address null
@@ -225,7 +248,7 @@ end_instruction_trap:
225248
jump state->pause equal single_step_enabled true
226249

227250
jump ipt_overrun notEqual current_tick @tick
228-
jump next_tick lessThanEq accumulator {{MAX_INSTRUCTION_ACCUMULATOR_USAGE}}
251+
jump next_tick lessThanEq accumulator {{MAX_INSTRUCTION_COST}}
229252
jump main notEqual interrupts_pending true
230253
jump check_interrupts always
231254

@@ -1757,7 +1780,7 @@ ECALL:
17571780

17581781
EBREAK:
17591782
set mcause 3 # breakpoint
1760-
# mtval is set to pc by main
1783+
set mtval pc
17611784
jump trap always
17621785

17631786
MRET:
@@ -2266,7 +2289,7 @@ set {{UART_RX_WRITE}} null
22662289
set {{UART_TX_START}} null
22672290
set {{UART_TX_READ}} null
22682291
set {{UART_TX_WRITE}} null
2269-
set {{MAX_INSTRUCTION_ACCUMULATOR_USAGE}} null
2292+
set {{MAX_INSTRUCTION_COST}} null
22702293
# local preprocessor constants
22712294
set {{UART_START_LINK}} null
22722295
set {{LABELS}} null

0 commit comments

Comments
 (0)