TEST            set 0
AnzObjekte      equ 4

                path "e:\bjl\js\symbols\"
                isyms "blit_eq.sym"
                isyms "jagregeq.sym"
                
                path "..\macro\"
                include "help.mac"
                include "joypad1.mac"
                path "e:\newsrc\tetris\"
****************
* global register equates
                include "globlreg.inc"
****************
* vars
leer            equ GPU_ENDRAM-4
LastJoy::       equ leer-8
seed1           equ LastJoy-3*4
stone_stat      equ seed1-7*4
STACK           EQU stone_stat-4
****************

YADD_init       EQU 1

;- ScoreScreen dimensions
max_x           equ 16
max_y           equ 25

;- PlayField dimensions
PFmax_x         equ 10+2-1      ; 2 for border
PFmax_y         equ 24+1-1      ; 1 for border

screen          equ $100000
screen1         equ screen+(PFmax_x+1)<<3*(PFmax_y+1)<<3*2
ScoreScreen     equ screen1+(PFmax_x+1)<<3*(PFmax_y+4)<<3*2

_68kvars        equ  $1000
OBL             equ $9f000
OBL0            equ $9f200
pattern         equ $a0000
Stones          equ $b0000
Maske           equ $c0000
PlayField       equ $f0000+(PFmax_x+1)<<4

PlayFieldTop    equ PlayField+(PFmax_x+1)*(PFmax_y+1)*4-4


*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* PART "MACRO-definitions
MACRO WaitVBL
                xor FLAG,FLAG
;               store FLAG,(BORDER)             ; for test purpose
                cmpq #0,FLAG
.\_wait         jr z,.\_wait
                cmpq #0,FLAG
;               store FLAG,(BORDER)
ENDM

MACRO WAITBLITTER
.\wait@         load (blitter+$38),r0
                shrq #1,r0
                jr cc,.\wait@
                nop
ENDM

MACRO MyINITLSUB
                WAITBLITTER
                movei #LSUBrun_\0+$8000,r0      ; dest-adr
                movei #LSUBstart_\0,r1
                store r0,(blitter)
                store r1,(blitter+$24)
                movei #BLIT_PITCH1|BLIT_PIXEL8|BLIT_WID320|BLIT_XADDPHR,r0
                xor r1,r1
                store r0,(blitter+4)
                store r0,(blitter+$28)
                store r1,(blitter+$c)
                store r1,(blitter+$18)
                store r1,(blitter+$30)

                movei #1<<16|(LSUBlen_\0),r0
                movei #BLIT_SRCEN|BLIT_LFU_REPLACE|BLIT_BUSHI*0,r1
                store r0,(blitter+$3c)
                store r1,(blitter+$38)
                WAITBLITTER
ENDM
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                IF TEST
                run $4000
                ibytes "tetris.inl"             ; get 68k-prg
                dc.l 0,start,ende-start         ; simulate Header
                align 4                                         ; better is that ?!(
                ENDIF

                run $98000

start           INITLSUB INIT
                movei #init,r0
                jump (r0)
                nop
****************
IMODULE IRQ_SUB                 ; pre-assembled IRQ-routs
****************
* LSUB MAIN    *
LSUB MAIN,MODend_IRQ_SUB

main::

                movei #1<<14|%11111<<9|%01100<<4,r0
                store r0,(status)
                nop
                nop
                movei #$f00026,r0
                storew r1,(r0)


                MyINITLSUB NEWGAME

                movei #ClearPf,r0
                JSR (r0)
                MyINITLSUB GAME
;>                movei #ClearSecond,r0
;>                JSR (r0)
                movei #draw_pf,r0
                JSR (r0)
main1           MyINITLSUB NEWGAME
                CALL NEWGAME
                MyINITLSUB GAME
                CALL GAME
                movei #main1,r0
                jump (r0)
                nop
                include "textscr.inc"
****************
IncPoints::
****************
                movefa Points.a,r1
                add r1,r0
                moveta r0,Points.a
****************
ShowPoints::
****************
                PUSH r2
                movei #8<<16|6,r1
                movefa Points.a,r0
                moveta r1,Cursor.a
                movefa High.a,r1
                cmp r1,r0
                jr n,.cont      ; points<high =>
                moveq #0,r2
                moveta r0,High.a
                subq #1,r2      ; set flag
.cont           movei #PrintDez6,r1
                JSR (r1)
                movei #8<<16|8,r0
                cmpq #0,r2
                POP r2
                movei #PrintDez6,r1
                moveta r0,Cursor.a
                jump nz,(r1)
                movefa High.a,r0
.cont1          RTS
***************
ShowHigh::
                movei #8<<16|8,r0
                movei #PrintDez6,r1
                moveta r0,Cursor.a
                jump (r1)
                movefa High.a,r0
****************
IncLines::
****************
                movefa Lines.a,r0
                movefa LinesToGo.a,r1
                addq #1,r0
                subq #1,r1
                moveta r0,Lines.a
                jr nz,.cont
                moveta r1,LinesToGo.a
                                movei #IncLevel,r0
                                PUSH r0
.cont           
****************
ShowLines::
****************
                movei #10<<16|4,r0
                movei #PrintDez3,r1
                moveta r0,Cursor.a
                jump (r1)
                movefa Lines.a,r0
****************
IncLevel::
****************
                movefa Level.a,r0
                moveq #10,r1
                subq #1,r0
                moveta r1,LinesToGo.a
                jr nz,.cont
                moveta r0,Level.a
                                addq #1,r0
                                moveta r0,Level.a
.cont
****************
ShowLevel::
                movefa Level.a,r0
                movei #"0"+9,r1
                neg r0
                add r1,r0
                movei #12<<16|2,r1
                moveta r1,Cursor.a
                movei #PrintChar,r1
                jump (r1)
                nop
****************
*   ShowTime   *
****************
Minutes         REG 3
Seconds         REG 2

ShowTime::      PUSH Seconds,Minutes
                movefa sec.a,r0
                movei #60,r1
                move r0,Seconds ; sec merken
                div r1,r0
                move r0,Minutes
                mult r1,r0
                sub r0,Seconds
                movei #PrintDez2,r1
                movei #8<<16,r0
                moveta r0,Cursor.a
                move Minutes,r0
                JSR (r1)
                movei #11<<16,r0
                moveta r0,Cursor.a
                move Seconds,r0
                JSR (r1)
                POP Minutes,Seconds
                RTS
UNREG Minutes,Seconds
****************
*    RANDOM    *
PTR             REG 14
SEED3           REG 6
SEED2           REG 5
SEED1           REG 4

Random::        PUSH r2,r3,r4,r5,r6,r14
                movei #seed1,PTR
                load (PTR),r0
                move r0,SEED1   ; retten
                movei #206,r2
                div r2,r0       ; r0 = (seed1 / 206)
                sub r0,SEED1    ; r2 = (seed1 mod 206)
                movei #157,r2
                moveq #21,r3
                mult r2,SEED1   ; r2 = r2*157
                mult r3,r0      ; r0 = r0*21
                sub r0,SEED1
                jr nn,.cont
                nop
                                movei #206*157,r0
                                add r0,SEED1
.cont           store SEED1,(PTR)
                load (PTR+4),r0
                move r0,SEED2
                movei #217,r2
                div r2,r0
                sub r0,SEED2
                movei #146,r2
                movei #45,r3
                mult r2,SEED2
                mult r3,r0
                sub r0,SEED2
                jr nn,.cont1
                nop
                                movei #31727,r0
                                add r0,SEED2
.cont1          store SEED2,(PTR+4)
                load (PTR+8),r0
                move r0,SEED3
                movei #222,r2
                div r2,r0
                sub r0,SEED3
                movei #142,r2
                movei #133,r3
                mult r2,SEED3
                mult r3,r0
                sub r0,SEED3
                jr nn,.cont2
                nop
                                movei #31657,r0
                                add r0,SEED3
.cont2          store SEED3,(PTR+8)
                movei #706,r0
                sub SEED2,SEED1
                movei #32362,r1
                cmp SEED1,r0    ; SEED1-SEED2>706
                jr nn_nz,.cont3
                add SEED3,SEED2
                                sub r1,SEED2
.cont3          cmpq #1,SEED2
                jr nn,.cont4
                nop
                                add r1,SEED2
.cont4          move SEED2,r0
                POP r14,r6,r5,r4,r3,r2
                RTS
UNREG PTR,SEED1,SEED2,SEED3
****************
*   NewStone   *
NewStone::      movei #Random,r3
.loop           JSR (r3)
                shlq #29,r0
                shrq #29,r0
                cmpq #7,r0
                jr nn,.loop
                moveta r0,StonePtr2.a
                move r0,r2
                shlq #2,r0
                movei #stone_stat,r1
                add r0,r1
                load (r1),r0
                addq #1,r0
                store r0,(r1)
                moveq #0,r0
                moveta r0,y_pos.a
                moveta r0,StonePtr.a
                movei #40,r0
                moveta r0,x_pos.a
                movei #steine,r1
                shlq #5,r2
                add r2,r1
                addq #4,r1
                load (r1),r0
                neg r0
                moveta r0,y_pos.a
                moveq #YADD_init,YADD
;RTS
****************
*   ShowStat   *
SUM             REG 7
LOOP            REG 6
PRINTDEZ3       REG 5
TXT_Y           REG 4
COUNTER         REG 3
PTR             REG 2
ShowStat::      PUSH r2,r3,r4,r5,r6,r7
                movei #stone_stat,PTR
                movei #PrintDez3,PRINTDEZ3
                movei #.loop,LOOP
                moveq #7,COUNTER
                moveq #10,TXT_Y
                moveq #0,SUM
                moveq #10,r1
.loop           load (PTR),r0
                shlq #16,r1
                add r0,SUM
                or TXT_Y,r1
                addq #2,TXT_Y
                moveta r1,Cursor.a
                JSR (PRINTDEZ3)
                subq #1,COUNTER
                addqt #4,PTR
                jump nz,(LOOP)
                moveq #10,r1
                movei #8<<16,r1
                move SUM,r0
                or TXT_Y,r1
                moveta r1,Cursor.a
                movei #PrintDez6,r1
                JSR (r1)
                POP r7,r6,r5,r4,r3,r2
                RTS
UNREG LOOP,PRINTDEZ3,TXT_Y,COUNTER,PTR,SUM
****************
ENDLSUB MAIN
****************
*    NEWGAME   *
* level selection
                LSUB NEWGAME,LSUBend_MAIN
                include "newgame.inc"
                ENDLSUB NEWGAME
****************
*     game     *
                include "game.inc"
****************
* LSUB  INIT   *
                LSUB INIT,$f03e00
;---------------
;-     init    -
;---------------
init::          movei $f02100,status
                moveta status,status
                movei #1<<14|%11111<<9,r1             ; Timer + OP-IRQs erlauben
                store r1,(status); REGPAGE=1; no IRQs
                nop
                nop                                                             ; wait
                moveta status,IRQ_FLAGADR.a
                movei #STACK,SP
                moveta SP,IRQ_SP.a
                move SP,r31
                subq #32,SP
                movei #$f0211c,r0
                moveq #0,r1
                store r1,(r0)   ; 32bit division

* setup Random seeds
                movei #seed1,r0
                movei #$f00004,r1
                load (r1),r1
                shrq #17,r1
                store r1,(r0)
                addq #4,r0
                store r1,(r0)
                addq #4,r0
                store r1,(r0)
* init some regs
                movei #screen,screen_ptr
                movei #screen1,screen1_ptr
                movei #ScoreScreen,r0
                moveta r0,ScreenPtr.a
                movei #pattern,pattern_ptr
                movei #$f0002a,BORDER
                movei #joypad,JOYPAD
                movei #$f02200,blitter
                moveta blitter,blitter.a

                moveq #4,r0
                moveta r0,LastLevel.a
                moveq #0,r0
                moveta r0,StonePtr.a
                moveta r0,StonePtr2.a
                moveta r0,LinesToGo.a
                moveta r0,Lines.a
                moveta r0,Points.a
                moveta r0,High.a
                moveta r0,x_pos.a
                moveta r0,y_pos.a
                moveq #0,X_Flag
                moveq #0,Y_Flag
                moveq #YADD_init,YADD
                moveq #0,PauseFlag
;---------------
;- check VI-frequenz
;---------------
                movei #$f14002,r0
                loadw (r0),r1
                movei #160-1,r0
                btst #4,r1
                jr z,.cont
                nop
                                subq #160-133,r0
.cont           moveta r0,DropInit0.a
;---------------
;- Init ms-Timer
                movei #$f00050,r0
                movei #26590<<16,r1
                moveq #0,r2
                store r1,(r0)   ; Start
                moveta r2,sec.a
                moveq #20,r0
                moveta r0,_50Hz.a
;---------------
;- init score-screen
;---------------
clr             reg 2
ptr             reg 1
count           reg 0

                movefa ScreenPtr.a,ptr
                xor clr,clr
                movei #max_x*max_y<<1-1,count

                store clr,(ptr)
.loop           subq #1,count
                addqt #4,ptr
                jr nz,.loop
                store clr,(ptr)
                moveta clr,Cursor.a
                movei #$f00400,ptr
                movei #$ff,clr
                store clr,(ptr)                 ; set color
unreg clr,ptr,count
;---------------                
                INITMODULE IRQ_SUB
                INITLSUB MAIN

                movei #main,r0
                jump (r0)
                nop
                ENDLSUB INIT
****************
* extern DATA  *
****************

                align 32
steine::
; MMM
; M
                dc.l -8,-8
                dc.l -8,-8
                dc.l -16,-8
                dc.l -8,-16
; M
; MMM
                dc.l -16,-8
                dc.l -8,-16
                dc.l -8,-8
                dc.l -8,-8
;  MM
; MM
                dc.l -8,-8
                dc.l -8,-8
                dc.l -16,-8
                dc.l -8,-16
; MM
;  MM
                dc.l -8,-8
                dc.l -8,-8
                dc.l -16,-8
                dc.l -8,-16
; MMMM
                dc.l -24,-8
                dc.l 0,-24
                dc.l -8,0
                dc.l -8,-8
;  M
; MMM
                dc.l -8,-8
                dc.l -8,-8
                dc.l -16,-8
                dc.l -8,-16
; MM
; MM
                dc.l -8,-8
                dc.l -8,-8
                dc.l -8,-8
                dc.l -8,-8
; ASCII-Tabelle

                ALIGN 8
ASCII           ibytes "\newsrc\tetris\light8x8.fnt"

if 0
                ALIGN 8
NewASCII
                dc.b %11000000
                dc.b %11000000
                dc.b 0,0
                dc.b 0,0
                dc.b %11111100
                dc.b %11111100


                dc.b %00001100
                dc.b %00001100
                dc.b 0,0
                dc.b 0,0
                dc.b %11111100
                dc.b %11111100

                dc.b %11110000
                dc.b %11110000
                dc.b 0,0
                dc.b 0,0
                dc.b %00111100
                dc.b %00111100

                dc.b %00111100
                dc.b %00111100
                dc.b 0,0
                dc.b 0,0
                dc.b %11110000
                dc.b %11110000

                dc.b %11111111
                dc.b 0,0,0
                dc.b 0,0,0
                dc.b %11111111

                dc.b %00110000
                dc.b %00110000
                dc.b 0,0
                dc.b 0,0
                dc.b %11111100
                dc.b %11111100

                dc.b %11110000
                dc.b %11110000
                dc.b 0,0
                dc.b 0,0
                dc.b %11110000
                dc.b %11110000
endif
                ALIGN 4
ScoreTxt        dc.b "Time:   00:00   "
                dc.b "                "
                dc.b "Level:      0   "
                dc.b "                "
                dc.b "Lines:    000   "
                dc.b "                "
                dc.b "Points: 00000   "
                dc.b "                "
                dc.b "High:   00000   "
                dc.b "                "
                dc.b "     ",1,"    000   "
                dc.b "                "
                dc.b "     ",2,"    000   "
                dc.b "                "
                dc.b "     ",3,"    000   "
                dc.b "                "
                dc.b "     ",4,"    000   "
                dc.b "                "
                dc.b "     ",5,"    000   "
                dc.b "                "
                dc.b "     ",6,"    000   "
                dc.b "                "
                dc.b "     ",7,"    000   "
                dc.b "                "
                dc.b "total : 00000   "
                dc.b 0
                ALIGN 4
AskLevel        dc.b "Please Choose   "
                dc.b "                "
                dc.b "Level:     [0]  "
                dc.b 0
                ALIGN 4

                dc.b 0
ende
****************
END

