@@ -120,29 +120,8 @@ main:
120120 jump main__skip_time_update lessThan @time next_time_update
121121 op add next_time_update @time 1
122122
123- # update cycle
124- # we define the number of cycles as "@ipt * ticks since reset"
125- # this should be a decent estimation of the number of mlog instructions executed since reset
126- # TODO: we could theoretically only update these when they're actually being read, but that would look weird on a debugger
127- op sub cycle @tick RESET_TICK
128- op mul cycle cycle @ipt
129- op floor cycle cycle
130-
131- op mod low cycle 0x100000000
132- op shr high cycle 32
133-
134- write low CSRS CSR_CYCLE
135- write high CSRS CSR_CYCLEH
136-
137- # update time
138- op sub time @time RESET_TIME
139- op floor time time
140-
141- op mod low time 0x100000000
142- op shr high time 32
143-
144- write low CSRS CSR_TIME
145- write high CSRS CSR_TIMEH
123+ op add ret @counter 1
124+ jump update_time_and_cycle always
146125
147126main__skip_time_update:
148127
@@ -488,11 +467,17 @@ inaccessible_store:
488467
489468# enable the reset switch, wait until it's disabled, then reset the processor
490469halt:
470+ op add ret @counter 1
471+ jump update_time_and_cycle always
472+
491473 control enabled RESET_SWITCH true
492474 jump reset always
493475
494476# we jump here after instruction fetch if the single-step switch is enabled
495477pause_step:
478+ op add ret @counter 1
479+ jump update_time_and_cycle always
480+
496481 control enabled PAUSE_SWITCH true
497482 set pause_step_tick @tick
498483
@@ -507,10 +492,41 @@ pause_step__loop:
507492 op sub delta @tick pause_step_tick
508493 op add RESET_TICK RESET_TICK delta
509494
495+ op add ret @counter 1
496+ jump update_time_and_cycle always
497+
510498 jump unpause_step always
511499
512500# helper functions
513501
502+ # update the time/timeh and cycle/cycleh CSRs
503+ # TODO: we could theoretically only update these when they're actually being read, but that would look weird on a debugger
504+ update_time_and_cycle:
505+ # update time
506+ op sub time @time RESET_TIME
507+ op floor time time
508+
509+ op mod low time 0x100000000
510+ op shr high time 32
511+
512+ write low CSRS CSR_TIME
513+ write high CSRS CSR_TIMEH
514+
515+ # update cycle
516+ # we define the number of cycles as "@ipt * ticks since reset"
517+ # this should be a decent estimation of the number of mlog instructions executed since reset
518+ op sub cycle @tick RESET_TICK
519+ op mul cycle cycle @ipt
520+ op floor cycle cycle
521+
522+ op mod low cycle 0x100000000
523+ op shr high cycle 32
524+
525+ write low CSRS CSR_CYCLE
526+ write high CSRS CSR_CYCLEH
527+
528+ set @counter ret
529+
514530# sign extension function
515531# value, length -> result
516532extend_sign:
@@ -1264,6 +1280,9 @@ ECALL_drawflush:
12641280 jump end_instruction_preserve_instret always
12651281
12661282EBREAK:
1283+ op add ret @counter 1
1284+ jump update_time_and_cycle always
1285+
12671286 control enabled PAUSE_SWITCH true
12681287 print "Paused on EBREAK, disable pause switch to continue"
12691288 printflush ERROR_OUTPUT
@@ -1281,6 +1300,9 @@ EBREAK__loop:
12811300 op sub delta @tick pause_tick
12821301 op add RESET_TICK RESET_TICK delta
12831302
1303+ op add ret @counter 1
1304+ jump update_time_and_cycle always
1305+
12841306 printflush ERROR_OUTPUT
12851307 jump end_instruction_preserve_instret always
12861308
0 commit comments