
 1000  *SAVE S.CRC GENERATOR (HANSKNECHT)
 1010  *--------------------------------
 1020  BUFFER .EQ $4000
 1030  LIMIT  .EQ $4102
 1040  *--------------------------------
 1050  CRC    .EQ $00,01
 1060  PNTR   .EQ $02,03
 1070  TPTR   .EQ $04,05
 1080  TMASK  .EQ $06
 1090  SPTR   .EQ $07,08
 1100  SMASK  .EQ $09
 1110  *--------------------------------
 1120  PRNTAX .EQ $F941
 1130  CROUT  .EQ $FD8E
 1140  PRBYTE .EQ $FDDA
 1150  COUT   .EQ $FDED
 1160  *--------------------------------
 1170  *      SIMULATE SENDING A BUFFER-FULL
 1180  *--------------------------------
 1190  SEND   JSR SETUP    CLEAR CRC, POINT AT BUFFER
 1200         LDA #0       CLEAR CRC BYTES IN BUFFER
 1210         STA LIMIT-1
 1220         STA LIMIT-2
 1230         JSR CRC.BUFFER    COMPUTE CRC OF 258 BYTES
 1240         LDX CRC      STORE CRC INTO LAST 2 BYTES
 1250         LDA CRC+1
 1260         STX LIMIT-1
 1270         STA LIMIT-2
 1280         JSR PRNTAX   DISPLAY THE CRC
 1290         JMP CROUT    <RETURN> AND RETURN
 1300  *--------------------------------
 1310  *      SIMULATE RECEIVING A BUFFER-FULL
 1320  *--------------------------------
 1330  RECV   JSR SETUP    CLEAR CRC, POINT AT BUFFER
 1340         JSR CRC.BUFFER    COMPUTE CRC OF 258 BYTES
 1350         LDX CRC      DISPLAY CRC IN HEX
 1360         LDA CRC+1
 1370         JSR PRNTAX
 1380         JMP CROUT
 1390  *--------------------------------
 1400  *      TRY "RECEIVING" THE 258 BYTES
 1410  *      WITH A KNOWN SINGLE-BIT ERROR.
 1420  *--------------------------------
 1430  TEST.SINGLE.BIT.ERRORS
 1440         LDA #BUFFER
 1450         STA TPTR     FOR TPTR = BUFFER TO LIMIT
 1460         LDA /BUFFER
 1470         STA TPTR+1
 1480  .1     LDA TPTR+1        PRINT TPTR"-"
 1490         LDX TPTR
 1500         JSR PRNTAX
 1510         LDA #"-"
 1520         JSR COUT
 1530         LDA #$80          FOR TMASK =
 1540         STA TMASK             $80,40,20,10,8,4,2,1
 1550  .2     LDY #0
 1560         LDA (TPTR),Y          INVERT BIT, MAKING ERROR
 1570         EOR TMASK
 1580         STA (TPTR),Y
 1590         JSR SETUP    CLEAR CRC, POINT AT BUFFER
 1600         JSR CRC.BUFFER        COMPUTE CRC
 1610         LDA #" "              PRINT " "CRC
 1620         JSR COUT
 1630         LDA CRC+1
 1640         LDX CRC
 1650         JSR PRNTAX
 1660         LDA (TPTR),Y          FIX ERRONEOUS BIT
 1670         EOR TMASK
 1680         STA (TPTR),Y
 1690         LSR TMASK         NEXT TMASK
 1700         BNE .2            ...MORE
 1710         JSR CROUT         PRINT<CR>
 1720         INC TPTR     NEXT TPTR
 1730         BNE .3
 1740         INC TPTR+1
 1750  .3     LDA TPTR
 1760         CMP #LIMIT
 1770         LDA TPTR+1
 1780         SBC /LIMIT+1
 1790         BCC .1       ...MORE
 1800         RTS
 1810  *--------------------------------
 1820  TEST.DOUBLE.BIT.ERRORS
 1830         LDA #BUFFER
 1840         STA SPTR     FOR SPTR=BUFFER TO LIMIT
 1850         LDA /BUFFER
 1860         STA SPTR+1
 1870  *--------------------------------
 1880  .1     LDA #$80     FOR SMASK=80,40,20,10,8,4,2,1
 1890         STA SMASK
 1900  *--------------------------------
 1910  .2     LDA #BUFFER  FOR TPTR=BUFFER TO LIMIT
 1920         STA TPTR
 1930         LDA /BUFFER
 1940         STA TPTR+1
 1950  *--------------------------------
 1960  .3     LDA #$80     FOR TMASK=80,40,20,10,8,4,2,1
 1970         STA TMASK
 1980  *--------------------------------
 1990  .4     LDY #0
 2000         LDA (TPTR),Y      MAKE FIRST ERROR
 2010         EOR TMASK
 2020         STA (TPTR),Y
 2030         LDA (SPTR),Y      MAKE SECOND ERROR
 2040         EOR SMASK
 2050         STA (SPTR),Y
 2060         JSR SETUP    CLEAR CRC, POINT AT BUFFER
 2070         JSR CRC.BUFFER    COMPUTE CRC
 2080         LDA (SPTR),Y      FIX BOTH ERRORS
 2090         EOR SMASK
 2100         STA (SPTR),Y
 2110         LDA (TPTR),Y
 2120         EOR TMASK
 2130         STA (TPTR),Y
 2140  *--------------------------------
 2150         LDA CRC      IF CRC=0, DISPLAY POINTERS
 2160         ORA CRC+1
 2170         BNE .5       ...CRC .NE. 0, SO CONTINUE
 2180         JSR DISPLAY.POINTERS
 2190  *--------------------------------
 2200  .5     LSR TMASK    NEXT TMASK
 2210         BNE .4       ...MORE
 2220         INC TPTR     NEXT TPTR
 2230         BNE .6
 2240         INC TPTR+1
 2250  .6     LDA TPTR
 2260         CMP #LIMIT
 2270         LDA TPTR+1
 2280         SBC /LIMIT+1
 2290         BCC .3       ...MORE
 2300  *--------------------------------
 2310         LSR SMASK    NEXT SMASK
 2320         BNE .2       ...MORE IN THIS BYTE
 2330         INC SPTR     NEXT SPTR
 2340         BNE .7
 2350         INC SPTR+1
 2360  .7     LDA SPTR
 2370         CMP #LIMIT
 2380         LDA SPTR+1
 2390         SBC /LIMIT+1
 2400         BCC .1       ...MORE
 2410         RTS
 2420  *--------------------------------
 2430  DISPLAY.POINTERS
 2440         LDA TPTR+1   PRINT TPTR"-"TMASK" ";
 2450         LDX TPTR
 2460         JSR PRNTAX
 2470         LDA #"-"
 2480         JSR COUT
 2490         LDA TMASK
 2500         JSR PRBYTE
 2510         LDA #" "
 2520         JSR COUT
 2530         LDA SPTR+1   PRINT SPTR"-"SMASK
 2540         LDX SPTR
 2550         JSR PRNTAX
 2560         LDA #"-"
 2570         JSR COUT
 2580         LDA SMASK
 2590         JSR PRBYTE
 2600         JMP CROUT
 2610  *--------------------------------
 2620  SETUP  LDA #0       CLEAR CRC
 2630         STA CRC
 2640         STA CRC+1
 2650         LDA #BUFFER  SET UP PNTR TO BUFFER
 2660         STA PNTR
 2670         LDA /BUFFER
 2680         STA PNTR+1
 2690         RTS
 2700  *--------------------------------
 2710  *      COMPUTE CRC FROM (PNTR) THRU LIMIT
 2720  *--------------------------------
 2730  CRC.BUFFER
 2740  .1     LDY #0       SCAN THRU THE BUFFER
 2750         LDA (PNTR),Y
 2760         JSR CRC.BYTE
 2770         INC PNTR     NEXT BYTE
 2780         BNE .2
 2790         INC PNTR+1
 2800  .2     LDA PNTR     CHECK LIMIT
 2810         CMP #LIMIT
 2820         LDA PNTR+1
 2830         SBC /LIMIT
 2840         BCC .1       MORE TO GO
 2850         RTS
 2860  *--------------------------------
 2870  *      COMPUTE CRC ON A SINGLE BYTE
 2880  *--------------------------------
 2890  CRC.BYTE
 2900         LDX #8       DO 8 BITS
 2910  .1     ASL          MSB OF BYTE TO CARRY
 2920         ROL CRC
 2930         ROL CRC+1
 2940         BCC .2       --> 0, GET NEXT BIT
 2950         PHA          --> 1, TOGGLE POLYNOMIAL BITS
 2960         LDA CRC
 2970         EOR #$21     TOGGLE BITS 0 AND 5
 2980         STA CRC
 2990         LDA CRC+1
 3000         EOR #$10     TOGGLE BIT 12
 3010         STA CRC+1
 3020         PLA
 3030  .2     DEX          NEXT BIT
 3040         BNE .1
 3050         RTS
 3060  *--------------------------------
 3070  *   FIND WHICH BIT IS BAD IN BUFFER+CRC
 3080  *
 3090  *      RESULT IS BIT POSITION IN MESSAGE,
 3100  *      WHERE THE FIRST BIT OF THE MESSAGE IS BIT 0
 3110  *      AND (IN THIS CASE) THE LAST CRC BIT IS BIT $80F.
 3120  *
 3130  *      ALGORITHM BY BRUCE LOVE, NEW ZEALAND
 3140  *--------------------------------
 3150  BIT.NUMBER .EQ $10,11
 3160  DUMMY.CRC  .EQ $12,13
 3170  *--------------------------------
 3180  FIND.BAD.BIT
 3190         LDA #$80F    TOTAL # BITS - 1
 3200         STA BIT.NUMBER    (WE WILL COUNT BACKWARDS)
 3210         LDA /$80F
 3220         STA BIT.NUMBER+1
 3230         LDA #$0001   STARTING POINT FOR BIT FINDER
 3240         STA DUMMY.CRC
 3250         LDA /$0001
 3260         STA DUMMY.CRC+1
 3270  .1     LDA CRC      COMPARE RECEIVED CRC WITH
 3280         CMP DUMMY.CRC        PROCESSED VALUE;
 3290         BNE .2       IF THEY MATCH, WE HAVE FOUND THE
 3300         LDA CRC+1    BAD BIT.
 3310         CMP DUMMY.CRC+1
 3320         BEQ .4       ...FOUND BAD BIT!
 3330  .2     LDA BIT.NUMBER        DECREMENT BIT COUNTER
 3340         BNE .3
 3350         DEC BIT.NUMBER+1
 3360         BMI .5       WENT TOO FAR
 3370  .3     DEC BIT.NUMBER
 3380         ASL DUMMY.CRC
 3390         ROL DUMMY.CRC+1
 3400         BCC .1
 3410         LDA DUMMY.CRC
 3420         EOR #$21
 3430         STA DUMMY.CRC
 3440         LDA DUMMY.CRC+1
 3450         EOR #$10
 3460         STA DUMMY.CRC+1
 3470         JMP .1
 3480  .4     LDA BIT.NUMBER+1  PRINT THE BIT NUMBER
 3490         JSR PRBYTE        (IF $8000, THE ERROR WAS
 3500         LDA BIT.NUMBER    NOT A SINGLE BIT)
 3510         JSR PRBYTE
 3520         JMP CROUT
 3530  .5     BRK
 3540  *--------------------------------

