###############################################################
#
#               OPTIMIZED RUSSIAN TEXT
#
#

# Это была славная попытка оптимизации русского текста (на манер Gemini), но она провалилась, так как под все возможные алфавиты просто нет места в видеопамяти
# Зато мы научились писать команды копирования спрайтов и кодировать координаты vram и clut


                 #               Coords XY init
lui         t2,MyAddr

addiu v0,r4,-0xa0      #shift char code for A is 0
sll v0,2            #charcode * 4
ori v1,r17,0xf900       #current char params start
     
addu v0,v0,v1       # shift to current char params addr
lw t7,0x0(v0)       #t7  = CURRENT CHAR PARAMS load

addiu t8,t2,-0x500         #dest temp scratch addr 8008fb00
clear     r18



lh          v1,-0xE(t2)                  #Load my temp CHAR COUNTER for check
lhu         t5,0x20(sp) # r13 = ld half (sp+20) = 003A ---X from sp+20

sll         t6,v1,0x1                    #t6 = charcount*2
addu        v1,t6,v1                    #v1 = t6+v1 = charcount * 3
sll         v1,0x1             #v1 * 2 = charcount * 6
addu        v0,v1,t5          #v0 = INIT X + CURCHAR * 6

sh         v0,0x8(s3)  #DAT_1f8003d8      save r2 half to 1f8003d8 = X (3A) !!!!!!!!!!!!!!!!
#SaveYinScratch:
sh         s8,0xa(s3)  # DAT_1f8003da   *r19(1st scr DMA)+0a = r30 (Y??)    !!!!!!!!!!!!!!!!


#lw t5,0x0(s3)       #copying COMMAND 00 00 00 04
#nop
lui         t5,0x400 #load 4 commands in command
nop
sw t5,0x0(t8)       #paste to tempChain...








add5command:
lui          t5, 0x0500       # 'its 5 commands'
sw          t5,0x0(t8)       #paste...
lui         t6, 0xe100#################################
ori         t6,0x023f                     #e100023f - TextPage = interfaceTim - BEFORE LAST COMMAND
sw          t6,0x4(t8)       #paste...
addiu       t8, 0x4         #SHIFT temp DMA addr +4 (After TexPage)




IsRusChar:

lw t6,0x4(s3)       #copying COMMAND 80 80 80 64
nop
sw t6,0x4(t8)       #paste...

#######################DEST X AND Y HERE@@@@@@@@@@@@@@@@@@@@@@@@@
lh t5,0x8(s3)       #DEST X HERE
nop                 # Need to recalculate X
addiu t5, 0x1       #right 1 pixel >
sh t5,0x8(t8)       #paste DEST X


lh t6,0xA(s3)        #load dest Y
sra v1, t7, 24          ##>> SHIFT 24 bits = Y vertical offset
addu t6,t6,v1         #shift dest y
#addiu t6,0x2
sh t6,0xA(t8)       #save dest y


Ori         v1,v1,0x10      #Concat X offs (1<< ) and Y vertical offset - 0x1(2)?
sb          v1,-0x8(t2)                  #Store coords to temp



li v1, 0xffff
and v0, t7, v1          #truncate SRC (FFFF) coords from myTable
sh v0,0xc(t8)       #Saving source coords to chain



lbu v0, 0xac(gp)    #load color
li v1, 0x3d         #initial CLUT X
addiu v0,-0x1       # color-1
sra v0, 1           # color /2
addiu v0, 0x1b0     #vertical color shift in VRAM
sll v0, 6           #vertical shift << 6 bits
or  t6,v0,V1        # vertical<<6 & horis = CLUT   



sh t6,0xe(t8)      #save clut

li v1,0xff
li t5,6
sh t5,0x10(t8)       #paste...
sra v0, t7, 16      #>> shift by 16 bit right
and t5, v0, v1      #truncate (AND FF)

       #NEED TO MAKE DEST SIZE IN T5
sh t5,0x12(t8)       #paste...



clear t6
sw t6,0x14(t8)############################################

            #Fixing text coords

#lw t7,0x8(t8)           #LOAD X,Y TO T7 FOR STORE IN PARAMS!!
lw t7,0x8(s3)           #LOAD ORIGINAL X,Y TO T7 FOR STORE IN PARAMS!!
nop



            #MAIN COMMAND PAGELOOP INIT
move       t4,r20      # r12 = r20 (params)
move       t1,r20      # r12 = r20 (params again)






PageLoop2:

        lui        t2,0xff00   # r10 = ld upper ff00 = ff000000                                                                #DECREASE COUNTER & GET CHAIN ADDR
        lw         v0,-0x4930(s1)#     r2 = *freeChainAddr
        nop
        and        r4,v0,t2    #  r4 = r2 & ff00..
        and        v0,v0,s0    #  r2 & 00FFFFFF
        or         r5,v0,s7    # r5 = r2 or r23 (80000000) = 801ADF10
        lw         v0,0x4(s5)  # CmdCounter    r2 = *7b6d0+4 = 705
        lw         v1,0x0(r5)  # r3 = Next Free ChainAddr
        addiu      v0,v0,-0x1  # r2 -= 1 Counter Down
        and        v1,v1,s0    # r3 & 00FFFFFF cut begin byte
        or         r4,r4,v1    # r4 & r3 = ff......
        sw         v0,0x4(s5)  # CmdCounter    save DMA Counter
        sw         r4,-0x4930(s1)# freeChain     save new freeChain


        lui t8,0x8009
        addiu t8,t8,-0x500         #reset temp dma to 8008fb00
CopyCmdFromTemp:
        lw t5,0x0(t8)       #copying chain (comand count)
        lw t6,0x4(t8)       #copying       (comand id)
        sw t5,0x0(r5)       #paste...
        sw t6,0x4(r5)       #paste...
        lw t5,0x8(t8)       #copying chain  (YyyyXxxx)
        lw t6,0xc(t8)       #copying        (clutYyXx)  #COPYING FROM TEMP TO MAIN CHAIN
        sw t5,0x8(r5)       #paste...
        sw t6,0xc(r5)       #paste...
        lw t5,0x10(t8)       #copying chain (SizeYSizeX)
        lw t6,0x14(t8)       #copying       (TexPage command???)
        sw t5,0x10(r5)       #paste...
        sw t6,0x14(r5)       #paste...

        #need to make TexPage on First letter!

        lhu        v0,0x4(t4)   #t1->t4         r2 = half (DMA params)+4 (charCounter)
        nop
        bne        v0,zero,NotFirstCharForLastCommand        #     if CharCount<>0 then Goto...
        nop
        lui          v0,0x0600      #6 COMMANDS! ON FIRST LETTER
        sw          v0,0x0(r5)  
        lui        t5, 0xe100
        ori        t5, 0x0234
        sw         t5,0x018(r5)       #store sixth command    e100000234


NotFirstCharForLastCommand:

                        #OR SHIFTED COMMAND

        #NEED TO COPY XY TO T8 TO COPY TO PARAMS!!!! 
        #lw t8,0x8(t8)

###
        lw         v1,0x18(t1)                   #r3 = *r9+18 (next param???)        
        lw         v0,0x0(r5)                   # r2 = *cur New Chainaddr
        lw         v1,0x0(v1)                   # r3 = *r3
        and        v0,v0,t2                     # r2 & ff000000
        and        v1,v1,s0                     #r3 & 00FFFFFF
        or         v0,v0,v1                     #  r2 | r3 = 04FFFFFF - commands with FFFFFF
        sw         v0,0x0(r5)                   #  *curChainAddr r5 = r2 (upd ..04 with ..FF04)
        lw         r4,0x18(t1)                  #  r4 = *r8+18 (first chain addr?)
        nop
        lw         v0,0x0(r4)           #         r2 = *r4 FIRST CHAIN LINK TO NEXT??
        and        v1,r5,s0                     # r3 = r5 (cur chain) & 00FFFFFF
        and        v0,v0,t2                     # r2 & FF000000
        or         v0,v0,v1                     # r2 | r3 = last chain adr w 10&....
        sw         v0,0x0(r4)                   # *r4 = r2 FIRST CHAIN NEXT ADDR???
        
        sw         r5,0x18(t1)  #t0->t1         #    r5 = *r8+18 curChainAddr
        lhu        v0,0x4(t4)   #t1->t4         r2 = half (DMA params)+4 (charCounter)
        nop
        bne        v0,zero,NotFirstChar2        #     if CharCount<>0 then Goto...
        nop
        lui         t2,MyAddr
        lb          v0,-0x8(t2)                  #load offset coords from temp
        nop
        sb         v0,0x3c(r20) #store first char offset for moving string (is first in new type)
        #andi           v0, 0xF #V0 and F = Y offset
        
        

        sw         r5,0x28(t1)  #t0->t1        #*curChain+28 = currentChainAddr
        sw         t7,0xc(t4) #t1->t4            # Params+0c = save word X+Y              STORE COORDS T7!!!!!
        
        

        
NotFirstChar2:

        addiu      t1,t1,0x4                    #         r8 (params) += 4 (shift params for NextPage)
        addiu      v0,t4,0x8                   #          r2 = r9+8 (shift first chain param for Page)
        sltu       v0,t1,v0                     #         r2 = r8 < r2(r9+8) (first page pass)
        bne        v0,zero,PageLoop2            #          if r2<>0 (r8<r2) then goto Next Page Loop
        nop

        j OptimizedSpriteCopyEnd
        nop