   10REM SAVE"$.Arabic.Source.Flip"
   20OsWrch = &FFEE
   30OsWord = &FFF1
   40OsByte = &FFF4
   50OsNewl = &FFE7
   60DIM O% &BFFF-P%
   70Q%=O%:R%=P%
   80PROCConsts
   90ExtIns = &FF00+3*Ins
  100PROCVars
  110very_temp=T%
  120KeyComEnd=99:KeyCom=90
  130CurrentState = J%
  140left=FNzp(2,"left")
  150right=FNzp(2,"right")
  160Mode = FNzp(2,"Mode")
  170oswptr=FNzp(2,"oswptr")
  180data=FNzp(2,"data:Flip")
  190HisIns = FNRmb(2)
  200ScrLines = FNRmb(1)
  210ScrBase = FNRmb(1)
  220Blocks = FNRmb(1)
  230ContextAnalyseFlag = FNRmb(1)
  240NumberReverseFlag = FNRmb(1)
  250EnteredAlready=FNRmb(1)
  260PRINT"EnteredAlready flag at &10";~EnteredAlready MOD 256
  270TextDirnFlag = FNRmb(1)
  280dummy4=FNRmb(1)
  290FontDirnFlag = FNRmb(1)
  300dummy1=FNRmb(1)
  310ScreenDirnFlag = FNRmb(1)
  320dummy2=FNRmb(1)
  330osw = FNRmb(9)
  340px = FNzp(2,"px")
  350FOR Pass = 4 TO 6+L% STEP 2+L%
  360O%=Q%
  370P%=R%
  380[OPT Pass
  390.Mode3
  400  OPT FNload("Mode3",128*2)
  410.EndMode3
  420  EQUW &8000
  430.Mode012
  440  OPT FNload("Mode012",32*2)
  450.EndMode0
  460  EQUW &8000
  470.Mode45
  480  OPT FNload("Mode45",32*2)
  490.EndMode45
  500  EQUW &8000
  510.Mode6
  520  OPT FNload("Mode6",128*2)
  530.EndMode6
  540  EQUW &8000
  550.ModeBases
  560  EQUW Mode012 \ 0
  570  EQUW Mode012 \ 1
  580  EQUW Mode012 \ 2
  590  EQUW Mode3
  600  EQUW Mode45  \ 4
  610  EQUW Mode45  \ 5
  620  EQUW Mode6
  630.LineTab
  640  EQUB 31
  650  EQUB 31
  660  EQUB 31
  670  EQUB 24
  680  EQUB 31
  690  EQUB 31
  700  EQUB 24
  710.BaseTab
  720  EQUB &30
  730  EQUB &30
  740  EQUB &30
  750  EQUB &40
  760  EQUB &58
  770  EQUB &58
  780  EQUB &60
  790.BlockTab
  800  EQUB (&280 DIV 8 DIV 2)-1
  810  EQUB (&280 DIV 8 DIV 2)-1
  820  EQUB (&280 DIV 8 DIV 2)-1
  830  EQUB (&280 DIV 8 DIV 2)-1
  840  EQUB (&140 DIV 8 DIV 2)-1
  850  EQUB (&140 DIV 8 DIV 2)-1
  860  EQUB (&140 DIV 8 DIV 2)-1
  870  OPT FNAlign(256)
  880.charflip
  890.Px0346
  900  OPT FNload("Px0346",256)
  910.Px15
  920  OPT FNload("Px15",256)
  930.Px2
  940  OPT FNload("Px2",256)
  950.pxtab
  960  EQUW Px0346
  970  EQUW Px15
  980  EQUW Px2
  990  EQUW Px0346
 1000  EQUW Px0346
 1010  EQUW Px15
 1020  EQUW Px0346
 1030 
 1040.FlipHandlerInit
 1050  OPT FNEnter
 1060  Lda #False:OPT FNSta(ContextAnalyseFlag):Lda #0:OPT FNSta(CurrentState)
 1070  Lda #False:OPT FNSta(NumberReverseFlag)
 1080  Lda #LeftRight:OPT FNSta(TextDirnFlag)
 1090  Lda #LeftRight:OPT FNSta(FontDirnFlag)
 1100  Lda #LeftRight:OPT FNSta(ScreenDirnFlag)
 1110  Lda #0:OPT FNSta(EnteredAlready)
 1120\ Install interception for Insert-in-buffer to trap <FLIP> key
 1130  Lda InsV:OPT FNSta(HisIns)
 1140  Lda InsV+1:OPT FNSta(HisIns+1)
 1150  Lda #ExtIns MOD 256:Sta InsV
 1160  Lda #ExtIns DIV 256:Sta InsV+1
 1170  Lda #MyIns MOD 256:Sta &D9F+3*Ins+0
 1180  Lda #MyIns DIV 256:Sta &D9F+3*Ins+1
 1190  Lda &F4:Sta &D9F+3*Ins+2
 1200  OPT FNExit
 1210  Rts
 1220 
 1230.MyIns
 1240  PHP:PHY:PHX:PHA
 1250  Cpx #0:Beq lkkj:Jmp notkeyboard:.lkkj
 1260\\\BRA noarrowswap
 1270  \ If in english left-right mode, Bra noarrowswap
 1280  Lda data:Pha:Lda data+1:Pha
 1290  Ldx &F4:Lda &DF0,X:Sta data+1:Lda #TextDirnFlag:Sta data
 1300  Lda (data)
 1310  Ply:Sty data+1:Ply:Sty data
 1320  Cmp #LeftRight:Beq noarrowswap
 1330  PLA:PLX:PLY:PHY:PHX:PHA
 1340  \ If in cursor edit mode, Bra noarrowswap
 1350  PHX:PHY:PHA
 1360  Lda #&75:Jsr OsByte:Txa:And #&40:Bne UnstackAndNoSwap
 1370  PLA:PLY:PLX
 1380\STA &90
 1390  Cmp #&8C:Beq ggg1 \ Left  **** SHOULD BE TABLE DRIVEN - quick hack here!
 1400  Cmp #&8D:Beq ggg1 \ Right
 1410  Cmp #&9C:Beq ggg1 \ Left
 1420  Cmp #&9D:Beq ggg1 \ Right
 1430  Cmp #&AC:Beq ggg1 \ Left
 1440  Cmp #&AD:Beq ggg1 \ Right
 1450  Cmp #&BC:Beq ggg1 \ Left
 1460  Cmp #&BD:Beq ggg1 \ Right
 1470  Bra noarrowswap
 1480.ggg1 PLA:Eor #1:PHA:Bra noarrowswap  \ Actually, Eor (data),TextDirnFlag
 1490.UnstackAndNoSwap  PLA:PLY:PLX
 1500.noarrowswap
 1510  PLA:PHA
 1520\STA &91
 1530  Cmp #&B1:Bne kkkk:PLA:Lda #&BB:PHA:.kkkk
 1540  Cmp #&8B:Bne notflip
 1550  Lda #&75:Jsr OsByte:Txa:And #&40:Bne CursorEditing
 1560  Jsr CopyKeyFlip
 1570  PLA:PLX:PLY:PLP
 1580  Rts
 1590.CursorEditing
 1600.notflip
 1610.notkeyboard
 1620  PLA:PLX:PLY:PLP
 1630  OPT FNJmpI(HisIns)
 1640 
 1650.otherCountry
 1660  \ Undo any reversals
 1670  PHY:PHX:PHA
 1680  Jsr ScreenFlip_UK
 1690  Jsr FontFlip_UK
 1700  Jsr TextFlip_UK
 1710  PLA:PLX:PLY
 1720  Rts
 1730 
 1740 
 1750.FlipHandler \ ***** OSBYTE ENTRY POINT WITH A,X,Y SET UP
 1760  Cpx #21:Bne otherCountry
 1770  Cpy #0:Bne x1
 1780  Jmp CountryArabic
 1790.x1
 1800  Cpy #1:Bne x2
 1810  Jmp ScreenFlip_Other
 1820.x2
 1830  Cpy #2:Bne x3
 1840  Jmp ScreenFlip_Arabic
 1850.x3
 1860  Cpy #3:Bne x4
 1870  Jmp ScreenFlip_UK
 1880.x4
 1890  Cpy #4:Bne x5
 1900  Jmp FontFlip_Other
 1910.x5
 1920  Cpy #6:Bne x6
 1930  Jmp FontFlip_Arabic
 1940.x6
 1950  Cpy #7:Bne x7
 1960  Jmp FontFlip_UK
 1970.x7
 1980  Cpy #8:Bne x8
 1990  Jmp TextFlip_Other
 2000.x8
 2010  Cpy #10:Bne x9
 2020  Jmp TextFlip_Arabic
 2030.x9
 2040  Cpy #11:Bne x10
 2050  Jmp TextFlip_UK
 2060.x10
 2070  Cpy #14:Bne x11
 2080  Jmp Arabic_TextOn
 2090.x11
 2100  Cpy #15:Bne x12
 2110  Jmp Arabic_TextOff
 2120.x12
 2130  Cpy #18:Bne x13
 2140  Jmp Arabic_NumeralsOn
 2150.x13
 2160  Cpy #19:Bne x14
 2170  Jmp Arabic_NumeralsOff
 2180.x14
 2190  Rts
 2200 
 2210.ScreenFlip_Other
 2220  OPT FNEnter
 2230\Lda #ASC("A"):\Jsr &E7E6
 2240  Jsr ScreenFlip
 2250  Jsr TextFlipAndMoveCursor
 2260  OPT FNExit
 2270  Lda #0
 2280  Rts
 2290.ScreenFlip_Arabic
 2300  OPT FNEnter
 2310\Lda #ASC("B"):\Jsr &E7E6
 2320  OPT FNLda(ScreenDirnFlag):Cmp #LeftRight:Bne z1a
 2330\Lda #ASC("C"):\Jsr &E7E6
 2340  Jsr ScreenFlip
 2350.z1a
 2360  OPT FNLda(TextDirnFlag):Cmp #LeftRight:Bne z1
 2370\Lda #ASC("D"):\Jsr &E7E6
 2380  Jsr TextFlipAndMoveCursor
 2390.z1
 2400  OPT FNExit
 2410  Lda #0
 2420  Rts
 2430.ScreenFlip_UK
 2440  OPT FNEnter
 2450\Lda #ASC("E"):\Jsr &E7E6
 2460  OPT FNLda(ScreenDirnFlag):Cmp #RightLeft:Bne z2a
 2470\Lda #ASC("F"):\Jsr &E7E6
 2480  Jsr ScreenFlip
 2490.z2a
 2500  OPT FNLda(TextDirnFlag):Cmp #RightLeft:Bne z2
 2510\Lda #ASC("G"):\Jsr &E7E6
 2520  Jsr TextFlipAndMoveCursor
 2530.z2
 2540  OPT FNExit
 2550  Lda #0
 2560  Rts
 2570.FontFlip_Other
 2580  OPT FNEnter
 2590\Lda #ASC("H"):\Jsr &E7E6
 2600  Jsr FontFlip
 2610  OPT FNExit
 2620  Lda #0
 2630  Rts
 2640.FontFlip_Arabic
 2650  OPT FNEnter
 2660\Lda #ASC("I"):\Jsr &E7E6
 2670  OPT FNLda(FontDirnFlag):Cmp #RightLeft:Beq z3
 2680\Lda #ASC("J"):\Jsr &E7E6
 2690  Jsr FontFlip
 2700.z3
 2710  OPT FNExit
 2720  Lda #0
 2730  Rts
 2740.FontFlip_UK
 2750  OPT FNEnter
 2760\Lda #ASC("K"):\Jsr &E7E6
 2770  OPT FNLda(FontDirnFlag):Cmp #LeftRight:Beq z4
 2780\Lda #ASC("L"):\Jsr &E7E6
 2790  Jsr FontFlip
 2800.z4
 2810  OPT FNExit
 2820  Lda #0
 2830  Rts
 2840.TextFlip_Other
 2850  OPT FNEnter
 2860\Lda #ASC("M"):\Jsr &E7E6
 2870  Jsr TextFlip
 2880  OPT FNExit
 2890  Lda #0
 2900  Rts
 2910.TextFlip_UK
 2920  OPT FNEnter
 2930\Lda #ASC("N"):\Jsr &E7E6
 2940  OPT FNLda(TextDirnFlag):Cmp #LeftRight:Beq z5
 2950\Lda #ASC("O"):\Jsr &E7E6
 2960  Jsr TextFlip
 2970.z5
 2980  OPT FNExit
 2990  Lda #0
 3000  Rts
 3010.TextFlip_Arabic
 3020  OPT FNEnter
 3030\Lda #ASC("P"):\Jsr &E7E6
 3040  OPT FNLda(TextDirnFlag):Cmp #RightLeft:Beq z6
 3050\Lda #ASC("Q"):\Jsr &E7E6
 3060  Jsr TextFlip
 3070.z6
 3080  OPT FNExit
 3090  Lda #0
 3100  Rts
 3110.Arabic_TextOn
 3120  OPT FNEnter
 3130\Lda #ASC("R"):\Jsr &E7E6
 3140  Lda #True:OPT FNSta(ContextAnalyseFlag)
 3150  OPT FNExit
 3160  Lda #0
 3170  Rts
 3180.Arabic_TextOff
 3190  OPT FNEnter
 3200\Lda #ASC("S"):\Jsr &E7E6
 3210  Lda #False:OPT FNSta(ContextAnalyseFlag):Lda #0:OPT FNSta(CurrentState)
 3220  OPT FNExit
 3230  Lda #0
 3240  Rts
 3250.Arabic_NumeralsOn
 3260  OPT FNEnter
 3270\Lda #ASC("T"):\Jsr &E7E6
 3280  Lda #True:OPT FNSta(NumberReverseFlag)
 3290  OPT FNExit
 3300  Lda #0
 3310  Rts
 3320.Arabic_NumeralsOff
 3330  OPT FNEnter
 3340\Lda #ASC("U"):\Jsr &E7E6
 3350  Lda #False:OPT FNSta(NumberReverseFlag)
 3360  OPT FNExit
 3370  Lda #0
 3380  Rts
 3390 
 3400.CopyKeyFlip
 3410  OPT FNEnter
 3420  PHP:SEI
 3430  OPT FNLda(EnteredAlready):Clc:Adc #1:OPT FNSta(EnteredAlready)
 3440  Cmp #1:Bne NotRecursive
 3450  Lda #&75:Jsr OsByte:Txa:And #&40:Bne CursorEditing2
 3460  PLP
 3470  Jsr FontFlip
 3480  Jsr ScreenFlip
 3490  Jsr TextFlipAndMoveCursor
 3500  Jsr KeyBoardFlip
 3510  PHP:SEI
 3520.CursorEditing2
 3530.NotRecursive
 3540  OPT FNLda(EnteredAlready):Sec:Sbc #1:OPT FNSta(EnteredAlready)
 3550  PLP
 3560  OPT FNExit
 3570  Rts
 3580 
 3590.KeyBoardFlip
 3600  \ Depending on current text direction, select appropriate keyboard
 3610  OPT FNLda(ScreenDirnFlag)
 3620  Cmp #LeftRight:Beq SetUK
 3630.SetArab
 3640  Ldx #ArabKb MOD 256:Ldy #ArabKb DIV 256
 3650  Jsr DoOScli
 3660  Rts
 3670.SetUK
 3680  Ldx #UKKb MOD 256:Ldy #UKKb DIV 256
 3690  Jsr DoOScli
 3700  Rts
 3710 
 3720.DoOScli
 3730  Lda oswptr:PHA:Lda oswptr+1:PHA
 3740  Stx oswptr:Sty oswptr+1:Ldy #0
 3750.comlp
 3760  Lda (oswptr),Y
 3770  Sta &140,Y:Iny
 3780  Cmp #13:Bne comlp
 3790  Ldx #&40:Ldy #1:Jsr OsCli
 3800  PLA:Sta oswptr+1:PLA:Sta oswptr
 3810  Rts
 3820 
 3830.ArabKb EQUS "*KEYBOARD ARABIC":EQUB 13
 3840.UKKb EQUS "*KEYBOARD UK":EQUB 13
 3850 
 3860.CountryArabic \ Select Keyboard and Alphabet too.
 3870 \ **** Should the *Country command issue the *Alphabet/*Keyboard commands
 3880 \      too or should they be recursively invoked by the OsByte(70) ???
 3890 \      Here we assume doing them twice will do no harm.
 3900  PHX:PHY
 3910\Lda #ASC("V"):\Jsr &E7E6
 3920  Lda #71:Ldx #21:Jsr OsByte
 3930  OPT FNEnter
 3940  Jsr FontFlip_magic \ Start up with Arabic letters reversed
 3950  OPT FNExit
 3960  Lda #71:Ldx #21+128:\Jsr OsByte   \ *** Actually, it is significant
 3970                                \ whether or not the fonts are set up
 3980                                \ BEFORE or AFTER the fontflip!
 3990  Jsr KeyBoardArabic \ Fudge because Paul hasn't supplied FX 71 correctly
 4000  Jsr FontFlip_Arabic
 4010  Jsr ScreenFlip_Arabic
 4020  OPT FNEnter
 4030  Lda #True:OPT FNSta(ContextAnalyseFlag)
 4040  Lda #True:OPT FNSta(NumberReverseFlag)
 4050  OPT FNExit
 4060  PLY:PLX
 4070  Lda #0
 4080  Rts
 4090 
 4100.KeyBoardArabic
 4110\Lda #ASC("W"):\Jsr &E7E6
 4120  Ldy #KeyComEnd-KeyCom-1
 4130.lpkey
 4140  Lda KeyCom,Y:Sta &100,Y
 4150  Dey
 4160  Bpl lpkey
 4170  Ldx #0:Ldy #1:Jsr OsCli
 4180  Rts
 4190.KeyCom
 4200  EQUS "*KEYBOARD ARABIC":EQUB 13
 4210.KeyComEnd
 4220 
 4230.ScreenFlip
 4240  Lda #0:Jsr OsWrch  \ Flush pending context analyses
 4250  Lda &FE34:PHA:And #1:Asl A:Asl A:Sta oswptr:PLA:PHA
 4260  And #(&FF EOR 4):Ora oswptr:Sta &FE34 \ Select displayed screen.
 4270\Lda #ASC("X"):\Jsr &E7E6
 4280  OPT FNLda(ScreenDirnFlag): Eor #1: OPT FNSta(ScreenDirnFlag)
 4290  Lda #160:Ldx #&55:Ldy #0
 4300  Jsr &FFF4
 4310  Txa: And #7: Tax: Lda LineTab,X: PHY:OPT FNSta(ScrLines):PLY
 4320  Lda BaseTab,X:OPT FNSta(ScrBase)  \ Y corrupted!
 4330  Lda BlockTab,X:OPT FNSta(Blocks)  \ Ditto
 4340  Txa
 4350  Asl A
 4360  Tay
 4370  Lda ModeBases,Y:Sta Mode
 4380  Lda pxtab,Y:Sta px
 4390  Iny:Lda ModeBases,Y:Sta Mode+1
 4400  Lda pxtab,Y:Sta px+1
 4410  Lda #160:Ldx #&50:Ldy #0
 4420  Jsr &FFF4
 4430  Stx left:Sty left+1
 4440  Ldy #0
 4450.INK
 4460  Lda (Mode),Y:INY:Pha
 4470  Lda (Mode),Y:Tax
 4480  Bmi NotFnd
 4490  Pla:INY
 4500  Cmp left:Bne INK
 4510  Cpx left+1:Bne INK
 4520\ FOUND...
 4530  PHY:OPT FNLda(ScrLines):PLY:Tax
 4540  DEY:DEY
 4550.EachRow
 4560  Lda (Mode),Y:INY:Sta left
 4570  Lda (Mode),Y:Sta left+1
 4580  Bpl nowrp
 4590  Ldy #0
 4600  Bra EachRow
 4610.nowrp
 4620  INY
 4630  Lda (Mode),Y:INY:Sec:Sbc #8:Sta right
 4640  Lda (Mode),Y:Sbc #0:Sta right+1
 4650  DEY
 4660  Jsr FlipRow
 4670  Dex
 4680  Bpl EachRow
 4690  PLA:Sta &FE34
 4700  Rts
 4710.NotFnd
 4720  BRK:EQUB 17:EQUS "Didn't find it...":EQUB 0
 4730.FlipRow
 4740  PHX:PHY
 4750  OPT FNLda(Blocks):Tax
 4760.NextBlk
 4770  Ldy #7
 4780.lp2
 4790  Lda (left),Y
 4800  PHA
 4810  Lda (right),Y
 4820  Phy:tay:lda (px),y:ply
 4830  Sta (left),Y
 4840  PLA
 4850  Phy:tay:lda (px),y:ply
 4860  Sta (right),Y
 4870  Dey
 4880  Bpl lp2
 4890  Jsr Closer \ Corrupts Y
 4900  Dex
 4910  Bpl NextBlk
 4920  PLY:PLX
 4930  Rts
 4940 
 4950.PHEXIT
 4960  Lda left+1:Jsr PB
 4970  Lda left:Jsr PB
 4980  Lda #ASC("-"):Jsr &E7E6
 4990  Lda right+1:Jsr PB
 5000  Lda right:Jsr PB
 5010  Lda #ASC(","):Jsr &E7E6
 5020  Rts
 5030 
 5040.PB
 5050  PHA
 5060  Lsr A:Lsr A:Lsr A:Lsr A
 5070  Jsr PN
 5080  Pla
 5090.PN
 5100  And #&0F
 5110  Cmp #10
 5120  Bcc zzz1
 5130  Adc #6
 5140.zzz1
 5150  Adc #ASC("0")
 5160  Jmp &E7E6
 5170 
 5180.Closer \ we allow this to corrupt Y
 5190  Pha
 5200  Lda left:Clc:Adc #8:Sta left
 5210  Lda left+1:Adc #0:Sta left+1
 5220  Lda left:Cmp #0:Bne qq4:Lda left+1:Cmp #&80:Bne qq4
 5230  OPT FNLda(ScrBase):Sta left+1     \ Y corrupted!
 5240.qq4
 5250  Lda right:Cmp #0:Bne qq3
 5260  OPT FNLda(ScrBase): Cmp right+1:Bne qq3 \ Y again corrupted
 5270  Lda #&80:Sta right+1
 5280.qq3
 5290  Lda right:Sec:Sbc #8:Sta right
 5300  Lda right+1:Sbc #0:Sta right+1
 5310  Pla
 5320  Rts
 5330 
 5340.FontFlip_magic
 5350  Lda #128+32
 5360  Bra FontFlip_p
 5370.FontFlip
 5380\Lda #ASC("Y"):\Jsr &E7E6
 5390  OPT FNLda(FontDirnFlag): Eor #1: OPT FNSta(FontDirnFlag)
 5400\ use 'charflip' to reverse bits in byte
 5410  Lda #32
 5420.FontFlip_p
 5430.chflp
 5440  \Cmp #ASC("0"):\Bne tt1:\Lda #ASC("9")+1:\.tt1
 5450  Pha
 5460  Jsr flipch
 5470  Pla:Clc:Adc #1
 5480  Bne chflp
 5490  Rts
 5500 
 5510.flipch
 5520\  PHP:\SEI
 5530  Ldy oswptr:Phy
 5540  Ldy oswptr+1:Phy
 5550  Pha
 5560  Ldx &F4:Lda &DF0,X:Sta oswptr+1
 5570  Lda #osw: Sta oswptr
 5580  Pla:Sta (oswptr)
 5590  Lda #10:Ldx oswptr:Ldy oswptr+1
 5600  Jsr OsWord
 5610  Lda #23:Jsr OsWrch
 5620  Lda (oswptr):Jsr OsWrch
 5630  Ldy #1
 5640.eachfl
 5650  Lda (oswptr),Y:Tax:Lda charflip,X
 5660  Jsr OsWrch
 5670  Iny:Cpy #9:Bne eachfl
 5680  Ply:Sty oswptr+1
 5690  Ply:Sty oswptr
 5700\  PLP
 5710  Rts
 5720 
 5730.TextFlipAndMoveCursor
 5740  Lda #134: Jsr OsByte: Phy: Phx \ (X,Y) -> X, Y regs
 5750  Jsr TextFlip
 5760  Lda #31: Jsr OsWrch: Pla: Jsr OsWrch: Pla: Jsr OsWrch
 5770  Rts
 5780 
 5790.TextFlip
 5800\Lda #ASC("Z"):\Jsr &E7E6
 5810  OPT FNLda(TextDirnFlag): Eor #1: OPT FNSta(TextDirnFlag)
 5820  Lda #23:Jsr OsWrch:
 5830  Lda #16:Jsr OsWrch:
 5840  Lda #1*2:Jsr OsWrch:\Lda #(255-7*2):Lda #255:Jsr OsWrch
 5850  Lda #0:Jsr OsWrch:Jsr OsWrch:Jsr OsWrch:Jsr OsWrch:Jsr OsWrch:Jsr OsWrch
 5860  Rts
 5870 
 5880]
 5890NEXT Pass
 5900C%=FlipHandler:G%=FlipHandlerInit
 5910H% = ContextAnalyseFlag:I% = NumberReverseFlag
 5920OSCLI("Save $.Arabic.Object.FlipObj "+STR$~(Q%)+" "+STR$~(O%)+" "+STR$~(R%-&8000+&3000)+"  "+STR$~(R%-&8000+&3000))
 5930CHAIN"$.Arabic.Source.StateMC"
 5940DEFFNload(F$,Size)
 5950OSCLI("Load $.Arabic.Object."+F$+" "+STR$~(O%))
 5960REMIF ((Pass AND 3)=0) AND ((P%+Size-1)DIV256 <> P%DIV256) THEN PRINT"Table ";F$;" over page boundary"
 5970O%=O%+Size:P%=P%+Size
 5980=Pass
 5990 
 6000DEFFNAlign(Boundary) : REM &40, &80, or &100
 6010Boundary=Boundary-1
 6020NewP%=(P%+Boundary) AND (NOT Boundary)
 6030O%=O%+(NewP%-P%)
 6040P%=NewP%
 6050=Pass
 6060 
 6070DEFPROCConsts
 6080REM TESTING BUG FIXdata=S%
 6090True = 0:False = 1
 6100LeftRight = 0:RightLeft = 1
 6110                                  REM MyXXX's are initialised here for
 6120                                  REM 2-pass assembly in Basic.
 6130Ins = 21   : InsV = FNVector(Ins)
 6140OsByte = &FFF4
 6150Byte = 5   : ByteV = FNVector(Byte)
 6160OsWord = &FFF1
 6170OsCli  = &FFF7
 6180OsRdCh = &FFE0
 6190RdCh = 8   : RdChV = FNVector(RdCh)
 6200OsRdSc = &FFB9
 6210OsWrCh = &FFEE
 6220OsNewl = &FFE7
 6230OsAscii = &FFE3
 6240OsWrSc = &FFB3
 6250OsFind = &FFCE
 6260OsFile = &FFDD
 6270OsArgs = &FFDA
 6280OsGbPb = &FFD1
 6290OsBPut = &FFD4
 6300OsBGet = &FFD7
 6310OsEvent = &FFBF
 6320GSInit = &FFC2
 6330GSRead = &FFC5
 6340ENDPROC
 6350 
 6360DEFFNVector(N)
 6370 = &200 + 2*N
 6380 
 6390DEFPROCVars
 6400ENDPROC
 6410 
 6420DEFFNzp(N,N$)
 6430LOCAL I
 6440  I = N%
 6450  N% = N% + N
 6460  IF N% > Z% THEN PRINT"ERROR: Using too much zero page.":END
 6470  PRINT"Zero page variable '";N$;"' at &";~I;" (";N;" byte";
 6480  IF N<>0 THEN PRINT "s";
 6490  PRINT ")"
 6500  = I
 6510 
 6520DEFFNRmb(N)
 6530LOCAL I
 6540  I = M%
 6550  M% = M% + N
 6560  = I
 6570 
 6580DEFFNEnter   : REM Called once on entry to Rom
 6590               REM Sets uplocal data area for easy access
 6600[OPT Pass
 6610  Lda data:PHA
 6620  Lda data+1:PHA
 6630  Ldx &F4:Lda &DF0,X
 6640  Sta data+1
 6650  Stz data
 6660  OPT FNStack(left)
 6670  OPT FNStack(left+1)
 6680  OPT FNStack(right)
 6690  OPT FNStack(right+1)
 6700  OPT FNStack(Mode)
 6710  OPT FNStack(Mode+1)
 6720  OPT FNStack(px)
 6730  OPT FNStack(px+1)
 6740  \Lda #ASC("<"):\Jsr &E7E6
 6750  \OPT FNLda(TextDirnFlag):\Jsr PB
 6760  \OPT FNLda(FontDirnFlag):\Jsr PB
 6770  \OPT FNLda(ScreenDirnFlag):\Jsr PB
 6780  \Lda #ASC(">"):\Jsr &E7E6
 6790]
 6800=Pass
 6810 
 6820DEFFNLda(variable_offset)
 6830[OPT Pass
 6840  Ldy #variable_offset:Lda (data),Y
 6850]
 6860=Pass
 6870 
 6880DEFFNSta(variable_offset)
 6890[OPT Pass
 6900  Ldy #variable_offset:Sta (data),Y
 6910]
 6920=Pass
 6930 
 6940DEFFNExit :REM Called once on exit from Rom
 6950[OPT Pass
 6960  \Lda #ASC("["):\Jsr &E7E6
 6970  \OPT FNLda(TextDirnFlag):\Jsr PB
 6980  \OPT FNLda(FontDirnFlag):\Jsr PB
 6990  \OPT FNLda(ScreenDirnFlag):\Jsr PB
 7000  \Lda #ASC("]"):\Jsr &E7E6
 7010  OPT FNUnstack(px+1)
 7020  OPT FNUnstack(px)
 7030  OPT FNUnstack(Mode+1)
 7040  OPT FNUnstack(Mode)
 7050  OPT FNUnstack(right+1)
 7060  OPT FNUnstack(right)
 7070  OPT FNUnstack(left+1)
 7080  OPT FNUnstack(left)
 7090  Pla: Sta data+1
 7100  Pla: Sta data
 7110]
 7120=Pass
 7130 
 7140DEFFNStack(reg)
 7150[OPT Pass
 7160  Lda reg:Pha
 7170]
 7180=Pass
 7190 
 7200DEFFNUnstack(reg)
 7210[OPT Pass
 7220  Pla:Sta reg
 7230]
 7240=Pass
 7250 
 7260DEFFNJmpI(offset)  : REM Jumps VIA private ram - indirect.
 7270IF offset >= &100 THEN =FNLongJmpI(offset)
 7280[OPT Pass
 7290  PHP:PHA:PHX
 7300  Ldx &F4:Lda &DF0,X:Sta very_temp+1:Lda #offset:Sta very_temp
 7310  Lda (very_temp):Pha:Inc very_temp:Bne P%+4:Inc very_temp+1
 7320  Lda (very_temp):Sta very_temp+1:Pla:Sta very_temp
 7330  PLX:PLA:PLP
 7340  Jmp (very_temp)
 7350]
 7360=Pass
