-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet.s
1420 lines (1248 loc) · 23.3 KB
/
telnet.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
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
; Telnet client v1.0.1 beta example for M4 Board
; Written by Duke 2018
; Requires firmware v1.1.0 upwards
; Assembles with RASM (www.roudoudou.com/rasm)
; Formatted for Notepad++, see :
; http://www.cpcwiki.eu/forum/amstrad-cpc-hardware/amstrad-cpc-wifi/msg150664/#msg150664
; to easily cross compile and test quick on real CPC
org 0x1000
nolist
DATAPORT equ 0xFE00
ACKPORT equ 0xFC00
; m4 commands used
C_NETSOCKET equ 0x4331
C_NETCONNECT equ 0x4332
C_NETCLOSE equ 0x4333
C_NETSEND equ 0x4334
C_NETRECV equ 0x4335
C_NETHOSTIP equ 0x4336
; firmware functions used
km_read_char equ 0xBB09
km_wait_key equ 0xBB18
txt_output equ 0xBB5A
txt_set_column equ 0xBB6F
txt_set_row equ 0xBB72
txt_set_cursor equ 0xBB75
txt_get_cursor equ 0xBB78
txt_cur_on equ 0xBB81
scr_reset equ 0xBC0E
scr_set_ink equ 0xBC32
scr_set_border equ 0xBC38
mc_wait_flyback equ 0xBD19
kl_rom_select equ 0xb90f
; telnet negotiation codes
DO equ 0xfd
WONT equ 0xfc
WILL equ 0xfb
DONT equ 0xfe
CMD equ 0xff
CMD_ECHO equ 1
CMD_WINDOW_SIZE equ 31
start: ld a,2
call scr_reset ; set mode 2
xor a
ld b,a
call scr_set_border
xor a
ld b,0
ld c,0
call scr_set_ink
ld a,1
ld b,26
ld c,26
call scr_set_ink
ld h,20
ld l,1
call txt_set_cursor
ld hl,msgtitle
call disptextz
ld h,20
ld l,2
call txt_set_cursor
ld hl,msgtitle2
call disptextz
call crlf
; find rom M4 rom number
ld a,(m4_rom_num)
cp 0xFF
call z,find_m4_rom
cp 0xFF
jr nz, found_m4
ld hl,msgnom4
call disptextz
jp exit
found_m4: ld hl,msgfoundm4
call disptextz
ld hl,(0xFF00) ; get version
ld a,h
call print_lownib
ld a,0x2E
call txt_output
ld a,l
rr a
rr a
rr a
rr a
call print_lownib
ld a,0x2E
call txt_output
ld a,l
call print_lownib
; compare version
ld de,0x110 ; v1.1.0 lowest version required
ld a,h
xor d
jp m,cmpgte2
sbc hl,de
jr nc,cmpgte3
cmpgte1: ld hl,msgverfail
call disptextz
jp exit
cmpgte2: bit 7,d
jr z,cmpgte1
cmpgte3: ld hl,msgok
call disptextz
; ask for server / ip
loop_ip:
ld hl,msgserverip
call disptextz
call get_server
cp 0
jr nz, loop_ip
ld hl,msgconnecting
call disptextz
ld hl,ip_addr
call disp_ip
ld hl,msgport
call disptextz
ld hl,(port)
call disp_port
call crlf
call telnet_session
jr loop_ip
exit:
jp km_wait_key
print_lownib:
and 0xF ; keep lower nibble
add 48 ; 0 + x = neric ascii
jp txt_output
get_server:
ld hl,buf
call get_textinput
;cp 0xFC ; ESC?
;ret z
xor a
cp c
jr z, get_server
; check if any none neric chars
ld b,c
ld hl,buf
check_neric:
ld a,(hl)
cp 59 ; bigger than ':' ?
jr nc,dolookup
inc hl
djnz check_neric
jp convert_ip
; make dns lookup
dolookup:
; copy name to packet
ld hl,buf
ld de,lookup_name
ld b,0
copydns: ld a,(hl)
cp 58
jr z,copydns_done
cp 0
jr z,copydns_done
ld a,b
ldi
inc a
ld b,a
jr copydns
copydns_done:
push hl
xor a
ld (de),a ; terminate with zero
ld hl,cmdlookup
inc b
inc b
inc b
ld (hl),b ; set size
; disp servername
ld hl,msgresolve
call disptextz
ld hl,lookup_name
call disptextz
; do the lookup
call dnslookup
pop hl
cp 0
jr z, lookup_ok
ld hl,msgfail
call disptextz
ld a,1
ret
lookup_ok: push hl ; contains port "offset"
ld hl,msgok
call disptextz
; copy IP from socket 0 info
ld hl,(0xFF06)
ld de,4
add hl,de
ld de,ip_addr
ldi
ldi
ldi
ldi
pop hl
jr check_port
; convert ascii IP to binary, no checking for non decimal chars format must be x.x.x.x
convert_ip:
ld hl,buf
call ascii2dec
ld (ip_addr+3),a
call ascii2dec
ld (ip_addr+2),a
call ascii2dec
ld (ip_addr+1),a
call ascii2dec
ld (ip_addr),a
dec hl
check_port: ld a,(hl)
cp 0x3A ; any ':' for port number ?
jr nz, no_port
push hl
pop ix
call port2dec
jr got_port
no_port: ld hl,23
got_port:
ld (port),hl
xor a
ret
dnslookup: ld hl,(0xFF02) ; get response buffer address
push hl
pop iy
ld hl,(0xFF06) ; get sock info
push hl
pop ix ; ix ptr to current socket status
ld hl,cmdlookup
call sendcmd
ld a,(iy+3)
cp 1
jr z,wait_lookup
ld a,1
ret
wait_lookup:
ld a,(ix+0)
cp 5 ; ip lookup in progress
jr z, wait_lookup
ret
; actual telnet session
; M4 rom should be mapped as upper rom.
telnet_session:
ld hl,(0xFF02) ; get response buffer address
push hl
pop iy
; get a socket
ld hl,cmdsocket
call sendcmd
ld a,(iy+3)
cp 255
ret z
; store socket in predefined packets
ld (csocket),a
ld (clsocket),a
ld (rsocket),a
ld (sendsock),a
; multiply by 16 and add to socket status buffer
sla a
sla a
sla a
sla a
ld hl,(0xFF06) ; get sock info
ld e,a
ld d,0
add hl,de ; sockinfo + (socket*4)
push hl
pop ix ; ix ptr to current socket status
; connect to server
ld hl,cmdconnect
call sendcmd
ld a,(iy+3)
cp 255
jp z,exit_close
wait_connect:
ld a,(ix) ; get socket status (0 ==IDLE (OK), 1 == connect in progress, 2 == send in progress)
cp 1 ; connect in progress?
jr z,wait_connect
cp 0
jr z,connect_ok
call disp_error
jp exit_close
connect_ok: ld hl,msgconnect
call disptextz
mainloop: ld bc,1
call recv_noblock
call km_read_char
jr nc,mainloop
cp 0xFC ; ESC?
jp z, exit_close
cp 0x9 ; TAB?
jr nz, no_pause
wait_no_tab:
call km_read_char
cp 0x9
jr z, wait_no_tab
pause_loop:
call km_read_char
cp 0xFC ; ESC?
jp z, exit_close
cp 0x9 ; TAB again to leave
jr nz, pause_loop
jr mainloop
no_pause:
ld hl,sendtext
ld (hl),a
wait_send: ld a,(ix)
cp 2 ; send in progress?
jr z,wait_send
cp 0
call nz,disp_error
;xor a
;ld (isEscapeCode),a
ld a,(hl)
;call txt_output
cp 0xD
jr nz, plain_text
inc hl
ld a,0xA
;call txt_output
ld (hl),a
ld a,7
ld (cmdsend),a
ld a,2
ld (sendsize),a
ld hl,cmdsend
call sendcmd
jp mainloop
plain_text:
ld a,6
ld (cmdsend),a
ld a,1
ld (sendsize),a
ld hl,cmdsend
call sendcmd
jp mainloop
; call when CMD (0xFF) detected, read next two bytes of command
; IY = socket structure ptr
negotiate:
ld bc,2
call recv
cp 0xFF
jp z, exit_close
cp 3
jp z, exit_close
xor a
cp c
jr nz, check_negotiate
cp b
jr z,negotiate ; keep looping, want a reply. Could do other stuff here!
check_negotiate:
ld a,(iy+6)
cp 0xFD ; DO
jr nz, will_not
ld a,(iy+7)
cp CMD_WINDOW_SIZE
jr nz, will_not
; negotiate window size
ld a,8
ld (cmdsend),a
ld hl,sendsize
ld (hl),3
inc hl
ld (hl),0
inc hl
ld (hl),0xFF ; CMD
inc hl
ld (hl),0xFB ; WILL
inc hl
ld (hl),CMD_WINDOW_SIZE
ld hl, cmdsend
call sendcmd
ld a,14
ld (cmdsend),a
ld hl,sendsize
ld (hl),9
inc hl
ld (hl),0
inc hl
ld (hl),0xFF ; CMD
inc hl
ld (hl),0xFA ; SB sub negotiation
inc hl
ld (hl),CMD_WINDOW_SIZE
inc hl
ld (hl),0
inc hl
ld (hl),80
inc hl
ld (hl),0
inc hl
ld (hl),24
inc hl
ld (hl),255
inc hl
ld (hl),240 ; End of subnegotiation parameters.
_wait_send: ld a,(ix)
cp 2 ; send in progress?
jr z,_wait_send
cp 0
call nz,disp_error
ld hl, cmdsend
call sendcmd
ret
will_not:
ld a,(iy+6)
cp 0xFD ; DO
jr nz, not_do
ld a,0xFC ; WONT
jr next_telcmd
not_do: cp 0xFC ; WILL
jr nz, next_telcmd
ld a,0xFD ; DO
next_telcmd:
ld hl,sendsize
ld (hl),3
inc hl
ld (hl),0
inc hl
ld (hl),0xFF ; CMD
inc hl
ld (hl),a ;
inc hl
ld a,(iy+7)
ld (hl),a ;
ld a,8
ld (cmdsend),a
ld hl, cmdsend
call sendcmd
ret
recv_noblock:
push af
push bc
push de
push hl
;ld bc,2048 - to do empty entire receive buffer and use index
ld bc,1
call recv
cp 0xFF
jp z, exit_close
cp 3
jp z, exit_close
xor a
cp c
jr nz, got_msg2
cp b
jr nz, got_msg2
pop hl
pop de
pop bc
pop af
ret
got_msg2:
; disp received msg
push iy
pop hl
ld de,0x6
add hl,de ; received text pointer
ld a,(hl)
cp CMD
jr nz,not_tel_cmd
call negotiate
jp recvdone
not_tel_cmd:
ld b,a
cp 0x1B ; escape code sequence?
jr nz, notescapeCode
ld (isEscapeCode),a
xor a
ld (EscapeCount),a
jp recvdone
notescapeCode:
ld a,(isEscapeCode)
cp 0
jp z, not_in_escmode
ld hl, EscapeBuf
ld a, (EscapeCount)
inc a
ld e,a
ld d,0
add hl,de
ld (EscapeCount),a
cp 1
jp z, skip_check_esc_code ; we only want 0x1B,'[' ... for now
ld a,(hl)
cp 'A' ; cursor up
jr nz, not_A
ld b,11 ; VT
ld a, (EscapeCount)
cp 2
jp z,do_control_code
call escape_val
ld b,a
call txt_get_cursor ; H = col, L = line
ld a,l
sub b ; new line (should do <0 etc checks, gah)
call txt_set_row
jp isok2
not_A:
cp 'B' ; cursor down
jr nz, not_B:
ld a, (EscapeCount)
ld b,10 ; LF
cp 2
jp z,do_control_code
call escape_val
ld b,a
call txt_get_cursor ; H = col, L = line
ld a,l
add b ; new line
call txt_set_row
jp isok2
jp do_control_code
not_B:
cp 'C' ; cursor forward
jr nz, not_C
ld a, (EscapeCount)
ld b,9 ; TAB
cp 2
jp z,do_control_code
call escape_val
ld b,a
call txt_get_cursor ; H = col, L = line
ld a,h
add b ; new column, should check
call txt_set_column
jp isok2
not_C:
cp 'D' ; cursor backwards
jr nz, not_D
ld a, (EscapeCount)
ld b,8 ; BS
cp 2
jp z,do_control_code
call escape_val
ld b,a
call txt_get_cursor ; H = col, L = line
ld a,h
sub b ; new column, should check
call txt_set_column
jp isok2
not_D: cp 'E' ; cursor next line
jr nz, not_E
ld a, (EscapeCount)
call escape_val
ld b,1
cp 0
jr z, default_1line
ld b,a
default_1line:
call txt_get_cursor ; H = col, L = line
add l
ld l,a
ld h,0
call txt_set_cursor
jp isok2
not_E: cp 'F'
jr nz, not_F
call escape_val
ld b,1
cp 0
jr z, default_1line2
ld b,a
default_1line2:
call txt_get_cursor ; H = col, L = line
ld a,l
sub b
ld l,a
ld h,0
call txt_set_cursor
jp isok2
not_F: cp 'G'
jr nz, not_G
call escape_val
ld b,1
cp 0
jr z, default_1col
ld b,a
default_1col:
call txt_get_cursor ; H = col, L = line
ld a,h
sub b
ld h,a
call txt_set_cursor
jp isok2
not_G:
cp 's'
jr nz, not_s
call txt_get_cursor
ld (curPos),hl
jp isok2
not_s:
cp 'u'
jr nz, not_u
ld hl,(curPos)
call txt_set_cursor
jp isok2
not_u:
skip_check_esc_code:
; filter out unsused sequences
; upper case
cp 0x41
jr c, recvdone ; less than
cp 0x5A
jr c, isok2
; check lower case
cp 0x61
jr c, recvdone ; less than
cp 0x7A
jr nc, recvdone
isok2:
xor a
ld (isEscapeCode),a
jr recvdone
do_control_code:
xor a
ld (isEscapeCode),a
not_in_escmode:
ld a,b
call txt_output
recvdone:
pop hl
pop de
pop bc
pop af
ret
exit_close:
call disp_error
ld hl,cmdclose
call sendcmd
jp loop_ip
ret
; recv tcp data
; in
; bc = receive size
; out
; a = receive status
; bc = received size
recv: ; connection still active
ld a,(ix) ;
cp 3 ; socket status (3 == remote closed connection)
ret z
; check if anything in buffer ?
ld a,(ix+2)
cp 0
jr nz,recv_cont
ld a,(ix+3)
cp 0
jr nz,recv_cont
ld bc,0
ld a,1
ret
recv_cont:
; set receive size
ld a,c
ld (rsize),a
ld a,b
ld (rsize+1),a
ld hl,cmdrecv
call sendcmd
ld a,(iy+3)
cp 0 ; all good ?
jr z,recv_ok
push af
call disp_error
pop af
ld bc,0
ret
recv_ok:
ld c,(iy+4)
ld b,(iy+5)
ret
;
; Find M4 ROM location
;
find_m4_rom:
ld iy,m4_rom_name ; rom identification line
ld d,127 ; start looking for from (counting downwards)
romloop: push de
ld c,d
call kl_rom_select ; system/interrupt friendly
ld a,(0xC000)
cp 1
jr nz, not_this_rom
ld hl,(0xC004) ; get rsxcommand_table
push iy
pop de
cmp_loop:
ld a,(de)
xor (hl) ; hl points at rom name
jr z, match_char
not_this_rom:
pop de
dec d
jr nz, romloop
ld a,255 ; not found!
ret
match_char:
ld a,(de)
inc hl
inc de
and 0x80
jr z,cmp_loop
; rom found, store the rom number
pop de ; rom number
ld a,d
ld (m4_rom_num),a
ret
;
; Send command to M4
; HL = packet to send
;
sendcmd:
ld bc,0xFE00
ld d,(hl)
inc d
sendloop: inc b
outi
dec d
jr nz,sendloop
ld bc,0xFC00
out (c),c
ret
; display text
; HL = text
; BC = length
disptext: xor a
cp c
jr nz, not_dispend
cp b
ret z
not_dispend:
ld a,(hl)
push bc
call txt_output
pop bc
inc hl
dec bc
jr disptext
; display text zero terminated
; HL = text
disptextz: ld a,(hl)
or a
ret z
call txt_output
inc hl
jr disptextz
;
; Display error code in ascii (hex)
;
; a = error code
disp_error:
cp 3
jr nz, not_rc3
ld hl,msgconnclosed
jp disptextz
not_rc3: cp 0xFC
jr nz,notuser
ld hl,msguserabort
jp disptextz
notuser:
push af
ld hl,msgsenderror
ld bc,9
call disptext
pop bc
ld a,b
srl a
srl a
srl a
srl a
add a,0x90
daa
adc a,0x40
daa
call txt_output
ld a,b
and 0x0f
add a,0x90
daa
adc a,0x40
daa
call txt_output
ld a,10
call txt_output
ld a,13
call txt_output
ret
disphex: ld b,a
srl a
srl a
srl a
srl a
add a,0x90
daa
adc a,0x40
daa
call txt_output
ld a,b
and 0x0f
add a,0x90
daa
adc a,0x40
daa
call txt_output
ld a,32
call txt_output
ret
;
; Get input text line.
;
; in
; hl = dest buf
; return
; bc = out size
get_textinput:
ld bc,0
call txt_cur_on
inputloop:
re: call mc_wait_flyback
call km_read_char
jr nc,re
cp 0x7F
jr nz, not_delkey
ld a,c
cp 0
jr z, inputloop
push hl
push bc
call txt_get_cursor
dec h
push hl
call txt_set_cursor
ld a,32
call txt_output
pop hl
call txt_set_cursor
pop bc
pop hl
dec hl
dec bc
jr inputloop
not_delkey:
cp 13
jr z, terminate
cp 0xFC
ret z
cp 32
jr c, inputloop
cp 0x7e
jr nc, inputloop
ld (hl),a
inc hl
inc bc
push hl
push bc
call txt_output
call txt_get_cursor
;push hl
;ld a,32
;call txt_output
;pop hl
call txt_set_cursor
pop bc
pop hl