-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.S
154 lines (130 loc) · 2.31 KB
/
log.S
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
; SPDX-FileCopyrightText: 2023 Andreas Sig Rosvall
;
; SPDX-License-Identifier: GPL-3.0-or-later
; Really simple and fast logging over uart
; The main point is to be fast and reentrant, to facilitate logging
; even from nested interrupts without causing stack overflows
; or any other annoying problems.
.module log
.optsdcc -mmcs51 --model-small
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; USART registers and register bits
U0CSR = 0x86
U1CSR = 0xf8
U0DBUF = 0xc1
U1DBUF = 0xf9
U0UCR = 0xc4
U1UCR = 0xfb
U0GCR = 0xc5
U1GCR = 0xfc
U1CSR_ACTIVE = 0xf8
U1CSR_TX_BYTE = 0xf9
U1CSR_RX_BYTE = 0xfa
U1CSR_ERR = 0xfb
U1CSR_FE = 0xfc
U1CSR_SLAVE = 0xfd
U1CSR_RE = 0xfe
U1CSR_MODE = 0xff
; We're using USART1, because it has bit-adressable status bits
uart_busy = U1CSR_ACTIVE
uart_fifo = U1DBUF
; Handy character names for prefixes/suffixes
_sp = '
_cl = ':
_lf = '\n
_eof = '\0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.area CSEG (CODE)
.macro UART_TX EXPR
; Transmit byte
mov uart_fifo, EXPR
; Wait until sent
jb uart_busy, .
.endm
_putchar::
UART_TX dpl
ret
_puts::
mov b, #_lf
sjmp puts
_puts_sp_cl::
mov b, #_cl
sjmp puts
_puts_nolf::
mov b, #_eof
sjmp puts
.macro define_log_str PREFIX, SUFFIX
_log_'PREFIX'_s'SUFFIX::
mov a, #''PREFIX
mov b, #SUFFIX
sjmp log_pref_s
.endm
define_log_str D, _lf
define_log_str I, _lf
define_log_str W, _lf
define_log_str E, _lf
define_log_str D, _cl
define_log_str I, _cl
define_log_str W, _cl
define_log_str E, _cl
log_pref_s:
UART_TX a
UART_TX #_cl
UART_TX #_sp
; fallthrough to puts
puts:
puts_loop:
clr a
movc a, @a+dptr
jz put_char_b
inc dptr
UART_TX a
sjmp puts_loop
put_char_b:
mov a, b
jz put_char_b_skip
UART_TX a
put_char_b_skip:
ret
.macro HEXNYB R
; Convert low nybble to ascii hex character (0-9,A-F)
anl R, #0x0f
add R, #0x90
da R
addc R, #0x40
da R
.endm
puthex16:
; MSD
mov a, dph
swap a
HEXNYB a
UART_TX a
; LSD
mov a, dph
HEXNYB a
UART_TX a
; fallthrough to puthex8
puthex8:
; MSD
mov a, dpl
swap a
HEXNYB a
UART_TX a
; LSD
mov a, dpl
HEXNYB a
UART_TX a
sjmp put_char_b
_puthex8::
mov b, #'\n
sjmp puthex8
_puthex8_nolf::
mov b, #0
sjmp puthex8
_puthex16::
mov b, #'\n
sjmp puthex16
_puthex16_nolf::
mov b, #0
sjmp puthex16