1 |
301 |
jshamlet |
; Copyright (c)2022 Jeremy Seth Henry
|
2 |
|
|
; All rights reserved.
|
3 |
|
|
;
|
4 |
|
|
; Redistribution and use in source and binary forms, with or without
|
5 |
|
|
; modification, are permitted provided that the following conditions are met:
|
6 |
|
|
; * Redistributions of source code must retain the above copyright
|
7 |
|
|
; notice, this list of conditions and the following disclaimer.
|
8 |
|
|
; * Redistributions in binary form must reproduce the above copyright
|
9 |
|
|
; notice, this list of conditions and the following disclaimer in the
|
10 |
|
|
; documentation and/or other materials provided with the distribution,
|
11 |
|
|
; where applicable (as part of a user interface, debugging port, etc.)
|
12 |
|
|
;
|
13 |
|
|
; THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
|
14 |
|
|
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
15 |
|
|
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
16 |
|
|
; DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
|
17 |
|
|
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
18 |
|
|
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
19 |
|
|
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
20 |
|
|
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
21 |
|
|
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
22 |
|
|
; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
23 |
|
|
;
|
24 |
|
|
;------------------------------------------------------------------------------
|
25 |
|
|
; sys_const.s
|
26 |
|
|
;
|
27 |
|
|
; Common constants & macros for generic hardware or structures used by several
|
28 |
|
|
; tasks
|
29 |
|
|
;
|
30 |
|
|
; Revision History
|
31 |
|
|
; Author Date Change
|
32 |
|
|
;---------------- -------- ---------------------------------------------------
|
33 |
|
|
; Seth Henry 7/15/22 Initial Release
|
34 |
|
|
;------------------------------------------------------------------------------
|
35 |
|
|
|
36 |
|
|
;------------------------------------------------------------------------------
|
37 |
|
|
;-- Serial Port Constants & Macros
|
38 |
|
|
;------------------------------------------------------------------------------
|
39 |
|
|
.DEFINE UART_DATA 0
|
40 |
|
|
.DEFINE UART_STATUS 1
|
41 |
|
|
|
42 |
|
|
.DEFINE UART_RX_PERR 3
|
43 |
|
|
.DEFINE UART_RX_EMPTY 4
|
44 |
|
|
.DEFINE UART_RX_FULL 5
|
45 |
|
|
.DEFINE UART_TX_EMPTY 6
|
46 |
|
|
.DEFINE UART_TX_FULL 7
|
47 |
|
|
|
48 |
|
|
.DEFINE UART_RX_PERR_BIT 2^UART_RX_PERR
|
49 |
|
|
.DEFINE UART_RX_PERR_MASK UART_RX_PERR_BIT ~ $FF
|
50 |
|
|
|
51 |
|
|
.DEFINE UART_RX_EMPTY_BIT 2^UART_RX_EMPTY
|
52 |
|
|
.DEFINE UART_RX_EMPTY_MASK UART_RX_EMPTY_BIT ~ $FF
|
53 |
|
|
|
54 |
|
|
.DEFINE UART_RX_FULL_BIT 2^UART_RX_FULL
|
55 |
|
|
.DEFINE UART_RX_FULL_MASK UART_RX_FULL_BIT ~ $FF
|
56 |
|
|
|
57 |
|
|
.DEFINE UART_TX_EMPTY_BIT 2^UART_TX_EMPTY
|
58 |
|
|
.DEFINE UART_TX_EMPTY_MASK UART_TX_EMPTY_BIT ~ $FF
|
59 |
|
|
|
60 |
|
|
.DEFINE UART_TX_FULL_BIT 2^UART_TX_FULL
|
61 |
|
|
.DEFINE UART_TX_FULL_MASK UART_TX_FULL_BIT ~ $FF
|
62 |
|
|
|
63 |
|
|
; Checking the UART flags involves testing bits in the status register
|
64 |
|
|
; The assembler isn't bright enough to use the defined constant, so this macro
|
65 |
|
|
; does the test on the correct bit
|
66 |
|
|
|
67 |
|
|
.MACRO CHECK_UART_RX_PERR
|
68 |
|
|
BTT 3
|
69 |
|
|
.ENDM
|
70 |
|
|
|
71 |
|
|
.MACRO CHECK_UART_RX_EMPTY
|
72 |
|
|
BTT 4
|
73 |
|
|
.ENDM
|
74 |
|
|
|
75 |
|
|
.MACRO CHECK_UART_RX_FULL
|
76 |
|
|
BTT 5
|
77 |
|
|
.ENDM
|
78 |
|
|
|
79 |
|
|
.MACRO CHECK_UART_TX_EMPTY
|
80 |
|
|
BTT 6
|
81 |
|
|
.ENDM
|
82 |
|
|
|
83 |
|
|
.MACRO CHECK_UART_TX_FULL
|
84 |
|
|
BTT 7
|
85 |
|
|
.ENDM
|
86 |
|
|
|
87 |
|
|
; Register Map:
|
88 |
|
|
; Offset Bitfield Description Read/Write
|
89 |
|
|
; 0x00 AAAAAAAA TX Data (WR) RX Data (RD) (RW)
|
90 |
|
|
; 0x01 EDCBA--- FIFO Status (RO*)
|
91 |
|
|
; A: RX Parity Error (write to clear)
|
92 |
|
|
; B: RX FIFO Empty
|
93 |
|
|
; C: RX FIFO almost full (922/1024)
|
94 |
|
|
; D: TX FIFO Empty
|
95 |
|
|
; E: TX FIFO almost full (922/1024)
|
96 |
|
|
;
|
97 |
|
|
;------------------------------------------------------------------------------
|
98 |
|
|
|
99 |
|
|
;------------------------------------------------------------------------------
|
100 |
|
|
; Macros for setting the return value in Cmd_Error
|
101 |
|
|
;------------------------------------------------------------------------------
|
102 |
|
|
.DEFINE RX_CERR $FB
|
103 |
|
|
.DEFINE RX_TERR $FC
|
104 |
|
|
.DEFINE RX_LERR $FD
|
105 |
|
|
.DEFINE RX_PERR $FE
|
106 |
|
|
.DEFINE RX_OKAY $FF
|
107 |
|
|
|
108 |
|
|
.MACRO RETURN_RX_CMD_OKAY
|
109 |
|
|
LDI R0, #RX_OKAY
|
110 |
|
|
.ENDM
|
111 |
|
|
|
112 |
|
|
.MACRO RETURN_RX_CMD_ERROR
|
113 |
|
|
LDI R0, #RX_CERR
|
114 |
|
|
.ENDM
|
115 |
|
|
|
116 |
|
|
.MACRO RETURN_RX_TIMEOUT_ERROR
|
117 |
|
|
LDI R0, #RX_TERR
|
118 |
|
|
.ENDM
|
119 |
|
|
|
120 |
|
|
.MACRO RETURN_RX_LENGTH_ERROR
|
121 |
|
|
LDI R0, #RX_LERR
|
122 |
|
|
.ENDM
|
123 |
|
|
|
124 |
|
|
.MACRO RETURN_RX_PARITY_ERROR
|
125 |
|
|
LDI R0, #RX_PERR
|
126 |
|
|
.ENDM
|
127 |
|
|
;------------------------------------------------------------------------------
|
128 |
|
|
|
129 |
|
|
;------------------------------------------------------------------------------
|
130 |
|
|
; Common buffer/memory copy
|
131 |
|
|
; Uses R3:R2 as source, R5:R4 as destination, and R1 as the counter variable
|
132 |
|
|
;------------------------------------------------------------------------------
|
133 |
|
|
.MACRO MEM_COPY
|
134 |
|
|
__MEM_CP_LP\@:LDX R2++
|
135 |
|
|
STX R4++
|
136 |
|
|
DBNZ R1, __MEM_CP_LP\@
|
137 |
|
|
.ENDM
|
138 |
|
|
;------------------------------------------------------------------------------
|
139 |
|
|
|
140 |
|
|
;------------------------------------------------------------------------------
|
141 |
|
|
; Common buffer/memory fill/flush
|
142 |
|
|
; Uses R5:R4 as destination, and R1 as the counter variable
|
143 |
|
|
;------------------------------------------------------------------------------
|
144 |
|
|
.MACRO MEM_FILL
|
145 |
|
|
__MEM_FL_LP\@:STX R4++
|
146 |
|
|
DBNZ R1, __MEM_FL_LP\@
|
147 |
|
|
.ENDM
|
148 |
|
|
|
149 |
|
|
.MACRO MEM_FLUSH
|
150 |
|
|
CLR R0
|
151 |
|
|
MEM_FILL
|
152 |
|
|
.ENDM
|
153 |
|
|
;------------------------------------------------------------------------------
|
154 |
|
|
|
155 |
|
|
;------------------------------------------------------------------------------
|
156 |
|
|
; Compute 16-bit Checksum
|
157 |
|
|
;
|
158 |
|
|
; Uses R3:R2 as source, R5:R4 as destination, and R1 as the counter variable
|
159 |
|
|
; Adds all of the memory locations specified to a 16-bit accumulator, returning
|
160 |
|
|
; the result in R5:R4 (destination)
|
161 |
|
|
;------------------------------------------------------------------------------
|
162 |
|
|
.MACRO CALC_CHECKSUM16
|
163 |
|
|
CLR R0
|
164 |
|
|
T0X R4 ; Initialize accumulator R5:R0 to 0
|
165 |
|
|
T0X R5
|
166 |
|
|
T0X R7 ; Initialize R7 to 0
|
167 |
|
|
|
168 |
|
|
__CHK_SM_LP\@:LDX R2++
|
169 |
|
|
ADD R4
|
170 |
|
|
T0X R4
|
171 |
|
|
|
172 |
|
|
TX0 R5
|
173 |
|
|
ADC R7
|
174 |
|
|
T0X R5
|
175 |
|
|
|
176 |
|
|
DBNZ R1, __CHK_SM_LP\@
|
177 |
|
|
.ENDM
|
178 |
|
|
|
179 |
|
|
; These macros assume that the pointer in R3:R2 has been left pointing to the
|
180 |
|
|
; end of the receive buffer by the CALC_CHECKSUM16 macro. Do NOT repoint
|
181 |
|
|
; R3:R2, and be sure to use these macros in the order that the checksum bytes
|
182 |
|
|
; appear in the packet. (THESE ARE ORDER DEPENDENT)
|
183 |
|
|
|
184 |
|
|
.MACRO CHECK_SUM_LB
|
185 |
|
|
LDX R2++
|
186 |
|
|
XOR R4
|
187 |
|
|
.ENDM
|
188 |
|
|
|
189 |
|
|
.MACRO CHECK_SUM_UB
|
190 |
|
|
LDX R2++
|
191 |
|
|
XOR R5
|
192 |
|
|
.ENDM
|
193 |
|
|
;------------------------------------------------------------------------------
|
194 |
|
|
|
195 |
|
|
;------------------------------------------------------------------------------
|
196 |
|
|
; Convert ASCII to HEX
|
197 |
|
|
;
|
198 |
|
|
; Converts an ASCII value into an integer value from 0x0 to 0xF
|
199 |
|
|
; Assumes incoming data is in R0. Returns a value in R0
|
200 |
|
|
; Returns a value of 0x00 to 0x0F for valid characters, or
|
201 |
|
|
; 0xFF (-1) for invalid characters
|
202 |
|
|
;------------------------------------------------------------------------------
|
203 |
|
|
.MACRO CONVERT_ASC2HEX
|
204 |
|
|
PSH R1 ; Preserve R1 and R2
|
205 |
|
|
PSH R2
|
206 |
|
|
|
207 |
|
|
T0X R2 ; Make a copy of R0 to R2 for backup
|
208 |
|
|
LDI R1, #$F0 ; Mask off the lower bits to figure out the range
|
209 |
|
|
AND R1
|
210 |
|
|
T0X R1 ; Copy the upper 4-bits to R1 for branching tests
|
211 |
|
|
|
212 |
|
|
LDI R0, #$30 ; Is it a decimal char 0-9?
|
213 |
|
|
XOR R1
|
214 |
|
|
BRZ _CNV_AH_09_\@
|
215 |
|
|
|
216 |
|
|
LDI R0, #$40 ; Is it a hex char A-F?
|
217 |
|
|
XOR R1
|
218 |
|
|
BRZ _CNV_AH_AF_\@
|
219 |
|
|
|
220 |
|
|
LDI R0, #$60 ; Is it a hex char a-f?
|
221 |
|
|
XOR R1
|
222 |
|
|
BRZ _CNV_AH_AF_\@
|
223 |
|
|
|
224 |
|
|
BNI _CNV_AH_NV_\@
|
225 |
|
|
|
226 |
|
|
; Valid HEX characters 0-9 are ASCII codes 0x30 to 0x39. Codes 0x3A to 0x3F are
|
227 |
|
|
; invalid, so check that the value in R0 is LESS than $3A.
|
228 |
|
|
_CNV_AH_09_\@:TX0 R2 ; Restore R0 from backup
|
229 |
|
|
LDI R1, #$0F ; Mask off the upper bits to check validity
|
230 |
|
|
AND R1
|
231 |
|
|
T0X R2 ; Backup the lower 4-bits to R2
|
232 |
|
|
|
233 |
|
|
; Check for 0x0A to 0x0F
|
234 |
|
|
LDI R1, #$0A ; Load R1 with 0x0A (:)
|
235 |
|
|
CMP R1 ; Compare R0 to R1
|
236 |
|
|
BNN _CNV_AH_NV_\@ ; Branch if not negative (R0 > 9)
|
237 |
|
|
|
238 |
|
|
TX0 R2 ; Restore the lower 4-bits to R0
|
239 |
|
|
BNI _CNV_AH_EX_\@
|
240 |
|
|
|
241 |
|
|
; Valid HEX characters A-F are 0x41 to 0x46 OR 0x61 to $66. The upper 4-bits
|
242 |
|
|
; have already been checked, so just verify that the lower 4-bits are between
|
243 |
|
|
; 0x01 and 0x06. 0x00 and 0x07 to 0x0F are invalid,
|
244 |
|
|
; so check that the value in R0 is LESS than $3A
|
245 |
|
|
_CNV_AH_AF_\@:TX0 R2 ; Restore R0 from backup
|
246 |
|
|
LDI R1, #$0F ; Mask off the upper bits to check validity
|
247 |
|
|
AND R1
|
248 |
|
|
T0X R2 ; Backup the lower 4-bits to R2
|
249 |
|
|
|
250 |
|
|
; Check for 0x00
|
251 |
|
|
BRZ _CNV_AH_NV_\@ ; 0x0 is an invalid code
|
252 |
|
|
|
253 |
|
|
; Check for 0x07 to 0x0F
|
254 |
|
|
LDI R1, #$07 ; Load R1 with 0x7 (G or g)
|
255 |
|
|
CMP R1 ; Compare R0 to R1
|
256 |
|
|
BNN _CNV_AH_NV_\@ ; Branch of not negative (R0 > 6)
|
257 |
|
|
|
258 |
|
|
; If this is a valid character, add 9 to the lower 4-bits for the result
|
259 |
|
|
; (0x01 to 0x06 -> 0x0A to 0x0F)
|
260 |
|
|
TX0 R2 ; Restore lower 4-bits to R0
|
261 |
|
|
LDI R1, #$09 ; Load R1 with 0x9
|
262 |
|
|
ADD R1 ; Added 9 to R0 to convert to A-F
|
263 |
|
|
BNI _CNV_AH_EX_\@
|
264 |
|
|
|
265 |
|
|
_CNV_AH_NV_\@:LDI R0, #$FF ; Return 0xFF on an invalid character
|
266 |
|
|
|
267 |
|
|
_CNV_AH_EX_\@:POP R2 ; Restore R1 and R2
|
268 |
|
|
POP R1
|
269 |
|
|
.ENDM
|
270 |
|
|
;------------------------------------------------------------------------------
|
271 |
|
|
|
272 |
|
|
;------------------------------------------------------------------------------
|
273 |
|
|
; Convert the lower nibble of R0 into a valid ASCII character
|
274 |
|
|
; Accepts data in R0 and returns the ASCII code in R0
|
275 |
|
|
;------------------------------------------------------------------------------
|
276 |
|
|
.MACRO CONVERT_NIB2ASC
|
277 |
|
|
PSH R1 ; Preserve R1
|
278 |
|
|
|
279 |
|
|
LDI R1, #$0F ; Mask away the upper 4-bits
|
280 |
|
|
AND R1
|
281 |
|
|
|
282 |
|
|
T0X R1 ; Copy to R1
|
283 |
|
|
LDI R0, #$09 ; Load R0 with 0x09
|
284 |
|
|
CMP R1 ; Compare R0 to R1
|
285 |
|
|
BNN _CNV_NA_09_\@
|
286 |
|
|
; Fall into _MC_FM_CHA_AF
|
287 |
|
|
|
288 |
|
|
_CNV_NA_AF_\@:LDI R0, #$37 ; Add 0x37 to the nibble to get the ASCII value
|
289 |
|
|
ADD R1
|
290 |
|
|
BNI _CNV_NA_EX_\@
|
291 |
|
|
|
292 |
|
|
_CNV_NA_09_\@:LDI R0, #$30 ; Add 0x30 to the nibble to get the ASCII value
|
293 |
|
|
OR R1
|
294 |
|
|
; Fall into _MC_FM_CHA_EX
|
295 |
|
|
|
296 |
|
|
_CNV_NA_EX_\@:POP R1
|
297 |
|
|
.ENDM
|
298 |
|
|
;------------------------------------------------------------------------------
|
299 |
|
|
|
300 |
|
|
;------------------------------------------------------------------------------
|
301 |
|
|
; Convert the packed BCD value in R0 to its ASCII equivalent in R3:R2
|
302 |
|
|
; $59 -> $35, $39
|
303 |
|
|
;------------------------------------------------------------------------------
|
304 |
|
|
.MACRO CONV_PBCD_TO_ASCII
|
305 |
|
|
T0X R2 ; Copy R0 -> R2 for re-use
|
306 |
|
|
LDI R0, #$F0 ; Set the upper nibble mask. BCD is only 3-bit
|
307 |
|
|
AND R2 ; Mask off the upper 3-bits of value -> R0
|
308 |
|
|
CLP PSR_C ; Clear the carry (in case CPU options not set)
|
309 |
|
|
ROR R0 ; Shift the upper nibble down
|
310 |
|
|
ROR R0
|
311 |
|
|
ROR R0
|
312 |
|
|
ROR R0
|
313 |
|
|
LDI R1, #$30 ; Set the ASCII upper nibble mask of $30
|
314 |
|
|
OR R1 ; Create an ASCII character
|
315 |
|
|
T0X R3 ; Transfer the upper character to R3
|
316 |
|
|
|
317 |
|
|
LDI R0, #$0F ; Set the lower nibble mask
|
318 |
|
|
AND R2 ; Mask off the lower bits
|
319 |
|
|
OR R1 ; Create an ASCII character
|
320 |
|
|
T0X R2 ; Store the lower character to R1
|
321 |
|
|
RTS
|
322 |
|
|
.ENDM
|
323 |
|
|
;------------------------------------------------------------------------------
|