	jmp notnedded

	; work with original background

    ;  LEA    _custom,a0               ; Get base address of custom hardware...
    ;  MOVE.L $21000,BPL1PTH(a0)      ; Write bit-plane 1 pointer
    ;  MOVE.L $25000,BPL2PTH(a0)      ; Write bit-plane 2 pointer


        LEA    $dff000,a0        ; Point at bit-plane
        MOVE.W #100,d0         ; Write 2000 longwords = 8000 bytes
ZLOOP:   MOVE.L #0,(a0)+         ; Write out a zero
        dbra   d0,ZLOOP          ; Decrement counter and loop until done


zapwait:

	move.w    $DFF006,$DFF180 ; background


	btst	#6,$bfe001 ; test left mouse left mouse click
	bne	zapwait       ; if not pressed jump to wait

notnedded:



	jmp notjump

	move.w	#$4000,$dff09a ; INTENA - c	lear external interrupt

	or.b	#%10000000,$bfd100 ; CIABPRB stops drive mot	ors
	and.b	#%10000111,$bfd100 ; CIABPRB

	move.w	#$01a0,$dff096 ; DMACON c	lear bitplane, copper, sprite

	move.w	#$1200,$dff100 ; BPLCON0 one bitplane, col	or burst
	move.w	#$0000,$dff102 ; BPLCON1 scroll
	move.w	#$003f,$dff104 ; BPLCON2 video
	move.w	#0,$dff108     ; BPL1MOD bitplane modulo odd planes
	move.w	#0,$dff10a     ; BPL2MOD bitplane modulo even planes
	move.w	#$2c81,$dff08e ; DIWSTRT upper left c	orner of display ($81,$2c)
	move.w	#$f4c1,$dff090 ; DIWSTOP enable PAL trick
	move.w	#$38c1,$dff090 ; DIWSTOP lower right c	orner of display ($1c1,$12c)
	move.w	#$0038,$dff092 ; DDFSTRT Data fetch start
	move.w	#$00d0,$dff094 ; DDFSTOP Data fetch stop

zwait:

	btst	#6,$bfe001 ; test left mouse left mouse click
	bne	zwait       ; if not pressed jump to wait

notjump:

	jmp nosprite
; mc0501
	move.w	#$4000,$dff09a ; INTENA - c	lear external interrupt

	or.b	#%10000000,$bfd100 ; CIABPRB stops drive mot	ors
	and.b	#%10000111,$bfd100 ; CIABPRB

	move.w	#$01a0,$dff096 ; DMACON c	lear bitplane, copper, sprite

	move.w	#$1200,$dff100 ; BPLCON0 one bitplane, col	or burst
	move.w	#$0000,$dff102 ; BPLCON1 scroll
	move.w	#$003f,$dff104 ; BPLCON2 video
	move.w	#0,$dff108     ; BPL1MOD bitplane modulo odd planes
	move.w	#0,$dff10a     ; BPL2MOD bitplane modulo even planes
	move.w	#$2c81,$dff08e ; DIWSTRT upper left c	orner of display ($81,$2c)
	move.w	#$f4c1,$dff090 ; DIWSTOP enable PAL trick
	move.w	#$38c1,$dff090 ; DIWSTOP lower right c	orner of display ($1c1,$12c)
	move.w	#$0038,$dff092 ; DDFSTRT Data fetch start
	move.w	#$00d0,$dff094 ; DDFSTOP Data fetch stop

	lea.l	sprite,a1 ; put sprite 	address into a1
	lea.l	ucopper,a2 ; put copper 	address into a2
	move.l	a1,d1     ; 	move sprite 	address into d1
	move.w	d1,6(a2)  ; transfer sprite 	address high to copper
	swap	d1        ; 	swap
	move.w	d1,2(a2)  ; transfer sprite 	address low to copper

	lea.l	blanksprite,a1 ; put blanksprite 	address into a1
	lea.l	copper,a2      ; put copper 	address into a2
	add.l	#10,a2         ; 	add 10 to copper 	address in a2
	move.l	a1,d1          ; 	move blanksprite 	address into d1
	moveq	#6,d0          ; setup sprite counter

sprcoploop:            ; set all 7 sprite pointers
	swap	d1             ; high 	and low to point to blanksprite 
	move.w	d1,(a2)
	addq.l	#4,a2
	swap	d1
	move.w	d1,(a2)
	addq.l	#4,a2
	dbra	d0,sprcoploop  ; loop trough all 7 sprite pointers

	lea.l	uuscreen,a1      ; put screen 	address into a1
	lea.l	ubplcop,a2      ; put bplcop 	address into a2
	move.l	a1,d1          ; transfer screen 	address to bplcop
	move.w	d1,6(a2)
	swap	d1
	move.w	d1,2(a2)

	lea.l	ucopper,a1      ; put copper 	address into a1
	move.l	a1,$dff080     ; COP1LCH (also sets COP1LCL)
	move.w	$dff088,d0     ; COPJMP1 
	move.w	#$81a0,$dff096 ; DMACON set bitplane, copper, sprite

uwait:              ; wait until at beam line 0
	move.l	$dff004,d0 ; read VPOSR 	and VHPOSR into d0 as one long w	ord
	asr.l	#8,d0      ; shift right 8 places
	and.l	#$1ff,d0
	cmp.w	#0,d0
	bne	uwait       ; if not equal jump to wait

uwait2:             ; wait until at beam line 1
	move.l	$dff004,d0 ; read VPOSR 	and VHPOSR into d0 as one long w	ord
	asr.l	#8,d0
	and.l	#$1ff,d0
	cmp.w	#1,d0
	bne	uwait2      ; if not equal jump to wait

	bsr		movesprite ; branch to subroutine 	movesprite

	btst	#6,$bfe001 ; test left mouse left mouse click
	bne	uwait       ; if not pressed jump to wait

	move.w	#$0080,$dff096 ; reestablish DMA's 	and copper

	move.l	$04,a6
	move.l	156(a6),a1
	move.l	38(a1),$dff080

	move.w	#$8080,$dff096

	move.w	#$c000,$dff09a
	

	movesprite:         ; 	movesprite subroutine
	lea.l	sprite,a1
	cmp.b	#250,2(a1)  ; sprite bottom line at 250
	bne	notbottom   ; if not go to notbottom

	move.b	#30,(a1)
	move.b	#44,2(a1)

notbottom:
	add.b	#1,(a1)     ; 	move sprite top line by 1
	add.b	#1,2(a1)    ; 	move sprite bottom line by 1


	
	
	jmp 	nosprite

; 	rts                 ; return from subroutine

	even 
ucopper:
	dc.w	$0120,$0000 ; SPR0PTH
	dc.w	$0122,$0000 ; SPR0PTL
	dc.w	$0124,$0000 ; SPR1PTH
	dc.w	$0126,$0000 ; SPR1PTL
	dc.w	$0128,$0000 ; SPR2PTH
	dc.w	$012a,$0000 ; SPR2PTL
	dc.w	$012c,$0000 ; SPR3PTH
	dc.w	$012e,$0000 ; SPR3PTL
	dc.w	$0130,$0000 ; SPR4PTH
	dc.w	$0132,$0000 ; SPR4PTL
	dc.w	$0134,$0000 ; SPR5PTH
	dc.w	$0136,$0000 ; SPR5PTL
	dc.w	$0138,$0000 ; SPR6PTH
	dc.w	$013a,$0000 ; SPR6PTL
	dc.w	$013c,$0000 ; SPR7PTH
	dc.w	$013e,$0000 ; SPR7PTL

	dc.w	$2c01,$fffe
	dc.w	$0100,$1200

ubplcop:
	dc.w	$00e0,$0000 ; BPL1PTH
	dc.w	$00e2,$0000 ; BPL1PTL

	dc.w	$0180,$0000 ; COL	or00 black
	dc.w	$0182,$0ff0 ; COL	or01 yellow
	dc.w	$01a2,$0f00 ; COL	or17 sprite0 red 
	dc.w	$01a4,$0fff ; COL	or18 sprite0 white
	dc.w	$01a6,$000b ; COL	or19 sprite0 blue

	dc.w	$ffdf,$fffe ; wait($df,$ff) enables waits > $ff vertical
	dc.w	$2c01,$fffe ; wait($01,$12c) - $2c is $12c
	dc.w	$0100,$0200 ; BPLCON0 unset bitplanes, enable col	or burst
                    ; needed to supp	ort older PAL chips
	dc.w	$ffff,$fffe ; end of copper

uuscreen:
	blk.b	10240,0 ; allocate 1 kb of mem	ory 	and set it to zero

sprite:
	dc.w	$1e8c,$2c00
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$0300,$FFFF ; %0000 0011 0000 0000, %1111 1111 1111 1111
	dc.w	$FFFF,$FFFF ; %1111 1111 1111 1111, %1111 1111 1111 1111
	dc.w	$FFFF,$FFFF ; %1111 1111 1111 1111, %1111 1111 1111 1111
	dc.w	$0300,$FFFF ; %0000 0011 0000 0000, %1111 1111 1111 1111
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$FB7F,$0780 ; %1111 1011 0111 1111, %0000 0111 1000 0000
	dc.w	$0000,$0000 ; %0000 0000 0000 0000, %0000 0000 0000 0000
	dc.w	$0000,$0000 ; %0000 0000 0000 0000, %0000 0000 0000 0000

blanksprite:
	dc.w	$0000,$0000 ; an empty sprite


nosprite:

	jmp noscreen
; 
; Letter IV mc0402
; https://www.markwrobel.dk/post/amiga-machine-code-letter4-dma-revisited/

	move.w #$01a0,$dff096 ; DMACON disable bitplane, copper, sprite

	move.w #$1200,$dff100 ; BPLCON0 enable 1 bitplane, color burst
	move.w #$0000,$dff102 ; BPLCON1 (scroll)
	move.w #$0000,$dff104 ; BPLCON2 (video)
	move.w #0,$dff108     ; BPL1MOD
	move.w #0,$dff10a     ; BPL2MOD
	move.w #$2c81,$dff08e ; DIWSTRT top right corner ($81,$2c)
	move.w #$f4c1,$dff090 ; DIWSTOP enable PAL trick
	move.w #$38c1,$dff090 ; DIWSTOP buttom left corner ($1c1,$12c)
	move.w #$0038,$dff092 ; DDFSTRT
	move.w #$00d0,$dff094 ; DDFSTOP

	lea.l screen,a1 ; address of screen into a1
	lea.l bplcop,a2 ; address of bplcop into a2
	move.l a1,d1
	move.w d1,6(a2) ; first halve d1 into addr a2 points to + 6 words
	swap d1         ; swap data register halves
	move.w d1,2(a2) ; first halve d1 into addr a2 points to + 2 words

	lea.l tcopper,a1   ; address of copper into a1
	move.l a1,$dff080 ; COP1LCH, move long, no need for COP1LCL

	move.w #$8180,$dff096 ; DMACON enable bitplane, copper

	move.l #0,d0
waitt:

    add.l  #1,d0
	; move.w d0,$DFF180 ; background

	btst #6,$bfe001 ; test left mouse button
	bne waitt        ; if not pressed go to wait

	jmp noscreen
	even
tcopper:
	dc.w $2c01,$fffe ; wait($01,$2c)
	dc.w $0100,$1200 ; move to BPLCON0 enable 1 bitplane, color burst

bplcop:
	dc.w $00e0,$0000 ; move to BPL1PTH
	dc.w $00e2,$0000 ; move to BPL1PTL

	dc.w $0180,$0000 ; move to COLOR00 black
	dc.w $0182,$0ff0 ; move to COLOR01 yellow

	dc.w $ffdf,$fffe ; wait($df,$ff) enable wait > $ff horiz
	dc.w $2c01,$fffe ; wait($01,$12c)
	dc.w $0100,$0200 ; move to BPLCON0 disable bitplane
                 ; needed to support older PAL chips.
	dc.w $ffff,$fffe ; end of copper

screen:
	blk.b 10240,0 ; allocate block of bytes and set to 0
noscreen:

	jmp nocopperx

; Letter III initial bitplane program
start:


	move.w #$01a0,$dff096	; DMACON, disable bitplane, copper, sprite

	move.w  #$1200,$dff100  ; BPLCON0, enable 1 bitplane, enable color
	move.w  #0,$dff102	; BPLCON1 (Scroll)
	move.w  #0,$dff104	; BPLCON2 (Sprites, dual playfields)
	move.w  #0,$dff108	; BPL1MOD (odd planes)
	move.w  #0,$dff10a	; BPL2MOD (even planes)

	move.w  #$2c81,$dff08e	; DIWSTRT
	move.w  #$f4c1,$dff090  ; DIWSTOP (enable PAL trick)
	move.w  #$38c1,$dff090  ; DIWSTOP (PAL trick)
	move.w  #$0038,$dff092  ; DDFSTRT
	move.w  #$00d0,$dff094  ; DDFSTOP

	lea.l   copper,a1
	move.l  a1,$dff080      ; COP1LCH pointet to the copper list

	move.w  #$8180,$dff096  ; DMACON  enable bitplane, enable copper
	move.l #0,d0
wait:
    add.l  #1,d0
	move.w d0,$DFF180 ; background

	btst    #6,$bfe001	; wait for left mouse click
	bne     wait

	jmp nocopperx

copper:
	dc.w    $2c01,$fffe	; wait($01, $2c)
	dc.w    $0100,$1200	; move to $DFF100 BPLCON0, use 1 bitplane, enable color

	dc.w    $00e0,$0000	; move to BPL1PTH, bitplane pointer high
	dc.w    $00e2,$0000	; move to BPL1PTL, bitplane pointer low

	dc.w    $0180,$0000	; move to COLOR00, black
	dc.w    $0182,$0ff0	; move to COLOR01, yellow

	dc.w    $ffdf,$fffe	; wait($df, $ff) - enables waits > $ff vertical
	dc.w    $2c01,$fffe	; wait($01, $2c) - $2c is $12c

	dc.w    $0100,$0200	; move to $DFF100 BPLCON0, disbale bitplanes, enable color
                        ; needed to support older PAL chips.

	dc.w    $ffff,$fffe	; end of copper

nocopperx:


	jmp toxic

initial_fizzle_state = 1
pixel_value = 1

_start:
    move.w #$01a0,$dff096 ; DMACON disable bitplane, copper, sprite

    ; set up 320x256
    move.w #$1200,$dff100 ; BPLCON0 enable 1 bitplane, color burst
    move.w #$0000,$dff102 ; BPLCON1 (scroll)
    move.w #$0000,$dff104 ; BPLCON2 (video)
    move.w #0,$dff108     ; BPL1MOD
    move.w #0,$dff10a     ; BPL2MOD
    move.w #$2c81,$dff08e ; DIWSTRT top right corner ($81,$2c)
    move.w #$f4c1,$dff090 ; DIWSTOP enable PAL trick
    move.w #$38c1,$dff090 ; DIWSTOP buttom left corner ($1c1,$12c)
    move.w #$0038,$dff092 ; DDFSTRT
    move.w #$00d0,$dff094 ; DDFSTOP

    lea.l uscreen,a1 ; address of screen into a1
    lea.l bplcopx,a2 ; address of bplcop into a2
    move.l a1,d1
    move.w d1,6(a2) ; first halve d1 into addr a2 points to + 6 words
    swap d1         ; swap data register halves
    move.w d1,2(a2) ; first halve d1 into addr a2 points to + 2 words

    lea.l copperg,a1   ; address of copper into a1
    move.l a1,$dff080 ; COP1LCH, move long, no need for COP1LCL

    move.w #$8180,$dff096 ; DMACON enable bitplane, copper
    
    move.w #initial_fizzle_state,d0 ; initial fizzle state
    move.w #pixel_value,d2 ; initial pixel value
    lea.l uscreen,a1 ; address of screen into a1
mainx:        
    bsr fizzle
    cmp.l #initial_fizzle_state,d0
    bne do_not_toggle_pixel_value
    eor.b #1,d2
do_not_toggle_pixel_value:    
    btst #6,$bfe001 ; test left mouse button
    bne mainx        ; if not pressed go to wait

exit_main:
    move.w  #$0080,$dff096  ; restablish DMA's and copper
    move.l  $4,a6
    move.l  156(a6),a1
    move.l  38(a1),$dff080
    move.w  #$80a0,$dff096
    rts

; fizzle subroutine
; Fills the screen with pseudo random pixels using LFSR.
; Syntax: (d0=state) = fizzle(d0=state, d2=set_or_clear, a1=screen)
; Arguments: state = state of the LFSR
;            screen = address of the screen buffer 320*256
; Result: d0: state of the LFSR
fizzle:
    movem.l d1/d5/d6,-(a7)
    move.l d0,d5
    and.l  #$1ff00,d0  ; mask x value
    lsr.l  #8,d0

    move.l d5,d1
    and.l #$ff,d1      ; mask y value

    move.l d5,d6    
    lsr.l #1,d5
    btst #0,d6
    beq lsb_is_zero
    eor.l #$12000,d5 ; %0001 0010 000 000 000 - tabs on 17 and 14 length 17
lsb_is_zero:
    cmp.l #320,d0
    bge exit_fizzle
    bsr pixel
exit_fizzle:
    move.l d5,d0
    movem.l (a7)+,d1/d5/d6
    rts

; pixel subroutine
; Draws a pixel on the screeen given an x and y coordinate
; Syntax: pixel(d0=x, d1=y, d2=set_or_clear, a1=screen)
; Arguments: x = The x coordinate
;            y = The y coordinate
;            set_or_clear = If 0 clear pixel, otherwise set pixel
;            screen = address of the screen buffer
pixel:
    movem.l d0-d1/d3-d4,-(a7)
    lsl.w   #3,d1        ; multiply d1 with 8 and store in d1
    move.w  d1,d3        ; move d1 into d3
    lsl.w   #2,d1        ; multiply d1 with 4 and store in d1
    add.w   d3,d1        ; store (y*8)+(y*32)=y*40 in d1
    move.w  d0,d4        ; move d0 into d4
    lsr.w   #3,d0        ; divide d0 with 8 and store in d0
    not.b   d4           ; invert d4
    andi.w  #7,d4        ; keep 3 least significant bits
    add.w   d1,d0        ; add the offsets from x and y 
    tst		d2
    beq     clear_pixel
    bset    d4,(a1,d0.w) ; set the d4'th bit of a1 + d0.w
    bra     cont
clear_pixel:
    bclr    d4,(a1,d0.w) ; set the d4'th bit of a1 + d0.w
cont:
    movem.l (a7)+,d0-d1/d3-d4
    rts                  ; return from subroutine

copperg:
    dc.w $2c01,$fffe ; wait($01,$2c)
    dc.w $0100,$1200 ; move to BPLCON0 enable 1 bitplane, color burst

bplcopx:
    dc.w $00e0,$0000 ; move to BPL1PTH
    dc.w $00e2,$0000 ; move to BPL1PTL

    dc.w $0180,$0000 ; move to COLOR00 black
    dc.w $0182,$0ff0 ; move to COLOR01 yellow

    dc.w $ffdf,$fffe ; wait($df,$ff) enable wait > $ff horiz
    dc.w $2c01,$fffe ; wait($01,$12c)
    dc.w $0100,$0200 ; move to BPLCON0 disable bitplane
                     ; needed to support older PAL chips.
    dc.w $ffff,$fffe ; end of copper

uscreen:
    blk.b 10240,0 ; allocate block of bytes and set to 0

toxic:

; ------------------
; CLEAN SCREEN
; ------------------


	jmp here


; ------------------
; MOUSE
; ------------------
	move.l #0,d3

mains:                 ; just a label
	bsr	mouse         ; 	branch to 	subroutine mouse

	lea.l	mousex,a1     ; move mousex 	address into a1
	lea.l	mousey,a2     ; move mousey 	address into a2

	move.w	(a1),d1       ; move value at mousex 	address into d1
	move.w	(a2),d2       ; move value at mousey 	address into d2

	
	cmp #100,d1
	bcs smaller_t
    add.l  #1,d3
	move.w d3,$DFF180 ; background
smaller_t:

	btst	#6,$bfe001    ; test left mouse button
	bne	mains          ; if not pressed goto main
	
	; rts                   ; return from 	subroutine - exit program

	jmp here

mouse:                    ; 	subroutine (mousex, mousey) = mouse()
	movem.l	d0-d7/a0-a6,-(a7) ; save registers on stack
	move.w	$dff00a,d0        ; move value in JOY0DAT to d0
	andi.l	#255,d0           ; keep lower byte in d0 (mouse x counter) using immidiate AND
	moveq	#0,d2             ; move 0 into d2 (lower bound on x)
	move.l	#639,d3           ; move 639 into d3 (upper bound on x)
	lea.l	oldx,a1           ; move oldx 	address into a1
	lea.l	mousex,a2         ; move mousex 	address into a2
	bsr.s	calcmouse         ; 	branch to 	subroutine calcmouse
	move.w	$dff00a,d0        ; move value in JOY0DAT to d0
	lsr.w	#8,d0             ; shift left 8 bits
	andi.l	#255,d0           ; keep lower byte in d0 (mouse y counter) using immidiate AND 
	moveq	#0,d2             ; move 0 into d2 (lower bound on y)
	move.l	#511,d3           ; move 511 into d3 (upper bound on y)
	lea.l	oldy,a1           ; move 	address of oldy into a1
	lea.l	mousey,a2         ; move 	address of mousey into a2
	bsr.s	calcmouse         ; 	branch to 	subroutine calcmouse
	movem.l	(a7)+,d0-d7/a0-a6 ; load registers from stack
	rts                       ; return from 	subroutine

calcmouse:                ; 	subroutine calcmouse(a1=ol	dcountPtr,a2=newCoordinatePtr,d0=newCount,d2=lowerBound,d3=upperBound)
	moveq	#0,d1             ; move 0 into d1
	move.w	(a1),d1           ; move value from 	address in a1 (ol	dcount) to d1
	move.w	d0,(a1)           ; move d0 (newCount) into 	address pointed to by a1
	move.l	d0,d5             ; move d0 (newCount) into d5
	move.l	d1,d6             ; move d1 (ol	dcount) into d6
	sub.w	d0,d1             ; 	subtract word d0 (newCount) from d1 (ol	dcount) and store result in d1 (countDiff)
	cmp.w	#-128,d1          ; compare -128 with d1 (countDiff)
	blt.s	mc_less           ; if d1 < -128 goto mc_less
	cmp.w	#127,d1           ; compare 127 with d1 (countDiff)
	bgt.s	mc_more           ; if d1 > 127 goto mc_more
	cmp.w	#0,d1             ; compare 0 with d1 (countDiff)
	blt.s	mc_chk2           ; if d1 < 0 goto mc_chk2
mc_chk1:                  ; label
	cmp.w	d5,d6             ; compare d5 (newCount) with d6 (ol	dcount)
	bge.s	mc_chk1ok         ; if d6 > d5 goto mc_chk1ok
	neg.w	d1                ; 	negate d1 (countDiff)
mc_chk1ok:                ; label
	bra.s	mc_storem         ; 	branch always to mc_storem
mc_chk2:                  ; label
	cmp.w	d5,d6             ; compare d5 (newCount) with d6 (ol	dcount)
	ble.s	mc_chk2ok         ; d6 < d5 goto mc_chk2ok
	neg.w	d1                ; 	negate d1 (countDiff)
mc_chk2ok:                ; label
	bra.s	mc_storem         ; 	branch always to mc_storem
mc_less:                  ; label
	add.w	#256,d1           ; 	add 256 to d1 and store in d1 (countDiff)
	bra.s	mc_storem         ; 	branch always to mc_storem
mc_more:                  ; label
	sub.w	#256,d1           ; 	subtract 256 from d1 and store in d1 (countDiff)
mc_storem:                ; label
	neg.w	d1                ; 	negate d1 (countDiff)
	add.w	d1,(a2)           ; 	add d1 (countDiff) to the value pointed to by a2 (newCoordinatePtr)
	move.w	(a2),d0           ; move value from 	address in a2 (newCoordinatePtr) to d0
	cmp.w	d2,d0             ; compare d2 (lowerBound) with d0
	blt.s	mc_toosmall       ; if d0 < d2 goto mc_toosmall
	cmp.w	d3,d0             ; compare d3 (upperBound) with d0
	bgt.s	mc_toolarge       ; if d0 > d3 goto mc_toolarge
	rts                       ; return from 	subroutine
mc_toosmall:              ; label
	move.w	d2,(a2)           ; move value in d2 (lowerBound) to 	address pointed to by a2 (newCoordinatePtr)
	rts                       ; return from 	subroutine
mc_toolarge:              ; label
	move.w	d3,(a2)           ; move value in d3 (upperBound) to 	address pointed to by a2 (newCoordinatePtr)
	rts                       ; return from 	subroutine
oldx:       
	dc.l	$0000             ; allocate space for oldx (mouse x counter)
oldy:       
	dc.l	$0000             ; allocate soace for oldy (mouse y counter)
mousex:       
	dc.w	$0000             ; allocate space for mousex (mouse x coordinate)
mousey:       
	dc.w	$0000   

here:

;======================================================================================================================
; Poo's Journey 
; by Prince / Phaze101
; Based on the BASIC Game for the 64
; by Massimo Bonnucchi
; Written in Amiga 68K to show that till Chapter 7 of the Book you can start writing Games
;======================================================================================================================

	section	GameCode,CODE_P

	
	; rainbow
	jmp norainbow
	move.w    #$4000,$DFF09A
	move.w    #$03A0,$DFF096
loopx:
	move.w    $DFF006,$DFF180 ; background
	btst      #6,$BFE001
	bne.s     loopx
	; move.w	$83A0, $DFF096
	; move.w	$C000, $DFF09A
norainbow:

	jmp nobuff
first:
	move.l	#16,d0
	move.l	#$00,a0
	lea.l	buffer,a1
loop:
	move.b	(a0)+,(a1)+
	; sub.l	#1,d0
	; bne	loop

	dbra	d0,loop

	jmp nobuff
	even
buffer:
	blk.b	16,0
nobuff:

	; bitplanes - what the fuck
	; https://www.markwrobel.dk/post/amiga-machine-code-letter4/

	; high res https://amitopia.com/amiga-pal-high-res-games-is-a-unknown-rarity/#:~:text=Burnout%20is%20one%20of%20very,on%20any%20of%20the%20sides.
	; 640*480 high res ...

	; https://retrocomputing.stackexchange.com/questions/2146/reason-for-the-amiga-clock-speed/2149#2149






;======================================================================================================================
; Startup File
; Makes sure we take control of the OS the proper way
;======================================================================================================================

	include	"lib/startup.s"

;======================================================================================================================
; Which DMAs we will enable
;
; 15	SET/CLR	Set/Clear control bit.
;				Determines if bits written with a 1 get set or cleared
;			    Bits written with a zero are unchanged
; 14	BBUSY	Blitter busy status bit (read only)
; 13	BZERO	Blitter logic zero status bit (read only)
; 12	X	
; 11	X	
; 10	BLTPRI	Blitter DMA priority (also called "blitter nasty" for stealing from CPU)
; 09	DMAEN	Enable all DMA below (also UHRES DMA)
; 08	BPLEN	Bit plane DMA enable
; 07	COPEN	Coprocessor DMA enable
; 06	BLTEN	Blitter DMA enable
; 05	SPREN	Sprite DMA enable
; 04	DSKEN	Disk DMA enable
; 03	AUD3EN	Audio channel 3 DMA enable
; 02	AUD2EN	Audio channel 2 DMA enable
; 01	AUD1EN	Audio channel 1 DMA enable
; 00	AUD0EN	Audio channel 0 DMA enable
;======================================================================================================================

								;5432109876543210
DMASET					equ		%1000001110101111	; Enable Copper, Bitplane DMA, Sprite DMA, Sound DMA

WaitDisk				equ		30		; Defined in Startup.s : Time to wait 50-150 to save/load (50 = 1 second in Pal system)

;======================================================================================================================
; Game Source Files + Data Files
;======================================================================================================================

; Game Constants
		include	"Source/HWConstants.s"
		include	"Source/Constants.s"

; Game Core Files
		include	"Source/Utils.s"
		include	"Source/Player.s"
		include	"Source/Enemies.s"
		include	"Source/GameOver.s"
		include	"Source/Intro.s"
		include	"Source/Hud.s"
		include	"Source/GameCore.s"
		include	"Lib/music.s"

; Game Data Files
		include	"Source/VarTables.s"
		include	"Source/Data.s"





;======================================================================================================================
; Main Program Start
;======================================================================================================================

START:
		;--------------------------------------------------------------------------------------------------------------
		; Saving everything for startup file
		;--------------------------------------------------------------------------------------------------------------

		movem.l	d0-d7/a0-a6,-(SP)

		bsr.w	WaitForRaster

		;--------------------------------------------------------------------------------------------------------------
		; The following is more setup to complete the startup file 
		;--------------------------------------------------------------------------------------------------------------

		; Custom Chips HW Base Address
		lea		$dff000,a5

		; DMA Set / Enable
		move.w	#DMASET,$96(a5)		; DMACON - Enable Bitplane, Copper DMA, Sprties

		; Disable AGA for compatibility
		move.w	#0,$1fc(a5)			; Disable AGA
		move.w	#$c00,$106(a5)		; Disable AGA
		move.w	#$11,$10c(a5)		; Disable AGA

		;--------------------------------------------------------------------------------------------------------------
		; Init Music
		;--------------------------------------------------------------------------------------------------------------

	

		move.l	#GameScreenBitplanes,d0		; Source Image Address that is the address of Bitplane 1
		lea		GameBitplanePointers,a0		; Bitplane Pointers in the Copper
		moveq.l	#NoOfBpls-1,d1				; Number of Bitplanes
		move.l	#BitplaneSize,d2			; Add the bitplane size to the source start Image Address
		bsr.w	SetBitPlanes				; Set the Bitplane Pointers

;		bsr.w	CheckForJoystickFire

		bsr.w	mt_init


		;--------------------------------------------------------------------------------------------------------------
		; Init Main Game Screen
		; This only need to be initialised once
		;--------------------------------------------------------------------------------------------------------------

		bsr.w	InitBitPlanes
		bsr.w	InitColours

		;--------------------------------------------------------------------------------------------------------------
		; Init the Intro Screen
		;--------------------------------------------------------------------------------------------------------------

	.Intro:
		; Init the Intro Screen Bitplanes and Colours
		bsr.w	InitIntroScr  ; wird nachträglich gemacht!
		bsr.w	SetIntroCol	

		;--------------------------------------------------------------------------------------------------------------
		; The following is more setup to complete the startup file 
		;--------------------------------------------------------------------------------------------------------------

		; Custom Chips HW Base Address
		lea		$dff000,a5

		; Init Copper
		move.l	#GenericScreen,$80(a5)	; Point to our copper
		move.w	d0,$88(a5)			; Init our copper

		;--------------------------------------------------------------------------------------------------------------
		; Play Music while we wait for the user to press the fire button
		; and also till the player releases the fire button
		;--------------------------------------------------------------------------------------------------------------

xxx:
  	    BTST   #6,$BFE001      ; Test left mouse button
   		BNE.S   xxx



		jmp flow

				move.l  #5,d2

techno:
		bsr.w	WaitForRaster

		move.l #0,d2
	xyz:
		lea GenericBPL,a3
		; move.l  IntroScreen,a3
;		move.l $000000e4,a3 ; $0000,$00e2
		move.l  #40,d0   ; bytes / 32 bytes (long*8)
	.clear_datax:
		move.l   #1,d2
		move.l   d2,(a3)+
		move.l   d2,(a3)+
		move.l   d2,(a3)+
		move.l   d2,(a3)+
		move.l   d2,(a3)+
		move.l   d2,(a3)+
		move.l   d2,(a3)+
		move.l   d2,(a3)+
;		clr.l    (a3)+
;		clr.l    (a3)+
;		clr.l    (a3)+
;		clr.l    (a3)+
;		clr.l    (a3)+
;		clr.l    (a3)+
;		clr.l    (a3)+
;		clr.l    (a3)+
		dbra d0,.clear_datax

		; rainy boy
; loopxt:

		;jsr ChkKey
		; move.w    d0,$DFF180
		move.w    $DFF006,$DFF180
		;; DFF006  VHPOSR    Read vert and horiz position of beam
		;	DFF180  COLOR00   Color table 0 (background color)

; 		check mouse
;		btst      #6,$BFE001
;		bne.s     loopxt


xhag:
  	    BTST   #6,$BFE001      ; Test left mouse button
   		BNE.S   techno


		flow:



		; bsr.w	CheckForJoystickFire
	.hallo:

  	;   	BTST   #6,$BFE001      ; Test left mouse button
   	;	BNE.S   .hallo


		;--------------------------------------------------------------------------------------------------------------
		; Initialise the Game Over Screen
		;--------------------------------------------------------------------------------------------------------------

		bsr.w	InitGameOverScr
		bsr.w	SetGameOverCol

		;--------------------------------------------------------------------------------------------------------------
		; Game Initialisation
		; It is better to do this before we switch to the Game Copper List
		;--------------------------------------------------------------------------------------------------------------

		bsr.w	WaitForRaster
		
		bsr.w	InitGameVars

		bsr.w	ResetEnemies

		bsr.w	PrintInfo

		bsr.w	InitSprite

		;--------------------------------------------------------------------------------------------------------------
		; The following is more setup to complete the startup file 
		;--------------------------------------------------------------------------------------------------------------

		; Custom Chips HW Base Address
		lea		$dff000,a5

		; Init Copper
		move.l	#CopperList,$80(a5)	; Point to our copper
		move.w	d0,$88(a5)			; Init our copper

		;--------------------------------------------------------------------------------------------------------------
		; Game Initialisation Continue
		;--------------------------------------------------------------------------------------------------------------

		bsr.w	InitRespawnTimer

		move.w	CLXDAT,d0			; Read to be cleared

	; ==================== Game Loop =====================
	.GameLoop:
		bsr.w	WaitForRaster

		bsr.w	UpdateRespawn

		bsr.w	UpdateEnemiesScr

		bsr.w	UpdateLevel

		bsr.w	ReadJoystick

		bsr.w	CheckMiddleFall

		bsr.w	UpdateAnimPlayer

		bsr.w	CheckCollision

		bsr.w	UpdateHUD

		bsr.w	PrintInfo

		bsr.w	DrawText

		bsr.w 	tryout

		bsr.w	UpdateBars

		bsr.w	mt_music


		


		cmp.w	#1,Collision
		bne.s	.GameLoop

	; ================ End Of Game Loop ==================

	.GameOver:

		move.w	#0,Collision

		;--------------------------------------------------------------------------------------------------------------
		; The following is more setup to complete the startup file 
		;--------------------------------------------------------------------------------------------------------------

		; Custom Chips HW Base Address
		lea		$dff000,a5

		; Init Copper
		move.l	#GenericScreen,$80(a5)	; Point to our copper
		move.w	d0,$88(a5)				; Init our copper

		;--------------------------------------------------------------------------------------------------------------
		; Play Music while we wait for the user to press the fire button
		; and also till the player releases the fire button
		;--------------------------------------------------------------------------------------------------------------

		bsr.w	CheckForJoystickFire

		; bra.w	.Intro

		
	; The Game Never arrives here, we never Exit
	.Exit:
		bsr.w	mt_end

		movem.l	(SP)+,d0-d7/a0-a6

		rts

tryout:

		rts 

;======================================================================================================================
; Prints a Character to the screen
;======================================================================================================================

DrawText:
	MOVE.L	StrCodingPtr(PC),A0			; Text Address in a0
	MOVEQ	#0,D2					; Clear d2
	MOVE.B	(A0)+,D2				; Next Char d2
	CMP.B	#$ff,d2					; End of text? ($FF)
	beq.s	XText_Exit				; Yes - Exit
	TST.B	d2						; End of Line? ($00)
	bne.s	XNotEOL					; No Not End Of Line

	ADD.L	#20*7,XBitplanePtr		; Screen is 40 byte wide and the font is 8 bytes high
	ADDQ.L	#1,StrCodingPtr				; Hold the column on the on the screen or where we are on the current line
									; hence reset it to 1 since we are starting a new line
	move.b	(a0)+,d2				; First character on the line skipping 0

XNotEOL:
	SUB.B	#$20,D2					; Subtract 32 from the char ASCII value 
									; so that we can transform for example
									; if it was a space which is $20 to a $00
									; the asterics $21 to $01 etc etc
	LSL.W	#3,D2					; Multiply the same number by 8,
									; since the font is 8 pixels high
	MOVE.L	D2,A2					; move the multiplication result to A2
	ADD.L	#XFont,A2				; Add the value in A2 to the font Base address

	; Display char on bitplanes
	MOVE.L	XBitplanePtr(PC),A3		; Bitplane Address in a3
	MOVE.B	(A2)+,40*0(A3)			; Print Line 1 of char
	MOVE.B	(A2)+,40*1(A3)			; Print Line 2 of char
	MOVE.B	(A2)+,40*2(A3)			; Print Line 3 of char
	MOVE.B	(A2)+,40*3(A3)			; Print Line 4 of char
	MOVE.B	(A2)+,40*4(A3)			; Print Line 5 of char
	MOVE.B	(A2)+,40*5(A3)			; Print Line 6 of char
	MOVE.B	(A2)+,40*6(A3)			; Print Line 7 of char
	MOVE.B	(A2)+,40*7(A3)			; Print Line 8 of char

	ADDQ.L	#1,XBitplanePtr	 		; We need to move 8 bits or 1 byte for next character
	ADDQ.L	#1,StrCodingPtr				; Next character to print

XBitplanePtr:
	dc.l	GameScreenBitplanes

XText_Exit:
	RTS

StrCodingPtr:
	dc.l	StrCoding
StrCoding:
    dc.b    "LA1n PRODUCTION"
    dc.b    $ff

XFont:
	incbin	"gfx/nice.fnt"
	

;======================================================================================================================
;  Chip Data Section
;======================================================================================================================

		include	"Source/C_SpriteStructs.s"
		include	"Source/C_Copper.s"
		include	"Source/C_Data.s"
	

ChkMouse:			;gives border-checked x & y in D0/D1 & [X]/[Y]
	move.w $a-2(a6),d3		;new state
	lea OldMs(PC),a0
	move.w (a0),d2
	move.w d3,(a0)+
	sub.b d2,d3			;X diff
	move.b d3,d0
	ext.w d0
	add.w (a0),d0
	bmi.s .L
	moveq #0,d0
.L:	cmp.w #Wid,d0
	blt.s .R
	move.w #Wid-1,d0
.R:	move.w d0,(a0)+
	lsr.w #8,d2
	lsr.w #8,d3
	sub.b d2,d3			;Y diff
	move.b d3,d1
	ext.w d1
	add.w (a0),d1
	bmi.s .U
	moveq #0,d1
.U:	cmp.w #Hgt,d1
	blt.s .D
	move.w #Hgt-1,d1
.D:	move.w d1,(a0)+
	RTS


ChkKey:		;read ext. ASCII key code-->d0.0=no key!auto-fixes shift etc.
	moveq #0,d0
	lea Key(PC),a1
	btst #3,$1f-2(a6)
	beq.s .End
	lea $bfed01,a0
	btst #3,(a0)
	beq.s .End			;key pressed down?
	clr.b (a0)			;nec?
	moveq #0,d1
	move.b -$100(a0),d1		;read key
	move.b #$50,$100(a0)		;output+force load (start handshake)
	lea 6-2(a6),a2			;<-for spd/Size only
	move.b (a2),d2			;wait for 75 microsecs=60 or so cycles
	not.b d1
	lsr.b #1,d1
	bcc.s .Pres
	moveq #-1,d0			;neg=released!
.Pres:	move.w Shift(PC),d3
	bpl.s .Shift
	move.w Caps(PC),d3
	bmi.s .NoSh
.Shift:	add.w #KeyTblS-KeyTbl,d1
.NoSh:	move.b KeyTbl-Key(a1,d1.w),d0	;fetch ASCII equivalent
	moveq #0,d1
	move.b d0,d1
	bpl.s .WLup1			;special key is neg!
	addq.b #5,d1			;less than -5?
	bmi.s .WLup1			;then no special key
	add.w d1,d1
	move.w d0,2(a1,d1.w)		;put state in corr. key-slot (-=OFF!)
.WLup1:	cmp.b (a2),d2			;QikFix:wait AT LEAST 1 scanline
	beq.s .WLup1
	move.b (a2),d2			;make this into an int later!
.WLup2:	cmp.b (a2),d2
	beq.s .WLup2

	move.b #$10,$100(a0)		;input+force load (handshake done)
	move.w #8,$9c-2(a6)		;clear lev2-intreq
.End:	move.w d0,(a1)
	RTS				;d0/[key]=Ascii key
Key:	dc.w 0
Caps:	dc.w -1
Amiga:	dc.w -1				;special keys state
Alt:	dc.w -1				;-=OFF(!)
Shift:	dc.w -1
Ctrl:	dc.w -1				;DONT SEP ^5!
KeyTbl:					;clr unused keybytes for final opti.
dc.b "`1234567890-=\",0,"0"
dc.b "qwertyuiop[]",0,"123"
dc.b "asdfghjkl;'",0,0,"456"
dc.b 0,"zxcvbnm,./",0,".789"
dc.b " ",8,9,13,13,27,127,0,0,0,"-",0,31,30,29,28	;28-31=cursor keys
dc.b -7,-8,-9,-10,-11,-12,-13,-14,-15,-16,"()/*+",-6	;F-keys,Help
dc.b -2,-2,-5,-1,-3,-3,-4,-4				;Shift,Ctrl,Alt,<A>
dc.b 128,129,130,131,132,133,134,135
dc.b 136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151
KeyTblS:				;SHIFTED
dc.b "~!@#$%^&*()_+|",0,"0"
dc.b "QWERTYUIOP{}",0,"123"
dc.b "ASDFGHJKL:",34,0,0,"456"
dc.b 0,"ZXCVBNM<>?",0,".789"
dc.b " ",8,9,13,13,27,127,0,0,0,"-",0,31,30,29,28	;28-31=cursor keys
dc.b -7,-8,-9,-10,-11,-12,-13,-14,-15,-16,"()/*+",-6	;F-keys,Help
dc.b -2,-2,-5,-1,-3,-3,-4,-4				;Shift,Ctrl,Alt,<A>
dc.b 128,129,130,131,132,133,134,135
dc.b 136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151	





