-
Notifications
You must be signed in to change notification settings - Fork 2
/
ys.a18
1724 lines (1688 loc) · 34 KB
/
ys.a18
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; -------------------------------------------------------------------
; -------------------------------------------------------------------
; *** ys: send one or more files using YMODEM batch protocol
; ***
; *** Build #
; *** 1: Proof of concept version
; *** 2: First bug squashed: 256 & 512 byte files were sent
; *** as 0 bytes
; *** 3: Added file timestamp to block 0
; *** 4: Inlined some Tx/Rx code to make it fast enough to
; *** catch the ACK & CRC/NAK at the end of a block send
; *** 5: Dec/hex oops in "Couldn't open file:" message
; *** 6: missed a "pop rc" when O_READ didn't read any bytes
; *** 7: moved endrom to end of uninitialised data so that
; *** Elf/OS 0.4.x can tell if we're colliding with the heap.
; *** Make stack manipulation interrupt safe on entry and
; *** exit (even though soft UART code is unlikely to work
; *** if interrupted).
; *** 8: Add UART type detection & auto config. Both hard &
; *** soft UARTs supported.
; *** 9: Expand directory files on cmd line into their component
; *** data files for sending (NOT recursive)
; *******************************************************************
; *** This software is released to the public domain. ***
; *** You have permission to use, modify, copy, and distribute ***
; *** this software as you please. ***
; *******************************************************************
;
; Register usage:
;
; System:
; r0 - Resesrved for DMA
; r1 - Reserved for interrupts
; r2 - Stack pointer
; r3 - Program counter
; r4 - SCALL program counter
; r5 - SRET program counter
; r6 - SCALL return address
; re.0 - SCALL overwrites with D
; re.1 - Baud rate constant
;
; Main program :
;
; ra - Command line pointer (on exec)
;
; All other registers are available for subroutines.
;
include "bios.inc"
include "kernel.inc"
;
debug equ 0
info equ 0
;
; ***************************************************
; ***** Constants *****
; ***************************************************
cr equ 'M'-'@'
lf equ 'J'-'@'
bs equ 'H'-'@'
;
soh equ 'A'-'@' ; 128 byte packet header
stx equ 'B'-'@' ; 1K byte packet header
eot equ 'D'-'@' ; end of file transfer marker
ack equ 'F'-'@' ; Acknowledge (good packet)
nak equ 'U'-'@' ; Negative acknowledge (bad packet)
can equ 'X'-'@' ; cancel transfer
crc equ 'C' ; use CRC rather than checksum
;
; ***************************************************
; ***** This block is the Execution header for *****
; ***** a stand-alone program. It begins 6 *****
; *****bytes before the program start. *****
; ***************************************************
LoadAdr equ 2000h
org LoadAdr-6 ; Header starts at 01ffah
dw LoadAdr
dw EndRom-LoadAdr
dw LoadAdr
;
; ***************************************************
; ***** Code start *****
; ***************************************************
br Start
;
; ***************************************************
; ***** Program header *****
; ***************************************************
date
Build: dw 9 ; build number
db 'expand dirs in cmd tail',0
;
monthDays:
db 31,28,31,30,31,30,31,31,30,31,30,31
;
CmdFilDes:
db 0,0,0,0 ; current file offset
dw CmdDta ; DTA
dw 0 ; eof
db 0 ; flags
CmdDirSec:
db 0,0,0,0 ; dir sector
CmdDirOfs:
dw 0 ; dir offset
db 0,0,0,0 ; current sector in DTA
;
SndFilDes:
db 0,0,0,0 ; current file offset
dw SndDta ; DTA
dw 0 ; eof
db 0 ; flags
SndDirSec:
db 0,0,0,0 ; dir sector
SndDirOfs:
dw 0 ; dir offset
db 0,0,0,0 ; current sector in DTA
;
; ***************************************************
; ***** Main program *****
; ***************************************************
start:
push r6 ; save Elf/OS's return address on its stack
ldi 1 ; assume interrupts are enabled
lsie ; skip if they are
ldi 0 ; mark interrupts disabled
plo re ; save IE flag
ldi 023h ; setup for DIS (X=2, P=3)
str r2
dis ; disable interrupts
dec r2
load rf,saveStack ; save Elf/OS's stack
ghi r2
str rf
inc rf
glo r2
str rf
load r2,localStack ; use our own stack
glo re ; recover IE flag
lbz start2 ; jump if interrupts were disabled
ldi 023h ; setup for RET (X=2, P=3)
str r2
ret ; re-enable interrupts
dec r2
start2:
ghi re ; turn off console echo
ani 11111110b
phi re
call UARTsetup ; auto detect/config UART
;
call NextFileName ; point to first file name
lbnz NextFile ; and go send it
call O_INMSG ; otherwise display usage message
db 'Usage: ys filename [filename...]',cr,lf,0
lbr Exit ; and return to os
NextFile:
call ProcFile ; process next file (either send or expand)
call NextFileName ; point to next file name in cmd tail
lbnz NextFile ; loop for next file
call WaitNAKorCRC
load rf,blknum
ldi 0
str rf
call ZeroBlock0 ; send zero filled block 0 to finish up
if debug=0
load rf,SoftAckedAction
ldi low nextBlock
str rf
load rf,HardAckedAction
ldi high nextBlock
str rf
inc rf
ldi low nextBlock
str rf
endi
load rf,block0
load rc,128
ldi 10
plo r8
call SendBlock
lbr Exit ; and return to os
;
; ***************************************************
; ***** NextFilename: advance RA to the next *****
; ***** non space character *****
; ***************************************************
NextFileName:
lda ra ; move past any spaces
smi ' '
lbz NextFileName
dec ra ; move back to non-space character
ldn ra ; get byte
retn
;
; ***************************************************
; ***** Exit: return to Elf/OS, restoring OS *****
; ***** stack and R6 contents *****
; ***************************************************
Exit:
ghi re ; restore console echo
ori 00000001b
phi re
ldi 1 ; assume interrupts are enabled
lsie ; skip if they are
ldi 0 ; mark interrupts disabled
plo re ; save IE flag
ldi 023h ; setup for DIS (X=2, P=3)
str r2
dis ; disable interrupts
dec r2
load rf,saveStack ; restore Elf/OS's stack
lda rf
phi r2
ldn rf
plo r2
glo re ; recover IE flag
lbz Exit2 ; jump if interrupts were disabled
ldi 023h ; setup for RET (X=2, P=3)
str r2
ret ; re-enable interrupts
dec r2
Exit2:
pop r6 ; restore Elf/OS's return address
retn ; return to Elf/OS
;
; ***************************************************
; ***** Process file: if a directory file, *****
; ***** send each ordinary file contained in it *****
; ***** If an ordinary file, just send it *****
; ***** RA: points to file name *****
; ***************************************************
ProcFile:
load rd,cmdFilename ; copy filename from cmd tail
load rc,sndFilename
ProcCopyName:
lda ra ; look for first less <= space
str rd
inc rd
str rc
inc rc
smi ' '+1
lbdf ProcCopyName
dec ra
dec rd
ldi 0 ; zero terminate filename
str rd
inc rd
dec rc
str rc
push rd ; save block ptr
load rd,CmdFilDes ; get file descriptor
load rf,cmdFilename
ldi 0 ; flags for open, append
plo r7
call O_OPEN ; attempt to open file to determine size
lbnf CmdFileOpened ; jump if file was opened
call Cancel
call O_INMSG
db 'Couldn',27h,'t open file: ',0
load rf,cmdFilename
call O_MSG ; display it
call crlf
lbr Exit ; and return to os
CmdFileOpened:
load rd,CmdFilDes
;
push rf
ldi 0e0h ; lba mode
phi r8
load rf,CmdDirSec+1 ; point to dir sector in CmdFilDes
lda rf ; retrieve sector
plo r8
lda rf
phi r7
lda rf
plo r7
load rf,DirBuffer ; where to load sector
call f_ideread ; call bios to read the sector
;
load rf,CmdDirOfs+1 ; need dirent offset
ldn rf
adi 6 ; point to flags
plo rd
dec rf
ldn rf
adci 0 ; propagate carry
phi rd ; r7 now points to flags
glo rd ; now point to correct spot in sector buffer
adi low DirBuffer
plo rd
ghi rd
adci high Dirbuffer
phi rd
;
ldn rd ; get flags
pushd
ani ff_dir
bnz isDirFile
;
load rd,CmdFilDes
call O_CLOSE
push ra ; save cmd tail ptr
load ra,cmdFilename
call SendFile ; ordinary file, just send it
pop ra
retn
isDirFile:
load rc,32 ; read next directory entry
load rf,dirEntBuffer
load rd,CmdFilDes
call O_READ
glo rc
lbz eofdir ; no more
load rf,dirEntBuffer
lda rf ; see if it has a non zero sector
lbnz occupied ; (indicates in-use file)
lda rf
lbnz occupied
lda rf
lbnz occupied
lda rf
lbz isDirFile
occupied:
load rf,dirEntBuffer+6
ldn rf ; make sure it's not a sub directory
ani ff_dir
lbnz isDirFile
push ra ; save cmd tail ptr
load rf,cmdFilename
load ra,sndFilename
copyDir:
lda rf
str ra
inc ra
bnz copyDir
dec ra ; point back to terminator
dec ra ; point back to last char of dir
ldn ra
smi '/'
bz hasTrailingSlash
inc ra
ldi '/'
str ra
hasTrailingSlash:
inc ra
load rf,dirEntBuffer+12
copyFileName:
lda rf
str ra
inc ra
bnz copyFileName
load ra,sndFilename
call SendFile ; send file from directory entry
pop ra ; restore cmd tail ptr
lbr isDirFile
eofdir:
load rd,CmdFilDes
call O_CLOSE
retn
;
; ***************************************************
; ***** Send file: send a single file *****
; ***** RA: points to file name *****
; ***************************************************
SendFile:
call ZeroBlock0
load rd,block0 ; copy filename to block
ldn ra ; don't allow leading /
smi '/'
bnz B0loop1 ; jump if not an absolute path
inc ra ; make it a relative path
B0loop1:
lda ra ; look for first less <= space
str rd
inc rd
smi ' '+1
lbdf B0loop1
dec ra
dec rd
inc rd ; skip over 0 terminator
push rd ; save block ptr
load rd,SndFilDes ; get file descriptor
load rf,sndFilename
ldi O_APPND ; flags for open, append
plo r7
call O_OPEN ; attempt to open file to determine size
lbnf SndFileOpened ; jump if file was opened
call Cancel
call O_INMSG
db 'Couldn',27h,'t open file: ',0
load rf,sndFilename
call O_MSG ; display it
call crlf
lbr Exit ; and return to os
SndFileOpened:
load rd,SndFilDes
load rf,fileSize
lda rd ; put 32 bit offset into
phi r7 ; R7:R8 and fileSize
str rf
inc rf
;
lda rd
plo r7
str rf
inc rf
;
lda rd
phi r8
str rf
inc rf
;
ldn rd
plo r8
str rf
;
dec rd ; restore descriptor
dec rd
dec rd
;
push r7
push r8
;
load r8,0
load r7,0
load rd,SndFilDes
load rc,0 ; rewind file to start
call O_SEEK
;
pop r8
pop r7
pop rf ; restore block ptr
call uint32_out ; convert number to ascii
ldi ' ' ; finish file size with space
str rf
inc rf
;
push ra
push rf
ldi 0e0h ; lba mode
phi r8
load rf,SndDirSec+1 ; point to dir sector in SndFilDes
lda rf ; retrieve sector
plo r8
lda rf
phi r7
lda rf
plo r7
load rf,DirBuffer ; where to load sector
call f_ideread ; call bios to read the sector
;
load rf,SndDirOfs+1 ; need dirent offset
ldn rf
adi 7 ; point to date block
plo ra
dec rf
ldn rf
adci 0 ; propagate carry
phi ra ; r7 now points to date block
glo ra ; now point to correct spot in sector buffer
adi low DirBuffer
plo ra
ghi ra
adci high DirBuffer
phi ra
;
load rf,year
lda ra ; get year/month
shr ; shift high month bit into DF
ldn ra ; get low bits of month
shrc ; shift high bit in
shr ; then shift into position
shr
shr
shr
inc rf
str rf ; store month
inc rf ; point to day storage
ldn ra ; recover day
ani 31 ; mask for day
str rf ; store day
dec rf
dec rf ; point back to year offset storage
dec ra ; point back to year
lda ra ; get year
shr ; shift out high bit of month
str rf ; store year offset from 1972
inc rf
inc rf
inc rf ; point to hours storage
inc ra ; point to time
ldn ra ; retrieve hours
shr ; shift to proper position
shr
shr
str rf ; store hours
inc rf ; point to minutes storage
lda ra ; get minutes
ani 07h ; strip out hours
shl ; shift to needed spot
shl
shl
str r2 ; save for combination
ldn ra ; get low bits of minutes
shr ; shift into position
shr
shr
shr
shr
or ; combine with high bites
str rf ; store minutes
inc rf ; point to seconds storage
ldn ra ; get seconds
ani 1fh ; strip minutes out
shl ; multiply by 2
str rf ; store seconds
;
load rf,year ; point back to year
load r7,03c2h ; number of seconds
load r8,6700h ; from Jan 1 1970 to Jan 1 1972
ldn rf ; year offset from 1972
plo r9
ldi 0
phi r9 ; leap year counter
yearLoop:
glo r9
bz yearsDone
load ra,01e1h ; seconds in a year
load rb,3380h
call add32bits
ghi r9
ani 00000011b
bnz notLeapYear
load ra,0001h ; seconds in the leap day
load rb,5180h ; (86400)
call add32bits
notLeapYear:
ghi r9
dec r9
adi 1
phi r9
lbr yearLoop
yearsDone:
load ra,0001h ; seconds in a day
load rb,5180h ; (86400)
lda rf ; re-get year
ani 00000011b ; rc.1 will be 0 if leap year
phi rc ;
lda rf ; month # (1-12)
smi 1
lbz monthsDone
plo rc ; rc.0 is month # -1
load rd,monthDays
monthsLoop:
lda rd
plo r9 ; rb.0 is # days in month
monthLoop:
call add32bits ; add a day's worth of seconds
dec r9 ; decrement days left in month
glo r9
lbnz monthLoop
ghi rc ; leap year?
bnz noLeapYearChk
glo rd ; test for February
smi low monthDays+2
bnz notFebruary
call add32bits ; add in another day in Feb in leap year
noLeapYearChk:
notFebruary:
glo rc
smi 1
plo rc
lbnz monthsLoop
monthsDone:
lda rf ; day of month
smi 1
lbz domDone
plo r9
domLoop:
call add32bits
dec r9
glo r9
lbnz domLoop
domDone:
load ra,0
load rb,3600 ; seconds in an hour
lda rf
bz hoursDone
plo r9
hoursLoop:
call add32bits
dec r9
glo r9
bnz hoursLoop
hoursDone:
load rb,60 ; seconds in a minute
lda rf
lbz minutesDone
plo r9
minutesLoop:
call add32bits
dec r9
glo r9
lbnz minutesLoop
minutesDone:
ldn rf ; get seconds
plo rb
call add32bits
;
pop rf ; restore block 0 ptr
ldi '0' shr 2 ; convert r7:r8 to octal number
phi r9 ; and add to block 0 string
ldi 2 ; 2 bits on first digit
plo r9
ldi 11 ; 11 digits in a 32 bit number
plo ra
bitLoop:
glo r8
shl
plo r8
ghi r8
shlc
phi r8
glo r7
shlc
plo r7
ghi r7
shlc
phi r7
ghi r9
shlc
dec r9
phi r9
glo r9
lbnz bitLoop
ghi r9
str rf
inc rf
ldi '0' shr 3 ; next octal digit setup
phi r9
ldi 3 ; 3 bits for remaining digits
plo r9
dec ra
glo ra
lbnz bitLoop
;
pop ra
;
ldi ' ' ; finish timestamp with space
str rf
inc rf
ldi '0' ; default file mode to '0'
str rf ; no trailing nul needed: already 0'd
;
call WaitNAKorCRC
smi crc
lbz CrcMode
smi nak-crc
lbz SaveMode
SendFileError:
load rd,SndFilDes
call O_CLOSE
smi 0 ; DF=1, error
retn
CrcMode:
ldi 1
SaveMode:
plo r7
load rf,blkMode
glo r7
str rf
;
if debug=0
load rf,SoftAckedAction
ldi low SoftReadne2
str rf
load rf,HardAckedAction
ldi high HardAcked
str rf
inc rf
ldi low HardAcked
str rf
endi
load rf,blkNum
ldi 0
str rf
load rf,block0
load rc,128
ldi 10
plo r8
call SendBlock ; send file block 0
lbdf SendFileError
;
if debug
pushd
call crlf
load rf,hexcount
ldi 0
str rf
popd
endif
;
smi crc
bz fileSendLoop
smi nak-crc
bnz SendFileError
;
FileSendLoop:
load rf,fileSize
load rc,1024 ; assume we've got at least 1K
lda rf ; left to send
lbnz MoreToGo
lda rf
lbnz MoreToGo
ldn rf
smi high 1024
lbdf MoreToGo ; jump if >= 1K left to go
NearingTheEnd:
load rc,128 ; switch to 128 byte sectors
lda rf
lbnz MoreToGo
ldn rf
lbz FileDone
MoreToGo:
push rc
load rd,SndFilDes
load rf,blockBuffer
call O_READ
glo rc
lbnz ReadBytes
ghi rc
lbz FileDonePop
ReadBytes:
load rf,fileSize+3 ; LSB
;
glo rc ; decrement fileSize by
str r2 ; # bytes read
ldn rf
sm
str rf
;
dec rf
ghi rc
str r2
ldn rf
smb
str rf
;
dec rf
ldn rf
sdbi 0
str rf
;
dec rf
ldn rf
sdbi 0
str rf
;
pop rc ; retrieve block size
if debug=0
load rf,SoftAckedAction
ldi low nextBlock
str rf
load rf,HardAckedAction
ldi high nextBlock
str rf
inc rf
ldi low nextBlock
str rf
endi
load rf,blockBuffer
ldi 10
plo r8
call SendBlock
lbdf SendFileError
smi ack
bz BlockACKed
call Cancel
lbr SendFileError
BlockACKed:
lbr fileSendLoop
FileDonePop:
pop rc ; discard block size
FileDone:
call SendEOT ; signal file done and get response
smi ack ; ACK'd?
lbnz SendFileError
load rd,SndFilDes
call O_CLOSE
adi 0 ; DF=0, no error
retn ; all done!
;
; ***************************************************
; ***** ZeroBlock0: zeroes block 0 *****
; ***************************************************
ZeroBlock0:
load rf,block0
ZeroLoop:
ldi 0
str rf
inc rf
glo rf
smi low (block0+128)
lbnz ZeroLoop
retn
;
; ***************************************************
; ***** Cancel: send 8 CANs followed by 8 BSs *****
; ***************************************************
Cancel:
push r7
ldi 8
plo r7
cancelLoop:
ldi can
call TTYout ; send CAN (^X) 8 times
dec r7
glo r7
lbnz cancelLoop
ldi 8
plo r7
bsLoop:
ldi bs ; send BS (^H) 8 times
call TTYout
dec r7
glo r7
lbnz bsLoop
pop r7
retn
;
; ***************************************************
; ***** WaitNAKorCRC: wait for CRC or NAK char *****
; ***************************************************
WaitNAKorCRC:
if debug
ldi crc
else
call f_read
plo re
smi crc
bz GotNAKorCRC
smi nak-crc
bnz WaitNAKorCRC
GotNAKorCRC:
glo re
endi
retn
;
; ***************************************************
; ***** GetACK: wait for ACK/NAK or CAN char *****
; ***************************************************
GetACK:
if debug
ldi ack
else
call f_read ; read response to block sent
endi
plo re
smi ack
lbz GotACK ; ACK'd, all good
smi nak-ack
lbz DidntGetACK ; NAK'd, probably have to resend
smi can-nak
lbz DidntGetACK ; CAN'd, terminating transfer
lbr GetACK
GotACK:
glo re
adi 0 ; DF = 0, ACKed
retn
DidntGetACK:
glo re
smi 0 ; DF = 1, not ACKed
retn
;
; ***************************************************
; ***** SendEOT: send EOT, wait for ACK *****
; ***************************************************
SendEOT:
ldi eot
call TTYout ; send EOT (^D)
if debug
call crlf
push rf
load rf,hexcount
ldi 0
str rf
pop rf
else
call f_read
smi ack ; wait for ACK (^F)
lbnz SendEOT
endi
ldi ack
retn
;
; *******************************************
; ***** Send file block *****
; ***** RF - pointer to block *****
; ***** RC - Block length *****
; ***** R8 - # retries *****
; *******************************************
ResendBlock:
pop rf
pop rc
SendBlock:
push rc
push rf
ghi rc ; 1K or 128b block?
lbz Send128
ldi stx ; 1K
lbr SendStart
Send128:
ldi soh ; 128b
SendStart:
call TTYout ; SOH/STK
load rd,blkNum
ldn rd
call TTYout ; block #
ldn rd
sdi 255
call TTYout ; NOT block #
;
ldi high crcTableLow
phi r9
ldi high crcTableHigh
phi rb
;
load r7,0
load rd,blkMode
ldn rd ; 0=checksum, 1=CRC
lbz SendWithChecksum
;
SendWithCRC:
ldn rf
call TTYout ; send data byte
lda rf ; retrieve again and advance ptr
;
str r2
ghi r7 ; j = (crc >> 8) ^ byte
xor
plo r9
plo rb
;
glo r7 ; crc = (crc << 8) ^ table[j]
sex rb
xor
sex r2
phi r7
ldn r9
plo r7
;
dec rc ; loop while data bytes
ghi rc ; left to send
lbnz SendWithCRC
glo rc
lbnz SendWithCRC
;
ghi r7 ; send hi byte of CRC
call TTYout
HardUARTlbr1 equ $+1
lbr SoftUARTlastChar
;
SendWithChecksum:
lda rf
str r2
glo r7
add
plo r7
ldn r2
call TTYout ; send data byte
dec rc
ghi rc
lbnz SendWithChecksum
glo rc
lbnz SendWithChecksum
HardUARTlbr2 equ $+1
lbr SoftUARTlastChar
;
; With a physical UART, we've got no worries about missing the ACK
; after sending the last character of the block, even after block 0
; when we have to handle 2 characters (the ACK/NAK then the CRC/NAK
; character).
;
HardUARTlastChar:
glo r7
call TTYout ; send checksum or low byte of CRC
CALL GetACK
HardAckedAction equ $+1
lbnf HardAcked ; block ACK'd
smi nak
lbnz SendBlockError
dec r8 ; block NAK'd
glo r8 ; resend if any retries left
lbnz ResendBlock
lbr SendBlockError
HardAcked:
call WaitNAKorCRC
lbr nextBlock
;
page
;