DrawCharLine_19000:
    addiu      sp, sp, -0x50
    sw         s6, 0x40(sp)
    move       s6, a0
    sw         s1, 0x2c(sp)
    move       s1, a1
    sw         s2, 0x30(sp)
    move       s2, a2
    sw         s3, 0x34(sp)

    lui        s3, 0x1f80
    ori        s3, s3, 0x3d0
    
    lui        t0, 0xff
    ori        t0, t0, 0xffff
    sw         s0, 0x28(sp)
    andi       s0, a3, 0xff
    move       a0, s0
    
InitDMA:
    lui        a3, 0x8008
    lui        a2, 0xff00
    addiu      t1, a3, -0x6c20
    lw         v1, -0x6c20(a3)
    lbu        a1, 0x60(sp)
    lui        v0, 0x8000
    
    sw         ra, 0x4c(sp)
    sw         s8, 0x48(sp)
    sw         s7, 0x44(sp)
    sw         s5, 0x3c(sp)
    sw         s4, 0x38(sp)

    and        a2, v1, a2
    andi       a1, a1, 0xff
    and        v1, v1, t0
    or         s4, v1, v0

    lw         v0, 0x4(t1)
    lw         v1, 0x0(s4)
    addiu      v0, v0, -0x1
    and        v1, v1, t0
    or         a2, a2, v1
    sw         v0, 0x4(t1)

    jal        FUN_8001adc8
    sw         a2, -0x6c20(a3)
    move       a0, s4

    sll        a1, s1, 0x10
    sra        a1, a1, 0x10
    sll        a2, s2, 0x10
    sra        a2, a2, 0x10
    jal        FUN_80019d70
    move       a3, s0

; Настройка GPU регистров
SetupGPURegisters:
    lui        a3, 0x1f80
    ori        a3, a3, 0x348
    lui        a2, 0x1f80
    ori        a2, a2, 0x38c

    li         v0, 0x3
    sh         v0, 0x1c(sp)
    li         v0, 0x6
    li         a1, 0xd
    lui        a0, 0xa000

; GPU init in scratch
ScratchCPUtoVRAMinit:
    sh         v0, 0x1e(sp)
    sb         a1, 0x3(a3)
    sw         a0, 0x4(a3)

    lhu        t0, 0x9c(gp)
    lhu        v0, 0x9e(gp)
    lui        v1, 0x100

    lui        at, 0x1f80
    sw         v1, 0x37c(at)

    sb         a1, 0x3(a2)
    sw         a0, 0x4(a2)

calculateCoords:
    lh         a1, 0x9c(gp)
    move       s8, s2
    lui        at, 0x1f80
    sw         v1, 0x3c0(at)

    sh         t0, 0x18(sp)
    sh         v0, 0x1a(sp)
    lw         a0, 0x18(sp)
    addiu      v0, v0, 0x6
    sh         v0, 0x1a(sp)

    lw         v0, 0x1c(sp)
    lw         v1, 0x18(sp)
    move       t0, a1
    sw         v0, 0xc(a3)
    sw         v0, 0xc(a2)

    lui        v0, 0x8001
    sw         a0, 0x8(a3)
    sw         v1, 0x8(a2)


;4bit pattern in scratch
    lui        t8, 0x1f80
    ori        t8, t8, 0x2e0
    addiu      t7, v0, 0x3e8
    lwl        t5, 0x3(t7)
    lwr        t5, 0x0(t7)
    lwl        t6, 0x7(t7)
    lwr        t6, 0x4(t7)
    swl        t5, 0x3(t8)
    swr        t5, 0x0(t8)
    swl        t6, 0x7(t8)
    swr        t6, 0x4(t8)
    lwl        t5, 0xb(t7)
    lwr        t5, 0x8(t7)
    lwl        t6, 0xf(t7)
    lwr        t6, 0xc(t7)
    swl        t5, 0xb(t8)
    swr        t5, 0x8(t8)
    swl        t6, 0xf(t8)
    swr        t6, 0xc(t8)
    lwl        t5, 0x13(t7)
    lwr        t5, 0x10(t7)
    lwl        t6, 0x17(t7)
    lwr        t6, 0x14(t7)
    swl        t5, 0x13(t8)
    swr        t5, 0x10(t8)
    swl        t6, 0x17(t8)
    swr        t6, 0x14(t8)
    lwl        t5, 0x1b(t7)
    lwr        t5, 0x18(t7)
    lwl        t6, 0x1f(t7)
    lwr        t6, 0x1c(t7)
    swl        t5, 0x1b(t8)
    swr        t5, 0x18(t8)
    swl        t6, 0x1f(t8)
    swr        t6, 0x1c(t8)

; Copy Sprite DMA Command Forming
    li         v0, 0x4
    sb         v0, 0x3(s3)
    li         v0, 0x64
    sb         v0, 0x7(s3)
    li         v0, 0x80
    sb         v0, 0x4(s3)
    sb         v0, 0x5(s3)
    sb         v0, 0x6(s3)

    bgez       a1, LAB_800191fc
    sh         s1, 0x20(sp)

AddOffsetX:
    addiu      t0, a1, 0x3f

LAB_800191fc:
    sra        v0, t0, 0x6
    sll        v0, v0, 0x6
    subu       v0, a1, v0
    lh         v1, 0x9e(gp)
    sll        v0, v0, 0x2
    sb         v0, 0xc(s3)

    bgez       v1, LAB_80019220
    move       v0, v1

AddOffsetY:
    addiu      v0, v1, 0xff

LAB_80019220:
    sra        v0, v0, 0x8
    sll        v0, v0, 0x8
    subu       v0, v1, v0
    sb         v0, 0xd(s3)

    lhu        v1, 0xa4(gp)
    li         v0, 0xc
    sh         v0, 0x10(s3)
    sh         v0, 0x12(s3)
    sh         v1, 0xe(s3)

LoadCharacter:
    lhu        a0, 0x0(s6)
    nop
    sltiu      v0, a0, 0x1000
    beq        v0, zero, LAB_80019518
    addiu      s6, s6, 0x2

; Подготовка к отрисовке символа
    lui        s1, 0x8008
    addiu      s5, s1, -0x6c20
    lui        s0, 0xff
    ori        s0, s0, 0xffff
    lui        s7, 0x8000

LAB_8001926c:
    jal        MakeCharInScratch
    clear      s2

; Настройка DMA для символа
SetupDMAForChar:
    lw         v0, 0xac(gp)
    lhu        v1, 0x4(s4)
    addiu      v0, v0, 0xc
    mult       v1, v0
    lui        t2, 0xff00
    move       t4, s4
    addiu      t3, sp, 0x10
    move       t1, s4

    sh         s8, 0xa(s3)
    lhu        t5, 0x20(sp)
    mflo       t6
    addu       v0, t5, t6
    sh         v0, 0x8(s3)

LAB_800192a8:
    lw         v0, -0x6c20(s1)
    nop
    and        a0, v0, t2
    and        v0, v0, s0
    or         a1, v0, s7
    lw         v0, 0x4(s5)
    lw         v1, 0x0(a1)
    addiu      v0, v0, -0x1
    and        v1, v1, s0
    or         a0, a0, v1
    sw         v0, 0x4(s5)
    sw         a0, -0x6c20(s1)


    lw         t7, 0x0(s3)
    lw         t8, 0x4(s3)
    lw         t5, 0x8(s3)
    lw         t6, 0xc(s3)

    sw         t7, 0x0(a1)
    sw         t8, 0x4(a1)
    sw         t5, 0x8(a1)
    sw         t6, 0xc(a1)

    lw         t7, 0x10(s3)
    nop
    sw         t7, 0x10(a1)

; Подготовка адресов в DMA-цепочке в RAM
DMAcharAddres:
    lw         v1, 0x18(t1)
    lw         v0, 0x0(a1)
    lw         v1, 0x0(v1)
    and        v0, v0, t2
    and        v1, v1, s0
    or         v0, v0, v1
    sw         v0, 0x0(a1)

    lw         a0, 0x18(t1)
    nop
    lw         v0, 0x0(a0)
    and        v1, a1, s0
    and        v0, v0, t2
    or         v0, v0, v1
    sw         v0, 0x0(a0)
    sw         a1, 0x18(t1)


    lhu        v0, 0x4(t4)
    nop
    bne        v0, zero, LAB_80019354
    clear      t0

    sw         a1, 0x28(t1)

LAB_80019354:
    move       a3, t0
    move       a2, t3

LAB_8001935c:
    lui        a1, 0x1f80
    lw         v0, -0x6c20(s1)
    ori        a1, a1, 0x348
    and        v0, v0, s0
    or         v0, v0, s7
    sw         v0, 0x0(a2)
    lw         v1, -0x6c20(s1)
    lw         v0, 0x0(v0)
    lw         a0, 0x4(s5)
    and        v1, v1, t2
    and        v0, v0, s0
    or         v1, v1, v0
    addiu      a0, a0, -0x1
    sw         v1, -0x6c20(s1)
    sw         a0, 0x4(s5)
    lw         a0, 0x0(a2)
    addu       v1, a3, a1
    or         v0, v1, a0
    andi       v0, v0, 0x3
    beq        v0, zero, LAB_80019404
    addiu      v0, v1, 0x30

LAB_800193b0:
    lwl        t7, 0x3(v1)
    lwr        t7, 0x0(v1)
    lwl        t8, 0x7(v1)
    lwr        t8, 0x4(v1)
    lwl        t5, 0xb(v1)
    lwr        t5, 0x8(v1)
    lwl        t6, 0xf(v1)
    lwr        t6, 0xc(v1)
    swl        t7, 0x3(a0)
    swr        t7, 0x0(a0)
    swl        t8, 0x7(a0)
    swr        t8, 0x4(a0)
    swl        t5, 0xb(a0)
    swr        t5, 0x8(a0)
    swl        t6, 0xf(a0)
    swr        t6, 0xc(a0)
    addiu      v1, v1, 0x10
    bne        v1, v0, LAB_800193b0
    addiu      a0, a0, 0x10
    j          LAB_80019430
    nop

LAB_80019404:
    lw         t7, 0x0(v1)
    lw         t8, 0x4(v1)
    lw         t5, 0x8(v1)
    lw         t6, 0xc(v1)
    sw         t7, 0x0(a0)
    sw         t8, 0x4(a0)
    sw         t5, 0x8(a0)
    sw         t6, 0xc(a0)
    addiu      v1, v1, 0x10
    bne        v1, v0, LAB_80019404
    addiu      a0, a0, 0x10

LAB_80019430:
    lwl        t7, 0x3(v1)
    lwr        t7, 0x0(v1)
    lwl        t8, 0x7(v1)
    lwr        t8, 0x4(v1)
    swl        t7, 0x3(a0)
    swr        t7, 0x0(a0)
    swl        t8, 0x7(a0)
    swr        t8, 0x4(a0)
    addiu      a3, a3, 0x44
    addiu      t0, t0, 0x1
    sltiu      v0, t0, 0x2
    bne        v0, zero, LAB_8001935c
    addiu      a2, a2, 0x4
    lw         a0, 0x10(sp)
    lw         v1, 0x18(t1)
    lw         v0, 0x0(a0)
    lw         v1, 0x0(v1)
    and        v0, v0, t2
    and        v1, v1, s0
    or         v0, v0, v1
    sw         v0, 0x0(a0)
    lw         a0, 0x18(t1)
    lw         v1, 0x10(sp)
    lw         v0, 0x0(a0)
    and        v1, v1, s0
    and        v0, v0, t2
    or         v0, v0, v1
    sw         v0, 0x0(a0)
    lw         a0, 0x14(sp)
    lw         v1, 0x10(sp)
    lw         v0, 0x0(a0)
    lw         v1, 0x0(v1)
    and        v0, v0, t2
    and        v1, v1, s0
    or         v0, v0, v1
    sw         v0, 0x0(a0)
    lw         a0, 0x10(sp)
    lw         v1, 0x14(sp)
    lw         v0, 0x0(a0)
    and        v1, v1, s0
    and        v0, v0, t2
    or         v0, v0, v1
    sw         v0, 0x0(a0)
    lw         v0, 0x14(sp)
    addiu      s2, s2, 0x1
    sw         v0, 0x18(t1)
    sltiu      v0, s2, 0x2
    bne        v0, zero, LAB_800192a8
    addiu      t1, t1, 0x4
    lhu        v0, 0x4(s4)
    nop
    addiu      v0, v0, 0x1
    sh         v0, 0x4(s4)
    lhu        a0, 0x0(s6)
    nop
    sltiu      v0, a0, 0x1000
    bne        v0, zero, LAB_8001926c
    addiu      s6, s6, 0x2

LAB_80019518:
    clear      s2
    lui        s3, 0x8008
    addiu      s5, s3, -0x6c20
    lui        s1, 0xff
    ori        s1, s1, 0xffff
    move       s0, s4
    clear      a1

LAB_80019534:
    li         a2, 0x1
    addu       s2, s2, a2
    lui        v0, 0x8000
    lui        a0, 0xff00
    lw         v1, -0x6c20(s3)
    lw         a3, 0xa0(gp)
    and        v1, v1, s1
    or         v1, v1, v0
    sw         v1, 0x30(s0)
    
    lw         v0, -0x6c20(s3)
    lw         v1, 0x0(v1)
    and        v0, v0, a0
    and        v1, v1, s1
    lw         a0, 0x4(s5)
    or         v0, v0, v1
    sw         v0, -0x6c20(s3)
    addiu      a0, a0, -0x1
    sw         a0, 0x4(s5)

    lw         a0, 0x30(s0)
    jal        SetDrawTPage
    addiu      s0, s0, 0x4

    sltiu      v0, s2, 0x2
    bne        v0, zero, LAB_80019534
    clear      a1

    move       v0, s4
    lw         ra, 0x4c(sp)
    lw         s8, 0x48(sp)
    lw         s7, 0x44(sp)
    lw         s6, 0x40(sp)
    lw         s5, 0x3c(sp)
    lw         s4, 0x38(sp)
    lw         s3, 0x34(sp)
    lw         s2, 0x30(sp)
    lw         s1, 0x2c(sp)
    lw         s0, 0x28(sp)

    jr         ra
    addiu      sp, sp, 0x50