10MODE7 20OSWRCH=&FFEE 30OSRDCH=&FFE0 40true=0 50false=&FF 60locals=&70 70esc=locals 80mask=locals+1 90char=locals+2 100byte=locals+3 110hex_only=locals+4 120add_needed=locals+5 130sub_needed=locals+6 140hex_needed=locals+7 150esc_seen=locals+8 160new_esc_next=locals+9 170XonXoff=locals+10 180DIM Code 1024 190FOR Pass = 0 TO 2 STEP 2 200P% = Code 210[OPT Pass 220.put_in_RS423 230 \ Char is in A 240 And mask 250 Sta char 260 Bit hex_only 270 Bmi not_hex_only 280 \ 290 Cmp #ASC("0") 300 Bcc test_control 310 Cmp #ASC("0")+15+1 320 Bcs test_control \ IF '0' <= A <= '0'+15 THEN ... 330 Lda hex_needed 340 Cmp #1 350 Beq last_nibble 360 Cmp #2 370 Beq first_nibble 380 EQUB 0:EQUB 1:EQUS "hex_needed wrong value":EQUB 0 390 .first_nibble 400 Lda char 410 Sec: Sbc #ASC("0") 420 Asl A: Asl A: Asl A: Asl A 430 Sta byte 440 Lda #1 450 Sta hex_needed 460 Jmp ignore 470 .last_nibble 480 Lda char 490 Sec: Sbc #ASC("0") 500 Ora byte 510 Sta char 520 Lda #2 530 Sta hex_needed 540 .end_case_1 550 Jmp over_tests 560 .test_control 570 Lda char 580 Cmp #ASC(" ") 590 Bcs invalid_hex 600 Jmp ignore 610 .invalid_hex 620 Lda #false 630 Sta hex_only 640 .over_tests 650 .not_hex_only 660 Lda char 670 Cmp esc 680 Bne test_esc_seen 690 Bit esc_seen 700 Bmi got_esc 710 Lda #false 720 Sta esc_seen 730 Jmp end_if_esc_seen 740 .got_esc 750 Lda #true 760 Sta esc_seen 770 Jmp ignore 780 .end_if_esc_seen 790 Jmp over_esc_handler 800 .test_esc_seen 810 Bit esc_seen 820 Bpl handle_esc 830 Bit sub_needed 840 Bmi test_add 850 Lda char 860 Sec: Sbc #64 870 Sta char 880 Lda #false 890 Sta sub_needed 900 .test_add 910 Bit add_needed 920 Bmi test_hex_pair 930 Lda char 940 Clc: Adc #128 950 Sta char 960 Lda #false 970 Sta add_needed 980 .test_hex_pair 990 Lda hex_needed 1000 Cmp #2 1010 Bne next_hex_test 1020 Lda char 1030 Sec: Sbc #ASC("0") 1040 Asl A: Asl A: Asl A: Asl A 1050 Sta char 1060 Lda #1 1070 Sta hex_needed 1080 Jmp ignore 1090 .next_hex_test 1100 Cmp #1 1110 Beq got_last_digit 1120 Cmp #0 1130 Beq no_hex_fiddles 1140 EQUB 0:EQUB 2:EQUS "hex_needed value wrong":EQUB 0 1150 .got_last_digit 1160 Lda char 1170 Sec: Sbc #ASC("0") 1180 Ora byte 1190 Sta char 1200 Lda #0 1210 Sta hex_needed 1220 .no_hex_fiddles 1230 .test_new_esc 1240 Bit new_esc_next 1250 Bmi not_new_esc 1260 Lda char 1270 Sta esc 1271 Lda #false 1272 Sta new_esc_next 1280 Jmp ignore 1290 .not_new_esc 1300 Jmp over_final_else 1310 .handle_esc 1320 Lda char 1330 Cmp #ASC("8") 1340 Bne test_7 1350 Lda #&FF: Sta mask 1360 Jmp end_case 1370 .test_7 1380 Cmp #ASC("7") 1390 Bne test_4 1400 Lda #&7F: Sta mask 1410 Jmp end_case 1420 .test_4 1430 Cmp #ASC("4") 1440 Bne test_minus 1450 Lda #true 1460 Sta hex_only 1470 Lda #2 1480 Sta hex_needed 1490 Jmp end_case 1500 .test_minus 1510 Cmp #ASC("-") 1520 Bne test_plus 1530 Lda #true 1540 Sta sub_needed 1550 Jmp end_case 1560 .test_plus 1570 Cmp #ASC("+") 1580 Bne test_and 1590 Lda #true 1600 Sta add_needed 1610 Jmp end_case 1620 .test_and 1630 Cmp #ASC("&") 1640 Bne test_pling 1650 Lda #2 1660 Sta hex_needed 1670 Jmp end_case 1680 .test_pling 1690 Cmp #ASC("!") 1700 Bne test_query 1710 Lda #true 1720 Sta XonXoff 1730 Jmp end_case 1740 .test_query 1750 Cmp #ASC("?") 1760 Bne test_equals 1770 Lda #false 1780 Sta XonXoff 1790 Jmp end_case 1800 .test_equals 1810 Cmp #ASC("=") 1820 Bne default 1830 Lda #true 1840 Sta new_esc_next 1850 Jmp end_case 1860 .default 1870 EQUB 0:EQUB 3:EQUS "Bad protocol after escape":EQUB 0 1880 .end_case 1881 Lda #false 1882 Sta esc_seen 1890 Jmp ignore 1900 .over_final_else 1910 .over_esc_handler 1920\ 1950\ Put char in buffer 1951 1952 1960.ignore 1990\return 1991 1992 2010.init 2020Lda #false: Sta add_needed 2021 Sta sub_needed 2022 Sta hex_only 2023 Sta esc_seen 2024 Sta new_esc_next 2040Lda #ASC("|"): Sta esc 2050Lda #0: Sta hex_needed 2051Lda #&7F: Sta mask 2100] 2110NEXT