summaryrefslogtreecommitdiff
path: root/assembly/rrxing2/util.S
blob: 4d55c6be2095b4a85a3272ca3f694e95e7d192e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Railroad Crossing 2 for RVController - Utility Functions and Macros
# A product of Scheiße & Broken Signalling Systems, a Cheapie Systems company
# This is free and unencumbered software released into the public domain.
# See http://unlicense.org/ for more information

.macro digiline_send channel msg
	la a0,(\channel)
	la a1,(\msg)
	li a7,129
	ecall
.endm

.macro set_timeri time
	li t0,0x80
	csrs mie,t0 # unmask timer interrupt
	ld t1,2(gp) # read mtime
	li t0,(\time)
	add t1,t1,t0 # calculate target value (lower half)
	sltu t0,t1,t0 # check if it overflowed
	add t2,t2,t0 # add the overflow into the high half if there was any
	sd t1,10(gp) # set mtimecmp to the target value
.endm

.macro stop_timer
	li t1,0xffffffff
	li t2,0xffffffff
	sd t1,10(gp) # set mtime to far in the future
	li t1,0x80
	csrc mie,t1 # mask timer interrupt
.endm

.macro set_timer_to label
	cm.push {ra},-16
	la a0,(\label)
	lbu a0,0(a0)
	call set_timer
	cm.pop {ra},16
.endm

set_timer:
# Expects a number of seconds in a0
li t0,0x80
csrs mie,t0 # unmask timer interrupt
ld t1,2(gp) # read mtime
add t1,t1,a0 # calculate target value (lower half)
sltu t0,t1,a0 # check if it overflowed
add t2,t2,t0 # add the overflow into the high half if there was any
sd t1,10(gp) # set mtimecmp to the target value
ret

hexdump:
# Expects a pointer to the output location in a0 and a number in a1, always outputs 8 digits
li t0,7
1: li t1,0xf
slli t2,t0,2 # Multiply digit number by 4 to get shift amount
sll t1,t1,t2 # Shift mask over to the necessary digit
and t1,t1,a1 # Get the necessary digit
srl t1,t1,t2 # Move it to the ones place
addi t1,t1,0x30 # Try converting it to an ASCII digit
li t2,0x3a
blt t1,t2,2f # If it's out of range...
addi t1,t1,7 # ...turn it into a letter
2: sb t1,0(a0) # Write out the digit
addi a0,a0,1 # Advance the pointer
addi t0,t0,-1
bgez t0,1b
ret