
 1000  *SAVE BETTER.DIV.7
 1010  *--------------------------------
 1020  VERSION    .EQ 1
 1030  RBSC65802  .EQ 1
 1040  HACKNEY    .EQ 2
 1050  TWO.C      .EQ 3
 1060  RBSC6502   .EQ 4
 1070  *--------------------------------
 1080  DIVIDEND   .EQ 0,1
 1090  QUO.REM    .EQ 2,3
 1100  T1         .EQ 4,5
 1110  T2         .EQ 6,7
 1120  *--------------------------------
 1130  CROUT  .EQ $FD8E
 1140  PRBYTE .EQ $FDDA
 1150  COUT   .EQ $FDED
 1160  *--------------------------------
 1170         .OP 65802
 1180         .LIST CON
 1190  *--------------------------------
 1200  TEST
 1210         CLC          ENTER NATIVE MODE
 1220         XCE
 1230    .DO VERSION=HACKNEY
 1240         JSR BUILD.HACKNEY.TABLE
 1250    .FIN
 1260         REP #$20     16-BIT A-REGISTER
 1270         LDA ##$3FF   LARGEST VALUE TO TEST
 1280         STA DIVIDEND
 1290  .1     LDA DIVIDEND
 1300    .DO VERSION=RBSC65802
 1310         JSR DIVIDE.BY.SEVEN.65802
 1320         STA QUO.REM  QUO IN 15...8, REM IN 7...0
 1330    .FIN
 1340    .DO VERSION=HACKNEY
 1350         JSR HACKNEY.DIV7
 1360         STA QUO.REM  QUO IN 15...8, REM IN 7...0
 1370    .FIN
 1380    .DO VERSION=RBSC6502
 1390         JSR DIVIDE.BY.SEVEN.6502
 1400    .FIN
 1410    .DO VERSION=TWO.C
 1420         JSR DIV7.TWOC
 1430    .FIN
 1440         JSR CHECK    TEST RESULT BY MULTIPLYING
 1450         BCC .2       ...CORRECT ANSWER
 1460         JSR PRINT    ...INCORRECT DIVISION
 1470  .2     JSR PAUSE    CHECK FOR KEYPRESS
 1480         BEQ .3       <RET>, ABORT
 1490         REP #$20     16-BIT A-REGISTER
 1500         DEC DIVIDEND
 1510         BPL .1       ...NEXT ONE
 1520  .3     SEC          RETURN TO EMULATION MODE
 1530         XCE
 1540         RTS
 1550  *--------------------------------
 1560  *   QUO = VAL * .001001001001001
 1570  *--------------------------------
 1580  DIVIDE.BY.SEVEN.65802
 1590         STA T1       SAVE ORIGINAL VALUE
 1600         ASL          MULTIPLY BY 64
 1610         ASL
 1620         ASL
 1630         ASL
 1640         ASL
 1650         ASL
 1660         ADC T1       ADD, EQUIV. TO * .01000001
 1670         STA T1       SAVE RESULT
 1680         LSR          DIVIDE BY 8, WHICH IS
 1690         LSR               EQUIV. TO * .00001000001
 1700         LSR
 1710         ADC T1       EQUIV TO * .01001001001
 1720    .DO 0
 1730         STA T1       EXTENDED PRECISION METHOD
 1740         XBA          GET EQUIV. TO * .00000000000001
 1750         AND ##$00FF
 1760         LSR
 1770         LSR
 1780         LSR
 1790         LSR
 1800         ADC T1       EQUIV. TO * .01001001001001
 1810    .ELSE
 1820         CMP ##$8800  FUDGE FACTOR METHOD
 1830         ADC ##$0008  ADD $0008 TO ALL VALUES,
 1840         CMP ##$8800       AND $0002 MORE TO BIG ONES
 1850         ADC ##$0000
 1860    .FIN
 1870         LSR          DIVIDE BY 2, RESULT IS QUOTIENT
 1880         SEP #$20          IN HI BYTE, REM IN NEXT 3 BITS
 1890         LSR          ISOLATE REMAINDER IN LO BYTE
 1900         LSR
 1910         LSR
 1920         LSR
 1930         LSR
 1940         REP #$20
 1950         RTS
 1960  *--------------------------------
 1970  DIVIDE.BY.SEVEN.6502
 1980         PHP          SAVE M&X BITS
 1990         SEC          SWITCH TO EMULATION MODE
 2000         XCE
 2010         PHP
 2020  *--------------------------------
 2030         LDA DIVIDEND
 2040         LSR
 2050         LSR
 2060         LSR
 2070         ADC DIVIDEND
 2080         ROR
 2090         LSR
 2100         LSR
 2110         ADC DIVIDEND
 2120         ROR
 2130         AND #$FC
 2140         STA T1
 2150         LSR
 2160         STA T2
 2170         LSR
 2180         STA QUO.REM+1     QUO = LO-BYTE/7
 2190         ADC T1
 2200         ADC T2            QUO*7
 2210         EOR #$FF          -QUO*7
 2220         SEC
 2230         ADC DIVIDEND      REM
 2240         LDX DIVIDEND+1    0,1, OR 2
 2250         ADC RTBL,X
 2260         CMP #7
 2270         BCC .1
 2280         SBC #7
 2290  .1     STA QUO.REM       FINAL REMAINDER
 2300         LDA QTBL,X
 2310         ADC QUO.REM+1
 2320         STA QUO.REM+1     FINAL QUOTIENT
 2330  *--------------------------------
 2340         PLP          SWITCH TO ORIGINAL MODE
 2350         XCE
 2360         PLP          X&M BITS
 2370         RTS
 2380  *--------------------------------
 2390  QTBL   .DA #0,#36,#73,#109
 2400  RTBL   .DA #-1,#3,#0,#4
 2410  *--------------------------------
 2420  PRINT
 2430         PHP          SAVE M&X BITS
 2440         SEC          SWITCH TO EMULATION MODE
 2450         XCE
 2460         PHP          SAVE ORIGINAL MODE (C-BIT)
 2470         LDA DIVIDEND+1
 2480         ORA #"0"     PRINT DIVIDEND IN HEX
 2490         JSR COUT
 2500         LDA DIVIDEND
 2510         JSR PRBYTE
 2520         LDA #" "     PRINT QUOTIENT IN HEX
 2530         JSR COUT
 2540         LDA QUO.REM+1
 2550         JSR PRBYTE
 2560         LDA #" "     PRINT REMAINDER IN HEX
 2570         JSR COUT
 2580         LDA QUO.REM
 2590         JSR PRBYTE
 2600         JSR CROUT    <RETURN>
 2610         PLP          RESTORE NATIVE/EMULATION BIT
 2620         XCE
 2630         PLP          RESTORE M&X BITS
 2640         RTS
 2650  *--------------------------------
 2660  CHECK
 2670         LDA QUO.REM
 2680         AND ##$FF00  ISOLATE QUOTIENT
 2690         LSR          DIVIDE BY 64 FOR NOW
 2700         LSR
 2710         LSR
 2720         LSR
 2730         LSR
 2740         LSR
 2750         STA T1
 2760         LSR          MULTIPLY BY SEVEN
 2770         STA T2
 2780         LSR
 2790         ADC T1
 2800         ADC T2
 2810         STA T1       QUO * 7
 2820         LDA QUO.REM  CHECK FOR VALID REMAINDER
 2830         AND ##$00FF  0...7
 2840         CMP ##7
 2850         BCS .1       ...INVALID REMAINDER
 2860         ADC T1       ADD QUO*7
 2870         CMP DIVIDEND ...BETTER BE SAME!
 2880         BNE .1       ...NOT, INVALID QUO & REM
 2890         CLC          SIGNAL VALID ANSWERS
 2900         RTS
 2910  .1     SEC          SIGNAL INVALID ANSWERS
 2920         RTS
 2930  *--------------------------------
 2940  PAUSE
 2950         SEP #$20     8-BIT A-REGISTER
 2960         LDA $C000    CHECK KEYBOARD
 2970         BPL .2       NOTHING TYPED
 2980         STA $C010    CLEAR STROBE
 2990         CMP #$8D     <RETURN>?
 3000         BEQ .2       <RET>, SO DON'T PAUSE
 3010  .1     LDA $C000    SOME OTHER KEY, SO PAUSE
 3020         BPL .1       ...TILL ANOTHER KEY TYPED
 3030         STA $C010    CLEAR STROBE
 3040  .2     CMP #$8D     .EQ. IF <RET>
 3050         RTS          ...ELSE .NE.
 3060  *--------------------------------
 3070  *   DIVIDE BY 7 FROM NEW //C ROMS (AT $CB4F-CBB0)
 3080  *      USED TO GET NUMBER OF 7-BYTES PACKETS
 3090  *      IN A BUFFER, FOR THE PROTOCOL CONVERTER
 3100  *--------------------------------
 3110  DIV7.TWOC
 3120         PHP          SAVE X&M BITS
 3130         SEC          ENTER EMULATION MODE
 3140         XCE
 3150         PHP          SAVE PREVIOUS MODE
 3160  *---ALGORITHM FROM //C-----------
 3170         LDX DIVIDEND+1    HI BYTE (0, 1, OR 2)
 3180         LDA PDIV7TAB,X   0, $100, OR $200 DIVIDED BY 7
 3190         STA QUO.REM+1   QUOTIENT SO FAR
 3200         LDA PMOD7TAB,X   0, $100, OR $200 MOD 7
 3210         STA QUO.REM     REMAINDER SO FAR
 3220  *---PROCESS NEXT 5 BITS----------
 3230         LDX #5
 3240         LDA DIVIDEND      LOW BYTE
 3250         STA T1            WORKING COPY
 3260         AND #7            LOW 3 BITS
 3270         TAY          SAVE FOR LATER USE
 3280  .1     ASL T1       GET NEXT BIT FROM DIVIDEND IN CARRY
 3290         BCC .4       IF CLEAR, NO EFFECT ON QUO,MOD
 3300         LDA MOD7TAB,X     GET MOD7 FOR 2^N
 3310  .2     CLC          UPDATE MOD VALUE
 3320         ADC QUO.REM
 3330         CMP #7       OVERFLOW?
 3340         BCC .3       ...NO
 3350         SBC #7       ...YES, CORRECT
 3360  .3     STA QUO.REM  REMAINDER SO FAR
 3370         LDA DIV7TAB,X     GET QUOTIENT FOR 2^N
 3380         ADC QUO.REM+1
 3390         STA QUO.REM+1     QUOTIENT SO FAR
 3400  .4     DEX               ONE LESS BIT TO DEAL WITH
 3410         BMI .5            ...FINISHED
 3420         BNE .1            ...FIVE TIMES
 3430         TYA               GET BACK FIRST 3 BITS
 3440         JMP .2            ADD IN REMAINDER
 3450  *---RETURN TO CALLER-------------
 3460  .5     PLP          ORIGINAL MODE
 3470         XCE
 3480         PLP          RESTORE X&M BITS
 3490         RTS
 3500  *--------------------------------
 3510  PDIV7TAB .DA #0,#36,#73
 3520  PMOD7TAB .DA #0,#4,#1
 3530  MOD7TAB .DA #0,#1,#2,#4,#1,#2
 3540  DIV7TAB .DA #0,#1,#2,#4,#9,#18
 3550  *--------------------------------
 3560  HACKNEY.DIV7
 3570         STA T1       SAVE VALUE
 3580         AND ##$0007  SAVE LOWER 3 BITS (MOD 8)
 3590         STA T2
 3600         LDA T1       DIVIDE BY 8
 3610         LSR
 3620         LSR
 3630         LSR
 3640         ASL          DOUBLE FOR TABLE INDEX
 3650         TAX          GET QUO & REM FROM TABLE
 3660         LDA TABLE,X
 3670         ASL          MULTIPLY BOTH BY 8
 3680         ASL
 3690         ASL
 3700         ADC T2       ADD LOWER BITS BACK
 3710         TAX          SAVE RESULT
 3720         AND ##$FF00  KEEP QUOTIENT
 3730         STA T1
 3740         TXA          GET REMAINDER
 3750         ASL          DOUBLE FOR INDEX
 3760         TAX
 3770         LDA TABLE,X  GET QUO & REM FROM TABLE
 3780         CLC          ADD PREVIOUS QUOTIENT
 3790         ADC T1
 3800         RTS
 3810  *--------------------------------
 3820  BUILD.HACKNEY.TABLE
 3830         PHP          SAVE M&X BITS
 3840         REP #$20     LONG A-REG
 3850         LDA ##TABLE
 3860         STA T1
 3870         SEP #$30     ALL REGS SHORT
 3880         LDX #0       X = REMAINDER
 3890         TXY          Y = QUOTIENT
 3900  .1     TXA          STORE CURRENT REMAINDER
 3910         STA (T1)
 3920         INC T1
 3930         TYA          STORE CURRENT QUOTIENT
 3940         STA (T1)
 3950         INC T1
 3960         INX          NEXT REMAINDER
 3970         CPX #7
 3980         BCC .1       ...NO CHANGE TO QUOTIENT
 3990         LDX #0       NEXT QUOTIENT
 4000         INY
 4010    .DO 1
 4020         CPY #10      STOP AFTER QUO=9, REM=6
 4030    .ELSE
 4040         CPY #16      STOP AFTER QUO=15, REM=6
 4050    .FIN
 4060         BCC .1       ...NOT YET
 4070         PLP          RESTORE M&X BITS
 4080         RTS
 4090  *--------------------------------
 4100         .BS *+255/256*256-*
 4110  TABLE  .EQ *
 4120  *--------------------------------

