Subversion Repositories MB01 Project

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 - 1
;;
2
;; Copyright (c) 2016 Marco Granati <mg@unet.bz>
3
;;
4
;; Permission to use, copy, modify, and distribute this software for any
5
;; purpose with or without fee is hereby granted, provided that the above
6
;; copyright notice and this permission notice appear in all copies.
7
;;
8
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
;;
16
 
17
;; name: sp.asm
18
;; rev.: 2016/07/28
19
;; bios C816 version v1.0
20
 
21
;; serial port handler
22
 
23
_ACIA_INC_	.SET	1
24
 
25
	.INCLUDE INC\GLOBAL.INC
26
	.INCLUDE INC\DIRP00.INC
27
 
28
.LIST	on
29
 
30
; page 0 local var's (declared in bios temp. work area)
31
_P0BTMP:	.SECTION page0, ref_only, offset bwrktmp	; DP Tmp. BIOS
32
 
33
crc16		.DW
34
bycnt		.DW		; data packet lenght
35
byndx		.DW		; data buffer index
36
pkttyp		.DB		; expected packet type
37
pktnum		.DB		; current packet number
38
expect		.DB		; expected packet number
39
tmpx		.DB
40
 
41
	.ENDS
42
 
43
	.CODEF8
44
 
45
	.LONGA	off
46
	.LONGI	off
47
 
48
; X=serial port
49
_spget0:
50
	txa
51
	bne	_spget1
52
 
53
	SPGETB	0
54
 
55
_spget1:
56
	SPGETB	1
57
 
58
; X=serial port
59
	.PUBLIC _spput0
60
_spput0:
61
	txy
62
	bne	_spput1
63
 
64
	SPPUTB	0
65
 
66
_spput1:
67
	SPPUTB	1
68
 
69
; A=mode, X=serial #
70
spset:
71
	txy
72
	beq	?sr0		; serial #0
73
	ldx	#4		; serial #1
74
?sr0:	sei			; disable interrupt
75
	sta	spmode,x
76
	tay
77
	lda	#$7F		; disable all interrupts
78
	sta	.ABS.ACIAIER,x
79
	lda	.ABS.ACIARDR,x	; discard any pending received data
80
	lda	.ABS.ACIAISR,x	; read current interrupt status
81
	lda	.ABS.ACIACSR,x	; save current control status
82
	sta	spcsr,x
83
	tya			; mode
84
	and	#00001100B	; mask on bits 3 & 2 (parity mode)
85
	ora	#11100000B	; 8 data bits, RTS=DTR=low
86
	sta	spfr,x		; value of format register
87
	;ora	#00000011B
88
	sta	.ABS.ACIAFR,x	; set format register: RTS=DTR=1
89
	tya
90
	ldy	#01001110B	; access to ACR, one stop bit, 38400
91
	bit	#00010000B
92
	bne	?br		; select baud rate=38400
93
	ldy	#01001101B	; access to ACR, one stop bit, 19200
94
?br:	tya
95
	sta	.ABS.ACIACR,x	; set control register
96
	stz	.ABS.ACIAACR,x	; set aux. control reg. (normal tx, parity)
97
	lda	#$80
98
	tsb	VIA1+VIAPRANH	; enble RS232
99
	lda	#00000010B	; check RS485
100
	bit	spmode,x
101
	beq	?cnt		; interface type: RS232
102
	lda	spmode,x
103
	and	#10111111B	; software handshake only for RS485
104
	sta	spmode,x
105
	lda	#$80
106
	trb	VIA1+VIAPRANH	; enble RS485 interface
107
	lda	#$40
108
	trb	VIA0+VIAPRANH	; disable 120 ohm termination
109
	lda	#00000001B	; check termination status
110
	bit	spmode,x
111
	beq	?cnt		; termination off
112
	lda	#$40
113
	tsb	VIA0+VIAPRANH	; enable 120 ohm termination
114
?cnt:	stz	sppause,x	; init work area
115
	stz	spout,x
116
	stz	spstat,x
117
	bit	spmode,x
118
	CPU16			; init buffer's pointer's
119
	bvs	?hw		; hardware handshake
120
	lda	#NGUARD1
121
	ldy	#NFREE1
122
	bra	?do
123
?hw:	lda	#NGUARD2
124
	ldy	#NFREE2
125
?do:	sta	icntmax,x
126
	sty	icntmin,x
127
	sec
128
	lda	#SIBUFSIZ
129
	sbc	icntmax,x
130
	sta	icntmax,x
131
	stz	ibuftail,x
132
	stz	ibufhead,x
133
	stz	ibufcnt,x
134
	stz	obuftail,x
135
	stz	obufhead,x
136
	stz	obufcnt,x
137
	CPU08
138
	lda	.ABS.ACIARDR,x	; discard any pending received data
139
	lda	.ABS.ACIAISR,x	; read current interrupt status
140
	lda	.ABS.ACIACSR,x	; save current control status
141
	sta	spcsr,x
142
	and	#00111011B	; mask lines level
143
	bit	#00001000B	; check DSR line level
144
	beq	?sav		; DSR is low
145
	ora	#01000000B	; DSR is high
146
?sav:	asl	a		; 7:DSR, 6:CTS, 5:DCD, 2:DTR, 1:RTS
147
	sta	splin,x		; save lines level
148
	lda	#$F9		; enable interrupts
149
	sta	.ABS.ACIAIER,x
150
	cli
151
	rts
152
 
153
; X=serial #
154
spres:
155
	txy
156
	beq	?sr0		; serial #0
157
	ldx	#4		; serial #1
158
?sr0:	sei			; disable interrupt
159
	lda	#$7F		; disable all interrupts
160
	sta	.ABS.ACIAIER,x
161
	lda	spfr,x
162
	ora	#00000011B	; set RTS=DTR=1
163
	sta	.ABS.ACIAFR,x
164
	lda	.ABS.ACIARDR,x	; discard any pending received data
165
	lda	.ABS.ACIAISR,x	; read current interrupt status
166
	lda	.ABS.ACIACSR,x	; save current control status
167
	sta	spcsr,x
168
	cli
169
	rts
170
 
171
;---- TRANSFER
172
 
173
; X=port (0 or 4)
174
; get byte with timeout
175
sptget:
176
	lda	#$20		; set T2 count PB6 pulse (1ms)
177
	tsb	.ABS.VIA0+VIAACR
178
	stz	.ABS.VIA0+VIAT2CL
179
	lda	#4		; set 1024ms timeout
180
	sta	.ABS.VIA0+VIAT2CH
181
?lp:	jsr	_spget0		; get byte within timeout
182
	bcc	?exit		; ok, data available
183
	tay			; CF=1 here
184
	bne	?exit		; some rx error(s)
185
	lda	#T2IFRB
186
	bit	.ABS.VIA0+VIAIFR
187
	beq	?lp		; no timeout: loop
188
	lda	#$08		; set timeout error - CF=1 here
189
?exit:	rts
190
 
191
sndnack:
192
	;ldy	#NACK
193
	bra	sndack2
194
 
195
sndack:
196
	ldy	#ACK
197
 
198
sndack2:
199
	sty	tmpx
200
	lda	#SYN
201
	jsr	sptput
202
	bcs	exit1
203
	lda	tmpx
204
	jsr	sptput
205
	bcs	exit1
206
	lda	pktnum
207
 
208
spxput:
209
	cmp	#SYN
210
	beq	?esc
211
	cmp	#SPXON
212
	beq	?esc
213
	cmp	#SPXOFF
214
	beq	?esc
215
	cmp	#DLE
216
	bne	sptput
217
?esc:	eor	#$20
218
	sta	tmpx
219
	lda	#DLE
220
	jsr	sptput
221
	bcs	exit1
222
	lda	tmpx
223
 
224
; X=port (0 or 4)
225
; put byte with timeout
226
sptput:
227
	tay			; Y=data
228
	lda	#$20		; set T2 count PB6 pulse (1ms)
229
	tsb	.ABS.VIA0+VIAACR
230
	stz	.ABS.VIA0+VIAT2CL
231
	lda	#4		; set 1024ms timeout
232
	sta	.ABS.VIA0+VIAT2CH
233
?lp:	tya			; byte to send
234
	jsr	_spput0		; put byte within timeout
235
	bcc	exit1		; ok
236
	iny			; Y=$FF if remote disconnession
237
	beq	?err		; remote terminal disconnected
238
	tay			; Y=data
239
	lda	#T2IFRB
240
	bit	.ABS.VIA0+VIAIFR
241
	beq	?lp		; no timeout: loop
242
	tya			; restore data
243
?err:	ldy	#$80		; timeout or remote disconnession
244
exit1:	rts
245
 
246
; frame alignment
247
; A=expected packet type (SOH or STX)
248
; X=0 (serial port 0)
249
; exit with CF=0 if no error (ZF=0 if STX/SOH, ZF=1 if EOT)
250
fndsyn:
251
	sta	pkttyp
252
	jsr	sptget
253
	bcs	?done		; error
254
	cmp	#SYN
255
	bne	fndsyn		; skip bytes until find start of trame
256
?lp:	jsr	sptget		; next byte
257
	bcs	?done		; error
258
	cmp	#SYN		; discard further consecutive sync's
259
	beq	?lp
260
	cmp	pkttyp		; expected SOH, STX or EOT
261
	bne	?eot
262
	lda	#$FF		; initialize crc computation
263
	sta	crc16
264
	sta	crc16+1		; if received STX or SOH exit with ZF=0
265
?ok:	clc			; no error
266
	rts
267
?eot:	ldy	pkttyp		; when expect STX, an EOT is legal
268
	cpy	#STX
269
	bne	?err		; unexpected packet type
270
	cmp	#EOT		; received an EOT?
271
	beq	?ok		; exit with ZF=1 (EOT)
272
?err:	lda	#$10		; unexpected packet type error
273
	sec
274
?done:	rts
275
 
276
; get escaped byte
277
; X=serial port
278
spxget:
279
	jsr	sptget
280
	bcs	?done
281
	cmp	#SYN		; here a SYN is an error
282
	beq	?done		; exit
283
	cmp	#DLE
284
	bne	?ok
285
	jsr	sptget
286
	bcs	?done
287
	eor	#$20
288
?ok:	clc
289
?done:	rts
290
 
291
updcrc:
292
	tay
293
	eor 	crc16+1		; quick CRC computation with lookup tables
294
	tax			; index for lookup table
295
	lda 	crc16
296
	eor 	>crchi,x
297
	sta 	crc16+1
298
	lda 	>crclo,x
299
	sta 	crc16
300
	tya
301
	rts
302
 
303
; X=serial port (0 or 4)
304
getdat:
305
	ACC16
306
	lda	#$200
307
	ldy	pkttyp
308
	cpy	#STX
309
	beq	?st
310
	lda	#$10
311
?st:	sta	bycnt
312
	stz	byndx
313
	ACC08
314
?lp:	jsr	spxget
315
	bcs	?err
316
	jsr	updcrc
317
	INDEX16
318
	ldx	byndx
319
	sta	>SPBUF,x
320
	inx
321
	stx	byndx
322
	cpx	bycnt
323
	INDEX08
324
	ldx	#0
325
	bcc	?lp
326
	jsr	spxget
327
	bcs	?err
328
	cmp	crc16
329
	bne	?err
330
	jsr	spxget
331
	bcs	?err
332
	cmp	crc16+1
333
	bne	?err
334
	clc
335
	rts
336
?err:	sec
337
	rts
338
 
339
; X=serial port (0 or 4)
340
getpkt:
341
	lda	#STX
342
	jsr	fndsyn
343
	bcs	?done
344
	jsr	spxget
345
	bcs	?done
346
	sta	pktnum
347
	jsr	spxget
348
	bcs	?done
349
	eor	#$FF
350
	cmp	pktnum
351
	bne	?err
352
	cmp	expect
353
	bne	?err
354
	jsr	getdat
355
	bcs	?err
356
 
357
	; send positive ACK
358
	jmp	sndack
359
 
360
?err:
361
?done:	rts
362
 
363
 
364
;------------------
365
 
366
lspget:
367
	.PUBLIC lspget
368
	phd			; save DP
369
	phb			; save DBR
370
	pea	#0		; set DP = $0000
371
	pld
372
	ldy	#0		; set DBR = $00
373
	phy
374
	plb
375
	jsr	_spget0
376
	plb
377
	pld
378
	rtl
379
 
380
lspput:
381
	.PUBLIC lspput
382
	phd			; save DP
383
	phb			; save DBR
384
	pea	#0		; set DP = $0000
385
	pld
386
	ldy	#0		; set DBR = $00
387
	phy
388
	plb
389
	jsr	_spput0
390
	plb
391
	pld
392
	rtl
393
 
394
lspset:
395
	.PUBLIC lspset
396
	phd			; save DP
397
	phb			; save DBR
398
	pea	#0		; set DP = $0000
399
	pld
400
	ldy	#0		; set DBR = $00
401
	phy
402
	plb
403
	jsr	spset
404
	plb
405
	pld
406
	rtl
407
 
408
lspres:
409
	.PUBLIC lspres
410
	phd			; save DP
411
	phb			; save DBR
412
	pea	#0		; set DP = $0000
413
	pld
414
	ldy	#0		; set DBR = $00
415
	phy
416
	plb
417
	jsr	spres
418
	plb
419
	pld
420
	rtl
421
 
422
crc16a:
423
	xba
424
	lda	#0
425
	ldx	#8
426
	ACC16
427
	eor	crc16
428
?lp:	asl	a
429
	bcc	?skp
430
	eor	#$1021
431
?skp:	dex
432
	bne	?lp
433
	sta	crc16
434
	ACC08
435
	rts
436
 
437
crc16b:
438
	ldx	#8
439
?lp:	tay
440
	eor	crc16
441
	lsr	a
442
	ACC16
443
	php
444
	lda	crc16
445
	lsr	a
446
	plp
447
	bcc	?nxt
448
	eor	#$8408
449
?nxt:	sta	crc16
450
	ACC08
451
	tya
452
	lsr	a
453
	dex
454
	bne	?lp
455
	rts
456
 
457
crccalc:
458
	phd
459
	pea	#0
460
	pld
461
	stx	$ff
462
 
463
	lda	#$FF
464
	sta	crc16
465
	sta	crc16+1
466
	jsr	crctb
467
	ACC16
468
	lda	crc16
469
	sta	$f0
470
	ACC08
471
	lda	#$00
472
	sta	crc16
473
	lda	#$00
474
	sta	crc16+1
475
	jsr	crctb
476
	ACC16
477
	lda	crc16
478
	sta	$f2
479
	ACC08
480
	lda	#$0F
481
	sta	crc16
482
	lda	#$1D
483
	sta	crc16+1
484
	jsr	crctb
485
	ACC16
486
	lda	crc16
487
	sta	$f4
488
	ACC08
489
	pld
490
	rtl
491
 
492
 
493
crctb:
494
	stz	$fe
495
?lp:
496
	ldx	$fe
497
	cpx	$ff
498
	bcs	?end
499
	lda	>$100000,x
500
	inx
501
	stx	$fe
502
 
503
	eor 	crc16+1 		; Quick CRC computation with lookup tables
504
       	tax		 	; updates the two bytes at crc & crc+1
505
       	lda 	crc16		; with the byte send in the "A" register
506
       	eor 	>crchi,x
507
       	sta 	crc16+1
508
      	lda 	>crclo,x
509
       	sta 	crc16
510
 
511
       	bra	?lp
512
?end:
513
	rts			; y=82 on exit
514
 
515
crclo
516
 .byte $00,$21,$42,$63,$84,$A5,$C6,$E7,$08,$29,$4A,$6B,$8C,$AD,$CE,$EF
517
 .byte $31,$10,$73,$52,$B5,$94,$F7,$D6,$39,$18,$7B,$5A,$BD,$9C,$FF,$DE
518
 .byte $62,$43,$20,$01,$E6,$C7,$A4,$85,$6A,$4B,$28,$09,$EE,$CF,$AC,$8D
519
 .byte $53,$72,$11,$30,$D7,$F6,$95,$B4,$5B,$7A,$19,$38,$DF,$FE,$9D,$BC
520
 .byte $C4,$E5,$86,$A7,$40,$61,$02,$23,$CC,$ED,$8E,$AF,$48,$69,$0A,$2B
521
 .byte $F5,$D4,$B7,$96,$71,$50,$33,$12,$FD,$DC,$BF,$9E,$79,$58,$3B,$1A
522
 .byte $A6,$87,$E4,$C5,$22,$03,$60,$41,$AE,$8F,$EC,$CD,$2A,$0B,$68,$49
523
 .byte $97,$B6,$D5,$F4,$13,$32,$51,$70,$9F,$BE,$DD,$FC,$1B,$3A,$59,$78
524
 .byte $88,$A9,$CA,$EB,$0C,$2D,$4E,$6F,$80,$A1,$C2,$E3,$04,$25,$46,$67
525
 .byte $B9,$98,$FB,$DA,$3D,$1C,$7F,$5E,$B1,$90,$F3,$D2,$35,$14,$77,$56
526
 .byte $EA,$CB,$A8,$89,$6E,$4F,$2C,$0D,$E2,$C3,$A0,$81,$66,$47,$24,$05
527
 .byte $DB,$FA,$99,$B8,$5F,$7E,$1D,$3C,$D3,$F2,$91,$B0,$57,$76,$15,$34
528
 .byte $4C,$6D,$0E,$2F,$C8,$E9,$8A,$AB,$44,$65,$06,$27,$C0,$E1,$82,$A3
529
 .byte $7D,$5C,$3F,$1E,$F9,$D8,$BB,$9A,$75,$54,$37,$16,$F1,$D0,$B3,$92
530
 .byte $2E,$0F,$6C,$4D,$AA,$8B,$E8,$C9,$26,$07,$64,$45,$A2,$83,$E0,$C1
531
 .byte $1F,$3E,$5D,$7C,$9B,$BA,$D9,$F8,$17,$36,$55,$74,$93,$B2,$D1,$F0
532
 
533
; hi byte CRC lookup table (should be page aligned)
534
crchi
535
 .byte $00,$10,$20,$30,$40,$50,$60,$70,$81,$91,$A1,$B1,$C1,$D1,$E1,$F1
536
 .byte $12,$02,$32,$22,$52,$42,$72,$62,$93,$83,$B3,$A3,$D3,$C3,$F3,$E3
537
 .byte $24,$34,$04,$14,$64,$74,$44,$54,$A5,$B5,$85,$95,$E5,$F5,$C5,$D5
538
 .byte $36,$26,$16,$06,$76,$66,$56,$46,$B7,$A7,$97,$87,$F7,$E7,$D7,$C7
539
 .byte $48,$58,$68,$78,$08,$18,$28,$38,$C9,$D9,$E9,$F9,$89,$99,$A9,$B9
540
 .byte $5A,$4A,$7A,$6A,$1A,$0A,$3A,$2A,$DB,$CB,$FB,$EB,$9B,$8B,$BB,$AB
541
 .byte $6C,$7C,$4C,$5C,$2C,$3C,$0C,$1C,$ED,$FD,$CD,$DD,$AD,$BD,$8D,$9D
542
 .byte $7E,$6E,$5E,$4E,$3E,$2E,$1E,$0E,$FF,$EF,$DF,$CF,$BF,$AF,$9F,$8F
543
 .byte $91,$81,$B1,$A1,$D1,$C1,$F1,$E1,$10,$00,$30,$20,$50,$40,$70,$60
544
 .byte $83,$93,$A3,$B3,$C3,$D3,$E3,$F3,$02,$12,$22,$32,$42,$52,$62,$72
545
 .byte $B5,$A5,$95,$85,$F5,$E5,$D5,$C5,$34,$24,$14,$04,$74,$64,$54,$44
546
 .byte $A7,$B7,$87,$97,$E7,$F7,$C7,$D7,$26,$36,$06,$16,$66,$76,$46,$56
547
 .byte $D9,$C9,$F9,$E9,$99,$89,$B9,$A9,$58,$48,$78,$68,$18,$08,$38,$28
548
 .byte $CB,$DB,$EB,$FB,$8B,$9B,$AB,$BB,$4A,$5A,$6A,$7A,$0A,$1A,$2A,$3A
549
 .byte $FD,$ED,$DD,$CD,$BD,$AD,$9D,$8D,$7C,$6C,$5C,$4C,$3C,$2C,$1C,$0C
550
 .byte $EF,$FF,$CF,$DF,$AF,$BF,$8F,$9F,$6E,$7E,$4E,$5E,$2E,$3E,$0E,$1E