[render_text] Move all glyphs to the left...
[wikipediardware.git] / forth / serial.s
bloba1a9d4a55a2d5fb66020fcc453581131213578cf
1 ;;; Serial - simple serial I/O
3 ;;; Copyright 2009 Christopher Hall <hsw@openmoko.com>
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions are
7 ;;; met:
8 ;;;
9 ;;; 1. Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; 2. Redistributions in binary form must reproduce the above copyright
13 ;;; notice, this list of conditions and the following disclaimer in
14 ;;; the documentation and/or other materials provided with the
15 ;;; distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY
18 ;;; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 ;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 ;;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
21 ;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 ;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 ;;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 ;;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 ;;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 ;;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ;;; macro for regs.inc
30 .macro REGDEF, address, bits, name
31 R\bits\()_\name = \address
32 .endm
34 .include "c33regs.inc"
36 ;;; register usage
37 ;;; r0 .. r3 must be preserved
38 ;;; r4 result low
39 ;;; r5 result high
40 ;;; r6 .. r9 arguments 1..4
41 ;;; r10 ..r14 reserved
42 ;;; r15 __dp value
44 .section .text
47 ;;; Bits for: REG_EFSIFx_STATUS
48 RXDxNUM1 = (1 << 7)
49 RXDxNUM0 = (1 << 6)
50 TENDx = (1 << 5)
51 FERx = (1 << 4)
52 PERx = (1 << 3)
53 OERx = (1 << 2)
54 TDBEx = (1 << 1)
55 RDBFx = (1 << 0)
58 ;;; print cr and lf
59 ;;; input:
60 ;;; output:
61 .global Serial_PutCRLF
62 Serial_PutCRLF:
63 xld.w %r6, 0x0d
64 xcall Serial_PutChar
65 xld.w %r6, 0x0a
66 jp Serial_PutChar
69 ;;; print a space
70 ;;; input:
71 ;;; output:
72 .global Serial_PutSpace
73 Serial_PutSpace:
74 xld.w %r6, 0x20
75 jp Serial_PutChar
78 ;;; print a character
79 ;;; input:
80 ;;; r6 = char
81 ;;; output:
82 .global Serial_PutChar
83 Serial_PutChar:
84 xld.w %r4, R8_EFSIF0_STATUS
85 Serial_PutChar_wait:
86 ld.ub %r5, [%r4]
87 xand %r5, TDBEx
88 jreq Serial_PutChar_wait
90 xld.w %r5, R8_EFSIF0_TXD
91 ld.b [%r5], %r6
92 ret
94 ;;; print a string
95 ;;; input:
96 ;;; r6 = address of '\0' terminated string
97 ;;; output:
98 ;;; temporary:
99 ;;; r9 = address during loop
100 .global Serial_PutString
101 Serial_PutString:
102 ld.w %r9, %r6
103 Serial_PutString_loop:
104 ld.ub %r6, [%r9]+
105 cmp %r6, 0
106 jreq Serial_PutString_done
107 cmp %r6, 10
108 jreq Serial_PutString_crlf
109 xcall Serial_PutChar
110 jp Serial_PutString_loop
111 Serial_PutString_crlf:
112 xcall Serial_PutCRLF
113 jp Serial_PutString_loop
114 Serial_PutString_done:
118 ;;; print a hex nibble
119 ;;; input:
120 ;;; r6 = 4 bit number to print
121 ;;; output:
122 .global Serial_PutNibble
123 Serial_PutNibble:
124 xand %r6, 0x0f
125 xadd %r6, '0'
126 xcmp %r6, '9'
127 jrle Serial_PutNibble_l1
128 xadd %r6, 'a' - '9' - 1
129 Serial_PutNibble_l1:
130 xcall Serial_PutChar
134 ;;; print a hex word
135 ;;; input:
136 ;;; r6 = 32 bit number to print
137 ;;; output:
138 ;;; temporary:
139 ;;; r9 = thet word being output
140 .global Serial_PutHex
141 Serial_PutHex:
142 ld.w %r9, %r6
143 xsrl %r6, 28
144 xcall Serial_PutNibble
146 ld.w %r6, %r9
147 xsrl %r6, 24
148 xcall Serial_PutNibble
150 ld.w %r6, %r9
151 xsrl %r6, 20
152 xcall Serial_PutNibble
154 ld.w %r6, %r9
155 xsrl %r6, 16
156 xcall Serial_PutNibble
158 ld.w %r6, %r9
159 xsrl %r6, 12
160 xcall Serial_PutNibble
162 ld.w %r6, %r9
163 xsrl %r6, 8
164 xcall Serial_PutNibble
166 ld.w %r6, %r9
167 xsrl %r6, 4
168 xcall Serial_PutNibble
170 ld.w %r6, %r9
171 xcall Serial_PutNibble
176 ;;; read a character
177 ;;; input:
178 ;;; output:
179 ;;; r4 = char
180 .global Serial_GetChar
181 Serial_GetChar:
182 call Serial_InputAvailable
183 or %r4, %r4
184 jreq Serial_GetChar
185 xld.w %r4, R8_EFSIF0_RXD
186 ld.ub %r4, [%r4]
190 ;;; see if input is available
191 ;;; input:
192 ;;; output:
193 ;;; r4 = 0 => not ready
194 ;;; 1 => ready
195 .global Serial_InputAvailable
196 Serial_InputAvailable:
197 xld.w %r4, R8_EFSIF0_STATUS
198 ld.ub %r4, [%r4]
199 xand %r4, RDBFx
200 jreq Serial_InputAvailable_done
201 ld.w %r4, 1
202 Serial_InputAvailable_done: