Skip to content

Instantly share code, notes, and snippets.

@jonasdn
Last active August 11, 2020 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasdn/29d9bfa950742f4a1c4693ab74f9b170 to your computer and use it in GitHub Desktop.
Save jonasdn/29d9bfa950742f4a1c4693ab74f9b170 to your computer and use it in GitHub Desktop.
.section .text
.equ MTIME_REG, 0x200bff8 # memory address of MTIME register
.equ RTC_FREQ, 33
.global wait_ms
#
# Arguments:
# - a0: number of milliseconds to busy wait
#
wait_ms:
li s0, MTIME_REG # li: load the constant MTIME_REG into s0 to
lw s1, 0(s0) # lw: load the value at offset 0 of MTIME_REG
# get the number of cycles counted by the RTC
li s2, RTC_FREQ # Load the constant RTC_FREQ into S2
mul s2, s2, a0 # Multiply milliseconds with the frequency
# to get cycles to wait
add s2, s1, s2 # Add cycles to wait to cycles counted
cmp:
lw s1, 0(s0) # Load current number of cycles into s1
blt s1, s2, cmp # If current number of cycles are lower
# than target, keep looping
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment