;command line: asm bentro3 send  base
 include regs.asm

tricmdsize equ  176 ;112 ;96
tricount equ 12
drawcolor = 1   ;for drawing
;modikbuffer equ $A0500000
base equ   $80000400;$80310000
 org (base-$1000)
 DCB $1000,0
;   OPT M-   ;NO MACROS
    OPT W-   ;NO WARNINGS

 include gfxdefs.asm

    include macros.asm
    include staticm.asm

            
            jal     INITVID  ;initialize video screen
            nop

                li      t4,3-1
                li      t1,$100000
                li      t0,$B0000000+$100000
@wait           lw      t2,$A4600010
                andi    t2,3
                bnez    t2,@wait
                lui     a0,$A460
                sw      t1,0(a0)
                sw      t0,4(a0)
                li      t2,$100000
                sw      t2,$C(a0)
                li      t3,$100000
                addu    t1,t3
                addu    t0,t3
                nop
                sync
                bnez    t4,@wait
                addu    t4,-1
@wait2
                lw      t2,$A4600010
                andi    t2,3
                bnez    t2,@wait2


            LA      sp,STACK
            la      a8,stack2
            jal     storeint
            nop

        li      t0,MI_INTR_MASK_SET_VI  ;only catch vi int
        sw      t0,MI_INTR_MASK_REG

        li      t0,(SR_IBIT3|SR_IE)
        mfc0    t1,C0_SR
        nop
        nop
        or      t1,t0
        mtc0    t0,C0_SR
        nop

            jal     Audio_init
            nop


        la      a0,king1pic
        la      a1,texture
        jal     rncunpack
        nop

                lw      a0,$A0000318
                bne     a0,$800000,@nomem
                la      t0,(sample4-$80000400)+$B0001000
                li      t1,$80500000
                lui     a0,$A460
                sw      t1,0(a0)
                sw      t0,4(a0)
                li      t2,$100000
                sw      t2,$C(a0)
@nomem          
                la      a0,pdxfont
                sw      a0,rncfile
                la      a1,fonts
                sw      a1,unpackdestination
                li      t0,1
                sw      t0,startdepack

START:     

            jal Readcontroller1
            nop
START2:
            jal WTOF            ;wait TOF loop at Beginning
            nop


            jal swapframebuffers
            nop

            jal updatesamplemusic
            nop
; b @exit
            lw          t1,samplefinish
            beqz        t1,@exit
@checkfirst                             
            lw          t0,whatsample
            addu        t0,1
            sw          t0,whatsample
            sw          r0,samplefinish
            beqz        t0,@exit
; bne t0,1,@exit
            beq         t0,2,@play2
            bgt         t0,2,@play3

               ;play sample 2
            la          t1,sample2
            sw          t1,current_sample
            la          t2,(endsample2-sample2)
            sw          t2,end_sample
            la          t0,coronermv
            sw          t0,rncfile
            la          t1,texture
            sw          t1,unpackdestination
            li          t2,1
            sw          t2,startdepack
            

            b   @exit
@play2       ;play sample3

            la          t1,sample3
            sw          t1,current_sample
            la          t2,(endsample3-sample3)
            sw          t2,end_sample
            la          t0,king2pic
            sw          t0,rncfile
            la          t1,texture
            sw          t1,unpackdestination
            li          t2,1
            sw          t2,startdepack
            b           @exit
            nop
@play3     ;play sample4
                lw      t5,$A0000318
                bne     t5,$800000,@nomem
            la          t1,$80500000  ;sample4
            sw          t1,current_sample
            la          t2,(endsample4-sample4)
            sw          t2,end_sample
@nomem
            la          t0,destopprod
            sw          t0,rncfile
            la          t1,texture
            sw          t1,unpackdestination
            li          t2,1
            sw          t2,startdepack
            li          t5,3
            sw          t5,whatsample
@exit
            jal controller
            nop


                la      a0,object1
                la      a1,matrix1
                jal     rotateobject
                nop

                la      a0,object2
                la      a1,matrix1+4*16*4
                jal     rotateobject
                nop
            li   a1,$BFC007C8
            lh   d0,6(a1)
            move d1,d0
            sra  d0,8+2     ;l/r
            la   a0,object2+2*3
            lh   d2,2(a0)   ;yaxis
            addu d2,d0
            andi d2,511
            sh   d2,2(a0)   ;yaxis
            lh   d3,0(a0)   ;xaxis
            sll  d1,24
            sra  d1,24+2    ;u/d
            addu d3,d1
            andi d3,511
            sh   d3,0(a0)   ;xaxis


          la    a0,RDP_start|$A0000000
          sw    a0,glistp



  gSegment             0,0,glistp 
  gDisplaylist         clearscreenDL,glistp

  gSetColorImage       frame2,glistp
  gFillRectangle       0, 0, 319, 239,glistp
        
  gNoOp                glistp
  gPipeSync            glistp
  gSetColorImage       Zimg,glistp
  gSetFillColor        (($3fff>>8)&%11111),(($3fff>>3)&%11111),(($3fff>>2)&%11111),1,glistp
;  gSetFillColor        0,0,0,0,glistp
  gFillRectangle       0, 0, 319, 239,glistp
  gNoOp                glistp
  gPipeSync            glistp
  gSetColorImage       frame2,glistp
  gSetDepthImage       Zimg,glistp

  gSetCycleType        G_CYC_2CYCLE,glistp
  G_RM_AA_XLU_SURF 1
  G_RM_AA_XLU_SURF 2     
  gRenderMode          glistp
  gTexture             $8000,$8000,0,0,G_ON,glistp

;  gTextureRectangle    40,40,32,32,glistp

  gPerspective          perspective,glistp

  G_RM_AA_XLU_SURF 1
  G_RM_AA_XLU_SURF 2     
  gRenderMode          glistp
  gMatrix               projmatrix,G_MTX_PROJECTION|G_MTX_LOAD|G_MTX_NOPUSH,glistp

  gSetGeometryMode     (G_SHADE|G_SHADING_SMOOTH|G_ZBUFFER|G_LIGHTING|G_CULL_BACK),glistp
  gMatrix               matrix1+4*16*4,G_MTX_MODELVIEW|G_MTX_LOAD|G_MTX_NOPUSH,glistp
  gMatrix               matrix1+4*16*5,G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_NOPUSH,glistp
  gMatrix               matrix1+4*16*6,G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_NOPUSH,glistp
  gMatrix               matrix1+4*16*7,G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_NOPUSH,glistp
  gDisplaylist         cube_DL,glistp

  gCleargeometryMode   G_CULL_BACK,glistp
  gMatrix               matrix1,G_MTX_MODELVIEW|G_MTX_LOAD|G_MTX_NOPUSH,glistp
  gMatrix               matrix1+4*16,G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_NOPUSH,glistp
  gMatrix               matrix1+4*16*2,G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_NOPUSH,glistp
  gMatrix               matrix1+4*16*3,G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_NOPUSH,glistp
  gDisplaylist         GRID_DL,glistp
  
  gSpriteInit          glistp

;  gSetPrimcolor 255,255,255,255,glistp
  lw    a0,fontfade
  addu  a0,1
  beql  a0,256,@nomas
  addu  a0,-1
  sw    a0,fontfade
@nomas

  li    t0,4-1
  li    t1,24
  li    t3,0
@shifter
  sllv  t2,a0,t1
  or    t3,t2
  addu  t1,-8
  bnez  t0,@shifter
  addu  t0,-1
  move  a0,t3
  lw    a1,glistp
  jal   DPsetprimcolor
  nop
  sw    a1,glistp

  lw    d5,whatsample
  beq  d5,1,@show2
  beq  d5,2,@show3
  bgt  d5,2,@show4
        li      d0,120
        li      d1,190
        li      d2,5
        li      d3,1
        la      a0,king1t
        jal     placetext
        nop
        lw      t0,fontscale
        addu    t0,$1000
        andi    t0,$3FFFF
        sw      t0,fontscale
        b       nomoretext
@show2  
        lw      d0,fontxpos
        la      a0,sintable
        addu    d0,9
        andi    d0,$1ff
        sw      d0,fontxpos
        sll     d0,2
        addu    a0,d0
        lw      d0,0(a0)
        li      d1,20
        multu   d0,d1
        mflo    d0
        sra     d0,16
        addu    d0,112

        li      d1,190
        li      d2,6
        li      d3,1
        la      a0,coronert
        jal     placetext
        nop
        li      t0,$11000
        sw      t0,fontscale

        b       nomoretext
@show3 
        lw      d0,fontxpos
        la      a0,sintable
        addu    d0,4
        andi    d0,$1ff
        sw      d0,fontxpos
        sll     d0,2
        addu    a0,d0
        lw      d0,0(a0)
        li      d1,80
        multu   d0,d1
        mflo    d0
        sra     d0,16
        addu    d0,112

        li      d1,190
        li      d2,6
        li      d3,1
        la      a0,king2t
        jal     placetext
        nop
        b       nomoretext
@show4
        lw      d0,fontxpos
        la      a0,sintable
        addu    d0,6
        andi    d0,$1ff
        sw      d0,fontxpos
        sll     d0,2
        addu    a0,d0
        lw      d0,0(a0)
        addu    d2,d0,$20000
        sw      d2,fontscale
        li      d1,90
        multu   d0,d1
        mflo    d0
        sra     d0,16
        addu    d0,16

        li      d1,190
        li      d2,18
        li      d3,1
        la      a0,destopt
        jal     placetext
        nop
nomoretext


  gFullSync            glistp
  gEndDisplayList      glistp

           lw   d0,glistp
           lw   d1,glistp0
           subu d2,d0,d1
           sw   d2,(GFXtask+$34)|$A0000000
;           sw   d0,memory+$10*3+3*4


           jal dorsp
           nop

           lw   t0,whatsample
           la   a0,object1
           beqz t0,@dofirst
           beq  t0,1,@dosecond
           beq  t0,2,@dosecond
           bgt  t0,2,@dothird

@dofirst   
           lh   t2,4*2(a0)       ;yaxis
           addu t2,-2
           andi t2,$1ff
           sh   t2,4*2(a0)
           b    @nowave
@dosecond  

           lw   t3,waveamplitude
           addu t3,1
           beq  t3,62,@skipadd
           sw   t3,waveamplitude
@skipadd
           lh   t2,4*2(a0)       ;yaxis
           beqz t2,@dowave  ;only wave
           addu t2,2
           andi t2,$1ff
           sh   t2,4*2(a0)
           b    @dowave
@dothird
           lw   t3,waveamplitude
           addu t3,-1
           beq  t3,30,@skipsub
           sw   t3,waveamplitude
@skipsub
           lh   t2,3*2(a0)       ;xaxis
           addu t2,1
           andi t2,$1ff
           sh   t2,3*2(a0)


@dowave
           jal  wavevtx
           nop
@nowave
           lh   t0,object1+4
           addu t0,60
           bgt  t0,$FFFFFE80,@skipy
           sh   t0,object1+4
@skipy
           lw   t1,cubepath
           la   a0,object2
           beq  t1,1,@path2
           beq  t1,2,@path3
           beq  t1,3,@path4
           lh   t2,0(a0)
           addu t2,3
           sh   t2,0(a0)
           li   t1,0
           blt  t2,$F0,@nochange
           nop
           li   t1,1    ;path2
@path2     lh   t3,2(a0)
           addu t3,-3
           sh   t3,2(a0)
           bgt  t3,$FFFFFF40,@nochange
           nop
           li   t1,2
@path3
           lh   t2,0(a0)
           addu t2,-3
           sh   t2,0(a0)
           bgt  t2,$FFFFFF10,@nochange
           nop
           li   t1,3
@path4     lh   t3,2(a0)
           addu t3,3
           sh   t3,2(a0)
           blt  t3,$d0,@nochange
           nop
           li   t1,0
@nochange  sw   t1,cubepath
           lh   t2,10(a0)
           addu t2,2
           andi t2,$1ff
           sh   t2,10(a0)

            lw d0,watchpoint
           sw   d0,memory+$10*3+3*4

 if (1=0)
            jal cursor2
            nop
            jal cursor
            nop
            jal showmem
            nop
            jal peekmem
            nop
            jal showloc
            nop
            jal showposit
            nop
            jal tellstatus
            nop
 endif

;        b @skipcont

        lw      t0,startdepack
        beqz    t0,@skipstart
        lw      a0,rncfile
        lw      a1,unpackdestination
        sw      r0,startdepack
        li      t0,1
        sw      t0,continuedepack
        jal     rncunpack
        nop
;        li      d0,$DD1
;        sw      d0,watchpoint
        sw      r0,continuedepack
        j       start
        nop
@skipstart
        lw      t1,continuedepack
        beqz    t1,@skipcont
;        li      t2,$BADF
;        sw      t2,watchpoint
;        b @skipcont

        la     t9,rncstack
        lw     at,$0000(t9)
        lw     v0,$0004(t9)
        lw     v1,$0008(t9)
        lw     a0,$000c(t9)
        lw     a1,$0010(t9)
        lw     a2,$0014(t9)
        lw     a3,$0018(t9)
        lw     t0,$001c(t9)
        lw     t1,$0020(t9)
        lw     t2,$0024(t9)
        lw     t3,$0028(t9)
        lw     t4,$002c(t9)
        lw     t5,$0030(t9)
        lw     t6,$0034(t9)
        lw     t7,$0038(t9)
        lw     s0,$003c(t9)
        lw     s1,$0040(t9)
        lw     s2,$0044(t9)
        lw     s3,$0048(t9)
        lw     s4,$004c(t9)
        lw     s5,$0050(t9)
        lw     s6,$0054(t9)
        lw     s7,$0058(t9)
        lw     t8,$005c(t9)
;        lw     t9,$0060(t9)
        lw     sp,$0064(t9)
        lw     s8,$0068(t9)
        lw     ra,$006c(t9)
        lw     t9,-4(t9)
        jr      t9
        nop


@skipcont



             j start
             nop


temp:   dw 0
temp3:  DW whatsample  ;RDP_start          ;where to look at
temp4:  DW whatsample ;RDP_start ;            ;RDP_start 
xstat: DW 0        ;0=peek memory 1=poke memory
alternate: DW 1
position1:  DW 0
position2: DW 0
color:   DW $1E00
color2:  DW $1E00
joypad: DW 0
statusreg: dw 0
glistp0:        dw RDP_start|$A0000000
glistp:         dw RDP_start|$A0000000
donce1: dw 1


watchpoint: dw 0


WTOF:
               lw      d0,vblankdone
;               bnez    d0,@skipv
               lui     a0,$A440     ;wait for TOF
               lw      d1,$10(a0)
               ADDIU   d0,r0,$00
               bne    d0,d1,WTOF

;                b WTOF
               nop
@skipv         sw     r0,vblankdone
               jr      ra
               nop
vblankdone: dw 0


rotateobject:
                ;a0=object
                ;a1=matrix
                or      a1,$A0000000
                lh      t0,0(a0)
                sh      t0,12*2(a1)
                lh      t1,2(a0)
                sh      t1,13*2(a1)
                lh      t2,4(a0)
                sh      t2,14*2(a1)


                addu    a1,32*2
                addu    a0,3*2
                la      a2,sintable
                la      a3,sintable+(128*4)
;x axis
                lh      t0,0(a0)        ;x
                andi    t0,$1ff
                sll     t0,2
                addu    a4,t0,a2
                lw      t1,0(a4)        ;sin
                srl     t3,t1,16
                sh      t3,6*2(a1)
                sh      t1,16*2+6*2(a1)
                negu    t1
                srl     t4,t1,16
                sh      t4,9*2(a1)
                sh      t1,16*2+9*2(a1)

                addu    a5,t0,a3
                lw      t2,0(a5)        ;cos
                srl     t3,t2,16
                sh      t3,5*2(a1)
                sh      t2,16*2+5*2(a1)
                sh      t3,10*2(a1)
                sh      t2,16*2+10*2(a1)

                addu    a1,32*2
;y axis
                lh      t0,2(a0)
                andi    t0,$1ff
                sll     t0,2
                addu    a4,a2,t0
                lw      t1,0(a4)        ;sin
                srl     t2,t1,16

                sh      t2,8*2(a1)
                sh      t1,16*2+8*2(a1)
                negu    t1
                srl     t2,t1,16
                sh      t2,2*2(a1)
                sh      t1,16*2+2*2(a1)
                addu    a4,a3,t0        ;cos
                lw      t1,0(a4)
                srl     t2,t1,16
                sh      t2,0(a1)
                sh      t1,16*2(a1)
                sh      t2,10*2(a1)
                sh      t1,16*2+10*2(a1)

                addu    a1,32*2
;z-axis
                lh      t0,4(a0)
                andi    t0,$1ff
                sll     t0,2
                addu    a4,a2,t0        ;sin
                lw      t1,0(a4)
                srl     t2,t1,16
                sh      t2,1*2(a1)
                sh      t1,16*2+1*2(a1)
                negu    t1
                srl     t3,t1,16
                sh      t3,4*2(a1)
                sh      t1,16*2+4*2(a1)
                addu    a4,a3,t0
                lw      t2,0(a4)
                srl     t3,t2,16
                sh      t3,0(a1)
                sh      t2,16*2(a1)
                sh      t3,5*2(a1)
                sh      t2,16*2+5*2(a1)
                jr      ra
                nop







perspective: dw $11

 include inits.asm
 include rdp.asm
 include controller.asm
 include dumper.asm
 include RSP3d.asm
 include depack.asm
 include effects.asm
 include sprite3.asm
 include text.asm

 cnop 0,8
;box
; include box.asm
  include pic.asm
 
;pyramid
  include cube.asm

  cnop 0,8
clearscreenDL:
  gsViewport            Viewport
  gsCleargeometryMode   (G_SHADE|G_SHADING_SMOOTH|G_CULL_BOTH|G_FOG|G_LIGHTING|G_TEXTURE_GEN|G_TEXTURE_GEN_LINEAR|G_LOD)
  gsTexture             0,0,0,0,G_OFF
  gsSetGeometryMode     (G_SHADE|G_SHADING_SMOOTH|G_ZBUFFER|G_LIGHTING)
  gsSetCycleType        G_CYC_1CYCLE
  gsPipelineMode        G_PM_1PRIMITIVE
  gsSetScissor          0, 0, 320, 240
  gsTextureLOD          G_TL_LOD
  gsTextureLUT          G_TT_NONE
  gsTextureDetail       G_TD_CLAMP
  gsTexturePersp        G_TP_PERSP
  gsTextureFilter       G_TF_BILERP
  gsTextureConvert      G_TC_FILT
  gsCombineKey          G_CK_NONE
  gsAlphaCompare        G_AC_NONE
  gsColorDither         G_CD_ENABLE
  gsSetLights1          lights
  gsCombineMode         \G_CC_TRILERP\,\G_CC_MODULATEI2\
  gsPipeSync            
  gsSetCycleType        G_CYC_FILL
  G_RM_OPA_SURF        1
  G_RM_OPA_SURF        2
  gsRenderMode          
  gsSetFillColor        0,0,0,1
  gsenddisplaylist


 cnop 0,8
PROJMATRIX:
        DW        0x00010000, 0x00000000
        DW        0x00000002, 0x00000000
        DW        0x00000000, 0xFFFEFFFF
        DW        0x00000000, 0xFF350000
 ;       /* fractional portion: */
        DW        0xCF870000, 0x00000000
        DW        0x00006A09, 0x00000000
        DW        0x00000000, 0xF9150000
        DW        0x00000000, 0x4C1C0000

MATRIX1:
   rept 2
        DW        0x00010000, 0x00000000
        DW        0x00000001, 0x00000000
        DW        0x00000000, 0x00010000
        DW        0x00000000, 0x00000001
 ;       /* fractional portion: */
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
;MATRIX2:
        DW        0x00010000, 0x00000000
        DW        0x00000001, 0x00000000
        DW        0x00000000, 0x00010000
        DW        0x00000000, 0x00000001
 ;       /* fractional portion: */
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
;MATRIX4:
        DW        0x00010000, 0x00000000
        DW        0x00000001, 0x00000000
        DW        0x00000000, 0x00010000
        DW        0x00000000, 0x00000001
 ;       /* fractional portion: */
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
;MATRIX5:
        DW        0x00010000, 0x00000000
        DW        0x00000001, 0x00000000
        DW        0x00000000, 0x00010000
        DW        0x00000000, 0x00000001
 ;       /* fractional portion: */
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
        DW        0x00000000, 0x00000000
 CNOP 0,8
 endr

 cnop 0,4
object1:
         dh 0,0,$E000      ;x,y,z
         dh $1d3,0,0       ;rotations

object2:
         dh $FD00,$D0,$FE00       ;x,y,z
         dh 0,0,0       ;rotations
cubepath dw 0
 cnop 0,8
squatbuck
 incbin "squatbuk.raw"
pdxfont
 incbin "pdxfont.rnc"
 obj $80373000
Texture:
 objend

 obj $8036b000
fonts:
 objend

 obj $8036A000
stack
 objend

 OBJ $80350000
stack2
 objend

king1pic
 incbin "king1.rnc"

king2pic
 incbin "abialb.rnc"

coronermv:
 incbin "fs_mv.rnc"

destopprod:
 incbin "cloudy.rnc"

 cnop 0,4
sintable:
 include 32sin.asm


 cnop 0,4
 include samsfx.asm

 obj $802f0000
 CNOP 0,8
RDP_start:
 objend


